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