minimization 0.2.3 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.summary = "A suite for minimization in Ruby"
22
22
 
23
23
  s.add_runtime_dependency 'text-table', '~> 1.2'
24
-
24
+
25
25
  s.add_development_dependency 'rake', '~> 10'
26
26
  s.add_development_dependency 'bundler', '~> 1.3'
27
27
  s.add_development_dependency 'rspec', '~> 3.2'
@@ -0,0 +1,68 @@
1
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/multidim/conjugate_gradient.rb"
2
+
3
+ describe Minimization::FletcherReeves do
4
+
5
+ before :all do
6
+ @n = 3
7
+ @limit = 100
8
+ @epsilon = 1e-6
9
+ @p = Array.new(@n)
10
+ @start_point = Array.new(@n)
11
+
12
+ 0.upto(@n - 1) do |i|
13
+ @p[i] = rand(@limit)
14
+ end
15
+
16
+ 0.upto(@n - 1) do |i|
17
+ @start_point[i] = rand(@limit)
18
+ end
19
+
20
+ # fletcher_reeves example 1
21
+ puts @p.inspect
22
+ f = proc{ |x| (x[0] - @p[0])**2 + (x[1] - @p[1])**2 + (x[2] - @p[2])**2 }
23
+ fd = proc{ |x| [ 2 * (x[0] - @p[0]) , 2 * (x[1] - @p[1]) , 2 * (x[2] - @p[2]) ] }
24
+ @min1 = Minimization::FletcherReeves.minimize(f, fd, @start_point)
25
+
26
+ # fletcher_reeves example 2
27
+ @k = rand(@limit)
28
+ f2 = proc{ |x| ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] )**2 + @k}
29
+ fd2 = proc{ |x|
30
+ r0 = ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] ) * 2 * @p[0]
31
+ r1 = ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] ) * 2 * @p[1]
32
+ r2 = ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] ) * 2 * @p[2]
33
+ [r0, r1, r2]
34
+ }
35
+ @min2 = Minimization::FletcherReeves.minimize(f2, fd2, @start_point)
36
+
37
+ # fletcher_reeves example 3 : unidimensional
38
+ f3 = proc{ |x| ( (x[0] - @p[0])**2 + @k ) }
39
+ fd3 = proc{ |x| [ (x[0] - @p[0]) * 2 ] }
40
+ starting_point_3 = [rand(@limit)]
41
+ @min3 = Minimization::FletcherReeves.minimize(f3, fd3, starting_point_3)
42
+
43
+ end
44
+
45
+ it "#x_minimum be close to expected in example 1 :fletcher_reeves" do
46
+ 0.upto(@n - 1) do |i|
47
+ expect(@min1.x_minimum[i]).to be_within(@epsilon).of(@p[i])
48
+ end
49
+ end
50
+
51
+ it "#f_minimum be close to expected in example 1 :fletcher_reeves" do
52
+ expect(@min1.f_minimum).to be_within(@epsilon).of(0)
53
+ end
54
+
55
+ it "#f_minimum be close to expected in example 2 :fletcher_reeves" do
56
+ expect(@min2.f_minimum).to be_within(@epsilon).of(@k)
57
+ end
58
+
59
+ it "#x_minimum be close to expected in example 3 :fletcher_reeves" do
60
+ expect(@min3.x_minimum[0]).to be_within(@epsilon).of(@p[0])
61
+ end
62
+
63
+ it "#f_minimum be close to expected in example 3 :fletcher_reeves" do
64
+ expect(@min3.f_minimum).to be_within(@epsilon).of(@k)
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,54 @@
1
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/multidim/conjugate_gradient.rb"
2
+
3
+ describe Minimization::PolakRibiere do
4
+
5
+ before :all do
6
+ @n = 3
7
+ @limit = 100
8
+ @epsilon = 1e-6
9
+ @p = Array.new(@n)
10
+ @start_point = Array.new(@n)
11
+
12
+ 0.upto(@n - 1) do |i|
13
+ @p[i] = rand(@limit)
14
+ end
15
+
16
+ 0.upto(@n - 1) do |i|
17
+ @start_point[i] = rand(@limit)
18
+ end
19
+
20
+ # example 1
21
+ f = proc{ |x| (x[0] - @p[0])**2 + (x[1] - @p[1])**2 + (x[2] - @p[2])**2 }
22
+ fd = proc{ |x| [ 2 * (x[0] - @p[0]) , 2 * (x[1] - @p[1]) , 2 * (x[2] - @p[2]) ] }
23
+ @min1 = Minimization::PolakRibiere.minimize(f, fd, @start_point)
24
+
25
+ # example 2 : unidimensional
26
+ @k = rand(@limit)
27
+ f2 = proc{ |x| ( (x[0] - @p[0])**2 + @k ) }
28
+ fd2 = proc{ |x| [ (x[0] - @p[0]) * 2 ] }
29
+ starting_point_2 = [rand(@limit)]
30
+ @min2 = Minimization::PolakRibiere.minimize(f2, fd2, starting_point_2)
31
+
32
+ end
33
+
34
+ it "#x_minimum be close to expected in example 1 :polak_ribiere" do
35
+ 0.upto(@n - 1) do |i|
36
+ expect(@min1.x_minimum[i]).to be_within(@epsilon).of(@p[i])
37
+ end
38
+ end
39
+
40
+ it "#f_minimum be close to expected in example 1 :polak_ribiere" do
41
+ expect(@min1.f_minimum).to be_within(@epsilon).of(0)
42
+ end
43
+
44
+ it "#x_minimum be close to expected in example 2 :polak_ribiere" do
45
+ expect(@min2.x_minimum[0]).to be_within(@epsilon).of(@p[0])
46
+ end
47
+
48
+ it "#f_minimum be close to expected in example 2 :polak_ribiere" do
49
+ expect(@min2.f_minimum).to be_within(@epsilon).of(@k)
50
+ end
51
+
52
+ end
53
+
54
+
@@ -0,0 +1,57 @@
1
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/multidim/nelder_mead.rb"
2
+
3
+ describe Minimization::NelderMead do
4
+ before :all do
5
+ @n = 3
6
+ @limit = 100
7
+ @epsilon = 1e-6
8
+ @p = Array.new(@n)
9
+ @start_point = Array.new(@n)
10
+
11
+ 0.upto(@n - 1) do |i|
12
+ @p[i] = rand(@limit)
13
+ end
14
+
15
+ 0.upto(@n - 1) do |i|
16
+ @start_point[i] = rand(@limit)
17
+ end
18
+
19
+ # example 1
20
+ f = proc{ |x| (x[0] - @p[0])**2 + (x[1] - @p[1])**2 + (x[2] - @p[2])**2 }
21
+ @min1 = Minimization::NelderMead.minimize(f, @start_point)
22
+
23
+ # example 2
24
+ @k = rand(@limit)
25
+ f2 = proc{ |x| ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] )**2 + @k}
26
+ @min2 = Minimization::NelderMead.minimize(f2, @start_point)
27
+
28
+ # example 3 : unidimensional
29
+ f3 = proc{ |x| (x[0] - @p[0])**2 + @k}
30
+ @min3 = Minimization::NelderMead.minimize(f3, [@k])
31
+ end
32
+
33
+ it "#x_minimum be close to expected in example 1" do
34
+ 0.upto(@n - 1) do |i|
35
+ expect(@min1.x_minimum[i]).to be_within(@epsilon).of(@p[i])
36
+ end
37
+ end
38
+
39
+ it "#f_minimum be close to expected in example 1" do
40
+ expect(@min1.f_minimum).to be_within(@epsilon).of(0)
41
+ end
42
+
43
+ it "#f_minimum be close to expected in example 2" do
44
+ expect(@min2.f_minimum).to be_within(@epsilon).of(@k)
45
+ end
46
+
47
+ it "#x_minimum be close to expected in example 3" do
48
+ expect(@min3.x_minimum[0]).to be_within(@epsilon).of(@p[0])
49
+ end
50
+
51
+ it "#f_minimum be close to expected in example 3" do
52
+ expect(@min3.f_minimum).to be_within(@epsilon).of(@k)
53
+ end
54
+
55
+ end
56
+
57
+
@@ -0,0 +1,57 @@
1
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/multidim/powell.rb"
2
+
3
+ describe Minimization::Powell do
4
+ before :all do
5
+ @n = 3
6
+ @limit = 100
7
+ @epsilon = 1e-5
8
+ @p = Array.new(@n)
9
+ @start_point = Array.new(@n)
10
+
11
+ 0.upto(@n - 1) do |i|
12
+ @p[i] = rand(@limit)
13
+ end
14
+
15
+ 0.upto(@n - 1) do |i|
16
+ @start_point[i] = rand(@limit)
17
+ end
18
+
19
+ # example 1
20
+ f = proc{ |x| (x[0] - @p[0])**2 + (x[1] - @p[1])**2 + (x[2] - @p[2])**2 }
21
+ @min1 = Minimization::Powell.minimize(f, @start_point, [-@limit, -@limit, -@limit], [@limit, @limit, @limit])
22
+
23
+ # example 2
24
+ @k = rand(@limit)
25
+ f2 = proc{ |x| ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] )**2 + @k}
26
+ @min2 = Minimization::Powell.minimize(f2, @start_point, [-@limit, -@limit, -@limit], [@limit, @limit, @limit])
27
+
28
+ # example 3 : unidimensional
29
+ f3 = proc{ |x| (x[0] - @p[0])**2 + @k}
30
+ @min3 = Minimization::Powell.minimize(f3, @start_point, [-@limit, -@limit, -@limit], [@limit, @limit, @limit])
31
+
32
+ end
33
+
34
+ it "#x_minimum be close to expected in example 1" do
35
+ 0.upto(@n - 1) do |i|
36
+ expect(@min1.x_minimum[i]).to be_within(@epsilon).of(@p[i])
37
+ end
38
+ end
39
+
40
+ it "#f_minimum be close to expected in example 1" do
41
+ expect(@min1.f_minimum).to be_within(@epsilon).of(0)
42
+ end
43
+
44
+ it "#f_minimum be close to expected in example 2" do
45
+ expect(@min2.f_minimum).to be_within(@epsilon).of(@k)
46
+ end
47
+
48
+ it "#x_minimum be close to expected in example 3" do
49
+ expect(@min3.x_minimum[0]).to be_within(@epsilon).of(@p[0])
50
+ end
51
+
52
+ it "#f_minimum be close to expected in example 3" do
53
+ expect(@min3.f_minimum).to be_within(@epsilon).of(@k)
54
+ end
55
+
56
+ end
57
+
@@ -2,11 +2,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'minimization.rb'
4
4
  require 'rspec'
