1 ## Copyright (C) 2000-2012 Gabriele Pannocchia.
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H})
21 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q})
22 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b})
23 ## @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})
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}, @var{A_lb}, @var{A_in}, @var{A_ub})
25 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@dots{}, @var{options})
26 ## Solve the quadratic program
29 ## \min_x {1 \over 2} x^T H x + x^T q
36 ## min 0.5 x'*H*x + x'*q
45 ## Ax = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} \leq A_{ub}
54 ## A_lb <= A_in*x <= A_ub
60 ## using a null-space active-set method.
62 ## Any bound (@var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb},
63 ## @var{A_ub}) may be set to the empty matrix (@code{[]}) if not
64 ## present. If the initial guess is feasible the algorithm is faster.
68 ## An optional structure containing the following
69 ## parameter(s) used to define the behavior of the solver. Missing elements
70 ## in the structure take on default values, so you only need to set the
71 ## elements that you wish to change from the default.
74 ## @item MaxIter (default: 200)
75 ## Maximum number of iterations.
81 ## Structure containing run-time information about the algorithm. The
82 ## following fields are defined:
86 ## The number of iterations required to find the solution.
89 ## An integer indicating the status of the solution.
93 ## The problem is feasible and convex. Global solution found.
96 ## The problem is not convex. Local solution found.
99 ## The problem is not convex and unbounded.
102 ## Maximum number of iterations reached.
105 ## The problem is infeasible.
111 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup.
112 ## PKG_ADD: [~] = __all_opts__ ("qp");
114 function [x, obj, INFO, lambda] = qp (x0, H, varargin)
118 if (nargin == 1 && ischar (x0) && strcmp (x0, 'defaults'))
119 x = optimset ("MaxIter", 200);
123 if (nargs > 2 && isstruct (varargin{end}))
124 options = varargin{end};
162 if (nargs == 2 || nargs == 3 || nargs == 5 || nargs == 7 || nargs == 10)
164 maxit = optimget (options, "MaxIter", 200);
166 ## Checking the quadratic penalty
168 error ("qp: quadratic penalty matrix not square");
169 elseif (! ishermitian (H))
170 ## warning ("qp: quadratic penalty matrix not hermitian");
175 ## Checking the initial guess (if empty it is resized to the
176 ## right dimension and filled with 0)
179 elseif (numel (x0) != n)
180 error ("qp: the initial guess has incorrect length");
186 elseif (numel (q) != n)
187 error ("qp: the linear term has incorrect length");
190 ## Equality constraint matrices
191 if (isempty (A) || isempty (b))
196 [n_eq, n1] = size (A);
198 error ("qp: equality constraint matrix has incorrect column dimension");
200 if (numel (b) != n_eq)
201 error ("qp: equality constraint matrix and vector have inconsistent dimension");
212 error ("qp: lower bound has incorrect length");
213 elseif (isempty (ub))
221 error ("qp: upper bound has incorrect length");
222 elseif (isempty (lb))
223 Ain = [Ain; -eye(n)];
228 if (! isempty (lb) && ! isempty (ub))
231 if (abs(lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i)))))
232 ## These are actually an equality constraint
236 b = [b; 0.5*(lb(i) + ub(i))];
241 Ain = [Ain; tmprow; -tmprow];
242 bin = [bin; lb(i); -ub(i)];
249 ## Inequality constraints
251 [dimA_in, n1] = size (A_in);
253 error ("qp: inequality constraint matrix has incorrect column dimension");
255 if (! isempty (A_lb))
256 if (numel (A_lb) != dimA_in)
257 error ("qp: inequality constraint matrix and lower bound vector inconsistent");
258 elseif (isempty (A_ub))
263 if (! isempty (A_ub))
264 if (numel (A_ub) != dimA_in)
265 error ("qp: inequality constraint matrix and upper bound vector inconsistent");
266 elseif (isempty (A_lb))
272 if (! isempty (A_lb) && ! isempty (A_ub))
275 if (abs (A_lb(i) - A_ub(i)) < rtol*(1 + max (abs (A_lb(i) + A_ub(i)))))
276 ## These are actually an equality constraint
279 b = [b; 0.5*(A_lb(i) + A_ub(i))];
283 Ain = [Ain; tmprow; -tmprow];
284 bin = [bin; A_lb(i); -A_ub(i)];
292 ## Now we should have the following QP:
294 ## min_x 0.5*x'*H*x + x'*q
298 ## Discard inequality constraints that have -Inf bounds since those
299 ## will never be active.
300 idx = isinf (bin) & bin < 0;
307 ## Check if the initial guess is feasible.
308 if (isa (x0, "single") || isa (H, "single") || isa (q, "single") || isa (A, "single")
309 || isa (b, "single"))
310 rtol = sqrt (eps ("single"));
315 eq_infeasible = (n_eq > 0 && norm (A*x0-b) > rtol*(1+abs (b)));
316 in_infeasible = (n_in > 0 && any (Ain*x0-bin < -rtol*(1+abs (bin))));
319 if (eq_infeasible || in_infeasible)
320 ## The initial guess is not feasible.
321 ## First define xbar that is feasible with respect to the equality
325 error ("qp: equality constraint matrix must be full row rank");
332 ## Check if xbar is feasible with respect to the inequality
335 res = Ain * xbar - bin;
336 if (any (res < -rtol * (1 + abs (bin))))
337 ## xbar is not feasible with respect to the inequality
338 ## constraints. Compute a step in the null space of the
339 ## equality constraints, by solving a QP. If the slack is
340 ## small, we have a feasible initial guess. Otherwise, the
341 ## problem is infeasible.
345 ## The problem is infeasible because A is square and full
346 ## rank, but xbar is not feasible.
352 ## Solve an LP with additional slack variables to find
353 ## a feasible starting point.
356 Atmp = [Ain*Z, gamma];
362 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)];
363 lb = [-Inf(n-n_eq,1); zeros(n_in,1)];
365 ctype = repmat ("L", n_in, 1);
366 [P, dummy, status] = glpk (ctmp, Atmp, btmp, lb, ub, ctype);
367 if ((status == 180 || status == 181 || status == 151)
368 && all (abs (P(n-n_eq+1:end)) < rtol * (1 + norm (btmp))))
369 ## We found a feasible starting point
371 x0 = xbar + Z*P(1:n-n_eq);
376 ## The problem is infeasible
381 ## xbar is feasible. We use it a starting point.
385 ## xbar is feasible. We use it a starting point.
391 ## The initial (or computed) guess is feasible.
392 ## We call the solver.
393 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit);
399 obj = 0.5 * x' * H * x + q' * x;
400 INFO.solveiter = iter;