root/trunk/openopt/scikits/openopt/examples/nlp_1.py

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 # 1st arg - objective function
35 # 2nd arg - x0
36 p = NLP(lambda x: ((x-5)**2).sum(), 8*cos(arange(N)), iprint = 50, maxIter = 1e3)
37
38 # f(x) gradient (optional):
39 p.df = lambda x: 2*(x-5)
40
41 # c(x) <= 0 constraints
42 p.c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8]
43
44 # dc(x)/dx: non-lin ineq constraints gradients (optional):
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 # h(x) = 0 constraints
54 h1 = lambda x: (x[149]-1)**6
55 h2 = lambda x: (x[148]-1.5)**6
56 p.h = [h1, h2]
57
58 ### dh(x)/dx: non-lin eq constraints gradients (optional):
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 ##p.ftol = 1e-4# one of stop criteria, default 1e-6
84 ##p.xtol = 1e-5# one of stop criteria, default 1e-6
85
86 p.contol = 1e-3 # required constraints tolerance, default for NLP is 1e-6
87
88 # ALGENCAN solver ignores xtol and ftol; using maxTime, maxCPUTime, maxIter, maxFunEvals, fEnough is recommended.
89
90 # Note that in algencan gradtol means norm of projected gradient of  the Augmented Lagrangian
91 # so it should be something like 1e-3...1e-5
92 p.gtol = 1e-7 # (default gtol = 1e-6)
93
94 ##p.debug = 1
95
96 #optional: user-supplied 1st derivatives check
97 p.checkdf()
98 p.checkdc()
99 p.checkdh()
100
101
102 p.maxIter = 10000
103
104 #optional: graphic output, requires pylab (matplotlib)
105 p.plot = 1
106 p.maxFunEvals = 1e7
107
108 r = p.solve('ralg')
109
110 # r.xf and r.ff are optim point and optim objFun value
111 # r.ff should be something like 128.08949
Note: See TracBrowser for help on using the browser.