7
|
1 // f-qpsol.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 |
289
|
30 #include <strstream.h> |
|
31 |
1
|
32 #include "QPSOL.h" |
|
33 |
1352
|
34 #include "defun-dld.h" |
1
|
35 #include "error.h" |
1352
|
36 #include "gripes.h" |
|
37 #include "help.h" |
289
|
38 #include "pager.h" |
1352
|
39 #include "tree-const.h" |
|
40 #include "utils.h" |
|
41 #include "variables.h" |
1
|
42 |
646
|
43 #ifndef QPSOL_MISSING |
|
44 |
1
|
45 // This should probably be defined in some shared file and declared in |
|
46 // a header file... |
|
47 extern int linear_constraints_ok (const ColumnVector& x, |
|
48 const ColumnVector& llb, const Matrix& c, |
|
49 const ColumnVector& lub, char *warn_for, |
|
50 int warn); |
|
51 |
289
|
52 static QPSOL_options qpsol_opts; |
|
53 |
646
|
54 #endif |
|
55 |
519
|
56 #if defined (QPSOL_MISSING) |
1684
|
57 DEFUN_DLD_BUILTIN ("qpsol", Fqpsol, Sqpsol, FSqpsol, 00, |
519
|
58 "This function requires QPSOL, which is not freely\n\ |
|
59 redistributable. For more information, read the file\n\ |
|
60 libcruft/qpsol/README.MISSING in the source distribution.") |
|
61 #else |
1684
|
62 DEFUN_DLD_BUILTIN ("qpsol", Fqpsol, Sqpsol, FSqpsol, 11, |
1686
|
63 "[X, OBJ, INFO, LAMBDA] = qpsol (X, H, C [, LB, UB] [, A_LB, A, A_UB])\n\ |
519
|
64 \n\ |
|
65 Groups of arguments surrounded in `[]' are optional, but\n\ |
|
66 must appear in the same relative order shown above.") |
|
67 #endif |
1
|
68 { |
|
69 /* |
|
70 |
|
71 Handle all of the following: |
|
72 |
|
73 1. qpsol (x, H, c) |
|
74 2. qpsol (x, H, c, lb, ub) |
|
75 3. qpsol (x, H, c, lb, ub, llb, A, lub) |
|
76 4. qpsol (x, H, c, llb, A, lub) |
|
77 |
|
78 */ |
|
79 |
497
|
80 Octave_object retval; |
1
|
81 |
519
|
82 #if defined (QPSOL_MISSING) |
|
83 |
1357
|
84 // Force a bad value of inform, and empty matrices for x, phi, and |
|
85 // lambda. |
519
|
86 |
|
87 retval.resize (4, Matrix ()); |
|
88 |
|
89 retval(2) = -1.0; |
|
90 |
|
91 print_usage ("qpsol"); |
|
92 |
|
93 #else |
|
94 |
506
|
95 int nargin = args.length (); |
|
96 |
712
|
97 if (nargin < 3 || nargin == 4 || nargin == 7 || nargin > 8 |
519
|
98 || nargout > 4) |
|
99 { |
|
100 print_usage ("qpsol"); |
|
101 return retval; |
|
102 } |
|
103 |
712
|
104 ColumnVector x = args(0).vector_value (); |
636
|
105 |
|
106 if (error_state || x.capacity () == 0) |
1
|
107 { |
157
|
108 error ("qpsol: expecting vector as first argument"); |
1
|
109 return retval; |
|
110 } |
|
111 |
712
|
112 Matrix H = args(1).matrix_value (); |
636
|
113 |
|
114 if (error_state || H.rows () != H.columns () || H.rows () != x.capacity ()) |
1
|
115 { |
213
|
116 error ("qpsol: H must be a square matrix consistent with the size of x"); |
1
|
117 return retval; |
|
118 } |
|
119 |
712
|
120 ColumnVector c = args(2).vector_value (); |
636
|
121 |
|
122 if (error_state || c.capacity () != x.capacity ()) |
1
|
123 { |
157
|
124 error ("qpsol: c must be a vector the same size as x"); |
1
|
125 return retval; |
|
126 } |
|
127 |
|
128 Bounds bounds; |
712
|
129 if (nargin == 5 || nargin == 8) |
1
|
130 { |
712
|
131 ColumnVector lb = args(3).vector_value (); |
|
132 ColumnVector ub = args(4).vector_value (); |
1
|
133 |
|
134 int lb_len = lb.capacity (); |
|
135 int ub_len = ub.capacity (); |
636
|
136 |
|
137 if (error_state || lb_len != ub_len || lb_len != x.capacity ()) |
1
|
138 { |
213
|
139 error ("qpsol: lower and upper bounds and decision variable vector"); |
|
140 error ("must all have the same number of elements"); |
1
|
141 return retval; |
|
142 } |
|
143 |
|
144 bounds.resize (lb_len); |
|
145 bounds.set_lower_bounds (lb); |
|
146 bounds.set_upper_bounds (ub); |
|
147 } |
|
148 |
|
149 ColumnVector soln; |
|
150 double objf; |
|
151 ColumnVector lambda; |
|
152 int inform; |
|
153 |
712
|
154 if (nargin == 3) |
1
|
155 { |
|
156 // 1. qpsol (x, H, c) |
|
157 |
|
158 QPSOL qp (x, H, c); |
289
|
159 qp.copy (qpsol_opts); |
1
|
160 soln = qp.minimize (objf, inform, lambda); |
|
161 |
|
162 goto solved; |
|
163 } |
|
164 |
712
|
165 if (nargin == 5) |
1
|
166 { |
|
167 // 2. qpsol (x, H, c, lb, ub) |
|
168 |
|
169 QPSOL qp (x, H, c, bounds); |
289
|
170 qp.copy (qpsol_opts); |
1
|
171 soln = qp.minimize (objf, inform, lambda); |
|
172 |
|
173 goto solved; |
|
174 } |
|
175 |
712
|
176 if (nargin == 6 || nargin == 8) |
1
|
177 { |
945
|
178 ColumnVector lub = args(nargin-1).vector_value (); |
|
179 ColumnVector llb = args(nargin-3).vector_value (); |
1
|
180 |
636
|
181 if (error_state || llb.capacity () == 0 || lub.capacity () == 0) |
157
|
182 { |
|
183 error ("qpsol: bounds for linear constraints must be vectors"); |
|
184 return retval; |
|
185 } |
|
186 |
945
|
187 Matrix A = args(nargin-2).matrix_value (); |
636
|
188 |
|
189 if (error_state) |
|
190 { |
|
191 error ("qpsol: invalid linear constraint matrix"); |
|
192 return retval; |
|
193 } |
|
194 |
1
|
195 if (! linear_constraints_ok (x, llb, A, lub, "qpsol", 1)) |
|
196 return retval; |
|
197 |
156
|
198 LinConst linear_constraints (llb, A, lub); |
|
199 |
712
|
200 if (nargin == 8) |
1
|
201 { |
|
202 // 3. qpsol (x, H, c, lb, ub, llb, A, lub) |
|
203 |
|
204 QPSOL qp (x, H, c, bounds, linear_constraints); |
289
|
205 qp.copy (qpsol_opts); |
1
|
206 soln = qp.minimize (objf, inform, lambda); |
|
207 } |
|
208 else |
|
209 { |
|
210 // 4. qpsol (x, H, c, llb, A, lub) |
|
211 |
|
212 QPSOL qp (x, H, c, linear_constraints); |
289
|
213 qp.copy (qpsol_opts); |
1
|
214 soln = qp.minimize (objf, inform, lambda); |
|
215 } |
|
216 goto solved; |
|
217 } |
|
218 |
|
219 return retval; |
|
220 |
|
221 solved: |
|
222 |
497
|
223 retval.resize (nargout ? nargout : 1); |
516
|
224 retval(0) = soln, 1; |
1
|
225 if (nargout > 1) |
516
|
226 retval(1) = objf; |
1
|
227 if (nargout > 2) |
516
|
228 retval(2) = (double) inform; |
1
|
229 if (nargout > 3) |
516
|
230 retval(3) = lambda; |
1
|
231 |
519
|
232 #endif |
|
233 |
1
|
234 return retval; |
|
235 } |
|
236 |
646
|
237 #ifndef QPSOL_MISSING |
|
238 |
289
|
239 typedef void (QPSOL_options::*d_set_opt_mf) (double); |
|
240 typedef void (QPSOL_options::*i_set_opt_mf) (int); |
|
241 typedef double (QPSOL_options::*d_get_opt_mf) (void); |
|
242 typedef int (QPSOL_options::*i_get_opt_mf) (void); |
|
243 |
|
244 #define MAX_TOKENS 2 |
|
245 |
|
246 struct QPSOL_OPTIONS |
|
247 { |
540
|
248 const char *keyword; |
|
249 const char *kw_tok[MAX_TOKENS + 1]; |
289
|
250 int min_len[MAX_TOKENS + 1]; |
|
251 int min_toks_to_match; |
|
252 d_set_opt_mf d_set_fcn; |
|
253 i_set_opt_mf i_set_fcn; |
|
254 d_get_opt_mf d_get_fcn; |
|
255 i_get_opt_mf i_get_fcn; |
|
256 }; |
|
257 |
497
|
258 static QPSOL_OPTIONS qpsol_option_table [] = |
289
|
259 { |
|
260 { "feasibility tolerance", |
519
|
261 { "feasibility", "tolerance", 0, }, |
289
|
262 { 1, 0, 0, }, 1, |
519
|
263 QPSOL_options::set_feasibility_tolerance, 0, |
|
264 QPSOL_options::feasibility_tolerance, 0, }, |
289
|
265 |
|
266 { "infinite bound", |
519
|
267 { "infinite", "bound", 0, }, |
289
|
268 { 2, 0, 0, }, 1, |
519
|
269 QPSOL_options::set_infinite_bound, 0, |
|
270 QPSOL_options::infinite_bound, 0, }, |
289
|
271 |
|
272 { "iteration limit", |
519
|
273 { "iteration", "limit", 0, }, |
289
|
274 { 2, 0, 0, }, 1, |
519
|
275 0, QPSOL_options::set_iteration_limit, |
|
276 0, QPSOL_options::iteration_limit, }, |
289
|
277 |
|
278 { "print level", |
519
|
279 { "print", "level", 0, }, |
289
|
280 { 1, 0, 0, }, 1, |
519
|
281 0, QPSOL_options::set_print_level, |
|
282 0, QPSOL_options::print_level, }, |
289
|
283 |
519
|
284 { 0, |
|
285 { 0, 0, 0, }, |
289
|
286 { 0, 0, 0, }, 0, |
519
|
287 0, 0, 0, 0, }, |
289
|
288 }; |
|
289 |
|
290 static void |
|
291 print_qpsol_option_list (void) |
|
292 { |
|
293 ostrstream output_buf; |
|
294 |
|
295 print_usage ("qpsol_options", 1); |
|
296 |
|
297 output_buf << "\n" |
|
298 << "Options for qpsol include:\n\n" |
|
299 << " keyword value\n" |
|
300 << " ------- -----\n\n"; |
|
301 |
|
302 QPSOL_OPTIONS *list = qpsol_option_table; |
|
303 |
540
|
304 const char *keyword; |
519
|
305 while ((keyword = list->keyword) != 0) |
289
|
306 { |
|
307 output_buf.form (" %-40s ", keyword); |
|
308 if (list->d_get_fcn) |
|
309 { |
|
310 double val = (qpsol_opts.*list->d_get_fcn) (); |
|
311 if (val < 0.0) |
|
312 output_buf << "computed automatically"; |
|
313 else |
|
314 output_buf << val; |
|
315 } |
|
316 else |
|
317 { |
|
318 int val = (qpsol_opts.*list->i_get_fcn) (); |
|
319 if (val < 0) |
|
320 output_buf << "depends on problem size"; |
|
321 else |
|
322 output_buf << val; |
|
323 } |
|
324 output_buf << "\n"; |
|
325 list++; |
|
326 } |
|
327 |
|
328 output_buf << "\n" << ends; |
|
329 maybe_page_output (output_buf); |
|
330 } |
|
331 |
|
332 static void |
1363
|
333 set_qpsol_option (const char *keyword, double val) |
289
|
334 { |
|
335 QPSOL_OPTIONS *list = qpsol_option_table; |
|
336 |
519
|
337 while (list->keyword != 0) |
289
|
338 { |
|
339 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
340 list->min_toks_to_match, MAX_TOKENS)) |
|
341 { |
|
342 if (list->d_set_fcn) |
|
343 (qpsol_opts.*list->d_set_fcn) (val); |
|
344 else |
1086
|
345 { |
|
346 if (xisnan (val)) |
|
347 { |
|
348 error ("qpsol_options: %s: expecting integer, found NaN", |
|
349 keyword); |
|
350 } |
|
351 else |
|
352 (qpsol_opts.*list->i_set_fcn) (NINT (val)); |
|
353 } |
289
|
354 return; |
|
355 } |
|
356 list++; |
|
357 } |
|
358 |
|
359 warning ("qpsol_options: no match for `%s'", keyword); |
|
360 } |
|
361 |
1329
|
362 static Octave_object |
1363
|
363 show_qpsol_option (const char *keyword) |
1329
|
364 { |
|
365 Octave_object retval; |
|
366 |
|
367 QPSOL_OPTIONS *list = qpsol_option_table; |
|
368 |
|
369 while (list->keyword != 0) |
|
370 { |
|
371 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
372 list->min_toks_to_match, MAX_TOKENS)) |
|
373 { |
|
374 if (list->d_get_fcn) |
|
375 return (qpsol_opts.*list->d_get_fcn) (); |
|
376 else |
|
377 return (double) (qpsol_opts.*list->i_get_fcn) (); |
|
378 } |
|
379 list++; |
|
380 } |
|
381 |
|
382 warning ("qpsol_options: no match for `%s'", keyword); |
|
383 |
|
384 return retval; |
|
385 } |
|
386 |
646
|
387 #endif |
|
388 |
519
|
389 #if defined (QPSOL_MISSING) |
1684
|
390 DEFUN_DLD_BUILTIN ("qpsol_options", Fqpsol_options, Sqpsol_options, |
|
391 FSqpsol_options, 00, |
519
|
392 "This function requires QPSOL, which is not freely\n\ |
|
393 redistributable. For more information, read the file\n\ |
|
394 libcruft/qpsol/README.MISSING in the source distribution.") |
|
395 #else |
1684
|
396 DEFUN_DLD_BUILTIN ("qpsol_options", Fqpsol_options, Sqpsol_options, |
|
397 FSqpsol_options, 10, |
519
|
398 "qpsol_options (KEYWORD, VALUE)\n |
|
399 \n\ |
|
400 Set or show options for qpsol. Keywords may be abbreviated\n\ |
|
401 to the shortest match.") |
|
402 #endif |
272
|
403 { |
497
|
404 Octave_object retval; |
272
|
405 |
519
|
406 #if defined (QPSOL_MISSING) |
|
407 |
|
408 print_usage ("qpsol"); |
|
409 |
|
410 #else |
|
411 |
506
|
412 int nargin = args.length (); |
|
413 |
712
|
414 if (nargin == 0) |
289
|
415 { |
|
416 print_qpsol_option_list (); |
636
|
417 return retval; |
289
|
418 } |
1329
|
419 else if (nargin == 1 || nargin == 2) |
289
|
420 { |
1728
|
421 string tstr = args(0).string_value (); |
|
422 const char *keyword = tstr.c_str (); |
636
|
423 |
|
424 if (! error_state) |
289
|
425 { |
1329
|
426 if (nargin == 1) |
|
427 return show_qpsol_option (keyword); |
|
428 else |
|
429 { |
|
430 double val = args(1).double_value (); |
636
|
431 |
1329
|
432 if (! error_state) |
|
433 { |
|
434 set_qpsol_option (keyword, val); |
|
435 return retval; |
|
436 } |
636
|
437 } |
289
|
438 } |
|
439 } |
636
|
440 |
|
441 print_usage ("qpsol_options"); |
289
|
442 |
519
|
443 #endif |
|
444 |
272
|
445 return retval; |
|
446 } |
|
447 |
1
|
448 /* |
|
449 ;;; Local Variables: *** |
|
450 ;;; mode: C++ *** |
|
451 ;;; page-delimiter: "^/\\*" *** |
|
452 ;;; End: *** |
|
453 */ |