Could someone help me figuring out how to filter out the nonzero values and its corresponding keys in this dense array?
Thank you!
Please quote your code and post an MWE.
Here is the quoted code for a network problem that I’m working on
using JuMP
using Gurobi
supply_nodes=["i1","i2","i3"]
s=[4,3,2]
s_dict=Dict()
for i in 1:length(supply_nodes)
s_dict[supply_nodes[i]]=s[i]
end
demand_nodes=["j1","j2","j3"]
d=[3,5,1]
d_dict=Dict()
for i in 1:length(demand_nodes)
d_dict[demand_nodes[i]]=d[i]
end
dist=[1 2 3;2 1 2;3 2 1]
dist_dict=Dict()
for i in 1:length(supply_nodes)
for j in 1:length(demand_nodes)
dist_dict[supply_nodes[i],demand_nodes[j]]=dist[i,j]
end
end
Demand=5
model=Model(with_optimizer(Gurobi.Optimizer))
@variable(model,f1[supply_nodes,demand_nodes]>=0)
@constraint (model,sum(f1[i,j] for i in supply_nodes, j in demand_nodes)==Demand)
for j in demand_nodes
@constraint(model,sum(f1[i,j] for i in supply_nodes <=d_dict[j]
end
for i in supply_nodes
@constraint(model,sum(f1[i,j] for j in demand_nodes <= s_dict[i]
end
@objective(model, Min, sum(f1[i,j]*dist_dict[i,j] for i in supply_nodes, j in demand_nodes))
optimize!(model)
JuMP.value.(f1)
In this simplified case, nonzero f1 are obvious to see, but when I have a very large network, f1 could be a very large array, how could I filter out the nonzero elements of f1?