# Conditionnal constraint

**URL:** <https://discourse.julialang.org/t/conditionnal-constraint/87157>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 12, 2022, 6:32pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157 "2022-09-12T18:32:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 12, 2022, 6:32pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/1 "2022-09-12T18:32:08Z")

</div>

Hello,

I need help to implement a constraint.

The goal is to assign students to courses. Some courses can have a maximum of 8 students, others can have a maximum of 12.

I first wrote my constraint like this:

```julia
For j in 1:s
if (ws2[j+1,3]==1)
    @constraint(TB, contrainte2a[j=1:s], sum(x[i,j] for i in 1:e) <= 8)
else
    @constraint(TB, contrainte2b[j=1:s], sum(x[i,j] for i in 1:e) <= 12)
end
end

```

The “ws2[j+1,3]==1” refers to a column in an excel table.

I get this error message:

```julia
LoadError: An object of name contrainte2a is already attached to this model. If this
    is intended, consider using the anonymous construction syntax, e.g.,
    `x = @variable(model, [1:N], ...)` where the name of the object does
    not appear inside the macro.
    Alternatively, use `unregister(model, :contrainte2a)` to first unregister
    the existing name from the model. Note that this will not delete the
    object; it will just remove the reference at `model[:contrainte2a]`.

```

I think the error comes from putting a constraint in an if.

Does anyone know how to fix this problem?

Thank you in advance for your help 🙂

---

<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 12, 2022, 8:44pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/2 "2022-09-12T20:44:23Z")

</div>

Your code had a couple of issues:

- It has an outer loop over `j`
  - But then each constraint was indexed over `j` as well `[j=1:s]`

- Each constraint was named with `connstrainte2a`
  - But then you were trying to add these in a loop

Do instead:

```julia
for j in 1:s
    if ws2[j+1, 3] == 1
        @constraint(TB, sum(x[i, j] for i in 1:e) <= 8)
    else
        @constraint(TB, sum(x[i, j] for i in 1:e) <= 12)
    end
end

```

---

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 13, 2022, 4:32pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/3 "2022-09-13T16:32:20Z")

</div>

It works, thank you very much!

---

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 13, 2022, 4:41pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/4 "2022-09-13T16:41:41Z")

</div>

Now I would like to tell the program that if the box in the excel document is equal to 1, it must return that x[i,j]=1.

Is it correct to implement it this way?

```julia
for i in 1:e, j in 1:s
    if (ws[i+1,3]==1)
    @constraint(TB, x[i,j]==1)
    end
end

```

---

<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 13, 2022, 10:26pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/5 "2022-09-13T22:26:16Z")

</div>

Yes. You could also use the more compact syntax:

```julia
@constraint(TB, [i=1:e, j=1:s; ws[i+1,3]==1], x[i,j] == 1)

```

---

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 14, 2022, 5:10pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/6 "2022-09-14T17:10:02Z")

</div>

Thank you for your answer.

I copied what you told me but I get the following message:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f1cda62843c14ab446dc0551060b9e3c42777f2f.png)

Do you know why?

---

<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 14, 2022, 5:48pm UTC](https://discourse.julialang.org/t/conditionnal-constraint/87157/7 "2022-09-14T17:48:04Z")

</div>

You didn’t copy exactly what I wrote 😆
