| 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 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, xtol = 1e-8, ftol = 1e-8) |
|---|
| 69 | p.c = lambda x: abs(x[4]-0.8) + abs(x[5]-1.5) - 0.015 |
|---|
| 70 | #optional: |
|---|
| 71 | #p.plot = 1 |
|---|
| 72 | r = p.solve('ralg') |
|---|
| 73 | print r.xf[:8] |
|---|
| 74 | |
|---|
| 75 | """ |
|---|
| 76 | ----------------------------------------------------- |
|---|
| 77 | solver: ralg problem: unnamed goal: minimum |
|---|
| 78 | iter objFunVal log10(maxResidual) |
|---|
| 79 | 0 2.825e+06 0.02 |
|---|
| 80 | --= non-feasible ralg iter =-- |
|---|
| 81 | itn: 0 |
|---|
| 82 | curr f: [ 2824966.83813157] |
|---|
| 83 | max constraint value 1.04116752789 |
|---|
| 84 | --= non-feasible ralg iter =-- |
|---|
| 85 | itn: 1 |
|---|
| 86 | curr f: [ 2824973.2896607] |
|---|
| 87 | max constraint value 1.75725959686 |
|---|
| 88 | --= non-feasible ralg iter =-- |
|---|
| 89 | itn: 2 |
|---|
| 90 | curr f: [ 2824966.83813157] |
|---|
| 91 | max constraint value 1.04116752789 |
|---|
| 92 | --= non-feasible ralg iter =-- |
|---|
| 93 | itn: 3 |
|---|
| 94 | curr f: [ 2824970.22518437] |
|---|
| 95 | max constraint value 0.413756712605 |
|---|
| 96 | --= non-feasible ralg iter =-- |
|---|
| 97 | itn: 4 |
|---|
| 98 | curr f: [ 2824969.02632034] |
|---|
| 99 | max constraint value 0.0818395397163 |
|---|
| 100 | --= non-feasible ralg iter =-- |
|---|
| 101 | itn: 5 |
|---|
| 102 | curr f: [ 2824969.37414607] |
|---|
| 103 | max constraint value 0.0406513995891 |
|---|
| 104 | --= non-feasible ralg iter =-- |
|---|
| 105 | itn: 6 |
|---|
| 106 | curr f: [ 2824969.20023321] |
|---|
| 107 | max constraint value 0.00849187556755 |
|---|
| 108 | --= non-feasible ralg iter =-- |
|---|
| 109 | itn: 7 |
|---|
| 110 | curr f: [ 2824969.20119103] |
|---|
| 111 | max constraint value 0.00560799704173 |
|---|
| 112 | --= non-feasible ralg iter =-- |
|---|
| 113 | itn: 8 |
|---|
| 114 | curr f: [ 2824969.2065267] |
|---|
| 115 | max constraint value 0.00416641026253 |
|---|
| 116 | --= non-feasible ralg iter =-- |
|---|
| 117 | itn: 9 |
|---|
| 118 | curr f: [ 2824969.22185181] |
|---|
| 119 | max constraint value 0.0421905566026 |
|---|
| 120 | --= non-feasible ralg iter =-- |
|---|
| 121 | itn: 10 |
|---|
| 122 | curr f: [ 2824969.2065267] |
|---|
| 123 | max constraint value 0.00416641026253 |
|---|
| 124 | --= non-feasible ralg iter =-- |
|---|
| 125 | itn: 11 |
|---|
| 126 | curr f: [ 2824969.20952515] |
|---|
| 127 | max constraint value 0.00327175155207 |
|---|
| 128 | 100 2.665e+04 -100.00 |
|---|
| 129 | 200 4.845e+03 -100.00 |
|---|
| 130 | 300 1.947e+02 -100.00 |
|---|
| 131 | 400 9.298e+01 -100.00 |
|---|
| 132 | 500 5.160e+01 -100.00 |
|---|
| 133 | 600 2.600e+01 -100.00 |
|---|
| 134 | 700 1.070e+01 -100.00 |
|---|
| 135 | 800 6.994e+00 -100.00 |
|---|
| 136 | 900 5.375e+00 -100.00 |
|---|
| 137 | 1000 5.375e+00 -100.00 |
|---|
| 138 | 1094 5.375e+00 -100.00 |
|---|
| 139 | istop: 4 (|| F[k] - F[k-1] || < ftol) |
|---|
| 140 | Solver: Time Elapsed = 4.62 CPU Time Elapsed = 4.48 |
|---|
| 141 | objFunValue: 5.3748608 (feasible, max constraint = 0) |
|---|
| 142 | [ -1.06086135e-07 5.65437885e-08 -1.29682567e-07 6.12571176e-09 |
|---|
| 143 | 7.95256506e-01 1.49731951e+00 -1.42518171e-09 4.15961658e-08] |
|---|
| 144 | """ |
|---|