Home > matpower5.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 %   $Id: linear_constraints.m 2137 2013-03-29 18:52:50Z ray $
0016 %   by Ray Zimmerman, PSERC Cornell
0017 %   Copyright (c) 2008-2012 by Power System Engineering Research Center (PSERC)
0018 %
0019 %   This file is part of MATPOWER.
0020 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0021 %
0022 %   MATPOWER is free software: you can redistribute it and/or modify
0023 %   it under the terms of the GNU General Public License as published
0024 %   by the Free Software Foundation, either version 3 of the License,
0025 %   or (at your option) any later version.
0026 %
0027 %   MATPOWER is distributed in the hope that it will be useful,
0028 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0029 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0030 %   GNU General Public License for more details.
0031 %
0032 %   You should have received a copy of the GNU General Public License
0033 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0034 %
0035 %   Additional permission under GNU GPL version 3 section 7
0036 %
0037 %   If you modify MATPOWER, or any covered work, to interface with
0038 %   other modules (such as MATLAB code and MEX-files) available in a
0039 %   MATLAB(R) or comparable environment containing parts covered
0040 %   under other licensing terms, the licensors of MATPOWER grant
0041 %   you additional permission to convey the resulting work.
0042 
0043 
0044 %% initialize A, l and u
0045 nnzA = 0;
0046 for k = 1:om.lin.NS
0047     name = om.lin.order(k).name;
0048     idx  = om.lin.order(k).idx;
0049     if isempty(idx)
0050         nnzA = nnzA + nnz(om.lin.data.A.(name));
0051     else
0052         s = substruct('.', name, '{}', idx);
0053         nnzA = nnzA + nnz(subsref(om.lin.data.A, s));
0054     end
0055 end
0056 At = sparse([], [], [], om.var.N, om.lin.N, nnzA);  %% use A transpose for speed
0057 u = Inf * ones(om.lin.N, 1);
0058 l = -u;
0059 
0060 %% fill in each piece
0061 for k = 1:om.lin.NS
0062     name = om.lin.order(k).name;
0063     idx  = om.lin.order(k).idx;
0064     if isempty(idx)
0065         N = om.lin.idx.N.(name);
0066     else
0067         s1 = substruct('.', name, '()', idx);
0068         s2 = substruct('.', name, '{}', idx);
0069         N = subsref(om.lin.idx.N, s1);
0070     end
0071     if N                                %% non-zero number of rows to add
0072         if isempty(idx)
0073             Ak = om.lin.data.A.(name);          %% A for kth linear constrain set
0074             i1 = om.lin.idx.i1.(name);          %% starting row index
0075             iN = om.lin.idx.iN.(name);          %% ending row index
0076             vsl = om.lin.data.vs.(name);        %% var set list
0077         else
0078             Ak = subsref(om.lin.data.A, s2);    %% A for kth linear constrain set
0079             i1 = subsref(om.lin.idx.i1, s1);    %% starting row index
0080             iN = subsref(om.lin.idx.iN, s1);    %% ending row index
0081             vsl = subsref(om.lin.data.vs, s2);  %% var set list
0082         end
0083         if isempty(vsl)         %% full rows
0084             if size(Ak,2) == om.var.N
0085                 At(:, i1:iN) = Ak';     %% assign as columns in transpose for speed
0086             else                %% must have added vars since adding
0087                                 %% this constraint set
0088                 At(1:size(Ak,2), i1:iN) = Ak';  %% assign as columns in transpose for speed
0089             end
0090         else                    %% selected columns
0091             kN = 0;                             %% initialize last col of Ak used
0092             Ai = sparse(N, om.var.N);
0093             for v = 1:length(vsl)
0094                 s = substruct('.', vsl(v).name, '()', vsl(v).idx);
0095                 j1 = subsref(om.var.idx.i1, s); %% starting column in A
0096                 jN = subsref(om.var.idx.iN, s); %% ending column in A
0097                 k1 = kN + 1;                    %% starting column in Ak
0098                 kN = kN + subsref(om.var.idx.N, s);%% ending column in Ak
0099                 Ai(:, j1:jN) = Ak(:, k1:kN);
0100             end
0101             At(:, i1:iN) = Ai';     %% assign as columns in transpose for speed
0102         end
0103 
0104         if isempty(idx)
0105             l(i1:iN) = om.lin.data.l.(name);
0106             u(i1:iN) = om.lin.data.u.(name);
0107         else
0108             l(i1:iN) = subsref(om.lin.data.l, s2);
0109             u(i1:iN) = subsref(om.lin.data.u, s2);
0110         end
0111     end
0112 end
0113 A = At';

Generated on Mon 26-Jan-2015 15:21:31 by m2html © 2005