| 1 |
from numpy import * |
|---|
| 2 |
from openopt import * |
|---|
| 3 |
|
|---|
| 4 |
coeff = 1e-7 |
|---|
| 5 |
|
|---|
| 6 |
f = lambda x: (x[0]-20)**2+(coeff * x[1] - 80)**2 # objFun |
|---|
| 7 |
c = lambda x: (x[0]-14)**2-1 # non-lin ineq constraint(s) c(x) <= 0 |
|---|
| 8 |
# for the problem involved: f_opt =25, x_opt = [15.0, 8.0e9] |
|---|
| 9 |
|
|---|
| 10 |
x0 = [-4,4] |
|---|
| 11 |
# even modification of stop criteria can't help to achieve the desired solution: |
|---|
| 12 |
someModifiedStopCriteria = {'gtol': 1e-15, 'ftol': 1e-13, 'xtol': 1e-13, 'maxIter': 1e3} |
|---|
| 13 |
|
|---|
| 14 |
# using default diffInt = 1e-7 is inappropriate: |
|---|
| 15 |
p = NLP(f, x0, c=c, iprint = 100, **someModifiedStopCriteria) |
|---|
| 16 |
r = p.solve('ralg') |
|---|
| 17 |
print r.ff, r.xf # will print something like "6424.9999886000014 [ 15.0000005 4. ]" |
|---|
| 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], iprint = 100, **someModifiedStopCriteria) |
|---|
| 34 |
r = p.solve('ralg') |
|---|
| 35 |
print r.ff, r.xf # "24.999996490694787 [ 1.50000004e+01 8.00004473e+09]" - much better |
|---|
| 36 |
""" |
|---|
| 37 |
Full Output: |
|---|
| 38 |
----------------------------------------------------- |
|---|
| 39 |
solver: ralg problem: unnamed goal: minimum |
|---|
| 40 |
iter objFunVal log10(maxResidual) |
|---|
| 41 |
0 6.976e+03 2.51 |
|---|
| 42 |
51 6.425e+03 -6.10 |
|---|
| 43 |
istop: 4 (|| F[k] - F[k-1] || < ftol) |
|---|
| 44 |
Solver: Time Elapsed = 0.16 CPU Time Elapsed = 0.16 |
|---|
| 45 |
objFunValue: 6424.9999 (feasible, max constraint = 8e-07) |
|---|
| 46 |
6424.999932 [ 15.0000004 4. ] |
|---|
| 47 |
----------------------------------------------------- |
|---|
| 48 |
solver: ralg problem: unnamed goal: minimum |
|---|
| 49 |
iter objFunVal log10(maxResidual) |
|---|
| 50 |
0 6.976e+03 2.51 |
|---|
| 51 |
100 4.419e+01 -5.99 |
|---|
| 52 |
200 2.504e+01 -6.10 |
|---|
| 53 |
300 2.503e+01 -6.10 |
|---|
| 54 |
400 2.503e+01 -6.10 |
|---|
| 55 |
500 2.503e+01 -6.10 |
|---|
| 56 |
506 2.500e+01 -6.91 |
|---|
| 57 |
istop: 3 (|| X[k] - X[k-1] || < xtol) |
|---|
| 58 |
Solver: Time Elapsed = 1.59 CPU Time Elapsed = 1.59 |
|---|
| 59 |
objFunValue: 25.000189 (feasible, max constraint = 1.23911e-07) |
|---|
| 60 |
25.0001894297 [ 1.50000001e+01 8.00137858e+08] |
|---|
| 61 |
""" |
|---|