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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
# = minimization.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.
#
# Algorithms for unidimensional minimization
require 'text-table'
module Minimization
VERSION="0.2.1"
FailedIteration=Class.new(Exception)
# Base class for unidimensional minimizers
class Unidimensional
# Default value for error on f(x)
EPSILON=1e-6
# Default number of maximum iterations
MAX_ITERATIONS=100
# Minimum value for x
attr_reader :x_minimum
# Minimum value for f(x)
attr_reader :f_minimum
# Log of iterations. Should be an array
attr_reader :log
# Name of fields of log
attr_reader :log_header
# Absolute error on x
attr_accessor :epsilon
# Expected value. Fast minimum finding if set
attr_reader :expected
# Numbers of iterations
attr_reader :iterations
# Create a new minimizer
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
# Set expected value
def expected=(v)
@expected=v
end
def log_summary
@log.join("\n")
end
# Convenience method to minimize
# == Parameters:
# * <tt>lower</tt>: Lower possible value
# * <tt>upper</tt>: Higher possible value
# * <tt>expected</tt>: Optional expected value. Faster the search is near correct value.
# * <tt>&block</tt>: Block with function to minimize
# == Usage:
# minimizer=Minimization::GoldenSection.minimize(-1000, 1000) {|x|
# x**2 }
#
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
# Iterate to find the minimum
def iterate
raise "You should implement this"
end
def f(x)
@proc.call(x)
end
end
# Classic Newton-Raphson minimization method.
# Requires first and second derivative
# == Usage
# f = lambda {|x| x**2}
# fd = lambda {|x| 2x}
# fdd = lambda {|x| 2}
# min = Minimization::NewtonRaphson.new(-1000,1000, f,fd,fdd)
# min.iterate
# min.x_minimum
# min.f_minimum
#
class NewtonRaphson < Unidimensional
# == Parameters:
# * <tt>lower</tt>: Lower possible value
# * <tt>upper</tt>: Higher possible value
# * <tt>proc</tt>: Original function
# * <tt>proc_1d</tt>: First derivative
# * <tt>proc_2d</tt>: Second derivative
#
def initialize(lower, upper, proc, proc_1d, proc_2d)
super(lower,upper,proc)
@proc_1d=proc_1d
@proc_2d=proc_2d
end
# Raises an error
def self.minimize(*args)
raise "You should use #new and #iterate"
end
def iterate
# First
x_prev=@lower
x=@expected
failed=true
k=0
while (x-x_prev).abs > @epsilon and k<@max_iteration
k+=1
x_prev=x
x=x-(@proc_1d.call(x).quo(@proc_2d.call(x)))
f_prev=f(x_prev)
f=f(x)
x_min,x_max=[x,x_prev].min, [x,x_prev].max
f_min,f_max=[f,f_prev].min, [f,f_prev].max
@log << [k, x_min, x_max, f_min, f_max, (x_prev-x).abs, (f-f_prev).abs]
end
raise FailedIteration, "Not converged" if k>=@max_iteration
@x_minimum = x;
@f_minimum = f(x);
end
end
# = Golden Section Minimizer.
# Basic minimization algorithm. Slow, but robust.
# See Unidimensional for methods.
# == Usage.
# require 'minimization'
# min=Minimization::GoldenSection.new(-1000,20000 , proc {|x| (x+1)**2}
# min.expected=1.5 # Expected value
# min.iterate
# min.x_minimum
# min.f_minimum
# min.log
class GoldenSection < Unidimensional
# Start the iteration
def iterate
ax=@lower
bx=@expected
cx=@upper
c = (3-Math::sqrt(5)).quo(2);
r = 1-c;
x0 = ax;
x3 = cx;
if ((cx-bx).abs > (bx-ax).abs)
x1 = bx;
x2 = bx + c*(cx-bx);
else
x2 = bx;
x1 = bx - c*(bx-ax);
end
f1 = f(x1);
f2 = f(x2);
k = 1;
while (x3-x0).abs > @epsilon and k<@max_iteration
if f2 < f1
x0 = x1;
x1 = x2;
x2 = r*x1 + c*x3; # x2 = x1+c*(x3-x1)
f1 = f2;
f2 = f(x2);
else
x3 = x2;
x2 = x1;
x1 = r*x2 + c*x0; # x1 = x2+c*(x0-x2)
f2 = f1;
f1 = f(x1);
end
@log << [k, x3,x0, f1,f2,(x3-x0).abs, (f1-f2).abs]
k +=1;
end
if f1 < f2
@x_minimum = x1;
@f_minimum = f1;
else
@x_minimum = x2;
@f_minimum = f2;
end
true
end
end
# Direct port of Brent algorithm found on GSL.
# See Unidimensional for methods.
# == Usage
# min=Minimization::Brent.new(-1000,20000 , proc {|x| (x+1)**2}
# min.expected=1.5 # Expected value
# min.iterate
# min.x_minimum
# min.f_minimum
# min.log
class Brent < Unidimensional
GSL_SQRT_DBL_EPSILON=1.4901161193847656e-08
def initialize(lower,upper, proc)
super
@do_bracketing=true
# Init
golden = 0.3819660; #golden = (3 - sqrt(5))/2
v = @lower + golden * (@upper - @lower);
w = v;
@x_minimum = v ;
@f_minimum = f(v) ;
@x_lower=@lower
@x_upper=@upper
@f_lower = f(@lower) ;
@f_upper = f(@lower) ;
@v = v;
@w = w;
@d = 0;
@e = 0;
@f_v=f(v)
@f_w=@f_v
end
def expected=(v)
@x_minimum=v
@f_minimum=f(v)
@do_bracketing=false
end
def bracketing
eval_max=10
f_left = @f_lower;
f_right = @f_upper;
x_left = @x_lower;
x_right= @x_upper;
golden = 0.3819660; # golden = (3 - sqrt(5))/2 */
nb_eval=0
if (f_right >= f_left)
x_center = (x_right - x_left) * golden + x_left;
nb_eval+=1;
f_center=f(x_center)
else
x_center = x_right ;
f_center = f_right ;
x_right = (x_center - x_left).quo(golden) + x_left;
nb_eval+=1;
f_right=f(x_right);
end
begin
@log << ["B#{nb_eval}", x_left, x_right, f_left, f_right, (x_left-x_right).abs, (f_left-f_right).abs]
if (f_center < f_left )
if (f_center < f_right)
@x_lower = x_left;
@x_upper = x_right;
@x_minimum = x_center;
@f_lower = f_left;
@f_upper = f_right;
@f_minimum = f_center;
return true;
elsif (f_center > f_right)
x_left = x_center;
f_left = f_center;
x_center = x_right;
f_center = f_right;
x_right = (x_center - x_left).quo(golden) + x_left;
nb_eval+=1;
f_right=f(x_right);
else # f_center == f_right */
x_right = x_center;
f_right = f_center;
x_center = (x_right - x_left).quo(golden) + x_left;
nb_eval+=1;
f_center=f(x_center);
end
else # f_center >= f_left */
x_right = x_center;
f_right = f_center;
x_center = (x_right - x_left) * golden + x_left;
nb_eval+=1;
f_center=f(x_center);
end
end while ((nb_eval < eval_max) and
((x_right - x_left) > GSL_SQRT_DBL_EPSILON * ( (x_right + x_left) * 0.5 ) + GSL_SQRT_DBL_EPSILON))
@x_lower = x_left;
@x_upper = x_right;
@x_minimum = x_center;
@f_lower = f_left;
@f_upper = f_right;
@f_minimum = f_center;
return false;
end
# Start the minimization process
# If you want to control manually the process, use brent_iterate
def iterate
k=0
bracketing if @do_bracketing
while k<@max_iteration and (@x_lower-@x_upper).abs>@epsilon
k+=1
result=brent_iterate
raise FailedIteration,"Error on iteration" if !result
begin
@log << [k, @x_lower, @x_upper, @f_lower, @f_upper, (@x_lower-@x_upper).abs, (@f_lower-@f_upper).abs]
rescue =>@e
@log << [k, @e.to_s,nil,nil,nil,nil,nil]
end
end
@iterations=k
return true
end
# Generate one iteration.
def brent_iterate
x_left = @x_lower;
x_right = @x_upper;
z = @x_minimum;
d = @e;
e = @d;
v = @v;
w = @w;
f_v = @f_v;
f_w = @f_w;
f_z = @f_minimum;
golden = 0.3819660; # golden = (3 - sqrt(5))/2 */
w_lower = (z - x_left)
w_upper = (x_right - z)
tolerance = GSL_SQRT_DBL_EPSILON * z.abs
midpoint = 0.5 * (x_left + x_right)
_p,q,r=0,0,0
if (e.abs > tolerance)
# fit parabola */
r = (z - w) * (f_z - f_v);
q = (z - v) * (f_z - f_w);
_p = (z - v) * q - (z - w) * r;
q = 2 * (q - r);
if (q > 0)
_p = -_p
else
q = -q;
end
r = e;
e = d;
end
if (_p.abs < (0.5 * q * r).abs and _p < q * w_lower and _p < q * w_upper)
t2 = 2 * tolerance ;
d = _p.quo(q);
u = z + d;
if ((u - x_left) < t2 or (x_right - u) < t2)
d = (z < midpoint) ? tolerance : -tolerance ;
end
else
e = (z < midpoint) ? x_right - z : -(z - x_left) ;
d = golden * e;
end
if ( d.abs >= tolerance)
u = z + d;
else
u = z + ((d > 0) ? tolerance : -tolerance) ;
end
@e = e;
@d = d;
f_u=f(u)
if (f_u <= f_z)
if (u < z)
@x_upper = z;
@f_upper = f_z;
else
@x_lower = z;
@f_lower = f_z;
end
@v = w;
@f_v = f_w;
@w = z;
@f_w = f_z;
@x_minimum = u;
@f_minimum = f_u;
return true;
else
if (u < z)
@x_lower = u;
@f_lower = f_u;
return true;
else
@x_upper = u;
@f_upper = f_u;
return true;
end
if (f_u <= f_w or w == z)
@v = w;
@f_v = f_w;
@w = u;
@f_w = f_u;
return true;
elsif f_u <= f_v or v == z or v == w
@v = u;
@f_v = f_u;
return true;
end
end
return false
end
end
end
|