Skip to content

Commit

Permalink
Fix several issues in new implementation of params_quad_cost()
Browse files Browse the repository at this point in the history
- Q specified as column vector was not being handled
- varsets were only being used to order columns, but they need to order rows as well
- c is kept as dense vector
  • Loading branch information
rdzman committed Jul 9, 2019
1 parent 1d2dd93 commit 1318651
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/@opt_model/params_quad_cost.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,48 @@

% columns 1,2 are sub indices i,j; column 3 is nonzero values
Q_SparseInds = cell(om.qdc.NS,3);
c_SparseInds = cell(om.qdc.NS,3);

for i = 1:om.qdc.NS
name = om.qdc.order(i).name;
idx = om.qdc.order(i).idx;
[Qk, ck, kk, vs] = om.params_quad_cost(name, idx);
haveQ = ~isempty(Qk);
havec = ~isempty(ck);

nk = max(size(Qk, 1), size(ck, 1)); %% size of Qk and/or ck
if isempty(vs)
% vars added since adding this cost set
if haveQ %% Qk is a matrix
if size(Qk, 2) == 1 %% Qk is a column vector
[rowInds,colInds,nonzeroVals] = find(Qk);
Q_SparseInds(i,:) = {rowInds, rowInds, nonzeroVals};
elseif haveQ %% Qk is a matrix
[rowInds,colInds,nonzeroVals] = find(Qk);
Q_SparseInds(i,:) = {rowInds, colInds, nonzeroVals};
end
if havec
[rowInds,colInds,nonzeroVals] = find(ck);
c_SparseInds(i,:) = {rowInds, colInds, nonzeroVals};
if nk == nx %% full size
ck_full = ck;
else %% vars added since adding this cost set
ck_full = zeros(nx, 1);
ck_full(1:nk) = ck;
end
end
else
jj = om.varsets_idx(vs)'; %% indices for var set
if haveQ
% find nonzero sub indices and values
if size(Qk, 2) == 1 %% Qk is a column vector
[rowInds,colInds,nonzeroVals] = find(Qk);
Q_SparseInds(i,:) = {jj(rowInds), jj(rowInds), nonzeroVals};
elseif haveQ %% Qk is a matrix
[rowInds,colInds,nonzeroVals] = find(Qk);
% jj indices map to full matrix
Q_SparseInds(i,:) = {rowInds, jj(colInds), nonzeroVals};
Q_SparseInds(i,:) = {jj(rowInds), jj(colInds), nonzeroVals};
end
if havec
[rowInds,colInds,nonzeroVals] = find(ck);
c_SparseInds(i,:) = {jj(rowInds), colInds, nonzeroVals};
ck_full = zeros(nx, 1);
ck_full(jj) = ck;
end
end
if havec
c = c + ck_full;
end
k = k + sum(kk);
end

Expand All @@ -113,12 +123,6 @@
% https://www.mathworks.com/help/matlab/ref/sparse.html
Q = sparse(I, J, nonzeroValues, nx, nx);

I = vertcat(c_SparseInds{:,1});
J = vertcat(c_SparseInds{:,2});
nonzeroValues = vertcat(c_SparseInds{:,3});

c = sparse(I, J, nonzeroValues, nx, 1);

%% cache aggregated parameters
om.qdc.params = struct('Q', Q, 'c', c, 'k', k);
else %% return cached values
Expand Down

0 comments on commit 1318651

Please sign in to comment.