QPS_GLPK Linear Program Solver based on GLPK - GNU Linear Programming Kit. [X, F, EXITFLAG, OUTPUT, LAMBDA] = ... QPS_GLPK(H, C, A, L, U, XMIN, XMAX, X0, OPT) [X, F, EXITFLAG, OUTPUT, LAMBDA] = QPS_GLPK(PROBLEM) A wrapper function providing a standardized interface for using GLKP to solve the following LP (linear programming) problem: min 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 : dummy matrix (possibly sparse) of quadratic cost coefficients for QP problems, which GLPK does not handle 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 glpk_opt - options struct for GLPK, 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, <= 0 - infeasible, unbounded or other OUTPUT : struct with errnum and status fields from GLPK output args 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 GLPK. 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_glpk(H, c, A, l, u, xmin, xmax, x0, opt) x = qps_glpk(H, c, A, l, u) x = qps_glpk(H, c, A, l, u, xmin, xmax) x = qps_glpk(H, c, A, l, u, xmin, xmax, x0) x = qps_glpk(H, c, A, l, u, xmin, xmax, x0, opt) x = qps_glpk(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_glpk(...) [x, f] = qps_glpk(...) [x, f, exitflag] = qps_glpk(...) [x, f, exitflag, output] = qps_glpk(...) [x, f, exitflag, output, lambda] = qps_glpk(...) Example: (problem from from https://v8doc.sas.com/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_glpk(H, c, A, l, u, xmin, [], x0, opt); See also QPS_MASTER, GLPK.
0001 function [x, f, eflag, output, lambda] = qps_glpk(H, c, A, l, u, xmin, xmax, x0, opt) 0002 %QPS_GLPK Linear Program Solver based on GLPK - GNU Linear Programming Kit. 0003 % [X, F, EXITFLAG, OUTPUT, LAMBDA] = ... 0004 % QPS_GLPK(H, C, A, L, U, XMIN, XMAX, X0, OPT) 0005 % [X, F, EXITFLAG, OUTPUT, LAMBDA] = QPS_GLPK(PROBLEM) 0006 % A wrapper function providing a standardized interface for using 0007 % GLKP to solve the following LP (linear programming) problem: 0008 % 0009 % min C'*X 0010 % X 0011 % 0012 % subject to 0013 % 0014 % L <= A*X <= U (linear constraints) 0015 % XMIN <= X <= XMAX (variable bounds) 0016 % 0017 % Inputs (all optional except H, C, A and L): 0018 % H : dummy matrix (possibly sparse) of quadratic cost coefficients 0019 % for QP problems, which GLPK does not handle 0020 % C : vector of linear cost coefficients 0021 % A, L, U : define the optional linear constraints. Default 0022 % values for the elements of L and U are -Inf and Inf, 0023 % respectively. 0024 % XMIN, XMAX : optional lower and upper bounds on the 0025 % X variables, defaults are -Inf and Inf, respectively. 0026 % X0 : optional starting value of optimization vector X (NOT USED) 0027 % OPT : optional options structure with the following fields, 0028 % all of which are also optional (default values shown in 0029 % parentheses) 0030 % verbose (0) - controls level of progress output displayed 0031 % 0 = no progress output 0032 % 1 = some progress output 0033 % 2 = verbose progress output 0034 % glpk_opt - options struct for GLPK, value in verbose 0035 % overrides these options 0036 % PROBLEM : The inputs can alternatively be supplied in a single 0037 % PROBLEM struct with fields corresponding to the input arguments 0038 % described above: H, c, A, l, u, xmin, xmax, x0, opt 0039 % 0040 % Outputs: 0041 % X : solution vector 0042 % F : final objective function value 0043 % EXITFLAG : exit flag, 1 - optimal, <= 0 - infeasible, unbounded or other 0044 % OUTPUT : struct with errnum and status fields from GLPK output args 0045 % LAMBDA : struct containing the Langrange and Kuhn-Tucker 0046 % multipliers on the constraints, with fields: 0047 % mu_l - lower (left-hand) limit on linear constraints 0048 % mu_u - upper (right-hand) limit on linear constraints 0049 % lower - lower bound on optimization variables 0050 % upper - upper bound on optimization variables 0051 % 0052 % Note the calling syntax is almost identical to that of GLPK. The main 0053 % difference is that the linear constraints are specified with A, L, U 0054 % instead of A, B, Aeq, Beq. 0055 % 0056 % Calling syntax options: 0057 % [x, f, exitflag, output, lambda] = ... 0058 % qps_glpk(H, c, A, l, u, xmin, xmax, x0, opt) 0059 % 0060 % x = qps_glpk(H, c, A, l, u) 0061 % x = qps_glpk(H, c, A, l, u, xmin, xmax) 0062 % x = qps_glpk(H, c, A, l, u, xmin, xmax, x0) 0063 % x = qps_glpk(H, c, A, l, u, xmin, xmax, x0, opt) 0064 % x = qps_glpk(problem), where problem is a struct with fields: 0065 % H, c, A, l, u, xmin, xmax, x0, opt 0066 % all fields except 'c', 'A' and 'l' or 'u' are optional 0067 % x = qps_glpk(...) 0068 % [x, f] = qps_glpk(...) 0069 % [x, f, exitflag] = qps_glpk(...) 0070 % [x, f, exitflag, output] = qps_glpk(...) 0071 % [x, f, exitflag, output, lambda] = qps_glpk(...) 0072 % 0073 % 0074 % Example: (problem from from https://v8doc.sas.com/sashtml/iml/chap8/sect12.htm) 0075 % H = [ 1003.1 4.3 6.3 5.9; 0076 % 4.3 2.2 2.1 3.9; 0077 % 6.3 2.1 3.5 4.8; 0078 % 5.9 3.9 4.8 10 ]; 0079 % c = zeros(4,1); 0080 % A = [ 1 1 1 1; 0081 % 0.17 0.11 0.10 0.18 ]; 0082 % l = [1; 0.10]; 0083 % u = [1; Inf]; 0084 % xmin = zeros(4,1); 0085 % x0 = [1; 0; 0; 1]; 0086 % opt = struct('verbose', 2); 0087 % [x, f, s, out, lambda] = qps_glpk(H, c, A, l, u, xmin, [], x0, opt); 0088 % 0089 % See also QPS_MASTER, GLPK. 0090 0091 % MP-Opt-Model 0092 % Copyright (c) 2010-2020, Power Systems Engineering Research Center (PSERC) 0093 % by Ray Zimmerman, PSERC Cornell 0094 % 0095 % This file is part of MP-Opt-Model. 0096 % Covered by the 3-clause BSD License (see LICENSE file for details). 0097 % See https://github.com/MATPOWER/mp-opt-model for more info. 0098 0099 %% check for Optimization Toolbox 0100 % if ~have_feature('quadprog') 0101 % error('qps_glpk: requires the MEX interface to GLPK'); 0102 % end 0103 0104 %%----- input argument handling ----- 0105 %% gather inputs 0106 if nargin == 1 && isstruct(H) %% problem struct 0107 p = H; 0108 if isfield(p, 'opt'), opt = p.opt; else, opt = []; end 0109 if isfield(p, 'x0'), x0 = p.x0; else, x0 = []; end 0110 if isfield(p, 'xmax'), xmax = p.xmax; else, xmax = []; end 0111 if isfield(p, 'xmin'), xmin = p.xmin; else, xmin = []; end 0112 if isfield(p, 'u'), u = p.u; else, u = []; end 0113 if isfield(p, 'l'), l = p.l; else, l = []; end 0114 if isfield(p, 'A'), A = p.A; else, A = []; end 0115 if isfield(p, 'c'), c = p.c; else, c = []; end 0116 if isfield(p, 'H'), H = p.H; else, H = []; end 0117 else %% individual args 0118 if nargin < 9 0119 opt = []; 0120 if nargin < 8 0121 x0 = []; 0122 if nargin < 7 0123 xmax = []; 0124 if nargin < 6 0125 xmin = []; 0126 end 0127 end 0128 end 0129 end 0130 end 0131 0132 %% define nx, set default values for missing optional inputs 0133 if isempty(H) || ~any(any(H)) 0134 if isempty(A) && isempty(xmin) && isempty(xmax) 0135 error('qps_glpk: LP problem must include constraints or variable bounds'); 0136 else 0137 if ~isempty(A) 0138 nx = size(A, 2); 0139 elseif ~isempty(xmin) 0140 nx = length(xmin); 0141 else % if ~isempty(xmax) 0142 nx = length(xmax); 0143 end 0144 end 0145 else 0146 error('qps_glpk: GLPK handles only LP problems, not QP problems'); 0147 nx = size(H, 1); 0148 end 0149 if isempty(c) 0150 c = zeros(nx, 1); 0151 end 0152 if isempty(A) || (~isempty(A) && (isempty(l) || all(l == -Inf)) && ... 0153 (isempty(u) || all(u == Inf))) 0154 A = sparse(0,nx); %% no limits => no linear constraints 0155 end 0156 nA = size(A, 1); %% number of original linear constraints 0157 if isempty(u) %% By default, linear inequalities are ... 0158 u = Inf(nA, 1); %% ... unbounded above and ... 0159 end 0160 if isempty(l) 0161 l = -Inf(nA, 1); %% ... unbounded below. 0162 end 0163 if isempty(xmin) %% By default, optimization variables are ... 0164 xmin = -Inf(nx, 1); %% ... unbounded below and ... 0165 end 0166 if isempty(xmax) 0167 xmax = Inf(nx, 1); %% ... unbounded above. 0168 end 0169 if isempty(x0) 0170 x0 = zeros(nx, 1); 0171 end 0172 0173 %% default options 0174 if ~isempty(opt) && isfield(opt, 'verbose') && ~isempty(opt.verbose) 0175 verbose = opt.verbose; 0176 else 0177 verbose = 0; 0178 end 0179 0180 %% split up linear constraints 0181 ieq = find( abs(u-l) <= eps ); %% equality 0182 igt = find( u >= 1e10 & l > -1e10 ); %% greater than, unbounded above 0183 ilt = find( l <= -1e10 & u < 1e10 ); %% less than, unbounded below 0184 ibx = find( (abs(u-l) > eps) & (u < 1e10) & (l > -1e10) ); 0185 AA = [ A(ieq, :); A(ilt, :); -A(igt, :); A(ibx, :); -A(ibx, :) ]; 0186 bb = [ u(ieq); u(ilt); -l(igt); u(ibx); -l(ibx)]; 0187 0188 %% grab some dimensions 0189 nlt = length(ilt); %% number of upper bounded linear inequalities 0190 ngt = length(igt); %% number of lower bounded linear inequalities 0191 nbx = length(ibx); %% number of doubly bounded linear inequalities 0192 neq = length(ieq); %% number of equalities 0193 nie = nlt+ngt+2*nbx; %% number of inequalities 0194 0195 ctype = [repmat('S', neq, 1); repmat('U', nlt+ngt+2*nbx, 1)]; 0196 vtype = repmat('C', nx, 1); 0197 0198 %% set options struct for GLPK 0199 if ~isempty(opt) && isfield(opt, 'glpk_opt') && ~isempty(opt.glpk_opt) 0200 glpk_opt = glpk_options(opt.glpk_opt); 0201 else 0202 glpk_opt = glpk_options; 0203 end 0204 glpk_opt.msglev = verbose; 0205 0206 %% call the solver 0207 [x, f, errnum, extra] = ... 0208 glpk(c, AA, bb, xmin, xmax, ctype, vtype, 1, glpk_opt); 0209 0210 %% set exit flag 0211 if isfield(extra, 'status') %% status found in extra.status 0212 output.errnum = errnum; 0213 output.status = extra.status; 0214 eflag = -errnum; 0215 if eflag == 0 && extra.status == 5 0216 eflag = 1; 0217 end 0218 else %% status found in errnum 0219 output.errnum = []; 0220 output.status = errnum; 0221 if have_feature('octave') 0222 if errnum == 180 || errnum == 151 || errnum == 171 0223 eflag = 1; 0224 else 0225 eflag = 0; 0226 end 0227 else 0228 if errnum == 5 0229 eflag = 1; 0230 else 0231 eflag = 0; 0232 end 0233 end 0234 end 0235 0236 %% repackage lambdas 0237 if isempty(extra) || ~isfield(extra, 'lambda') || isempty(extra.lambda) 0238 lambda = struct( ... 0239 'mu_l', zeros(nA, 1), ... 0240 'mu_u', zeros(nA, 1), ... 0241 'lower', zeros(nx, 1), ... 0242 'upper', zeros(nx, 1) ... 0243 ); 0244 else 0245 lam.eqlin = extra.lambda(1:neq); 0246 lam.ineqlin = extra.lambda(neq+(1:nie)); 0247 lam.lower = extra.redcosts; 0248 lam.upper = -extra.redcosts; 0249 lam.lower(lam.lower < 0) = 0; 0250 lam.upper(lam.upper < 0) = 0; 0251 kl = find(lam.eqlin > 0); %% lower bound binding 0252 ku = find(lam.eqlin < 0); %% upper bound binding 0253 0254 mu_l = zeros(nA, 1); 0255 mu_l(ieq(kl)) = lam.eqlin(kl); 0256 mu_l(igt) = -lam.ineqlin(nlt+(1:ngt)); 0257 mu_l(ibx) = -lam.ineqlin(nlt+ngt+nbx+(1:nbx)); 0258 0259 mu_u = zeros(nA, 1); 0260 mu_u(ieq(ku)) = -lam.eqlin(ku); 0261 mu_u(ilt) = -lam.ineqlin(1:nlt); 0262 mu_u(ibx) = -lam.ineqlin(nlt+ngt+(1:nbx)); 0263 0264 lambda = struct( ... 0265 'mu_l', mu_l, ... 0266 'mu_u', mu_u, ... 0267 'lower', lam.lower(1:nx), ... 0268 'upper', lam.upper(1:nx) ... 0269 ); 0270 end