# Constraints in a loop in the PowerModels

**URL:** <https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573>\
**Category:** Optimization (Mathematical)\
**Created:** [May 1, 2020, 7:21pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573 "2020-05-01T19:21:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [May 1, 2020, 7:21pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/1 "2020-05-01T19:21:45Z")

</div>

I want to enforce linear envelopes on the cosine function in PowerModelsAnnex. I implement the following code for doing that.

for (bp, buspair) in ref[:buspairs]  
for k=1:length(linear\_envelopes1)  
@constraint(model, cs[bp]\>= linear\_envelopes1 [k]\*td[bp]+ linear\_envelopes2 [k])  
end  
end

I get the following error. Can you please let me know how I can fix this. Thanks in advance!

ERROR: LoadError: The operators \<=, \>=, and == can only be used to specify scalar constraints. If you are trying to add a vectorized constraint, use the element-wise dot comparison operators (.\<=, .\>=, or .==) instead

---

<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:** [May 1, 2020, 7:36pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/2 "2020-05-01T19:36:15Z")

</div>

Some, or all of your components are vectors. Try the `.>=` as the error message suggests.

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [May 1, 2020, 8:05pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/3 "2020-05-01T20:05:09Z")

</div>

Thanks for the response! I have already tried that but I got the following error. I think it is better to define a dictionary for the linear envelope’s coefficient and then used the vectorized constraints.

ERROR: LoadError: MethodError: no method matching addVectorizedConstraint(::JuMP.Model, ::JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}})  
Closest candidates are:  
addVectorizedConstraint(::JuMP.Model, ::Array{JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}},N} where N) at C:\Users\Rasoul.julia\v0.6\JuMP\src\affexpr.jl:221  
addVectorizedConstraint(::JuMP.Model, ::Array{JuMP.GenericQuadConstraint{JuMP.GenericQuadExpr{Float64,JuMP.Variable}},N} where N) at C:\Users\Rasoul.julia\v0.6\JuMP\src\quadexpr.jl:172

---

<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:** [May 1, 2020, 8:26pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/4 "2020-05-01T20:26:13Z")

</div>

You should read the first post of [Please read: make it easier to help you - #11](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/11). It’s easier to help if you can provide a minimal working example. What are `cs[bp]`, `linear_envelopes1[k]`, `td[pb]`, and `linear_envelopes2[k]`?

However, it looks like you are using an old and outdated version of Julia/JuMP. You should update to Julia 1.0 and JuMP 0.21.

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [May 1, 2020, 9:44pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/5 "2020-05-01T21:44:31Z")

</div>

At first glance it looks like this code should work. So it should be mostly a debugging exercise along the lines @odow has suggested.

Concerning this code,

> [@Rasoul](#):
>
> for (bp, buspair) in ref[:buspairs]  
> for k=1:length(linear\_envelopes1)  
> @constraint(model, cs[bp]\>= linear\_envelopes1 [k]\*td[bp]+ linear\_envelopes2 [k])  
> end  
> end

One thing that stood out to me was that `linear_envelopes1` is not indexed first by `bp`. I would have exepected something like,

```julia
for (bp, buspair) in ref[:buspairs]
  for k=1:length(linear_envelopes1[bp])
    @constraint(model, cs[bp]>= linear_envelopes1[bp][k]*td[bp] + linear_envelopes2[bp][k])
  end
end

```

As I would expect each buspair to have its own envelope. You might be considering a case where there is only one envelope shared across all buspairs. In that case, what you originally proposed was along the right lines.

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [May 1, 2020, 11:20pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/6 "2020-05-01T23:20:41Z")

</div>

Thanks for the instructive response. In my code each buspair has its own linear envelopes. I have two options to implement the code, I can define a dictionary or I can define a two-dimensional array. Which one do you think is better in terms of execution time?

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [May 1, 2020, 11:41pm UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/7 "2020-05-01T23:41:08Z")

</div>

The set of bus pairs is very sparse, so I would recommend `Dict(bp => Array[line1, line2, ... n])`. The PWL cost function implementation gives an example of this. For example see, [PowerModels.jl/objective.jl at v0.15.5 · lanl-ansi/PowerModels.jl · GitHub](https://github.com/lanl-ansi/PowerModels.jl/blob/v0.15.5/src/core/objective.jl#L392)

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [May 9, 2020, 12:16am UTC](https://discourse.julialang.org/t/constraints-in-a-loop-in-the-powermodels/38573/8 "2020-05-09T00:16:33Z")

</div>

Thank you so much for the instructive response. It was really helpful!
