changelog shortlog tags changeset files revisions annotate raw

scripts/optimization/qp.m

changeset 9846: 1d90fc211872
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Sat Nov 21 21:44:51 2009 -0500 (33 hours ago)
permissions: -rw-r--r--
description: configure.ac: report freetype, fontconfig, and fltk cflags and libs info
1## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008,
2## 2009 Gabriele Pannocchia.
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb}, @var{A_in}, @var{A_ub})
22## Solve the quadratic program
23## @tex
24## $$
25## \min_x {1 \over 2} x^T H x + x^T q
26## $$
27## @end tex
28## @ifnottex
29##
30## @example
31## @group
32## min 0.5 x'*H*x + x'*q
33## x
34## @end group
35## @end example
36##
37## @end ifnottex
38## subject to
39## @tex
40## $$
41## Ax = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} \leq A_{ub}
42## $$
43## @end tex
44## @ifnottex
45##
46## @example
47## @group
48## A*x = b
49## lb <= x <= ub
50## A_lb <= A_in*x <= A_ub
51## @end group
52## @end example
53## @end ifnottex
54##
55## @noindent
56## using a null-space active-set method.
57##
58## Any bound (@var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb},
59## @var{A_ub}) may be set to the empty matrix (@code{[]}) if not
60## present. If the initial guess is feasible the algorithm is faster.
61##
62## The value @var{info} is a structure with the following fields:
63## @table @code
64## @item solveiter
65## The number of iterations required to find the solution.
66## @item info
67## An integer indicating the status of the solution, as follows:
68## @table @asis
69## @item 0
70## The problem is feasible and convex. Global solution found.
71## @item 1
72## The problem is not convex. Local solution found.
73## @item 2
74## The problem is not convex and unbounded.
75## @item 3
76## Maximum number of iterations reached.
77## @item 6
78## The problem is infeasible.
79## @end table
80## @end table
81## @end deftypefn
82
83function [x, obj, INFO, lambda] = qp (x0, H, q, A, b, lb, ub, A_lb, A_in, A_ub)
84
85 if (nargin == 5 || nargin == 7 || nargin == 10)
86
87 ## Checking the quadratic penalty
88 n = issquare (H);
89 if (n == 0)
90 error ("qp: quadratic penalty matrix not square");
91 endif
92
93 n1 = issymmetric (H);
94 if (n1 == 0)
95 ## warning ("qp: quadratic penalty matrix not symmetric");
96 H = (H + H')/2;
97 endif
98
99 ## Checking the initial guess (if empty it is resized to the
100 ## right dimension and filled with 0)
101 if (isempty (x0))
102 x0 = zeros (n, 1);
103 elseif (length (x0) != n)
104 error ("qp: the initial guess has incorrect length");
105 endif
106
107 ## Linear penalty.
108 if (length (q) != n)
109 error ("qp: the linear term has incorrect length");
110 endif
111
112 ## Equality constraint matrices
113 if (isempty (A) || isempty(b))
114 n_eq = 0;
115 A = zeros (n_eq, n);
116 b = zeros (n_eq, 1);
117 else
118 [n_eq, n1] = size (A);
119 if (n1 != n)
120 error ("qp: equality constraint matrix has incorrect column dimension");
121 endif
122 if (length (b) != n_eq)
123 error ("qp: equality constraint matrix and vector have inconsistent dimension");
124 endif
125 endif
126
127 ## Bound constraints
128 Ain = zeros (0, n);
129 bin = zeros (0, 1);
130 n_in = 0;
131 if (nargin > 5)
132 if (! isempty (lb))
133 if (length(lb) != n)
134 error ("qp: lower bound has incorrect length");
135 elseif (isempty (ub))
136 Ain = [Ain; eye(n)];
137 bin = [bin; lb];
138 endif
139 endif
140
141 if (! isempty (ub))
142 if (length (ub) != n)
143 error ("qp: upper bound has incorrect length");
144 elseif (isempty (lb))
145 Ain = [Ain; -eye(n)];
146 bin = [bin; -ub];
147 endif
148 endif
149
150 if (! isempty (lb) && ! isempty (ub))
151 rtol = sqrt (eps);
152 for i = 1:n
153 if (abs(lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i)))))
154 ## These are actually an equality constraint
155 tmprow = zeros(1,n);
156 tmprow(i) = 1;
157 A = [A;tmprow];
158 b = [b; 0.5*(lb(i) + ub(i))];
159 n_eq = n_eq + 1;
160 else
161 tmprow = zeros(1,n);
162 tmprow(i) = 1;
163 Ain = [Ain; tmprow; -tmprow];
164 bin = [bin; lb(i); -ub(i)];
165 n_in = n_in + 2;
166 endif
167 endfor
168 endif
169 endif
170
171 ## Inequality constraints
172 if (nargin > 7)
173 [dimA_in, n1] = size (A_in);
174 if (n1 != n)
175 error ("qp: inequality constraint matrix has incorrect column dimension");
176 else
177 if (! isempty (A_lb))
178 if (length (A_lb) != dimA_in)
179 error ("qp: inequality constraint matrix and lower bound vector inconsistent");
180 elseif (isempty (A_ub))
181 Ain = [Ain; A_in];
182 bin = [bin; A_lb];
183 endif
184 endif
185 if (! isempty (A_ub))
186 if (length (A_ub) != dimA_in)
187 error ("qp: inequality constraint matrix and upper bound vector inconsistent");
188 elseif (isempty (A_lb))
189 Ain = [Ain; -A_in];
190 bin = [bin; -A_ub];
191 endif
192 endif
193
194 if (! isempty (A_lb) && ! isempty (A_ub))
195 rtol = sqrt (eps);
196 for i = 1:dimA_in
197 if (abs (A_lb(i) - A_ub(i)) < rtol*(1 + max (abs (A_lb(i) + A_ub(i)))))
198 ## These are actually an equality constraint
199 tmprow = A_in(i,:);
200 A = [A;tmprow];
201 b = [b; 0.5*(A_lb(i) + A_ub(i))];
202 n_eq = n_eq + 1;
203 else
204 tmprow = A_in(i,:);
205 Ain = [Ain; tmprow; -tmprow];
206 bin = [bin; A_lb(i); -A_ub(i)];
207 n_in = n_in + 2;
208 endif
209 endfor
210 endif
211 endif
212 endif
213
214 ## Now we should have the following QP:
215 ##
216 ## min_x 0.5*x'*H*x + x'*q
217 ## s.t. A*x = b
218 ## Ain*x >= bin
219
220 ## Discard inequality constraints that have -Inf bounds since those
221 ## will never be active.
222 idx = isinf (bin) & bin < 0;
223
224 bin(idx) = [];
225 Ain(idx,:) = [];
226
227 n_in = length (bin);
228
229 ## Check if the initial guess is feasible.
230 if (isa (x0, "single") || isa (H, "single") || isa (q, "single") || isa (A, "single")
231 || isa (b, "single"))
232 rtol = sqrt (eps ("single"));
233 else
234 rtol = sqrt (eps);
235 endif
236
237 eq_infeasible = (n_eq > 0 && norm (A*x0-b) > rtol*(1+abs (b)));
238 in_infeasible = (n_in > 0 && any (Ain*x0-bin < -rtol*(1+abs (bin))));
239
240 info = 0;
241 if (eq_infeasible || in_infeasible)
242 ## The initial guess is not feasible.
243 ## First define xbar that is feasible with respect to the equality
244 ## constraints.
245 if (eq_infeasible)
246 if (rank (A) < n_eq)
247 error ("qp: equality constraint matrix must be full row rank")
248 endif
249 xbar = pinv (A) * b;
250 else
251 xbar = x0;
252 endif
253
254 ## Check if xbar is feasible with respect to the inequality
255 ## constraints also.
256 if (n_in > 0)
257 res = Ain * xbar - bin;
258 if (any (res < -rtol * (1 + abs (bin))))
259 ## xbar is not feasible with respect to the inequality
260 ## constraints. Compute a step in the null space of the
261 ## equality constraints, by solving a QP. If the slack is
262 ## small, we have a feasible initial guess. Otherwise, the
263 ## problem is infeasible.
264 if (n_eq > 0)
265 Z = null (A);
266 if (isempty (Z))
267 ## The problem is infeasible because A is square and full
268 ## rank, but xbar is not feasible.
269 info = 6;
270 endif
271 endif
272
273 if (info != 6)
274 ## Solve an LP with additional slack variables to find
275 ## a feasible starting point.
276 gamma = eye (n_in);
277 if (n_eq > 0)
278 Atmp = [Ain*Z, gamma];
279 btmp = -res;
280 else
281 Atmp = [Ain, gamma];
282 btmp = bin;
283 endif
284 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)];
285 lb = [-Inf*ones(n-n_eq,1); zeros(n_in,1)];
286 ub = [];
287 ctype = repmat ("L", n_in, 1);
288 [P, dummy, status] = glpk (ctmp, Atmp, btmp, lb, ub, ctype);
289 if ((status == 180 || status == 181 || status == 151)
290 && all (abs (P(n-n_eq+1:end)) < rtol * (1 + norm (btmp))))
291 ## We found a feasible starting point
292 if (n_eq > 0)
293 x0 = xbar + Z*P(1:n-n_eq);
294 else
295 x0 = P(1:n);
296 endif
297 else
298 ## The problem is infeasible
299 info = 6;
300 endif
301 endif
302 else
303 ## xbar is feasible. We use it a starting point.
304 x0 = xbar;
305 endif
306 else
307 ## xbar is feasible. We use it a starting point.
308 x0 = xbar;
309 endif
310 endif
311
312 if (info == 0)
313 ## FIXME -- make maxit a user option.
314 ## The initial (or computed) guess is feasible.
315 ## We call the solver.
316 maxit = 200;
317 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit);
318 else
319 iter = 0;
320 x = x0;
321 lambda = [];
322 endif
323 obj = 0.5 * x' * H * x + q' * x;
324 INFO.solveiter = iter;
325 INFO.info = info;
326
327 else
328 print_usage ();
329 endif
330
331endfunction