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