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

Revision 1547, 4.0 kB (checked in by dmitrey.kroshko, 2 months ago)

/examples/algencan.py: ALGENCAN renamed to algencan

Line 
1 from scikits.openopt import NLP
2 from numpy import cos, arange, ones, asarray, zeros, mat, array
3
4 N = 50
5 # objfunc:
6 # (x0-1)^4 + (x2-1)^4 + ... +(x49-1)^4 -> min (N=nVars=50)
7 f = lambda x : ((x-1)**4).sum()
8 x0 = cos(arange(N))
9 p = NLP(f, x0, maxIter = 1e3, maxFunEvals = 1e5)
10
11 # f(x) gradient (optional):
12 p.df = lambda x: 4*(x-1)**3
13
14
15 # lb<= x <= ub:
16 # x4 <= -2.5
17 # 3.5 <= x5 <= 4.5
18 # all other: lb = -5, ub = +15
19 p.lb = -5*ones(p.n)
20 p.ub = 15*ones(p.n)
21 p.ub[4] = -2.5
22 p.lb[5], p.ub[5] = 3.5, 4.5
23
24
25
26 # Ax <= b
27 # x0+...+xN>= 1.1*N
28 # x9 + x19 <= 1.5
29 # x10+x11 >= 1.6
30 p.A = zeros((3, p.n))
31 p.A[0, 9] = 1
32 p.A[0, 19] = 1
33 p.A[1, 10:12] = -1
34 p.A[2] = -ones(p.n)
35 p.b = [1.5, -1.6, -1.1*N]
36 # you can use any types of A, Aeq, b, beq:
37 # Python list, numpy.array, numpy.matrix, even Python touple
38 # so p.b = array([1.5, -1.6, -825]) or p.b = (1.5, -1.6, -825) are valid as well
39
40
41 # Aeq x = beq
42 # x20+x21 = 2.5
43 p.Aeq = zeros(p.n)
44 p.Aeq[20:22] = 1
45 p.beq = 2.5
46
47
48 # non-linear inequality constraints c(x) <= 0
49 # 2*x0^4 <= 1/32
50 # x1^2+x2^2 <= 1/8
51 # x25^2 +x25*x35 + x35^2<= 2.5
52
53 p.c = lambda x: [2* x[0] **4-1./32, x[1]**2+x[2]**2 - 1./8, x[25]**2 + x[35]**2 + x[25]*x[35] -2.5]
54 # other valid c:
55 # p.c = [lambda x: c1(x), lambda x : c2(x), lambda x : c3(x)]
56 # p.c = (lambda x: c1(x), lambda x : c2(x), lambda x : c3(x))
57 # p.c = lambda x: numpy.array(c1(x), c2(x), c3(x))
58 # def c(x):
59 #      return c1(x), c2(x), c3(x)
60 # p.c = c
61
62
63 # dc(x)/dx: non-lin ineq constraints gradients (optional):
64 def DC(x):
65     r = zeros((3, p.n))
66     r[0,0] = 2 * 4 * x[0]**3
67     r[1,1] = 2 * x[1]
68     r[1,2] = 2 * x[2]
69     r[2,25] = 2*x[25] + x[35]
70     r[2,35] = 2*x[35] + x[25]
71     return r
72 p.dc = DC
73
74 # non-linear equality constraints h(x) = 0
75 # 1e6*(x[last]-1)**4 = 0
76 # (x[last-1]-1.5)**4 = 0
77 h1 = lambda x: 1e4*(x[-1]-1)**4
78 h2 = lambda x: (x[-2]-1.5)**4
79 p.h = [h1, h2]
80 # dh(x)/dx: non-lin eq constraints gradients (optional):
81 def DH(x):
82     r = zeros((2, p.n))
83     r[0, -1] = 1e4*4 * (x[-1]-1)**3
84     r[1, -2] = 4 * (x[-2]-1.5)**3
85     return r
86 p.dh = DH
87
88 p.contol = 1e-3 # required constraints tolerance, default for NLP is 1e-6
89
90 # for ALGENCAN solver gradtol is the only one stop criterium connected to openopt
91 # (except maxfun, maxiter)
92 # Note that in ALGENCAN gradtol means norm of projected gradient of  the Augmented Lagrangian
93 # so it should be something like 1e-3...1e-5
94 p.gradtol = 1e-5 # gradient stop criterium (default for NLP is 1e-6)
95
96
97 # see also: help(NLP) -> maxTime, maxCPUTime, ftol and xtol
98 # that are connected to / used in lincher and some other solvers
99
100 # optional: check of user-supplied derivatives
101 p.checkdf()
102 p.checkdc()
103 p.checkdh()
104
105 # last but not least:
106 # please don't forget,
107 # Python indexing starts from ZERO!!
108
109 p.plot = 0
110 p.iprint = 0
111 r = p.solve('algencan')
112
113 #r = p.solve('ralg')
114 #r = p.solve('lincher')
115
116 """
117 typical output:
118 OpenOpt checks user-supplied gradient df (size: (50,))
119 according to:
120 prob.diffInt = 1e-07
121 prob.check.maxViolation = 1e-05
122 max(abs(df_user - df_numerical)) = 2.50111104094e-06
123 (is registered in df number 41)
124 sum(abs(df_user - df_numerical)) = 4.45203815948e-05
125 ========================
126 OpenOpt checks user-supplied gradient dc (size: (50, 3))
127 according to:
128 prob.diffInt = 1e-07
129 prob.check.maxViolation = 1e-05
130 max(abs(dc_user - dc_numerical)) = 1.20371180401e-06
131 (is registered in dc number 0)
132 sum(abs(dc_user - dc_numerical)) = 1.60141862837e-06
133 ========================
134 OpenOpt checks user-supplied gradient dh (size: (50, 2))
135 according to:
136 prob.diffInt = 1e-07
137 prob.check.maxViolation = 1e-05
138 dh num   i,j:dh[i]/dx[j]   user-supplied     numerical         difference
139      98            49 / 0         -1.369e+04     -1.369e+04     -2.941e-03
140 max(abs(dh_user - dh_numerical)) = 0.00294061290697
141 (is registered in dh number 98)
142 sum(abs(dh_user - dh_numerical)) = 0.00294343472179
143 ========================
144 starting solver ALGENCAN (GPL  license)  with problem  unnamed
145 solver ALGENCAN has finished solving the problem unnamed
146 istop:  1000
147 Solver:   Time elapsed = 0.34   CPU Time Elapsed = 0.34
148 objFunValue: 190.041570332 (feasible, max constraint =  0.000677961)
149 """
Note: See TracBrowser for help on using the browser.