# Set-indexed SparseArrays

**URL:** https://discourse.julialang.org/t/set-indexed-sparsearrays/124545
**Category:** Optimization (Mathematical)
**Created:** [January 8, 2025, 5:05pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545 "2025-01-08T17:05:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![maxminn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxminn/32/214592_2.png) [@maxminn](https://discourse.julialang.org/u/maxminn)
#### Post date: [January 8, 2025, 5:05pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545/1 "2025-01-08T17:05:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)
#### Post date: [January 8, 2025, 5:23pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545/2 "2025-01-08T17:23:43Z")

</div>

You will probably get more eyes on this in [Optimization (Mathematical) - Julia Programming Language](https://discourse.julialang.org/c/domain/opt/13) 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](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_sets_and_indexing/) but is there something which remains that is proving difficult to accomplish?

---

<div class="post-metadata">

### Author: ![maxminn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxminn/32/214592_2.png) [@maxminn](https://discourse.julialang.org/u/maxminn)
#### Post date: [January 8, 2025, 6:11pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545/3 "2025-01-08T18:11:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)
#### Post date: [January 8, 2025, 6:26pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545/4 "2025-01-08T18:26:08Z")

</div>

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

```julia
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.

```julia
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](https://jump.dev/JuMP.jl/stable/tutorials/linear/multi_commodity_network/)

---

<div class="post-metadata">

### Author: ![maxminn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxminn/32/214592_2.png) [@maxminn](https://discourse.julialang.org/u/maxminn)
#### Post date: [January 8, 2025, 7:15pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545/5 "2025-01-08T19:15:31Z")

</div>

Thank you, Sean. This is very helpful.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [January 8, 2025, 7:21pm UTC](https://discourse.julialang.org/t/set-indexed-sparsearrays/124545/6 "2025-01-08T19:21:19Z")

</div>

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.
