2

I want to code this constraint.

enter image description here

d and a in the below code are the subsets of set S with the size of N. For example: (N=5, T=3, S=6), d=[1,2,2,3,1] (the elements of d are the first three digits of S and the size of d is N) and a=[6,4,5,6,4] (the elements of a are the three last digits of set S and the size of a is N).

In the constraint, s should start with d and end with a.

It should be like s[j=1]=1:6, s[j=2]=2:4, s[j=3]=2:5, s[j=4]=3:6, s[j=5]1:4.

I do not know how to deal with this set that depends on the other sets. Can you please help me to code my constraint correctly? The below code is not working correctly.

N = 5
T=3
S=6
Cap=15
Q=rand(1:5,N)
d=[1,2,2,3,1]
a=[6,4,5,6,4]
@variable(model, x[j=1:N,t=1:T,s=1:S], Bin)
@constraint(model, [j= 1:N,t = 1:T, s = d[j]:a[j]], sum(x[j,t,s] * Q[j] for j=1:N) <= Cap)
Oscar Dowson
  • 2,395
  • 1
  • 5
  • 13
Mahsa
  • 69
  • 1

1 Answers1

1
N, T, S = 5, 3, 6
Q = rand(1:5,N)
d = [1, 2, 2, 3, 1]
a = [6, 4, 5, 6, 4]

using JuMP
model = Model()
@variable(model, x[1:N, 1:T, 1:S], Bin)
@constraint(
    model, 
    [t = 1:T, s = 1:S],
    sum(x[j, t, s] * Q[j] for j in 1:N if d[j] <= s < a[j]) <= 15,
)

p.s. There's no need to post multiple comments and questions: Coding arrays in constraint JuMP You should also consider posting on the Julia discourse instead: https://discourse.julialang.org/c/domain/opt/13. It's easier to have a conversation there.

Oscar Dowson
  • 2,395
  • 1
  • 5
  • 13
  • Thanks. In this case for example for d[1] <= s <= a[1], s=[1,2,3,4,5,6]. I have another constraint considers for example for j=1 s={d[1],a[1]} (s=[1,6]), for j=2 s={d[2],a[2]} (s=[2,4]),...and so on. Can you please help me coding that? – Mahsa Oct 12 '21 at 16:46
  • `if s in union(d[j], a[j])`. This syntax is not specific to JuMP. You can use any Julia syntax in the summation. In future, please make a post on the Julia discourse. I also suggest you work through some of the JuMP tutorials. – Oscar Dowson Oct 13 '21 at 23:10