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