| 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 | # from all OO-connected NLP solvers |
|---|
| 26 | # only ralg can handle x0 out of dom objFunc: |
|---|
| 27 | # x0 = n+15*(cos(arange(n))) |
|---|
| 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 | # suppose we don't know that a <= b <=> a^3 <= b^3 |
|---|
| 35 | # elseware it could be simplified to box-bound constraints |
|---|
| 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 | #p.checkdf() |
|---|
| 44 | #p.checkdc() |
|---|
| 45 | r = p.solve(solver) |
|---|
| 46 | # expected r.xf = [0, 1, 2, ...] |
|---|