Home > matpower5.1 > @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-2015 by Power System Engineering Research Center (PSERC)
0041 %   by Ray Zimmerman, PSERC Cornell
0042 %
0043 %   $Id: add_vars.m 2644 2015-03-11 19:34:22Z ray $
0044 %
0045 %   This file is part of MATPOWER.
0046 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0047 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0048 
0049 %% set up default args
0050 if iscell(idx)
0051     if ~isempty(varargin)
0052         % (calls to substruct() are relatively expensive ...
0053         % s1 = substruct('.', name, '()', idx);
0054         % s2 = substruct('.', name, '{}', idx);
0055         % ... so replace them with these more efficient lines)
0056         s1 = struct('type', {'.', '()'}, 'subs', {name, idx});
0057         s2 = s1;
0058         s2(2).type = '{}';
0059 
0060         %% prevent duplicate named var sets
0061         if subsref(om.var.idx.i1, s1) ~= 0
0062             str = '%d'; for m = 2:length(idx), str = [str ',%d']; end
0063             nname = sprintf(['%s(' str, ')'], name, idx{:});
0064             error('@opt_model/add_vars: variable set named ''%s'' already exists', nname);
0065         end
0066 
0067         N = varargin{1};
0068         args = varargin(2:end);
0069     else        %% just setting dimensions for indexed set
0070         %% prevent duplicate named var sets
0071         if isfield(om.var.idx.N, name)
0072             error('@opt_model/add_vars: variable set named ''%s'' already exists', name);
0073         end
0074 
0075         N = -1;
0076         args = {};
0077     end
0078 else
0079     %% prevent duplicate named var sets
0080     if isfield(om.var.idx.N, name)
0081         error('@opt_model/add_vars: variable set named ''%s'' already exists', name);
0082     end
0083 
0084     N = idx;
0085     idx = {};
0086     args = varargin;
0087 end
0088 nargs = length(args);
0089 
0090 if N ~= -1      %% not just setting dimensions for indexed set
0091     v0 = []; vl = []; vu = []; vt = [];
0092     if nargs >= 1
0093         v0 = args{1};
0094         if nargs >= 2
0095             vl = args{2};
0096             if nargs >= 3
0097                 vu = args{3};
0098                 if nargs >= 4
0099                     vt = args{4};
0100                 end
0101             end
0102         end
0103     end
0104     if isempty(v0)
0105         v0 = zeros(N, 1);   %% init to zero by default
0106     end
0107     if isempty(vl)
0108         vl = -Inf(N, 1);    %% unbounded below by default
0109     end
0110     if isempty(vu)
0111         vu = Inf(N, 1);     %% unbounded above by default
0112     end
0113     if isempty(vt) && N > 0
0114         vt = 'C';           %% all continuous by default
0115     end
0116 end
0117 
0118 if isempty(idx)     %% simple named set
0119     %% add info about this var set
0120     om.var.idx.i1.(name)  = om.var.N + 1;   %% starting index
0121     om.var.idx.iN.(name)  = om.var.N + N;   %% ending index
0122     om.var.idx.N.(name)   = N;              %% number of vars
0123     om.var.data.v0.(name) = v0;             %% initial value
0124     om.var.data.vl.(name) = vl;             %% lower bound
0125     om.var.data.vu.(name) = vu;             %% upper bound
0126     om.var.data.vt.(name) = vt;             %% variable type
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     om.var.data.vt.(name) = cell(idx{:});   %% variable type
0144 else                %% indexed named set
0145     %% add info about this var set
0146     om.var.idx.i1  = subsasgn(om.var.idx.i1, s1, om.var.N + 1); %% starting index
0147     om.var.idx.iN  = subsasgn(om.var.idx.iN, s1, om.var.N + N); %% ending index
0148     om.var.idx.N   = subsasgn(om.var.idx.N,  s1, N);            %% number of vars
0149     om.var.data.v0 = subsasgn(om.var.data.v0, s2, v0);          %% initial value
0150     om.var.data.vl = subsasgn(om.var.data.vl, s2, vl);          %% lower bound
0151     om.var.data.vu = subsasgn(om.var.data.vu, s2, vu);          %% upper bound
0152     om.var.data.vt = subsasgn(om.var.data.vt, s2, vt);          %% variable type
0153     
0154     %% update number of vars and var sets
0155     om.var.N  = subsref(om.var.idx.iN, s1);
0156     om.var.NS = om.var.NS + 1;
0157     
0158     %% add to ordered list of var sets
0159     om.var.order(om.var.NS).name = name;
0160     om.var.order(om.var.NS).idx  = idx;
0161 end

Generated on Fri 20-Mar-2015 18:23:34 by m2html © 2005