| 1 |
""" |
|---|
| 2 |
usage: |
|---|
| 3 |
p = someOOclass(..., callback=MyIterFcn, ...) |
|---|
| 4 |
or |
|---|
| 5 |
p = ... |
|---|
| 6 |
p.callback = MyIterFcn |
|---|
| 7 |
or p.callback = (MyIterFcn1, MyIterFcn2, MyIterFcn3, ..., MyIterFcnN) |
|---|
| 8 |
or p.callback = [MyIterFcn1, MyIterFcn2, MyIterFcn3, ..., MyIterFcnN] |
|---|
| 9 |
|
|---|
| 10 |
each user-defined function MyIterFunc should return one of the following: |
|---|
| 11 |
|
|---|
| 12 |
1. a flag value - 0, 1, True, False |
|---|
| 13 |
flag = True or 1 means user want to stop calculations |
|---|
| 14 |
(r.istop=80, r.msg = 'user-defined' ) |
|---|
| 15 |
|
|---|
| 16 |
2. someRealValue like 15 or 80.15 or 1.5e4 (r.istop=someRealValue, r.msg = 'user-defined') |
|---|
| 17 |
|
|---|
| 18 |
3. Python list (or tuple) - [istop, msg] (r.istop=istop, r.msg=msg) |
|---|
| 19 |
|
|---|
| 20 |
works for ralg and lincher, but may doesn't work for some other solvers |
|---|
| 21 |
(like scipy_cobyla, that has neither native callback nor call gradient) |
|---|
| 22 |
""" |
|---|
| 23 |
|
|---|
| 24 |
def MyIterFcn(p): |
|---|
| 25 |
# observing non-feasible ralg iter points |
|---|
| 26 |
|
|---|
| 27 |
if p.rk > p.contol: # p.rk is current iter max residual |
|---|
| 28 |
print '--= non-feasible ralg iter =--' |
|---|
| 29 |
print 'itn:', p.iter |
|---|
| 30 |
#however, I inted to change p.iter to p.iter in OpenOpt code soon |
|---|
| 31 |
|
|---|
| 32 |
print 'curr f:', p.fk |
|---|
| 33 |
# print 'curr x[:8]:', p.xk[:8] |
|---|
| 34 |
print 'max constraint value', p.rk |
|---|
| 35 |
|
|---|
| 36 |
""" |
|---|
| 37 |
BTW you can store data in any unique field of p |
|---|
| 38 |
for example |
|---|
| 39 |
if some_cond: p.JohnSmith = 15 |
|---|
| 40 |
else: p.JohnSmith = 0 |
|---|
| 41 |
|
|---|
| 42 |
However, special field "user" is intended for the purpose: |
|---|
| 43 |
p.user.mydata1 = (something) |
|---|
| 44 |
# or, for another example: |
|---|
| 45 |
if p.iter == 0: p.user.mylist = [] |
|---|
| 46 |
p.user.mylist.append(something) |
|---|
| 47 |
""" |
|---|
| 48 |
|
|---|
| 49 |
if p.fk < 1.5 and p.rk < p.contol: |
|---|
| 50 |
#NB! you could use p.fEnough = 15, p.contol=1e-5 in prob assignment instead |
|---|
| 51 |
return (15, 'value obtained is enough' ) |
|---|
| 52 |
# or |
|---|
| 53 |
# return 15 (hence r.istop=15, r.msg='user-defined') |
|---|
| 54 |
# or return True (hence r.istop=80, r.msg='user-defined') |
|---|
| 55 |
# or return 1 (hence r.istop = 80, r.msg='user-defined') |
|---|
| 56 |
else: |
|---|
| 57 |
return False |
|---|
| 58 |
# or |
|---|
| 59 |
# return 0 |
|---|
| 60 |
|
|---|
| 61 |
from openopt import NSP |
|---|
| 62 |
from numpy import cos, asfarray, arange, sign |
|---|
| 63 |
N = 75 |
|---|
| 64 |
f = lambda x: sum(1.2 ** arange(len(x)) * abs(x)) |
|---|
| 65 |
df = lambda x: 1.2 ** arange(len(x)) * sign(x) |
|---|
| 66 |
x0 = cos(1+asfarray(range(N))) |
|---|
| 67 |
|
|---|
| 68 |
#non-linear constraint c(x) <= 0: |
|---|
| 69 |
c = lambda x: abs(x[4]-0.8) + abs(x[5]-1.5) - 0.015 |
|---|
| 70 |
|
|---|
| 71 |
p = NSP(f, x0, df=df, c=c, callback=MyIterFcn, contol = 1e-5, maxIter = 1e4, iprint = 100, xtol = 1e-8, ftol = 1e-8) |
|---|
| 72 |
|
|---|
| 73 |
#optional: |
|---|
| 74 |
#p.plot = 1 |
|---|
| 75 |
r = p.solve('ralg') |
|---|
| 76 |
print r.xf[:8] |
|---|
| 77 |
|
|---|
| 78 |
""" |
|---|
| 79 |
----------------------------------------------------- |
|---|
| 80 |
solver: ralg problem: unnamed goal: minimum |
|---|
| 81 |
iter objFunVal log10(maxResidual) |
|---|
| 82 |
0 2.825e+06 0.02 |
|---|
| 83 |
--= non-feasible ralg iter =-- |
|---|
| 84 |
itn: 0 |
|---|
| 85 |
curr f: [ 2824966.83813157] |
|---|
| 86 |
max constraint value 1.04116752789 |
|---|
| 87 |
--= non-feasible ralg iter =-- |
|---|
| 88 |
itn: 1 |
|---|
| 89 |
curr f: [ 2824973.2896607] |
|---|
| 90 |
max constraint value 1.75725959686 |
|---|
| 91 |
--= non-feasible ralg iter =-- |
|---|
| 92 |
itn: 2 |
|---|
| 93 |
curr f: [ 2824966.83813157] |
|---|
| 94 |
max constraint value 1.04116752789 |
|---|
| 95 |
--= non-feasible ralg iter =-- |
|---|
| 96 |
itn: 3 |
|---|
| 97 |
curr f: [ 2824970.22518437] |
|---|
| 98 |
max constraint value 0.413756712605 |
|---|
| 99 |
--= non-feasible ralg iter =-- |
|---|
| 100 |
itn: 4 |
|---|
| 101 |
curr f: [ 2824969.02632034] |
|---|
| 102 |
max constraint value 0.0818395397163 |
|---|
| 103 |
--= non-feasible ralg iter =-- |
|---|
| 104 |
itn: 5 |
|---|
| 105 |
curr f: [ 2824969.37414607] |
|---|
| 106 |
max constraint value 0.0406513995891 |
|---|
| 107 |
--= non-feasible ralg iter =-- |
|---|
| 108 |
itn: 6 |
|---|
| 109 |
curr f: [ 2824969.20023321] |
|---|
| 110 |
max constraint value 0.00849187556755 |
|---|
| 111 |
--= non-feasible ralg iter =-- |
|---|
| 112 |
itn: 7 |
|---|
| 113 |
curr f: [ 2824969.20119103] |
|---|
| 114 |
max constraint value 0.00560799704173 |
|---|
| 115 |
--= non-feasible ralg iter =-- |
|---|
| 116 |
itn: 8 |
|---|
| 117 |
curr f: [ 2824969.2065267] |
|---|
| 118 |
max constraint value 0.00416641026253 |
|---|
| 119 |
--= non-feasible ralg iter =-- |
|---|
| 120 |
itn: 9 |
|---|
| 121 |
curr f: [ 2824969.22185181] |
|---|
| 122 |
max constraint value 0.0421905566026 |
|---|
| 123 |
--= non-feasible ralg iter =-- |
|---|
| 124 |
itn: 10 |
|---|
| 125 |
curr f: [ 2824969.2065267] |
|---|
| 126 |
max constraint value 0.00416641026253 |
|---|
| 127 |
--= non-feasible ralg iter =-- |
|---|
| 128 |
itn: 11 |
|---|
| 129 |
curr f: [ 2824969.20952515] |
|---|
| 130 |
max constraint value 0.00327175155207 |
|---|
| 131 |
100 2.665e+04 -100.00 |
|---|
| 132 |
200 4.845e+03 -100.00 |
|---|
| 133 |
300 1.947e+02 -100.00 |
|---|
| 134 |
400 9.298e+01 -100.00 |
|---|
| 135 |
500 5.160e+01 -100.00 |
|---|
| 136 |
600 2.600e+01 -100.00 |
|---|
| 137 |
700 1.070e+01 -100.00 |
|---|
| 138 |
800 6.994e+00 -100.00 |
|---|
| 139 |
900 5.375e+00 -100.00 |
|---|
| 140 |
1000 5.375e+00 -100.00 |
|---|
| 141 |
1094 5.375e+00 -100.00 |
|---|
| 142 |
istop: 4 (|| F[k] - F[k-1] || < ftol) |
|---|
| 143 |
Solver: Time Elapsed = 4.62 CPU Time Elapsed = 4.48 |
|---|
| 144 |
objFunValue: 5.3748608 (feasible, max constraint = 0) |
|---|
| 145 |
[ -1.06086135e-07 5.65437885e-08 -1.29682567e-07 6.12571176e-09 |
|---|
| 146 |
7.95256506e-01 1.49731951e+00 -1.42518171e-09 4.15961658e-08] |
|---|
| 147 |
""" |
|---|