0001 function om = build_cost_params(om, force)
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 if nargin > 1 || ~isfield(om.cost.params, 'N')
0045
0046 nw = om.cost.N;
0047 nnzN = 0;
0048 nnzH = 0;
0049 for k = 1:om.cost.NS
0050 name = om.cost.order(k).name;
0051 idx = om.cost.order(k).idx;
0052 if isempty(idx)
0053 nnzN = nnzN + nnz(om.cost.data.N.(name));
0054 if isfield(om.cost.data.H, name)
0055 nnzH = nnzH + nnz(om.cost.data.H.(name));
0056 end
0057 else
0058 s = substruct('.', name, '{}', idx);
0059 nnzN = nnzN + nnz(subsref(om.cost.data.N, s));
0060 if isfield(om.cost.data.H, name)
0061 nnzH = nnzH + nnz(subsref(om.cost.data.H, s));
0062 end
0063 end
0064 end
0065 NNt = sparse([], [], [], om.var.N, nw, nnzN);
0066 Cw = zeros(nw, 1);
0067 H = sparse([], [], [], nw, nw, nnzH);
0068 dd = ones(nw, 1);
0069 rh = Cw;
0070 kk = Cw;
0071 mm = dd;
0072
0073
0074 for k = 1:om.cost.NS
0075 name = om.cost.order(k).name;
0076 idx = om.cost.order(k).idx;
0077 if isempty(idx)
0078 N = om.cost.idx.N.(name);
0079 else
0080 s1 = substruct('.', name, '()', idx);
0081 s2 = substruct('.', name, '{}', idx);
0082 N = subsref(om.cost.idx.N, s1);
0083 end
0084 if N
0085 if isempty(idx)
0086 Nk = om.cost.data.N.(name);
0087 i1 = om.cost.idx.i1.(name);
0088 iN = om.cost.idx.iN.(name);
0089 vsl = om.cost.data.vs.(name);
0090 else
0091 Nk = subsref(om.cost.data.N, s2);
0092 i1 = subsref(om.cost.idx.i1, s1);
0093 iN = subsref(om.cost.idx.iN, s1);
0094 vsl = subsref(om.cost.data.vs, s2);
0095 end
0096 if isempty(vsl)
0097 if size(Nk,2) == om.var.N
0098 NNt(:, i1:iN) = Nk';
0099 else
0100
0101 NNt(1:size(Nk,2), i1:iN) = Nk';
0102 end
0103 else
0104 kN = 0;
0105 Ni = sparse(N, om.var.N);
0106 for v = 1:length(vsl)
0107 s = substruct('.', vsl(v).name, '()', vsl(v).idx);
0108 j1 = subsref(om.var.idx.i1, s);
0109 jN = subsref(om.var.idx.iN, s);
0110 k1 = kN + 1;
0111 kN = kN + subsref(om.var.idx.N, s);
0112 Ni(:, j1:jN) = Nk(:, k1:kN);
0113 end
0114 NNt(:, i1:iN) = Ni';
0115 end
0116
0117 if isempty(idx)
0118 Cw(i1:iN) = om.cost.data.Cw.(name);
0119 if isfield(om.cost.data.H, name)
0120 H(i1:iN, i1:iN) = om.cost.data.H.(name);
0121 end
0122 if isfield(om.cost.data.dd, name)
0123 dd(i1:iN) = om.cost.data.dd.(name);
0124 end
0125 if isfield(om.cost.data.rh, name)
0126 rh(i1:iN) = om.cost.data.rh.(name);
0127 end
0128 if isfield(om.cost.data.kk, name)
0129 kk(i1:iN) = om.cost.data.kk.(name);
0130 end
0131 if isfield(om.cost.data.mm, name)
0132 mm(i1:iN) = om.cost.data.mm.(name);
0133 end
0134 else
0135 Cw(i1:iN) = subsref(om.cost.data.Cw, s2);
0136 if isfield(om.cost.data.H, name) && ~isempty(subsref(om.cost.data.H, s2))
0137 H(i1:iN, i1:iN) = subsref(om.cost.data.H, s2);
0138 end
0139 if isfield(om.cost.data.dd, name) && ~isempty(subsref(om.cost.data.dd, s2))
0140 dd(i1:iN) = subsref(om.cost.data.dd, s2);
0141 end
0142 if isfield(om.cost.data.rh, name) && ~isempty(subsref(om.cost.data.rh, s2))
0143 rh(i1:iN) = subsref(om.cost.data.rh, s2);
0144 end
0145 if isfield(om.cost.data.kk, name) && ~isempty(subsref(om.cost.data.kk, s2))
0146 kk(i1:iN) = subsref(om.cost.data.kk, s2);
0147 end
0148 if isfield(om.cost.data.mm, name) && ~isempty(subsref(om.cost.data.mm, s2))
0149 mm(i1:iN) = subsref(om.cost.data.mm, s2);
0150 end
0151 end
0152 end
0153 end
0154
0155
0156 om.cost.params = struct( ...
0157 'N', NNt', 'Cw', Cw, 'H', H, 'dd', dd, 'rh', rh, 'kk', kk, 'mm', mm );
0158 end