Changeset 416


Ignore:
Timestamp:
08/25/10 06:23:04 (6 years ago)
Author:
mmckerns
Message:

updated dejong to include rosen.der and rosen.hess

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/alta/mystic-0.1a2/models/dejong.py

    r143 r416  
    1818from numpy import sum as numpysum 
    1919from numpy import asarray, transpose 
     20from numpy import zeros_like, diag, zeros, atleast_1d 
    2021from math import floor 
    2122import random 
     
    3435 
    3536minimum 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 
    3838        x[:len(coeffs)]=coeffs 
    3939        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) 
    4141 
    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 
     49minimum 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 
     66coeffs 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 
     79both 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 
    4589 
    4690    pass 
Note: See TracChangeset for help on using the changeset viewer.