Converting GAMS to JuMP: Defining Sets

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!

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:

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) 
4 Likes

Thanks hellemo !

What if we have something like this:

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 ?

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> @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> line2dem
5-element Vector{Int64}:
 1
 4
 3
 6
 5

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

line2dem[l, d]

You might want to take a look at these tutorials:

1 Like

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:

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.

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.

1 Like

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:

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.

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

But I get an error:

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> lineIReal
4-element Vector{Int64}:
 1
 2
 1
 1

but L is a set which contains 5,

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.

1 Like

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

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)
1 Like