Home > matpower7.0 > lib > @opt_model > add_named_set.m

add_named_set

PURPOSE ^

ADD_NAMED_SET Adds a named set of variables/constraints/costs to the model.

SYNOPSIS ^

function om = add_named_set(om, set_type, name, idx, N, varargin)

DESCRIPTION ^

ADD_NAMED_SET  Adds a named set of variables/constraints/costs to the model.

   -----  PRIVATE METHOD  -----

   This method is intended to be a private method, used internally by
   the public methods ADD_VAR, ADD_LIN_CONSTRAINT, ADD_NLN_CONSTRAINT
   ADD_QUAD_COST, ADD_NLN_COST and ADD_LEGACY_COST.

   Variable Set
       OM.ADD_NAMED_SET('var', NAME, IDX_LIST, N, V0, VL, VU, VT);

   Linear Constraint Set
       OM.ADD_NAMED_SET('lin', NAME, IDX_LIST, N, A, L, U, VARSETS);

   Nonlinear Inequality Constraint Set
       OM.ADD_NAMED_SET('nle', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS);

   Nonlinear Inequality Constraint Set
       OM.ADD_NAMED_SET('nli', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS);

   Quadratic Cost Set
       OM.ADD_NAMED_SET('qdc', NAME, IDX_LIST, N, CP, VARSETS);

   General Nonlinear Cost Set
       OM.ADD_NAMED_SET('nlc', NAME, IDX_LIST, N, FCN, VARSETS);

   Legacy Cost Set
       OM.ADD_NAMED_SET('cost', NAME, IDX_LIST, N, CP, VARSETS);

   See also OPT_MODEL, ADD_VAR, ADD_LIN_CONSTRAINT, ADD_NLN_CONSTRAINT
            ADD_QUAD_COST and ADD_NLN_COST.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function om = add_named_set(om, set_type, name, idx, N, varargin)
