7
|
1 // f-npsol.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
|
4 Copyright (C) 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 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #ifndef NPSOL_MISSING |
|
29 |
|
30 #include "NPSOL.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
|
36 #include "utils.h" |
7
|
37 #include "f-npsol.h" |
1
|
38 |
|
39 // Global pointers for user defined functions required by npsol. |
|
40 static tree *npsol_objective; |
|
41 static tree *npsol_constraints; |
|
42 |
|
43 #ifdef WITH_DLD |
|
44 tree_constant * |
162
|
45 builtin_npsol_2 (const tree_constant *args, int nargin, int nargout) |
1
|
46 { |
|
47 return npsol (args, nargin, nargout); |
|
48 } |
|
49 #endif |
|
50 |
|
51 double |
162
|
52 npsol_objective_function (const ColumnVector& x) |
1
|
53 { |
|
54 int n = x.capacity (); |
|
55 |
|
56 tree_constant decision_vars; |
|
57 if (n > 1) |
|
58 { |
|
59 Matrix m (n, 1); |
|
60 for (int i = 0; i < n; i++) |
|
61 m (i, 0) = x.elem (i); |
|
62 decision_vars = tree_constant (m); |
|
63 } |
|
64 else |
|
65 { |
|
66 double d = x.elem (0); |
|
67 decision_vars = tree_constant (d); |
|
68 } |
|
69 |
|
70 // tree_constant name = tree_constant (npsol_objective->name ()); |
|
71 tree_constant *args = new tree_constant [2]; |
|
72 // args[0] = name; |
|
73 args[1] = decision_vars; |
|
74 |
255
|
75 static double retval; |
|
76 retval = 0.0; |
|
77 |
1
|
78 tree_constant objective_value; |
|
79 if (npsol_objective != NULL_TREE) |
|
80 { |
|
81 tree_constant *tmp = npsol_objective->eval (args, 2, 1, 0); |
255
|
82 |
1
|
83 delete [] args; |
255
|
84 |
|
85 if (error_state) |
|
86 { |
|
87 error ("npsol: error evaluating objective function"); |
|
88 npsol_objective_error = 1; // XXX FIXME XXX |
260
|
89 delete [] tmp; |
255
|
90 return retval; |
|
91 } |
|
92 |
1
|
93 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
94 { |
|
95 objective_value = tmp[0]; |
|
96 delete [] tmp; |
|
97 } |
|
98 else |
|
99 { |
|
100 delete [] tmp; |
158
|
101 error ("npsol: error evaluating objective function"); |
255
|
102 npsol_objective_error = 1; // XXX FIXME XXX |
|
103 return retval; |
1
|
104 } |
|
105 } |
|
106 |
|
107 switch (objective_value.const_type ()) |
|
108 { |
|
109 case tree_constant_rep::matrix_constant: |
|
110 { |
|
111 Matrix m = objective_value.matrix_value (); |
|
112 if (m.rows () == 1 && m.columns () == 1) |
|
113 retval = m.elem (0, 0); |
|
114 else |
255
|
115 { |
|
116 gripe_user_returned_invalid ("npsol_objective"); |
|
117 npsol_objective_error = 1; // XXX FIXME XXX |
|
118 } |
1
|
119 } |
|
120 break; |
|
121 case tree_constant_rep::scalar_constant: |
|
122 retval = objective_value.double_value (); |
|
123 break; |
|
124 default: |
|
125 gripe_user_returned_invalid ("npsol_objective"); |
255
|
126 npsol_objective_error = 1; // XXX FIXME XXX |
1
|
127 break; |
|
128 } |
|
129 |
|
130 return retval; |
|
131 } |
|
132 |
|
133 ColumnVector |
162
|
134 npsol_constraint_function (const ColumnVector& x) |
1
|
135 { |
|
136 ColumnVector retval; |
|
137 |
|
138 int n = x.capacity (); |
|
139 |
|
140 tree_constant decision_vars; |
|
141 if (n > 1) |
|
142 { |
|
143 Matrix m (n, 1); |
|
144 for (int i = 0; i < n; i++) |
|
145 m (i, 0) = x.elem (i); |
|
146 decision_vars = tree_constant (m); |
|
147 } |
|
148 else |
|
149 { |
|
150 double d = x.elem (0); |
|
151 decision_vars = tree_constant (d); |
|
152 } |
|
153 |
|
154 // tree_constant name = tree_constant (npsol_constraints->name ()); |
|
155 tree_constant *args = new tree_constant [2]; |
|
156 // args[0] = name; |
|
157 args[1] = decision_vars; |
|
158 |
|
159 if (npsol_constraints != NULL_TREE) |
|
160 { |
|
161 tree_constant *tmp = npsol_constraints->eval (args, 2, 1, 0); |
255
|
162 |
1
|
163 delete [] args; |
255
|
164 |
|
165 if (error_state) |
|
166 { |
260
|
167 delete [] tmp; |
255
|
168 error ("npsol: error evaluating constraints"); |
|
169 return retval; |
|
170 } |
|
171 |
1
|
172 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
173 { |
|
174 retval = tmp[0].to_vector (); |
255
|
175 |
1
|
176 delete [] tmp; |
255
|
177 |
|
178 if (retval.length () <= 0) |
|
179 error ("npsol: error evaluating constraints"); |
1
|
180 } |
|
181 else |
|
182 { |
|
183 delete [] tmp; |
158
|
184 error ("npsol: error evaluating constraints"); |
1
|
185 } |
|
186 } |
|
187 |
|
188 return retval; |
|
189 } |
|
190 |
|
191 int |
|
192 linear_constraints_ok (const ColumnVector& x, const ColumnVector& llb, |
|
193 const Matrix& c, const ColumnVector& lub, |
|
194 char *warn_for, int warn) |
|
195 { |
|
196 int x_len = x.capacity (); |
|
197 int llb_len = llb.capacity (); |
|
198 int lub_len = lub.capacity (); |
|
199 int c_rows = c.rows (); |
|
200 int c_cols = c.columns (); |
|
201 |
132
|
202 int ok = 1; |
|
203 if (warn) |
|
204 { |
|
205 if (c_rows == 0 || c_cols == 0 || llb_len == 0 || lub_len == 0) |
|
206 { |
|
207 ok = 0; |
158
|
208 error ("%s: linear constraints must have nonzero dimensions", |
|
209 warn_for); |
132
|
210 } |
|
211 else if (x_len != c_cols || llb_len != lub_len || llb_len != c_rows) |
|
212 { |
|
213 ok = 0; |
158
|
214 error ("%s: linear constraints have inconsistent dimensions", |
|
215 warn_for); |
132
|
216 } |
|
217 } |
1
|
218 |
|
219 return ok; |
|
220 } |
|
221 |
|
222 int |
|
223 nonlinear_constraints_ok (const ColumnVector& x, const ColumnVector& nllb, |
|
224 nonlinear_fcn g, const ColumnVector& nlub, |
|
225 char *warn_for, int warn) |
|
226 { |
|
227 int nllb_len = nllb.capacity (); |
|
228 int nlub_len = nlub.capacity (); |
|
229 ColumnVector c = (*g) (x); |
|
230 int c_len = c.capacity (); |
|
231 |
132
|
232 int ok = 1; |
|
233 if (warn) |
|
234 { |
|
235 if (nllb_len == 0 || nlub_len == 0 || c_len == 0) |
|
236 { |
|
237 ok = 0; |
158
|
238 error ("%s: nonlinear constraints have nonzero dimensions", |
|
239 warn_for); |
132
|
240 } |
|
241 else if (nllb_len != nlub_len || nllb_len != c_len) |
|
242 { |
|
243 ok = 0; |
162
|
244 error ("%s: nonlinear constraints have inconsistent dimensions", |
|
245 warn_for); |
132
|
246 } |
135
|
247 } |
1
|
248 return ok; |
|
249 } |
|
250 |
|
251 tree_constant * |
162
|
252 npsol (const tree_constant *args, int nargin, int nargout) |
1
|
253 { |
|
254 /* |
|
255 |
|
256 Handle all of the following: |
|
257 |
|
258 1. npsol (x, phi) |
|
259 2. npsol (x, phi, lb, ub) |
|
260 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
261 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
262 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
263 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
264 7. npsol (x, phi, llb, c, lub) |
|
265 8. npsol (x, phi, nllb, g, nlub) |
|
266 |
|
267 */ |
|
268 |
|
269 // Assumes that we have been given the correct number of arguments. |
|
270 |
|
271 tree_constant *retval = NULL_TREE_CONST; |
|
272 |
|
273 ColumnVector x = args[1].to_vector (); |
|
274 |
|
275 if (x.capacity () == 0) |
|
276 { |
158
|
277 error ("npsol: expecting vector as first argument"); |
1
|
278 return retval; |
|
279 } |
|
280 |
|
281 npsol_objective = is_valid_function (args[2], "npsol", 1); |
|
282 if (npsol_objective == NULL_TREE |
|
283 || takes_correct_nargs (npsol_objective, 2, "npsol", 1) != 1) |
|
284 return retval; |
|
285 |
|
286 Objective func (npsol_objective_function); |
|
287 |
|
288 ColumnVector soln; |
|
289 |
|
290 Bounds bounds; |
|
291 if (nargin == 5 || nargin == 8 || nargin == 11) |
|
292 { |
|
293 ColumnVector lb = args[3].to_vector (); |
|
294 ColumnVector ub = args[4].to_vector (); |
|
295 |
|
296 int lb_len = lb.capacity (); |
|
297 int ub_len = ub.capacity (); |
|
298 if (lb_len != ub_len || lb_len != x.capacity ()) |
|
299 { |
214
|
300 error ("npsol: lower and upper bounds and decision variable vector"); |
|
301 error ("must all have the same number of elements"); |
1
|
302 return retval; |
|
303 } |
|
304 |
|
305 bounds.resize (lb_len); |
|
306 bounds.set_lower_bounds (lb); |
|
307 bounds.set_upper_bounds (ub); |
|
308 } |
|
309 |
|
310 double objf; |
|
311 ColumnVector lambda; |
|
312 int inform; |
|
313 |
|
314 if (nargin == 3) |
|
315 { |
|
316 // 1. npsol (x, phi) |
|
317 |
|
318 NPSOL nlp (x, func); |
|
319 soln = nlp.minimize (objf, inform, lambda); |
|
320 |
|
321 goto solved; |
|
322 } |
|
323 |
|
324 if (nargin == 5) |
|
325 { |
|
326 // 2. npsol (x, phi, lb, ub) |
|
327 |
|
328 NPSOL nlp (x, func, bounds); |
|
329 soln = nlp.minimize (objf, inform, lambda); |
|
330 |
|
331 goto solved; |
|
332 } |
|
333 |
|
334 npsol_constraints = NULL_TREE; |
|
335 if (nargin == 6 || nargin == 8 || nargin == 9 || nargin == 11) |
|
336 npsol_constraints = is_valid_function (args[nargin-2], "npsol", 0); |
|
337 |
|
338 if (nargin == 8 || nargin == 6) |
|
339 { |
|
340 if (npsol_constraints == NULL_TREE) |
|
341 { |
|
342 ColumnVector lub = args[nargin-1].to_vector (); |
|
343 Matrix c = args[nargin-2].to_matrix (); |
|
344 ColumnVector llb = args[nargin-3].to_vector (); |
|
345 |
215
|
346 if (llb.capacity () == 0 || lub.capacity () == 0) |
|
347 { |
|
348 error ("npsol: bounds for linear constraints must be vectors"); |
|
349 return retval; |
|
350 } |
1
|
351 |
|
352 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
353 return retval; |
|
354 |
215
|
355 LinConst linear_constraints (llb, c, lub); |
|
356 |
1
|
357 if (nargin == 6) |
|
358 { |
|
359 // 7. npsol (x, phi, llb, c, lub) |
|
360 |
|
361 NPSOL nlp (x, func, linear_constraints); |
|
362 soln = nlp.minimize (objf, inform, lambda); |
|
363 } |
|
364 else |
|
365 { |
|
366 // 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
367 |
|
368 NPSOL nlp (x, func, bounds, linear_constraints); |
|
369 soln = nlp.minimize (objf, inform, lambda); |
|
370 } |
|
371 goto solved; |
|
372 } |
|
373 else |
|
374 { |
|
375 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
376 { |
|
377 ColumnVector nlub = args[nargin-1].to_vector (); |
|
378 ColumnVector nllb = args[nargin-3].to_vector (); |
|
379 |
|
380 NLFunc const_func (npsol_constraint_function); |
|
381 |
|
382 if (! nonlinear_constraints_ok |
|
383 (x, nllb, npsol_constraint_function, nlub, "npsol", 1)) |
|
384 return retval; |
|
385 |
|
386 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
387 |
|
388 if (nargin == 6) |
|
389 { |
|
390 // 8. npsol (x, phi, nllb, g, nlub) |
|
391 |
|
392 NPSOL nlp (x, func, nonlinear_constraints); |
|
393 soln = nlp.minimize (objf, inform, lambda); |
|
394 } |
|
395 else |
|
396 { |
|
397 // 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
398 |
|
399 NPSOL nlp (x, func, bounds, nonlinear_constraints); |
|
400 soln = nlp.minimize (objf, inform, lambda); |
|
401 } |
|
402 goto solved; |
|
403 } |
|
404 } |
|
405 } |
|
406 |
|
407 if (nargin == 9 || nargin == 11) |
|
408 { |
|
409 if (npsol_constraints == NULL_TREE) |
|
410 { |
|
411 // Produce error message. |
|
412 is_valid_function (args[nargin-2], "npsol", 1); |
|
413 } |
|
414 else |
|
415 { |
|
416 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
417 { |
|
418 ColumnVector nlub = args[nargin-1].to_vector (); |
|
419 ColumnVector nllb = args[nargin-3].to_vector (); |
|
420 |
|
421 NLFunc const_func (npsol_constraint_function); |
|
422 |
|
423 if (! nonlinear_constraints_ok |
|
424 (x, nllb, npsol_constraint_function, nlub, "npsol", 1)) |
|
425 return retval; |
|
426 |
|
427 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
428 |
|
429 ColumnVector lub = args[nargin-4].to_vector (); |
|
430 Matrix c = args[nargin-5].to_matrix (); |
|
431 ColumnVector llb = args[nargin-6].to_vector (); |
|
432 |
215
|
433 if (llb.capacity () == 0 || lub.capacity () == 0) |
|
434 { |
|
435 error ("npsol: bounds for linear constraints must be vectors"); |
|
436 return retval; |
|
437 } |
|
438 |
1
|
439 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
440 return retval; |
|
441 |
|
442 LinConst linear_constraints (llb, c, lub); |
|
443 |
|
444 if (nargin == 9) |
|
445 { |
|
446 // 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
447 |
|
448 NPSOL nlp (x, func, linear_constraints, |
|
449 nonlinear_constraints); |
|
450 |
|
451 soln = nlp.minimize (objf, inform, lambda); |
|
452 } |
|
453 else |
|
454 { |
|
455 // 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
456 |
|
457 NPSOL nlp (x, func, bounds, linear_constraints, |
|
458 nonlinear_constraints); |
|
459 |
|
460 soln = nlp.minimize (objf, inform, lambda); |
|
461 } |
|
462 goto solved; |
|
463 } |
|
464 } |
|
465 } |
|
466 |
|
467 return retval; |
|
468 |
|
469 solved: |
|
470 |
|
471 retval = new tree_constant [nargout+1]; |
|
472 retval[0] = tree_constant (soln, 1); |
|
473 if (nargout > 1) |
|
474 retval[1] = tree_constant (objf); |
|
475 if (nargout > 2) |
|
476 retval[2] = tree_constant ((double) inform); |
|
477 if (nargout > 3) |
|
478 retval[3] = tree_constant (lambda); |
|
479 |
|
480 return retval; |
|
481 } |
|
482 |
|
483 #endif |
|
484 |
|
485 /* |
|
486 ;;; Local Variables: *** |
|
487 ;;; mode: C++ *** |
|
488 ;;; page-delimiter: "^/\\*" *** |
|
489 ;;; End: *** |
|
490 */ |