Francesco Clementi
Francesco Clementi

Reputation: 2092

How to define a set in JuMP (JuliaOpt)

It should be easy, but it's almost 3 days that I'm trying to define a set in JuMP. Then, I need to set my variable y for each pair (i', i) belonging to the set O.

O={(i,j): t[j] - t[i] + 30 < d[i,j]; i in D, j in K, i !=j }

y[i,j]=0, for each (i,j) in O

Any help?

Upvotes: 0

Views: 699

Answers (2)

Francesco Clementi
Francesco Clementi

Reputation: 2092

I should have found the solution for the problem. This is the code to write the constraint for the set O:

for i=1:n, j=1:m
    if (i != j && t[i] - t[j] + 30 < d[i,j])
        @constraint(model, y[i,j] == 0)
    end
end

Upvotes: 0

Antonello
Antonello

Reputation: 6431

Coming from GAMS I initially got the same problem.

It ends up that JuMP doesn't really have (nor need) the concept of sets as defined in GAMS, AMPL or Pyomo, but it uses the native containers available in the core Julia language.

You can choose two approaches: you can use arrays and position-based indexing (should be faster) or use dictionaries and use name-based indexing (better reading of the model). I prefer the latter.

This is an excerpt of the GAMS tutorial transport.gms translated in Jump (the whole example is here):

# Define sets #
#  Sets
#       i   canning plants   / seattle, san-diego /
#       j   markets          / new-york, chicago, topeka / ;
plants  = ["seattle","san_diego"]          # canning plants
markets = ["new_york","chicago","topeka"]  # markets

# Define parameters #
#   Parameters
#       a(i)  capacity of plant i in cases
#         /    seattle     350
#              san-diego   600  /
a = Dict(              # capacity of plant i in cases
  "seattle"   => 350,
  "san_diego" => 600,
)
b = Dict(              # demand at market j in cases
  "new_york"  => 325,
  "chicago"   => 300,
  "topeka"    => 275,
)
# Variables
@variables trmodel begin
    x[p in plants, m in markets] >= 0 # shipment quantities in cases
end

# Constraints
@constraints trmodel begin
    supply[p in plants],   # observe supply limit at plant p
        sum(x[p,m] for m in markets)  <=  a[p]
    demand[m in markets],  # satisfy demand at market m
        sum(x[p,m] for p in plants)  >=  b[m]
end

Upvotes: 5

Related Questions