ADD_NAMED_SET Adds a named set of a particular type to the object. ----- PRIVATE METHOD ----- This method is intended to be a private method, used internally by the public methods for each set type, e.g. ADD_VAR, ADD_LIN_CONSTRAINT. This method handles the indexing part. The set-type-specific methods that call it need to handle any data that goes with each set added. E.g. Variable Set OBJ.ADD_NAMED_SET('var', NAME, IDX_LIST, N, V0, VL, VU, VT); Linear Constraint Set OBJ.ADD_NAMED_SET('lin', NAME, IDX_LIST, N, A, L, U, VARSETS); Nonlinear Inequality Constraint Set OBJ.ADD_NAMED_SET('nle', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS); Nonlinear Inequality Constraint Set OBJ.ADD_NAMED_SET('nli', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS); Quadratic Cost Set OBJ.ADD_NAMED_SET('qdc', NAME, IDX_LIST, N, CP, VARSETS); General Nonlinear Cost Set OBJ.ADD_NAMED_SET('nlc', NAME, IDX_LIST, N, FCN, VARSETS); See also OPT_MODEL and its methods ADD_VAR, ADD_LIN_CONSTRAINT, ADD_NLN_CONSTRAINT, ADD_QUAD_COST and ADD_NLN_COST.
0001 function obj = add_named_set(obj, set_type, name, idx, N, varargin) 0002 %ADD_NAMED_SET Adds a named set of a particular type to the object. 0003 % 0004 % ----- PRIVATE METHOD ----- 0005 % 0006 % This method is intended to be a private method, used internally by 0007 % the public methods for each set type, e.g. ADD_VAR, ADD_LIN_CONSTRAINT. 0008 % This method handles the indexing part. The set-type-specific methods 0009 % that call it need to handle any data that goes with each set added. 0010 % 0011 % E.g. 0012 % 0013 % Variable Set 0014 % OBJ.ADD_NAMED_SET('var', NAME, IDX_LIST, N, V0, VL, VU, VT); 0015 % 0016 % Linear Constraint Set 0017 % OBJ.ADD_NAMED_SET('lin', NAME, IDX_LIST, N, A, L, U, VARSETS); 0018 % 0019 % Nonlinear Inequality Constraint Set 0020 % OBJ.ADD_NAMED_SET('nle', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS); 0021 % 0022 % Nonlinear Inequality Constraint Set 0023 % OBJ.ADD_NAMED_SET('nli', NAME, IDX_LIST, N, FCN, HESS, COMPUTED_BY, VARSETS); 0024 % 0025 % Quadratic Cost Set 0026 % OBJ.ADD_NAMED_SET('qdc', NAME, IDX_LIST, N, CP, VARSETS); 0027 % 0028 % General Nonlinear Cost Set 0029 % OBJ.ADD_NAMED_SET('nlc', NAME, IDX_LIST, N, FCN, VARSETS); 0030 % 0031 % See also OPT_MODEL and its methods ADD_VAR, ADD_LIN_CONSTRAINT, 0032 % ADD_NLN_CONSTRAINT, ADD_QUAD_COST and ADD_NLN_COST. 0033 0034 % MP-Opt-Model 0035 % Copyright (c) 2008-2020, Power Systems Engineering Research Center (PSERC) 0036 % by Ray Zimmerman, PSERC Cornell 0037 % 0038 % This file is part of MP-Opt-Model. 0039 % Covered by the 3-clause BSD License (see LICENSE file for details). 0040 % See https://github.com/MATPOWER/mp-opt-model for more info. 0041 0042 %% check for valid type for named set 0043 st_label = obj.valid_named_set_type(set_type); 0044 if st_label 0045 ff = set_type; 0046 obj_ff = obj.(ff); 0047 obj.(ff) = []; 0048 else 0049 ff = fieldnames(obj.set_types); 0050 stypes = sprintf('\n ''%s''', ff{:}); 0051 error('@mp_idx_manager/add_named_set: ''%s'' is not a valid SET_TYPE, must be one of the following:%s', set_type, stypes); 0052 end 0053 0054 %% add general indexing info about this named set 0055 if isempty(idx) %% simple named set 0056 %% prevent duplicate name in set of specified type 0057 if isfield(obj_ff.idx.N, name) 0058 error('@mp_idx_manager/add_named_set: %s set named ''%s'' already exists', st_label, name); 0059 end 0060 0061 %% add indexing info about this set 0062 obj_ff.idx.i1.(name) = obj_ff.N + 1; %% starting index 0063 obj_ff.idx.iN.(name) = obj_ff.N + N; %% ending index 0064 obj_ff.idx.N.(name) = N; %% number in set 0065 obj_ff.N = obj_ff.idx.iN.(name); %% number of elements of this type 0066 obj_ff.NS = obj_ff.NS + 1; %% number of sets of this type 0067 obj_ff.order(obj_ff.NS).name = name; %% add name to ordered list of sets 0068 obj_ff.order(obj_ff.NS).idx = {}; 0069 else %% indexed named set 0070 %% calls to substruct() are relatively expensive, so we pre-build the 0071 %% struct for addressing numeric array fields 0072 %% sn = substruct('.', name, '()', idx); 0073 sn = struct('type', {'.', '()'}, 'subs', {name, idx}); %% num array field 0074 0075 %% prevent duplicate name in set of specified type 0076 if subsref(obj_ff.idx.i1, sn) ~= 0 0077 str = '%d'; for m = 2:length(idx), str = [str ',%d']; end 0078 nname = sprintf(['%s(' str, ')'], name, idx{:}); 0079 error('@mp_idx_manager/add_named_set: %s set named ''%s'' already exists', st_label, nname); 0080 end 0081 0082 %% add indexing info about this set 0083 obj_ff.idx.i1 = subsasgn(obj_ff.idx.i1, sn, obj_ff.N + 1); %% starting index 0084 obj_ff.idx.iN = subsasgn(obj_ff.idx.iN, sn, obj_ff.N + N); %% ending index 0085 obj_ff.idx.N = subsasgn(obj_ff.idx.N, sn, N); %% number in set 0086 obj_ff.N = subsref(obj_ff.idx.iN, sn); %% number of elements of this type 0087 obj_ff.NS = obj_ff.NS + 1; %% number of sets of this type 0088 obj_ff.order(obj_ff.NS).name = name; %% add name to ordered list of sets 0089 obj_ff.order(obj_ff.NS).idx = idx; %% add indices to ordered list of sets 0090 end 0091 0092 obj.(ff) = obj_ff;