1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Tests of PIPS NLP solver.
18 """
19
20 from math import sqrt
21
22 from numpy import zeros, ones, array, eye, prod, dot, asscalar, Inf
23
24 from scipy.sparse import csr_matrix as sparse
25 from scipy.sparse import eye as speye
26
27 from pypower.pips import pips
28
29 from pypower.t.t_begin import t_begin
30 from pypower.t.t_is import t_is
31 from pypower.t.t_end import t_end
32 from pypower.t.t_ok import t_ok
33
34
35
36
37 -def f2(x, return_hessian=False):
38 a = 100
39 f = a * (x[1] - x[0]**2)**2 + (1 - x[0])**2
40 df = array([
41 4 * a * (x[0]**3 - x[0] * x[1]) + 2 * x[0] - 2,
42 2 * a * (x[1] - x[0]**2)
43 ])
44
45 if not return_hessian:
46 return f, df
47
48 d2f = 4 * a * sparse([
49 [3 * x[0]**2 - x[1] + 1. / (2 * a), -x[0]],
50 [ -x[0], 0.5]
51 ])
52 return f, df, d2f
53
54
55
56
57 -def f3(x, return_hessian=False):
58 H = sparse([
59 [ 5, -2, -1],
60 [-2, 4, 3],
61 [-1, 3, 5]
62 ], dtype=float)
63 c = array([2, -35, -47], float)
64 f = 0.5 * dot(x * H, x) + dot(c, x) + 5
65 df = H * x + c
66 if not return_hessian:
67 return f, df
68 d2f = H
69 return f, df, d2f
70
71
72
73
74 -def f4(x, return_hessian=False):
75 H = sparse([
76 [1003.1, 4.3, 6.3, 5.9],
77 [ 4.3, 2.2, 2.1, 3.9],
78 [ 6.3, 2.1, 3.5, 4.8],
79 [ 5.9, 3.9, 4.8, 10.0]
80 ])
81 c = zeros(4)
82 f = 0.5 * dot(x * H, x) + dot(c, x)
83 df = H * x + c
84 if not return_hessian:
85 return f, df
86 d2f = H
87 return f, df, d2f
88
89
90
91
92 -def f5(x, return_hessian=False):
93 c = -array([1.0, 1.0])
94 f = dot(c, x)
95 df = c
96 if not return_hessian:
97 return f, df
98 d2f = sparse((2, 2))
99 return f, df, d2f
100
102 h = dot([[-1.0, -1.0],
103 [ 1.0, 1.0]], x**2) + [1, -2]
104 dh = 2 * sparse([[-x[0], x[0]],
105 [-x[1], x[1]]])
106 g = array([])
107 dg = None
108 return h, g, dh, dg
109
110 -def hess5(x, lam, cost_mult):
111 mu = lam['ineqnonlin']
112 Lxx = 2 * dot([-1.0, 1.0], mu) * eye(2)
113 return Lxx
114
115
116
117
118 -def f6(x, return_hessian=False):
119 f = -x[0] * x[1] - x[1] * x[2]
120 df = -array([x[1], x[0] + x[2], x[1]])
121 if not return_hessian:
122 return f, df
123 d2f = -sparse([[0, 1, 0],
124 [1, 0, 1],
125 [0, 1, 0]], dtype=float)
126 return f, df, d2f
127
129 h = dot([[1.0, -1.0, 1.0],
130 [1.0, 1.0, 1.0]], x**2) + [-2.0, -10.0]
131 dh = 2 * sparse([[ x[0], x[0]],
132 [-x[1], x[1]],
133 [ x[2], x[2]]], dtype=float)
134 g = array([])
135 dg = None
136 return h, g, dh, dg
137
138 -def hess6(x, lam, cost_mult=1):
139 mu = lam['ineqnonlin']
140 Lxx = cost_mult * \
141 sparse([[ 0, -1, 0],
142 [-1, 0, -1],
143 [ 0, -1, 0]], dtype=float) + \
144 sparse([[2 * dot([1, 1], mu), 0, 0],
145 [0, 2 * dot([-1, 1], mu), 0],
146 [0, 0, 2 * dot([1, 1], mu)]], dtype=float)
147 return Lxx
148
149
150
151
152 -def f7(x, return_hessian=False):
153 f = x[0] * x[3] * sum(x[:3]) + x[2]
154 df = array([ x[0] * x[3] + x[3] * sum(x[:3]),
155 x[0] * x[3],
156 x[0] * x[3] + 1,
157 x[0] * sum(x[:3]) ])
158 if not return_hessian:
159 return f, df
160 d2f = sparse([
161 [2 * x[3], x[3], x[3], 2 * x[0] + x[1] + x[2]],
162 [ x[3], 0.0, 0.0, x[0]],
163 [ x[3], 0.0, 0.0, x[0]],
164 [2 * x[0] + x[1] + x[2], x[0], x[0], 0.0]
165 ])
166 return f, df, d2f
167
169 g = array( [sum(x**2) - 40.0] )
170 h = array( [ -prod(x) + 25.0] )
171 dg = sparse( 2 * x ).T
172 dh = sparse( -prod(x) / x ).T
173 return h, g, dh, dg
174
175 -def hess7(x, lam, sigma=1):
176 lmbda = asscalar( lam['eqnonlin'] )
177 mu = asscalar( lam['ineqnonlin'] )
178 _, _, d2f = f7(x, True)
179
180 Lxx = sigma * d2f + lmbda * 2 * speye(4, 4) - \
181 mu * sparse([
182 [ 0.0, x[2] * x[3], x[2] * x[3], x[1] * x[2]],
183 [x[2] * x[2], 0.0, x[0] * x[3], x[0] * x[2]],
184 [x[1] * x[3], x[0] * x[3], 0.0, x[0] * x[1]],
185 [x[1] * x[2], x[0] * x[2], x[0] * x[1], 0.0]
186 ])
187 return Lxx
188
189
191 """Tests of pips NLP solver.
192
193 @author: Ray Zimmerman (PSERC Cornell)
194 @author: Richard Lincoln
195 """
196 t_begin(60, quiet)
197
198 t = 'unconstrained banana function : '
199
200 f_fcn = f2
201 x0 = array([-1.9, 2])
202
203 solution = pips(f_fcn, x0)
204 x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \
205 solution["lmbda"], solution["output"]
206 t_is(s, 1, 13, [t, 'success'])
207 t_is(x, [1, 1], 13, [t, 'x'])
208 t_is(f, 0, 13, [t, 'f'])
209 t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond'])
210 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l'])
211 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u'])
212 t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']'])
213 t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']'])
214
215 t = 'unconstrained 3-d quadratic : '
216
217 f_fcn = f3
218 x0 = array([0, 0, 0], float)
219
220 solution = pips(f_fcn, x0)
221 x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \
222 solution["lmbda"], solution["output"]
223 t_is(s, 1, 13, [t, 'success'])
224 t_is(x, [3, 5, 7], 13, [t, 'x'])
225 t_is(f, -244, 13, [t, 'f'])
226 t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond'])
227 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l'])
228 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u'])
229 t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']'])
230 t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']'])
231
232 t = 'constrained 4-d QP : '
233
234 f_fcn = f4
235 x0 = array([1.0, 0.0, 0.0, 1.0])
236 A = array([
237 [1.0, 1.0, 1.0, 1.0 ],
238 [0.17, 0.11, 0.10, 0.18]
239 ])
240 l = array([1, 0.10])
241 u = array([1.0, Inf])
242 xmin = zeros(4)
243
244 solution = pips(f_fcn, x0, A, l, u, xmin)
245 x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \
246 solution["lmbda"], solution["output"]
247 t_is(s, 1, 13, [t, 'success'])
248 t_is(x, array([0, 2.8, 0.2, 0]) / 3, 6, [t, 'x'])
249 t_is(f, 3.29 / 3, 6, [t, 'f'])
250 t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond'])
251 t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l'])
252 t_is(lam['mu_u'], array([0, 0]), 13, [t, 'lam.mu_u'])
253 t_is(lam['lower'], array([2.24, 0, 0, 1.7667]), 4, [t, 'lam[\'lower\']'])
254 t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']'])
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 t = 'constrained 2-d nonlinear : '
274
275 f_fcn = f5
276 gh_fcn = gh5
277 hess_fcn = hess5
278 x0 = array([1.1, 0.0])
279 xmin = zeros(2)
280
281
282 solution = pips(f_fcn, x0, xmin=xmin, gh_fcn=gh_fcn, hess_fcn=hess_fcn)
283 x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \
284 solution["lmbda"], solution["output"]
285 t_is(s, 1, 13, [t, 'success'])
286 t_is(x, [1, 1], 6, [t, 'x'])
287 t_is(f, -2, 6, [t, 'f'])
288 t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond'])
289 t_is(lam['ineqnonlin'], array([0, 0.5]), 6, [t, 'lam.ineqnonlin'])
290 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l'])
291 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u'])
292 t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']'])
293 t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']'])
294
295
296
297
298
299
300
301
302
303 t = 'constrained 3-d nonlinear : '
304
305 f_fcn = f6
306 gh_fcn = gh6
307 hess_fcn = hess6
308 x0 = array([1.0, 1.0, 0.0])
309
310 solution = pips(f_fcn, x0, gh_fcn=gh_fcn, hess_fcn=hess_fcn)
311 x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \
312 solution["lmbda"], solution["output"]
313 t_is(s, 1, 13, [t, 'success'])
314 t_is(x, [1.58113883, 2.23606798, 1.58113883], 6, [t, 'x'])
315 t_is(f, -5 * sqrt(2), 6, [t, 'f'])
316 t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond'])
317 t_is(lam['ineqnonlin'], array([0, sqrt(2) / 2]), 7, [t, 'lam.ineqnonlin'])
318 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l'])
319 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u'])
320 t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']'])
321 t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']'])
322
323
324
325
326
327
328
329
330
331 t = 'constrained 3-d nonlinear (dict) : '
332 p = {'f_fcn': f_fcn, 'x0': x0, 'gh_fcn': gh_fcn, 'hess_fcn': hess_fcn}
333 solution = pips(p)
334 x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \
335 solution["lmbda"], solution["output"]
336 t_is(s, 1, 13, [t, 'success'])
337 t_is(x, [1.58113883, 2.23606798, 1.58113883], 6, [t, 'x'])
338 t_is(f, -5 * sqrt(2), 6, [t, 'f'])
339 t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond'])
340 t_is(lam['ineqnonlin'], [0, sqrt(2) / 2], 7, [t, 'lam.ineqnonlin'])
341 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l'])
342 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u'])
343 t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']'])
344 t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']'])
345
346 t = 'constrained 4-d nonlinear : '
347
348 f_fcn = f7
349 gh_fcn = gh7
350 hess_fcn = hess7
351 x0 = array([1.0, 5.0, 5.0, 1.0])
352 xmin = ones(4)
353 xmax = 5 * xmin
354
355 solution = pips(f_fcn, x0, xmin=xmin, xmax=xmax, gh_fcn=gh_fcn, hess_fcn=hess_fcn)
356 x, f, s, lam, _ = solution["x"], solution["f"], solution["eflag"], \
357 solution["lmbda"], solution["output"]
358 t_is(s, 1, 13, [t, 'success'])
359 t_is(x, [1, 4.7429994, 3.8211503, 1.3794082], 6, [t, 'x'])
360 t_is(f, 17.0140173, 6, [t, 'f'])
361 t_is(lam['eqnonlin'], 0.1614686, 5, [t, 'lam.eqnonlin'])
362 t_is(lam['ineqnonlin'], 0.55229366, 5, [t, 'lam.ineqnonlin'])
363 t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l'])
364 t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u'])
365 t_is(lam['lower'], [1.08787121024, 0, 0, 0], 5, [t, 'lam[\'lower\']'])
366 t_is(lam['upper'], zeros(x.shape), 7, [t, 'lam[\'upper\']'])
367
368 t_end()
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 if __name__ == '__main__':
394 t_pips(quiet=False)
395