7
|
1 // f-qpsol.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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" |
7
|
40 #include "f-qpsol.h" |
1
|
41 |
|
42 // This should probably be defined in some shared file and declared in |
|
43 // a header file... |
|
44 extern int linear_constraints_ok (const ColumnVector& x, |
|
45 const ColumnVector& llb, const Matrix& c, |
|
46 const ColumnVector& lub, char *warn_for, |
|
47 int warn); |
|
48 |
|
49 #ifdef WITH_DLD |
|
50 tree_constant * |
162
|
51 builtin_qpsol_2 (const tree_constant *args, int nargin, int nargout) |
1
|
52 { |
|
53 return qpsol (args, nargin, nargout); |
|
54 } |
272
|
55 |
|
56 tree_constant * |
|
57 builtin_qpsol_options_2 (const tree_constant *args, int nargin, int nargout) |
|
58 { |
|
59 return qpsol_options (args, nargin, nargout); |
|
60 } |
1
|
61 #endif |
|
62 |
289
|
63 static QPSOL_options qpsol_opts; |
|
64 |
1
|
65 tree_constant * |
162
|
66 qpsol (const tree_constant *args, int nargin, int nargout) |
1
|
67 { |
|
68 /* |
|
69 |
|
70 Handle all of the following: |
|
71 |
|
72 1. qpsol (x, H, c) |
|
73 2. qpsol (x, H, c, lb, ub) |
|
74 3. qpsol (x, H, c, lb, ub, llb, A, lub) |
|
75 4. qpsol (x, H, c, llb, A, lub) |
|
76 |
|
77 */ |
|
78 |
|
79 // Assumes that we have been given the correct number of arguments. |
|
80 |
|
81 tree_constant *retval = NULL_TREE_CONST; |
|
82 |
|
83 ColumnVector x = args[1].to_vector (); |
|
84 if (x.capacity () == 0) |
|
85 { |
157
|
86 error ("qpsol: expecting vector as first argument"); |
1
|
87 return retval; |
|
88 } |
|
89 |
|
90 Matrix H = args[2].to_matrix (); |
|
91 if (H.rows () != H.columns () || H.rows () != x.capacity ()) |
|
92 { |
213
|
93 error ("qpsol: H must be a square matrix consistent with the size of x"); |
1
|
94 return retval; |
|
95 } |
|
96 |
|
97 ColumnVector c = args[3].to_vector (); |
|
98 if (c.capacity () != x.capacity ()) |
|
99 { |
157
|
100 error ("qpsol: c must be a vector the same size as x"); |
1
|
101 return retval; |
|
102 } |
|
103 |
|
104 Bounds bounds; |
|
105 if (nargin == 6 || nargin == 9) |
|
106 { |
|
107 ColumnVector lb = args[4].to_vector (); |
|
108 ColumnVector ub = args[5].to_vector (); |
|
109 |
|
110 int lb_len = lb.capacity (); |
|
111 int ub_len = ub.capacity (); |
|
112 if (lb_len != ub_len || lb_len != x.capacity ()) |
|
113 { |
213
|
114 error ("qpsol: lower and upper bounds and decision variable vector"); |
|
115 error ("must all have the same number of elements"); |
1
|
116 return retval; |
|
117 } |
|
118 |
|
119 bounds.resize (lb_len); |
|
120 bounds.set_lower_bounds (lb); |
|
121 bounds.set_upper_bounds (ub); |
|
122 } |
|
123 |
|
124 ColumnVector soln; |
|
125 double objf; |
|
126 ColumnVector lambda; |
|
127 int inform; |
|
128 |
|
129 if (nargin == 4) |
|
130 { |
|
131 // 1. qpsol (x, H, c) |
|
132 |
|
133 QPSOL qp (x, H, c); |
289
|
134 qp.copy (qpsol_opts); |
1
|
135 soln = qp.minimize (objf, inform, lambda); |
|
136 |
|
137 goto solved; |
|
138 } |
|
139 |
|
140 if (nargin == 6) |
|
141 { |
|
142 // 2. qpsol (x, H, c, lb, ub) |
|
143 |
|
144 QPSOL qp (x, H, c, bounds); |
289
|
145 qp.copy (qpsol_opts); |
1
|
146 soln = qp.minimize (objf, inform, lambda); |
|
147 |
|
148 goto solved; |
|
149 } |
|
150 |
|
151 if (nargin == 7 || nargin == 9) |
|
152 { |
|
153 ColumnVector lub = args[nargin-1].to_vector (); |
|
154 Matrix A = args[nargin-2].to_matrix (); |
|
155 ColumnVector llb = args[nargin-3].to_vector (); |
|
156 |
157
|
157 if (llb.capacity () == 0 || lub.capacity () == 0) |
|
158 { |
|
159 error ("qpsol: bounds for linear constraints must be vectors"); |
|
160 return retval; |
|
161 } |
|
162 |
1
|
163 if (! linear_constraints_ok (x, llb, A, lub, "qpsol", 1)) |
|
164 return retval; |
|
165 |
156
|
166 LinConst linear_constraints (llb, A, lub); |
|
167 |
1
|
168 if (nargin == 9) |
|
169 { |
|
170 // 3. qpsol (x, H, c, lb, ub, llb, A, lub) |
|
171 |
|
172 QPSOL qp (x, H, c, bounds, linear_constraints); |
289
|
173 qp.copy (qpsol_opts); |
1
|
174 soln = qp.minimize (objf, inform, lambda); |
|
175 } |
|
176 else |
|
177 { |
|
178 // 4. qpsol (x, H, c, llb, A, lub) |
|
179 |
|
180 QPSOL qp (x, H, c, linear_constraints); |
289
|
181 qp.copy (qpsol_opts); |
1
|
182 soln = qp.minimize (objf, inform, lambda); |
|
183 } |
|
184 goto solved; |
|
185 } |
|
186 |
|
187 return retval; |
|
188 |
|
189 solved: |
|
190 |
|
191 retval = new tree_constant [nargout+1]; |
|
192 retval[0] = tree_constant (soln, 1); |
|
193 if (nargout > 1) |
|
194 retval[1] = tree_constant (objf); |
|
195 if (nargout > 2) |
|
196 retval[2] = tree_constant ((double) inform); |
|
197 if (nargout > 3) |
|
198 retval[3] = tree_constant (lambda); |
|
199 |
|
200 return retval; |
|
201 } |
|
202 |
289
|
203 typedef void (QPSOL_options::*d_set_opt_mf) (double); |
|
204 typedef void (QPSOL_options::*i_set_opt_mf) (int); |
|
205 typedef double (QPSOL_options::*d_get_opt_mf) (void); |
|
206 typedef int (QPSOL_options::*i_get_opt_mf) (void); |
|
207 |
|
208 #define MAX_TOKENS 2 |
|
209 |
|
210 struct QPSOL_OPTIONS |
|
211 { |
|
212 char *keyword; |
|
213 char *kw_tok[MAX_TOKENS + 1]; |
|
214 int min_len[MAX_TOKENS + 1]; |
|
215 int min_toks_to_match; |
|
216 d_set_opt_mf d_set_fcn; |
|
217 i_set_opt_mf i_set_fcn; |
|
218 d_get_opt_mf d_get_fcn; |
|
219 i_get_opt_mf i_get_fcn; |
|
220 }; |
|
221 |
|
222 static QPSOL_OPTIONS qpsol_option_table[] = |
|
223 { |
|
224 { "feasibility tolerance", |
|
225 { "feasibility", "tolerance", NULL, }, |
|
226 { 1, 0, 0, }, 1, |
|
227 QPSOL_options::set_feasibility_tolerance, NULL, |
|
228 QPSOL_options::feasibility_tolerance, NULL, }, |
|
229 |
|
230 { "infinite bound", |
|
231 { "infinite", "bound", NULL, }, |
|
232 { 2, 0, 0, }, 1, |
|
233 QPSOL_options::set_infinite_bound, NULL, |
|
234 QPSOL_options::infinite_bound, NULL, }, |
|
235 |
|
236 { "iteration limit", |
|
237 { "iteration", "limit", NULL, }, |
|
238 { 2, 0, 0, }, 1, |
|
239 NULL, QPSOL_options::set_iteration_limit, |
|
240 NULL, QPSOL_options::iteration_limit, }, |
|
241 |
|
242 { "print level", |
|
243 { "print", "level", NULL, }, |
|
244 { 1, 0, 0, }, 1, |
|
245 NULL, QPSOL_options::set_print_level, |
|
246 NULL, QPSOL_options::print_level, }, |
|
247 |
|
248 { NULL, |
|
249 { NULL, NULL, NULL, }, |
|
250 { 0, 0, 0, }, 0, |
|
251 NULL, NULL, NULL, NULL, }, |
|
252 }; |
|
253 |
|
254 static void |
|
255 print_qpsol_option_list (void) |
|
256 { |
|
257 ostrstream output_buf; |
|
258 |
|
259 print_usage ("qpsol_options", 1); |
|
260 |
|
261 output_buf << "\n" |
|
262 << "Options for qpsol include:\n\n" |
|
263 << " keyword value\n" |
|
264 << " ------- -----\n\n"; |
|
265 |
|
266 QPSOL_OPTIONS *list = qpsol_option_table; |
|
267 |
|
268 char *keyword; |
|
269 while ((keyword = list->keyword) != (char *) NULL) |
|
270 { |
|
271 output_buf.form (" %-40s ", keyword); |
|
272 if (list->d_get_fcn) |
|
273 { |
|
274 double val = (qpsol_opts.*list->d_get_fcn) (); |
|
275 if (val < 0.0) |
|
276 output_buf << "computed automatically"; |
|
277 else |
|
278 output_buf << val; |
|
279 } |
|
280 else |
|
281 { |
|
282 int val = (qpsol_opts.*list->i_get_fcn) (); |
|
283 if (val < 0) |
|
284 output_buf << "depends on problem size"; |
|
285 else |
|
286 output_buf << val; |
|
287 } |
|
288 output_buf << "\n"; |
|
289 list++; |
|
290 } |
|
291 |
|
292 output_buf << "\n" << ends; |
|
293 maybe_page_output (output_buf); |
|
294 } |
|
295 |
|
296 static void |
|
297 do_qpsol_option (char *keyword, double val) |
|
298 { |
|
299 QPSOL_OPTIONS *list = qpsol_option_table; |
|
300 |
|
301 while (list->keyword != (char *) NULL) |
|
302 { |
|
303 if (keyword_almost_match (list->kw_tok, list->min_len, keyword, |
|
304 list->min_toks_to_match, MAX_TOKENS)) |
|
305 { |
|
306 if (list->d_set_fcn) |
|
307 (qpsol_opts.*list->d_set_fcn) (val); |
|
308 else |
|
309 (qpsol_opts.*list->i_set_fcn) (NINT (val)); |
|
310 |
|
311 return; |
|
312 } |
|
313 list++; |
|
314 } |
|
315 |
|
316 warning ("qpsol_options: no match for `%s'", keyword); |
|
317 } |
|
318 |
272
|
319 tree_constant * |
|
320 qpsol_options (const tree_constant *args, int nargin, int nargout) |
|
321 { |
289
|
322 tree_constant *retval = NULL_TREE_CONST; |
272
|
323 |
289
|
324 if (nargin == 1) |
|
325 { |
|
326 print_qpsol_option_list (); |
|
327 } |
|
328 else if (nargin == 3) |
|
329 { |
|
330 if (args[1].is_string_type ()) |
|
331 { |
|
332 char *keyword = args[1].string_value (); |
|
333 double val = args[2].double_value (); |
|
334 do_qpsol_option (keyword, val); |
|
335 } |
|
336 else |
|
337 print_usage ("qpsol_options"); |
|
338 } |
|
339 else |
|
340 { |
|
341 print_usage ("qpsol_options"); |
|
342 } |
|
343 |
272
|
344 return retval; |
|
345 } |
|
346 |
1
|
347 #endif |
|
348 |
|
349 /* |
|
350 ;;; Local Variables: *** |
|
351 ;;; mode: C++ *** |
|
352 ;;; page-delimiter: "^/\\*" *** |
|
353 ;;; End: *** |
|
354 */ |