1
2
3
4
5
6 """
7 Class defining a quadratic 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()
15
17 """
18 A simple quadratic function
19 """
21 """
22 Get the value of the quadratic function at a specific point
23 """
24 return (x[0] + 2* x[1] - 7)**2 + (2 * x[0] + x[1] - 5)**2
25
27 """
28 Evaluates the gradient of the function
29 """
30 return numpy.array([2 * (x[0] + 2* x[1] - 7) + 4 * (2 * x[0] + x[1] - 5), 4 * (x[0] + 2* x[1] - 7) + 2 * (2 * x[0] + x[1] - 5)], dtype = numpy.float)
31
33 """
34 Evaluates the gradient of the function
35 """
36 return numpy.array([[10, 8], [8, 10]], dtype = numpy.float)
37
39 """
40 Global test class with a quadratic function
41 """
46
51
56
58 startPoint = numpy.zeros(2, numpy.float)
59 optimi = optimizer.StandardOptimizer(function = Quadratic(), step = step.FRConjugateGradientStep(), criterion = criterion.criterion(ftol=0.000001, iterations_max=1000, gtol = 0.0001), x0 = startPoint, line_search = line_search.StrongWolfePowellRule())
60 assert_almost_equal(optimi.optimize(), numpy.array([1, 3], dtype = numpy.float))
61
63 startPoint = numpy.zeros(2, numpy.float)
64 optimi = optimizer.StandardOptimizer(function = Quadratic(), step = step.CWConjugateGradientStep(), criterion = criterion.criterion(ftol=0.0000001, iterations_max=1000, gtol = 0.0001), x0 = startPoint, line_search = line_search.StrongWolfePowellRule())
65 assert_almost_equal(optimi.optimize(), numpy.array([1, 3], dtype = numpy.float), decimal=4)
66
68 startPoint = numpy.zeros(2, numpy.float)
69 optimi = optimizer.StandardOptimizer(function = Quadratic(), step = step.PRPConjugateGradientStep(), criterion = criterion.criterion(ftol=0.000001, iterations_max=1000, gtol = 0.0001), x0 = startPoint, line_search = line_search.StrongWolfePowellRule())
70 assert_almost_equal(optimi.optimize(), numpy.array([1, 3], dtype = numpy.float))
71
73 startPoint = numpy.zeros(2, numpy.float)
74 optimi = optimizer.StandardOptimizer(function = Quadratic(), step = step.DConjugateGradientStep(), criterion = criterion.criterion(ftol=0.000001, iterations_max=1000, gtol = 0.0001), x0 = startPoint, line_search = line_search.StrongWolfePowellRule())
75 assert_almost_equal(optimi.optimize(), numpy.array([1, 3], dtype = numpy.float))
76
78 startPoint = numpy.zeros(2, numpy.float)
79 optimi = optimizer.StandardOptimizer(function = Quadratic(), step = step.DYConjugateGradientStep(), criterion = criterion.criterion(ftol=0.000001, iterations_max=1000, gtol = 0.0001), x0 = startPoint, line_search = line_search.StrongWolfePowellRule())
80 assert_almost_equal(optimi.optimize(), numpy.array([1, 3], dtype = numpy.float))
81
83 startPoint = numpy.zeros(2, numpy.float)
84 optimi = optimizer.StandardOptimizer(function = Quadratic(), step = step.MarquardtStep(), criterion = criterion.criterion(gtol=0.00001, iterations_max=200), x0 = startPoint, line_search = line_search.BacktrackingSearch())
85 opt = optimi.optimize()
86 assert_almost_equal(optimi.optimize(), numpy.array([1, 3], dtype = numpy.float), decimal = 5)
87
88 if __name__ == "__main__":
89 NumpyTest().run()
90