# Different ways to declare a constraint

**URL:** <https://discourse.julialang.org/t/different-ways-to-declare-a-constraint/10415>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 18, 2018, 4:02pm UTC](https://discourse.julialang.org/t/different-ways-to-declare-a-constraint/10415 "2018-04-18T16:02:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 18, 2018, 4:02pm UTC](https://discourse.julialang.org/t/different-ways-to-declare-a-constraint/10415/1 "2018-04-18T16:02:02Z")

</div>

Hello, I’m taking my first steps into Julia and I’m trying to modelate a LP. I wrote the following constraint:

```julia
@constraint(m, ctte_equilibre[r in set_regions, tec in tech_by_site[r], dy in div_years, y in years, t in timeslice],
demand_df[Symbol(r)][tl] for tl in 1:size(periode_demande,1)==sum(x_prod[r, tec, dy, y, t]))

```

That gives me the following error:

```julia
 LoadError: In @constraint(m,ctte_equilibre[r in set_regions, tec in tech_by_site[r], dy in div_years, y in years, t in timesli
ce],(sum(x_prod[r, tec, dy, y, t]) == (demand_df[Symbol(r)])[tl] for tl = periode_demande)): Constraints must be in one of the follow
ing forms:
       expr1 <= expr2
       expr1 >= expr2
       expr1 == expr2
       lb <= expr <= ub

```

The constraint related to demande\_df can be expressed in a traditional loop as follow and I can acess all the elements desired at a time.

```julia
for r in set_regions

    for tl in 1:size(periode_demande,1)
        print(demand_df[Symbol(r)][tl], '\n') 
    end

end

```

However, when I wrote in the reduced to way in order to be able to pass it as a constraint, it seems don’t be appropried because it returns a object called "Generator{Base.Iterators.Prod2{Array{Int64,1},Array{String,1}},##481#482} "  
that I know nothing about. See bellow the reduced way I used:

```julia
demand_df[Symbol(r)][tl] for tl in 1:size(periode_demande,1),r in set_regions)

```

When I use this reduced way I cannot anymore to see any element at a time.  
Anyone could help me to understand my problem ? Thanks in advance.

---

<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:** [April 18, 2018, 6:16pm UTC](https://discourse.julialang.org/t/different-ways-to-declare-a-constraint/10415/2 "2018-04-18T18:16:13Z")

</div>

Why do you mean by

```julia
demand_df[Symbol(r)][tl] for tl in 1:size(periode_demande,1)==sum(x_prod[r, tec, dy, y, t]))

```

Do you mean that `demand_df[Symbol(r)][tl]==sum(x_prod[r, tec, dy, y, t]))` for all `tl` ?

---

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 18, 2018, 6:20pm UTC](https://discourse.julialang.org/t/different-ways-to-declare-a-constraint/10415/3 "2018-04-18T18:20:15Z")

</div>

yeahh, that’s it ! ‘demand\_df’ is a data frame and the data I’m interested in are present in the column associated with the key of the dictionnary.  
The demand for all tl must be equal to she sum of the the production represented here for ‘x\_prod’

---

<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:** [April 19, 2018, 9:58am UTC](https://discourse.julialang.org/t/different-ways-to-declare-a-constraint/10415/4 "2018-04-19T09:58:36Z")

</div>

Then why don’t you do

```julia
@constraint(m, ctte_equilibre[tl in 1:size(periode_demande,1), r in set_regions, tec in tech_by_site[r], dy in div_years, y in years, t in timeslice],
demand_df[Symbol(r)][tl]==sum(x_prod[r, tec, dy, y, t]))

```
