0

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?

Francesco Clementi
  • 1,874
  • 4
  • 13
  • 28
  • Before getting to the constraints, it would help to know how to enumerate the elements of the set `O` in Julia (the definition in the question is written in "math") – Dan Getz Nov 21 '17 at 09:02

2 Answers2

5

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
Antonello
  • 6,092
  • 3
  • 31
  • 56
  • Unrelated to your content, but interesting, that there is no julia-support in [code-prettify](https://github.com/google/code-prettify/issues/416). I was just ready to edit your syntax-highlighting. – sascha Nov 20 '17 at 12:05
  • @sascha, see also this thread: https://discourse.julialang.org/t/any-library-for-syntax-highlight-of-julia-code-for-web-sites/1871/5 – Antonello Nov 20 '17 at 12:35
  • Yeah, i got julia-support in Atom and co. But SO seems to be based on my link above (which seems kind of less active compared to Pygments) and it seems there is not much progress. Sad. – sascha Nov 20 '17 at 12:40
0

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
Francesco Clementi
  • 1,874
  • 4
  • 13
  • 28