| 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 |
|
|---|
| 26 |
|
|---|
| 27 |
if p.rk > p.contol: |
|---|
| 28 |
print '--= non-feasible ralg iter =--' |
|---|
| 29 |
print 'itn:', p.iter |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
print 'curr f:', p.fk |
|---|
| 33 |
|
|---|
| 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 |
|
|---|
| 51 |
return (15, 'value obtained is enough' ) |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
else: |
|---|
| 57 |
return False |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
from scikits.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 |
p = NSP(f, x0, df=df, callback=MyIterFcn, contol = 1e-5, maxIter = 1e4, iprint = 100) |
|---|
| 69 |
p.c = lambda x: abs(x[4]-0.8) + abs(x[5]-1.5) - 0.015 |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
r = p.solve('ralg') |
|---|
| 73 |
print r.xf[:8] |
|---|
| 74 |
|
|---|
| 75 |
""" |
|---|
| 76 |
starting solver ralg (license: BSD) with problem unnamed |
|---|
| 77 |
itn 0 : Fk= 2824966.83813 MaxResidual= 1.04116752789 |
|---|
| 78 |
--= non-feasible ralg iter =-- |
|---|
| 79 |
itn: 0 |
|---|
| 80 |
curr f: [ 2824966.83813157] |
|---|
| 81 |
max constraint value 1.04116752789 |
|---|
| 82 |
--= non-feasible ralg iter =-- |
|---|
| 83 |
itn: 1 |
|---|
| 84 |
curr f: [ 2824970.06389614] |
|---|
| 85 |
max constraint value 0.343046034487 |
|---|
| 86 |
--= non-feasible ralg iter =-- |
|---|
| 87 |
itn: 2 |
|---|
| 88 |
curr f: [ 2824968.85423442] |
|---|
| 89 |
max constraint value 0.157284051403 |
|---|
| 90 |
--= non-feasible ralg iter =-- |
|---|
| 91 |
itn: 3 |
|---|
| 92 |
curr f: [ 2824969.45906528] |
|---|
| 93 |
max constraint value 0.0778809915417 |
|---|
| 94 |
--= non-feasible ralg iter =-- |
|---|
| 95 |
itn: 4 |
|---|
| 96 |
curr f: [ 2824969.15664985] |
|---|
| 97 |
max constraint value 0.0247015299307 |
|---|
| 98 |
--= non-feasible ralg iter =-- |
|---|
| 99 |
itn: 5 |
|---|
| 100 |
curr f: [ 2824969.30785756] |
|---|
| 101 |
max constraint value 0.0115897308055 |
|---|
| 102 |
--= non-feasible ralg iter =-- |
|---|
| 103 |
itn: 6 |
|---|
| 104 |
curr f: [ 2824969.23225371] |
|---|
| 105 |
max constraint value 0.00849189931885 |
|---|
| 106 |
--= non-feasible ralg iter =-- |
|---|
| 107 |
itn: 7 |
|---|
| 108 |
curr f: [ 2824969.33884846] |
|---|
| 109 |
max constraint value 0.492613712337 |
|---|
| 110 |
--= non-feasible ralg iter =-- |
|---|
| 111 |
itn: 8 |
|---|
| 112 |
curr f: [ 2824969.23225371] |
|---|
| 113 |
max constraint value 0.00849189931885 |
|---|
| 114 |
--= non-feasible ralg iter =-- |
|---|
| 115 |
itn: 9 |
|---|
| 116 |
curr f: [ 2824969.2589024] |
|---|
| 117 |
max constraint value 0.0942845035952 |
|---|
| 118 |
--= non-feasible ralg iter =-- |
|---|
| 119 |
itn: 10 |
|---|
| 120 |
curr f: [ 2824969.23225371] |
|---|
| 121 |
max constraint value 0.00849189931885 |
|---|
| 122 |
itn 100 : Fk= 17069.511925 MaxResidual=0.00e+00 ls= 0 |
|---|
| 123 |
itn 200 : Fk= 3414.10073216 MaxResidual=0.00e+00 ls= 0 |
|---|
| 124 |
itn 300 : Fk= 677.859710072 MaxResidual=0.00e+00 ls= 0 |
|---|
| 125 |
itn 400 : Fk= 161.346642229 MaxResidual=0.00e+00 ls= 0 |
|---|
| 126 |
itn 500 : Fk= 60.7027237393 MaxResidual=0.00e+00 ls= 0 |
|---|
| 127 |
--= non-feasible ralg iter =-- |
|---|
| 128 |
itn: 578 |
|---|
| 129 |
curr f: [ 27.91842239] |
|---|
| 130 |
max constraint value 6.24426861277e-05 |
|---|
| 131 |
itn 600 : Fk= 21.6764999085 MaxResidual=0.00e+00 ls= 0 |
|---|
| 132 |
itn 700 : Fk= 12.5169643207 MaxResidual=0.00e+00 ls= 0 |
|---|
| 133 |
itn 800 : Fk= 7.57478371116 MaxResidual=0.00e+00 ls= 0 |
|---|
| 134 |
itn 900 : Fk= 6.62378055943 MaxResidual=0.00e+00 ls= 0 |
|---|
| 135 |
--= non-feasible ralg iter =-- |
|---|
| 136 |
itn: 927 |
|---|
| 137 |
curr f: [ 6.16120658] |
|---|
| 138 |
max constraint value 0.000134846161715 |
|---|
| 139 |
itn 1000 : Fk= 5.71836744086 MaxResidual=0.00e+00 ls= 0 |
|---|
| 140 |
--= non-feasible ralg iter =-- |
|---|
| 141 |
itn: 1073 |
|---|
| 142 |
curr f: [ 5.5631041] |
|---|
| 143 |
max constraint value 9.85148917757e-05 |
|---|
| 144 |
--= non-feasible ralg iter =-- |
|---|
| 145 |
itn: 1093 |
|---|
| 146 |
curr f: [ 5.52060738] |
|---|
| 147 |
max constraint value 1.27847590301e-05 |
|---|
| 148 |
itn 1100 : Fk= 5.52098442967 MaxResidual=0.00e+00 ls= 0 |
|---|
| 149 |
itn 1200 : Fk= 5.41042442915 MaxResidual=0.00e+00 ls= 0 |
|---|
| 150 |
--= non-feasible ralg iter =-- |
|---|
| 151 |
itn: 1249 |
|---|
| 152 |
curr f: [ 5.39170524] |
|---|
| 153 |
max constraint value 1.83549094677e-05 |
|---|
| 154 |
--= non-feasible ralg iter =-- |
|---|
| 155 |
itn: 1273 |
|---|
| 156 |
curr f: [ 5.38684431] |
|---|
| 157 |
max constraint value 9.76485916604e-05 |
|---|
| 158 |
itn 1300 : Fk= 5.37711869183 MaxResidual=0.00e+00 ls= 0 |
|---|
| 159 |
--= non-feasible ralg iter =-- |
|---|
| 160 |
itn: 1398 |
|---|
| 161 |
curr f: [ 5.36265833] |
|---|
| 162 |
max constraint value 1.19372030116e-05 |
|---|
| 163 |
itn 1400 : Fk= 5.36252872692 MaxResidual=0.00e+00 ls= 0 |
|---|
| 164 |
--= non-feasible ralg iter =-- |
|---|
| 165 |
itn: 1429 |
|---|
| 166 |
curr f: [ 5.36105646] |
|---|
| 167 |
max constraint value 1.92304779728e-05 |
|---|
| 168 |
itn 1500 : Fk= 5.35823803209 MaxResidual=0.00e+00 ls= 0 |
|---|
| 169 |
--= non-feasible ralg iter =-- |
|---|
| 170 |
itn: 1548 |
|---|
| 171 |
curr f: [ 5.35651366] |
|---|
| 172 |
max constraint value 1.04851597451e-05 |
|---|
| 173 |
--= non-feasible ralg iter =-- |
|---|
| 174 |
itn: 1583 |
|---|
| 175 |
curr f: [ 5.3557011] |
|---|
| 176 |
max constraint value 1.39563678882e-05 |
|---|
| 177 |
itn 1600 : Fk= 5.35573186663 MaxResidual=0.00e+00 ls= 0 |
|---|
| 178 |
itn 1601 : Fk= 5.35573213507 MaxResidual=0.00e+00 ls= 0 |
|---|
| 179 |
solver ralg has finished solving the problem unnamed |
|---|
| 180 |
istop: 4 (|| F[k] - F[k-1] || < ftol) |
|---|
| 181 |
Solver: Time Elapsed = 3.83 CPU Time Elapsed = 3.46 |
|---|
| 182 |
objFunValue: 5.35573213507 (feasible, max constraint = 0) |
|---|
| 183 |
[ -4.01239877e-05 -2.75097558e-05 -2.59255724e-08 -1.87457124e-05 |
|---|
| 184 |
7.99947105e-01 1.48505533e+00 -5.29523297e-06 -2.74567562e-06] |
|---|
| 185 |
""" |
|---|