7
|
1 // f-npsol.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
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 |
287
|
30 #include <strstream.h> |
|
31 |
1
|
32 #include "NPSOL.h" |
|
33 |
|
34 #include "tree-const.h" |
|
35 #include "variables.h" |
287
|
36 #include "builtins.h" |
1
|
37 #include "gripes.h" |
|
38 #include "error.h" |
287
|
39 #include "pager.h" |
1
|
40 #include "utils.h" |
7
|
41 #include "f-npsol.h" |
1
|
42 |
|
43 // Global pointers for user defined functions required by npsol. |
488
|
44 static tree_fvc *npsol_objective; |
|
45 static tree_fvc *npsol_constraints; |
1
|
46 |
|
47 #ifdef WITH_DLD |
497
|
48 Octave_object |
506
|
49 builtin_npsol_2 (const Octave_object& args, int nargout) |
1
|
50 { |
506
|
51 return npsol (args, nargout); |
1
|
52 } |
272
|
53 |
497
|
54 Octave_object |
506
|
55 builtin_npsol_options_2 (const Octave_object& args, int nargout) |
272
|
56 { |
506
|
57 return npsol_options (args, nargout); |
272
|
58 } |
1
|
59 #endif |
|
60 |
287
|
61 static NPSOL_options npsol_opts; |
|
62 |
1
|
63 double |
162
|
64 npsol_objective_function (const ColumnVector& x) |
1
|
65 { |
|
66 int n = x.capacity (); |
|
67 |
|
68 tree_constant decision_vars; |
|
69 if (n > 1) |
|
70 { |
|
71 Matrix m (n, 1); |
|
72 for (int i = 0; i < n; i++) |
|
73 m (i, 0) = x.elem (i); |
|
74 decision_vars = tree_constant (m); |
|
75 } |
|
76 else |
|
77 { |
|
78 double d = x.elem (0); |
|
79 decision_vars = tree_constant (d); |
|
80 } |
|
81 |
|
82 // tree_constant name = tree_constant (npsol_objective->name ()); |
497
|
83 Octave_object args (2); |
|
84 // args(0) = name; |
|
85 args(1) = decision_vars; |
1
|
86 |
255
|
87 static double retval; |
|
88 retval = 0.0; |
|
89 |
1
|
90 tree_constant objective_value; |
488
|
91 if (npsol_objective != (tree_fvc *) NULL) |
1
|
92 { |
506
|
93 Octave_object tmp = npsol_objective->eval (0, 1, args); |
255
|
94 |
|
95 if (error_state) |
|
96 { |
|
97 error ("npsol: error evaluating objective function"); |
|
98 npsol_objective_error = 1; // XXX FIXME XXX |
|
99 return retval; |
|
100 } |
|
101 |
497
|
102 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
103 objective_value = tmp(0); |
1
|
104 else |
|
105 { |
158
|
106 error ("npsol: error evaluating objective function"); |
255
|
107 npsol_objective_error = 1; // XXX FIXME XXX |
|
108 return retval; |
1
|
109 } |
|
110 } |
|
111 |
|
112 switch (objective_value.const_type ()) |
|
113 { |
|
114 case tree_constant_rep::matrix_constant: |
|
115 { |
|
116 Matrix m = objective_value.matrix_value (); |
|
117 if (m.rows () == 1 && m.columns () == 1) |
|
118 retval = m.elem (0, 0); |
|
119 else |
255
|
120 { |
|
121 gripe_user_returned_invalid ("npsol_objective"); |
|
122 npsol_objective_error = 1; // XXX FIXME XXX |
|
123 } |
1
|
124 } |
|
125 break; |
|
126 case tree_constant_rep::scalar_constant: |
|
127 retval = objective_value.double_value (); |
|
128 break; |
|
129 default: |
|
130 gripe_user_returned_invalid ("npsol_objective"); |
255
|
131 npsol_objective_error = 1; // XXX FIXME XXX |
1
|
132 break; |
|
133 } |
|
134 |
|
135 return retval; |
|
136 } |
|
137 |
|
138 ColumnVector |
162
|
139 npsol_constraint_function (const ColumnVector& x) |
1
|
140 { |
|
141 ColumnVector retval; |
|
142 |
|
143 int n = x.capacity (); |
|
144 |
|
145 tree_constant decision_vars; |
|
146 if (n > 1) |
|
147 { |
|
148 Matrix m (n, 1); |
|
149 for (int i = 0; i < n; i++) |
|
150 m (i, 0) = x.elem (i); |
|
151 decision_vars = tree_constant (m); |
|
152 } |
|
153 else |
|
154 { |
|
155 double d = x.elem (0); |
|
156 decision_vars = tree_constant (d); |
|
157 } |
|
158 |
|
159 // tree_constant name = tree_constant (npsol_constraints->name ()); |
497
|
160 Octave_object args (2); |
|
161 // args(0) = name; |
|
162 args(1) = decision_vars; |
1
|
163 |
488
|
164 if (npsol_constraints != (tree_fvc *)NULL) |
1
|
165 { |
506
|
166 Octave_object tmp = npsol_constraints->eval (0, 1, args); |
255
|
167 |
|
168 if (error_state) |
|
169 { |
|
170 error ("npsol: error evaluating constraints"); |
|
171 return retval; |
|
172 } |
|
173 |
497
|
174 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
175 { |
497
|
176 retval = tmp(0).to_vector (); |
255
|
177 |
|
178 if (retval.length () <= 0) |
|
179 error ("npsol: error evaluating constraints"); |
1
|
180 } |
|
181 else |
497
|
182 error ("npsol: error evaluating constraints"); |
1
|
183 } |
|
184 |
|
185 return retval; |
|
186 } |
|
187 |
|
188 int |
|
189 linear_constraints_ok (const ColumnVector& x, const ColumnVector& llb, |
|
190 const Matrix& c, const ColumnVector& lub, |
|
191 char *warn_for, int warn) |
|
192 { |
|
193 int x_len = x.capacity (); |
|
194 int llb_len = llb.capacity (); |
|
195 int lub_len = lub.capacity (); |
|
196 int c_rows = c.rows (); |
|
197 int c_cols = c.columns (); |
|
198 |
132
|
199 int ok = 1; |
|
200 if (warn) |
|
201 { |
|
202 if (c_rows == 0 || c_cols == 0 || llb_len == 0 || lub_len == 0) |
|
203 { |
|
204 ok = 0; |
158
|
205 error ("%s: linear constraints must have nonzero dimensions", |
|
206 warn_for); |
132
|
207 } |
|
208 else if (x_len != c_cols || llb_len != lub_len || llb_len != c_rows) |
|
209 { |
|
210 ok = 0; |
158
|
211 error ("%s: linear constraints have inconsistent dimensions", |
|
212 warn_for); |
132
|
213 } |
|
214 } |
1
|
215 |
|
216 return ok; |
|
217 } |
|
218 |
|
219 int |
|
220 nonlinear_constraints_ok (const ColumnVector& x, const ColumnVector& nllb, |
|
221 nonlinear_fcn g, const ColumnVector& nlub, |
|
222 char *warn_for, int warn) |
|
223 { |
|
224 int nllb_len = nllb.capacity (); |
|
225 int nlub_len = nlub.capacity (); |
|
226 ColumnVector c = (*g) (x); |
|
227 int c_len = c.capacity (); |
|
228 |
132
|
229 int ok = 1; |
|
230 if (warn) |
|
231 { |
|
232 if (nllb_len == 0 || nlub_len == 0 || c_len == 0) |
|
233 { |
|
234 ok = 0; |
158
|
235 error ("%s: nonlinear constraints have nonzero dimensions", |
|
236 warn_for); |
132
|
237 } |
|
238 else if (nllb_len != nlub_len || nllb_len != c_len) |
|
239 { |
|
240 ok = 0; |
162
|
241 error ("%s: nonlinear constraints have inconsistent dimensions", |
|
242 warn_for); |
132
|
243 } |
135
|
244 } |
1
|
245 return ok; |
|
246 } |
|
247 |
497
|
248 Octave_object |
506
|
249 npsol (const Octave_object& args, int nargout) |
1
|
250 { |
|
251 /* |
|
252 |
|
253 Handle all of the following: |
|
254 |
|
255 1. npsol (x, phi) |
|
256 2. npsol (x, phi, lb, ub) |
|
257 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
258 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
259 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
260 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
261 7. npsol (x, phi, llb, c, lub) |
|
262 8. npsol (x, phi, nllb, g, nlub) |
|
263 |
|
264 */ |
|
265 |
|
266 // Assumes that we have been given the correct number of arguments. |
|
267 |
497
|
268 Octave_object retval; |
1
|
269 |
506
|
270 int nargin = args.length (); |
|
271 |
497
|
272 ColumnVector x = args(1).to_vector (); |
1
|
273 |
|
274 if (x.capacity () == 0) |
|
275 { |
158
|
276 error ("npsol: expecting vector as first argument"); |
1
|
277 return retval; |
|
278 } |
|
279 |
497
|
280 npsol_objective = is_valid_function (args(2), "npsol", 1); |
488
|
281 if (npsol_objective == (tree_fvc *) NULL |
1
|
282 || takes_correct_nargs (npsol_objective, 2, "npsol", 1) != 1) |
|
283 return retval; |
|
284 |
|
285 Objective func (npsol_objective_function); |
|
286 |
|
287 ColumnVector soln; |
|
288 |
|
289 Bounds bounds; |
|
290 if (nargin == 5 || nargin == 8 || nargin == 11) |
|
291 { |
497
|
292 ColumnVector lb = args(3).to_vector (); |
|
293 ColumnVector ub = args(4).to_vector (); |
1
|
294 |
|
295 int lb_len = lb.capacity (); |
|
296 int ub_len = ub.capacity (); |
|
297 if (lb_len != ub_len || lb_len != x.capacity ()) |
|
298 { |
214
|
299 error ("npsol: lower and upper bounds and decision variable vector"); |
|
300 error ("must all have the same number of elements"); |
1
|
301 return retval; |
|
302 } |
|
303 |
|
304 bounds.resize (lb_len); |
|
305 bounds.set_lower_bounds (lb); |
|
306 bounds.set_upper_bounds (ub); |
|
307 } |
|
308 |
|
309 double objf; |
|
310 ColumnVector lambda; |
|
311 int inform; |
|
312 |
|
313 if (nargin == 3) |
|
314 { |
|
315 // 1. npsol (x, phi) |
|
316 |
|
317 NPSOL nlp (x, func); |
287
|
318 nlp.copy (npsol_opts); |
1
|
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); |
287
|
329 nlp.copy (npsol_opts); |
1
|
330 soln = nlp.minimize (objf, inform, lambda); |
|
331 |
|
332 goto solved; |
|
333 } |
|
334 |
488
|
335 npsol_constraints = (tree_fvc *) NULL; |
1
|
336 if (nargin == 6 || nargin == 8 || nargin == 9 || nargin == 11) |
497
|
337 npsol_constraints = is_valid_function (args(nargin-2), "npsol", 0); |
1
|
338 |
|
339 if (nargin == 8 || nargin == 6) |
|
340 { |
488
|
341 if (npsol_constraints == (tree_fvc *) NULL) |
1
|
342 { |
497
|
343 ColumnVector lub = args(nargin-1).to_vector (); |
|
344 Matrix c = args(nargin-2).to_matrix (); |
|
345 ColumnVector llb = args(nargin-3).to_vector (); |
1
|
346 |
215
|
347 if (llb.capacity () == 0 || lub.capacity () == 0) |
|
348 { |
|
349 error ("npsol: bounds for linear constraints must be vectors"); |
|
350 return retval; |
|
351 } |
1
|
352 |
|
353 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
354 return retval; |
|
355 |
215
|
356 LinConst linear_constraints (llb, c, lub); |
|
357 |
1
|
358 if (nargin == 6) |
|
359 { |
|
360 // 7. npsol (x, phi, llb, c, lub) |
|
361 |
|
362 NPSOL nlp (x, func, linear_constraints); |
287
|
363 nlp.copy (npsol_opts); |
1
|
364 soln = nlp.minimize (objf, inform, lambda); |
|
365 } |
|
366 else |
|
367 { |
|
368 // 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
369 |
|
370 NPSOL nlp (x, func, bounds, linear_constraints); |
287
|
371 nlp.copy (npsol_opts); |
1
|
372 soln = nlp.minimize (objf, inform, lambda); |
|
373 } |
|
374 goto solved; |
|
375 } |
|
376 else |
|
377 { |
|
378 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
379 { |
497
|
380 ColumnVector nlub = args(nargin-1).to_vector (); |
|
381 ColumnVector nllb = args(nargin-3).to_vector (); |
1
|
382 |
|
383 NLFunc const_func (npsol_constraint_function); |
|
384 |
|
385 if (! nonlinear_constraints_ok |
|
386 (x, nllb, npsol_constraint_function, nlub, "npsol", 1)) |
|
387 return retval; |
|
388 |
|
389 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
390 |
|
391 if (nargin == 6) |
|
392 { |
|
393 // 8. npsol (x, phi, nllb, g, nlub) |
|
394 |
|
395 NPSOL nlp (x, func, nonlinear_constraints); |
287
|
396 nlp.copy (npsol_opts); |
1
|
397 soln = nlp.minimize (objf, inform, lambda); |
|
398 } |
|
399 else |
|
400 { |
|
401 // 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
402 |
|
403 NPSOL nlp (x, func, bounds, nonlinear_constraints); |
287
|
404 nlp.copy (npsol_opts); |
1
|
405 soln = nlp.minimize (objf, inform, lambda); |
|
406 } |
|
407 goto solved; |
|
408 } |
|
409 } |
|
410 } |
|
411 |
|
412 if (nargin == 9 || nargin == 11) |
|
413 { |
488
|
414 if (npsol_constraints == (tree_fvc *) NULL) |
1
|
415 { |
|
416 // Produce error message. |
497
|
417 is_valid_function (args(nargin-2), "npsol", 1); |
1
|
418 } |
|
419 else |
|
420 { |
|
421 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
422 { |
497
|
423 ColumnVector nlub = args(nargin-1).to_vector (); |
|
424 ColumnVector nllb = args(nargin-3).to_vector (); |
1
|
425 |
|
426 NLFunc const_func (npsol_constraint_function); |
|
427 |
|
428 if (! nonlinear_constraints_ok |
|
429 (x, nllb, npsol_constraint_function, nlub, "npsol", 1)) |
|
430 return retval; |
|
431 |
|
432 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
433 |
497
|
434 ColumnVector lub = args(nargin-4).to_vector (); |
|
435 Matrix c = args(nargin-5).to_matrix (); |
|
436 ColumnVector llb = args(nargin-6).to_vector (); |
1
|
437 |
215
|
438 if (llb.capacity () == 0 || lub.capacity () == 0) |
|
439 { |
|
440 error ("npsol: bounds for linear constraints must be vectors"); |
|
441 return retval; |
|
442 } |
|
443 |
1
|
444 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
445 return retval; |
|
446 |
|
447 LinConst linear_constraints (llb, c, lub); |
|
448 |
|
449 if (nargin == 9) |
|
450 { |
|
451 // 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
452 |
|
453 NPSOL nlp (x, func, linear_constraints, |
|
454 nonlinear_constraints); |
287
|
455 nlp.copy (npsol_opts); |
1
|
456 soln = nlp.minimize (objf, inform, lambda); |
|
457 } |
|
458 else |
|
459 { |
|
460 // 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
461 |
|
462 NPSOL nlp (x, func, bounds, linear_constraints, |
|
463 nonlinear_constraints); |
287
|
464 nlp.copy (npsol_opts); |
1
|
465 soln = nlp.minimize (objf, inform, lambda); |
|
466 } |
|
467 goto solved; |
|
468 } |
|
469 } |
|
470 } |
|
471 |
|
472 return retval; |
|
473 |
|
474 solved: |
|
475 |
497
|
476 retval.resize (nargout ? nargout : 1); |
|
477 retval(0) = tree_constant (soln, 1); |
1
|
478 if (nargout > 1) |
497
|
479 retval(1) = tree_constant (objf); |
1
|
480 if (nargout > 2) |
497
|
481 retval(2) = tree_constant ((double) inform); |
1
|
482 if (nargout > 3) |
497
|
483 retval(3) = tree_constant (lambda); |
1
|
484 |
|
485 return retval; |
|
486 } |
|
487 |
287
|
488 typedef void (NPSOL_options::*d_set_opt_mf) (double); |
|
489 typedef void (NPSOL_options::*i_set_opt_mf) (int); |
|
490 typedef double (NPSOL_options::*d_get_opt_mf) (void); |
|
491 typedef int (NPSOL_options::*i_get_opt_mf) (void); |
|
492 |
|
493 #define MAX_TOKENS 5 |
|
494 |
|
495 struct NPSOL_OPTIONS |
|
496 { |
|
497 char *keyword; |
|
498 char *kw_tok[MAX_TOKENS + 1]; |
|
499 int min_len[MAX_TOKENS + 1]; |
|
500 int min_toks_to_match; |
|
501 d_set_opt_mf d_set_fcn; |
|
502 i_set_opt_mf i_set_fcn; |
|
503 d_get_opt_mf d_get_fcn; |
|
504 i_get_opt_mf i_get_fcn; |
|
505 }; |
|
506 |
497
|
507 static NPSOL_OPTIONS npsol_option_table [] = |
287
|
508 { |
|
509 { "central difference interval", |
|
510 { "central", "difference", "interval", NULL, NULL, NULL, }, |
|
511 { 2, 0, 0, 0, 0, 0, }, 1, |
|
512 NPSOL_options::set_central_difference_interval, NULL, |
|
513 NPSOL_options::central_difference_interval, NULL, }, |
|
514 |
|
515 { "crash tolerance", |
|
516 { "crash", "tolerance", NULL, NULL, NULL, NULL, }, |
|
517 { 2, 0, 0, 0, 0, 0, }, 1, |
|
518 NPSOL_options::set_crash_tolerance, NULL, |
|
519 NPSOL_options::crash_tolerance, NULL, }, |
|
520 |
|
521 { "derivative level", |
|
522 { "derivative", "level", NULL, NULL, NULL, NULL, }, |
|
523 { 1, 0, 0, 0, 0, 0, }, 1, |
|
524 NULL, NPSOL_options::set_derivative_level, |
|
525 NULL, NPSOL_options::derivative_level, }, |
|
526 |
|
527 { "difference interval", |
|
528 { "difference", "interval", NULL, NULL, NULL, NULL, }, |
|
529 { 3, 0, 0, 0, 0, 0, }, 1, |
|
530 NPSOL_options::set_difference_interval, NULL, |
|
531 NPSOL_options::difference_interval, NULL, }, |
|
532 |
|
533 { "function precision", |
|
534 { "function", "precision", NULL, NULL, NULL, NULL, }, |
|
535 { 2, 0, 0, 0, 0, 0, }, 1, |
|
536 NPSOL_options::set_function_precision, NULL, |
|
537 NPSOL_options::function_precision, NULL, }, |
|
538 |
|
539 { "infinite bound size", |
|
540 { "infinite", "bound", "size", NULL, NULL, NULL, }, |
|
541 { 1, 1, 0, 0, 0, 0, }, 2, |
|
542 NPSOL_options::set_infinite_bound, NULL, |
|
543 NPSOL_options::infinite_bound, NULL, }, |
|
544 |
|
545 { "infinite step size", |
|
546 { "infinite", "step", "size", NULL, NULL, NULL, }, |
|
547 { 1, 1, 0, 0, 0, 0, }, 2, |
|
548 NPSOL_options::set_infinite_step, NULL, |
|
549 NPSOL_options::infinite_step, NULL, }, |
|
550 |
|
551 { "linear feasibility tolerance", |
|
552 { "linear", "feasibility", "tolerance", NULL, NULL, NULL, }, |
|
553 { 5, 0, 0, 0, 0, 0, }, 1, |
|
554 NPSOL_options::set_linear_feasibility_tolerance, NULL, |
|
555 NPSOL_options::linear_feasibility_tolerance, NULL, }, |
|
556 |
|
557 { "linesearch tolerance", |
|
558 { "linesearch", "tolerance", NULL, NULL, NULL, NULL, }, |
|
559 { 5, 0, 0, 0, 0, 0, }, 1, |
|
560 NPSOL_options::set_linesearch_tolerance, NULL, |
|
561 NPSOL_options::linesearch_tolerance, NULL, }, |
|
562 |
|
563 { "major iteration limit", |
|
564 { "major", "iteration", "limit", NULL, NULL, NULL, }, |
|
565 { 2, 1, 0, 0, 0, 0, }, 2, |
|
566 NULL, NPSOL_options::set_major_iteration_limit, |
|
567 NULL, NPSOL_options::major_iteration_limit, }, |
|
568 |
|
569 { "minor iteration limit", |
|
570 { "minor", "iteration", "limit", NULL, NULL, NULL, }, |
|
571 { 2, 1, 0, 0, 0, 0, }, 2, |
|
572 NULL, NPSOL_options::set_minor_iteration_limit, |
|
573 NULL, NPSOL_options::minor_iteration_limit, }, |
|
574 |
|
575 { "major print level", |
|
576 { "major", "print", "level", NULL, NULL, NULL, }, |
|
577 { 2, 1, 0, 0, 0, 0, }, 2, |
|
578 NULL, NPSOL_options::set_major_print_level, |
|
579 NULL, NPSOL_options::major_print_level, }, |
|
580 |
|
581 { "minor print level", |
|
582 { "minor", "print", "level", NULL, NULL, NULL, }, |
|
583 { 2, 1, 0, 0, 0, 0, }, 2, |
|
584 NULL, NPSOL_options::set_minor_print_level, |
|
585 NULL, NPSOL_options::minor_print_level, }, |
|
586 |
|
587 { "nonlinear feasibility tolerance", |
|
588 { "nonlinear", "feasibility", "tolerance", NULL, NULL, }, |
|
589 { 1, 0, 0, 0, 0, 0, }, 1, |
|
590 NPSOL_options::set_nonlinear_feasibility_tolerance, NULL, |
|
591 NPSOL_options::nonlinear_feasibility_tolerance, NULL, }, |
|
592 |
|
593 { "optimality tolerance", |
|
594 { "optimality", "tolerance", NULL, NULL, NULL, NULL, }, |
|
595 { 1, 0, 0, 0, 0, 0, }, 1, |
|
596 NPSOL_options::set_optimality_tolerance, NULL, |
|
597 NPSOL_options::optimality_tolerance, NULL, }, |
|
598 |
|
599 { "start objective check at variable", |
|
600 { "start", "objective", "check", "at", "variable", NULL, }, |
|
601 { 3, 1, 0, 0, 0, 0, }, 2, |
|
602 NULL, NPSOL_options::set_start_objective_check, |
|
603 NULL, NPSOL_options::start_objective_check, }, |
|
604 |
|
605 { "start constraint check at variable", |
|
606 { "start", "constraint", "check", "at", "variable", NULL, }, |
|
607 { 3, 1, 0, 0, 0, 0, }, 2, |
|
608 NULL, NPSOL_options::set_start_constraint_check, |
|
609 NULL, NPSOL_options::start_constraint_check, }, |
|
610 |
|
611 { "stop objective check at variable", |
|
612 { "stop", "objective", "check", "at", "variable", NULL, }, |
|
613 { 3, 1, 0, 0, 0, 0, }, 2, |
|
614 NULL, NPSOL_options::set_stop_objective_check, |
|
615 NULL, NPSOL_options::stop_objective_check, }, |
|
616 |
|
617 { "stop constraint check at variable", |
|
618 { "stop", "constraint", "check", "at", "variable", NULL, }, |
|
619 { 3, 1, 0, 0, 0, 0, }, 2, |
|
620 NULL, NPSOL_options::set_stop_constraint_check, |
|
621 NULL, NPSOL_options::stop_constraint_check, }, |
|
622 |
|
623 { "verify level", |
|
624 { "verify", "level", NULL, NULL, NULL, NULL, }, |
|
625 { 1, 0, 0, 0, 0, 0, }, 1, |
|
626 NULL, NPSOL_options::set_verify_level, |
|
627 NULL, NPSOL_options::verify_level, }, |
|
628 |
|
629 { NULL, |
|
630 { NULL, NULL, NULL, NULL, NULL, NULL, }, |
|
631 { 0, 0, 0, 0, 0, 0, }, 0, |
|
632 NULL, NULL, NULL, NULL, }, |
|
633 }; |
|
634 |
|
635 static void |
|
636 print_npsol_option_list (void) |
|
637 { |
|
638 ostrstream output_buf; |
|
639 |
|
640 print_usage ("npsol_options", 1); |
|
641 |
|
642 output_buf << "\n" |
|
643 << "Options for npsol include:\n\n" |
|
644 << " keyword value\n" |
|
645 << " ------- -----\n\n"; |
|
646 |
|
647 NPSOL_OPTIONS *list = npsol_option_table; |
|
648 |
|
649 char *keyword; |
|
650 while ((keyword = list->keyword) != (char *) NULL) |
|
651 { |
|
652 output_buf.form (" %-40s ", keyword); |
|
653 if (list->d_get_fcn) |
|
654 { |
|
655 double val = (npsol_opts.*list->d_get_fcn) (); |
|
656 if (val < 0.0) |
|
657 output_buf << "computed automatically"; |
|
658 else |
|
659 output_buf << val; |
|
660 } |
|
661 else |
|
662 { |
|
663 int val = (npsol_opts.*list->i_get_fcn) (); |
|
664 if (val < 0) |
|
665 output_buf << "depends on problem size"; |
|
666 else |
|
667 output_buf << val; |
|
668 } |
|
669 output_buf << "\n"; |
|
670 list++; |
|
671 } |
|
672 |
|
673 output_buf << "\n" << ends; |
|
674 maybe_page_output (output_buf); |
|
675 } |
|
676 |
|
677 static void |
|
678 do_npsol_option (char *keyword, double val) |
|
679 { |
|
680 NPSOL_OPTIONS *list = npsol_option_table; |
|
681 |
|
682 while (list->keyword != (char *) NULL) |
|
683 { |
|
684 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
685 list->min_toks_to_match, MAX_TOKENS)) |
|
686 { |
|
687 if (list->d_set_fcn) |
|
688 (npsol_opts.*list->d_set_fcn) (val); |
|
689 else |
|
690 (npsol_opts.*list->i_set_fcn) (NINT (val)); |
|
691 |
|
692 return; |
|
693 } |
|
694 list++; |
|
695 } |
|
696 |
|
697 warning ("npsol_options: no match for `%s'", keyword); |
|
698 } |
|
699 |
497
|
700 Octave_object |
506
|
701 npsol_options (const Octave_object& args, int nargout) |
272
|
702 { |
497
|
703 Octave_object retval; |
272
|
704 |
506
|
705 int nargin = args.length (); |
|
706 |
287
|
707 if (nargin == 1) |
|
708 { |
|
709 print_npsol_option_list (); |
|
710 } |
|
711 else if (nargin == 3) |
|
712 { |
497
|
713 if (args(1).is_string_type ()) |
287
|
714 { |
497
|
715 char *keyword = args(1).string_value (); |
|
716 double val = args(2).double_value (); |
287
|
717 do_npsol_option (keyword, val); |
|
718 } |
|
719 else |
|
720 print_usage ("npsol_options"); |
|
721 } |
|
722 else |
|
723 { |
|
724 print_usage ("npsol_options"); |
|
725 } |
|
726 |
272
|
727 return retval; |
|
728 } |
|
729 |
1
|
730 #endif |
|
731 |
|
732 /* |
|
733 ;;; Local Variables: *** |
|
734 ;;; mode: C++ *** |
|
735 ;;; page-delimiter: "^/\\*" *** |
|
736 ;;; End: *** |
|
737 */ |