/[escript]/trunk/downunder/py_src/minimizers.py
ViewVC logotype

Diff of /trunk/downunder/py_src/minimizers.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4096 by caltinay, Wed Dec 5 05:32:22 2012 UTC revision 4097 by caltinay, Fri Dec 7 01:18:35 2012 UTC
# Line 34  except: Line 34  except:
34      sqrt=np.sqrt      sqrt=np.sqrt
35      EPSILON=1e-18      EPSILON=1e-18
36    
 import sys  
 if sys.version_info[0]>2:  
     xrange=range  
       
37  lslogger=logging.getLogger('inv.minimizer.linesearch')  lslogger=logging.getLogger('inv.minimizer.linesearch')
38  zoomlogger=logging.getLogger('inv.minimizer.linesearch.zoom')  zoomlogger=logging.getLogger('inv.minimizer.linesearch.zoom')
39    
# Line 146  class AbstractMinimizer(object): Line 142  class AbstractMinimizer(object):
142      Base class for function minimization methods.      Base class for function minimization methods.
143      """      """
144    
145      TOLERANCE_REACHED, MAX_ITERATIONS_REACHED=list(xrange(2))      TOLERANCE_REACHED, MAX_ITERATIONS_REACHED=list(range(2))
146    
147      def __init__(self, f, tol=1e-5, imax=300):      def __init__(self, f, tol=1e-5, imax=300):
148          """          """
# Line 260  class MinimizerLBFGS(AbstractMinimizer): Line 256  class MinimizerLBFGS(AbstractMinimizer):
256          fx=self._f(x, *args)          fx=self._f(x, *args)
257          if self._f.provides_inverse_Hessian_approximation:          if self._f.provides_inverse_Hessian_approximation:
258              self._f.updateHessian()              self._f.updateHessian()
259              invH_scale = None              invH_scale = None
260          else:          else:
261              invH_scale = self._initial_H              invH_scale = self._initial_H
262          k=0          k=0
# Line 298  class MinimizerLBFGS(AbstractMinimizer): Line 294  class MinimizerLBFGS(AbstractMinimizer):
294              rho=self._f.getDualProduct(delta_x, delta_g)              rho=self._f.getDualProduct(delta_x, delta_g)
295              print "rho =",rho              print "rho =",rho
296              if abs(rho)>0 :              if abs(rho)>0 :
297                 s_and_y.append((delta_x,delta_g, rho ))                  s_and_y.append((delta_x,delta_g, rho ))
298              else:              else:
299             raise ZeroDivisionError("LBFGS break down.")                  raise ZeroDivisionError("LBFGS break down.")
300              # this needs to be reviewed              # this needs to be reviewed
301              if fx_new==0.:              if fx_new==0.:
302                  error=fx                  error=fx
303              else:              else:
304                  error=abs(fx_new-fx)/abs(fx_new)                  error=abs(fx_new-fx)/abs(fx_new)
305                
306              self._f.updateHessian()              self._f.updateHessian()
307              x=x_new              x=x_new
308              gf=gf_new              gf=gf_new
# Line 318  class MinimizerLBFGS(AbstractMinimizer): Line 314  class MinimizerLBFGS(AbstractMinimizer):
314              # delete oldest vector pair              # delete oldest vector pair
315              if k>self._m: s_and_y.pop(0)              if k>self._m: s_and_y.pop(0)
316    
317              if not self._f.provides_inverse_Hessian_approximation:                          if not self._f.provides_inverse_Hessian_approximation:
318            # set the new scaling factor (approximation of inverse Hessian)                    # set the new scaling factor (approximation of inverse Hessian)
319            denom=self._f.getDualProduct(delta_g, delta_g)                    denom=self._f.getDualProduct(delta_g, delta_g)
320            if denom > 0:                    if denom > 0:
321                invH_scale=self._f.getDualProduct(delta_x,delta_g)/denom                        invH_scale=self._f.getDualProduct(delta_x,delta_g)/denom
322            else:                    else:
323                invH_scale=self._initial_H                        invH_scale=self._initial_H
324                self.logger.debug("LBFGS.Break down in H update. Resetting to initial value %s."%self._initial_H)                        self.logger.debug("LBFGS.Break down in H update. Resetting to initial value %s."%self._initial_H)
325    
326          if k >= self._imax:          if k >= self._imax:
327              reason=self.MAX_ITERATIONS_REACHED              reason=self.MAX_ITERATIONS_REACHED
# Line 348  class MinimizerLBFGS(AbstractMinimizer): Line 344  class MinimizerLBFGS(AbstractMinimizer):
344              alpha.append(a)              alpha.append(a)
345              q=q-a*y              q=q-a*y
346    
347          if self._f.provides_inverse_Hessian_approximation:            if self._f.provides_inverse_Hessian_approximation:
348               r= self._f.getInverseHessianApproximation(x, q, *args)               r = self._f.getInverseHessianApproximation(x, q, *args)
349          else:          else:
350               r= invH_scale * q               r = invH_scale * q
351                
352          for s,y,rho in s_and_y:          for s,y,rho in s_and_y:
353              beta=self._f.getDualProduct(r, y)/rho              beta = self._f.getDualProduct(r, y)/rho
354              a=alpha.pop()              a = alpha.pop()
355              r=r+s*(a-beta)              r = r + s * (a-beta)
356          return r          return r
357    
358  ##############################################################################  ##############################################################################

Legend:
Removed from v.4096  
changed lines
  Added in v.4097

  ViewVC Help
Powered by ViewVC 1.1.26