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})
22## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q})
23## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b})
24## @deftypefnx {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})
25## @deftypefnx {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})
26## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@dots{}, @var{options})
27## Solve the quadratic program
30## \min_x {1 \over 2} x^T H x + x^T q
37## min 0.5 x'*H*x + x'*q
46## Ax = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} \leq A_{ub}
55## A_lb <= A_in*x <= A_ub
61## using a null-space active-set method.
63## Any bound (@var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb},
64## @var{A_ub}) may be set to the empty matrix (@code{[]}) if not
65## present. If the initial guess is feasible the algorithm is faster.
69## An optional structure containing the following
70## parameter(s) used to define the behavior of the solver. Missing elements
71## in the structure take on default values, so you only need to set the
72## elements that you wish to change from the default.
75## @item MaxIter (default: 200)
76## Maximum number of iterations.
80## The value @var{info} is a structure with the following fields:
83## The number of iterations required to find the solution.
85## An integer indicating the status of the solution, as follows:
88## The problem is feasible and convex. Global solution found.
90## The problem is not convex. Local solution found.
92## The problem is not convex and unbounded.
94## Maximum number of iterations reached.
96## The problem is infeasible.
101## PKG_ADD: __all_opts__ ("qp");
103function [x, obj, INFO, lambda] = qp (x0, H, varargin)
107 if (nargin == 1 && ischar (x0) && strcmp (x0, 'defaults'))
108 x = optimset ("MaxIter", 200);
112 if (nargs > 2 && isstruct (varargin{end}))
113 options = varargin{end};
151 if (nargs == 2 || nargs == 3 || nargs == 5 || nargs == 7 || nargs == 10)
153 maxit = optimget (options, "MaxIter", 200);
155 ## Checking the quadratic penalty
157 error ("qp: quadratic penalty matrix not square");
158 elseif (! ishermitian (H))
159 ## warning ("qp: quadratic penalty matrix not hermitian");
164 ## Checking the initial guess (if empty it is resized to the
165 ## right dimension and filled with 0)
168 elseif (numel (x0) != n)
169 error ("qp: the initial guess has incorrect length");
175 elseif (numel (q) != n)
176 error ("qp: the linear term has incorrect length");
179 ## Equality constraint matrices
180 if (isempty (A) || isempty (b))
185 [n_eq, n1] = size (A);
187 error ("qp: equality constraint matrix has incorrect column dimension");
189 if (numel (b) != n_eq)
190 error ("qp: equality constraint matrix and vector have inconsistent dimension");
201 error ("qp: lower bound has incorrect length");
202 elseif (isempty (ub))
210 error ("qp: upper bound has incorrect length");
211 elseif (isempty (lb))
212 Ain = [Ain; -eye(n)];
217 if (! isempty (lb) && ! isempty (ub))
220 if (abs(lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i)))))
221 ## These are actually an equality constraint
225 b = [b; 0.5*(lb(i) + ub(i))];
230 Ain = [Ain; tmprow; -tmprow];
231 bin = [bin; lb(i); -ub(i)];
238 ## Inequality constraints
240 [dimA_in, n1] = size (A_in);
242 error ("qp: inequality constraint matrix has incorrect column dimension");
244 if (! isempty (A_lb))
245 if (numel (A_lb) != dimA_in)
246 error ("qp: inequality constraint matrix and lower bound vector inconsistent");
247 elseif (isempty (A_ub))
252 if (! isempty (A_ub))
253 if (numel (A_ub) != dimA_in)
254 error ("qp: inequality constraint matrix and upper bound vector inconsistent");
255 elseif (isempty (A_lb))
261 if (! isempty (A_lb) && ! isempty (A_ub))
264 if (abs (A_lb(i) - A_ub(i)) < rtol*(1 + max (abs (A_lb(i) + A_ub(i)))))
265 ## These are actually an equality constraint
268 b = [b; 0.5*(A_lb(i) + A_ub(i))];
272 Ain = [Ain; tmprow; -tmprow];
273 bin = [bin; A_lb(i); -A_ub(i)];
281 ## Now we should have the following QP:
283 ## min_x 0.5*x'*H*x + x'*q
287 ## Discard inequality constraints that have -Inf bounds since those
288 ## will never be active.
289 idx = isinf (bin) & bin < 0;
296 ## Check if the initial guess is feasible.
297 if (isa (x0, "single") || isa (H, "single") || isa (q, "single") || isa (A, "single")
298 || isa (b, "single"))
299 rtol = sqrt (eps ("single"));
304 eq_infeasible = (n_eq > 0 && norm (A*x0-b) > rtol*(1+abs (b)));
305 in_infeasible = (n_in > 0 && any (Ain*x0-bin < -rtol*(1+abs (bin))));
308 if (eq_infeasible || in_infeasible)
309 ## The initial guess is not feasible.
310 ## First define xbar that is feasible with respect to the equality
314 error ("qp: equality constraint matrix must be full row rank")
321 ## Check if xbar is feasible with respect to the inequality
324 res = Ain * xbar - bin;
325 if (any (res < -rtol * (1 + abs (bin))))
326 ## xbar is not feasible with respect to the inequality
327 ## constraints. Compute a step in the null space of the
328 ## equality constraints, by solving a QP. If the slack is
329 ## small, we have a feasible initial guess. Otherwise, the
330 ## problem is infeasible.
334 ## The problem is infeasible because A is square and full
335 ## rank, but xbar is not feasible.
341 ## Solve an LP with additional slack variables to find
342 ## a feasible starting point.
345 Atmp = [Ain*Z, gamma];
351 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)];
352 lb = [-Inf*ones(n-n_eq,1); zeros(n_in,1)];
354 ctype = repmat ("L", n_in, 1);
355 [P, dummy, status] = glpk (ctmp, Atmp, btmp, lb, ub, ctype);
356 if ((status == 180 || status == 181 || status == 151)
357 && all (abs (P(n-n_eq+1:end)) < rtol * (1 + norm (btmp))))
358 ## We found a feasible starting point
360 x0 = xbar + Z*P(1:n-n_eq);
365 ## The problem is infeasible
370 ## xbar is feasible. We use it a starting point.
374 ## xbar is feasible. We use it a starting point.
380 ## The initial (or computed) guess is feasible.
381 ## We call the solver.
382 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit);
388 obj = 0.5 * x' * H * x + q' * x;
389 INFO.solveiter = iter;