-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoptimization.py
executable file
·173 lines (159 loc) · 7.18 KB
/
optimization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# this function has:
# * a minimum at [x == -1.012273212379936, y == 0.0]
# (f_xx*f_yy - f_xy**2 > 0 and f_xx > 0)
# * a saddle point at [x == 0.9872574572835215, y == 0.0]
# (f_xx*f__y - f_xy**2 < 0)
# * a maximum at [x == 0.02501565538380409, y == 0.0]
# (f_xx*f_yy - f_xy**2 > 0 and f_xx < 0)
def func(X, y=None):
if y is None:
x, y = X
else:
x = X
return (x**2 + y**2)**2 - 2*x**2 - 2*y**2 + 0.1*x
def func_grad(X):
x, y = X
func_x = 4*(x**2 + y**2)*x - 4*x + 0.1
func_y = 4*(x**2 + y**2)*y - 4*y
return np.array([func_x, func_y])
def func_hess(X):
x, y = X
func_x_x = 12*x**2 + 4*y**2 - 4
func_x_y = 8*x*y
func_y_x = 8*x*y
func_y_y = 4*x**2 + 12*y**2 - 4
return np.array([[func_x_x, func_x_y], [func_y_x, func_y_y]])
def check_solution(x):
from scipy.linalg import det
hessian = func_hess(x)
det_hessian = det(hessian)
type_str = ' ||H|| = {0:.4f}'.format(det_hessian)
if det_hessian > 0.0:
type_str += ', f_xx = {0:.4f}'.format(hessian[0, 0])
if hessian[0, 0] > 0.0:
type_str += ': relative minimum'
else:
type_str += ': relative maximum'
elif det_hessian < 0.0:
type_str += ': saddle point'
else:
type_str += ': inconclusive'
print(type_str)
if __name__ == '__main__':
from argparse import ArgumentParser
import time
import numpy as np
import scipy.optimize as opt
# handle command line arguments
arg_parser = ArgumentParser(description='optimize a 2D-function')
arg_parser.add_argument('--x0', type=float, default=1.0,
help='initial x-value')
arg_parser.add_argument('--y0', type=float, default=0.0,
help='initial y-value')
arg_parser.add_argument('--xtol', type=float, default=1.0e-4,
help='line-search error tolerance (Powell)')
arg_parser.add_argument('--ftol', type=float, default=1.0e-4,
help='relative error in function value '
'acceptable for convergence (Powell)')
arg_parser.add_argument('--gtol', type=float, default=1.0e-5,
help='stop when the norm of the gradient is '
'less than gtol (CG & BFGS)')
arg_parser.add_argument('--avextol', type=float, default=1.0e-5,
help='stop when the average relative error is '
'less than gtol (N-CG)')
arg_parser.add_argument('--check', action='store_true',
help='check critical point')
options = arg_parser.parse_args()
# Powell's method
x0 = np.array([options.x0, options.y0])
start = time.time()
xopt, fopt, _, iter, funcalls, warnflag = opt.fmin_powell(
func, x0,
xtol=options.xtol,
ftol=options.ftol,
full_output=True,
disp=False
)
durationn = time.time() - start
output_str = ' minimum {fopt:.15} at ({x:.15}, {y:.15})'
info_str = ' needed {iter} iterations, {calls} function calls'
time_str = ' in {sec:.6f} seocnds'
warn_str = ' result flag = {flag}'
print("Powell's method:")
print(output_str.format(fopt=fopt, x=xopt[0], y=xopt[1]))
print(info_str.format(iter=iter, calls=funcalls))
print(time_str.format(sec=durationn))
print(warn_str.format(flag=warnflag))
if options.check:
check_solution(xopt)
# conjugate gradient method
x0 = np.array([options.x0, options.y0])
start = time.time()
xopt, fopt, funcalls, gradcalls, warnflag = opt.fmin_cg(
func, x0,
fprime=func_grad,
gtol=options.gtol,
full_output=True,
disp=False
)
durationn = time.time() - start
output_str = ' minimum {fopt:.15} at ({x:.15}, {y:.15})'
info_str = ' needed {calls} function calls, {gradcalls} gradient calls'
time_str = ' in {sec:.6f} seocnds'
warn_str = ' result flag = {flag}'
print('\nconjugate gradient method')
print(output_str.format(fopt=fopt, x=xopt[0], y=xopt[1]))
print(info_str.format(calls=funcalls, gradcalls=gradcalls))
print(time_str.format(sec=durationn))
print(warn_str.format(flag=warnflag))
if options.check:
check_solution(xopt)
# Broyden-Fletcher-Goldfarb-Shanno algorithm
x0 = np.array([options.x0, options.y0])
start = time.time()
xopt, fopt, _, _, funcalls, gradcalls, warnflag = opt.fmin_bfgs(
func, x0,
fprime=func_grad,
gtol=options.gtol,
full_output=True,
disp=False
)
durationn = time.time() - start
output_str = ' minimum {fopt:.15} at ({x:.15}, {y:.15})'
info_str = ' needed {calls} function calls, {gradcalls} gradient calls'
time_str = ' in {sec:.6f} seocnds'
warn_str = ' result flag = {flag}'
print('\nBFGS method')
print(output_str.format(fopt=fopt, x=xopt[0], y=xopt[1]))
print(info_str.format(calls=funcalls, gradcalls=gradcalls))
print(time_str.format(sec=durationn))
print(warn_str.format(flag=warnflag))
if options.check:
check_solution(xopt)
# Newton conjugate gradient method
x0 = np.array([options.x0, options.y0])
start = time.time()
xopt, fopt, funcalls, gradcalls, hesscalls, warnflag = opt.fmin_ncg(
func, x0,
fprime=func_grad,
fhess=func_hess,
avextol=options.avextol,
full_output=True,
disp=False
)
durationn = time.time() - start
output_str = ' minimum {fopt:.15} at ({x:.15}, {y:.15})'
info_str = (' needed {calls} function calls'
', {gradcalls} gradient calls'
', {hesscalls} Hessian calls')
time_str = ' in {sec:.6f} seocnds'
warn_str = ' result flag = {flag}'
print('\nNewton conjugate gradient method')
print(output_str.format(fopt=fopt, x=xopt[0], y=xopt[1]))
print(info_str.format(calls=funcalls, gradcalls=gradcalls,
hesscalls=hesscalls))
print(time_str.format(sec=durationn))
print(warn_str.format(flag=warnflag))
if options.check:
check_solution(xopt)