Changeset 416
- Timestamp:
- 08/25/10 06:23:04 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alta/mystic-0.1a2/models/dejong.py
r143 r416 18 18 from numpy import sum as numpysum 19 19 from numpy import asarray, transpose 20 from numpy import zeros_like, diag, zeros, atleast_1d 20 21 from math import floor 21 22 import random … … 34 35 35 36 minimum is f(x)=0.0 at xi=1.0""" 36 #ensure that there are 2 coefficients 37 x = [1]*2 37 x = [1]*2 # ensure that there are 2 coefficients 38 38 x[:len(coeffs)]=coeffs 39 39 x = asarray(x) #XXX: must be a numpy.array 40 return numpysum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0) 40 return numpysum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)#,axis=0) 41 41 42 # def forward(self,pts): 43 # """n-dimensional Rosenbrock; returns f(xi,yi,...) for pts=(x,y,...)""" 44 # return AbstractFunction.forward(self,pts) 42 #def forward(self,pts): 43 # """n-dimensional Rosenbrock; returns f(xi,yi,...) for pts=(x,y,...)""" 44 # return AbstractFunction.forward(self,pts) 45 46 def derivative(self,coeffs): 47 """evaluates n-dimensional Rosenbrock derivative for a list of coeffs 48 49 minimum is f'(x)=[0.0]*n at x=[1.0]*n; x must have len >= 2""" 50 l = len(coeffs) 51 x = [0]*l #XXX: ensure that there are 2 coefficients ? 52 x[:l]=coeffs 53 x = asarray(x) #XXX: must be a numpy.array 54 xm = x[1:-1] 55 xm_m1 = x[:-2] 56 xm_p1 = x[2:] 57 der = zeros_like(x) 58 der[1:-1] = 200*(xm-xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1-xm) 59 der[0] = -400*x[0]*(x[1]-x[0]**2) - 2*(1-x[0]) 60 der[-1] = 200*(x[-1]-x[-2]**2) 61 return list(der) 62 63 def hessian(self, coeffs): 64 """evaluates n-dimensional Rosenbrock hessian for the given coeffs 65 66 coeffs must have len >= 2""" 67 x = atleast_1d(coeffs) 68 H = diag(-400*x[:-1],1) - diag(400*x[:-1],-1) 69 diagonal = zeros(len(x), dtype=x.dtype) 70 diagonal[0] = 1200*x[0]-400*x[1]+2 71 diagonal[-1] = 200 72 diagonal[1:-1] = 202 + 1200*x[1:-1]**2 - 400*x[2:] 73 H = H + diag(diagonal) 74 return H 75 76 def hessian_product(self, coeffs, p): 77 """evaluates n-dimensional Rosenbrock hessian product for the given coeffs 78 79 both p and coeffs must have len >= 2""" 80 #XXX: not well-tested 81 p = atleast_1d(p) 82 x = atleast_1d(coeffs) 83 Hp = zeros(len(x), dtype=x.dtype) 84 Hp[0] = (1200*x[0]**2 - 400*x[1] + 2)*p[0] - 400*x[0]*p[1] 85 Hp[1:-1] = -400*x[:-2]*p[:-2]+(202+1200*x[1:-1]**2-400*x[2:])*p[1:-1] \ 86 -400*x[1:-1]*p[2:] 87 Hp[-1] = -400*x[-2]*p[-2] + 200*p[-1] 88 return Hp 45 89 46 90 pass
Note: See TracChangeset
for help on using the changeset viewer.