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