Class: Minimization::Unidimensional
- Inherits:
-
Object
- Object
- Minimization::Unidimensional
- Defined in:
- lib/minimization.rb
Overview
Base class for unidimensional minimizers
Direct Known Subclasses
Constant Summary collapse
- EPSILON =
Default value for error on f(x)
1e-6
- MAX_ITERATIONS =
Default number of maximum iterations
100
Instance Attribute Summary collapse
-
#epsilon ⇒ Object
Absolute error on x.
-
#expected ⇒ Object
Expected value.
-
#f_minimum ⇒ Object
readonly
Minimum value for f(x).
-
#iterations ⇒ Object
readonly
Numbers of iterations.
-
#log ⇒ Object
readonly
Log of iterations.
-
#log_header ⇒ Object
readonly
Name of fields of log.
-
#x_minimum ⇒ Object
readonly
Minimum value for x.
Class Method Summary collapse
-
.minimize(lower, upper, expected = nil, &block) ⇒ Object
Convenience method to minimize == Parameters: *
lower
: Lower possible value *upper
: Higher possible value *expected
: Optional expected value.
Instance Method Summary collapse
- #f(x) ⇒ Object
-
#initialize(lower, upper, proc) ⇒ Unidimensional
constructor
Create a new minimizer.
-
#iterate ⇒ Object
Iterate to find the minimum.
- #log_summary ⇒ Object
Constructor Details
#initialize(lower, upper, proc) ⇒ Unidimensional
Create a new minimizer
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/minimization.rb', line 45 def initialize(lower, upper, proc) raise "first argument should be lower than second" if lower>=upper @lower=lower @upper=upper @proc=proc golden = 0.3819660; @expected = @lower + golden * (@upper - @lower); @max_iteration=MAX_ITERATIONS @epsilon=EPSILON @iterations=0 @log=[] @log_header=%w{I xl xh f(xl) f(xh) dx df(x)} end |
Instance Attribute Details
#epsilon ⇒ Object
Absolute error on x
39 40 41 |
# File 'lib/minimization.rb', line 39 def epsilon @epsilon end |
#expected ⇒ Object
Expected value. Fast minimum finding if set
41 42 43 |
# File 'lib/minimization.rb', line 41 def expected @expected end |
#f_minimum ⇒ Object (readonly)
Minimum value for f(x)
33 34 35 |
# File 'lib/minimization.rb', line 33 def f_minimum @f_minimum end |
#iterations ⇒ Object (readonly)
Numbers of iterations
43 44 45 |
# File 'lib/minimization.rb', line 43 def iterations @iterations end |
#log ⇒ Object (readonly)
Log of iterations. Should be an array
35 36 37 |
# File 'lib/minimization.rb', line 35 def log @log end |
#log_header ⇒ Object (readonly)
Name of fields of log
37 38 39 |
# File 'lib/minimization.rb', line 37 def log_header @log_header end |
#x_minimum ⇒ Object (readonly)
Minimum value for x
31 32 33 |
# File 'lib/minimization.rb', line 31 def x_minimum @x_minimum end |
Class Method Details
.minimize(lower, upper, expected = nil, &block) ⇒ Object
Convenience method to minimize
Parameters:
-
lower
: Lower possible value -
upper
: Higher possible value -
expected
: Optional expected value. Faster the search is near correct value. -
&block
: Block with function to minimize
Usage:
minimizer=Minimization::GoldenSection.minimize(-1000, 1000) {|x|
x**2 }
75 76 77 78 79 80 |
# File 'lib/minimization.rb', line 75 def self.minimize(lower,upper,expected=nil,&block) minimizer=new(lower,upper,block) minimizer.expected=expected unless expected.nil? raise FailedIteration unless minimizer.iterate minimizer end |
Instance Method Details
#f(x) ⇒ Object
85 86 87 |
# File 'lib/minimization.rb', line 85 def f(x) @proc.call(x) end |
#iterate ⇒ Object
Iterate to find the minimum
82 83 84 |
# File 'lib/minimization.rb', line 82 def iterate raise "You should implement this" end |
#log_summary ⇒ Object
62 63 64 |
# File 'lib/minimization.rb', line 62 def log_summary @log.join("\n") end |