5
- require 'rspec/autorun'
6
5
 
7
- RSpec.configure do |config|
8
-
6
+ RSpec.configure do |config|
7
+ config.expect_with :rspec do |c|
8
+ c.syntax = [:should, :expect]
9
+ end
9
10
  end
11
+
10
12
 
11
13
  class String
12
14
  def deindent
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.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Bustos
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-27 00:00:00.000000000 Z
12
+ date: 2018-12-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: text-table
@@ -78,13 +78,24 @@ files:
78
78
  - ".gitignore"
79
79
  - ".travis.yml"
80
80
  - Gemfile
81
+ - Gemfile.lock
81
82
  - History.txt
82
83
  - LICENSE.txt
84
+ - Manifest.txt
83
85
  - README.md
84
86
  - Rakefile
85
87
  - lib/minimization.rb
86
88
  - lib/minimization/version.rb
89
+ - lib/multidim/brent_root_finder.rb
90
+ - lib/multidim/conjugate_gradient.rb
91
+ - lib/multidim/nelder_mead.rb
92
+ - lib/multidim/point_value_pair.rb
93
+ - lib/multidim/powell.rb
87
94
  - minimization.gemspec
95
+ - spec/minimization_conjugate_gradient_fletcher_reeves_spec.rb
96
+ - spec/minimization_conjugate_gradient_polak_ribiere_spec.rb
97
+ - spec/minimization_nelder_mead_spec.rb
98
+ - spec/minimization_powell_spec_spec.rb
88
99
  - spec/minimization_unidimensional_spec.rb
89
100
  - spec/spec.opts
90
101
  - spec/spec_helper.rb
@@ -106,12 +117,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
117
  - !ruby/object:Gem::Version
107
118
  version: '0'
108
119
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.4.5
120
+ rubygems_version: 3.0.1
111
121
  signing_key:
112
122
  specification_version: 4
113
123
  summary: A suite for minimization in Ruby
114
- test_files:
115
- - spec/minimization_unidimensional_spec.rb
116
- - spec/spec.opts
117
- - spec/spec_helper.rb
124
+ test_files: []