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