I want to build the following expression in the optimisation problem defined below.
A_{i0s} = x_i
A_{its} = A_{i,t-1,s} + y_{its} - z_{its} \quad \forall s, \forall i, \forall t > 0
I have given the code that I tried to build this expression, but I am getting it wrong. Any ideas? The variables y and z index t (time) from 1:T, but the expression need to index time from 0:T, where t =0 refers to the initial position.
Update: Just to add, I am able to achieve the desired outcome by defining A as variable and then defining a set of constraints. But I think these constraints can be avoided by defining this as expression.
model = Model(Cbc.Optimizer)
n = 10
T = 5
S= collect(1:3)
@variable(model, 0 <= x[i=1:n] <= 1)
@variable(model, 0 <= y[i=1:n, t=1:T, s in S] <= 1)
@variable(model, 0 <= z[i=1:n, t=1:T, s in S] <= 1)
Code for building expression (but incorrect)
# For t = 0
for i=1:n
for s in S
add_to_expression!(A[i, 0, s], x[i])
end
end
# For t = 1:T
for t=1:T
for i=1:n
for s in S
add_to_expression!(A[i, t, s], A[i, t-1, s] + y[i, t, s] - z[i, t, s])
end
end
end
# Define it as variable instead
# Define an auxiliary variable for A
@variable(model, A[1:n, 0:T, S])
# Constraints for the initialization at t=0
for i = 1:n
for s in S
@constraint(model, A[i, 0, s] == x[i])
end
end
# Constraints for the iteration relationship from t=1 to T
for t = 1:T
for i = 1:n
for s in S
@constraint(model, A[i, t, s] == A[i, t-1, s] + y[i, t, s] - z[i, t, s])
end
end
end