|
Revision 1386, 2.1 kB
(checked in by dmitrey.kroshko, 1 month ago)
|
soem changes in examples/nlp1
|
| Line | |
|---|
| 1 |
""" |
|---|
| 2 |
Example: |
|---|
| 3 |
(x0-5)^2 + (x2-5)^2 + ... +(x149-5)^2 -> min |
|---|
| 4 |
|
|---|
| 5 |
subjected to |
|---|
| 6 |
|
|---|
| 7 |
# lb<= x <= ub: |
|---|
| 8 |
x4 <= 4 |
|---|
| 9 |
8 <= x5 <= 15 |
|---|
| 10 |
|
|---|
| 11 |
# Ax <= b |
|---|
| 12 |
x0+...+x149 >= 825 |
|---|
| 13 |
x9 + x19 <= 3 |
|---|
| 14 |
x10+x11 <= 9 |
|---|
| 15 |
|
|---|
| 16 |
# Aeq x = beq |
|---|
| 17 |
x100+x101 = 11 |
|---|
| 18 |
|
|---|
| 19 |
# c(x) <= 0 |
|---|
| 20 |
2*x0^4-32 <= 0 |
|---|
| 21 |
x1^2+x2^2-8 <= 0 |
|---|
| 22 |
|
|---|
| 23 |
# h(x) = 0 |
|---|
| 24 |
(x[149]-1)**6 = 0 |
|---|
| 25 |
(x[148]-1.5)**6 = 0 |
|---|
| 26 |
""" |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
from scikits.openopt import NLP |
|---|
| 30 |
|
|---|
| 31 |
from numpy import cos, arange, ones, asarray, zeros, mat, array |
|---|
| 32 |
N = 150 |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
p = NLP(lambda x: ((x-5)**2).sum(), 8*cos(arange(N)), iprint = 50, maxIter = 1e3) |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
p.df = lambda x: 2*(x-5) |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
p.c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8] |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
def DC(x): |
|---|
| 46 |
r = zeros((len(p.c(p.x0)), p.n)) |
|---|
| 47 |
r[0,0] = 2 * 4 * x[0]**3 |
|---|
| 48 |
r[1,1] = 2 * x[1] |
|---|
| 49 |
r[1,2] = 2 * x[2] |
|---|
| 50 |
return r |
|---|
| 51 |
p.dc = DC |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
h1 = lambda x: (x[149]-1)**6 |
|---|
| 55 |
h2 = lambda x: (x[148]-1.5)**6 |
|---|
| 56 |
p.h = [h1, h2] |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
def DH(x): |
|---|
| 60 |
r = zeros((2, p.n)) |
|---|
| 61 |
r[0, -1] = 6*(x[149]-1)**5 |
|---|
| 62 |
r[1, -2] = 6*(x[148]-1.5)**5 |
|---|
| 63 |
return r |
|---|
| 64 |
p.dh = DH |
|---|
| 65 |
|
|---|
| 66 |
p.lb = -6*ones(p.n) |
|---|
| 67 |
p.ub = 6*ones(p.n) |
|---|
| 68 |
p.ub[4] = 4 |
|---|
| 69 |
p.lb[5], p.ub[5] = 8, 15 |
|---|
| 70 |
|
|---|
| 71 |
p.A = zeros((3, p.n)) |
|---|
| 72 |
p.A[0, 9] = 1 |
|---|
| 73 |
p.A[0, 19] = 1 |
|---|
| 74 |
p.A[1, 10:12] = 1 |
|---|
| 75 |
p.A[2] = -ones(p.n) |
|---|
| 76 |
p.b = [7, 9, -825] |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
p.Aeq = zeros(p.n) |
|---|
| 80 |
p.Aeq[100:102] = 1 |
|---|
| 81 |
p.beq = 11 |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
p.contol = 1e-3 |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
p.gtol = 1e-7 |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
p.checkdf() |
|---|
| 98 |
p.checkdc() |
|---|
| 99 |
p.checkdh() |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
p.maxIter = 10000 |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
p.plot = 1 |
|---|
| 106 |
p.maxFunEvals = 1e7 |
|---|
| 107 |
|
|---|
| 108 |
r = p.solve('ralg') |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|