How to concisely define a recursive constraint with JuMP, for example: x_{t}-x_{t-1}\geqslant 1\quad\forall t = 1,\cdots,T,
where x_0=0 but I don’t want to define an extra variable for it.
In another words, I want to define the following constraints: x_{t}-x_{t-1}\geqslant 1\quad\forall t = 2,\cdots,T, x_{t}\geqslant 1\quad t = 1.
I have tried the following code but it doesn’t work:
@constraint(model, constraints[t in 2:T], x[t] >= x[t-1]);
@constraint(model, constraints[t in 1], x[t] >= 0);
Since I just started using JuMP, I am not very familiar with it. I hope some seniors can help me correct the wrong syntax in the code, thanks!
Here are a few options. Particularly important for the third option: you are not restricted to using the data structures in JuMP. They are helpers, but you can use your own ones if it’s easier.
using JuMP
T = 3
model = Model()
@variable(model, x[1:T])
@constraint(model, constraints[t=1:T], x[t] >= get(x, t-1, 0))
model = Model()
@variable(model, x[1:T])
@constraint(model, constraints[t=1:T], x[t] >= (t > 1 ? x[t-1] : 0))
model = Model()
@variable(model, x[1:T])
constraints = model[:constraints] = ConstraintRef[
if t == 1
@constraint(model, x[t] >= 0, base_name = "constraints")
else
@constraint(model, x[t] >= x[t-1], base_name = "constraints")
end
for t in 1:T
]