# How to concisely define a recursive constraint？

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!

Without running any code:

Does this work?

@constraint(model, constraints[t in 2:T], x[t] - x[t-1] >= 1)
@constraint(model, constraint_boundary, x >= 1)


By the way, in your post all inequalities have “1” on the right-hand side and in your code it is “0”.

1 Like

Oh sorry…Thanks! But can they have the same name? I mean that I want constraints to be x[t] >= 0 and constraints[2:T] to be x[t] - x[t-1] >= 1.

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
]

3 Likes

thank you very much!