|
Revision 1504, 1.9 kB
(checked in by dmitrey.kroshko, 2 months ago)
|
add converter nlsp2nlp
|
| Line | |
|---|
| 1 |
""" |
|---|
| 2 |
Solving system of equations: |
|---|
| 3 |
x[0]**3+x[1]**3-9 = 0 |
|---|
| 4 |
x[0]-0.5*x[1] = 0 |
|---|
| 5 |
cos(x[2])+x[0]-1.5 = 0 |
|---|
| 6 |
with some constraints: |
|---|
| 7 |
150 <= x[2] <= 158 |
|---|
| 8 |
and possible non-linear constraint: |
|---|
| 9 |
(x[2] - 150.8)**2 <= 1.5 |
|---|
| 10 |
|
|---|
| 11 |
Note: |
|---|
| 12 |
1. Using Ax <= b constraints is also allowed |
|---|
| 13 |
2. You can try using equality constraints (h(x)=0, Aeq x = beq) as well. |
|---|
| 14 |
3. Required function tolerance is p.ftol, constraints tolerance is p.contol, |
|---|
| 15 |
and hence using h(x)=0 constraints is not 100% same |
|---|
| 16 |
to some additional f coords |
|---|
| 17 |
""" |
|---|
| 18 |
|
|---|
| 19 |
from scikits.openopt import NLSP |
|---|
| 20 |
from numpy import asfarray, zeros, cos, sin |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
f = lambda x: (x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5) |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
def df(x): |
|---|
| 31 |
df = zeros((3,3)) |
|---|
| 32 |
df[0,0] = 3*x[0]**2 |
|---|
| 33 |
df[0,1] = 3*x[1]**2 |
|---|
| 34 |
df[1,0] = 1 |
|---|
| 35 |
df[1,1] = -0.5 |
|---|
| 36 |
df[2,0] = 1 |
|---|
| 37 |
df[2,2] = -sin(x[2]) |
|---|
| 38 |
return df |
|---|
| 39 |
|
|---|
| 40 |
x0 = [8,15, 80] |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
p = NLSP(f, x0, df = df, maxFunEvals = 1e5, iprint = 10, plot=1, ftol = 1e-8, contol=1e-15) |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
p.lb[2] = 150 |
|---|
| 56 |
p.ub[2] = 158 |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
p.c = lambda x: (x[2] - 150.8)**2-1.5 |
|---|
| 60 |
|
|---|
| 61 |
p.dc = lambda x: asfarray((0, 0, 2*(x[2]-150.8))) |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
r = p.solve('nlp:ralg') |
|---|
| 70 |
|
|---|
| 71 |
print 'solution:', r.xf |
|---|
| 72 |
print 'max residual:', r.ff |
|---|