2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include <iostream.h> |
|
30 |
|
31 #include "NPSOL.h" |
|
32 #include "lo-mappers.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "gripes.h" |
|
37 #include "help.h" |
|
38 #include "oct-obj.h" |
2968
|
39 #include "ov-fcn.h" |
2928
|
40 #include "pager.h" |
|
41 #include "utils.h" |
|
42 #include "variables.h" |
|
43 |
|
44 #ifndef NPSOL_MISSING |
|
45 |
|
46 // Global pointers for user defined functions required by npsol. |
2968
|
47 static octave_function *npsol_objective; |
|
48 static octave_function *npsol_constraints; |
2928
|
49 |
|
50 static NPSOL_options npsol_opts; |
|
51 |
|
52 double |
|
53 npsol_objective_function (const ColumnVector& x) |
|
54 { |
|
55 int n = x.capacity (); |
|
56 |
|
57 octave_value 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 (i); |
|
63 decision_vars = m; |
|
64 } |
|
65 else |
|
66 { |
|
67 double d = x (0); |
|
68 decision_vars = d; |
|
69 } |
|
70 |
|
71 octave_value_list args; |
|
72 args(0) = decision_vars; |
|
73 |
|
74 static double retval; |
|
75 retval = 0.0; |
|
76 |
|
77 octave_value objective_value; |
|
78 if (npsol_objective) |
|
79 { |
2968
|
80 octave_value_list tmp = npsol_objective->do_index_op (1, args); |
2928
|
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 |
|
89 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
90 objective_value = tmp(0); |
|
91 else |
|
92 { |
|
93 error ("npsol: error evaluating objective function"); |
|
94 npsol_objective_error = 1; // XXX FIXME XXX |
|
95 return retval; |
|
96 } |
|
97 } |
|
98 |
|
99 if (objective_value.is_real_matrix ()) |
|
100 { |
|
101 Matrix m = objective_value.matrix_value (); |
|
102 if (m.rows () == 1 && m.columns () == 1) |
|
103 retval = m (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 { |
|
112 retval = objective_value.double_value (); |
|
113 } |
|
114 else |
|
115 { |
|
116 gripe_user_returned_invalid ("npsol_objective"); |
|
117 npsol_objective_error = 1; // XXX FIXME XXX |
|
118 } |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
|
123 ColumnVector |
|
124 npsol_constraint_function (const ColumnVector& x) |
|
125 { |
|
126 ColumnVector retval; |
|
127 |
|
128 int n = x.capacity (); |
|
129 |
|
130 octave_value 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 (i); |
|
136 decision_vars = m; |
|
137 } |
|
138 else |
|
139 { |
|
140 double d = x (0); |
|
141 decision_vars = d; |
|
142 } |
|
143 |
|
144 octave_value_list args; |
|
145 args(0) = decision_vars; |
|
146 |
|
147 if (npsol_constraints) |
|
148 { |
2968
|
149 octave_value_list tmp = npsol_constraints->do_index_op (1, args); |
2928
|
150 |
|
151 if (error_state) |
|
152 { |
|
153 error ("npsol: error evaluating constraints"); |
|
154 return retval; |
|
155 } |
|
156 |
|
157 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
158 { |
|
159 retval = tmp(0).vector_value (); |
|
160 |
|
161 if (error_state || retval.length () <= 0) |
|
162 error ("npsol: error evaluating constraints"); |
|
163 } |
|
164 else |
|
165 error ("npsol: error evaluating constraints"); |
|
166 } |
|
167 |
|
168 return retval; |
|
169 } |
|
170 |
|
171 static 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 |
|
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; |
|
188 error ("%s: linear constraints must have nonzero dimensions", |
|
189 warn_for); |
|
190 } |
|
191 else if (x_len != c_cols || llb_len != lub_len || llb_len != c_rows) |
|
192 { |
|
193 ok = 0; |
|
194 error ("%s: linear constraints have inconsistent dimensions", |
|
195 warn_for); |
|
196 } |
|
197 } |
|
198 |
|
199 return ok; |
|
200 } |
|
201 |
|
202 static int |
|
203 nonlinear_constraints_ok (const ColumnVector& x, const ColumnVector& nllb, |
|
204 NLFunc::nonlinear_fcn g, const ColumnVector& nlub, |
|
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 |
|
212 int ok = 1; |
|
213 if (warn) |
|
214 { |
|
215 if (nllb_len == 0 || nlub_len == 0 || c_len == 0) |
|
216 { |
|
217 ok = 0; |
|
218 error ("%s: nonlinear constraints have nonzero dimensions", |
|
219 warn_for); |
|
220 } |
|
221 else if (nllb_len != nlub_len || nllb_len != c_len) |
|
222 { |
|
223 ok = 0; |
|
224 error ("%s: nonlinear constraints have inconsistent dimensions", |
|
225 warn_for); |
|
226 } |
|
227 } |
|
228 return ok; |
|
229 } |
|
230 |
|
231 #endif |
|
232 |
|
233 #if defined (NPSOL_MISSING) |
|
234 DEFUN_DLD (npsol, , , |
|
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, args, nargout, |
|
240 "[X, OBJ, INFO, LAMBDA] = npsol (X, PHI [, LB, UB] [, A_LB, A, A_UB]\n\ |
|
241 [, G_LB, G, 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\n\ |
|
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 |
|
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 |
|
276 octave_value_list retval; |
|
277 |
|
278 #if defined (NPSOL_MISSING) |
|
279 |
|
280 // Force a bad value of inform, and empty matrices for x, phi, and |
|
281 // lambda. |
|
282 |
|
283 retval.resize (4, Matrix ()); |
|
284 |
|
285 retval(2) = -1.0; |
|
286 |
|
287 print_usage ("npsol"); |
|
288 |
|
289 #else |
|
290 |
|
291 int nargin = args.length (); |
|
292 |
|
293 if (nargin < 2 || nargin == 3 || nargin == 6 || nargin == 9 |
|
294 || nargin > 10 || nargout > 4) |
|
295 { |
|
296 print_usage ("npsol"); |
|
297 return retval; |
|
298 } |
|
299 |
|
300 ColumnVector x = args(0).vector_value (); |
|
301 |
|
302 if (error_state || x.capacity () == 0) |
|
303 { |
|
304 error ("npsol: expecting vector as first argument"); |
|
305 return retval; |
|
306 } |
|
307 |
|
308 npsol_objective = extract_function |
|
309 (args(1), "npsol", "__npsol_obj__", |
|
310 "function phi = __npsol_obj__ (x) phi = ", |
|
311 "; endfunction"); |
|
312 |
|
313 if (! npsol_objective) |
|
314 return retval; |
|
315 |
|
316 Objective func (npsol_objective_function); |
|
317 |
|
318 ColumnVector soln; |
|
319 |
|
320 Bounds bounds; |
|
321 if (nargin == 4 || nargin == 7 || nargin == 10) |
|
322 { |
|
323 ColumnVector lb = args(2).vector_value (); |
|
324 ColumnVector ub = args(3).vector_value (); |
|
325 |
|
326 int lb_len = lb.capacity (); |
|
327 int ub_len = ub.capacity (); |
|
328 |
|
329 if (error_state || lb_len != ub_len || lb_len != x.capacity ()) |
|
330 { |
|
331 error ("npsol: lower and upper bounds and decision variable vector"); |
|
332 error ("must all have the same number of elements"); |
|
333 return retval; |
|
334 } |
|
335 |
|
336 bounds.resize (lb_len); |
|
337 bounds.set_lower_bounds (lb); |
|
338 bounds.set_upper_bounds (ub); |
|
339 } |
|
340 |
|
341 double objf; |
|
342 ColumnVector lambda; |
|
343 int inform; |
|
344 |
|
345 if (nargin == 2) |
|
346 { |
|
347 // 1. npsol (x, phi) |
|
348 |
|
349 NPSOL nlp (x, func); |
|
350 nlp.set_options (npsol_opts); |
|
351 soln = nlp.minimize (objf, inform, lambda); |
|
352 |
|
353 goto solved; |
|
354 } |
|
355 |
|
356 if (nargin == 4) |
|
357 { |
|
358 // 2. npsol (x, phi, lb, ub) |
|
359 |
|
360 NPSOL nlp (x, func, bounds); |
|
361 nlp.set_options (npsol_opts); |
|
362 soln = nlp.minimize (objf, inform, lambda); |
|
363 |
|
364 goto solved; |
|
365 } |
|
366 |
|
367 npsol_constraints = 0; |
|
368 if (nargin == 5 || nargin == 7 || nargin == 8 || nargin == 10) |
|
369 npsol_constraints = extract_function |
|
370 (args(nargin-2), "npsol", "__npsol_constr__", |
|
371 "function y = __npsol_constr__ (x) y = ", |
|
372 "; endfunction"); |
|
373 |
|
374 if (nargin == 7 || nargin == 5) |
|
375 { |
|
376 if (! npsol_constraints) |
|
377 { |
|
378 ColumnVector lub = args(nargin-1).vector_value (); |
|
379 ColumnVector llb = args(nargin-3).vector_value (); |
|
380 |
|
381 if (error_state || llb.capacity () == 0 || lub.capacity () == 0) |
|
382 { |
|
383 error ("npsol: bounds for linear constraints must be vectors"); |
|
384 return retval; |
|
385 } |
|
386 |
|
387 Matrix c = args(nargin-2).matrix_value (); |
|
388 |
|
389 if (error_state) |
|
390 { |
|
391 error ("npsol: invalid linear constraint matrix"); |
|
392 return retval; |
|
393 } |
|
394 |
|
395 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
396 return retval; |
|
397 |
|
398 LinConst linear_constraints (llb, c, lub); |
|
399 |
|
400 if (nargin == 5) |
|
401 { |
|
402 // 7. npsol (x, phi, llb, c, lub) |
|
403 |
|
404 NPSOL nlp (x, func, linear_constraints); |
|
405 nlp.set_options (npsol_opts); |
|
406 soln = nlp.minimize (objf, inform, lambda); |
|
407 } |
|
408 else |
|
409 { |
|
410 // 3. npsol (x, phi, lb, ub, llb, c, lub) |
|
411 |
|
412 NPSOL nlp (x, func, bounds, linear_constraints); |
|
413 nlp.set_options (npsol_opts); |
|
414 soln = nlp.minimize (objf, inform, lambda); |
|
415 } |
|
416 goto solved; |
|
417 } |
|
418 else |
|
419 { |
|
420 ColumnVector nlub = args(nargin-1).vector_value (); |
|
421 ColumnVector nllb = args(nargin-3).vector_value (); |
|
422 |
|
423 if (error_state |
|
424 || (! nonlinear_constraints_ok |
|
425 (x, nllb, npsol_constraint_function, nlub, "npsol", 1))) |
|
426 return retval; |
|
427 |
|
428 NLFunc const_func (npsol_constraint_function); |
|
429 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
430 |
|
431 if (nargin == 5) |
|
432 { |
|
433 // 8. npsol (x, phi, nllb, g, nlub) |
|
434 |
|
435 NPSOL nlp (x, func, nonlinear_constraints); |
|
436 nlp.set_options (npsol_opts); |
|
437 soln = nlp.minimize (objf, inform, lambda); |
|
438 } |
|
439 else |
|
440 { |
|
441 // 5. npsol (x, phi, lb, ub, nllb, g, nlub) |
|
442 |
|
443 NPSOL nlp (x, func, bounds, nonlinear_constraints); |
|
444 nlp.set_options (npsol_opts); |
|
445 soln = nlp.minimize (objf, inform, lambda); |
|
446 } |
|
447 goto solved; |
|
448 } |
|
449 } |
|
450 |
|
451 if (nargin == 8 || nargin == 10) |
|
452 { |
|
453 if (! npsol_constraints) |
|
454 { |
|
455 // Produce error message. |
|
456 |
|
457 is_valid_function (args(nargin-2), "npsol", 1); |
|
458 } |
|
459 else |
|
460 { |
|
461 ColumnVector nlub = args(nargin-1).vector_value (); |
|
462 ColumnVector nllb = args(nargin-3).vector_value (); |
|
463 |
|
464 if (error_state |
|
465 || (! nonlinear_constraints_ok |
|
466 (x, nllb, npsol_constraint_function, nlub, "npsol", 1))) |
|
467 return retval; |
|
468 |
|
469 NLFunc const_func (npsol_constraint_function); |
|
470 NLConst nonlinear_constraints (nllb, const_func, nlub); |
|
471 |
|
472 ColumnVector lub = args(nargin-4).vector_value (); |
|
473 ColumnVector llb = args(nargin-6).vector_value (); |
|
474 |
|
475 if (error_state || llb.capacity () == 0 || lub.capacity () == 0) |
|
476 { |
|
477 error ("npsol: bounds for linear constraints must be vectors"); |
|
478 return retval; |
|
479 } |
|
480 |
|
481 Matrix c = args(nargin-5).matrix_value (); |
|
482 |
|
483 if (error_state) |
|
484 { |
|
485 error ("npsol: invalid linear constraint matrix"); |
|
486 return retval; |
|
487 } |
|
488 |
|
489 if (! linear_constraints_ok (x, llb, c, lub, "npsol", 1)) |
|
490 return retval; |
|
491 |
|
492 LinConst linear_constraints (llb, c, lub); |
|
493 |
|
494 if (nargin == 8) |
|
495 { |
|
496 // 6. npsol (x, phi, llb, c, lub, nllb, g, nlub) |
|
497 |
|
498 NPSOL nlp (x, func, linear_constraints, |
|
499 nonlinear_constraints); |
|
500 nlp.set_options (npsol_opts); |
|
501 soln = nlp.minimize (objf, inform, lambda); |
|
502 } |
|
503 else |
|
504 { |
|
505 // 4. npsol (x, phi, lb, ub, llb, c, lub, nllb, g, nlub) |
|
506 |
|
507 NPSOL nlp (x, func, bounds, linear_constraints, |
|
508 nonlinear_constraints); |
|
509 nlp.set_options (npsol_opts); |
|
510 soln = nlp.minimize (objf, inform, lambda); |
|
511 } |
|
512 goto solved; |
|
513 } |
|
514 } |
|
515 |
|
516 return retval; |
|
517 |
|
518 solved: |
|
519 |
|
520 retval.resize (nargout ? nargout : 1); |
|
521 retval(0) = soln, 1; |
|
522 if (nargout > 1) |
|
523 retval(1) = objf; |
|
524 if (nargout > 2) |
|
525 retval(2) = static_cast<double> (inform); |
|
526 if (nargout > 3) |
|
527 retval(3) = lambda; |
|
528 |
|
529 #endif |
|
530 |
|
531 return retval; |
|
532 } |
|
533 |
|
534 #ifndef NPSOL_MISSING |
|
535 |
|
536 typedef void (NPSOL_options::*d_set_opt_mf) (double); |
|
537 typedef void (NPSOL_options::*i_set_opt_mf) (int); |
|
538 typedef double (NPSOL_options::*d_get_opt_mf) (void); |
|
539 typedef int (NPSOL_options::*i_get_opt_mf) (void); |
|
540 |
|
541 #define MAX_TOKENS 5 |
|
542 |
|
543 struct NPSOL_OPTIONS |
|
544 { |
|
545 const char *keyword; |
|
546 const char *kw_tok[MAX_TOKENS + 1]; |
|
547 int min_len[MAX_TOKENS + 1]; |
|
548 int min_toks_to_match; |
|
549 d_set_opt_mf d_set_fcn; |
|
550 i_set_opt_mf i_set_fcn; |
|
551 d_get_opt_mf d_get_fcn; |
|
552 i_get_opt_mf i_get_fcn; |
|
553 }; |
|
554 |
|
555 static NPSOL_OPTIONS npsol_option_table [] = |
|
556 { |
|
557 { "central difference interval", |
|
558 { "central", "difference", "interval", 0, 0, 0, }, |
|
559 { 2, 0, 0, 0, 0, 0, }, 1, |
|
560 NPSOL_options::set_central_difference_interval, 0, |
|
561 NPSOL_options::central_difference_interval, 0, }, |
|
562 |
|
563 { "crash tolerance", |
|
564 { "crash", "tolerance", 0, 0, 0, 0, }, |
|
565 { 2, 0, 0, 0, 0, 0, }, 1, |
|
566 NPSOL_options::set_crash_tolerance, 0, |
|
567 NPSOL_options::crash_tolerance, 0, }, |
|
568 |
|
569 { "derivative level", |
|
570 { "derivative", "level", 0, 0, 0, 0, }, |
|
571 { 1, 0, 0, 0, 0, 0, }, 1, |
|
572 0, NPSOL_options::set_derivative_level, |
|
573 0, NPSOL_options::derivative_level, }, |
|
574 |
|
575 { "difference interval", |
|
576 { "difference", "interval", 0, 0, 0, 0, }, |
|
577 { 3, 0, 0, 0, 0, 0, }, 1, |
|
578 NPSOL_options::set_difference_interval, 0, |
|
579 NPSOL_options::difference_interval, 0, }, |
|
580 |
|
581 { "function precision", |
|
582 { "function", "precision", 0, 0, 0, 0, }, |
|
583 { 2, 0, 0, 0, 0, 0, }, 1, |
|
584 NPSOL_options::set_function_precision, 0, |
|
585 NPSOL_options::function_precision, 0, }, |
|
586 |
|
587 { "infinite bound size", |
|
588 { "infinite", "bound", "size", 0, 0, 0, }, |
|
589 { 1, 1, 0, 0, 0, 0, }, 2, |
|
590 NPSOL_options::set_infinite_bound, 0, |
|
591 NPSOL_options::infinite_bound, 0, }, |
|
592 |
|
593 { "infinite step size", |
|
594 { "infinite", "step", "size", 0, 0, 0, }, |
|
595 { 1, 1, 0, 0, 0, 0, }, 2, |
|
596 NPSOL_options::set_infinite_step, 0, |
|
597 NPSOL_options::infinite_step, 0, }, |
|
598 |
|
599 { "linear feasibility tolerance", |
|
600 { "linear", "feasibility", "tolerance", 0, 0, 0, }, |
|
601 { 5, 0, 0, 0, 0, 0, }, 1, |
|
602 NPSOL_options::set_linear_feasibility_tolerance, 0, |
|
603 NPSOL_options::linear_feasibility_tolerance, 0, }, |
|
604 |
|
605 { "linesearch tolerance", |
|
606 { "linesearch", "tolerance", 0, 0, 0, 0, }, |
|
607 { 5, 0, 0, 0, 0, 0, }, 1, |
|
608 NPSOL_options::set_linesearch_tolerance, 0, |
|
609 NPSOL_options::linesearch_tolerance, 0, }, |
|
610 |
|
611 { "major iteration limit", |
|
612 { "major", "iteration", "limit", 0, 0, 0, }, |
|
613 { 2, 1, 0, 0, 0, 0, }, 2, |
|
614 0, NPSOL_options::set_major_iteration_limit, |
|
615 0, NPSOL_options::major_iteration_limit, }, |
|
616 |
|
617 { "minor iteration limit", |
|
618 { "minor", "iteration", "limit", 0, 0, 0, }, |
|
619 { 2, 1, 0, 0, 0, 0, }, 2, |
|
620 0, NPSOL_options::set_minor_iteration_limit, |
|
621 0, NPSOL_options::minor_iteration_limit, }, |
|
622 |
|
623 { "major print level", |
|
624 { "major", "print", "level", 0, 0, 0, }, |
|
625 { 2, 1, 0, 0, 0, 0, }, 2, |
|
626 0, NPSOL_options::set_major_print_level, |
|
627 0, NPSOL_options::major_print_level, }, |
|
628 |
|
629 { "minor print level", |
|
630 { "minor", "print", "level", 0, 0, 0, }, |
|
631 { 2, 1, 0, 0, 0, 0, }, 2, |
|
632 0, NPSOL_options::set_minor_print_level, |
|
633 0, NPSOL_options::minor_print_level, }, |
|
634 |
|
635 { "nonlinear feasibility tolerance", |
|
636 { "nonlinear", "feasibility", "tolerance", 0, 0, }, |
|
637 { 1, 0, 0, 0, 0, 0, }, 1, |
|
638 NPSOL_options::set_nonlinear_feasibility_tolerance, 0, |
|
639 NPSOL_options::nonlinear_feasibility_tolerance, 0, }, |
|
640 |
|
641 { "optimality tolerance", |
|
642 { "optimality", "tolerance", 0, 0, 0, 0, }, |
|
643 { 1, 0, 0, 0, 0, 0, }, 1, |
|
644 NPSOL_options::set_optimality_tolerance, 0, |
|
645 NPSOL_options::optimality_tolerance, 0, }, |
|
646 |
|
647 { "start objective check at variable", |
|
648 { "start", "objective", "check", "at", "variable", 0, }, |
|
649 { 3, 1, 0, 0, 0, 0, }, 2, |
|
650 0, NPSOL_options::set_start_objective_check, |
|
651 0, NPSOL_options::start_objective_check, }, |
|
652 |
|
653 { "start constraint check at variable", |
|
654 { "start", "constraint", "check", "at", "variable", 0, }, |
|
655 { 3, 1, 0, 0, 0, 0, }, 2, |
|
656 0, NPSOL_options::set_start_constraint_check, |
|
657 0, NPSOL_options::start_constraint_check, }, |
|
658 |
|
659 { "stop objective check at variable", |
|
660 { "stop", "objective", "check", "at", "variable", 0, }, |
|
661 { 3, 1, 0, 0, 0, 0, }, 2, |
|
662 0, NPSOL_options::set_stop_objective_check, |
|
663 0, NPSOL_options::stop_objective_check, }, |
|
664 |
|
665 { "stop constraint check at variable", |
|
666 { "stop", "constraint", "check", "at", "variable", 0, }, |
|
667 { 3, 1, 0, 0, 0, 0, }, 2, |
|
668 0, NPSOL_options::set_stop_constraint_check, |
|
669 0, NPSOL_options::stop_constraint_check, }, |
|
670 |
|
671 { "verify level", |
|
672 { "verify", "level", 0, 0, 0, 0, }, |
|
673 { 1, 0, 0, 0, 0, 0, }, 1, |
|
674 0, NPSOL_options::set_verify_level, |
|
675 0, NPSOL_options::verify_level, }, |
|
676 |
|
677 { 0, |
|
678 { 0, 0, 0, 0, 0, 0, }, |
|
679 { 0, 0, 0, 0, 0, 0, }, 0, |
|
680 0, 0, 0, 0, }, |
|
681 }; |
|
682 |
|
683 static void |
|
684 print_npsol_option_list (ostream& os) |
|
685 { |
|
686 print_usage ("npsol_options", 1); |
|
687 |
|
688 os << "\n" |
|
689 << "Options for npsol include:\n\n" |
|
690 << " keyword value\n" |
|
691 << " ------- -----\n\n"; |
|
692 |
|
693 NPSOL_OPTIONS *list = npsol_option_table; |
|
694 |
|
695 const char *keyword; |
|
696 while ((keyword = list->keyword) != 0) |
|
697 { |
|
698 os.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 os << "computed automatically"; |
|
704 else |
|
705 os << val; |
|
706 } |
|
707 else |
|
708 { |
|
709 int val = (npsol_opts.*list->i_get_fcn) (); |
|
710 if (val < 0) |
|
711 os << "depends on problem size"; |
|
712 else |
|
713 os << val; |
|
714 } |
|
715 os << "\n"; |
|
716 list++; |
|
717 } |
|
718 |
|
719 os << "\n"; |
|
720 } |
|
721 |
|
722 static void |
|
723 set_npsol_option (const string& keyword, double val) |
|
724 { |
|
725 NPSOL_OPTIONS *list = npsol_option_table; |
|
726 |
|
727 while (list->keyword != 0) |
|
728 { |
|
729 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
730 list->min_toks_to_match, MAX_TOKENS)) |
|
731 { |
|
732 if (list->d_set_fcn) |
|
733 (npsol_opts.*list->d_set_fcn) (val); |
|
734 else |
|
735 { |
|
736 if (xisnan (val)) |
|
737 { |
|
738 error ("npsol_options: %s: expecting integer, found NaN", |
|
739 keyword.c_str ()); |
|
740 } |
|
741 else |
|
742 (npsol_opts.*list->i_set_fcn) (NINT (val)); |
|
743 } |
|
744 return; |
|
745 } |
|
746 list++; |
|
747 } |
|
748 |
|
749 warning ("npsol_options: no match for `%s'", keyword.c_str ()); |
|
750 } |
|
751 |
|
752 static octave_value_list |
|
753 show_npsol_option (const string& keyword) |
|
754 { |
|
755 octave_value retval; |
|
756 |
|
757 NPSOL_OPTIONS *list = npsol_option_table; |
|
758 |
|
759 while (list->keyword != 0) |
|
760 { |
|
761 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
762 list->min_toks_to_match, MAX_TOKENS)) |
|
763 { |
|
764 if (list->d_get_fcn) |
|
765 { |
|
766 double val = (npsol_opts.*list->d_get_fcn) (); |
|
767 if (val < 0.0) |
|
768 retval = "computed automatically"; |
|
769 else |
|
770 retval = val; |
|
771 } |
|
772 else |
|
773 { |
|
774 int val = (npsol_opts.*list->i_get_fcn) (); |
|
775 if (val < 0) |
|
776 retval = "depends on problem size"; |
|
777 else |
|
778 retval = static_cast<double> (val); |
|
779 } |
|
780 |
|
781 return retval; |
|
782 } |
|
783 list++; |
|
784 } |
|
785 |
|
786 warning ("npsol_options: no match for `%s'", keyword.c_str ()); |
|
787 |
|
788 return retval; |
|
789 } |
|
790 |
|
791 #endif |
|
792 |
|
793 #if defined (NPSOL_MISSING) |
|
794 DEFUN_DLD (npsol_options, , , |
|
795 "This function requires NPSOL, which is not freely\n\ |
|
796 redistributable. For more information, read the file\n\ |
|
797 libcruft/npsol/README.MISSING in the source distribution.") |
|
798 #else |
|
799 DEFUN_DLD (npsol_options, args, , |
|
800 "npsol_options (KEYWORD, VALUE)\n\ |
|
801 \n\ |
|
802 Set or show options for npsol. Keywords may be abbreviated\n\ |
|
803 to the shortest match.") |
|
804 #endif |
|
805 { |
|
806 octave_value_list retval; |
|
807 |
|
808 #if defined (NPSOL_MISSING) |
|
809 |
|
810 print_usage ("npsol_options"); |
|
811 |
|
812 #else |
|
813 |
|
814 int nargin = args.length (); |
|
815 |
|
816 if (nargin == 0) |
|
817 { |
|
818 print_npsol_option_list (octave_stdout); |
|
819 return retval; |
|
820 } |
|
821 else if (nargin == 1 || nargin == 2) |
|
822 { |
|
823 string keyword = args(0).string_value (); |
|
824 |
|
825 if (! error_state) |
|
826 { |
|
827 if (nargin == 1) |
|
828 return show_npsol_option (keyword); |
|
829 else |
|
830 { |
|
831 double val = args(1).double_value (); |
|
832 |
|
833 if (! error_state) |
|
834 { |
|
835 set_npsol_option (keyword, val); |
|
836 return retval; |
|
837 } |
|
838 } |
|
839 } |
|
840 } |
|
841 |
|
842 print_usage ("npsol_options"); |
|
843 |
|
844 #endif |
|
845 |
|
846 return retval; |
|
847 } |
|
848 |
|
849 /* |
|
850 ;;; Local Variables: *** |
|
851 ;;; mode: C++ *** |
|
852 ;;; End: *** |
|
853 */ |