ADD_LIN_CONSTRAINT Adds a set of linear constraints to the model. OM.ADD_LIN_CONSTRAINT(NAME, A, L, U); OM.ADD_LIN_CONSTRAINT(NAME, A, L, U, VARSETS); OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U); OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U, VARSETS); Linear constraints are of the form L <= A * x <= U, where x is a vector made of the vars specified in VARSETS (in the order given). This allows the A matrix to be defined only in terms of the relevant variables without the need to manually create a lot of zero columns. If VARSETS is empty, x is taken to be the full vector of all optimization variables. If L or U are empty, they are assumed to be appropriately sized vectors of -Inf and Inf, respectively. Indexed Named Sets A constraint set can be identified by a single NAME, as described above, such as 'Pmismatch', or by a name that is indexed by one or more indices, such as 'Pmismatch(3,4)'. For an indexed named set, before adding the constraint sets themselves, the dimensions of the indexed set must be set by calling INIT_INDEXED_NAME. The constraints are then added using the following, where all arguments are as described above, except IDX_LIST is a cell array of the indices for the particular constraint set being added. OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U); OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U, VARSETS); Examples: %% linear constraint om.add_lin_constraint('vl', Avl, lvl, uvl, {'Pg', 'Qg'}); %% linear constraints with indexed named set 'R(i,j)' om.init_indexed_name('lin', 'R', {2, 3}); for i = 1:2 for j = 1:3 om.add_lin_constraint('R', {i, j}, A{i,j}, ...); end end See also OPT_MODEL, PARAMS_LIN_CONSTRAINT.
0001 function om = add_lin_constraint(om, name, idx, A, l, u, varsets) 0002 %ADD_LIN_CONSTRAINT Adds a set of linear constraints to the model. 0003 % 0004 % OM.ADD_LIN_CONSTRAINT(NAME, A, L, U); 0005 % OM.ADD_LIN_CONSTRAINT(NAME, A, L, U, VARSETS); 0006 % 0007 % OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U); 0008 % OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U, VARSETS); 0009 % 0010 % Linear constraints are of the form L <= A * x <= U, where x is a 0011 % vector made of the vars specified in VARSETS (in the order given). 0012 % This allows the A matrix to be defined only in terms of the relevant 0013 % variables without the need to manually create a lot of zero columns. 0014 % If VARSETS is empty, x is taken to be the full vector of all 0015 % optimization variables. If L or U are empty, they are assumed to be 0016 % appropriately sized vectors of -Inf and Inf, respectively. 0017 % 0018 % Indexed Named Sets 0019 % A constraint set can be identified by a single NAME, as described 0020 % above, such as 'Pmismatch', or by a name that is indexed by one 0021 % or more indices, such as 'Pmismatch(3,4)'. For an indexed named 0022 % set, before adding the constraint sets themselves, the dimensions 0023 % of the indexed set must be set by calling INIT_INDEXED_NAME. 0024 % 0025 % The constraints are then added using the following, where 0026 % all arguments are as described above, except IDX_LIST is a cell 0027 % array of the indices for the particular constraint set being added. 0028 % 0029 % OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U); 0030 % OM.ADD_LIN_CONSTRAINT(NAME, IDX_LIST, A, L, U, VARSETS); 0031 % 0032 % Examples: 0033 % %% linear constraint 0034 % om.add_lin_constraint('vl', Avl, lvl, uvl, {'Pg', 'Qg'}); 0035 % 0036 % %% linear constraints with indexed named set 'R(i,j)' 0037 % om.init_indexed_name('lin', 'R', {2, 3}); 0038 % for i = 1:2 0039 % for j = 1:3 0040 % om.add_lin_constraint('R', {i, j}, A{i,j}, ...); 0041 % end 0042 % end 0043 % 0044 % See also OPT_MODEL, PARAMS_LIN_CONSTRAINT. 0045 0046 % MP-Opt-Model 0047 % Copyright (c) 2008-2020, Power Systems Engineering Research Center (PSERC) 0048 % by Ray Zimmerman, PSERC Cornell 0049 % 0050 % This file is part of MP-Opt-Model. 0051 % Covered by the 3-clause BSD License (see LICENSE file for details). 0052 % See https://github.com/MATPOWER/mp-opt-model for more info. 0053 0054 %% initialize input arguments 0055 if iscell(idx) %% indexed named set 0056 if nargin < 7 0057 varsets = {}; 0058 end 0059 else %% simple named set 0060 if nargin < 6 0061 varsets = {}; 0062 else 0063 varsets = u; 0064 end 0065 u = l; 0066 l = A; 0067 A = idx; 0068 idx = {}; 0069 end 0070 0071 [N, M] = size(A); 0072 if isempty(l) %% default l is -Inf 0073 l = -Inf(N, 1); 0074 elseif N > 1 && length(l) == 1 %% expand from scalar as needed 0075 l = l * ones(N, 1); 0076 end 0077 if isempty(u) %% default u is Inf 0078 u = Inf(N, 1); 0079 elseif N > 1 && length(u) == 1 %% expand from scalar as needed 0080 u = u * ones(N, 1); 0081 end 0082 0083 %% check sizes 0084 if size(l, 1) ~= N || size(u, 1) ~= N 0085 error('@opt_model/add_lin_constraint: sizes of A, l and u must match'); 0086 end 0087 0088 %% convert varsets from cell to struct array if necessary 0089 varsets = om.varsets_cell2struct(varsets); 0090 nv = om.varsets_len(varsets); %% number of variables 0091 0092 %% check consistency of varsets with size of A 0093 if M ~= nv 0094 error('@opt_model/add_lin_constraint: number of columns of A does not match\nnumber of variables, A is %d x %d, nv = %d\n', N, M, nv); 0095 end 0096 0097 %% add the named linear constraint set 0098 om.add_named_set('lin', name, idx, N, A, l, u, varsets);