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