Skip to content

Commit 174f16b

Browse files
committed
seperate classes added for 2 conjugate gradient methods:Polak Ribbiere, Fletcher Reeves
1 parent 944cbb2 commit 174f16b

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

lib/multidim/conjugate_gradient.rb

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@
44

55
module Minimization
66

7-
# = Conjugate Gradient minimizer.
8-
# A multidimensional minimization methods.
9-
# == Usage.
10-
# require 'minimization'
11-
# f = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
12-
# fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
13-
# min = Minimization::NonLinearConjugateGradientMinimizer.new(f, fd, [0, 0, 0], :polak_ribiere)
14-
# while(min.converging?)
15-
# min.minimize
16-
# end
17-
# min.x_minimum
18-
# min.f_minimum
19-
#
7+
# Conjugate Gradient minimizer class
8+
# The beta function may be :fletcher_reeves or :polak_ribiere
209
class NonLinearConjugateGradientMinimizer
2110

2211
attr_reader :x_minimum
@@ -188,4 +177,44 @@ def minimize
188177
end
189178
end
190179
end
180+
181+
182+
# = Conjugate Gradient Fletcher Reeves minimizer.
183+
# A multidimensional minimization methods.
184+
# == Usage.
185+
# require 'minimization'
186+
# f = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
187+
# fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
188+
# min = Minimization::FletcherReeves.new(f, fd, [0, 0, 0])
189+
# while(min.converging?)
190+
# min.minimize
191+
# end
192+
# min.x_minimum
193+
# min.f_minimum
194+
#
195+
class FletcherReeves < NonLinearConjugateGradientMinimizer
196+
def initialize(f, fd, start_point)
197+
super(f, fd, start_point, :fletcher_reeves)
198+
end
199+
end
200+
201+
# = Conjugate Gradient Polak Ribbiere minimizer.
202+
# A multidimensional minimization methods.
203+
# == Usage.
204+
# require 'minimization'
205+
# f = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
206+
# fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
207+
# min = Minimization::PolakRibiere.new(f, fd, [0, 0, 0])
208+
# while(min.converging?)
209+
# min.minimize
210+
# end
211+
# min.x_minimum
212+
# min.f_minimum
213+
#
214+
class PolakRibiere < NonLinearConjugateGradientMinimizer
215+
def initialize(f, fd, start_point)
216+
super(f, fd, start_point, :polak_ribiere)
217+
end
218+
end
219+
191220
end

spec/minimization_conjugate_gradient_fletcher_reeves.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "./../lib/multidim/conjugate_gradient.rb"
22

3-
describe Minimization::NonLinearConjugateGradientMinimizer do
3+
describe Minimization::PolakRibiere do
44

55
before :all do
66
@n = 3
@@ -20,7 +20,7 @@
2020
# fletcher_reeves example 1
2121
f = proc{ |x| (x[0] - @p[0])**2 + (x[1] - @p[1])**2 + (x[2] - @p[2])**2 }
2222
fd = proc{ |x| [ 2 * (x[0] - @p[0]) , 2 * (x[1] - @p[1]) , 2 * (x[2] - @p[2]) ] }
23-
@min1 = Minimization::NonLinearConjugateGradientMinimizer.new(f, fd, @start_point, :fletcher_reeves)
23+
@min1 = Minimization::PolakRibiere.new(f, fd, @start_point)
2424
while(@min1.converging?)
2525
@min1.minimize
2626
end
@@ -34,7 +34,7 @@
3434
r2 = ( @p[0]*x[0] + @p[1]*x[1] + @p[2]*x[2] ) * 2 * @p[2]
3535
[r0, r1, r2]
3636
}
37-
@min2 = Minimization::NonLinearConjugateGradientMinimizer.new(f2, fd2, @start_point, :fletcher_reeves)
37+
@min2 = Minimization::PolakRibiere.new(f2, fd2, @start_point)
3838
while(@min2.converging?)
3939
@min2.minimize
4040
end
@@ -43,7 +43,7 @@
4343
f3 = proc{ |x| ( (x[0] - @p[0])**2 + @k ) }
4444
fd3 = proc{ |x| [ (x[0] - @p[0]) * 2 ] }
4545
starting_point_3 = [rand(@limit)]
46-
@min3 = Minimization::NonLinearConjugateGradientMinimizer.new(f3, fd3, starting_point_3, :fletcher_reeves)
46+
@min3 = Minimization::PolakRibiere.new(f3, fd3, starting_point_3)
4747
while(@min3.converging?)
4848
@min3.minimize
4949
end

spec/minimization_conjugate_gradient_polak_ribiere.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "./../lib/multidim/conjugate_gradient.rb"
22

3-
describe Minimization::NonLinearConjugateGradientMinimizer do
3+
describe Minimization::PolakRibiere do
44

55
before :all do
66
@n = 3
@@ -20,7 +20,7 @@
2020
# example 1
2121
f = proc{ |x| (x[0] - @p[0])**2 + (x[1] - @p[1])**2 + (x[2] - @p[2])**2 }
2222
fd = proc{ |x| [ 2 * (x[0] - @p[0]) , 2 * (x[1] - @p[1]) , 2 * (x[2] - @p[2]) ] }
23-
@min1 = Minimization::NonLinearConjugateGradientMinimizer.new(f, fd, @start_point, :polak_ribiere)
23+
@min1 = Minimization::PolakRibiere.new(f, fd, @start_point)
2424
while(@min1.converging?)
2525
@min1.minimize
2626
end
@@ -30,7 +30,7 @@
3030
f2 = proc{ |x| ( (x[0] - @p[0])**2 + @k ) }
3131
fd2 = proc{ |x| [ (x[0] - @p[0]) * 2 ] }
3232
starting_point_2 = [rand(@limit)]
33-
@min2 = Minimization::NonLinearConjugateGradientMinimizer.new(f2, fd2, starting_point_2, :polak_ribiere)
33+
@min2 = Minimization::PolakRibiere.new(f2, fd2, starting_point_2)
3434
while(@min2.converging?)
3535
@min2.minimize
3636
end

0 commit comments

Comments
 (0)