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