minimization 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,4 +1,8 @@
1
- === 1.0.0 / 2010-02-24
1
+ === 0.1.1 / 2010-03-19
2
+
3
+ * New Minimization#minimize convenience method
4
+
5
+ === 0.1.0 / 2010-02-24
2
6
 
3
7
  * Golden Section and Brent Algorithm
4
8
 
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ Hoe.spec 'minimization' do
8
8
  self.version=Minimization::VERSION
9
9
  self.rubyforge_name = 'ruby-statsample' # if different than 'minimization'
10
10
  self.developer('Claudio Bustos', 'clbustos_AT_gmail.com')
11
- self.remote_rdoc_dir = 'minimizer'
11
+ self.remote_rdoc_dir = 'minimization'
12
12
  end
13
13
 
14
14
  # vim: syntax=ruby
data/lib/minimization.rb CHANGED
@@ -18,8 +18,10 @@
18
18
  #
19
19
 
20
20
  module Minimization
21
- VERSION="0.1.0"
22
- # Base class for unidimensional minimizers
21
+
22
+ VERSION="0.1.1"
23
+ FailedIteration=Class.new(Exception)
24
+ # Base class for unidimensional minimizers
23
25
  class Unidimensional
24
26
  # Default value for error
25
27
  EPSILON=1e-6
@@ -45,10 +47,20 @@ module Minimization
45
47
  @epsilon=EPSILON
46
48
  @iterations=0
47
49
  @log=""
48
-
49
- end
50
- def f(x)
51
- @proc.call(x)
50
+ end
51
+ # Convenience method to minimize
52
+ # Usage:
53
+ # minimizer=Minimization::GoldenSection.minimize(-1000, 1000) {|x|
54
+ # x**2 }
55
+ #
56
+ def self.minimize(lower,upper,expected=nil,&block)
57
+ minimizer=new(lower,upper,block)
58
+ minimizer.expected=expected unless expected.nil?
59
+ raise FailedIteration unless minimizer.iterate
60
+ minimizer
61
+ end
62
+ def f(x)
63
+ @proc.call(x)
52
64
  end
53
65
  end
54
66
  # = Golden Section Minimizer.
@@ -61,9 +73,9 @@ module Minimization
61
73
  # min.x_minimum
62
74
  # min.f_minimum
63
75
  # min.log
64
- class GoldenSection < Unidimensional
76
+ class GoldenSection < Unidimensional
65
77
  attr_accessor :expected
66
- def initialize(lower,upper, proc)
78
+ def initialize(lower,upper, proc)
67
79
  super
68
80
  end
69
81
  # Start the iteration
@@ -3,16 +3,26 @@ require "test/unit"
3
3
  require "minimization"
4
4
 
5
5
  class TestMinimization < Test::Unit::TestCase
6
+ def setup
7
+ @p1=rand(100)
8
+ @p2=rand(100)
9
+ @func=lambda {|x| (x-@p1)**2+@p2}
10
+ end
11
+ def test_facade
12
+ min=Minimization::GoldenSection.minimize(-1000,1000) {|x| (x-@p1)**2+@p2}
13
+ assert_in_delta(@p1,min.x_minimum, min.epsilon)
14
+ assert_in_delta(@p2,min.f_minimum, min.epsilon)
15
+ end
6
16
  def test_golden
7
- min=Minimization::GoldenSection.new(-1000,1000, proc {|x| (x-10)**2})
17
+ min=Minimization::GoldenSection.new(-1000,1000, @func)
8
18
  min.iterate
9
- assert_in_delta(10, min.x_minimum, min.epsilon)
10
- assert_in_delta(0, min.f_minimum, min.epsilon)
19
+ assert_in_delta(@p1, min.x_minimum, min.epsilon)
20
+ assert_in_delta(@p2, min.f_minimum, min.epsilon)
11
21
  end
12
22
  def test_brent
13
- min=Minimization::Brent.new(-1000,1000, proc {|x| (x-10)**2})
23
+ min=Minimization::Brent.new(-1000,1000, @func)
14
24
  min.iterate
15
- assert_in_delta(10, min.x_minimum, min.epsilon)
16
- assert_in_delta(0, min.f_minimum, min.epsilon)
25
+ assert_in_delta(@p1, min.x_minimum, min.epsilon)
26
+ assert_in_delta(@p2, min.f_minimum, min.epsilon)
17
27
  end
18
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Bustos
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-24 00:00:00 -03:00
12
+ date: 2010-03-19 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency