# How to build @constraints?

**URL:** <https://discourse.julialang.org/t/how-to-build-constraints/30950>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [November 11, 2019, 12:31pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950 "2019-11-11T12:31:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 11, 2019, 12:31pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/1 "2019-11-11T12:31:07Z")

</div>

This is my model, I am working on building a mathematical model and would like to be able to represent the constraint like this: `I[1] I[2] I[3] I[4] I[5] I[6] <=1500`

```julia
using JuMP, GLPK
m = Model(with_optimizer(GLPK.Optimizer))
c=6
@variable(m,I[1:c],lower_bound=0)
Ii = transpose(I)
@constraint(m, Ii[1:c].<=1500)

```

---

<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:** [November 11, 2019, 2:37pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/2 "2019-11-11T14:37:29Z")

</div>

You should read the documentation on [Variable Bounds](https://www.juliaopt.org/JuMP.jl/v0.20.0/variables/#Variable-bounds-1) and [Variable Containers](https://www.juliaopt.org/JuMP.jl/v0.20.0/variables/#Variable-containers-1).

You want something like

```nohighlight
using JuMP
model = Model()
c = 6
@variable(model, 0 <= x[1:c] <= 1500)

```

Alternatively,

```nohighlight
using JuMP
model = Model()
c = 6
@variable(model, x[1:c] >= 0)
@constraint(model, Ii[i = 1:c], x[i] <= 1500)

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 11, 2019, 4:03pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/4 "2019-11-11T16:03:34Z")

</div>

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 12, 2019, 1:37pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/5 "2019-11-12T13:37:11Z")

</div>

I have difficulty constructing constraints.

```julia
using JuMP,Cbc
model=Model(with_optimizer(Cbc.Optimizer))
d=6
MaxHE=20
@variable(model,NR[1:d],lower_bound=0,integer=true)
@variable(model,HE[1:d],lower_bound=0)
@constraint(model,HE.<= NR[1:d]*MaxHE)

```

I am trying to compile this result for the constraint:

```julia
HE[1] <=20NR[1] 
... #until you reach 
HE[6] <=20NR[6]

```

But the model gives me a different result than I want

---

<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:** [November 12, 2019, 2:28pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/6 "2019-11-12T14:28:01Z")

</div>

Take a read of the [Constraint Containers](https://www.juliaopt.org/JuMP.jl/v0.20.0/constraints/#Constraint-containers-1) section of the JuMP documentation. You want:

```nohighlight
using JuMP
model = Model()
d = 6
MaxHE = 20
@variable(model, NR[1:d] >= 0, Int)
@variable(model, HE[1:d] >= 0)
@constraint(model, [i = 1:d], HE[i] <= MaxHE * NR[i])

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 12, 2019, 2:49pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/8 "2019-11-12T14:49:51Z")

</div>

Thanks for your help @odow , but the result of compiling the code you submitted is the same result as my code.  
When I compile mine and your code, the answer I have is as follows:

```julia
6-element Array {ConstraintRef {Model, C, Shape} where Shape <: AbstractShape where C, 1}:
  HE [1]-20 NR[1] <= 0.0
  HE [2]-20 NR[2] <= 0.0
  HE [3]-20 NR[3] <= 0.0
  HE [4]-20 NR[4] <= 0.0
  HE [5]-20 NR[5] <= 0.0
  HE [6]-20 NR[6] <= 0.0

```

Since I would like to get this answer:

```julia
HE[1] <=20NR[1] 
....##until you reach
HE[6] <=20NR[6]

```

---

<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:** [November 12, 2019, 2:59pm UTC](https://discourse.julialang.org/t/how-to-build-constraints/30950/9 "2019-11-12T14:59:14Z")

</div>

These constraints are equivalent.
