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