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