1
|
1 // tc-qpsol.cc -*- C++ -*- |
|
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 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifndef QPSOL_MISSING |
|
29 |
|
30 #include "QPSOL.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "variables.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
|
36 #include "utils.h" |
|
37 |
|
38 // This should probably be defined in some shared file and declared in |
|
39 // a header file... |
|
40 extern int linear_constraints_ok (const ColumnVector& x, |
|
41 const ColumnVector& llb, const Matrix& c, |
|
42 const ColumnVector& lub, char *warn_for, |
|
43 int warn); |
|
44 |
|
45 #ifdef WITH_DLD |
|
46 tree_constant * |
|
47 builtin_qpsol_2 (tree_constant *args, int nargin, int nargout) |
|
48 { |
|
49 return qpsol (args, nargin, nargout); |
|
50 } |
|
51 #endif |
|
52 |
|
53 tree_constant * |
|
54 qpsol (tree_constant *args, int nargin, int nargout) |
|
55 { |
|
56 /* |
|
57 |
|
58 Handle all of the following: |
|
59 |
|
60 1. qpsol (x, H, c) |
|
61 2. qpsol (x, H, c, lb, ub) |
|
62 3. qpsol (x, H, c, lb, ub, llb, A, lub) |
|
63 4. qpsol (x, H, c, llb, A, lub) |
|
64 |
|
65 */ |
|
66 |
|
67 // Assumes that we have been given the correct number of arguments. |
|
68 |
|
69 tree_constant *retval = NULL_TREE_CONST; |
|
70 |
|
71 ColumnVector x = args[1].to_vector (); |
|
72 if (x.capacity () == 0) |
|
73 { |
|
74 message ("qpsol", "expecting vector as first argument"); |
|
75 return retval; |
|
76 } |
|
77 |
|
78 Matrix H = args[2].to_matrix (); |
|
79 if (H.rows () != H.columns () || H.rows () != x.capacity ()) |
|
80 { |
|
81 message ("qpsol", "H must be a square matrix consistent with the\ |
|
82 size of x"); |
|
83 return retval; |
|
84 } |
|
85 |
|
86 ColumnVector c = args[3].to_vector (); |
|
87 if (c.capacity () != x.capacity ()) |
|
88 { |
|
89 message ("qpsol", "c must be a vector the same size as x"); |
|
90 return retval; |
|
91 } |
|
92 |
|
93 Bounds bounds; |
|
94 if (nargin == 6 || nargin == 9) |
|
95 { |
|
96 ColumnVector lb = args[4].to_vector (); |
|
97 ColumnVector ub = args[5].to_vector (); |
|
98 |
|
99 int lb_len = lb.capacity (); |
|
100 int ub_len = ub.capacity (); |
|
101 if (lb_len != ub_len || lb_len != x.capacity ()) |
|
102 { |
|
103 message ("qpsol", "lower and upper bounds and decision variable\n\ |
|
104 vector must all have the same number of elements"); |
|
105 return retval; |
|
106 } |
|
107 |
|
108 bounds.resize (lb_len); |
|
109 bounds.set_lower_bounds (lb); |
|
110 bounds.set_upper_bounds (ub); |
|
111 } |
|
112 |
|
113 ColumnVector soln; |
|
114 double objf; |
|
115 ColumnVector lambda; |
|
116 int inform; |
|
117 |
|
118 if (nargin == 4) |
|
119 { |
|
120 // 1. qpsol (x, H, c) |
|
121 |
|
122 QPSOL qp (x, H, c); |
|
123 soln = qp.minimize (objf, inform, lambda); |
|
124 |
|
125 goto solved; |
|
126 } |
|
127 |
|
128 if (nargin == 6) |
|
129 { |
|
130 // 2. qpsol (x, H, c, lb, ub) |
|
131 |
|
132 QPSOL qp (x, H, c, bounds); |
|
133 soln = qp.minimize (objf, inform, lambda); |
|
134 |
|
135 goto solved; |
|
136 } |
|
137 |
|
138 if (nargin == 7 || nargin == 9) |
|
139 { |
|
140 ColumnVector lub = args[nargin-1].to_vector (); |
|
141 Matrix A = args[nargin-2].to_matrix (); |
|
142 ColumnVector llb = args[nargin-3].to_vector (); |
|
143 |
|
144 LinConst linear_constraints (llb, A, lub); |
|
145 |
|
146 if (! linear_constraints_ok (x, llb, A, lub, "qpsol", 1)) |
|
147 return retval; |
|
148 |
|
149 if (nargin == 9) |
|
150 { |
|
151 // 3. qpsol (x, H, c, lb, ub, llb, A, lub) |
|
152 |
|
153 QPSOL qp (x, H, c, bounds, linear_constraints); |
|
154 soln = qp.minimize (objf, inform, lambda); |
|
155 } |
|
156 else |
|
157 { |
|
158 // 4. qpsol (x, H, c, llb, A, lub) |
|
159 |
|
160 QPSOL qp (x, H, c, linear_constraints); |
|
161 soln = qp.minimize (objf, inform, lambda); |
|
162 } |
|
163 goto solved; |
|
164 } |
|
165 |
|
166 return retval; |
|
167 |
|
168 solved: |
|
169 |
|
170 retval = new tree_constant [nargout+1]; |
|
171 retval[0] = tree_constant (soln, 1); |
|
172 if (nargout > 1) |
|
173 retval[1] = tree_constant (objf); |
|
174 if (nargout > 2) |
|
175 retval[2] = tree_constant ((double) inform); |
|
176 if (nargout > 3) |
|
177 retval[3] = tree_constant (lambda); |
|
178 |
|
179 return retval; |
|
180 } |
|
181 |
|
182 #endif |
|
183 |
|
184 /* |
|
185 ;;; Local Variables: *** |
|
186 ;;; mode: C++ *** |
|
187 ;;; page-delimiter: "^/\\*" *** |
|
188 ;;; End: *** |
|
189 */ |