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 * |
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 |
|
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; |
158
|
88 error ("npsol: error evaluating objective function"); |
1
|
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 |
162
|
119 npsol_constraint_function (const ColumnVector& x) |
1
|
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; |
158
|
156 error ("npsol: error evaluating constraints"); |
1
|
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; |
158
|
181 error ("%s: linear constraints must have nonzero dimensions", |
|
182 warn_for); |
132
|
183 } |
|
184 else if (x_len != c_cols || llb_len != lub_len || llb_len != c_rows) |
|
185 { |
|
186 ok = 0; |
158
|
187 error ("%s: linear constraints have inconsistent dimensions", |
|
188 warn_for); |
132
|
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 (); |
|
204 |
132
|
205 int ok = 1; |
|
206 if (warn) |
|
207 { |
|
208 if (nllb_len == 0 || nlub_len == 0 || c_len == 0) |
|
209 { |
|
210 ok = 0; |
158
|
211 error ("%s: nonlinear constraints have nonzero dimensions", |
|
212 warn_for); |
132
|
213 } |
|
214 else if (nllb_len != nlub_len || nllb_len != c_len) |
|
215 { |
|
216 ok = 0; |
162
|
217 error ("%s: nonlinear constraints have inconsistent dimensions", |
|
218 warn_for); |
132
|
219 } |
135
|
220 } |
1
|
221 return ok; |
|
222 } |
|
223 |
|
224 tree_constant * |
162
|
225 npsol (const tree_constant *args, int nargin, int nargout) |
1
|
226 { |
|
227 /* |
|
228 |
|
229 Handle all of the following: |
|
230 |
|
231 1. npsol (x, phi) |
|
232 2. npsol (x, phi, lb, ub) |
|
233 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
234 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
235 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
236 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
237 7. npsol (x, phi, llb, c, lub) |
|
238 8. npsol (x, phi, nllb, g, nlub) |
|
239 |
|
240 */ |
|
241 |
|
242 // Assumes that we have been given the correct number of arguments. |
|
243 |
|
244 tree_constant *retval = NULL_TREE_CONST; |
|
245 |
|
246 ColumnVector x = args[1].to_vector (); |
|
247 |
|
248 if (x.capacity () == 0) |
|
249 { |
158
|
250 error ("npsol: expecting vector as first argument"); |
1
|
251 return retval; |
|
252 } |
|
253 |
|
254 npsol_objective = is_valid_function (args[2], "npsol", 1); |
|
255 if (npsol_objective == NULL_TREE |
|
256 || takes_correct_nargs (npsol_objective, 2, "npsol", 1) != 1) |
|
257 return retval; |
|
258 |
|
259 Objective func (npsol_objective_function); |
|
260 |
|
261 ColumnVector soln; |
|
262 |
|
263 Bounds bounds; |
|
264 if (nargin == 5 || nargin == 8 || nargin == 11) |
|
265 { |
|
266 ColumnVector lb = args[3].to_vector (); |
|
267 ColumnVector ub = args[4].to_vector (); |
|
268 |
|
269 int lb_len = lb.capacity (); |
|
270 int ub_len = ub.capacity (); |
|
271 if (lb_len != ub_len || lb_len != x.capacity ()) |
|
272 { |
214
|
273 error ("npsol: lower and upper bounds and decision variable vector"); |
|
274 error ("must all have the same number of elements"); |
1
|
275 return retval; |
|
276 } |
|
277 |
|
278 bounds.resize (lb_len); |
|
279 bounds.set_lower_bounds (lb); |
|
280 bounds.set_upper_bounds (ub); |
|
281 } |
|
282 |
|
283 double objf; |
|
284 ColumnVector lambda; |
|
285 int inform; |
|
286 |
|
287 if (nargin == 3) |
|
288 { |
|
289 // 1. npsol (x, phi) |
|
290 |
|
291 NPSOL nlp (x, func); |
|
292 soln = nlp.minimize (objf, inform, lambda); |
|
293 |
|
294 goto solved; |
|
295 } |
|
296 |
|
297 if (nargin == 5) |
|
298 { |
|
299 // 2. npsol (x, phi, lb, ub) |
|
300 |
|
301 NPSOL nlp (x, func, bounds); |
|
302 soln = nlp.minimize (objf, inform, lambda); |
|
303 |
|
304 goto solved; |
|
305 } |
|
306 |
|
307 npsol_constraints = NULL_TREE; |
|
308 if (nargin == 6 || nargin == 8 || nargin == 9 || nargin == 11) |
|
309 npsol_constraints = is_valid_function (args[nargin-2], "npsol", 0); |
|
310 |
|
311 if (nargin == 8 || nargin == 6) |
|
312 { |
|
313 if (npsol_constraints == NULL_TREE) |
|
314 { |
|
315 ColumnVector lub = args[nargin-1].to_vector (); |
|
316 Matrix c = args[nargin-2].to_matrix (); |
|
317 ColumnVector llb = args[nargin-3].to_vector (); |
|
318 |
|
319 LinConst linear_constraints (llb, c, lub); |
|
320 |
|
321 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
322 return retval; |
|
323 |
|
324 if (nargin == 6) |
|
325 { |
|
326 // 7. npsol (x, phi, llb, c, lub) |
|
327 |
|
328 NPSOL nlp (x, func, linear_constraints); |
|
329 soln = nlp.minimize (objf, inform, lambda); |
|
330 } |
|
331 else |
|
332 { |
|
333 // 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
334 |
|
335 NPSOL nlp (x, func, bounds, linear_constraints); |
|
336 soln = nlp.minimize (objf, inform, lambda); |
|
337 } |
|
338 goto solved; |
|
339 } |
|
340 else |
|
341 { |
|
342 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
343 { |
|
344 ColumnVector nlub = args[nargin-1].to_vector (); |
|
345 ColumnVector nllb = args[nargin-3].to_vector (); |
|
346 |
|
347 NLFunc const_func (npsol_constraint_function); |
|
348 |
|
349 if (! nonlinear_constraints_ok |
|
350 (x, nllb, npsol_constraint_function, nlub, "npsol", 1)) |
|
351 return retval; |
|
352 |
|
353 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
354 |
|
355 if (nargin == 6) |
|
356 { |
|
357 // 8. npsol (x, phi, nllb, g, nlub) |
|
358 |
|
359 NPSOL nlp (x, func, nonlinear_constraints); |
|
360 soln = nlp.minimize (objf, inform, lambda); |
|
361 } |
|
362 else |
|
363 { |
|
364 // 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
365 |
|
366 NPSOL nlp (x, func, bounds, nonlinear_constraints); |
|
367 soln = nlp.minimize (objf, inform, lambda); |
|
368 } |
|
369 goto solved; |
|
370 } |
|
371 } |
|
372 } |
|
373 |
|
374 if (nargin == 9 || nargin == 11) |
|
375 { |
|
376 if (npsol_constraints == NULL_TREE) |
|
377 { |
|
378 // Produce error message. |
|
379 is_valid_function (args[nargin-2], "npsol", 1); |
|
380 } |
|
381 else |
|
382 { |
|
383 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
384 { |
|
385 ColumnVector nlub = args[nargin-1].to_vector (); |
|
386 ColumnVector nllb = args[nargin-3].to_vector (); |
|
387 |
|
388 NLFunc const_func (npsol_constraint_function); |
|
389 |
|
390 if (! nonlinear_constraints_ok |
|
391 (x, nllb, npsol_constraint_function, nlub, "npsol", 1)) |
|
392 return retval; |
|
393 |
|
394 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
395 |
|
396 ColumnVector lub = args[nargin-4].to_vector (); |
|
397 Matrix c = args[nargin-5].to_matrix (); |
|
398 ColumnVector llb = args[nargin-6].to_vector (); |
|
399 |
|
400 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
401 return retval; |
|
402 |
|
403 LinConst linear_constraints (llb, c, lub); |
|
404 |
|
405 if (nargin == 9) |
|
406 { |
|
407 // 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
408 |
|
409 NPSOL nlp (x, func, linear_constraints, |
|
410 nonlinear_constraints); |
|
411 |
|
412 soln = nlp.minimize (objf, inform, lambda); |
|
413 } |
|
414 else |
|
415 { |
|
416 // 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
417 |
|
418 NPSOL nlp (x, func, bounds, linear_constraints, |
|
419 nonlinear_constraints); |
|
420 |
|
421 soln = nlp.minimize (objf, inform, lambda); |
|
422 } |
|
423 goto solved; |
|
424 } |
|
425 } |
|
426 } |
|
427 |
|
428 return retval; |
|
429 |
|
430 solved: |
|
431 |
|
432 retval = new tree_constant [nargout+1]; |
|
433 retval[0] = tree_constant (soln, 1); |
|
434 if (nargout > 1) |
|
435 retval[1] = tree_constant (objf); |
|
436 if (nargout > 2) |
|
437 retval[2] = tree_constant ((double) inform); |
|
438 if (nargout > 3) |
|
439 retval[3] = tree_constant (lambda); |
|
440 |
|
441 return retval; |
|
442 } |
|
443 |
|
444 #endif |
|
445 |
|
446 /* |
|
447 ;;; Local Variables: *** |
|
448 ;;; mode: C++ *** |
|
449 ;;; page-delimiter: "^/\\*" *** |
|
450 ;;; End: *** |
|
451 */ |