Home > matpower7.1 > mp-opt-model > 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 and ADD_NLN_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);

   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 and ADD_NLN_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 %   See also OPT_MODEL, ADD_VAR, ADD_LIN_CONSTRAINT, ADD_NLN_CONSTRAINT
0026 %            ADD_QUAD_COST and ADD_NLN_COST.
0027 
0028 %   MP-Opt-Model
0029 %   Copyright (c) 2008-2020, Power Systems Engineering Research Center (PSERC)
0030 %   by Ray Zimmerman, PSERC Cornell
0031 %
0032 %   This file is part of MP-Opt-Model.
0033 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0034 %   See https://github.com/MATPOWER/mp-opt-model for more info.
0035 
0036 %% call parent method (also checks for valid type for named set)
0037 om = add_named_set@mp_idx_manager(om, set_type, name, idx, N, varargin{:});
0038 
0039 if ~isempty(idx)
0040     %% calls to substruct() are relatively expensive, so we pre-build the
0041     %% struct for addressing cell array fields
0042     %% sc = substruct('.', name, '{}', idx);
0043     sc = struct('type', {'.', '{}'}, 'subs', {name, idx});  %% cell array field
0044 end
0045 
0046 %% add type-specific data for named set
0047 om_ff = om.(set_type);
0048 om.(set_type) = [];
0049 switch set_type
0050     case 'var'          %% variable set
0051         [v0, vl, vu, vt] = deal(varargin{:});
0052         if isempty(idx)
0053             om_ff.data.v0.(name) = v0;          %% initial value
0054             om_ff.data.vl.(name) = vl;          %% lower bound
0055             om_ff.data.vu.(name) = vu;          %% upper bound
0056             om_ff.data.vt.(name) = vt;          %% variable type
0057         else
0058             om_ff.data.v0 = subsasgn(om_ff.data.v0, sc, v0);    %% initial value
0059             om_ff.data.vl = subsasgn(om_ff.data.vl, sc, vl);    %% lower bound
0060             om_ff.data.vu = subsasgn(om_ff.data.vu, sc, vu);    %% upper bound
0061             om_ff.data.vt = subsasgn(om_ff.data.vt, sc, vt);    %% variable type
0062         end
0063     case 'lin'          %% linear constraint set
0064         [A, l, u, varsets] = deal(varargin{:});
0065         if isempty(idx)
0066             om_ff.data.A.(name)  = A;
0067             om_ff.data.l.(name)  = l;
0068             om_ff.data.u.(name)  = u;
0069             om_ff.data.vs.(name) = varsets;
0070         else
0071             om_ff.data.A  = subsasgn(om_ff.data.A, sc, A);
0072             om_ff.data.l  = subsasgn(om_ff.data.l, sc, l);
0073             om_ff.data.u  = subsasgn(om_ff.data.u, sc, u);
0074             om_ff.data.vs = subsasgn(om_ff.data.vs, sc, varsets);
0075         end
0076         if ~isempty(om_ff.params)       %% clear cache of aggregated params
0077             om_ff.params = [];
0078         end
0079     case {'nle', 'nli'} %% nonlinear constraint set
0080         [fcn, hess, computed_by, varsets] = deal(varargin{:});
0081         if isempty(idx)
0082             if isempty(computed_by)
0083                 if ~isempty(fcn)
0084                     om_ff.data.fcn.(name)  = fcn;
0085                     om_ff.data.hess.(name) = hess;
0086                 end
0087             else
0088                 if isfield(om_ff.data.include, computed_by)
0089                     om_ff.data.include.(computed_by).name{end+1} = name;
0090                     om_ff.data.include.(computed_by).N(end+1) = N;
0091                 else
0092                     om_ff.data.include.(computed_by).name = {name};
0093                     om_ff.data.include.(computed_by).N = N;
0094                 end
0095             end
0096             om_ff.data.vs.(name) = varsets;
0097         else
0098             if ~isempty(computed_by)
0099                 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');
0100             end
0101             om_ff.data.fcn  = subsasgn(om_ff.data.fcn, sc, fcn);
0102             om_ff.data.hess = subsasgn(om_ff.data.hess, sc, hess);
0103             om_ff.data.vs   = subsasgn(om_ff.data.vs, sc, varsets);
0104         end
0105     case 'qdc'          %% quadratic cost set
0106         [Q, c, k, varsets] = deal(varargin{:});
0107         if isempty(idx)
0108             om_ff.data.Q.(name)  = Q;
0109             om_ff.data.c.(name)  = c;
0110             om_ff.data.k.(name)  = k;
0111             om_ff.data.vs.(name) = varsets;
0112         else
0113             om_ff.data.Q  = subsasgn(om_ff.data.Q, sc, Q);
0114             om_ff.data.c  = subsasgn(om_ff.data.c, sc, c);
0115             om_ff.data.k  = subsasgn(om_ff.data.k, sc, k);
0116             om_ff.data.vs = subsasgn(om_ff.data.vs, sc, varsets);
0117         end
0118         if ~isempty(om_ff.params)       %% clear cache of aggregated params
0119             om_ff.params = [];
0120         end
0121     case 'nlc'          %% general nonlinear cost set
0122         [fcn, varsets] = deal(varargin{:});
0123         if isempty(idx)
0124             om_ff.data.fcn.(name)  = fcn;
0125             om_ff.data.vs.(name) = varsets;
0126         else
0127             om_ff.data.fcn  = subsasgn(om_ff.data.fcn, sc, fcn);
0128             om_ff.data.vs   = subsasgn(om_ff.data.vs, sc, varsets);
0129         end
0130 end
0131 om.(set_type) = om_ff;

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