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