Any discussion of support for set-indexed sparse arrays? I really want Julia to replace GAMS for my general modeling needs, but the data storage can’t compete.
You will probably get more eyes on this in Optimization (Mathematical) - Julia Programming Language given the mention of GAMS and mathematical programming.
That being said many groups including myself use JuMP for highly complex models, I assume you’ve checked out Getting started with sets and indexing · JuMP but is there something which remains that is proving difficult to accomplish?
Yes, I will use the first example from the page you referenced:
animals = [“dog”, “cat”, “chicken”, “cow”, “pig”]
model = Model()
@variable(model, x[animals])
Now let’s say we want variable or parameter values for each animal in a set of regions. Ideally it would look something like this:
animals = [“dog”, “cat”, “chicken”, “cow”, “pig”]
regions = [“N”, “E”, “S”, “W”]
model = Model()
@variable(model, x[(animals, regions)])
or for parameters:
population = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12;
13, 14, 15, 16;
17, 18, 19, 20]
and a call such as population(“chicken”,“S”) would return 11.
Even more ideally, if a specific instance equals 0, it would not be stored.
Most ideally, pairwise operations could be done across parameters and variables with the same indices, such as:
@constraint(model, value(animal, region) <= x(animal, region)*population(animal, region))
I know these are big asks. Currently, I am relying on DataFrames, but it is clunky. I looked at some methods used by SciML, but it is unclear how they would adapt to this. Functionality like this would definitely increase uptake among CGE modelers.
Thanks for the input.
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
Thank you, Sean. This is very helpful.
Hi @maxminn, welcome to the forum
A key thing to remember is that JuMP is not GAMS. The downside is that you can’t write JuMP models like you would a GAMS model. The upside is that JuMP is embedded in Julia, so you have fantastic flexibility to use the data structures that exist in the language (like Dict
, or Matrix
, or DataFrames.DataFrame
).
The flexibility makes it hard to give generic advice, so if you can post a small reproducible example of what you have in GAMS and what you have tried in JuMP, then we can certainly point you in the direction of some improvements.