ADD_VARS Adds a set of variables to the model. OM = ADD_VARS(OM, NAME, N, V0, VL, VU) OM = ADD_VARS(OM, NAME, N, V0, VL) OM = ADD_VARS(OM, NAME, N, V0) OM = ADD_VARS(OM, NAME, N) OM = ADD_VARS(OM, NAME, DIM_LIST) OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL, VU) OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL) OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0) OM = ADD_VARS(OM, NAME, IDX_LIST, N) Adds a set of variables to the model, where N is the number of variables in the set, V0 is the initial value of those variables, and VL and VU are the lower and upper bounds on the variables. The defaults for the last three arguments, which are optional, are for all values to be initialized to zero (V0 = 0) and unbounded (VL = -Inf, VU = Inf). Examples: om = add_vars(om, 'V', nb, V0, Vmin, Vmax); om = add_vars(om, 'x', {2, 3}); for i = 1:2 for j = 1:3 om = add_vars(om, 'x', {i, j}, nx(i,j), ...); end end See also OPT_MODEL, GETV.
0001 function om = add_vars(om, name, idx, varargin) 0002 %ADD_VARS Adds a set of variables to the model. 0003 % OM = ADD_VARS(OM, NAME, N, V0, VL, VU) 0004 % OM = ADD_VARS(OM, NAME, N, V0, VL) 0005 % OM = ADD_VARS(OM, NAME, N, V0) 0006 % OM = ADD_VARS(OM, NAME, N) 0007 % OM = ADD_VARS(OM, NAME, DIM_LIST) 0008 % OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL, VU) 0009 % OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL) 0010 % OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0) 0011 % OM = ADD_VARS(OM, NAME, IDX_LIST, N) 0012 % 0013 % Adds a set of variables to the model, where N is the number of 0014 % variables in the set, V0 is the initial value of those variables, 0015 % and VL and VU are the lower and upper bounds on the variables. 0016 % The defaults for the last three arguments, which are optional, 0017 % are for all values to be initialized to zero (V0 = 0) and unbounded 0018 % (VL = -Inf, VU = Inf). 0019 % 0020 % Examples: 0021 % om = add_vars(om, 'V', nb, V0, Vmin, Vmax); 0022 % 0023 % om = add_vars(om, 'x', {2, 3}); 0024 % for i = 1:2 0025 % for j = 1:3 0026 % om = add_vars(om, 'x', {i, j}, nx(i,j), ...); 0027 % end 0028 % end 0029 % 0030 % See also OPT_MODEL, GETV. 0031 0032 % MATPOWER 0033 % $Id: add_vars.m 2057 2012-06-26 19:12:12Z cvs $ 0034 % by Ray Zimmerman, PSERC Cornell 0035 % Copyright (c) 2008-2012 by Power System Engineering Research Center (PSERC) 0036 % 0037 % This file is part of MATPOWER. 0038 % See http://www.pserc.cornell.edu/matpower/ for more info. 0039 % 0040 % MATPOWER is free software: you can redistribute it and/or modify 0041 % it under the terms of the GNU General Public License as published 0042 % by the Free Software Foundation, either version 3 of the License, 0043 % or (at your option) any later version. 0044 % 0045 % MATPOWER is distributed in the hope that it will be useful, 0046 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0047 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0048 % GNU General Public License for more details. 0049 % 0050 % You should have received a copy of the GNU General Public License 0051 % along with MATPOWER. If not, see <http://www.gnu.org/licenses/>. 0052 % 0053 % Additional permission under GNU GPL version 3 section 7 0054 % 0055 % If you modify MATPOWER, or any covered work, to interface with 0056 % other modules (such as MATLAB code and MEX-files) available in a 0057 % MATLAB(R) or comparable environment containing parts covered 0058 % under other licensing terms, the licensors of MATPOWER grant 0059 % you additional permission to convey the resulting work. 0060 0061 %% set up default args 0062 if iscell(idx) 0063 if length(varargin) 0064 s1 = substruct('.', name, '()', idx); 0065 s2 = substruct('.', name, '{}', idx); 0066 0067 %% prevent duplicate named var sets 0068 if subsref(om.var.idx.i1, s1) ~= 0 0069 str = '%d'; for m = 2:length(idx), str = [str ',%d']; end 0070 nname = sprintf(['%s(' str, ')'], name, idx{:}); 0071 error('@opt_model/add_vars: variable set named ''%s'' already exists', nname); 0072 end 0073 0074 N = varargin{1}; 0075 args = { varargin{2:end} }; 0076 else %% just setting dimensions for indexed set 0077 %% prevent duplicate named var sets 0078 if isfield(om.var.idx.N, name) 0079 error('@opt_model/add_vars: variable set named ''%s'' already exists', name); 0080 end 0081 0082 N = -1; 0083 args = {}; 0084 end 0085 else 0086 %% prevent duplicate named var sets 0087 if isfield(om.var.idx.N, name) 0088 error('@opt_model/add_vars: variable set named ''%s'' already exists', name); 0089 end 0090 0091 N = idx; 0092 idx = {}; 0093 args = varargin; 0094 end 0095 nargs = length(args); 0096 0097 if N ~= -1 %% not just setting dimensions for indexed set 0098 v0 = []; vl = []; vu = []; 0099 if nargs >= 1 0100 v0 = args{1}; 0101 if nargs >= 2 0102 vl = args{2}; 0103 if nargs >= 3 0104 vu = args{3}; 0105 end 0106 end 0107 end 0108 if isempty(v0) 0109 v0 = zeros(N, 1); %% init to zero by default 0110 end 0111 if isempty(vl) 0112 vl = -Inf * ones(N, 1); %% unbounded below by default 0113 end 0114 if isempty(vu) 0115 vu = Inf * ones(N, 1); %% unbounded above by default 0116 end 0117 end 0118 0119 if isempty(idx) %% simple named set 0120 %% add info about this var set 0121 om.var.idx.i1.(name) = om.var.N + 1; %% starting index 0122 om.var.idx.iN.(name) = om.var.N + N; %% ending index 0123 om.var.idx.N.(name) = N; %% number of vars 0124 om.var.data.v0.(name) = v0; %% initial value 0125 om.var.data.vl.(name) = vl; %% lower bound 0126 om.var.data.vu.(name) = vu; %% upper bound 0127 0128 %% update number of vars and var sets 0129 om.var.N = om.var.idx.iN.(name); 0130 om.var.NS = om.var.NS + 1; 0131 0132 %% add to ordered list of var sets 0133 om.var.order(om.var.NS).name = name; 0134 om.var.order(om.var.NS).idx = {}; 0135 elseif N == -1 %% just setting dimensions for indexed set 0136 %% add info about this var set 0137 om.var.idx.i1.(name) = zeros(idx{:}); %% starting index 0138 om.var.idx.iN.(name) = zeros(idx{:}); %% ending index 0139 om.var.idx.N.(name) = zeros(idx{:}); %% number of vars 0140 om.var.data.v0.(name) = cell(idx{:}); %% initial value 0141 om.var.data.vl.(name) = cell(idx{:}); %% lower bound 0142 om.var.data.vu.(name) = cell(idx{:}); %% upper bound 0143 else %% indexed named set 0144 %% add info about this var set 0145 om.var.idx.i1 = subsasgn(om.var.idx.i1, s1, om.var.N + 1); %% starting index 0146 om.var.idx.iN = subsasgn(om.var.idx.iN, s1, om.var.N + N); %% ending index 0147 om.var.idx.N = subsasgn(om.var.idx.N, s1, N); %% number of vars 0148 om.var.data.v0 = subsasgn(om.var.data.v0, s2, v0); %% initial value 0149 om.var.data.vl = subsasgn(om.var.data.vl, s2, vl); %% lower bound 0150 om.var.data.vu = subsasgn(om.var.data.vu, s2, vu); %% upper bound 0151 0152 %% update number of vars and var sets 0153 om.var.N = subsref(om.var.idx.iN, s1); 0154 om.var.NS = om.var.NS + 1; 0155 0156 %% add to ordered list of var sets 0157 om.var.order(om.var.NS).name = name; 0158 om.var.order(om.var.NS).idx = idx; 0159 end