Home > matpower6.0 > @opt_model > linear_constraints.m

linear_constraints

PURPOSE ^

LINEAR_CONSTRAINTS Builds and returns the full set of linear constraints.

SYNOPSIS ^

function [A, l, u] = linear_constraints(om)

DESCRIPTION ^

LINEAR_CONSTRAINTS  Builds and returns the full set of linear constraints.
   [A, L, U] = LINEAR_CONSTRAINTS(OM)
   Builds the full set of linear constraints based on those added by
   ADD_CONSTRAINTS.

       L <= A * x <= U

   Example:
       [A, l, u] = linear_constraints(om);

   See also OPT_MODEL, ADD_CONSTRAINTS.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [A, l, u] = linear_constraints(om)
0002 %LINEAR_CONSTRAINTS  Builds and returns the full set of linear constraints.
0003 %   [A, L, U] = LINEAR_CONSTRAINTS(OM)
0004 %   Builds the full set of linear constraints based on those added by
0005 %   ADD_CONSTRAINTS.
0006 %
0007 %       L <= A * x <= U
0008 %
0009 %   Example:
0010 %       [A, l, u] = linear_constraints(om);
0011 %
0012 %   See also OPT_MODEL, ADD_CONSTRAINTS.
0013 
0014 %   MATPOWER
0015 %   Copyright (c) 2008-2016, Power Systems Engineering Research Center (PSERC)
0016 %   by Ray Zimmerman, PSERC Cornell
0017 %
0018 %   This file is part of MATPOWER.
0019 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0020 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0021 
0022 
0023 %% initialize A, l and u
0024 nnzA = 0;
0025 s = struct('type', {'.', '{}'}, 'subs', {'', 1});
0026 for k = 1:om.lin.NS
0027     name = om.lin.order(k).name;
0028     idx  = om.lin.order(k).idx;
0029     if isempty(idx)
0030         nnzA = nnzA + nnz(om.lin.data.A.(name));
0031     else
0032         % (calls to substruct() are relatively expensive ...
0033         % s = substruct('.', name, '{}', idx);
0034         % ... so replace it with these more efficient lines)
0035         s(1).subs = name;
0036         s(2).subs = idx;
0037         nnzA = nnzA + nnz(subsref(om.lin.data.A, s));
0038     end
0039 end
0040 At = sparse([], [], [], om.var.N, om.lin.N, nnzA);  %% use A transpose for speed
0041 u = Inf(om.lin.N, 1);
0042 l = -u;
0043 
0044 %% fill in each piece
0045 s2 = s;
0046 s(2).type = '()';
0047 s1 = s;
0048 for k = 1:om.lin.NS
0049     name = om.lin.order(k).name;
0050     idx  = om.lin.order(k).idx;
0051     if isempty(idx)
0052         N = om.lin.idx.N.(name);
0053     else
0054         % (calls to substruct() are relatively expensive ...
0055         % s1 = substruct('.', name, '()', idx);
0056         % s2 = substruct('.', name, '{}', idx);
0057         % ... so replace them with these more efficient lines)
0058         s1(1).subs = name;
0059         s1(2).subs = idx;
0060         s2(1).subs = name;
0061         s2(2).subs = idx;
0062         N = subsref(om.lin.idx.N, s1);
0063     end
0064     if N                                %% non-zero number of rows to add
0065         if isempty(idx)
0066             Ak = om.lin.data.A.(name);          %% A for kth linear constrain set
0067             i1 = om.lin.idx.i1.(name);          %% starting row index
0068             iN = om.lin.idx.iN.(name);          %% ending row index
0069             vsl = om.lin.data.vs.(name);        %% var set list
0070         else
0071             Ak = subsref(om.lin.data.A, s2);    %% A for kth linear constrain set
0072             i1 = subsref(om.lin.idx.i1, s1);    %% starting row index
0073             iN = subsref(om.lin.idx.iN, s1);    %% ending row index
0074             vsl = subsref(om.lin.data.vs, s2);  %% var set list
0075         end
0076         if isempty(vsl)         %% full rows
0077             if size(Ak,2) == om.var.N
0078                 At(:, i1:iN) = Ak';     %% assign as columns in transpose for speed
0079             else                %% must have added vars since adding
0080                                 %% this constraint set
0081                 At(1:size(Ak,2), i1:iN) = Ak';  %% assign as columns in transpose for speed
0082             end
0083         else                    %% selected columns
0084             kN = 0;                             %% initialize last col of Ak used
0085             Ai = sparse(N, om.var.N);
0086             for v = 1:length(vsl)
0087                 % (calls to substruct() are relatively expensive ...
0088                 % s = substruct('.', vsl(v).name, '()', vsl(v).idx);
0089                 % ... so replace it with these more efficient lines)
0090                 s(1).subs = vsl(v).name;
0091                 s(2).subs = vsl(v).idx;
0092                 j1 = subsref(om.var.idx.i1, s); %% starting column in A
0093                 jN = subsref(om.var.idx.iN, s); %% ending column in A
0094                 k1 = kN + 1;                    %% starting column in Ak
0095                 kN = kN + subsref(om.var.idx.N, s);%% ending column in Ak
0096                 Ai(:, j1:jN) = Ak(:, k1:kN);
0097             end
0098             At(:, i1:iN) = Ai';     %% assign as columns in transpose for speed
0099         end
0100 
0101         if isempty(idx)
0102             l(i1:iN) = om.lin.data.l.(name);
0103             u(i1:iN) = om.lin.data.u.(name);
0104         else
0105             l(i1:iN) = subsref(om.lin.data.l, s2);
0106             u(i1:iN) = subsref(om.lin.data.u, s2);
0107         end
0108     end
0109 end
0110 A = At';

Generated on Fri 16-Dec-2016 12:45:37 by m2html © 2005