# Complementarity constraints containing sum statements

**URL:** https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [April 22, 2022, 8:18am UTC](https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818 "2022-04-22T08:18:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rubenvanbeesten](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubenvanbeesten/32/35614_2.png) [@rubenvanbeesten](https://discourse.julialang.org/u/rubenvanbeesten)
#### Post date: [April 22, 2022, 8:18am UTC](https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818/1 "2022-04-22T08:18:18Z")

</div>

Hi,

I’m trying to model an mathematical program with equilibrium constraints (MPEC) in Julia/JuMP. I am currently using the [Complementarity.jl](https://github.com/chkwon/Complementarity.jl) package. I am following [this](https://github.com/chkwon/Complementarity.jl/blob/master/MPEC.md#bard1jl-----translated-from-bard1mod-in-macmpec) example (“bard1”) and try to adapt it to my needs.

Now, I run into problems when I try to include a summation statement in a complementarity constraint. To explain the issue, consider the following reformulation of the bard1 example, which works fine:

```julia
using JuMP, Ipopt, Complementarity

m = Model(Ipopt.Optimizer)
@variable(m, x[i in 1:2]>=0)
@variable(m, l[j in 1:3])

@NLobjective(m, Min, (x[1] - 5)^2 + (2*x[2] + 1)^2)

@NLconstraint(m, 2*(x[2]-1) - 1.5*x[1] + l[1] - l[2]*0.5 + l[3] == 0)

@complements(m, 0 <= 3*x[1] - x[2] - 3, l[1] >= 0)
@complements(m, 0 <= - x[1] + 0.5*x[2] + 4, l[2] >= 0)
@complements(m, 0 <= - x[1] - x[2] + 7, l[3] >= 0)

optimize!(m)

```

Now, my problem arises when I try to rewrite the third complementarity constraint as follows:

```julia
@complements(m, 0 <= - sum(x[i] for i in 1:2) + 7, l[3] >= 0)

```

If I try to run this line, I get the following error message:

```julia
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] macro expansion
   @ C:\Users\egbertrv\.julia\packages\JuMP\klrjG\src\parse_nlp.jl:521 [inlined]
 [2] macro expansion
   @ C:\Users\egbertrv\.julia\packages\JuMP\klrjG\src\macros.jl:1978 [inlined]
 [3] top-level scope
   @ C:\Users\egbertrv\.julia\packages\Complementarity\sQ4xs\src\mpec.jl:265

```

Of course I could hardcode all the constraints without using the summation function, but that will not be practically feasible for my problem.

Does anyone have an idea how to solve this issue? I’m open for any alternative that allows me to formulate and solve an MPEC in Julia/JuMP.

---

<div class="post-metadata">

### Author: ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)
#### Post date: [April 22, 2022, 9:34am UTC](https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818/2 "2022-04-22T09:34:41Z")

</div>

Really not sure, but I would try

`sum{x[i], i in 1:2}`

or

```julia
for i in 1:2
@complements(m, 0 <= - sum(x[i]) + 7, l[3] >= 0)
end

```

Or something like that… This is how I did a long time ago in [my former blog](http://stla.github.io/stlapblog/posts/KantorovichWithJulia.html) but there was no `@complements`.

---

<div class="post-metadata">

### Author: ![rubenvanbeesten](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubenvanbeesten/32/35614_2.png) [@rubenvanbeesten](https://discourse.julialang.org/u/rubenvanbeesten)
#### Post date: [April 22, 2022, 11:20am UTC](https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818/3 "2022-04-22T11:20:51Z")

</div>

Thanks for the reply! I tried both suggestions, but they don’t work unfortunately.

1. The curly brackets no longer seem to be supported:

```julia
ERROR: LoadError: The curly syntax (sum{},prod{},norm2{}) is no longer supported. Expression: sum{x[i], i in 1:2}.

```

1. The other suggestion also fails:

```julia
ERROR: sum() can appear in nonlinear expressions only if the argument is a generator statement, for example, sum(x[i] for i in 1:N).

```

1. Out of curiosity, I also tried

```julia
@complements(m, 0 <= - sum(x[:]) + 7, l[3] >= 0)

```

which is in line with the example in your blog post, but it throws the same error as under 2. above.

---

<div class="post-metadata">

### Author: ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)
#### Post date: [April 22, 2022, 11:54am UTC](https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818/4 "2022-04-22T11:54:11Z")

</div>

Not sure if this is the best way, but you can create an auxiliary expression with the sum and use the expression in `complements`:

```julia
using JuMP, Ipopt, Complementarity

m = Model(Ipopt.Optimizer)
@variable(m, x[i in 1:2]>=0)
@variable(m, l[j in 1:3])

@NLobjective(m, Min, (x[1] - 5)^2 + (2*x[2] + 1)^2)

@NLconstraint(m, 2*(x[2]-1) - 1.5*x[1] + l[1] - l[2]*0.5 + l[3] == 0)
@NLexpression(m,sumExp, sum(x[i] for i in 1:2)) #Create the expression

@complements(m, 0 <= 3*x[1] - x[2] - 3, l[1] >= 0)
@complements(m, 0 <= - x[1] + 0.5*x[2] + 4, l[2] >= 0)
@complements(m, 0 <= - x[1] - x[2] + 7, l[3] >= 0)

@complements(m, 0 <= - sumExp + 7, l[3] >= 0) #Use the expression here

optimize!(m)

```

---

<div class="post-metadata">

### Author: ![rubenvanbeesten](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubenvanbeesten/32/35614_2.png) [@rubenvanbeesten](https://discourse.julialang.org/u/rubenvanbeesten)
#### Post date: [April 22, 2022, 2:09pm UTC](https://discourse.julialang.org/t/complementarity-constraints-containing-sum-statements/79818/5 "2022-04-22T14:09:42Z")

</div>

Thanks, this works! 🙂

For future reference, I think the simplest way to use this workaround is as follows. For variable `x[i in I]`, define the expression

```julia
@NLexpression(m, exp_x[i in I], **my_expression** )

```

(where ` **my_expression** ` may contain summations), and use them to define the complementarity constraints as follows:

```julia
for i in I
    @complements(m, 0 <= exp_x[i], x[i] >= 0)
end

```

Thanks again!
