3
|
1 // NPSOL.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifndef NPSOL_MISSING |
|
25 |
|
26 #ifdef __GNUG__ |
|
27 #pragma implementation |
|
28 #endif |
|
29 |
|
30 #include <iostream.h> |
|
31 #include <math.h> |
|
32 #include "NPSOL.h" |
|
33 #include "f77-uscore.h" |
|
34 #include "sun-utils.h" |
|
35 |
|
36 extern "C" |
|
37 { |
|
38 int F77_FCN (npoptn) (char *, long); |
|
39 |
|
40 int F77_FCN (npsol) (int *, int *, int *, int *, int *, int *, |
|
41 double *, double *, double *, int (*)(), |
|
42 int (*)(), int *, int *, int *, double *, |
|
43 double *, double *, double *, double *, |
|
44 double *, double *, int *, int *, double *, |
|
45 int *); |
|
46 } |
|
47 |
|
48 static objective_fcn user_phi; |
|
49 static gradient_fcn user_grad; |
|
50 static nonlinear_fcn user_g; |
|
51 static jacobian_fcn user_jac; |
|
52 |
|
53 int |
|
54 npsol_objfun (int *mode, int *n, double *xx, double *objf, |
|
55 double *objgrd, int *nstate) |
|
56 { |
|
57 int nn = *n; |
|
58 Vector tmp_x (nn); |
|
59 |
|
60 for (int i = 0; i < nn; i++) |
|
61 tmp_x.elem (i) = xx[i]; |
|
62 |
|
63 if (*mode == 0 || *mode == 2) |
|
64 { |
|
65 double value = (*user_phi) (tmp_x); |
|
66 #if defined (sun) && defined (__GNUC__) |
|
67 assign_double (objf, value); |
|
68 #else |
|
69 *objf = value; |
|
70 #endif |
|
71 } |
|
72 |
|
73 if ((*mode == 1 || *mode == 2) && user_grad != NULL) |
|
74 { |
|
75 Vector tmp_grad (nn); |
|
76 |
|
77 tmp_grad = (*user_grad) (tmp_x); |
|
78 |
|
79 for (i = 0; i < nn; i++) |
|
80 objgrd[i] = tmp_grad.elem (i); |
|
81 } |
|
82 |
|
83 return 0; |
|
84 } |
|
85 |
|
86 int |
|
87 npsol_confun (int *mode, int *ncnln, int *n, int *nrowj, int *needc, |
|
88 double *xx, double *cons, double *cjac, int *nstate) |
|
89 { |
|
90 int nn = *n, nncnln = *ncnln; |
|
91 Vector tmp_x (nn); |
|
92 Vector tmp_c (nncnln); |
|
93 |
|
94 for (int i = 0; i < nn; i++) |
|
95 tmp_x.elem (i) = xx[i]; |
|
96 |
|
97 tmp_c = (*user_g) (tmp_x); |
|
98 |
|
99 for (i = 0; i < nncnln; i++) |
|
100 cons[i] = tmp_c.elem (i); |
|
101 |
|
102 if (user_jac != NULL) |
|
103 { |
|
104 Matrix tmp_jac (nncnln, nn); |
|
105 |
|
106 tmp_jac = (*user_jac) (tmp_x); |
|
107 |
|
108 int ld = *nrowj; |
|
109 for (int j = 0; j < nn; j++) |
|
110 for (i = 0; i < nncnln; i++) |
|
111 cjac[i+j*ld] = tmp_jac (i, j); |
|
112 } |
|
113 |
|
114 return 0; |
|
115 } |
|
116 |
|
117 Vector |
|
118 NPSOL::minimize (void) |
|
119 { |
|
120 double objf; |
|
121 int inform; |
|
122 Vector lambda; |
|
123 return minimize (objf, inform, lambda); |
|
124 } |
|
125 |
|
126 Vector |
|
127 NPSOL::minimize (double& objf) |
|
128 { |
|
129 int inform; |
|
130 Vector lambda; |
|
131 return minimize (objf, inform, lambda); |
|
132 } |
|
133 |
|
134 Vector |
|
135 NPSOL::minimize (double& objf, int& inform) |
|
136 { |
|
137 Vector lambda; |
|
138 return minimize (objf, inform, lambda); |
|
139 } |
|
140 |
|
141 Vector |
|
142 NPSOL::minimize (double& objf, int& inform, Vector& lambda) |
|
143 { |
|
144 // Dimensions of various things. |
|
145 |
|
146 int n = x.capacity (); |
|
147 int nclin = lc.size (); |
|
148 int ncnln = nlc.size (); |
|
149 int nrowa = 1 > nclin ? 1 : nclin; |
|
150 int nrowj = 1 > ncnln ? 1 : ncnln; |
|
151 int nrowr = n; |
|
152 |
|
153 // Informative stuff. |
|
154 |
|
155 int iter; |
|
156 int *istate = new int [n+nclin+ncnln]; |
|
157 |
|
158 // User defined function stuff is defined above in the functions |
|
159 // npsol_confun() and npsol_objfun(); |
|
160 |
|
161 // Constraint stuff. |
|
162 |
|
163 double dummy; |
|
164 double *pclin = &dummy; |
|
165 Matrix clin; |
|
166 if (nclin > 0) |
|
167 { |
|
168 clin = lc.constraint_matrix (); |
|
169 pclin = clin.fortran_vec (); |
|
170 } |
|
171 |
|
172 double *clow = new double [n+nclin+ncnln]; |
|
173 double *cup = new double [n+nclin+ncnln]; |
|
174 |
|
175 if (bnds.size () > 0) |
|
176 { |
|
177 for (int i = 0; i < n; i++) |
|
178 { |
|
179 clow[i] = bnds.lower_bound (i); |
|
180 cup[i] = bnds.upper_bound (i); |
|
181 } |
|
182 } |
|
183 else |
|
184 { |
|
185 double huge = 1.0e30; |
|
186 for (int i = 0; i < n; i++) |
|
187 { |
|
188 clow[i] = -huge; |
|
189 cup[i] = huge; |
|
190 } |
|
191 } |
|
192 |
|
193 for (int i = 0; i < nclin; i++) |
|
194 { |
|
195 clow[i+n] = lc.lower_bound (i); |
|
196 cup[i+n] = lc.upper_bound (i); |
|
197 } |
|
198 |
|
199 for (i = 0; i < ncnln; i++) |
|
200 { |
|
201 clow[i+n+nclin] = nlc.lower_bound (i); |
|
202 cup[i+n+nclin] = nlc.upper_bound (i); |
|
203 } |
|
204 |
|
205 double *c = &dummy; |
|
206 double *cjac = &dummy; |
|
207 if (ncnln > 0) |
|
208 { |
|
209 c = new double [ncnln]; |
|
210 cjac = new double [nrowj*n]; |
|
211 } |
|
212 |
|
213 // Objective stuff. |
|
214 |
|
215 double *objgrd = new double [n]; |
|
216 |
|
217 // Other stuff. |
|
218 |
|
219 double *r = new double [n*n]; |
|
220 |
|
221 lambda.resize (n+nclin+ncnln); |
|
222 double *pclambda = lambda.fortran_vec (); |
|
223 |
|
224 // Decision variable stuff. |
|
225 |
|
226 double *px = x.fortran_vec (); |
|
227 |
|
228 // Workspace parameters. |
|
229 |
|
230 int lenw; |
|
231 int leniw = 3 * n + nclin + 2 * ncnln; |
|
232 if (nclin == 0 && ncnln == 0) |
|
233 lenw = 20*n; |
|
234 else if (ncnln == 0) |
|
235 lenw = 2*n*(10 + n) + 11*nclin; |
|
236 else |
|
237 lenw = 2*n*(n + 10) + nclin*(n + 11) + ncnln*(2*n + 21); |
|
238 |
|
239 int *iw = new int [leniw]; |
|
240 double *w = new double [lenw]; |
|
241 |
|
242 user_phi = phi.objective_function (); |
|
243 user_grad = phi.gradient_function (); |
|
244 user_g = nlc.function (); |
|
245 user_jac = nlc.jacobian_function (); |
|
246 |
|
247 // Solve the damn thing. |
|
248 |
|
249 if (user_jac == NULL && user_grad == NULL) |
|
250 F77_FCN (npoptn) ("Derivative Level = 0", 20L); |
|
251 else if (user_jac == NULL && user_grad != NULL) |
|
252 F77_FCN (npoptn) ("Derivative Level = 1", 20L); |
|
253 else if (user_jac != NULL && user_grad == NULL) |
|
254 F77_FCN (npoptn) ("Derivative Level = 2", 20L); |
|
255 else if (user_jac != NULL && user_grad != NULL) |
|
256 F77_FCN (npoptn) ("Derivative Level = 3", 20L); |
|
257 |
130
|
258 int attempt = 0; |
|
259 while (attempt++ < 5) |
3
|
260 { |
|
261 |
|
262 F77_FCN (npsol) (&n, &nclin, &ncnln, &nrowa, &nrowj, &nrowr, pclin, |
|
263 clow, cup, npsol_confun, npsol_objfun, &inform, |
|
264 &iter, istate, c, cjac, pclambda, &objf, objgrd, r, |
|
265 px, iw, &leniw, w, &lenw); |
|
266 |
|
267 if (inform == 6 || inform == 1) |
|
268 continue; |
|
269 else |
|
270 break; |
|
271 } |
|
272 |
|
273 // See how it went. |
|
274 |
|
275 return x; |
|
276 } |
|
277 |
|
278 Vector |
|
279 NPSOL::minimize (const Vector& xnew) |
|
280 { |
|
281 x = xnew; |
|
282 return minimize (); |
|
283 } |
|
284 |
|
285 Vector |
|
286 NPSOL::minimize (const Vector& xnew, double& objf) |
|
287 { |
|
288 x = xnew; |
|
289 return minimize (objf); |
|
290 } |
|
291 |
|
292 Vector |
|
293 NPSOL::minimize (const Vector& xnew, double& objf, int& inform) |
|
294 { |
|
295 x = xnew; |
|
296 return minimize (objf, inform); |
|
297 } |
|
298 |
|
299 Vector |
|
300 NPSOL::minimize (const Vector& xnew, double& objf, int& inform, Vector& lambda) |
|
301 { |
|
302 x = xnew; |
|
303 return minimize (objf, inform, lambda); |
|
304 } |
|
305 |
|
306 NPSOL& |
|
307 NPSOL::option (char *s) |
|
308 { |
|
309 long len = strlen (s); |
|
310 F77_FCN (npoptn) (s, len); |
|
311 return *this; |
|
312 } |
|
313 |
|
314 void |
|
315 NPSOL::set_default_options (void) |
|
316 { |
|
317 F77_FCN (npoptn) ("Nolist", 6L); |
|
318 F77_FCN (npoptn) ("Defaults", 8L); |
|
319 F77_FCN (npoptn) ("Print Level 0", 13L); |
|
320 } |
|
321 |
|
322 #endif /* NPSOL_MISSING */ |
|
323 |
|
324 /* |
|
325 ;;; Local Variables: *** |
|
326 ;;; mode: C++ *** |
|
327 ;;; page-delimiter: "^/\\*" *** |
|
328 ;;; End: *** |
|
329 */ |