Mahsa
Mahsa

Reputation: 79

Dependent Arrays in Constraints JuMP

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)

Upvotes: 2

Views: 134

Answers (1)

Oscar Dowson
Oscar Dowson

Reputation: 2574

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.

Upvotes: 1

Related Questions