Home > matpower7.1 > mp-opt-model > lib > @opt_model > add_var.m

add_var

PURPOSE ^

ADD_VAR Adds a set of variables to the model.

SYNOPSIS ^

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

DESCRIPTION ^

ADD_VAR  Adds a set of variables to the model.
   OM.ADD_VAR(NAME, N, V0, VL, VU, VT)
   OM.ADD_VAR(NAME, N, V0, VL, VU)
   OM.ADD_VAR(NAME, N, V0, VL)
   OM.ADD_VAR(NAME, N, V0)
   OM.ADD_VAR(NAME, N)
   OM.ADD_VAR(NAME, DIM_LIST) (deprecated, use INIT_INDEXED_NAME instead)
   OM.ADD_VAR(NAME, IDX_LIST, N, V0, VL, VU, VT)
   OM.ADD_VAR(NAME, IDX_LIST, N, V0, VL, VU)
   OM.ADD_VAR(NAME, IDX_LIST, N, V0, VL)
   OM.ADD_VAR(NAME, IDX_LIST, N, V0)
   OM.ADD_VAR(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_var('V', nb, V0, Vmin, Vmax, 'C');

       om.init_indexed_name('x', {2, 3});
       for i = 1:2
         for j = 1:3
           om.add_var('x', {i, j}, nx(i,j), ...);
         end
       end

   See also OPT_MODEL, PARAMS_VAR.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function om = add_var(om, name, idx, varargin)
0002 %ADD_VAR  Adds a set of variables to the model.
0003 %   OM.ADD_VAR(NAME, N, V0, VL, VU, VT)
0004 %   OM.ADD_VAR(NAME, N, V0, VL, VU)
0005 %   OM.ADD_VAR(NAME, N, V0, VL)
0006 %   OM.ADD_VAR(NAME, N, V0)
0007 %   OM.ADD_VAR(NAME, N)
0008 %   OM.ADD_VAR(NAME, DIM_LIST) (deprecated, use INIT_INDEXED_NAME instead)
0009 %   OM.ADD_VAR(NAME, IDX_LIST, N, V0, VL, VU, VT)
0010 %   OM.ADD_VAR(NAME, IDX_LIST, N, V0, VL, VU)
0011 %   OM.ADD_VAR(NAME, IDX_LIST, N, V0, VL)
0012 %   OM.ADD_VAR(NAME, IDX_LIST, N, V0)
0013 %   OM.ADD_VAR(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_var('V', nb, V0, Vmin, Vmax, 'C');
0029 %
0030 %       om.init_indexed_name('x', {2, 3});
0031 %       for i = 1:2
0032 %         for j = 1:3
0033 %           om.add_var('x', {i, j}, nx(i,j), ...);
0034 %         end
0035 %       end
0036 %
0037 %   See also OPT_MODEL, PARAMS_VAR.
0038 
0039 %   MP-Opt-Model
0040 %   Copyright (c) 2008-2020, Power Systems Engineering Research Center (PSERC)
0041 %   by Ray Zimmerman, PSERC Cornell
0042 %
0043 %   This file is part of MP-Opt-Model.
0044 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0045 %   See https://github.com/MATPOWER/mp-opt-model for more info.
0046 
0047 %% set up default args
0048 if iscell(idx) && isempty(varargin) %% just setting dimensions for indexed set
0049     om.init_indexed_name('var', name, idx);
0050 else
0051     if iscell(idx)
0052         N = varargin{1};
0053         args = varargin(2:end);
0054     else
0055         N = idx;
0056         idx = {};
0057         args = varargin;
0058     end
0059     nargs = length(args);
0060     
0061     v0 = []; vl = []; vu = []; vt = [];
0062     if nargs >= 1
0063         v0 = args{1};
0064         if N > 1 && length(v0) == 1         %% expand from scalar as needed
0065             v0 = v0 * ones(N, 1);
0066         end
0067         if nargs >= 2
0068             vl = args{2};
0069             if N > 1 && length(vl) == 1     %% expand from scalar as needed
0070                 vl = vl * ones(N, 1);
0071             end
0072             if nargs >= 3
0073                 vu = args{3};
0074                 if N > 1 && length(vu) == 1 %% expand from scalar as needed
0075                     vu = vu * ones(N, 1);
0076                 end
0077                 if nargs >= 4
0078                     vt = args{4};
0079                 end
0080             end
0081         end
0082     end
0083     if isempty(v0)
0084         v0 = zeros(N, 1);   %% init to zero by default
0085     end
0086     if isempty(vl)
0087         vl = -Inf(N, 1);    %% unbounded below by default
0088     end
0089     if isempty(vu)
0090         vu = Inf(N, 1);     %% unbounded above by default
0091     end
0092     if isempty(vt) && N > 0
0093         vt = 'C';           %% all continuous by default
0094     end
0095 
0096     %% add the named variable set
0097     om.add_named_set('var', name, idx, N, v0, vl, vu, vt);
0098 end

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005