Class: Minimization::Unidimensional

Inherits:
Object
  • Object
show all
Defined in:
lib/minimization.rb

Overview

Base class for unidimensional minimizers

Direct Known Subclasses

Bisection, Brent, GoldenSection, NewtonRaphson

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

Class Method Summary collapse

Instance Method Summary collapse

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

#epsilonObject

Absolute error on x



39
40
41
# File 'lib/minimization.rb', line 39

def epsilon
  @epsilon
end

#expectedObject

Expected value. Fast minimum finding if set



41
42
43
# File 'lib/minimization.rb', line 41

def expected
  @expected
end

#f_minimumObject (readonly)

Minimum value for f(x)



33
34
35
# File 'lib/minimization.rb', line 33

def f_minimum
  @f_minimum
end

#iterationsObject (readonly)

Numbers of iterations



43
44
45
# File 'lib/minimization.rb', line 43

def iterations
  @iterations
end

#logObject (readonly)

Log of iterations. Should be an array



35
36
37
# File 'lib/minimization.rb', line 35

def log
  @log
end

#log_headerObject (readonly)

Name of fields of log



37
38
39
# File 'lib/minimization.rb', line 37

def log_header
  @log_header
end

#x_minimumObject (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 }

Raises:



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

#iterateObject

Iterate to find the minimum



82
83
84
# File 'lib/minimization.rb', line 82

def iterate
  raise "You should implement this"
end

#log_summaryObject



62
63
64
# File 'lib/minimization.rb', line 62

def log_summary
  @log.join("\n")
end