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 |
287
|
28 #include <strstream.h> |
|
29 |
1
|
30 #include "NPSOL.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
287
|
36 #include "pager.h" |
1
|
37 #include "utils.h" |
544
|
38 #include "help.h" |
519
|
39 #include "defun-dld.h" |
1
|
40 |
646
|
41 #ifndef NPSOL_MISSING |
|
42 |
1
|
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 { |
628
|
160 retval = tmp(0).vector_value (); |
255
|
161 |
636
|
162 if (error_state || retval.length () <= 0) |
255
|
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 |
646
|
232 #endif |
|
233 |
519
|
234 #if defined (NPSOL_MISSING) |
701
|
235 DEFUN_DLD_BUILTIN ("npsol", Fnpsol, Snpsol, 11, 3, |
519
|
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 |
701
|
240 DEFUN_DLD_BUILTIN ("npsol", Fnpsol, Snpsol, 11, 3, |
519
|
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 |
628
|
299 ColumnVector x = args(1).vector_value (); |
1
|
300 |
636
|
301 if (error_state || x.capacity () == 0) |
1
|
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 { |
628
|
319 ColumnVector lb = args(3).vector_value (); |
|
320 ColumnVector ub = args(4).vector_value (); |
1
|
321 |
|
322 int lb_len = lb.capacity (); |
|
323 int ub_len = ub.capacity (); |
636
|
324 |
|
325 if (error_state || lb_len != ub_len || lb_len != x.capacity ()) |
1
|
326 { |
214
|
327 error ("npsol: lower and upper bounds and decision variable vector"); |
|
328 error ("must all have the same number of elements"); |
1
|
329 return retval; |
|
330 } |
|
331 |
|
332 bounds.resize (lb_len); |
|
333 bounds.set_lower_bounds (lb); |
|
334 bounds.set_upper_bounds (ub); |
|
335 } |
|
336 |
|
337 double objf; |
|
338 ColumnVector lambda; |
|
339 int inform; |
|
340 |
|
341 if (nargin == 3) |
|
342 { |
|
343 // 1. npsol (x, phi) |
|
344 |
|
345 NPSOL nlp (x, func); |
287
|
346 nlp.copy (npsol_opts); |
1
|
347 soln = nlp.minimize (objf, inform, lambda); |
|
348 |
|
349 goto solved; |
|
350 } |
|
351 |
|
352 if (nargin == 5) |
|
353 { |
|
354 // 2. npsol (x, phi, lb, ub) |
|
355 |
|
356 NPSOL nlp (x, func, bounds); |
287
|
357 nlp.copy (npsol_opts); |
1
|
358 soln = nlp.minimize (objf, inform, lambda); |
|
359 |
|
360 goto solved; |
|
361 } |
|
362 |
519
|
363 npsol_constraints = 0; |
1
|
364 if (nargin == 6 || nargin == 8 || nargin == 9 || nargin == 11) |
497
|
365 npsol_constraints = is_valid_function (args(nargin-2), "npsol", 0); |
1
|
366 |
|
367 if (nargin == 8 || nargin == 6) |
|
368 { |
519
|
369 if (! npsol_constraints) |
1
|
370 { |
628
|
371 ColumnVector lub = args(nargin-1).vector_value (); |
|
372 ColumnVector llb = args(nargin-3).vector_value (); |
1
|
373 |
636
|
374 if (error_state || llb.capacity () == 0 || lub.capacity () == 0) |
215
|
375 { |
|
376 error ("npsol: bounds for linear constraints must be vectors"); |
|
377 return retval; |
|
378 } |
1
|
379 |
636
|
380 Matrix c = args(nargin-2).matrix_value (); |
|
381 |
|
382 if (error_state) |
|
383 { |
|
384 error ("npsol: invalid linear constraint matrix"); |
|
385 return retval; |
|
386 } |
|
387 |
1
|
388 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
389 return retval; |
|
390 |
215
|
391 LinConst linear_constraints (llb, c, lub); |
|
392 |
1
|
393 if (nargin == 6) |
|
394 { |
|
395 // 7. npsol (x, phi, llb, c, lub) |
|
396 |
|
397 NPSOL nlp (x, func, linear_constraints); |
287
|
398 nlp.copy (npsol_opts); |
1
|
399 soln = nlp.minimize (objf, inform, lambda); |
|
400 } |
|
401 else |
|
402 { |
|
403 // 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
404 |
|
405 NPSOL nlp (x, func, bounds, linear_constraints); |
287
|
406 nlp.copy (npsol_opts); |
1
|
407 soln = nlp.minimize (objf, inform, lambda); |
|
408 } |
|
409 goto solved; |
|
410 } |
|
411 else |
|
412 { |
|
413 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
414 { |
628
|
415 ColumnVector nlub = args(nargin-1).vector_value (); |
|
416 ColumnVector nllb = args(nargin-3).vector_value (); |
1
|
417 |
636
|
418 if (error_state |
|
419 || (! nonlinear_constraints_ok |
|
420 (x, nllb, npsol_constraint_function, nlub, "npsol", 1))) |
1
|
421 return retval; |
|
422 |
636
|
423 NLFunc const_func (npsol_constraint_function); |
1
|
424 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
425 |
|
426 if (nargin == 6) |
|
427 { |
|
428 // 8. npsol (x, phi, nllb, g, nlub) |
|
429 |
|
430 NPSOL nlp (x, func, nonlinear_constraints); |
287
|
431 nlp.copy (npsol_opts); |
1
|
432 soln = nlp.minimize (objf, inform, lambda); |
|
433 } |
|
434 else |
|
435 { |
|
436 // 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
437 |
|
438 NPSOL nlp (x, func, bounds, nonlinear_constraints); |
287
|
439 nlp.copy (npsol_opts); |
1
|
440 soln = nlp.minimize (objf, inform, lambda); |
|
441 } |
|
442 goto solved; |
|
443 } |
|
444 } |
|
445 } |
|
446 |
|
447 if (nargin == 9 || nargin == 11) |
|
448 { |
519
|
449 if (! npsol_constraints) |
1
|
450 { |
|
451 // Produce error message. |
497
|
452 is_valid_function (args(nargin-2), "npsol", 1); |
1
|
453 } |
|
454 else |
|
455 { |
|
456 if (takes_correct_nargs (npsol_constraints, 2, "npsol", 1)) |
|
457 { |
628
|
458 ColumnVector nlub = args(nargin-1).vector_value (); |
|
459 ColumnVector nllb = args(nargin-3).vector_value (); |
1
|
460 |
636
|
461 if (error_state |
|
462 || (! nonlinear_constraints_ok |
|
463 (x, nllb, npsol_constraint_function, nlub, "npsol", 1))) |
1
|
464 return retval; |
|
465 |
636
|
466 NLFunc const_func (npsol_constraint_function); |
1
|
467 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
468 |
628
|
469 ColumnVector lub = args(nargin-4).vector_value (); |
|
470 ColumnVector llb = args(nargin-6).vector_value (); |
1
|
471 |
636
|
472 if (error_state || llb.capacity () == 0 || lub.capacity () == 0) |
215
|
473 { |
|
474 error ("npsol: bounds for linear constraints must be vectors"); |
|
475 return retval; |
|
476 } |
636
|
477 |
|
478 Matrix c = args(nargin-5).matrix_value (); |
|
479 |
|
480 if (error_state) |
|
481 { |
|
482 error ("npsol: invalid linear constraint matrix"); |
|
483 return retval; |
|
484 } |
215
|
485 |
1
|
486 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
487 return retval; |
|
488 |
|
489 LinConst linear_constraints (llb, c, lub); |
|
490 |
|
491 if (nargin == 9) |
|
492 { |
|
493 // 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
494 |
|
495 NPSOL nlp (x, func, linear_constraints, |
|
496 nonlinear_constraints); |
287
|
497 nlp.copy (npsol_opts); |
1
|
498 soln = nlp.minimize (objf, inform, lambda); |
|
499 } |
|
500 else |
|
501 { |
|
502 // 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
503 |
|
504 NPSOL nlp (x, func, bounds, linear_constraints, |
|
505 nonlinear_constraints); |
287
|
506 nlp.copy (npsol_opts); |
1
|
507 soln = nlp.minimize (objf, inform, lambda); |
|
508 } |
|
509 goto solved; |
|
510 } |
|
511 } |
|
512 } |
|
513 |
|
514 return retval; |
|
515 |
|
516 solved: |
|
517 |
497
|
518 retval.resize (nargout ? nargout : 1); |
516
|
519 retval(0) = soln, 1; |
1
|
520 if (nargout > 1) |
516
|
521 retval(1) = objf; |
1
|
522 if (nargout > 2) |
516
|
523 retval(2) = (double) inform; |
1
|
524 if (nargout > 3) |
516
|
525 retval(3) = lambda; |
1
|
526 |
519
|
527 #endif |
|
528 |
1
|
529 return retval; |
|
530 } |
|
531 |
646
|
532 #ifndef NPSOL_MISSING |
|
533 |
287
|
534 typedef void (NPSOL_options::*d_set_opt_mf) (double); |
|
535 typedef void (NPSOL_options::*i_set_opt_mf) (int); |
|
536 typedef double (NPSOL_options::*d_get_opt_mf) (void); |
|
537 typedef int (NPSOL_options::*i_get_opt_mf) (void); |
|
538 |
|
539 #define MAX_TOKENS 5 |
|
540 |
|
541 struct NPSOL_OPTIONS |
|
542 { |
540
|
543 const char *keyword; |
|
544 const char *kw_tok[MAX_TOKENS + 1]; |
287
|
545 int min_len[MAX_TOKENS + 1]; |
|
546 int min_toks_to_match; |
|
547 d_set_opt_mf d_set_fcn; |
|
548 i_set_opt_mf i_set_fcn; |
|
549 d_get_opt_mf d_get_fcn; |
|
550 i_get_opt_mf i_get_fcn; |
|
551 }; |
|
552 |
497
|
553 static NPSOL_OPTIONS npsol_option_table [] = |
287
|
554 { |
|
555 { "central difference interval", |
519
|
556 { "central", "difference", "interval", 0, 0, 0, }, |
287
|
557 { 2, 0, 0, 0, 0, 0, }, 1, |
519
|
558 NPSOL_options::set_central_difference_interval, 0, |
|
559 NPSOL_options::central_difference_interval, 0, }, |
287
|
560 |
|
561 { "crash tolerance", |
519
|
562 { "crash", "tolerance", 0, 0, 0, 0, }, |
287
|
563 { 2, 0, 0, 0, 0, 0, }, 1, |
519
|
564 NPSOL_options::set_crash_tolerance, 0, |
|
565 NPSOL_options::crash_tolerance, 0, }, |
287
|
566 |
|
567 { "derivative level", |
519
|
568 { "derivative", "level", 0, 0, 0, 0, }, |
287
|
569 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
570 0, NPSOL_options::set_derivative_level, |
|
571 0, NPSOL_options::derivative_level, }, |
287
|
572 |
|
573 { "difference interval", |
519
|
574 { "difference", "interval", 0, 0, 0, 0, }, |
287
|
575 { 3, 0, 0, 0, 0, 0, }, 1, |
519
|
576 NPSOL_options::set_difference_interval, 0, |
|
577 NPSOL_options::difference_interval, 0, }, |
287
|
578 |
|
579 { "function precision", |
519
|
580 { "function", "precision", 0, 0, 0, 0, }, |
287
|
581 { 2, 0, 0, 0, 0, 0, }, 1, |
519
|
582 NPSOL_options::set_function_precision, 0, |
|
583 NPSOL_options::function_precision, 0, }, |
287
|
584 |
|
585 { "infinite bound size", |
519
|
586 { "infinite", "bound", "size", 0, 0, 0, }, |
287
|
587 { 1, 1, 0, 0, 0, 0, }, 2, |
519
|
588 NPSOL_options::set_infinite_bound, 0, |
|
589 NPSOL_options::infinite_bound, 0, }, |
287
|
590 |
|
591 { "infinite step size", |
519
|
592 { "infinite", "step", "size", 0, 0, 0, }, |
287
|
593 { 1, 1, 0, 0, 0, 0, }, 2, |
519
|
594 NPSOL_options::set_infinite_step, 0, |
|
595 NPSOL_options::infinite_step, 0, }, |
287
|
596 |
|
597 { "linear feasibility tolerance", |
519
|
598 { "linear", "feasibility", "tolerance", 0, 0, 0, }, |
287
|
599 { 5, 0, 0, 0, 0, 0, }, 1, |
519
|
600 NPSOL_options::set_linear_feasibility_tolerance, 0, |
|
601 NPSOL_options::linear_feasibility_tolerance, 0, }, |
287
|
602 |
|
603 { "linesearch tolerance", |
519
|
604 { "linesearch", "tolerance", 0, 0, 0, 0, }, |
287
|
605 { 5, 0, 0, 0, 0, 0, }, 1, |
519
|
606 NPSOL_options::set_linesearch_tolerance, 0, |
|
607 NPSOL_options::linesearch_tolerance, 0, }, |
287
|
608 |
|
609 { "major iteration limit", |
519
|
610 { "major", "iteration", "limit", 0, 0, 0, }, |
287
|
611 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
612 0, NPSOL_options::set_major_iteration_limit, |
|
613 0, NPSOL_options::major_iteration_limit, }, |
287
|
614 |
|
615 { "minor iteration limit", |
519
|
616 { "minor", "iteration", "limit", 0, 0, 0, }, |
287
|
617 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
618 0, NPSOL_options::set_minor_iteration_limit, |
|
619 0, NPSOL_options::minor_iteration_limit, }, |
287
|
620 |
|
621 { "major print level", |
519
|
622 { "major", "print", "level", 0, 0, 0, }, |
287
|
623 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
624 0, NPSOL_options::set_major_print_level, |
|
625 0, NPSOL_options::major_print_level, }, |
287
|
626 |
|
627 { "minor print level", |
519
|
628 { "minor", "print", "level", 0, 0, 0, }, |
287
|
629 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
630 0, NPSOL_options::set_minor_print_level, |
|
631 0, NPSOL_options::minor_print_level, }, |
287
|
632 |
|
633 { "nonlinear feasibility tolerance", |
519
|
634 { "nonlinear", "feasibility", "tolerance", 0, 0, }, |
287
|
635 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
636 NPSOL_options::set_nonlinear_feasibility_tolerance, 0, |
|
637 NPSOL_options::nonlinear_feasibility_tolerance, 0, }, |
287
|
638 |
|
639 { "optimality tolerance", |
519
|
640 { "optimality", "tolerance", 0, 0, 0, 0, }, |
287
|
641 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
642 NPSOL_options::set_optimality_tolerance, 0, |
|
643 NPSOL_options::optimality_tolerance, 0, }, |
287
|
644 |
|
645 { "start objective check at variable", |
519
|
646 { "start", "objective", "check", "at", "variable", 0, }, |
287
|
647 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
648 0, NPSOL_options::set_start_objective_check, |
|
649 0, NPSOL_options::start_objective_check, }, |
287
|
650 |
|
651 { "start constraint check at variable", |
519
|
652 { "start", "constraint", "check", "at", "variable", 0, }, |
287
|
653 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
654 0, NPSOL_options::set_start_constraint_check, |
|
655 0, NPSOL_options::start_constraint_check, }, |
287
|
656 |
|
657 { "stop objective check at variable", |
519
|
658 { "stop", "objective", "check", "at", "variable", 0, }, |
287
|
659 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
660 0, NPSOL_options::set_stop_objective_check, |
|
661 0, NPSOL_options::stop_objective_check, }, |
287
|
662 |
|
663 { "stop constraint check at variable", |
519
|
664 { "stop", "constraint", "check", "at", "variable", 0, }, |
287
|
665 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
666 0, NPSOL_options::set_stop_constraint_check, |
|
667 0, NPSOL_options::stop_constraint_check, }, |
287
|
668 |
|
669 { "verify level", |
519
|
670 { "verify", "level", 0, 0, 0, 0, }, |
287
|
671 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
672 0, NPSOL_options::set_verify_level, |
|
673 0, NPSOL_options::verify_level, }, |
287
|
674 |
519
|
675 { 0, |
|
676 { 0, 0, 0, 0, 0, 0, }, |
287
|
677 { 0, 0, 0, 0, 0, 0, }, 0, |
519
|
678 0, 0, 0, 0, }, |
287
|
679 }; |
|
680 |
|
681 static void |
|
682 print_npsol_option_list (void) |
|
683 { |
|
684 ostrstream output_buf; |
|
685 |
|
686 print_usage ("npsol_options", 1); |
|
687 |
|
688 output_buf << "\n" |
|
689 << "Options for npsol include:\n\n" |
|
690 << " keyword value\n" |
|
691 << " ------- -----\n\n"; |
|
692 |
|
693 NPSOL_OPTIONS *list = npsol_option_table; |
|
694 |
540
|
695 const char *keyword; |
519
|
696 while ((keyword = list->keyword) != 0) |
287
|
697 { |
|
698 output_buf.form (" %-40s ", keyword); |
|
699 if (list->d_get_fcn) |
|
700 { |
|
701 double val = (npsol_opts.*list->d_get_fcn) (); |
|
702 if (val < 0.0) |
|
703 output_buf << "computed automatically"; |
|
704 else |
|
705 output_buf << val; |
|
706 } |
|
707 else |
|
708 { |
|
709 int val = (npsol_opts.*list->i_get_fcn) (); |
|
710 if (val < 0) |
|
711 output_buf << "depends on problem size"; |
|
712 else |
|
713 output_buf << val; |
|
714 } |
|
715 output_buf << "\n"; |
|
716 list++; |
|
717 } |
|
718 |
|
719 output_buf << "\n" << ends; |
|
720 maybe_page_output (output_buf); |
|
721 } |
|
722 |
|
723 static void |
|
724 do_npsol_option (char *keyword, double val) |
|
725 { |
|
726 NPSOL_OPTIONS *list = npsol_option_table; |
|
727 |
519
|
728 while (list->keyword != 0) |
287
|
729 { |
|
730 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
731 list->min_toks_to_match, MAX_TOKENS)) |
|
732 { |
|
733 if (list->d_set_fcn) |
|
734 (npsol_opts.*list->d_set_fcn) (val); |
|
735 else |
|
736 (npsol_opts.*list->i_set_fcn) (NINT (val)); |
|
737 |
|
738 return; |
|
739 } |
|
740 list++; |
|
741 } |
|
742 |
|
743 warning ("npsol_options: no match for `%s'", keyword); |
|
744 } |
|
745 |
646
|
746 #endif |
|
747 |
519
|
748 #if defined (NPSOL_MISSING) |
701
|
749 DEFUN_DLD_BUILTIN ("npsol_options", Fnpsol_options, Snpsol_options, -1, 1, |
519
|
750 "This function requires NPSOL, which is not freely\n\ |
|
751 redistributable. For more information, read the file\n\ |
|
752 libcruft/npsol/README.MISSING in the source distribution.") |
|
753 #else |
701
|
754 DEFUN_DLD_BUILTIN ("npsol_options", Fnpsol_options, Snpsol_options, -1, 1, |
519
|
755 "npsol_options (KEYWORD, VALUE)\n\ |
|
756 \n\ |
|
757 Set or show options for npsol. Keywords may be abbreviated\n\ |
|
758 to the shortest match.") |
|
759 #endif |
272
|
760 { |
497
|
761 Octave_object retval; |
272
|
762 |
519
|
763 #if defined (NPSOL_MISSING) |
|
764 |
|
765 print_usage ("npsol_options"); |
|
766 |
|
767 #else |
|
768 |
506
|
769 int nargin = args.length (); |
|
770 |
287
|
771 if (nargin == 1) |
|
772 { |
|
773 print_npsol_option_list (); |
636
|
774 return retval; |
287
|
775 } |
|
776 else if (nargin == 3) |
|
777 { |
636
|
778 char *keyword = args(1).string_value (); |
|
779 |
|
780 if (! error_state) |
287
|
781 { |
497
|
782 double val = args(2).double_value (); |
636
|
783 |
|
784 if (! error_state) |
|
785 { |
|
786 do_npsol_option (keyword, val); |
|
787 return retval; |
|
788 } |
287
|
789 } |
|
790 } |
636
|
791 |
|
792 print_usage ("npsol_options"); |
287
|
793 |
519
|
794 #endif |
|
795 |
272
|
796 return retval; |
|
797 } |
|
798 |
1
|
799 /* |
|
800 ;;; Local Variables: *** |
|
801 ;;; mode: C++ *** |
|
802 ;;; page-delimiter: "^/\\*" *** |
|
803 ;;; End: *** |
|
804 */ |