QPS_CLP Quadratic Program Solver based on CLP - COIN-OR Linear Programming. [X, F, EXITFLAG, OUTPUT, LAMBDA] = ... QPS_CLP(H, C, A, L, U, XMIN, XMAX, X0, OPT) A wrapper function providing a MATPOWER standardized interface for using CLP to solve the following QP (quadratic programming) problem: min 1/2 X'*H*X + C'*X X subject to L <= A*X <= U (linear constraints) XMIN <= X <= XMAX (variable bounds) Inputs (all optional except H, C, A and L): H : matrix (possibly sparse) of quadratic cost coefficients C : vector of linear cost coefficients A, L, U : define the optional linear constraints. Default values for the elements of L and U are -Inf and Inf, respectively. XMIN, XMAX : optional lower and upper bounds on the X variables, defaults are -Inf and Inf, respectively. X0 : optional starting value of optimization vector X (NOT USED) OPT : optional options structure with the following fields, all of which are also optional (default values shown in parentheses) verbose (0) - controls level of progress output displayed 0 = no progress output 1 = some progress output 2 = verbose progress output clp_opt - options struct for CLP, value in verbose overrides these options PROBLEM : The inputs can alternatively be supplied in a single PROBLEM struct with fields corresponding to the input arguments described above: H, c, A, l, u, xmin, xmax, x0, opt Outputs: X : solution vector F : final objective function value EXITFLAG : exit flag, 1 - optimal, -1 - infeasible, -2 - unbounded -3 - max iterations/time exceeded OUTPUT : struct with fields exitflag - raw CLP exit flag: 0 - optimal, 1 - infeasible, 2 - unbounded, 3 - max iterations/time exceeded status - string with explanation of exitflag (iter - depending on build of solver this may contain the number of iterations) LAMBDA : struct containing the Langrange and Kuhn-Tucker multipliers on the constraints, with fields: mu_l - lower (left-hand) limit on linear constraints mu_u - upper (right-hand) limit on linear constraints lower - lower bound on optimization variables upper - upper bound on optimization variables Note the calling syntax is almost identical to that of CLP. The main difference is that the linear constraints are specified with A, L, U instead of A, B, Aeq, Beq. Calling syntax options: [x, f, exitflag, output, lambda] = ... qps_clp(H, c, A, l, u, xmin, xmax, x0, opt) x = qps_clp(H, c, A, l, u) x = qps_clp(H, c, A, l, u, xmin, xmax) x = qps_clp(H, c, A, l, u, xmin, xmax, x0) x = qps_clp(H, c, A, l, u, xmin, xmax, x0, opt) x = qps_clp(problem), where problem is a struct with fields: H, c, A, l, u, xmin, xmax, x0, opt all fields except 'c', 'A' and 'l' or 'u' are optional x = qps_clp(...) [x, f] = qps_clp(...) [x, f, exitflag] = qps_clp(...) [x, f, exitflag, output] = qps_clp(...) [x, f, exitflag, output, lambda] = qps_clp(...) Example: (problem from from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm) H = [ 1003.1 4.3 6.3 5.9; 4.3 2.2 2.1 3.9; 6.3 2.1 3.5 4.8; 5.9 3.9 4.8 10 ]; c = zeros(4,1); A = [ 1 1 1 1; 0.17 0.11 0.10 0.18 ]; l = [1; 0.10]; u = [1; Inf]; xmin = zeros(4,1); x0 = [1; 0; 0; 1]; opt = struct('verbose', 2); [x, f, s, out, lambda] = qps_clp(H, c, A, l, u, xmin, [], x0, opt); See also CLP.
0001 function [x, f, eflag, output, lambda] = qps_clp(H, c, A, l, u, xmin, xmax, x0, opt) 0002 %QPS_CLP Quadratic Program Solver based on CLP - COIN-OR Linear Programming. 0003 % [X, F, EXITFLAG, OUTPUT, LAMBDA] = ... 0004 % QPS_CLP(H, C, A, L, U, XMIN, XMAX, X0, OPT) 0005 % A wrapper function providing a MATPOWER standardized interface for using 0006 % CLP to solve the following QP (quadratic programming) problem: 0007 % 0008 % min 1/2 X'*H*X + C'*X 0009 % X 0010 % 0011 % subject to 0012 % 0013 % L <= A*X <= U (linear constraints) 0014 % XMIN <= X <= XMAX (variable bounds) 0015 % 0016 % Inputs (all optional except H, C, A and L): 0017 % H : matrix (possibly sparse) of quadratic cost coefficients 0018 % C : vector of linear cost coefficients 0019 % A, L, U : define the optional linear constraints. Default 0020 % values for the elements of L and U are -Inf and Inf, 0021 % respectively. 0022 % XMIN, XMAX : optional lower and upper bounds on the 0023 % X variables, defaults are -Inf and Inf, respectively. 0024 % X0 : optional starting value of optimization vector X (NOT USED) 0025 % OPT : optional options structure with the following fields, 0026 % all of which are also optional (default values shown in 0027 % parentheses) 0028 % verbose (0) - controls level of progress output displayed 0029 % 0 = no progress output 0030 % 1 = some progress output 0031 % 2 = verbose progress output 0032 % clp_opt - options struct for CLP, value in 0033 % verbose overrides these options 0034 % PROBLEM : The inputs can alternatively be supplied in a single 0035 % PROBLEM struct with fields corresponding to the input arguments 0036 % described above: H, c, A, l, u, xmin, xmax, x0, opt 0037 % 0038 % Outputs: 0039 % X : solution vector 0040 % F : final objective function value 0041 % EXITFLAG : exit flag, 1 - optimal, -1 - infeasible, -2 - unbounded 0042 % -3 - max iterations/time exceeded 0043 % OUTPUT : struct with fields 0044 % exitflag - raw CLP exit flag: 0 - optimal, 1 - infeasible, 0045 % 2 - unbounded, 3 - max iterations/time exceeded 0046 % status - string with explanation of exitflag 0047 % (iter - depending on build of solver this may contain 0048 % the number of iterations) 0049 % LAMBDA : struct containing the Langrange and Kuhn-Tucker 0050 % multipliers on the constraints, with fields: 0051 % mu_l - lower (left-hand) limit on linear constraints 0052 % mu_u - upper (right-hand) limit on linear constraints 0053 % lower - lower bound on optimization variables 0054 % upper - upper bound on optimization variables 0055 % 0056 % Note the calling syntax is almost identical to that of CLP. The main 0057 % difference is that the linear constraints are specified with A, L, U 0058 % instead of A, B, Aeq, Beq. 0059 % 0060 % Calling syntax options: 0061 % [x, f, exitflag, output, lambda] = ... 0062 % qps_clp(H, c, A, l, u, xmin, xmax, x0, opt) 0063 % 0064 % x = qps_clp(H, c, A, l, u) 0065 % x = qps_clp(H, c, A, l, u, xmin, xmax) 0066 % x = qps_clp(H, c, A, l, u, xmin, xmax, x0) 0067 % x = qps_clp(H, c, A, l, u, xmin, xmax, x0, opt) 0068 % x = qps_clp(problem), where problem is a struct with fields: 0069 % H, c, A, l, u, xmin, xmax, x0, opt 0070 % all fields except 'c', 'A' and 'l' or 'u' are optional 0071 % x = qps_clp(...) 0072 % [x, f] = qps_clp(...) 0073 % [x, f, exitflag] = qps_clp(...) 0074 % [x, f, exitflag, output] = qps_clp(...) 0075 % [x, f, exitflag, output, lambda] = qps_clp(...) 0076 % 0077 % 0078 % Example: (problem from from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm) 0079 % H = [ 1003.1 4.3 6.3 5.9; 0080 % 4.3 2.2 2.1 3.9; 0081 % 6.3 2.1 3.5 4.8; 0082 % 5.9 3.9 4.8 10 ]; 0083 % c = zeros(4,1); 0084 % A = [ 1 1 1 1; 0085 % 0.17 0.11 0.10 0.18 ]; 0086 % l = [1; 0.10]; 0087 % u = [1; Inf]; 0088 % xmin = zeros(4,1); 0089 % x0 = [1; 0; 0; 1]; 0090 % opt = struct('verbose', 2); 0091 % [x, f, s, out, lambda] = qps_clp(H, c, A, l, u, xmin, [], x0, opt); 0092 % 0093 % See also CLP. 0094 0095 % MATPOWER 0096 % Copyright (c) 2010-2015 by Power System Engineering Research Center (PSERC) 0097 % by Ray Zimmerman, PSERC Cornell 0098 % 0099 % $Id: qps_clp.m 2661 2015-03-20 17:02:46Z ray $ 0100 % 0101 % This file is part of MATPOWER. 0102 % Covered by the 3-clause BSD License (see LICENSE file for details). 0103 % See http://www.pserc.cornell.edu/matpower/ for more info. 0104 0105 %% check for Optimization Toolbox 0106 % if ~have_fcn('quadprog') 0107 % error('qps_clp: requires the MEX interface to CLP'); 0108 % end 0109 0110 %%----- input argument handling ----- 0111 %% gather inputs 0112 if nargin == 1 && isstruct(H) %% problem struct 0113 p = H; 0114 if isfield(p, 'opt'), opt = p.opt; else, opt = []; end 0115 if isfield(p, 'x0'), x0 = p.x0; else, x0 = []; end 0116 if isfield(p, 'xmax'), xmax = p.xmax; else, xmax = []; end 0117 if isfield(p, 'xmin'), xmin = p.xmin; else, xmin = []; end 0118 if isfield(p, 'u'), u = p.u; else, u = []; end 0119 if isfield(p, 'l'), l = p.l; else, l = []; end 0120 if isfield(p, 'A'), A = p.A; else, A = []; end 0121 if isfield(p, 'c'), c = p.c; else, c = []; end 0122 if isfield(p, 'H'), H = p.H; else, H = []; end 0123 else %% individual args 0124 if nargin < 9 0125 opt = []; 0126 if nargin < 8 0127 x0 = []; 0128 if nargin < 7 0129 xmax = []; 0130 if nargin < 6 0131 xmin = []; 0132 end 0133 end 0134 end 0135 end 0136 end 0137 0138 %% define nx, set default values for missing optional inputs 0139 if isempty(H) || ~any(any(H)) 0140 if isempty(A) && isempty(xmin) && isempty(xmax) 0141 error('qps_clp: LP problem must include constraints or variable bounds'); 0142 else 0143 if ~isempty(A) 0144 nx = size(A, 2); 0145 elseif ~isempty(xmin) 0146 nx = length(xmin); 0147 else % if ~isempty(xmax) 0148 nx = length(xmax); 0149 end 0150 end 0151 else 0152 nx = size(H, 1); 0153 end 0154 if isempty(c) 0155 c = zeros(nx, 1); 0156 end 0157 if isempty(A) || (~isempty(A) && (isempty(l) || all(l == -Inf)) && ... 0158 (isempty(u) || all(u == Inf))) 0159 A = sparse(0,nx); %% no limits => no linear constraints 0160 end 0161 nA = size(A, 1); %% number of original linear constraints 0162 if isempty(u) %% By default, linear inequalities are ... 0163 u = Inf(nA, 1); %% ... unbounded above and ... 0164 end 0165 if isempty(l) 0166 l = -Inf(nA, 1); %% ... unbounded below. 0167 end 0168 if isempty(xmin) %% By default, optimization variables are ... 0169 xmin = -Inf(nx, 1); %% ... unbounded below and ... 0170 end 0171 if isempty(xmax) 0172 xmax = Inf(nx, 1); %% ... unbounded above. 0173 end 0174 if isempty(x0) 0175 x0 = zeros(nx, 1); 0176 end 0177 if ~issparse(A) 0178 A = sparse(A); 0179 end 0180 if ~issparse(H) 0181 H = sparse(H); 0182 end 0183 0184 0185 %% default options 0186 if ~isempty(opt) && isfield(opt, 'verbose') && ~isempty(opt.verbose) 0187 verbose = opt.verbose; 0188 else 0189 verbose = 0; 0190 end 0191 0192 %% set up options struct for CLP 0193 if ~isempty(opt) && isfield(opt, 'clp_opt') && ~isempty(opt.clp_opt) 0194 clp_opt = clp_options(opt.clp_opt); 0195 else 0196 clp_opt = clp_options; 0197 end 0198 0199 if have_fcn('opti_clp') %% use OPTI Toolbox verision's MEX interface 0200 clp_opt.display = verbose; 0201 0202 [x, f, exitflag, iter, lam] = clp(tril(H), c, A, l, u, xmin, xmax, clp_opt); 0203 0204 output.iter = iter; 0205 0206 %% repackage lambdas 0207 % if isempty(x) 0208 % x = NaN(nx, 1); 0209 % f = NaN; 0210 % end 0211 % if isempty(lam) 0212 % lambda = struct( ... 0213 % 'mu_l', zeros(nA, 1), ... 0214 % 'mu_u', zeros(nA, 1), ... 0215 % 'lower', zeros(nx, 1), ... 0216 % 'upper', zeros(nx, 1) ... 0217 % ); 0218 % else 0219 mu_l = lam.dual_row; 0220 mu_u = -lam.dual_row; 0221 lower = lam.dual_col; 0222 upper = -lam.dual_col; 0223 mu_l(mu_l < 0) = 0; 0224 mu_u(mu_u < 0) = 0; 0225 lower(lower < 0) = 0; 0226 upper(upper < 0) = 0; 0227 0228 lambda = struct( ... 0229 'mu_l', mu_l, ... 0230 'mu_u', mu_u, ... 0231 'lower', lower, ... 0232 'upper', upper ... 0233 ); 0234 % end 0235 else 0236 clp_opt.verbose = verbose; 0237 0238 %% split up linear constraints 0239 ieq = find( abs(u-l) <= eps ); %% equality 0240 igt = find( u >= 1e10 & l > -1e10 ); %% greater than, unbounded above 0241 ilt = find( l <= -1e10 & u < 1e10 ); %% less than, unbounded below 0242 ibx = find( (abs(u-l) > eps) & (u < 1e10) & (l > -1e10) ); 0243 Ae = A(ieq, :); 0244 be = u(ieq); 0245 Ai = [ A(ilt, :); -A(igt, :); A(ibx, :); -A(ibx, :) ]; 0246 bi = [ u(ilt); -l(igt); u(ibx); -l(ibx)]; 0247 0248 %% grab some dimensions 0249 nlt = length(ilt); %% number of upper bounded linear inequalities 0250 ngt = length(igt); %% number of lower bounded linear inequalities 0251 nbx = length(ibx); %% number of doubly bounded linear inequalities 0252 0253 %% call the solver 0254 [x, z, exitflag] = ... 0255 clp(H, c, Ai, bi, Ae, be, xmin, xmax, clp_opt); 0256 0257 %% repackage lambdas 0258 if isempty(x) 0259 x = NaN(nx, 1); 0260 f = NaN; 0261 else 0262 if isempty(H) || ~any(any(H)) 0263 f = c'*x; 0264 else 0265 f = 0.5 * x'*H*x + c'*x; 0266 end 0267 end 0268 if isempty(z) 0269 lambda = struct( ... 0270 'mu_l', zeros(nA, 1), ... 0271 'mu_u', zeros(nA, 1), ... 0272 'lower', zeros(nx, 1), ... 0273 'upper', zeros(nx, 1) ... 0274 ); 0275 else 0276 neq = length(be); 0277 nie = length(bi); 0278 lam.eqlin = z(1:neq); 0279 lam.ineqlin = z(neq+(1:nie)); 0280 %%----- MEXCLP DOES NOT RETURN MULTIPLIERS ON VARIABLE BOUNDS :-/ ----- 0281 lam.lower = NaN(nx, 1); 0282 lam.upper = NaN(nx, 1); 0283 kl = find(lam.eqlin > 0); %% lower bound binding 0284 ku = find(lam.eqlin < 0); %% upper bound binding 0285 0286 mu_l = zeros(nA, 1); 0287 mu_l(ieq(kl)) = lam.eqlin(kl); 0288 mu_l(igt) = -lam.ineqlin(nlt+(1:ngt)); 0289 mu_l(ibx) = -lam.ineqlin(nlt+ngt+nbx+(1:nbx)); 0290 0291 mu_u = zeros(nA, 1); 0292 mu_u(ieq(ku)) = -lam.eqlin(ku); 0293 mu_u(ilt) = -lam.ineqlin(1:nlt); 0294 mu_u(ibx) = -lam.ineqlin(nlt+ngt+(1:nbx)); 0295 0296 lambda = struct( ... 0297 'mu_l', mu_l, ... 0298 'mu_u', mu_u, ... 0299 'lower', lam.lower, ... 0300 'upper', lam.upper ... 0301 ); 0302 end 0303 end 0304 0305 %% set eflag 0306 eflag = -exitflag; 0307 if eflag == 0 %% success 0308 eflag = 1; 0309 end 0310 0311 %% set status 0312 output.exitflag = exitflag; 0313 switch exitflag 0314 case 0 0315 output.status = 'optimal'; 0316 case 1 0317 output.status = 'primal infeasible'; 0318 case 2 0319 output.status = 'dual infeasible'; 0320 case 3 0321 output.status = 'max iterations or time exceeded'; 0322 otherwise 0323 output.status = 'unknown exit code'; 0324 end