0002 %ADD_NAMED_SET  Adds a named set of variables/constraints/costs to the model.
0003 %
0004 %   -----  PRIVATE METHOD  -----
0005 %
0006 %   This method is intended to be a private method, used internally by
0007 %   the public methods ADD_VAR, ADD_LIN_CONSTRAINT, ADD_NLN_CONSTRAINT
0008 %   ADD_QUAD_COST, ADD_NLN_COST and ADD_LEGACY_COST.
0009 %
0010 %   Variable Set
0011 %       OM.ADD_NAMED_SET('var', NAME, IDX_LIST, N, V0, VL, VU, VT);
0012 %
0013 %   Linear Constraint Set
0014 %       OM.ADD_NAMED_SET('lin', NAME, IDX_LIST, N, A, L, U, VARSETS);
0015 %
0016 %   Nonlinear Inequality Constraint Set
0017 %       OM.ADD_NAMED_SET('nle', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS);
0018 %
0019 %   Nonlinear Inequality Constraint Set
0020 %       OM.ADD_NAMED_SET('nli', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS);
0021 %
0022 %   Quadratic Cost Set
0023 %       OM.ADD_NAMED_SET('qdc', NAME, IDX_LIST, N, CP, VARSETS);
0024 %
0025 %   General Nonlinear Cost Set
0026 %       OM.ADD_NAMED_SET('nlc', NAME, IDX_LIST, N, FCN, VARSETS);
0027 %
0028 %   Legacy Cost Set
0029 %       OM.ADD_NAMED_SET('cost', NAME, IDX_LIST, N, CP, VARSETS);
0030 %
0031 %   See also OPT_MODEL, ADD_VAR, ADD_LIN_CONSTRAINT, ADD_NLN_CONSTRAINT
0032 %            ADD_QUAD_COST and ADD_NLN_COST.
0033 
0034 %   MATPOWER
0035 %   Copyright (c) 2008-2017, Power Systems Engineering Research Center (PSERC)
0036 %   by Ray Zimmerman, PSERC Cornell
0037 %
0038 %   This file is part of MATPOWER.
0039 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0040 %   See https://matpower.org for more info.
0041 
0042 %% check for valid type for named set
0043 st_label = om.valid_named_set_type(set_type);
0044 if st_label
0045     ff = set_type;
0046     om_ff = om.(ff);
0047 else
0048     error('@opt_model/add_named_set: ''%s'' is not a valid SET_TYPE, must be one of ''var'', ''lin'', ''nle'', ''nli'', ''qdc'', ''nlc'', ''cost''', set_type);
0049 end
0050 
0051 %% add general indexing info about this named set
0052 if isempty(idx)     %% simple named set
0053     %% prevent duplicate name in set of specified type
0054     if isfield(om_ff.idx.N, name)
0055         error('@opt_model/add_named_set: %s set named ''%s'' already exists', st_label, name);
0056     end
0057 
0058     %% add indexing info about this set
0059     om_ff.idx.i1.(name)  = om_ff.N + 1; %% starting index
0060     om_ff.idx.iN.(name)  = om_ff.N + N; %% ending index
0061     om_ff.idx.N.(name)   = N;           %% number in set
0062     om_ff.N  = om_ff.idx.iN.(name);     %% number of elements of this type
0063     om_ff.NS = om_ff.NS + 1;            %% number of sets of this type
0064     om_ff.order(om_ff.NS).name = name;  %% add name to ordered list of sets
0065     om_ff.order(om_ff.NS).idx  = {};
0066 else                %% indexed named set
0067     %% calls to substruct() are relatively expensive, so we pre-build the
0068     %% structs for addressing cell and numeric array fields
0069     %% sn = substruct('.', name, '()', idx);
0070     %% sc = substruct('.', name, '{}', idx);
0071     sc = struct('type', {'.', '{}'}, 'subs', {name, idx});  %% cell array field
0072     sn = sc; sn(2).type = '()';                             %% num array field
0073 
0074     %% prevent duplicate name in set of specified type
0075     if subsref(om_ff.idx.i1, sn) ~= 0
0076         str = '%d'; for m = 2:length(idx), str = [str ',%d']; end
0077         nname = sprintf(['%s(' str, ')'], name, idx{:});
0078         error('@opt_model/add_named_set: %s set named ''%s'' already exists', st_label, nname);
0079     end
0080 
0081     %% add indexing info about this set
0082     om_ff.idx.i1  = subsasgn(om_ff.idx.i1, sn, om_ff.N + 1);    %% starting index
0083     om_ff.idx.iN  = subsasgn(om_ff.idx.iN, sn, om_ff.N + N);    %% ending index
0084     om_ff.idx.N   = subsasgn(om_ff.idx.N,  sn, N);              %% number in set
0085     om_ff.N  = subsref(om_ff.idx.iN, sn);   %% number of elements of this type
0086     om_ff.NS = om_ff.NS + 1;                %% number of sets of this type
0087     om_ff.order(om_ff.NS).name = name;      %% add name to ordered list of sets
0088     om_ff.order(om_ff.NS).idx  = idx;       %% add indices to ordered list of sets
0089 end
0090 
0091 %% add type-specific data for named set
0092 switch ff
0093     case 'var'          %% variable set
0094         [v0, vl, vu, vt] = deal(varargin{:});
0095         if isempty(idx)
0096             om_ff.data.v0.(name) = v0;          %% initial value
0097             om_ff.data.vl.(name) = vl;          %% lower bound
0098             om_ff.data.vu.(name) = vu;          %% upper bound
0099             om_ff.data.vt.(name) = vt;          %% variable type
0100         else
0101             om_ff.data.v0 = subsasgn(om_ff.data.v0, sc, v0);    %% initial value
0102             om_ff.data.vl = subsasgn(om_ff.data.vl, sc, vl);    %% lower bound
0103             om_ff.data.vu = subsasgn(om_ff.data.vu, sc, vu);    %% upper bound
0104             om_ff.data.vt = subsasgn(om_ff.data.vt, sc, vt);    %% variable type
0105         end
0106     case 'lin'          %% linear constraint set
0107         [A, l, u, varsets] = deal(varargin{:});
0108         if isempty(idx)
0109             om_ff.data.A.(name)  = A;
0110             om_ff.data.l.(name)  = l;
0111             om_ff.data.u.(name)  = u;
0112             om_ff.data.vs.(name) = varsets;
0113         else
0114             om_ff.data.A  = subsasgn(om_ff.data.A, sc, A);
0115             om_ff.data.l  = subsasgn(om_ff.data.l, sc, l);
0116             om_ff.data.u  = subsasgn(om_ff.data.u, sc, u);
0117             om_ff.data.vs = subsasgn(om_ff.data.vs, sc, varsets);
0118         end
0119         if ~isempty(om_ff.params)       %% clear cache of aggregated params
0120             om_ff.params = [];
0121         end
0122     case {'nle', 'nli'} %% nonlinear constraint set
0123         [fcn, hess, computed_by, varsets] = deal(varargin{:});
0124         if isempty(idx)
0125             if isempty(computed_by)
0126                 if ~isempty(fcn)
0127                     om_ff.data.fcn.(name)  = fcn;
0128                     om_ff.data.hess.(name) = hess;
0129                 end
0130             else
0131                 if isfield(om_ff.data.include, computed_by)
0132                     om_ff.data.include.(computed_by).name{end+1} = name;
0133                     om_ff.data.include.(computed_by).N(end+1) = N;
0134                 else
0135                     om_ff.data.include.(computed_by).name = {name};
0136                     om_ff.data.include.(computed_by).N = N;
0137                 end
0138             end
0139             om_ff.data.vs.(name) = varsets;
0140         else
0141             if ~isempty(computed_by)
0142                 error('add_named_set: a nonlinear constraint set computed by another set is currently only implemented for simple named sets, not yet for indexed named sets');
0143             end
0144             om_ff.data.fcn  = subsasgn(om_ff.data.fcn, sc, fcn);
0145             om_ff.data.hess = subsasgn(om_ff.data.hess, sc, hess);
0146             om_ff.data.vs   = subsasgn(om_ff.data.vs, sc, varsets);
0147         end
0148     case 'qdc'          %% quadratic cost set
0149         [Q, c, k, varsets] = deal(varargin{:});
0150         if isempty(idx)
0151             om_ff.data.Q.(name)  = Q;
0152             om_ff.data.c.(name)  = c;
0153             om_ff.data.k.(name)  = k;
0154             om_ff.data.vs.(name) = varsets;
0155         else
0156             om_ff.data.Q  = subsasgn(om_ff.data.Q, sc, Q);
0157             om_ff.data.c  = subsasgn(om_ff.data.c, sc, c);
0158             om_ff.data.k  = subsasgn(om_ff.data.k, sc, k);
0159             om_ff.data.vs = subsasgn(om_ff.data.vs, sc, varsets);
0160         end
0161         if ~isempty(om_ff.params)       %% clear cache of aggregated params
0162             om_ff.params = [];
0163         end
0164     case 'nlc'          %% general nonlinear cost set
0165         [fcn, varsets] = deal(varargin{:});
0166         if isempty(idx)
0167             om_ff.data.fcn.(name)  = fcn;
0168             om_ff.data.vs.(name) = varsets;
0169         else
0170             om_ff.data.fcn  = subsasgn(om_ff.data.fcn, sc, fcn);
0171             om_ff.data.vs   = subsasgn(om_ff.data.vs, sc, varsets);
0172         end
0173     case 'cost'         %% cost set
0174         [cp, varsets] = deal(varargin{:});
0175         if isempty(idx)
0176             om_ff.data.N.(name)  = cp.N;
0177             om_ff.data.Cw.(name) = cp.Cw;
0178             om_ff.data.vs.(name) = varsets;
0179             if isfield(cp, 'H')
0180                 om_ff.data.H.(name)  = cp.H;
0181             end
0182             if isfield(cp, 'dd')
0183                 om_ff.data.dd.(name) = cp.dd;
0184             end
0185             if isfield(cp, 'rh')
0186                 om_ff.data.rh.(name) = cp.rh;
0187             end
0188             if isfield(cp, 'kk')
0189                 om_ff.data.kk.(name) = cp.kk;
0190             end
0191             if isfield(cp, 'mm')
0192                 om_ff.data.mm.(name) = cp.mm;
0193             end
0194         else
0195             om_ff.data.N  = subsasgn(om_ff.data.N,  sc, cp.N);
0196             om_ff.data.Cw = subsasgn(om_ff.data.Cw, sc, cp.Cw);
0197             om_ff.data.vs = subsasgn(om_ff.data.vs, sc, varsets);
0198             if isfield(cp, 'H')
0199                 om_ff.data.H = subsasgn(om_ff.data.H, sc, cp.H);
0200             end
0201             if isfield(cp, 'dd')
0202                 om_ff.data.dd = subsasgn(om_ff.data.dd, sc, cp.dd);
0203             end
0204             if isfield(cp, 'rh')
0205                 om_ff.data.rh = subsasgn(om_ff.data.rh, sc, cp.rh);
0206             end
0207             if isfield(cp, 'kk')
0208                 om_ff.data.kk = subsasgn(om_ff.data.kk, sc, cp.kk);
0209             end
0210             if isfield(cp, 'mm')
0211                 om_ff.data.mm = subsasgn(om_ff.data.mm, sc, cp.mm);
0212             end
0213         end
0214         if ~isempty(om_ff.params)       %% clear cache of aggregated params
0215             om_ff.params = [];
0216         end
0217 end
0218 om.(ff) = om_ff;

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005