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