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