0001 function om = add_costs(om, name, idx, 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
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 if iscell(idx)
0077 if ~isempty(varargin)
0078
0079
0080
0081
0082 s1 = struct('type', {'.', '()'}, 'subs', {name, idx});
0083 s2 = s1;
0084 s2(2).type = '{}';
0085
0086
0087 if subsref(om.cost.idx.i1, s1) ~= 0
0088 str = '%d'; for m = 2:length(idx), str = [str ',%d']; end
0089 nname = sprintf(['%s(' str, ')'], name, idx{:});
0090 error('@opt_model/add_costs: cost set named ''%s'' already exists', nname);
0091 end
0092
0093 cp = varargin{1};
0094 args = varargin(2:end);
0095 else
0096
0097 if isfield(om.cost.idx.N, name)
0098 error('@opt_model/add_costs: cost set named ''%s'' already exists', name);
0099 end
0100
0101 cp = [];
0102 args = {};
0103 end
0104 else
0105
0106 if isfield(om.cost.idx.N, name)
0107 error('@opt_model/add_costs: cost set named ''%s'' already exists', name);
0108 end
0109
0110 cp = idx;
0111 args = varargin;
0112 idx = {};
0113 end
0114
0115 if isempty(cp)
0116
0117 om.cost.idx.i1.(name) = zeros(idx{:});
0118 om.cost.idx.iN.(name) = zeros(idx{:});
0119 om.cost.idx.N.(name) = zeros(idx{:});
0120 om.cost.data.N.(name) = cell(idx{:});
0121 om.cost.data.Cw.(name) = cell(idx{:});
0122 om.cost.data.vs.(name) = cell(idx{:});
0123 else
0124 if isempty(args)
0125 varsets = {};
0126 else
0127 varsets = args{1};
0128 end
0129 if ~isempty(varsets) && iscell(varsets)
0130 empty_cells = cell(1, length(varsets));
0131 [empty_cells{:}] = deal({});
0132 varsets = struct('name', varsets, 'idx', empty_cells);
0133 end
0134 if isfield(cp, 'N')
0135 [nw, nx] = size(cp.N);
0136 else
0137 nw = length(cp.Cw);
0138 nx = nw;
0139 cp.N = speye(nw, nx);
0140 end
0141
0142
0143 if isempty(varsets)
0144 nv = om.var.N;
0145 else
0146 nv = 0;
0147 s = struct('type', {'.', '()'}, 'subs', {'', 1});
0148 for k = 1:length(varsets)
0149
0150
0151
0152 s(1).subs = varsets(k).name;
0153 s(2).subs = varsets(k).idx;
0154 nv = nv + subsref(om.var.idx.N, s);
0155 end
0156 end
0157 if nx ~= nv
0158 if nw == 0
0159 cp.N = sparse(nw, nx);
0160 else
0161 error('@opt_model/add_costs: number of columns in N (%d x %d) does not match\nnumber of variables (%d)\n', nw, nx, nv);
0162 end
0163 end
0164 if size(cp.Cw, 1) ~= nw
0165 error('@opt_model/add_costs: number of rows of Cw (%d x %d) and N (%d x %d) must match\n', size(cp.Cw), nw, nx);
0166 end
0167 if isfield(cp, 'H') && (size(cp.H, 1) ~= nw || size(cp.H, 2) ~= nw)
0168 error('@opt_model/add_costs: both dimensions of H (%d x %d) must match the number of rows in N (%d x %d)\n', size(cp.H), nw, nx);
0169 end
0170 if isfield(cp, 'dd') && size(cp.dd, 1) ~= nw
0171 error('@opt_model/add_costs: number of rows of dd (%d x %d) and N (%d x %d) must match\n', size(cp.dd), nw, nx);
0172 end
0173 if isfield(cp, 'rh') && size(cp.rh, 1) ~= nw
0174 error('@opt_model/add_costs: number of rows of rh (%d x %d) and N (%d x %d) must match\n', size(cp.rh), nw, nx);
0175 end
0176 if isfield(cp, 'kk') && size(cp.kk, 1) ~= nw
0177 error('@opt_model/add_costs: number of rows of kk (%d x %d) and N (%d x %d) must match\n', size(cp.kk), nw, nx);
0178 end
0179 if isfield(cp, 'mm') && size(cp.mm, 1) ~= nw
0180 error('@opt_model/add_costs: number of rows of mm (%d x %d) and N (%d x %d) must match\n', size(cp.mm), nw, nx);
0181 end
0182
0183 if isempty(idx)
0184
0185 om.cost.idx.i1.(name) = om.cost.N + 1;
0186 om.cost.idx.iN.(name) = om.cost.N + nw;
0187 om.cost.idx.N.(name) = nw;
0188 om.cost.data.N.(name) = cp.N;
0189 om.cost.data.Cw.(name) = cp.Cw;
0190 om.cost.data.vs.(name) = varsets;
0191 if isfield(cp, 'H')
0192 om.cost.data.H.(name) = cp.H;
0193 end
0194 if isfield(cp, 'dd')
0195 om.cost.data.dd.(name) = cp.dd;
0196 end
0197 if isfield(cp, 'rh')
0198 om.cost.data.rh.(name) = cp.rh;
0199 end
0200 if isfield(cp, 'kk')
0201 om.cost.data.kk.(name) = cp.kk;
0202 end
0203 if isfield(cp, 'mm')
0204 om.cost.data.mm.(name) = cp.mm;
0205 end
0206
0207
0208 om.cost.N = om.cost.idx.iN.(name);
0209 om.cost.NS = om.cost.NS + 1;
0210
0211
0212 om.cost.order(om.cost.NS).name = name;
0213 om.cost.order(om.cost.NS).idx = {};
0214 else
0215
0216 om.cost.idx.i1 = subsasgn(om.cost.idx.i1, s1, om.cost.N + 1);
0217 om.cost.idx.iN = subsasgn(om.cost.idx.iN, s1, om.cost.N + nw);
0218 om.cost.idx.N = subsasgn(om.cost.idx.N, s1, nw);
0219
0220 om.cost.data.N = subsasgn(om.cost.data.N, s2, cp.N);
0221 om.cost.data.Cw = subsasgn(om.cost.data.Cw, s2, cp.Cw);
0222 om.cost.data.vs = subsasgn(om.cost.data.vs, s2, varsets);
0223 if isfield(cp, 'H')
0224 om.cost.data.H = subsasgn(om.cost.data.H, s2, cp.H);
0225 end
0226 if isfield(cp, 'dd')
0227 om.cost.data.dd = subsasgn(om.cost.data.dd, s2, cp.dd);
0228 end
0229 if isfield(cp, 'rh')
0230 om.cost.data.rh = subsasgn(om.cost.data.rh, s2, cp.rh);
0231 end
0232 if isfield(cp, 'kk')
0233 om.cost.data.kk = subsasgn(om.cost.data.kk, s2, cp.kk);
0234 end
0235 if isfield(cp, 'mm')
0236 om.cost.data.mm = subsasgn(om.cost.data.mm, s2, cp.mm);
0237 end
0238
0239
0240 om.cost.N = subsref(om.cost.idx.iN, s1);
0241 om.cost.NS = om.cost.NS + 1;
0242
0243
0244 om.cost.order(om.cost.NS).name = name;
0245 om.cost.order(om.cost.NS).idx = idx;
0246 end
0247 end