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