Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/+mpecopt/Solver.m
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,8 @@ function create_mpec_functions(obj)
G_curr = mpec.G.(name)();
H_curr = mpec.H.(name)();

[ind_scalar_G,ind_nonscalar_G, ind_map_G] = find_nonscalar(G_curr, mpec.w.sym);
[ind_scalar_H,ind_nonscalar_H, ind_map_H] = find_nonscalar(H_curr, mpec.w.sym);
[ind_scalar_G,ind_nonscalar_G, ind_map_G] = find_nonscalar(G_curr, mpec.w.sym, mpec.p.sym);
[ind_scalar_H,ind_nonscalar_H, ind_map_H] = find_nonscalar(H_curr, mpec.w.sym, mpec.p.sym);

mpec.w.([name '_G_lift']) = {{'G', length(ind_nonscalar_G)}, 0, inf};
G_lift = G_curr(ind_nonscalar_G);
Expand Down Expand Up @@ -1000,8 +1000,8 @@ function create_mpec_functions(obj)

% TODO(@anton) this is in an if for performance reasons not to call find_nonscalar many times
% however it is likely that this can be done in 1 shot?
[ind_scalar_G,ind_nonscalar_G, ind_map_G] = find_nonscalar(G_curr, mpec.w.sym);
[ind_scalar_H,ind_nonscalar_H, ind_map_H] = find_nonscalar(H_curr, mpec.w.sym);
[ind_scalar_G,ind_nonscalar_G, ind_map_G] = find_nonscalar(G_curr, mpec.w.sym, mpec.p.sym);
[ind_scalar_H,ind_nonscalar_H, ind_map_H] = find_nonscalar(H_curr, mpec.w.sym, mpec.p.sym);

mpec.w.([name '_G_lift'])(curr{:}) = {{'G', length(ind_nonscalar_G)}, 0, inf};
G_lift = G_curr(ind_nonscalar_G);
Expand Down Expand Up @@ -1134,7 +1134,7 @@ function create_mpec_functions(obj)
else
% lifting with only those that are not scaler
% define lift vairables
[ind_scalar,ind_nonscalar_x1, ind_map] = find_nonscalar(G,x);
[ind_scalar,ind_nonscalar_x1, ind_map] = find_nonscalar(G,x,p);
n_lift_x1 = length(ind_nonscalar_x1);
if n_lift_x1 == 0
% TODO(@anton) Figure out what this does.
Expand Down Expand Up @@ -1170,7 +1170,7 @@ function create_mpec_functions(obj)
g = [g;x2-H];
else
% lifting with only those that are not scaler
[ind_scalar,ind_nonscalar_x2, ind_map] = find_nonscalar(H,x);
[ind_scalar,ind_nonscalar_x2, ind_map] = find_nonscalar(H,x,p);
n_lift_x2 = length(ind_nonscalar_x2);
if n_lift_x2 == 0
% TODO(@anton) Figure out what this does.
Expand Down
14 changes: 9 additions & 5 deletions src/find_nonscalar.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
function [ind_scalar,ind_nonscalar, ind_map] = find_nonscalar(g,w)
function [ind_scalar,ind_nonscalar, ind_map] = find_nonscalar(g,w,p)
% Implemented by Anton Pozharskiy within nosnoc: https://github.com/nosnoc/nosnoc
% This function returns the indices of g which contain elements of the symbolic vector w, along with its
% nonscalar indicies and the ind_map which are the indices in w which correspond to scalar elements of g.
% nonscalar indicies and the ind_map which are the indices in w which correspond to scalar elements of g.
if ~exist('p')
p = SX([]);
end
import casadi.*
% Take jacobian of g with respect to w.
ind_g_fun = Function('ind_G', {w}, {g.jacobian(w)});
ind_g_fun = Function('ind_G', {w,p}, {g.jacobian(w)});
p_val = SX(ones(length(p),1));
% HERE BE DRAGONS:
% `ind_g_fun(w) == 1` creates a symbolic array where literal `1` are ones, other constants are zeros and
% symbolics are nans when converted to a casadi.DM.
%
% We want to find rows which contain only exactly one nonstructural 0, 1, or nan.
% We get this from using find on the sparsity patern of the DM.
sp = DM(ind_g_fun(w) == 1).sparsity;
sp = DM(ind_g_fun(w,p) == 1).sparsity;
sub = sp.find;
[ind_g1, ind_g2] = ind2sub(size(ind_g_fun(w)),sub);
[ind_g1, ind_g2] = ind2sub(size(ind_g_fun(w,p)),sub);
% transpose because groupcounts expects column vector
c=groupcounts(ind_g1');
ind_scalar=ind_g1(find(c==1));
Expand Down