1## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008,
2## 2009 Gabriele Pannocchia.
4## This file is part of Octave.
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.
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.
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/>.
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
25## \min_x {1 \over 2} x^T H x + x^T q
32## min 0.5 x'*H*x + x'*q
41## Ax = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} \leq A_{ub}
50## A_lb <= A_in*x <= A_ub
56## using a null-space active-set method.
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.
62## The value @var{info} is a structure with the following fields:
65## The number of iterations required to find the solution.
67## An integer indicating the status of the solution, as follows:
70## The problem is feasible and convex. Global solution found.
72## The problem is not convex. Local solution found.
74## The problem is not convex and unbounded.
76## Maximum number of iterations reached.
78## The problem is infeasible.
83function [x, obj, INFO, lambda] = qp (x0, H, q, A, b, lb, ub, A_lb, A_in, A_ub)
85 if (nargin == 5 || nargin == 7 || nargin == 10)
87 ## Checking the quadratic penalty
90 error ("qp: quadratic penalty matrix not square");
95 ## warning ("qp: quadratic penalty matrix not symmetric");
99 ## Checking the initial guess (if empty it is resized to the
100 ## right dimension and filled with 0)
103 elseif (length (x0) != n)
104 error ("qp: the initial guess has incorrect length");
109 error ("qp: the linear term has incorrect length");
112 ## Equality constraint matrices
113 if (isempty (A) || isempty(b))
118 [n_eq, n1] = size (A);
120 error ("qp: equality constraint matrix has incorrect column dimension");
122 if (length (b) != n_eq)
123 error ("qp: equality constraint matrix and vector have inconsistent dimension");
134 error ("qp: lower bound has incorrect length");
135 elseif (isempty (ub))
142 if (length (ub) != n)
143 error ("qp: upper bound has incorrect length");
144 elseif (isempty (lb))
145 Ain = [Ain; -eye(n)];
150 if (! isempty (lb) && ! isempty (ub))
153 if (abs(lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i)))))
154 ## These are actually an equality constraint
158 b = [b; 0.5*(lb(i) + ub(i))];
163 Ain = [Ain; tmprow; -tmprow];
164 bin = [bin; lb(i); -ub(i)];
171 ## Inequality constraints
173 [dimA_in, n1] = size (A_in);
175 error ("qp: inequality constraint matrix has incorrect column dimension");
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))
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))
194 if (! isempty (A_lb) && ! isempty (A_ub))
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
201 b = [b; 0.5*(A_lb(i) + A_ub(i))];
205 Ain = [Ain; tmprow; -tmprow];
206 bin = [bin; A_lb(i); -A_ub(i)];
214 ## Now we should have the following QP:
216 ## min_x 0.5*x'*H*x + x'*q
220 ## Discard inequality constraints that have -Inf bounds since those
221 ## will never be active.
222 idx = isinf (bin) & bin < 0;
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"));
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))));
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
247 error ("qp: equality constraint matrix must be full row rank")
254 ## Check if xbar is feasible with respect to the inequality
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.
267 ## The problem is infeasible because A is square and full
268 ## rank, but xbar is not feasible.
274 ## Solve an LP with additional slack variables to find
275 ## a feasible starting point.
278 Atmp = [Ain*Z, gamma];
284 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)];
285 lb = [-Inf*ones(n-n_eq,1); zeros(n_in,1)];
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
293 x0 = xbar + Z*P(1:n-n_eq);
298 ## The problem is infeasible
303 ## xbar is feasible. We use it a starting point.
307 ## xbar is feasible. We use it a starting point.
313 ## FIXME -- make maxit a user option.
314 ## The initial (or computed) guess is feasible.
315 ## We call the solver.
317 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit);
323 obj = 0.5 * x' * H * x + q' * x;
324 INFO.solveiter = iter;