# Formulation of Equations for Complementarity.jl

**URL:** https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403
**Category:** Optimization (Mathematical)
**Tags:** jump, complementarity
**Created:** [July 24, 2024, 4:00am UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403 "2024-07-24T04:00:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Alio0712](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alio0712](https://discourse.julialang.org/u/Alio0712)
#### Post date: [July 24, 2024, 4:00am UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/1 "2024-07-24T04:00:37Z")

</div>

Dear community. I’m new to Julia and trying to code a Computable General Equilibrium Model. I have a question about Complementarity that I still can’t fully understand from going through various sources on the web.  
I know I can have only one complementarity constraint per equation, right? But what I’m not totally sure of is if I have to specify all equations in @mapping setting them equal to zero, which would force me to rethink all equations such that I make sure they are all set equal to 0.

Example:  
m=MCPModel()  
…  
@mapping(m, equation1, x^a - bq^2 + 5c …) equation is just an example  
@complementarity(m, equation1, x)

now does equation1 (featuring x) here have to be set equal to zero ALWAYS or doesn’t it need to be? What alternatives do I have? Sorry if the question is stupid.  
Thank you very much.  
Best!

---

<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: [July 24, 2024, 4:37am UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/2 "2024-07-24T04:37:06Z")

</div>

Hi @Alio0712, welcome to the forum!

Complementarity.jl is quite an old package. You can now use JuMP directly.

Here’s a tutorial with some MCP examples:

> **[Mixed complementarity problems · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/complementarity/)**
>
> Documentation for JuMP.

Let me know if you have any questions after reading the tutorial 😄

---

<div class="post-metadata">

### Author: ![Alio0712](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alio0712](https://discourse.julialang.org/u/Alio0712)
#### Post date: [July 24, 2024, 12:07pm UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/3 "2024-07-24T12:07:37Z")

</div>

Hi @odow. Thank you very much. As I’m not so familiar with coding per se (only used R and a bit MATLAB for econometrics) I looked for some examples of General Equilibrium in Julia and the simplest approach I found was using complementarity, not knowing if there was/is something better.  
So you recommend to just drop the complementarity package? I will start br trying to readapt the examples I have to JUMP only (as in the documentation you shared) and see if I get the idea. Since I see you’re the core contributor to JUMP (congratulations!), do you happen to know of anyone who works on computable general equilibrium using JUMP? Thank you very much.  
Best!

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [July 24, 2024, 2:49pm UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/4 "2024-07-24T14:49:57Z")

</div>

You might have a look at

- [GitHub - FRBNY-DSGE/DSGE.jl: Solve and estimate Dynamic Stochastic General Equilibrium models (including the New York Fed DSGE)](https://github.com/FRBNY-DSGE/DSGE.jl)
- [Quantitative Economics with Julia — Quantitative Economics with Julia](https://julia.quantecon.org/intro.html)

---

<div class="post-metadata">

### Author: ![Alio0712](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alio0712](https://discourse.julialang.org/u/Alio0712)
#### Post date: [July 24, 2024, 5:12pm UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/5 "2024-07-24T17:12:47Z")

</div>

Thank you very much @mthelm85. I have the second reference and will look into it and start small. Best!

---

<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: [July 24, 2024, 9:28pm UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/6 "2024-07-24T21:28:21Z")

</div>

> So you recommend to just drop the complementarity package?

Yes

> do you happen to know of anyone who works on computable general equilibrium using JuMP

In addition to Matt’s links, @EliLazarus is working on [GitHub - julia-mpsge/MPSGE.jl: MPSGE for Julia](https://github.com/julia-mpsge/MPSGE.jl)

---

<div class="post-metadata">

### Author: ![EliLazarus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elilazarus/32/39139_2.png) [@EliLazarus](https://discourse.julialang.org/u/EliLazarus)
#### Post date: [July 24, 2024, 10:24pm UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/7 "2024-07-24T22:24:59Z")

</div>

Yes, check out our package. We’ve built a Julia update of the MPSGE package (originally built in GAMS) so it’s pre-structured for CGE, MCP by default. Documentation is still a bit sparse, but the examples and test code should give a good idea, and I’d be happy to talk more.

---

<div class="post-metadata">

### Author: ![Alio0712](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alio0712](https://discourse.julialang.org/u/Alio0712)
#### Post date: [July 25, 2024, 12:09am UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/8 "2024-07-25T00:09:34Z")

</div>

Thank you very much @EliLazarus and @odow! I will definitely look into these resources. Maybe two quick questions that I’m still not absolutely certain about.

1. In order to be able to apply MCP, do I have to express ALL equations in the model setting them equal to 0?
2. Does the complementarity condition for each equation have to feature a variable that is present in this equation or can any equation have a complementarity variable that is not necessarily present in this equation?

Thanks! Best!

---

<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: [July 25, 2024, 12:23am UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/9 "2024-07-25T00:23:18Z")

</div>

> In order to be able to apply MCP, do I have to express ALL equations in the model setting them equal to 0?

I don’t understand the question. We don’t have any `== 0` constraints in a MCP. Do you have a small example of the problem you are trying to solve?

> Does the complementarity condition for each equation have to feature a variable that is present in this equation

No. But you need one complementarity condition for every variable in the model.

---

<div class="post-metadata">

### Author: ![Alio0712](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alio0712](https://discourse.julialang.org/u/Alio0712)
#### Post date: [July 25, 2024, 12:52am UTC](https://discourse.julialang.org/t/formulation-of-equations-for-complementarity-jl/117403/10 "2024-07-25T00:52:02Z")

</div>

> [@odow](#):
>
> I don’t understand the question. We don’t have any `== 0` constraints in a MCP. Do you have a small example of the problem you are trying to solve?

No, I don’t, because I haven’t yet tried to write my model as an MCP problem, but in the model I saw written with the complementarity package all equations are expressed such that, in equilibrium, there value would be 0. This is where my question comes from. In this small model I saw, it made sense, but I think in my larger model some equations might not be naturally 0 in equilibrium. I will come back to you after I try to write my own model if I’m still unsure about this.

> [@odow](#):
>
> No. But you need one complementarity condition for every variable in the model.

Perfect. Thank you very much!
