File: conjugate_gradient.rb

package info (click to toggle)
ruby-minimization 0.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 1,243; makefile: 3
file content (284 lines) | stat: -rw-r--r-- 9,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# = conjugate_gradient.rb -
# Minimization- Minimization algorithms on pure Ruby
# Copyright (C) 2010 Claudio Bustos
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# This algorith was adopted and ported into Ruby from Apache-commons
# Math library's NonLinearConjugateGradientOptimizer.java file. Therefore this file is under
# Apache License Version 2.
#
# Conjugate Gradient Algorithms for Multidimensional minimization

require "#{File.expand_path(File.dirname(__FILE__))}/point_value_pair.rb"
require "#{File.expand_path(File.dirname(__FILE__))}/../minimization.rb"
require "#{File.expand_path(File.dirname(__FILE__))}/brent_root_finder.rb"

module Minimization

  # Conjugate Gradient minimizer class
  # The beta function may be :fletcher_reeves or :polak_ribiere
  class NonLinearConjugateGradientMinimizer

    attr_reader :x_minimum
    attr_reader :f_minimum
    attr_reader :converging

    attr_accessor :initial_step

    MAX_ITERATIONS_DEFAULT  = 100000
    EPSILON_DEFAULT         = 1e-6

    alias :converging? :converging

    def initialize(f, fd, start_point, beta_formula)
      @epsilon     = EPSILON_DEFAULT
      @safe_min    = 4.503599e15
      @f           = f
      @fd          = fd
      @start_point = start_point

      @max_iterations     = MAX_ITERATIONS_DEFAULT
      @iterations         = 0
      @update_formula     = beta_formula
      @relative_threshold = 100 * @epsilon
      @absolute_threshold = 100 * @safe_min

      @initial_step = 1.0 # initial step default
      @converging   = true

      # do initial steps
      @point = @start_point.clone
      @n      = @point.length
      @r      = gradient(@point)
      0.upto(@n - 1) do |i|
        @r[i] = -@r[i]
      end
      
      # Initial search direction.
      @steepest_descent = precondition(@point, @r)
      @search_direction = @steepest_descent.clone

      @delta = 0
      0.upto(@n - 1) do |i|
          @delta += @r[i] * @search_direction[i]
      end
      @current = nil
    end

    def f(x)
      return @f.call(x)
    end

    def gradient(x)
      return @fd.call(x)
    end

    def find_upper_bound(a, h, search_direction)
      ya   = line_search_func(a, search_direction).to_f
      yb   = ya
      step = h
      # check step value for float max value exceeds
      while step < Float::MAX
        b  = a + step
        yb = line_search_func(b, search_direction).to_f
        if (ya * yb <= 0)
          return b
        end
        step *= [2, ya / yb].max
      end
      # raise error if bracketing failed
      raise "Unable to bracket minimum in line search."
    end

    def precondition(point, r)
      return r.clone # case: identity preconditioner has been used as the default
    end

    def converged(previous, current)
      p          = f(previous)
      c          = f(current)
      difference = (p - c).abs
      size       = [p.abs, c.abs].max
      return ((difference <= size * @relative_threshold) or (difference <= @absolute_threshold))
    end

    # solver to use during line search
    def solve(min, max, start_value, search_direction)
      # check start_value to eliminate unnessasary calculations ...
      func        = proc{|x| line_search_func(x, search_direction)}
      root_finder = Minimization::BrentRootFinder.new(func)
      root        = root_finder.find_root(min, max, func)
      return root
    end

    def line_search_func(x, search_direction)
      # current point in the search direction
      shifted_point = @point.clone
      0.upto(shifted_point.length - 1) do |i|
        shifted_point[i] += x * search_direction[i]
      end

      # gradient of the objective function
      gradient = gradient(shifted_point)

      # dot product with the search direction
      dot_product = 0
      0.upto(gradient.length - 1) do |i|
        dot_product += gradient[i] * search_direction[i]
      end

      return dot_product
    end
    
    # iterate one step of conjugate gradient minimizer
    # == Usage:
    #  f  = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
    #  fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
    #  min = Minimization::FletcherReeves.new(f, fd, [0, 0, 0])
    #  while(min.converging?)
    #    min.iterate
    #  end
    #  min.x_minimum
    #  min.f_minimum
    #
    def iterate
      @iterations  += 1
      @previous     = @current
      @current      = Minimization::PointValuePair.new(@point, f(@point))
      # set converging parameter
      @converging   = !(@previous != nil and converged(@previous.point, @current.point))
      # set results
      @x_minimum    = @current.point
      @f_minimum    = @current.value

      # set search_direction to be used in solve and find_upper_bound methods
      ub   = find_upper_bound(0, @initial_step, @search_direction)
      step = solve(0, ub, 1e-15, @search_direction)

      # Validate new point
      0.upto(@point.length - 1) do |i|
        @point[i] += step * @search_direction[i]
      end

      @r = gradient(@point)
      0.upto(@n - 1) do |i|
        @r[i] = -@r[i]
      end

      # Compute beta
      delta_old            = @delta
      new_steepest_descent = precondition(@point, @r)
      @delta                = 0
      0.upto(@n - 1) do |i|
        @delta += @r[i] * new_steepest_descent[i]
      end

      if (@update_formula == :fletcher_reeves)
        beta = @delta.to_f / delta_old
      elsif(@update_formula == :polak_ribiere)
        deltaMid = 0
        0.upto(@r.length - 1) do |i|
          deltaMid += @r[i] * @steepest_descent[i]
        end
        beta = (@delta - deltaMid).to_f / delta_old
      else
        raise "Unknown beta formula type"
      end
      @steepest_descent = new_steepest_descent

      # Compute conjugate search direction
      if ((@iterations % @n == 0) or (beta < 0))
        # Break conjugation: reset search direction
        @search_direction = @steepest_descent.clone
      else
        # Compute new conjugate search direction
        0.upto(@n - 1) do |i|
          @search_direction[i] = @steepest_descent[i] + beta * @search_direction[i]
        end
      end
    end
  end
  
  
  # = Conjugate Gradient Fletcher Reeves minimizer.
  # A multidimensional minimization methods.
  # == Usage.
  #  require 'minimization'
  #  f  = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
  #  fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
  #  min = Minimization::FletcherReeves.minimize(f, fd, [0, 0, 0])
  #  min.x_minimum
  #  min.f_minimum
  #
  class FletcherReeves < NonLinearConjugateGradientMinimizer
    def initialize(f, fd, start_point)
      super(f, fd, start_point, :fletcher_reeves)
    end

    # Convenience method to minimize using Fletcher Reeves method
    # == Parameters:
    # * <tt>f</tt>: Function to minimize
    # * <tt>fd</tt>: First derivative of f
    # * <tt>start_point</tt>: Starting point
    # == Usage:
    #  f  = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
    #  fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
    #  min = Minimization::FletcherReeves.minimize(f, fd, [0, 0, 0])
    #
    def self.minimize(f, fd, start_point)
      min = Minimization::FletcherReeves.new(f, fd, start_point)
      while(min.converging?)
        min.iterate
      end
      return min
    end
  end

  # = Conjugate Gradient Polak Ribbiere minimizer.
  # A multidimensional minimization methods.
  # == Usage.
  #  require 'minimization'
  #  f  = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
  #  fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
  #  min = Minimization::PolakRibiere.minimize(f, fd, [0, 0, 0])
  #  min.x_minimum
  #  min.f_minimum
  #
  class PolakRibiere < NonLinearConjugateGradientMinimizer
    def initialize(f, fd, start_point)
      super(f, fd, start_point, :polak_ribiere)
    end

    # Convenience method to minimize using Polak Ribiere method
    # == Parameters:
    # * <tt>f</tt>: Function to minimize
    # * <tt>fd</tt>: First derivative of f
    # * <tt>start_point</tt>: Starting point
    # == Usage:
    #  f  = proc{ |x| (x[0] - 2)**2 + (x[1] - 5)**2 + (x[2] - 100)**2 }
    #  fd = proc{ |x| [ 2 * (x[0] - 2) , 2 * (x[1] - 5) , 2 * (x[2] - 100) ] }
    #  min = Minimization::PolakRibiere.minimize(f, fd, [0, 0, 0])
    #
    def self.minimize(f, fd, start_point)
      min = Minimization::PolakRibiere.new(f, fd, start_point)
      while(min.converging?)
        min.iterate
      end
      return min
    end
  end

end