Home > matpower7.0 > lib > qps_clp.m

qps_clp

PURPOSE ^

QPS_CLP Quadratic Program Solver based on CLP - COIN-OR Linear Programming.

SYNOPSIS ^

function [x, f, eflag, output, lambda] = qps_clp(H, c, A, l, u, xmin, xmax, x0, opt)

DESCRIPTION ^

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 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_clp(H, c, A, l, u, xmin, [], x0, opt);

   See also CLP.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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 https://v8doc.sas.com/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-2016, Power Systems Engineering Research Center (PSERC)
0097 %   by Ray Zimmerman, PSERC Cornell
0098 %
0099 %   This file is part of MATPOWER.
0100 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0101 %   See https://matpower.org for more info.
0102 
0103 %% check for Optimization Toolbox
0104 % if ~have_fcn('quadprog')
0105 %     error('qps_clp: requires the MEX interface to CLP');
0106 % end
0107 
0108 %%----- input argument handling  -----
0109 %% gather inputs
0110 if nargin == 1 && isstruct(H)       %% problem struct
0111     p = H;
0112     if isfield(p, 'opt'),   opt = p.opt;    else,   opt = [];   end
0113     if isfield(p, 'x0'),    x0 = p.x0;      else,   x0 = [];    end
0114     if isfield(p, 'xmax'),  xmax = p.xmax;  else,   xmax = [];  end
0115     if isfield(p, 'xmin'),  xmin = p.xmin;  else,   xmin = [];  end
0116     if isfield(p, 'u'),     u = p.u;        else,   u = [];     end
0117     if isfield(p, 'l'),     l = p.l;        else,   l = [];     end
0118     if isfield(p, 'A'),     A = p.A;        else,   A = [];     end
0119     if isfield(p, 'c'),     c = p.c;        else,   c = [];     end
0120     if isfield(p, 'H'),     H = p.H;        else,   H = [];     end
0121 else                                %% individual args
0122     if nargin < 9
0123         opt = [];
0124         if nargin < 8
0125             x0 = [];
0126             if nargin < 7
0127                 xmax = [];
0128                 if nargin < 6
0129                     xmin = [];
0130                 end
0131             end
0132         end
0133     end
0134 end
0135 
0136 %% define nx, set default values for missing optional inputs
0137 if isempty(H) || ~any(any(H))
0138     if isempty(A) && isempty(xmin) && isempty(xmax)
0139         error('qps_clp: LP problem must include constraints or variable bounds');
0140     else
0141         if ~isempty(A)
0142             nx = size(A, 2);
0143         elseif ~isempty(xmin)
0144             nx = length(xmin);
0145         else    % if ~isempty(xmax)
0146             nx = length(xmax);
0147         end
0148     end
0149 else
0150     nx = size(H, 1);
0151 end
0152 if isempty(c)
0153     c = zeros(nx, 1);
0154 end
0155 if isempty(A) || (~isempty(A) && (isempty(l) || all(l == -Inf)) && ...
0156                                  (isempty(u) || all(u == Inf)))
0157     A = sparse(0,nx);           %% no limits => no linear constraints
0158 end
0159 nA = size(A, 1);                %% number of original linear constraints
0160 if isempty(u)                   %% By default, linear inequalities are ...
0161     u = Inf(nA, 1);             %% ... unbounded above and ...
0162 end
0163 if isempty(l)
0164     l = -Inf(nA, 1);            %% ... unbounded below.
0165 end
0166 if isempty(xmin)                %% By default, optimization variables are ...
0167     xmin = -Inf(nx, 1);         %% ... unbounded below and ...
0168 end
0169 if isempty(xmax)
0170     xmax = Inf(nx, 1);          %% ... unbounded above.
0171 end
0172 if isempty(x0)
0173     x0 = zeros(nx, 1);
0174 end
0175 if ~issparse(A)
0176     A = sparse(A);
0177 end
0178 if ~issparse(H)
0179     H = sparse(H);
0180 end
0181 
0182 
0183 %% default options
0184 if ~isempty(opt) && isfield(opt, 'verbose') && ~isempty(opt.verbose)
0185     verbose = opt.verbose;
0186 else
0187     verbose = 0;
0188 end
0189 
0190 %% set up options struct for CLP
0191 if ~isempty(opt) && isfield(opt, 'clp_opt') && ~isempty(opt.clp_opt)
0192     clp_opt = clp_options(opt.clp_opt);
0193 else
0194     clp_opt = clp_options;
0195 end
0196 
0197 if have_fcn('opti_clp')     %% use OPTI Toolbox verision's MEX interface
0198     clp_opt.display = verbose;
0199 
0200     [x, f, exitflag, iter, lam] = clp(tril(H), c, A, l, u, xmin, xmax, clp_opt);
0201     
0202     output.iter = iter;
0203 
0204     %% repackage lambdas
0205 %     if isempty(x)
0206 %         x = NaN(nx, 1);
0207 %         f = NaN;
0208 %     end
0209 %     if isempty(lam)
0210 %         lambda = struct( ...
0211 %             'mu_l', zeros(nA, 1), ...
0212 %             'mu_u', zeros(nA, 1), ...
0213 %             'lower', zeros(nx, 1), ...
0214 %             'upper', zeros(nx, 1) ...
0215 %         );
0216 %     else
0217         mu_l = lam.dual_row;
0218         mu_u = -lam.dual_row;
0219         lower = lam.dual_col;
0220         upper = -lam.dual_col;
0221         mu_l(mu_l < 0) = 0;
0222         mu_u(mu_u < 0) = 0;
0223         lower(lower < 0) = 0;
0224         upper(upper < 0) = 0;
0225         
0226         lambda = struct( ...
0227             'mu_l', mu_l, ...
0228             'mu_u', mu_u, ...
0229             'lower', lower, ...
0230             'upper', upper ...
0231         );
0232 %     end
0233 else
0234     clp_opt.verbose = verbose;
0235 
0236     %% split up linear constraints
0237     ieq = find( abs(u-l) <= eps );          %% equality
0238     igt = find( u >=  1e10 & l > -1e10 );   %% greater than, unbounded above
0239     ilt = find( l <= -1e10 & u <  1e10 );   %% less than, unbounded below
0240     ibx = find( (abs(u-l) > eps) & (u < 1e10) & (l > -1e10) );
0241     Ae = A(ieq, :);
0242     be = u(ieq);
0243     Ai  = [ A(ilt, :); -A(igt, :); A(ibx, :); -A(ibx, :) ];
0244     bi  = [ u(ilt);    -l(igt);    u(ibx);    -l(ibx)];
0245 
0246     %% grab some dimensions
0247     nlt = length(ilt);      %% number of upper bounded linear inequalities
0248     ngt = length(igt);      %% number of lower bounded linear inequalities
0249     nbx = length(ibx);      %% number of doubly bounded linear inequalities
0250 
0251     %% call the solver
0252     [x, z, exitflag] = ...
0253         clp(H, c, Ai, bi, Ae, be, xmin, xmax, clp_opt);
0254 
0255     %% repackage lambdas
0256     if isempty(x)
0257         x = NaN(nx, 1);
0258         f = NaN;
0259     else
0260         if isempty(H) || ~any(any(H))
0261             f = c'*x;
0262         else
0263             f = 0.5 * x'*H*x + c'*x;
0264         end
0265     end
0266     if isempty(z)
0267         lambda = struct( ...
0268             'mu_l', zeros(nA, 1), ...
0269             'mu_u', zeros(nA, 1), ...
0270             'lower', zeros(nx, 1), ...
0271             'upper', zeros(nx, 1) ...
0272         );
0273     else
0274         neq = length(be);
0275         nie = length(bi);
0276         lam.eqlin = z(1:neq);
0277         lam.ineqlin = z(neq+(1:nie));
0278     %%-----  MEXCLP DOES NOT RETURN MULTIPLIERS ON VARIABLE BOUNDS :-/  -----
0279         lam.lower = NaN(nx, 1);
0280         lam.upper = NaN(nx, 1);
0281         kl = find(lam.eqlin > 0);   %% lower bound binding
0282         ku = find(lam.eqlin < 0);   %% upper bound binding
0283 
0284         mu_l = zeros(nA, 1);
0285         mu_l(ieq(kl)) = lam.eqlin(kl);
0286         mu_l(igt) = -lam.ineqlin(nlt+(1:ngt));
0287         mu_l(ibx) = -lam.ineqlin(nlt+ngt+nbx+(1:nbx));
0288 
0289         mu_u = zeros(nA, 1);
0290         mu_u(ieq(ku)) = -lam.eqlin(ku);
0291         mu_u(ilt) = -lam.ineqlin(1:nlt);
0292         mu_u(ibx) = -lam.ineqlin(nlt+ngt+(1:nbx));
0293 
0294         lambda = struct( ...
0295             'mu_l', mu_l, ...
0296             'mu_u', mu_u, ...
0297             'lower', lam.lower, ...
0298             'upper', lam.upper ...
0299         );
0300     end
0301 end
0302 
0303 %% set eflag
0304 eflag = -exitflag;
0305 if eflag == 0       %% success
0306     eflag = 1;
0307 end
0308 
0309 %% set status
0310 output.exitflag = exitflag;
0311 switch exitflag
0312     case 0
0313         output.status = 'optimal';
0314     case 1
0315         output.status = 'primal infeasible';
0316     case 2
0317         output.status = 'dual infeasible';
0318     case 3
0319         output.status = 'max iterations or time exceeded';
0320     otherwise
0321         output.status = 'unknown exit code';
0322 end

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005