Home | Trees | Indices | Help |
|
---|
|
1 #!/usr/bin/env python 2 3 # Matthieu Brucher 4 # Last Change : 2007-08-24 10:59 5 6 """ 7 Class defining the Rosenbrock function 8 """ 9 10 import numpy 11 from numpy.testing import * 12 set_package_path() 13 from optimizers import criterion, step, optimizer, line_search 14 restore_path() 1517 """ 18 The Rosenbrock function 19 """ 255627 """ 28 Get the value of the Rosenbrock function at a specific point 29 """ 30 return numpy.sum(100.0 * (x[1:] - x[:-1]**2.0)**2.0 + (1. - x[:-1])**2.0)3133 """ 34 Evaluates the gradient of the function 35 """ 36 xm = x[1:-1] 37 xm_m1 = x[:-2] 38 xm_p1 = x[2:] 39 der = numpy.zeros(x.shape, x.dtype) 40 der[1:-1] = 200. * (xm - xm_m1**2.) - 400. * (xm_p1 - xm**2.) * xm - 2. * (1. - xm) 41 der[0] = -400. * x[0] * (x[1] - x[0]**2.) - 2. * (1. - x[0]) 42 der[-1] = 200. * (x[-1] - x[-2]**2.) 43 return der4446 """ 47 Evaluates the gradient of the function 48 """ 49 H = numpy.diag(-400. * x[:-1], 1) - numpy.diag(400. * x[:-1],-1) 50 diagonal = numpy.zeros(len(x), x.dtype) 51 diagonal[0] = 1200. * x[0]**2. - 400. * x[1] + 2. 52 diagonal[-1] = 200. 53 diagonal[1:-1] = 202. + 1200. * x[1:-1]**2. - 400. * x[2:] 54 H += numpy.diag(diagonal) 55 return H58 """ 59 Global test class with the Rosenbrock function 60 """95 96 if __name__ == "__main__": 97 NumpyTest().run() 9862 startPoint = numpy.empty(2, numpy.float) 63 startPoint[0] = -1.01 64 startPoint[-1] = 1.01 65 optimi = optimizer.StandardOptimizer(function = Rosenbrock(2), step = step.GradientStep(), criterion = criterion.OrComposition(criterion.MonotonyCriterion(0.00001), criterion.IterationCriterion(10000)), x0 = startPoint, line_search = line_search.SimpleLineSearch(alpha_step = 0.001)) 66 assert_almost_equal(optimi.optimize(), numpy.ones(2, numpy.float), decimal=1)6769 startPoint = numpy.empty(2, numpy.float) 70 startPoint[0] = -1.01 71 startPoint[-1] = 1.01 72 optimi = optimizer.StandardOptimizer(function = Rosenbrock(2), step = step.GradientStep(), criterion = criterion.RelativeValueCriterion(0.00001), x0 = startPoint, line_search = line_search.SimpleLineSearch(alpha_step = 0.001)) 73 assert_almost_equal(optimi.optimize(), numpy.ones(2, numpy.float), decimal=1)7476 startPoint = numpy.empty(2, numpy.float) 77 startPoint[0] = -1.01 78 startPoint[-1] = 1.01 79 optimi = optimizer.StandardOptimizer(function = Rosenbrock(2), step = step.NewtonStep(), criterion = criterion.RelativeValueCriterion(0.00001), x0 = startPoint, line_search = line_search.SimpleLineSearch()) 80 assert_almost_equal(optimi.optimize(), numpy.ones(2, numpy.float))8183 startPoint = numpy.empty(2, numpy.float) 84 startPoint[0] = -1.01 85 startPoint[-1] = 1.01 86 optimi = optimizer.StandardOptimizer(function = Rosenbrock(2), step = step.CWConjugateGradientStep(), criterion = criterion.criterion(iterations_max = 1000, ftol = 0.00000001, gtol = 0.0001), x0 = startPoint, line_search = line_search.WolfePowellRule()) 87 assert_array_almost_equal(optimi.optimize(), numpy.ones(2, numpy.float))8890 startPoint = numpy.empty(2, numpy.float) 91 startPoint[0] = -1.01 92 startPoint[-1] = 1.01 93 optimi = optimizer.StandardOptimizer(function = Rosenbrock(2), step = step.DYConjugateGradientStep(), criterion = criterion.criterion(iterations_max = 1000, ftol = 0.00000001, gtol = 0.0001), x0 = startPoint, line_search = line_search.StrongWolfePowellRule()) 94 assert_array_almost_equal(optimi.optimize(), numpy.ones(2, numpy.float), decimal = 4)
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Dec 2 23:44:37 2012 | http://epydoc.sourceforge.net |