7
|
1 // f-npsol.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1728
|
28 #include <string> |
|
29 |
287
|
30 #include <strstream.h> |
|
31 |
1
|
32 #include "NPSOL.h" |
|
33 |
1352
|
34 #include "defun-dld.h" |
1
|
35 #include "error.h" |
1352
|
36 #include "gripes.h" |
|
37 #include "help.h" |
1740
|
38 #include "mappers.h" |
|
39 #include "oct-obj.h" |
287
|
40 #include "pager.h" |
1
|
41 #include "utils.h" |
1352
|
42 #include "variables.h" |
1
|
43 |
646
|
44 #ifndef NPSOL_MISSING |
|
45 |
1
|
46 // Global pointers for user defined functions required by npsol. |
488
|
47 static tree_fvc *npsol_objective; |
|
48 static tree_fvc *npsol_constraints; |
1
|
49 |
287
|
50 static NPSOL_options npsol_opts; |
|
51 |
1
|
52 double |
162
|
53 npsol_objective_function (const ColumnVector& x) |
1
|
54 { |
|
55 int n = x.capacity (); |
|
56 |
|
57 tree_constant decision_vars; |
|
58 if (n > 1) |
|
59 { |
|
60 Matrix m (n, 1); |
|
61 for (int i = 0; i < n; i++) |
|
62 m (i, 0) = x.elem (i); |
516
|
63 decision_vars = m; |
1
|
64 } |
|
65 else |
|
66 { |
|
67 double d = x.elem (0); |
516
|
68 decision_vars = d; |
1
|
69 } |
|
70 |
565
|
71 Octave_object args; |
718
|
72 args(0) = decision_vars; |
1
|
73 |
255
|
74 static double retval; |
|
75 retval = 0.0; |
|
76 |
1
|
77 tree_constant objective_value; |
519
|
78 if (npsol_objective) |
1
|
79 { |
506
|
80 Octave_object tmp = npsol_objective->eval (0, 1, args); |
255
|
81 |
|
82 if (error_state) |
|
83 { |
|
84 error ("npsol: error evaluating objective function"); |
|
85 npsol_objective_error = 1; // XXX FIXME XXX |
|
86 return retval; |
|
87 } |
|
88 |
497
|
89 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
90 objective_value = tmp(0); |
1
|
91 else |
|
92 { |
158
|
93 error ("npsol: error evaluating objective function"); |
255
|
94 npsol_objective_error = 1; // XXX FIXME XXX |
|
95 return retval; |
1
|
96 } |
|
97 } |
|
98 |
620
|
99 if (objective_value.is_real_matrix ()) |
1
|
100 { |
620
|
101 Matrix m = objective_value.matrix_value (); |
|
102 if (m.rows () == 1 && m.columns () == 1) |
|
103 retval = m.elem (0, 0); |
|
104 else |
|
105 { |
|
106 gripe_user_returned_invalid ("npsol_objective"); |
|
107 npsol_objective_error = 1; // XXX FIXME XXX |
|
108 } |
|
109 } |
|
110 else if (objective_value.is_real_scalar ()) |
|
111 { |
1
|
112 retval = objective_value.double_value (); |
620
|
113 } |
|
114 else |
|
115 { |
1
|
116 gripe_user_returned_invalid ("npsol_objective"); |
255
|
117 npsol_objective_error = 1; // XXX FIXME XXX |
1
|
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 |
565
|
144 Octave_object args; |
718
|
145 args(0) = decision_vars; |
1
|
146 |
519
|
147 if (npsol_constraints) |
1
|
148 { |
506
|
149 Octave_object tmp = npsol_constraints->eval (0, 1, args); |
255
|
150 |
|
151 if (error_state) |
|
152 { |
|
153 error ("npsol: error evaluating constraints"); |
|
154 return retval; |
|
155 } |
|
156 |
497
|
157 if (tmp.length () > 0 && tmp(0).is_defined ()) |
1
|
158 { |
628
|
159 retval = tmp(0).vector_value (); |
255
|
160 |
636
|
161 if (error_state || retval.length () <= 0) |
255
|
162 error ("npsol: error evaluating constraints"); |
1
|
163 } |
|
164 else |
497
|
165 error ("npsol: error evaluating constraints"); |
1
|
166 } |
|
167 |
|
168 return retval; |
|
169 } |
|
170 |
|
171 int |
|
172 linear_constraints_ok (const ColumnVector& x, const ColumnVector& llb, |
|
173 const Matrix& c, const ColumnVector& lub, |
|
174 char *warn_for, int warn) |
|
175 { |
|
176 int x_len = x.capacity (); |
|
177 int llb_len = llb.capacity (); |
|
178 int lub_len = lub.capacity (); |
|
179 int c_rows = c.rows (); |
|
180 int c_cols = c.columns (); |
|
181 |
132
|
182 int ok = 1; |
|
183 if (warn) |
|
184 { |
|
185 if (c_rows == 0 || c_cols == 0 || llb_len == 0 || lub_len == 0) |
|
186 { |
|
187 ok = 0; |
158
|
188 error ("%s: linear constraints must have nonzero dimensions", |
|
189 warn_for); |
132
|
190 } |
|
191 else if (x_len != c_cols || llb_len != lub_len || llb_len != c_rows) |
|
192 { |
|
193 ok = 0; |
158
|
194 error ("%s: linear constraints have inconsistent dimensions", |
|
195 warn_for); |
132
|
196 } |
|
197 } |
1
|
198 |
|
199 return ok; |
|
200 } |
|
201 |
|
202 int |
|
203 nonlinear_constraints_ok (const ColumnVector& x, const ColumnVector& nllb, |
1537
|
204 NLFunc::nonlinear_fcn g, const ColumnVector& nlub, |
1
|
205 char *warn_for, int warn) |
|
206 { |
|
207 int nllb_len = nllb.capacity (); |
|
208 int nlub_len = nlub.capacity (); |
|
209 ColumnVector c = (*g) (x); |
|
210 int c_len = c.capacity (); |
|
211 |
132
|
212 int ok = 1; |
|
213 if (warn) |
|
214 { |
|
215 if (nllb_len == 0 || nlub_len == 0 || c_len == 0) |
|
216 { |
|
217 ok = 0; |
158
|
218 error ("%s: nonlinear constraints have nonzero dimensions", |
|
219 warn_for); |
132
|
220 } |
|
221 else if (nllb_len != nlub_len || nllb_len != c_len) |
|
222 { |
|
223 ok = 0; |
162
|
224 error ("%s: nonlinear constraints have inconsistent dimensions", |
|
225 warn_for); |
132
|
226 } |
135
|
227 } |
1
|
228 return ok; |
|
229 } |
|
230 |
646
|
231 #endif |
|
232 |
519
|
233 #if defined (NPSOL_MISSING) |
1684
|
234 DEFUN_DLD_BUILTIN ("npsol", Fnpsol, Snpsol, FSnpsol, 00, |
519
|
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 |
1684
|
239 DEFUN_DLD_BUILTIN ("npsol", Fnpsol, Snpsol, FSnpsol, 11, |
1686
|
240 "[X, OBJ, INFO, LAMBDA] = npsol (X, PHI [, LB, UB] [, A_LB, A, A_UB]\n\ |
|
241 [, G_LB, G, G_UB])\n\ |
519
|
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 |
1357
|
280 // Force a bad value of inform, and empty matrices for x, phi, and |
|
281 // lambda. |
519
|
282 |
|
283 retval.resize (4, Matrix ()); |
|
284 |
|
285 retval(2) = -1.0; |
|
286 |
|
287 print_usage ("npsol"); |
|
288 |
|
289 #else |
|
290 |
506
|
291 int nargin = args.length (); |
|
292 |
712
|
293 if (nargin < 2 || nargin == 3 || nargin == 6 || nargin == 9 |
|
294 || nargin > 10 || nargout > 4) |
519
|
295 { |
|
296 print_usage ("npsol"); |
|
297 return retval; |
|
298 } |
|
299 |
712
|
300 ColumnVector x = args(0).vector_value (); |
1
|
301 |
636
|
302 if (error_state || x.capacity () == 0) |
1
|
303 { |
158
|
304 error ("npsol: expecting vector as first argument"); |
1
|
305 return retval; |
|
306 } |
|
307 |
712
|
308 npsol_objective = is_valid_function (args(1), "npsol", 1); |
1488
|
309 if (! npsol_objective) |
1
|
310 return retval; |
|
311 |
|
312 Objective func (npsol_objective_function); |
|
313 |
|
314 ColumnVector soln; |
|
315 |
|
316 Bounds bounds; |
712
|
317 if (nargin == 4 || nargin == 7 || nargin == 10) |
1
|
318 { |
712
|
319 ColumnVector lb = args(2).vector_value (); |
|
320 ColumnVector ub = args(3).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 |
712
|
341 if (nargin == 2) |
1
|
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 |
712
|
352 if (nargin == 4) |
1
|
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; |
712
|
364 if (nargin == 5 || nargin == 7 || nargin == 8 || nargin == 10) |
945
|
365 npsol_constraints = is_valid_function (args(nargin-2), "npsol", 0); |
1
|
366 |
712
|
367 if (nargin == 7 || nargin == 5) |
1
|
368 { |
519
|
369 if (! npsol_constraints) |
1
|
370 { |
945
|
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 |
945
|
380 Matrix c = args(nargin-2).matrix_value (); |
636
|
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 |
712
|
393 if (nargin == 5) |
1
|
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 { |
1488
|
413 ColumnVector nlub = args(nargin-1).vector_value (); |
|
414 ColumnVector nllb = args(nargin-3).vector_value (); |
1
|
415 |
1488
|
416 if (error_state |
|
417 || (! nonlinear_constraints_ok |
|
418 (x, nllb, npsol_constraint_function, nlub, "npsol", 1))) |
|
419 return retval; |
1
|
420 |
1488
|
421 NLFunc const_func (npsol_constraint_function); |
|
422 NLConst nonlinear_constraints (nllb, const_func, nlub); |
1
|
423 |
1488
|
424 if (nargin == 5) |
|
425 { |
|
426 // 8. npsol (x, phi, nllb, g, nlub) |
1
|
427 |
1488
|
428 NPSOL nlp (x, func, nonlinear_constraints); |
|
429 nlp.copy (npsol_opts); |
|
430 soln = nlp.minimize (objf, inform, lambda); |
|
431 } |
|
432 else |
|
433 { |
|
434 // 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
1
|
435 |
1488
|
436 NPSOL nlp (x, func, bounds, nonlinear_constraints); |
|
437 nlp.copy (npsol_opts); |
|
438 soln = nlp.minimize (objf, inform, lambda); |
1
|
439 } |
1488
|
440 goto solved; |
1
|
441 } |
|
442 } |
|
443 |
712
|
444 if (nargin == 8 || nargin == 10) |
1
|
445 { |
519
|
446 if (! npsol_constraints) |
1
|
447 { |
|
448 // Produce error message. |
1357
|
449 |
945
|
450 is_valid_function (args(nargin-2), "npsol", 1); |
1
|
451 } |
|
452 else |
|
453 { |
1488
|
454 ColumnVector nlub = args(nargin-1).vector_value (); |
|
455 ColumnVector nllb = args(nargin-3).vector_value (); |
1
|
456 |
1488
|
457 if (error_state |
|
458 || (! nonlinear_constraints_ok |
|
459 (x, nllb, npsol_constraint_function, nlub, "npsol", 1))) |
|
460 return retval; |
|
461 |
|
462 NLFunc const_func (npsol_constraint_function); |
|
463 NLConst nonlinear_constraints (nllb, const_func, nlub); |
1
|
464 |
1488
|
465 ColumnVector lub = args(nargin-4).vector_value (); |
|
466 ColumnVector llb = args(nargin-6).vector_value (); |
1
|
467 |
1488
|
468 if (error_state || llb.capacity () == 0 || lub.capacity () == 0) |
|
469 { |
|
470 error ("npsol: bounds for linear constraints must be vectors"); |
|
471 return retval; |
|
472 } |
636
|
473 |
1488
|
474 Matrix c = args(nargin-5).matrix_value (); |
636
|
475 |
1488
|
476 if (error_state) |
|
477 { |
|
478 error ("npsol: invalid linear constraint matrix"); |
|
479 return retval; |
|
480 } |
215
|
481 |
1488
|
482 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
483 return retval; |
1
|
484 |
1488
|
485 LinConst linear_constraints (llb, c, lub); |
1
|
486 |
1488
|
487 if (nargin == 8) |
|
488 { |
|
489 // 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
1
|
490 |
1488
|
491 NPSOL nlp (x, func, linear_constraints, |
|
492 nonlinear_constraints); |
|
493 nlp.copy (npsol_opts); |
|
494 soln = nlp.minimize (objf, inform, lambda); |
|
495 } |
|
496 else |
|
497 { |
|
498 // 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
1
|
499 |
1488
|
500 NPSOL nlp (x, func, bounds, linear_constraints, |
|
501 nonlinear_constraints); |
|
502 nlp.copy (npsol_opts); |
|
503 soln = nlp.minimize (objf, inform, lambda); |
1
|
504 } |
1488
|
505 goto solved; |
1
|
506 } |
|
507 } |
|
508 |
|
509 return retval; |
|
510 |
|
511 solved: |
|
512 |
497
|
513 retval.resize (nargout ? nargout : 1); |
516
|
514 retval(0) = soln, 1; |
1
|
515 if (nargout > 1) |
516
|
516 retval(1) = objf; |
1
|
517 if (nargout > 2) |
516
|
518 retval(2) = (double) inform; |
1
|
519 if (nargout > 3) |
516
|
520 retval(3) = lambda; |
1
|
521 |
519
|
522 #endif |
|
523 |
1
|
524 return retval; |
|
525 } |
|
526 |
646
|
527 #ifndef NPSOL_MISSING |
|
528 |
287
|
529 typedef void (NPSOL_options::*d_set_opt_mf) (double); |
|
530 typedef void (NPSOL_options::*i_set_opt_mf) (int); |
|
531 typedef double (NPSOL_options::*d_get_opt_mf) (void); |
|
532 typedef int (NPSOL_options::*i_get_opt_mf) (void); |
|
533 |
|
534 #define MAX_TOKENS 5 |
|
535 |
|
536 struct NPSOL_OPTIONS |
|
537 { |
540
|
538 const char *keyword; |
|
539 const char *kw_tok[MAX_TOKENS + 1]; |
287
|
540 int min_len[MAX_TOKENS + 1]; |
|
541 int min_toks_to_match; |
|
542 d_set_opt_mf d_set_fcn; |
|
543 i_set_opt_mf i_set_fcn; |
|
544 d_get_opt_mf d_get_fcn; |
|
545 i_get_opt_mf i_get_fcn; |
|
546 }; |
|
547 |
497
|
548 static NPSOL_OPTIONS npsol_option_table [] = |
287
|
549 { |
|
550 { "central difference interval", |
519
|
551 { "central", "difference", "interval", 0, 0, 0, }, |
287
|
552 { 2, 0, 0, 0, 0, 0, }, 1, |
519
|
553 NPSOL_options::set_central_difference_interval, 0, |
|
554 NPSOL_options::central_difference_interval, 0, }, |
287
|
555 |
|
556 { "crash tolerance", |
519
|
557 { "crash", "tolerance", 0, 0, 0, 0, }, |
287
|
558 { 2, 0, 0, 0, 0, 0, }, 1, |
519
|
559 NPSOL_options::set_crash_tolerance, 0, |
|
560 NPSOL_options::crash_tolerance, 0, }, |
287
|
561 |
|
562 { "derivative level", |
519
|
563 { "derivative", "level", 0, 0, 0, 0, }, |
287
|
564 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
565 0, NPSOL_options::set_derivative_level, |
|
566 0, NPSOL_options::derivative_level, }, |
287
|
567 |
|
568 { "difference interval", |
519
|
569 { "difference", "interval", 0, 0, 0, 0, }, |
287
|
570 { 3, 0, 0, 0, 0, 0, }, 1, |
519
|
571 NPSOL_options::set_difference_interval, 0, |
|
572 NPSOL_options::difference_interval, 0, }, |
287
|
573 |
|
574 { "function precision", |
519
|
575 { "function", "precision", 0, 0, 0, 0, }, |
287
|
576 { 2, 0, 0, 0, 0, 0, }, 1, |
519
|
577 NPSOL_options::set_function_precision, 0, |
|
578 NPSOL_options::function_precision, 0, }, |
287
|
579 |
|
580 { "infinite bound size", |
519
|
581 { "infinite", "bound", "size", 0, 0, 0, }, |
287
|
582 { 1, 1, 0, 0, 0, 0, }, 2, |
519
|
583 NPSOL_options::set_infinite_bound, 0, |
|
584 NPSOL_options::infinite_bound, 0, }, |
287
|
585 |
|
586 { "infinite step size", |
519
|
587 { "infinite", "step", "size", 0, 0, 0, }, |
287
|
588 { 1, 1, 0, 0, 0, 0, }, 2, |
519
|
589 NPSOL_options::set_infinite_step, 0, |
|
590 NPSOL_options::infinite_step, 0, }, |
287
|
591 |
|
592 { "linear feasibility tolerance", |
519
|
593 { "linear", "feasibility", "tolerance", 0, 0, 0, }, |
287
|
594 { 5, 0, 0, 0, 0, 0, }, 1, |
519
|
595 NPSOL_options::set_linear_feasibility_tolerance, 0, |
|
596 NPSOL_options::linear_feasibility_tolerance, 0, }, |
287
|
597 |
|
598 { "linesearch tolerance", |
519
|
599 { "linesearch", "tolerance", 0, 0, 0, 0, }, |
287
|
600 { 5, 0, 0, 0, 0, 0, }, 1, |
519
|
601 NPSOL_options::set_linesearch_tolerance, 0, |
|
602 NPSOL_options::linesearch_tolerance, 0, }, |
287
|
603 |
|
604 { "major iteration limit", |
519
|
605 { "major", "iteration", "limit", 0, 0, 0, }, |
287
|
606 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
607 0, NPSOL_options::set_major_iteration_limit, |
|
608 0, NPSOL_options::major_iteration_limit, }, |
287
|
609 |
|
610 { "minor iteration limit", |
519
|
611 { "minor", "iteration", "limit", 0, 0, 0, }, |
287
|
612 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
613 0, NPSOL_options::set_minor_iteration_limit, |
|
614 0, NPSOL_options::minor_iteration_limit, }, |
287
|
615 |
|
616 { "major print level", |
519
|
617 { "major", "print", "level", 0, 0, 0, }, |
287
|
618 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
619 0, NPSOL_options::set_major_print_level, |
|
620 0, NPSOL_options::major_print_level, }, |
287
|
621 |
|
622 { "minor print level", |
519
|
623 { "minor", "print", "level", 0, 0, 0, }, |
287
|
624 { 2, 1, 0, 0, 0, 0, }, 2, |
519
|
625 0, NPSOL_options::set_minor_print_level, |
|
626 0, NPSOL_options::minor_print_level, }, |
287
|
627 |
|
628 { "nonlinear feasibility tolerance", |
519
|
629 { "nonlinear", "feasibility", "tolerance", 0, 0, }, |
287
|
630 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
631 NPSOL_options::set_nonlinear_feasibility_tolerance, 0, |
|
632 NPSOL_options::nonlinear_feasibility_tolerance, 0, }, |
287
|
633 |
|
634 { "optimality tolerance", |
519
|
635 { "optimality", "tolerance", 0, 0, 0, 0, }, |
287
|
636 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
637 NPSOL_options::set_optimality_tolerance, 0, |
|
638 NPSOL_options::optimality_tolerance, 0, }, |
287
|
639 |
|
640 { "start objective check at variable", |
519
|
641 { "start", "objective", "check", "at", "variable", 0, }, |
287
|
642 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
643 0, NPSOL_options::set_start_objective_check, |
|
644 0, NPSOL_options::start_objective_check, }, |
287
|
645 |
|
646 { "start constraint check at variable", |
519
|
647 { "start", "constraint", "check", "at", "variable", 0, }, |
287
|
648 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
649 0, NPSOL_options::set_start_constraint_check, |
|
650 0, NPSOL_options::start_constraint_check, }, |
287
|
651 |
|
652 { "stop objective check at variable", |
519
|
653 { "stop", "objective", "check", "at", "variable", 0, }, |
287
|
654 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
655 0, NPSOL_options::set_stop_objective_check, |
|
656 0, NPSOL_options::stop_objective_check, }, |
287
|
657 |
|
658 { "stop constraint check at variable", |
519
|
659 { "stop", "constraint", "check", "at", "variable", 0, }, |
287
|
660 { 3, 1, 0, 0, 0, 0, }, 2, |
519
|
661 0, NPSOL_options::set_stop_constraint_check, |
|
662 0, NPSOL_options::stop_constraint_check, }, |
287
|
663 |
|
664 { "verify level", |
519
|
665 { "verify", "level", 0, 0, 0, 0, }, |
287
|
666 { 1, 0, 0, 0, 0, 0, }, 1, |
519
|
667 0, NPSOL_options::set_verify_level, |
|
668 0, NPSOL_options::verify_level, }, |
287
|
669 |
519
|
670 { 0, |
|
671 { 0, 0, 0, 0, 0, 0, }, |
287
|
672 { 0, 0, 0, 0, 0, 0, }, 0, |
519
|
673 0, 0, 0, 0, }, |
287
|
674 }; |
|
675 |
|
676 static void |
|
677 print_npsol_option_list (void) |
|
678 { |
|
679 ostrstream output_buf; |
|
680 |
|
681 print_usage ("npsol_options", 1); |
|
682 |
|
683 output_buf << "\n" |
|
684 << "Options for npsol include:\n\n" |
|
685 << " keyword value\n" |
|
686 << " ------- -----\n\n"; |
|
687 |
|
688 NPSOL_OPTIONS *list = npsol_option_table; |
|
689 |
540
|
690 const char *keyword; |
519
|
691 while ((keyword = list->keyword) != 0) |
287
|
692 { |
|
693 output_buf.form (" %-40s ", keyword); |
|
694 if (list->d_get_fcn) |
|
695 { |
|
696 double val = (npsol_opts.*list->d_get_fcn) (); |
|
697 if (val < 0.0) |
|
698 output_buf << "computed automatically"; |
|
699 else |
|
700 output_buf << val; |
|
701 } |
|
702 else |
|
703 { |
|
704 int val = (npsol_opts.*list->i_get_fcn) (); |
|
705 if (val < 0) |
|
706 output_buf << "depends on problem size"; |
|
707 else |
|
708 output_buf << val; |
|
709 } |
|
710 output_buf << "\n"; |
|
711 list++; |
|
712 } |
|
713 |
|
714 output_buf << "\n" << ends; |
|
715 maybe_page_output (output_buf); |
|
716 } |
|
717 |
|
718 static void |
1363
|
719 set_npsol_option (const char *keyword, double val) |
287
|
720 { |
|
721 NPSOL_OPTIONS *list = npsol_option_table; |
|
722 |
519
|
723 while (list->keyword != 0) |
287
|
724 { |
|
725 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
726 list->min_toks_to_match, MAX_TOKENS)) |
|
727 { |
|
728 if (list->d_set_fcn) |
|
729 (npsol_opts.*list->d_set_fcn) (val); |
|
730 else |
1086
|
731 { |
|
732 if (xisnan (val)) |
|
733 { |
|
734 error ("npsol_options: %s: expecting integer, found NaN", |
|
735 keyword); |
|
736 } |
|
737 else |
|
738 (npsol_opts.*list->i_set_fcn) (NINT (val)); |
|
739 } |
287
|
740 return; |
|
741 } |
|
742 list++; |
|
743 } |
|
744 |
|
745 warning ("npsol_options: no match for `%s'", keyword); |
|
746 } |
|
747 |
1329
|
748 static Octave_object |
1363
|
749 show_npsol_option (const char *keyword) |
1329
|
750 { |
|
751 Octave_object retval; |
|
752 |
|
753 NPSOL_OPTIONS *list = npsol_option_table; |
|
754 |
|
755 while (list->keyword != 0) |
|
756 { |
|
757 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
758 list->min_toks_to_match, MAX_TOKENS)) |
|
759 { |
|
760 if (list->d_get_fcn) |
|
761 return (npsol_opts.*list->d_get_fcn) (); |
|
762 else |
|
763 return (double) (npsol_opts.*list->i_get_fcn) (); |
|
764 } |
|
765 list++; |
|
766 } |
|
767 |
|
768 warning ("npsol_options: no match for `%s'", keyword); |
|
769 |
|
770 return retval; |
|
771 } |
|
772 |
646
|
773 #endif |
|
774 |
519
|
775 #if defined (NPSOL_MISSING) |
1684
|
776 DEFUN_DLD_BUILTIN ("npsol_options", Fnpsol_options, Snpsol_options, |
|
777 FSnpsol_options, 00, |
519
|
778 "This function requires NPSOL, which is not freely\n\ |
|
779 redistributable. For more information, read the file\n\ |
|
780 libcruft/npsol/README.MISSING in the source distribution.") |
|
781 #else |
1684
|
782 DEFUN_DLD_BUILTIN ("npsol_options", Fnpsol_options, Snpsol_options, |
|
783 FSnpsol_options, 10, |
519
|
784 "npsol_options (KEYWORD, VALUE)\n\ |
|
785 \n\ |
|
786 Set or show options for npsol. Keywords may be abbreviated\n\ |
|
787 to the shortest match.") |
|
788 #endif |
272
|
789 { |
497
|
790 Octave_object retval; |
272
|
791 |
519
|
792 #if defined (NPSOL_MISSING) |
|
793 |
|
794 print_usage ("npsol_options"); |
|
795 |
|
796 #else |
|
797 |
506
|
798 int nargin = args.length (); |
|
799 |
712
|
800 if (nargin == 0) |
287
|
801 { |
|
802 print_npsol_option_list (); |
636
|
803 return retval; |
287
|
804 } |
1329
|
805 else if (nargin == 1 || nargin == 2) |
287
|
806 { |
1728
|
807 string tstr = args(0).string_value (); |
|
808 const char *keyword = tstr.c_str (); |
636
|
809 |
|
810 if (! error_state) |
287
|
811 { |
1329
|
812 if (nargin == 1) |
|
813 return show_npsol_option (keyword); |
|
814 else |
|
815 { |
|
816 double val = args(1).double_value (); |
636
|
817 |
1329
|
818 if (! error_state) |
|
819 { |
|
820 set_npsol_option (keyword, val); |
|
821 return retval; |
|
822 } |
636
|
823 } |
287
|
824 } |
|
825 } |
636
|
826 |
|
827 print_usage ("npsol_options"); |
287
|
828 |
519
|
829 #endif |
|
830 |
272
|
831 return retval; |
|
832 } |
|
833 |
1
|
834 /* |
|
835 ;;; Local Variables: *** |
|
836 ;;; mode: C++ *** |
|
837 ;;; page-delimiter: "^/\\*" *** |
|
838 ;;; End: *** |
|
839 */ |