# Converting GAMS to JuMP: Defining Sets

**URL:** https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875
**Category:** Optimization (Mathematical)
**Tags:** jump, constraint
**Created:** [January 16, 2024, 5:15pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875 "2024-01-16T17:15:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)
#### Post date: [January 16, 2024, 5:15pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/1 "2024-01-16T17:15:41Z")

</div>

Hi all, I want to convert GAMS script to JuMP, and I have problems dealing with the sets in GAMS.

Here is the constraint in GAMS:  
eDemIR(d)… demIReal(d) + sum(l,line2dem(l,d)\*lineIReal(l)) =e= 0;

l, d are sets in GAMS. l = {1,2,3} and d = {1,2,3,4,5}

What is the proper way of converting this to JuMP?

This is how I tried to define every set in the constraint:  
D = [1,2,3,4,5]  
L = [1,2,3]  
@constraint(model, eDemIR[d in D], demIReal[d in D] + sum(line2dem[l, d] \* lineIReal[l] for l in L) == 0)

Thanks!

---

<div class="post-metadata">

### Author: ![hellemo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hellemo/32/324_2.png) [@hellemo](https://discourse.julialang.org/u/hellemo)
#### Post date: [January 16, 2024, 5:53pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/2 "2024-01-16T17:53:50Z")

</div>

Welcome to the forum, @sati !

My GAMS skills are a bit rusty, but I guess you would want to do something like this in JuMP:

```julia
using JuMP
model = Model()
D = [1,2,3,4,5]
L = [1,2,3]
@variable(model, demIReal[D])
@variable(model, line2dem[L,D])
@variable(model, lineIReal[L])
@constraint(model, eDemIR[d in D], demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0) 

```

---

<div class="post-metadata">

### Author: ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)
#### Post date: [January 18, 2024, 7:31pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/3 "2024-01-18T19:31:17Z")

</div>

Thanks [hellemo](https://discourse.julialang.org/u/hellemo) !

What if we have something like this:

```julia
using JuMP
model = Model()
D = [1,2,3,4,5]
L = [1,2,3]
line2dem = [1,4,3,6,5]
lineIReal = [1,2,1,1]
demIReal = [1,2,1,2]
@constraint(model, eDemIR[d in D], demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0) 

```

is this correct ?

---

<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 18, 2024, 8:07pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/4 "2024-01-18T20:07:38Z")

</div>

Hi @sati, welcome to the forum.

In this example you haven’t defined any variables?

But also, no. If you run the code, you’ll get an error

```Julia
julia> @constraint(model, eDemIR[d in D], demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0)
ERROR: BoundsError: attempt to access 5-element Vector{Int64} at index [1, 2]
Stacktrace:
  [1] getindex(::Vector{Int64}, ::Int64, ::Int64)

```

because `line2dem` is a vector:

```Julia
julia> line2dem
5-element Vector{Int64}:
 1
 4
 3
 6
 5

```

but you tried to index it as if it was a matrix:

```Julia
line2dem[l, d]

```

You might want to take a look at these tutorials:

- [Getting started with Julia · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_julia/)
- [Getting started with sets and indexing · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_sets_and_indexing/)

---

<div class="post-metadata">

### Author: ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)
#### Post date: [January 19, 2024, 5:44pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/5 "2024-01-19T17:44:13Z")

</div>

Thanks for your answer @odow !

I edited my question to be clear. I want to convert this GAMS constraint to JuMP  
…  
eDemIR(d)… demIV[d] + demIReal(d) + sum(l,line2dem(l,d)\*lineIReal(l)) =e= 0;  
…

l, d are sets in GAMS. l = {1,2,3} and d = {1,2,3,4,5}

In GAMS we define sets in the beginning. How can I convert this term “line2dem(l,d)” in JuMP?  
What is the proper way of converting this GAMS constraint to the equivalent JuMP?  
There are vectors and variables.

This is what I have:

```julia
using JuMP
model = Model()
D = [1,2,3,4,5]
L = [1,2,3]
line2dem = [1,4,3,6,5]
lineIReal = [1,2,1,1]
demIReal = [1,2,1,2]
@variable(model, demIV[D])
@constraint(model, eDemIR[d in D], demIV[d] + demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0)
..
..

```

Thanks.

---

<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 19, 2024, 9:59pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/6 "2024-01-19T21:59:59Z")

</div>

Your JuMP syntax in the `@constraint` is correct. But your input data is not correct.

What is `lin2dem`? Is it data? Is it a decision variable?

`line2dem(l,d)` looks like it is a matrix (it has two dimensions). Your definition `line2dem = [1,4,3,6,5]` is a vector. It has one dimension.

---

<div class="post-metadata">

### Author: ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)
#### Post date: [January 27, 2024, 12:57pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/7 "2024-01-27T12:57:41Z")

</div>

Thanks for your answer @odow !

Yes, line2dem is data and it’s a sparse matrix. I edited it below. So is it correct to use this matrix in the constraint? is this definition of the constraint correct?

* * *

I edited my question to be clear. I want to convert this GAMS constraint to JuMP

eDemIR(d)… demIV[d] + demIReal(d) + sum(l,line2dem(l,d)\*lineIReal(l)) =e= 0;

l, d are sets in GAMS. l = {1,2,3} and d = {1,2,3,4,5}

In GAMS we define sets in the beginning. How can I convert this term “line2dem(l,d)” in JuMP?  
What is the proper way of converting this GAMS constraint to the equivalent JuMP?  
There are vectors and variables.

This is what I have:

```julia
using JuMP
model = Model()
D = [1,2,3]
L = [1,2,3,4,5]
line2dem = sparse([1, 2, 3], [1, 2, 3], [1, 1, 1], 8, 3)
lineIReal = [1,2,1,1]
demIReal = [1,2,1,2]
@variable(model, demIV[D])
@constraint(model, eDemIR[d in D], demIV[d] + demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0)

```

Thanks.

---

<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 28, 2024, 8:52pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/8 "2024-01-28T20:52:18Z")

</div>

Again, your JuMP code is all correct. The constraint syntax matches the GAMS syntax.

But I get an error:

```Julia
julia> @constraint(model, eDemIR[d in D], demIV[d] + demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0)
ERROR: BoundsError: attempt to access 4-element Vector{Int64} at index [5]

```

because `lineIReal` is a vector with four elements:

```julia
julia> lineIReal
4-element Vector{Int64}:
 1
 2
 1
 1

```

but `L` is a set which contains `5`,

```Julia
julia> L
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

so you cannot index `lineIReal[5]`.

You just need to use the right data.

---

<div class="post-metadata">

### Author: ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)
#### Post date: [February 2, 2024, 2:14pm UTC](https://discourse.julialang.org/t/converting-gams-to-jump-defining-sets/108875/9 "2024-02-02T14:14:52Z")

</div>

Thanks @odow. The syntax and definitions are correct. These are just sample data and changing dimensions will fix.

```julia
using JuMP
model = Model()
D = [1,2,3]
L = [1,2,3,4,5]
line2dem = sparse([1, 2, 3], [1, 2, 3], [1, 1, 1], 8, 3)
lineIReal = [1,2,1,1,2]
demIReal = [1,2,1]
@variable(model, demIV[D])
@constraint(model, eDemIR[d in D], demIV[d] + demIReal[d] + sum(line2dem[l, d] * lineIReal[l] for l in L) == 0)

```
