# Right way to define a constraint

**URL:** https://discourse.julialang.org/t/right-way-to-define-a-constraint/78116
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 19, 2022, 12:27am UTC](https://discourse.julialang.org/t/right-way-to-define-a-constraint/78116 "2022-03-19T00:27:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Popeye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/popeye/32/25244_2.png) [@Popeye](https://discourse.julialang.org/u/Popeye)
#### Post date: [March 19, 2022, 12:27am UTC](https://discourse.julialang.org/t/right-way-to-define-a-constraint/78116/1 "2022-03-19T00:27:29Z")

</div>

I am having difficulty in defining a constraint efficiently that is based on a matrix of decision variables. A MWE is given below but as you will see it creates a number of intermediate expressions, so it is not the best approach.

I think I am complicating it unnecessarily in the code below. Could this constraint be simplified?

```julia
rate = [0.02,0.025,0.03,0.035]
pd = [5.0,6.5,8.0,9.3,10.0,11.1,9.0,6.5,5.0,6.5]
id = [2.0,1.5,3.0,4.3,4.0,6.1,5.0,3.5,2.0,3.5]

model = Model()
@variable(model,p[i=1:4,j=1:10]>=0)
@expression(model,ex1[i=1:4,j=1:10],sum(p[i,t] for t in j:10))
@expression(model,ex2,rate .* ex1)
@expression(model,ex3[i=1:4,j=1:10],ex2[i,j]+p[i,j])
@constraint(model,constraint[j=1:10],sum(ex3[i,j] for i in 1:4) <= pd[j] + id[j])

```

The constraint is based on a set of rules:  
(1): I define a matrix of decision variables `p[i=1:4,j=1:10]`  
(2): Create a matrix expression `ex1` that calculates the cumulative sum of the decision variables by adding together elements to the right, ie. `sum(p[i,t] for t in j:10)`  
(3): Multiply a vector containing data (`rate`) with ex1 and add the variable `p[i,j]`. This gives the expression `ex3`.  
(4): Constraint is given by sum across the rows for each column in `ex3` should be less than or equal to certain values.

---

<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: [March 20, 2022, 7:15pm UTC](https://discourse.julialang.org/t/right-way-to-define-a-constraint/78116/2 "2022-03-20T19:15:46Z")

</div>

> but as you will see it creates a number of intermediate expressions, so it is not the best approach

Is the time to build the problem a bottleneck in your problem? If not, write the model that is the most readable to you. Only try improving performance if it the limiting factor.

Is there any reason not to write it just as:

```Julia
model = Model()
@variable(model, p[1:4, 1:10] >= 0)
@expression(model, ex1[i=1:4, j=1:10], sum(p[i, t] for t in j:10))
@constraint(
    model,
    [j=1:10],
    sum(rate[i] * ex1[i, j] + p[i, j] for i in 1:4) <= pd[j] + id[j],
)

```

In general, I wouldn’t expect the performance to be too different between the two approaches.

---

<div class="post-metadata">

### Author: ![Popeye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/popeye/32/25244_2.png) [@Popeye](https://discourse.julialang.org/u/Popeye)
#### Post date: [March 20, 2022, 11:54pm UTC](https://discourse.julialang.org/t/right-way-to-define-a-constraint/78116/3 "2022-03-20T23:54:36Z")

</div>

Thank you.

> [@odow](#):
>
> Is the time to build the problem a bottleneck in your problem? If not, write the model that is the most readable to you. Only try improving performance if it the limiting factor.

Time to build the problem was not a bottleneck, but clarity was an issue. I thought I was creating too many intermediate expressions and code can be made concise. The answer you have provided is perfect.
