/usr/lib/aster/Utilitai/optimize.py is in code-aster 11.5.0+dfsg2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | # coding=utf-8
# CONFIGURATION MANAGEMENT OF EDF VERSION
# ======================================================================
# COPYRIGHT (C) 1991 - 2006 EDF R&D WWW.CODE-ASTER.ORG
# 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 EDF R&D CODE_ASTER,
# 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
# ======================================================================
# person_in_charge: aimery.assire at edf.fr
#
# ******NOTICE***************
# optimize.py module by Travis E. Oliphant
#
# You may copy and use this module as you see fit with no
# guarantee implied provided you keep this notice in all copies.
# *****END NOTICE************
# A collection of optimization algorithms. Version 0.3.1
# Minimization routines
"""optimize.py
A collection of general-purpose optimization routines using Numeric
fmin --- Nelder-Mead Simplex algorithm (uses only function calls)
fminBFGS --- Quasi-Newton method (uses function and gradient)
fminNCG --- Line-search Newton Conjugate Gradient (uses function, gradient
and hessian (if it's provided))
"""
import numpy as Num
max = Num.max
min = Num.min
abs = Num.absolute
__version__="0.3.1"
def rosen(x): # The Rosenbrock function
return Num.sum(100.0*(x[1:len(x)]-x[0:-1]**2.0)**2.0 + (1-x[0:-1])**2.0)
def rosen_der(x):
xm = x[1:-1]
xm_m1 = x[0:-2]
xm_p1 = x[2:len(x)]
der = Num.zeros(x.shape,x.dtype.char)
der[1:-1] = 200*(xm-xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1-xm)
der[0] = -400*x[0]*(x[1]-x[0]**2) - 2*(1-x[0])
der[-1] = 200*(x[-1]-x[-2]**2)
return der
def rosen3_hess_p(x,p):
assert(len(x)==3)
assert(len(p)==3)
hessp = Num.zeros((3,),x.dtype.char)
hessp[0] = (2 + 800*x[0]**2 - 400*(-x[0]**2 + x[1])) * p[0] \
- 400*x[0]*p[1] \
+ 0
hessp[1] = - 400*x[0]*p[0] \
+ (202 + 800*x[1]**2 - 400*(-x[1]**2 + x[2]))*p[1] \
- 400*x[1] * p[2]
hessp[2] = 0 \
- 400*x[1] * p[1] \
+ 200 * p[2]
return hessp
def rosen3_hess(x):
assert(len(x)==3)
hessp = Num.zeros((3,3),x.dtype.char)
hessp[0,:] = [2 + 800*x[0]**2 -400*(-x[0]**2 + x[1]), -400*x[0], 0]
hessp[1,:] = [-400*x[0], 202+800*x[1]**2 -400*(-x[1]**2 + x[2]), -400*x[1]]
hessp[2,:] = [0,-400*x[1], 200]
return hessp
def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None, fulloutput=0, printmessg=1):
"""xopt,{fval,warnflag} = fmin(function, x0, args=(), xtol=1e-4, ftol=1e-4,
maxiter=200*len(x0), maxfun=200*len(x0), fulloutput=0, printmessg=0)
Uses a Nelder-Mead Simplex algorithm to find the minimum of function
of one or more variables.
"""
x0 = Num.asarray(x0)
assert (len(x0.shape)==1)
N = len(x0)
if maxiter is None:
maxiter = N * 200
if maxfun is None:
maxfun = N * 200
rho = 1; chi = 2; psi = 0.5; sigma = 0.5;
one2np1 = range(1,N+1)
sim = Num.zeros((N+1,N),x0.dtype.char)
fsim = Num.zeros((N+1,),'d')
sim[0] = x0
fsim[0] = apply(func,(x0,)+args)
nonzdelt = 0.05
zdelt = 0.00025
for k in range(0,N):
y = Num.array(x0,copy=1)
if y[k] != 0:
y[k] = (1+nonzdelt)*y[k]
else:
y[k] = zdelt
sim[k+1] = y
f = apply(func,(y,)+args)
fsim[k+1] = f
ind = Num.argsort(fsim)
fsim = Num.take(fsim,ind) # sort so sim[0,:] has the lowest function value
sim = Num.take(sim,ind,0)
iterations = 1
funcalls = N+1
while (funcalls < maxfun and iterations < maxiter):
if (max(Num.ravel(abs(sim[1:len(sim)]-sim[0]))) <= xtol \
and max(abs(fsim[0]-fsim[1:len(fsim)])) <= ftol):
break
xbar = Num.add.reduce(sim[0:-1],0) / N
xr = (1+rho)*xbar - rho*sim[-1]
fxr = apply(func,(xr,)+args)
funcalls = funcalls + 1
doshrink = 0
if fxr < fsim[0]:
xe = (1+rho*chi)*xbar - rho*chi*sim[-1]
fxe = apply(func,(xe,)+args)
funcalls = funcalls + 1
if fxe < fxr:
sim[-1] = xe
fsim[-1] = fxe
else:
sim[-1] = xr
fsim[-1] = fxr
else: # fsim[0] <= fxr
if fxr < fsim[-2]:
sim[-1] = xr
fsim[-1] = fxr
else: # fxr >= fsim[-2]
# Perform contraction
if fxr < fsim[-1]:
xc = (1+psi*rho)*xbar - psi*rho*sim[-1]
fxc = apply(func,(xc,)+args)
funcalls = funcalls + 1
if fxc <= fxr:
sim[-1] = xc
fsim[-1] = fxc
else:
doshrink=1
else:
# Perform an inside contraction
xcc = (1-psi)*xbar + psi*sim[-1]
fxcc = apply(func,(xcc,)+args)
funcalls = funcalls + 1
if fxcc < fsim[-1]:
sim[-1] = xcc
fsim[-1] = fxcc
else:
doshrink = 1
if doshrink:
for j in one2np1:
sim[j] = sim[0] + sigma*(sim[j] - sim[0])
fsim[j] = apply(func,(sim[j],)+args)
funcalls = funcalls + N
ind = Num.argsort(fsim)
sim = Num.take(sim,ind,0)
fsim = Num.take(fsim,ind)
iterations = iterations + 1
x = sim[0]
fval = min(fsim)
warnflag = 0
if funcalls >= maxfun:
warnflag = 1
if printmessg:
print "Warning: Maximum number of function evaluations has been exceeded."
elif iterations >= maxiter:
warnflag = 2
if printmessg:
print "Warning: Maximum number of iterations has been exceeded"
else:
if printmessg:
print "Optimization terminated successfully."
print " Current function value: %f" % fval
print " Iterations: %d" % iterations
print " Function evaluations: %d" % funcalls
if fulloutput:
return x, fval, warnflag
else:
return x
def zoom(a_lo, a_hi):
pass
def line_search(f, fprime, xk, pk, gfk, args=(), c1=1e-4, c2=0.9, amax=50):
"""alpha, fc, gc = line_search(f, xk, pk, gfk,
args=(), c1=1e-4, c2=0.9, amax=1)
minimize the function f(xk+alpha pk) using the line search algorithm of
Wright and Nocedal in 'Numerical Optimization', 1999, pg. 59-60
"""
fc = 0
gc = 0
alpha0 = 1.0
phi0 = apply(f,(xk,)+args)
phi_a0 = apply(f,(xk+alpha0*pk,)+args)
fc = fc + 2
derphi0 = Num.dot(gfk,pk)
derphi_a0 = Num.dot(apply(fprime,(xk+alpha0*pk,)+args),pk)
gc = gc + 1
# check to see if alpha0 = 1 satisfies Strong Wolfe conditions.
if (phi_a0 <= phi0 + c1*alpha0*derphi0) \
and (abs(derphi_a0) <= c2*abs(derphi0)):
return alpha0, fc, gc
alpha0 = 0
alpha1 = 1
phi_a1 = phi_a0
phi_a0 = phi0
i = 1
while 1:
if (phi_a1 > phi0 + c1*alpha1*derphi0) or \
((phi_a1 >= phi_a0) and (i > 1)):
return zoom(alpha0, alpha1)
derphi_a1 = Num.dot(apply(fprime,(xk+alpha1*pk,)+args),pk)
gc = gc + 1
if (abs(derphi_a1) <= -c2*derphi0):
return alpha1
if (derphi_a1 >= 0):
return zoom(alpha1, alpha0)
alpha2 = (amax-alpha1)*0.25 + alpha1
i = i + 1
alpha0 = alpha1
alpha1 = alpha2
phi_a0 = phi_a1
phi_a1 = apply(f,(xk+alpha1*pk,)+args)
def line_search_BFGS(f, xk, pk, gfk, args=(), c1=1e-4, alpha0=1):
"""alpha, fc, gc = line_search(f, xk, pk, gfk,
args=(), c1=1e-4, alpha0=1)
minimize over alpha, the function f(xk+alpha pk) using the interpolation
algorithm (Armiijo backtracking) as suggested by
Wright and Nocedal in 'Numerical Optimization', 1999, pg. 56-57
"""
fc = 0
phi0 = apply(f,(xk,)+args) # compute f(xk)
phi_a0 = apply(f,(xk+alpha0*pk,)+args) # compute f
fc = fc + 2
derphi0 = Num.dot(gfk,pk)
if (phi_a0 <= phi0 + c1*alpha0*derphi0):
return alpha0, fc, 0
# Otherwise compute the minimizer of a quadratic interpolant:
alpha1 = -(derphi0) * alpha0**2 / 2.0 / (phi_a0 - phi0 - derphi0 * alpha0)
phi_a1 = apply(f,(xk+alpha1*pk,)+args)
fc = fc + 1
if (phi_a1 <= phi0 + c1*alpha1*derphi0):
return alpha1, fc, 0
# Otherwise loop with cubic interpolation until we find an alpha which satifies
# the first Wolfe condition (since we are backtracking, we will assume that
# the value of alpha is not too small and satisfies the second condition.
while 1: # we are assuming pk is a descent direction
factor = alpha0**2 * alpha1**2 * (alpha1-alpha0)
a = alpha0**2 * (phi_a1 - phi0 - derphi0*alpha1) - \
alpha1**2 * (phi_a0 - phi0 - derphi0*alpha0)
a = a / factor
b = -alpha0**3 * (phi_a1 - phi0 - derphi0*alpha1) + \
alpha1**3 * (phi_a0 - phi0 - derphi0*alpha0)
b = b / factor
alpha2 = (-b + Num.sqrt(abs(b**2 - 3 * a * derphi0))) / (3.0*a)
phi_a2 = apply(f,(xk+alpha2*pk,)+args)
fc = fc + 1
if (phi_a2 <= phi0 + c1*alpha2*derphi0):
return alpha2, fc, 0
if (alpha1 - alpha2) > alpha1 / 2.0 or (1 - alpha2/alpha1) < 0.96:
alpha2 = alpha1 / 2.0
alpha0 = alpha1
alpha1 = alpha2
phi_a0 = phi_a1
phi_a1 = phi_a2
epsilon = 1e-8
def approx_fprime(xk,f,*args):
f0 = apply(f,(xk,)+args)
grad = Num.zeros((len(xk),),'d')
ei = Num.zeros((len(xk),),'d')
for k in range(len(xk)):
ei[k] = 1.0
grad[k] = (apply(f,(xk+epsilon*ei,)+args) - f0)/epsilon
ei[k] = 0.0
return grad
def approx_fhess_p(x0,p,fprime,*args):
f2 = apply(fprime,(x0+epsilon*p,)+args)
f1 = apply(fprime,(x0,)+args)
return (f2 - f1)/epsilon
def fminBFGS(f, x0, fprime=None, args=(), avegtol=1e-5, maxiter=None, fulloutput=0, printmessg=1):
"""xopt = fminBFGS(f, x0, fprime=None, args=(), avegtol=1e-5,
maxiter=None, fulloutput=0, printmessg=1)
Optimize the function, f, whose gradient is given by fprime using the
quasi-Newton method of Broyden, Fletcher, Goldfarb, and Shanno (BFGS)
See Wright, and Nocedal 'Numerical Optimization', 1999, pg. 198.
"""
app_fprime = 0
if fprime is None:
app_fprime = 1
x0 = Num.asarray(x0)
if maxiter is None:
maxiter = len(x0)*200
func_calls = 0
grad_calls = 0
k = 0
N = len(x0)
gtol = N*avegtol
I = Num.eye(N)
Hk = I
if app_fprime:
gfk = apply(approx_fprime,(x0,f)+args)
func_calls = func_calls + len(x0) + 1
else:
gfk = apply(fprime,(x0,)+args)
grad_calls = grad_calls + 1
xk = x0
sk = [2*gtol]
while (Num.add.reduce(abs(gfk)) > gtol) and (k < maxiter):
pk = -Num.dot(Hk,gfk)
alpha_k, fc, gc = line_search_BFGS(f,xk,pk,gfk,args)
func_calls = func_calls + fc
xkp1 = xk + alpha_k * pk
sk = xkp1 - xk
xk = xkp1
if app_fprime:
gfkp1 = apply(approx_fprime,(xkp1,f)+args)
func_calls = func_calls + gc + len(x0) + 1
else:
gfkp1 = apply(fprime,(xkp1,)+args)
grad_calls = grad_calls + gc + 1
yk = gfkp1 - gfk
k = k + 1
rhok = 1 / Num.dot(yk,sk)
A1 = I - sk[:,Num.newaxis] * yk[Num.newaxis,:] * rhok
A2 = I - yk[:,Num.newaxis] * sk[Num.newaxis,:] * rhok
Hk = Num.dot(A1,Num.dot(Hk,A2)) + rhok * sk[:,Num.newaxis] * sk[Num.newaxis,:]
gfk = gfkp1
if printmessg or fulloutput:
fval = apply(f,(xk,)+args)
if k >= maxiter:
warnflag = 1
if printmessg:
print "Warning: Maximum number of iterations has been exceeded"
print " Current function value: %f" % fval
print " Iterations: %d" % k
print " Function evaluations: %d" % func_calls
print " Gradient evaluations: %d" % grad_calls
else:
warnflag = 0
if printmessg:
print "Optimization terminated successfully."
print " Current function value: %f" % fval
print " Iterations: %d" % k
print " Function evaluations: %d" % func_calls
print " Gradient evaluations: %d" % grad_calls
if fulloutput:
return xk, fval, func_calls, grad_calls, warnflag
else:
return xk
def fminNCG(f, x0, fprime, fhess_p=None, fhess=None, args=(), avextol=1e-5, maxiter=None, fulloutput=0, printmessg=1):
"""xopt = fminNCG(f, x0, fprime, fhess_p=None, fhess=None, args=(), avextol=1e-5,
maxiter=None, fulloutput=0, printmessg=1)
Optimize the function, f, whose gradient is given by fprime using the
Newton-CG method. fhess_p must compute the hessian times an arbitrary
vector. If it is not given, finite-differences on fprime are used to
compute it. See Wright, and Nocedal 'Numerical Optimization', 1999,
pg. 140.
"""
x0 = Num.asarray(x0)
fcalls = 0
gcalls = 0
hcalls = 0
approx_hessp = 0
if fhess_p is None and fhess is None: # Define hessian product
approx_hessp = 1
xtol = len(x0)*avextol
update = [2*xtol]
xk = x0
k = 0
while (Num.add.reduce(abs(update)) > xtol) and (k < maxiter):
# Compute a search direction pk by applying the CG method to
# del2 f(xk) p = - grad f(xk) starting from 0.
b = -apply(fprime,(xk,)+args)
gcalls = gcalls + 1
maggrad = Num.add.reduce(abs(b))
eta = min([0.5,Num.sqrt(maggrad)])
termcond = eta * maggrad
xsupi = 0
ri = -b
psupi = -ri
i = 0
dri0 = Num.dot(ri,ri)
if fhess is not None: # you want to compute hessian once.
A = apply(fhess,(xk,)+args)
hcalls = hcalls + 1
while Num.add.reduce(abs(ri)) > termcond:
if fhess is None:
if approx_hessp:
Ap = apply(approx_fhess_p,(xk,psupi,fprime)+args)
gcalls = gcalls + 2
else:
Ap = apply(fhess_p,(xk,psupi)+args)
hcalls = hcalls + 1
else:
Ap = Num.dot(A,psupi)
# check curvature
curv = Num.dot(psupi,Ap)
if (curv <= 0):
if (i > 0):
break
else:
xsupi = xsupi + dri0/curv * psupi
break
alphai = dri0 / curv
xsupi = xsupi + alphai * psupi
ri = ri + alphai * Ap
dri1 = Num.dot(ri,ri)
betai = dri1 / dri0
psupi = -ri + betai * psupi
i = i + 1
dri0 = dri1 # update Num.dot(ri,ri) for next time.
pk = xsupi # search direction is solution to system.
gfk = -b # gradient at xk
alphak, fc, gc = line_search_BFGS(f,xk,pk,gfk,args)
fcalls = fcalls + fc
gcalls = gcalls + gc
update = alphak * pk
xk = xk + update
k = k + 1
if printmessg or fulloutput:
fval = apply(f,(xk,)+args)
if k >= maxiter:
warnflag = 1
if printmessg:
print "Warning: Maximum number of iterations has been exceeded"
print " Current function value: %f" % fval
print " Iterations: %d" % k
print " Function evaluations: %d" % fcalls
print " Gradient evaluations: %d" % gcalls
print " Hessian evaluations: %d" % hcalls
else:
warnflag = 0
if printmessg:
print "Optimization terminated successfully."
print " Current function value: %f" % fval
print " Iterations: %d" % k
print " Function evaluations: %d" % fcalls
print " Gradient evaluations: %d" % gcalls
print " Hessian evaluations: %d" % hcalls
if fulloutput:
return xk, fval, fcalls, gcalls, hcalls, warnflag
else:
return xk
if __name__ == "__main__":
import string
import time
times = []
algor = []
x0 = [0.8,1.2,0.7]
start = time.time()
x = fmin(rosen,x0)
print x
times.append(time.time() - start)
algor.append('Nelder-Mead Simplex\t')
start = time.time()
x = fminBFGS(rosen, x0, fprime=rosen_der, maxiter=80)
print x
times.append(time.time() - start)
algor.append('BFGS Quasi-Newton\t')
start = time.time()
x = fminBFGS(rosen, x0, avegtol=1e-4, maxiter=100)
print x
times.append(time.time() - start)
algor.append('BFGS without gradient\t')
start = time.time()
x = fminNCG(rosen, x0, rosen_der, fhess_p=rosen3_hess_p, maxiter=80)
print x
times.append(time.time() - start)
algor.append('Newton-CG with hessian product')
start = time.time()
x = fminNCG(rosen, x0, rosen_der, fhess=rosen3_hess, maxiter=80)
print x
times.append(time.time() - start)
algor.append('Newton-CG with full hessian')
print "\nMinimizing the Rosenbrock function of order 3\n"
print " Algorithm \t\t\t Seconds"
print "===========\t\t\t ========="
for k in range(len(algor)):
print algor[k], "\t -- ", times[k]
|