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
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/minimization.rb', line 53 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
47 48 49 |
# File 'lib/minimization.rb', line 47 def epsilon @epsilon end |
#expected ⇒ Object
Expected value. Fast minimum finding if set
49 50 51 |
# File 'lib/minimization.rb', line 49 def expected @expected end |
#f_minimum ⇒ Object (readonly)
Minimum value for f(x)
41 42 43 |
# File 'lib/minimization.rb', line 41 def f_minimum @f_minimum end |
#iterations ⇒ Object (readonly)
Numbers of iterations
51 52 53 |
# File 'lib/minimization.rb', line 51 def iterations @iterations end |
#log ⇒ Object (readonly)
Log of iterations. Should be an array
43 44 45 |
# File 'lib/minimization.rb', line 43 def log @log end |
#log_header ⇒ Object (readonly)
Name of fields of log
45 46 47 |
# File 'lib/minimization.rb', line 45 def log_header @log_header end |
#x_minimum ⇒ Object (readonly)
Minimum value for x
39 40 41 |
# File 'lib/minimization.rb', line 39 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 }
83 84 85 86 87 88 |
# File 'lib/minimization.rb', line 83 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
93 94 95 |
# File 'lib/minimization.rb', line 93 def f(x) @proc.call(x) end |
#iterate ⇒ Object
Iterate to find the minimum
90 91 92 |
# File 'lib/minimization.rb', line 90 def iterate raise "You should implement this" end |
#log_summary ⇒ Object
70 71 72 |
# File 'lib/minimization.rb', line 70 def log_summary @log.join("\n") end |