| 1 |
""" |
|---|
| 2 |
Some non-linear functions have much more restricted dom than R^nVars. |
|---|
| 3 |
For example F(x) = log(x); dom F = R+ = {x: x>0} |
|---|
| 4 |
|
|---|
| 5 |
For optimization solvers it is wont to expect user-povided F(x) = nan if x is out of dom. |
|---|
| 6 |
|
|---|
| 7 |
I can't inform how succsesfully OO-connected solvers |
|---|
| 8 |
will handle a prob instance with restricted dom |
|---|
| 9 |
because it seems to be too prob-specific |
|---|
| 10 |
|
|---|
| 11 |
Still I can inform that ralg handles the problems rather well |
|---|
| 12 |
provided in every point x from R^nVars at least one ineq constraint is active |
|---|
| 13 |
(i.e. value constr[i](x) belongs to R+) |
|---|
| 14 |
|
|---|
| 15 |
Note also that some solvers require x0 inside dom objFunc. |
|---|
| 16 |
For ralg it doesn't matter. |
|---|
| 17 |
""" |
|---|
| 18 |
|
|---|
| 19 |
from numpy import * |
|---|
| 20 |
from scikits.openopt import NLP |
|---|
| 21 |
|
|---|
| 22 |
n = 15 |
|---|
| 23 |
x0 = n+15*(1+cos(arange(n))) |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
f = lambda x: (x**2).sum() + sqrt(x**3-arange(n)**3).sum() |
|---|
| 30 |
df = lambda x: 2*x + 0.5*3*x**2/sqrt(x**3-arange(n)**3) |
|---|
| 31 |
c = [] |
|---|
| 32 |
dc = [] |
|---|
| 33 |
for i in xrange(n): |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
c += [lambda x, i=i: i**3-x[i]**3] |
|---|
| 37 |
dc += [lambda x, i=i: hstack((zeros(i), -3*x[i]**2, zeros(n-i-1)))] |
|---|
| 38 |
|
|---|
| 39 |
solvers = ['ralg', 'ipopt'] |
|---|
| 40 |
for solver in solvers: |
|---|
| 41 |
p = NLP(f, x0, df=df, c=c, dc=dc, iprint = 100, maxIter = 10000, maxFunEvals = 1e8, xtol=4e-7) |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
r = p.solve(solver) |
|---|
| 46 |
|
|---|