# How to concisely define a recursive constraint？

**URL:** <https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 17, 2022, 5:00pm UTC](https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411 "2022-09-17T17:00:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 17, 2022, 5:00pm UTC](https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411/1 "2022-09-17T17:00:04Z")

</div>

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:

```julia
@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!

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 17, 2022, 6:20pm UTC](https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411/2 "2022-09-17T18:20:25Z")

</div>

Without running any code:

Does this work?

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

```

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

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 17, 2022, 6:56pm UTC](https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411/3 "2022-09-17T18:56:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 17, 2022, 9:28pm UTC](https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411/4 "2022-09-17T21:28:00Z")

</div>

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.

```julia
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
]

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 18, 2022, 1:55am UTC](https://discourse.julialang.org/t/how-to-concisely-define-a-recursive-constraint/87411/5 "2022-09-18T01:55:31Z")

</div>

thank you very much!
