Set-indexed SparseArrays

Thanks, it makes sense. But with removing the tuple from the code you provided it does seem to work in the manner you expect?

using JuMP

animals = ["dog", "cat", "chicken", "cow", "pig"]
regions = ["N", "S", "E", "W"]
model = Model()
@variable(model, x[animals, regions])

x["dog","N"]

In that way it seems you could make decision variables (value and x) which accomplish the task. And population could perhaps be a Dict indexed by (animal, region) mapping to Int as you need.

Or if you wanted to continue to index using something more like what you had, if not every animal is related to each region, that should be possible too.

animals_regions = [(a, rand(regions)) for a in animals]
@variable(model, x[animals_regions])
x[animals_regions[1]]

For general use of subsets of products to index decision variables, I’d still go with the DataFrames approach as it can be really nice when done well. Quite a few large energy modeling projects are built on that approach (JuMP + DataFrames). You’ve probably read this tutorial? The network multi-commodity flow problem · JuMP

1 Like