0001 function om = add_named_set(om, set_type, name, idx, N, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
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
0052 if isempty(idx)
0053
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
0059 om_ff.idx.i1.(name) = om_ff.N + 1;
0060 om_ff.idx.iN.(name) = om_ff.N + N;
0061 om_ff.idx.N.(name) = N;
0062 om_ff.N = om_ff.idx.iN.(name);
0063 om_ff.NS = om_ff.NS + 1;
0064 om_ff.order(om_ff.NS).name = name;
0065 om_ff.order(om_ff.NS).idx = {};
0066 else
0067
0068
0069
0070
0071 sc = struct('type', {'.', '{}'}, 'subs', {name, idx});
0072 sn = sc; sn(2).type = '()';
0073
0074
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
0082 om_ff.idx.i1 = subsasgn(om_ff.idx.i1, sn, om_ff.N + 1);
0083 om_ff.idx.iN = subsasgn(om_ff.idx.iN, sn, om_ff.N + N);
0084 om_ff.idx.N = subsasgn(om_ff.idx.N, sn, N);
0085 om_ff.N = subsref(om_ff.idx.iN, sn);
0086 om_ff.NS = om_ff.NS + 1;
0087 om_ff.order(om_ff.NS).name = name;
0088 om_ff.order(om_ff.NS).idx = idx;
0089 end
0090
0091
0092 switch ff
0093 case 'var'
0094 [v0, vl, vu, vt] = deal(varargin{:});
0095 if isempty(idx)
0096 om_ff.data.v0.(name) = v0;
0097 om_ff.data.vl.(name) = vl;
0098 om_ff.data.vu.(name) = vu;
0099 om_ff.data.vt.(name) = vt;
0100 else
0101 om_ff.data.v0 = subsasgn(om_ff.data.v0, sc, v0);
0102 om_ff.data.vl = subsasgn(om_ff.data.vl, sc, vl);
0103 om_ff.data.vu = subsasgn(om_ff.data.vu, sc, vu);
0104 om_ff.data.vt = subsasgn(om_ff.data.vt, sc, vt);
0105 end
0106 case 'lin'
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)
0120 om_ff.params = [];
0121 end
0122 case {'nle', 'nli'}
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'
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)
0162 om_ff.params = [];
0163 end
0164 case 'nlc'
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'
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)
0215 om_ff.params = [];
0216 end
0217 end
0218 om.(ff) = om_ff;