# Constraint declaration

**URL:** <https://discourse.julialang.org/t/constraint-declaration/92683>\
**Category:** Optimization (Mathematical)\
**Created:** [January 8, 2023, 5:55pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683 "2023-01-08T17:55:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymus12](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anonymus12](https://discourse.julialang.org/u/anonymus12)\
**Post date:** [January 8, 2023, 5:55pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/1 "2023-01-08T17:55:08Z")

</div>

![Bildschirmfoto 2023-01-08 um 18.50.34](https://global.discourse-cdn.com/julialang/original/3X/0/5/05768d73e56d6d2d34a1b38ff14903f7453a647d.png)

i want to build an MEXCLP using Julia/Jump in VS Code. Everything is working fine with the declaration except for this particular constraint.

I tried this declaration:  
@constraint(model, c1, sum(x[j] for j in W[i]) \>= sum(y[i,k] for k in 1:p) for i in V)

But I get an error. Is this declaration correct for the picture I added, which shows the constraint? Maybe I did something wrong with the declaration of p, W and V so it doesn’t work?

---

<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:** [January 8, 2023, 7:48pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/2 "2023-01-08T19:48:22Z")

</div>

> [@anonymus12](#):
>
> @constraint(model, c1, sum(x[j] for j in W[i]) \>= sum(y[i,k] for k in 1:p) for i in V)

Use:

```Julia
@constraint(model, c1[i in V], sum(x[j] for j in W[i]) >= sum(y[i,k] for k in 1:p))

```

---

<div class="post-metadata">

**Author:** ![anonymus12](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anonymus12](https://discourse.julialang.org/u/anonymus12)\
**Post date:** [January 8, 2023, 8:10pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/3 "2023-01-08T20:10:57Z")

</div>

Thank you!

Could you help me with this constraint as well?  
 ![Bildschirmfoto 2023-01-08 um 21.10.15](https://global.discourse-cdn.com/julialang/original/3X/f/3/f312d0f3babb6a2f9bb6ca0f3a183da04b1fc1da.png)

I tried: @constraint(model, c2, sum(x[j] for j in W:p) \<= p)

---

<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:** [January 8, 2023, 9:58pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/4 "2023-01-08T21:58:54Z")

</div>

From your previous post, it looks like `W` is an array, which you indexed like `W[i]`.

So it doesn’t mean anything, mathematically, to sum \sum\limits\_{j \in W}^p x\_j. Perhaps you just meant \sum\limits\_{j \in W\_p} x\_j?

If so, the JuMP equivalent is:

`@constraint(model, c2, sum(x[j] for j in W[p]) <= p)`.

It’s easier to help if you read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) and can provide a reproducible example.

---

<div class="post-metadata">

**Author:** ![anonymus12](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anonymus12](https://discourse.julialang.org/u/anonymus12)\
**Post date:** [January 19, 2023, 1:29pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/5 "2023-01-19T13:29:04Z")

</div>

Thank you so much for reply!

It was really helpful!

I have a new model and I would like to add the following constraint

 ![Bildschirmfoto 2023-01-19 um 14.25.42](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d6a2b83cd99c9e90fea8047f0429d5824e90e03.png)

Is my declaration correct?  
`@constraint(model, c10[u in ` setdiff(U, [1])`, j !=j‘], sum(sum(V[j,j‘,u] for j in J) for j‘ in J) <=F)`

The sets and parameters etc. have all been declared. I just want to know if I implemented the " u in set U / {1}" and "j !=j’ " correctly.

Thank you and have a nice day !

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [January 19, 2023, 2:16pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/6 "2023-01-19T14:16:50Z")

</div>

The condition `j != j‘` should be separated with `;`, not `,`, see [Containers · JuMP](https://jump.dev/JuMP.jl/stable/manual/containers/#Containers)  
Otherwise, you just create another dimension indexed by the `Bool` `true` or `false` so you will actually add the constraints when it is `false`.

---

<div class="post-metadata">

**Author:** ![anonymus12](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anonymus12](https://discourse.julialang.org/u/anonymus12)\
**Post date:** [January 19, 2023, 3:27pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/7 "2023-01-19T15:27:36Z")

</div>

Thank you,  
so I should always use a semicolon when separating conditions?

I have another example:

 ![Bildschirmfoto 2023-01-19 um 16.25.06](https://global.discourse-cdn.com/julialang/original/3X/7/0/709df0e47f789d20e1177a69e8b8531a157498c7.png)

`@constraint(model, c` `7[c in J; u in ` setdiff(U, [1])], - `sum(` `V[j,c,u] for j in J))`

is this implementation correct?

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [January 19, 2023, 4:09pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/8 "2023-01-19T16:09:40Z")

</div>

Sorry I misread, it should be

```julia
constraint(model, c10[u in setdiff(U, [1]), sum(sum(V[j,j‘,u] for j in J) for j‘ in J if j !=j‘) <=F)

```

A `,` means “for all”, a `;` means “if”. So in your second example, you need a comma.  
In your first example, j was not a constraint index but an index of the for loop so it’s the classical Julia syntax

---

<div class="post-metadata">

**Author:** ![anonymus12](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anonymus12](https://discourse.julialang.org/u/anonymus12)\
**Post date:** [January 19, 2023, 4:51pm UTC](https://discourse.julialang.org/t/constraint-declaration/92683/9 "2023-01-19T16:51:33Z")

</div>

Thanks for clarifying !
