| 1 |
from numpy import * |
|---|
| 2 |
from scikits.openopt import * |
|---|
| 3 |
|
|---|
| 4 |
coeff = 1e-8 |
|---|
| 5 |
|
|---|
| 6 |
f = lambda x: (x[0]-20)**2+(coeff * x[1] - 80)**2 |
|---|
| 7 |
c = lambda x: (x[0]-14)**2-1 |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
x0 = [-4,4] |
|---|
| 11 |
|
|---|
| 12 |
someModifiedStopCriteria = {'gradtol': 1e-15, 'ftol': 1e-15, 'xtol': 1e-15} |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
p = NLP(f, x0, c=c, **someModifiedStopCriteria) |
|---|
| 16 |
r = p.solve('ralg') |
|---|
| 17 |
print r.ff, r.xf |
|---|
| 18 |
""" |
|---|
| 19 |
for to improve the solution we will use |
|---|
| 20 |
changing either p.diffInt from default 1e-7 to [1e-7, 1] |
|---|
| 21 |
or p.scale from default None to [1, 1e-7] |
|---|
| 22 |
|
|---|
| 23 |
latter (using p.scale) is more recommended |
|---|
| 24 |
because it affects xtol for those solvers |
|---|
| 25 |
who use OO stop criteria |
|---|
| 26 |
(ralg, lincher, nsmm, nssolve and mb some others) |
|---|
| 27 |
xtol will be compared to scaled x shift: |
|---|
| 28 |
is || (x[k] - x[k-1]) * scale || < xtol |
|---|
| 29 |
|
|---|
| 30 |
You can define scale and diffInt as |
|---|
| 31 |
numpy arrays, matrices, Python lists, tuples |
|---|
| 32 |
""" |
|---|
| 33 |
p = NLP(f, x0, c=c, scale = [1, coeff], **someModifiedStopCriteria) |
|---|
| 34 |
r = p.solve('ralg') |
|---|
| 35 |
print r.ff, r.xf |
|---|
| 36 |
""" |
|---|
| 37 |
Full Output: |
|---|
| 38 |
starting solver ralg (license: BSD) with problem unnamed |
|---|
| 39 |
itn 0 : Fk= 6975.9999935999995 MaxResidual= 323.0 |
|---|
| 40 |
itn 10 Fk: 6424.9985147662055 MaxResidual: 2.96e-04 ls: 5 |
|---|
| 41 |
itn 20 Fk: 6424.9999835226936 MaxResidual: 2.02e-06 ls: 4 |
|---|
| 42 |
itn 30 Fk: 6424.9999885998468 MaxResidual: 1.00e-06 ls: 5 |
|---|
| 43 |
itn 40 Fk: 6424.999988599995 MaxResidual: 1.00e-06 ls: 5 |
|---|
| 44 |
itn 50 Fk: 6424.9999886000005 MaxResidual: 1.00e-06 ls: 78 |
|---|
| 45 |
itn 51 Fk: 6424.9999886000014 MaxResidual: 1.00e-06 ls: 0 |
|---|
| 46 |
ralg has finished solving the problem unnamed |
|---|
| 47 |
istop: 2 (|| gradient F(X[k]) || < gradtol) |
|---|
| 48 |
Solver: Time Elapsed = 0.54 CPU Time Elapsed = 0.39 |
|---|
| 49 |
objFunValue: 6424.9999886000014 (feasible, max constraint = 1e-06) |
|---|
| 50 |
6424.9999886000014 [ 15.0000005 4. ] |
|---|
| 51 |
starting solver ralg (license: BSD) with problem unnamed |
|---|
| 52 |
itn 0 : Fk= 6975.9999935999995 MaxResidual= 323.0 |
|---|
| 53 |
itn 10 Fk: 6424.9985147649186 MaxResidual: 2.96e-04 ls: 5 |
|---|
| 54 |
itn 20 Fk: 6424.9999824449724 MaxResidual: 1.80e-06 ls: 4 |
|---|
| 55 |
itn 30 Fk: 6424.9959805950612 MaxResidual: 1.00e-06 ls: 99 |
|---|
| 56 |
itn 40 Fk: 25.121367939538644 MaxResidual: 0.00e+00 ls: 1 |
|---|
| 57 |
itn 50 Fk: 25.000287679235381 MaxResidual: 0.00e+00 ls: -1 |
|---|
| 58 |
itn 60 Fk: 24.999999424995089 MaxResidual: 1.47e-07 ls: 1 |
|---|
| 59 |
itn 62 Fk: 24.999996226675954 MaxResidual: 7.95e-07 ls: -1 |
|---|
| 60 |
ralg has finished solving the problem unnamed |
|---|
| 61 |
istop: 2 (|| gradient F(X[k]) || < gradtol) |
|---|
| 62 |
Solver: Time Elapsed = 1.33 CPU Time Elapsed = 1.07 |
|---|
| 63 |
objFunValue: 24.999996489689014 (feasible, max constraint = 7.42082e-07) |
|---|
| 64 |
24.999996489689014 [ 1.50000004e+01 8.00004473e+09] |
|---|
| 65 |
""" |
|---|