| 1 |
from scikits.openopt import NLP |
|---|
| 2 |
from numpy import cos, arange, ones, asarray, zeros, mat, array |
|---|
| 3 |
|
|---|
| 4 |
N = 50 |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
f = lambda x : ((x-1)**4).sum() |
|---|
| 8 |
x0 = cos(arange(N)) |
|---|
| 9 |
p = NLP(f, x0, maxIter = 1e3, maxFunEvals = 1e5) |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
p.df = lambda x: 4*(x-1)**3 |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
p.lb = -5*ones(p.n) |
|---|
| 20 |
p.ub = 15*ones(p.n) |
|---|
| 21 |
p.ub[4] = -2.5 |
|---|
| 22 |
p.lb[5], p.ub[5] = 3.5, 4.5 |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
p.A = zeros((3, p.n)) |
|---|
| 31 |
p.A[0, 9] = 1 |
|---|
| 32 |
p.A[0, 19] = 1 |
|---|
| 33 |
p.A[1, 10:12] = -1 |
|---|
| 34 |
p.A[2] = -ones(p.n) |
|---|
| 35 |
p.b = [1.5, -1.6, -1.1*N] |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
p.Aeq = zeros(p.n) |
|---|
| 44 |
p.Aeq[20:22] = 1 |
|---|
| 45 |
p.beq = 2.5 |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
p.c = lambda x: [2* x[0] **4-1./32, x[1]**2+x[2]**2 - 1./8, x[25]**2 + x[35]**2 + x[25]*x[35] -2.5] |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
def DC(x): |
|---|
| 65 |
r = zeros((3, p.n)) |
|---|
| 66 |
r[0,0] = 2 * 4 * x[0]**3 |
|---|
| 67 |
r[1,1] = 2 * x[1] |
|---|
| 68 |
r[1,2] = 2 * x[2] |
|---|
| 69 |
r[2,25] = 2*x[25] + x[35] |
|---|
| 70 |
r[2,35] = 2*x[35] + x[25] |
|---|
| 71 |
return r |
|---|
| 72 |
p.dc = DC |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
h1 = lambda x: 1e4*(x[-1]-1)**4 |
|---|
| 78 |
h2 = lambda x: (x[-2]-1.5)**4 |
|---|
| 79 |
p.h = [h1, h2] |
|---|
| 80 |
|
|---|
| 81 |
def DH(x): |
|---|
| 82 |
r = zeros((2, p.n)) |
|---|
| 83 |
r[0, -1] = 1e4*4 * (x[-1]-1)**3 |
|---|
| 84 |
r[1, -2] = 4 * (x[-2]-1.5)**3 |
|---|
| 85 |
return r |
|---|
| 86 |
p.dh = DH |
|---|
| 87 |
|
|---|
| 88 |
p.contol = 1e-3 |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
p.gradtol = 1e-5 |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
p.checkdf() |
|---|
| 102 |
p.checkdc() |
|---|
| 103 |
p.checkdh() |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
p.plot = 0 |
|---|
| 110 |
p.iprint = 0 |
|---|
| 111 |
r = p.solve('algencan') |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
""" |
|---|
| 117 |
typical output: |
|---|
| 118 |
OpenOpt checks user-supplied gradient df (size: (50,)) |
|---|
| 119 |
according to: |
|---|
| 120 |
prob.diffInt = 1e-07 |
|---|
| 121 |
prob.check.maxViolation = 1e-05 |
|---|
| 122 |
max(abs(df_user - df_numerical)) = 2.50111104094e-06 |
|---|
| 123 |
(is registered in df number 41) |
|---|
| 124 |
sum(abs(df_user - df_numerical)) = 4.45203815948e-05 |
|---|
| 125 |
======================== |
|---|
| 126 |
OpenOpt checks user-supplied gradient dc (size: (50, 3)) |
|---|
| 127 |
according to: |
|---|
| 128 |
prob.diffInt = 1e-07 |
|---|
| 129 |
prob.check.maxViolation = 1e-05 |
|---|
| 130 |
max(abs(dc_user - dc_numerical)) = 1.20371180401e-06 |
|---|
| 131 |
(is registered in dc number 0) |
|---|
| 132 |
sum(abs(dc_user - dc_numerical)) = 1.60141862837e-06 |
|---|
| 133 |
======================== |
|---|
| 134 |
OpenOpt checks user-supplied gradient dh (size: (50, 2)) |
|---|
| 135 |
according to: |
|---|
| 136 |
prob.diffInt = 1e-07 |
|---|
| 137 |
prob.check.maxViolation = 1e-05 |
|---|
| 138 |
dh num i,j:dh[i]/dx[j] user-supplied numerical difference |
|---|
| 139 |
98 49 / 0 -1.369e+04 -1.369e+04 -2.941e-03 |
|---|
| 140 |
max(abs(dh_user - dh_numerical)) = 0.00294061290697 |
|---|
| 141 |
(is registered in dh number 98) |
|---|
| 142 |
sum(abs(dh_user - dh_numerical)) = 0.00294343472179 |
|---|
| 143 |
======================== |
|---|
| 144 |
starting solver ALGENCAN (GPL license) with problem unnamed |
|---|
| 145 |
solver ALGENCAN has finished solving the problem unnamed |
|---|
| 146 |
istop: 1000 |
|---|
| 147 |
Solver: Time elapsed = 0.34 CPU Time Elapsed = 0.34 |
|---|
| 148 |
objFunValue: 190.041570332 (feasible, max constraint = 0.000677961) |
|---|
| 149 |
""" |
|---|