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

add_vars

PURPOSE ^

ADD_VARS Adds a set of variables to the model.

SYNOPSIS ^

function om = add_vars(om, name, idx, varargin)

DESCRIPTION ^

ADD_VARS  Adds a set of variables to the model.
   OM = ADD_VARS(OM, NAME, N, V0, VL, VU, VT)
   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, VT)
   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,
   VL and VU are the lower and upper bounds on the variables and VT
   is the variable type. The accepted values for elements of VT are:
       'C' - continuous
       'I' - integer
       'B' - binary
   V0, VL and VU are N x 1 column vectors, VT is a scalar or a 1 x N row
   vector. The defaults for the last four arguments, which are all optional,
   are for all values to be initialized to zero (V0 = 0), unbounded
   (VL = -Inf, VU = Inf), and continuous (VT = 'C').

   Examples:
       om = add_vars(om, 'V', nb, V0, Vmin, Vmax, 'C');

       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.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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, VT)
0004 %   OM = ADD_VARS(OM, NAME, N, V0, VL, VU)
0005 %   OM = ADD_VARS(OM, NAME, N, V0, VL)
0006 %   OM = ADD_VARS(OM, NAME, N, V0)
0007 %   OM = ADD_VARS(OM, NAME, N)
0008 %   OM = ADD_VARS(OM, NAME, DIM_LIST)
0009 %   OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL, VU, VT)
0010 %   OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL, VU)
0011 %   OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0, VL)
0012 %   OM = ADD_VARS(OM, NAME, IDX_LIST, N, V0)
0013 %   OM = ADD_VARS(OM, NAME, IDX_LIST, N)
0014 %
0015 %   Adds a set of variables to the model, where N is the number of
0016 %   variables in the set, V0 is the initial value of those variables,
0017 %   VL and VU are the lower and upper bounds on the variables and VT
0018 %   is the variable type. The accepted values for elements of VT are:
0019 %       'C' - continuous
0020 %       'I' - integer
0021 %       'B' - binary
0022 %   V0, VL and VU are N x 1 column vectors, VT is a scalar or a 1 x N row
0023 %   vector. The defaults for the last four arguments, which are all optional,
0024 %   are for all values to be initialized to zero (V0 = 0), unbounded
0025 %   (VL = -Inf, VU = Inf), and continuous (VT = 'C').
0026 %
0027 %   Examples:
0028 %       om = add_vars(om, 'V', nb, V0, Vmin, Vmax, 'C');
0029 %
0030 %       om = add_vars(om, 'x', {2, 3});
0031 %       for i = 1:2
0032 %         for j = 1:3
0033 %           om = add_vars(om, 'x', {i, j}, nx(i,j), ...);
0034 %         end
0035 %       end
0036 %
0037 %   See also OPT_MODEL, GETV.
0038 
0039 %   MATPOWER
0040 %   Copyright (c) 2008-2016, Power Systems Engineering Research Center (PSERC)
0041 %   by Ray Zimmerman, PSERC Cornell
0042 %
0043 %   This file is part of MATPOWER.
0044 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0045 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0046 
0047 %% set up default args
0048 if iscell(idx)
0049     if ~isempty(varargin)
0050         % (calls to substruct() are relatively expensive ...
0051         % s1 = substruct('.', name, '()', idx);
0052         % s2 = substruct('.', name, '{}', idx);
0053         % ... so replace them with these more efficient lines)
0054         s1 = struct('type', {'.', '()'}, 'subs', {name, idx});
0055         s2 = s1;
0056         s2(2).type = '{}';
0057 
0058         %% prevent duplicate named var sets
0059         if subsref(om.var.idx.i1, s1) ~= 0
0060             str = '%d'; for m = 2:length(idx), str = [str ',%d']; end
0061             nname = sprintf(['%s(' str, ')'], name, idx{:});
0062             error('@opt_model/add_vars: variable set named ''%s'' already exists', nname);
0063         end
0064 
0065         N = varargin{1};
0066         args = varargin(2:end);
0067     else        %% just setting dimensions for indexed set
0068         %% prevent duplicate named var sets
0069         if isfield(om.var.idx.N, name)
0070             error('@opt_model/add_vars: variable set named ''%s'' already exists', name);
0071         end
0072 
0073         N = -1;
0074         args = {};
0075     end
0076 else
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 = idx;
0083     idx = {};
0084     args = varargin;
0085 end
0086 nargs = length(args);
0087 
0088 if N ~= -1      %% not just setting dimensions for indexed set
0089     v0 = []; vl = []; vu = []; vt = [];
0090     if nargs >= 1
0091         v0 = args{1};
0092         if nargs >= 2
0093             vl = args{2};
0094             if nargs >= 3
0095                 vu = args{3};
0096                 if nargs >= 4
0097                     vt = args{4};
0098                 end
0099             end
0100         end
0101     end
0102     if isempty(v0)
0103         v0 = zeros(N, 1);   %% init to zero by default
0104     end
0105     if isempty(vl)
0106         vl = -Inf(N, 1);    %% unbounded below by default
0107     end
0108     if isempty(vu)
0109         vu = Inf(N, 1);     %% unbounded above by default
0110     end
0111     if isempty(vt) && N > 0
0112         vt = 'C';           %% all continuous by default
0113     end
0114 end
0115 
0116 if isempty(idx)     %% simple named set
0117     %% add info about this var set
0118     om.var.idx.i1.(name)  = om.var.N + 1;   %% starting index
0119     om.var.idx.iN.(name)  = om.var.N + N;   %% ending index
0120     om.var.idx.N.(name)   = N;              %% number of vars
0121     om.var.data.v0.(name) = v0;             %% initial value
0122     om.var.data.vl.(name) = vl;             %% lower bound
0123     om.var.data.vu.(name) = vu;             %% upper bound
0124     om.var.data.vt.(name) = vt;             %% variable type
0125     
0126     %% update number of vars and var sets
0127     om.var.N  = om.var.idx.iN.(name);
0128     om.var.NS = om.var.NS + 1;
0129     
0130     %% add to ordered list of var sets
0131     om.var.order(om.var.NS).name = name;
0132     om.var.order(om.var.NS).idx  = {};
0133 elseif N == -1      %% just setting dimensions for indexed set
0134     %% use column vector if single dimension
0135     if length(idx) == 1
0136         idx = {idx{:}, 1};
0137     end
0138     
0139     %% add info about this var set
0140     om.var.idx.i1.(name)  = zeros(idx{:});  %% starting index
0141     om.var.idx.iN.(name)  = zeros(idx{:});  %% ending index
0142     om.var.idx.N.(name)   = zeros(idx{:});  %% number of vars
0143     om.var.data.v0.(name) = cell(idx{:});   %% initial value
0144     om.var.data.vl.(name) = cell(idx{:});   %% lower bound
0145     om.var.data.vu.(name) = cell(idx{:});   %% upper bound
0146     om.var.data.vt.(name) = cell(idx{:});   %% variable type
0147 else                %% indexed named set
0148     %% add info about this var set
0149     om.var.idx.i1  = subsasgn(om.var.idx.i1, s1, om.var.N + 1); %% starting index
0150     om.var.idx.iN  = subsasgn(om.var.idx.iN, s1, om.var.N + N); %% ending index
0151     om.var.idx.N   = subsasgn(om.var.idx.N,  s1, N);            %% number of vars
0152     om.var.data.v0 = subsasgn(om.var.data.v0, s2, v0);          %% initial value
0153     om.var.data.vl = subsasgn(om.var.data.vl, s2, vl);          %% lower bound
0154     om.var.data.vu = subsasgn(om.var.data.vu, s2, vu);          %% upper bound
0155     om.var.data.vt = subsasgn(om.var.data.vt, s2, vt);          %% variable type
0156     
0157     %% update number of vars and var sets
0158     om.var.N  = subsref(om.var.idx.iN, s1);
0159     om.var.NS = om.var.NS + 1;
0160     
0161     %% add to ordered list of var sets
0162     om.var.order(om.var.NS).name = name;
0163     om.var.order(om.var.NS).idx  = idx;
0164 end

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