# Key error when adding a complementarity condition

**URL:** <https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810>\
**Category:** Optimization (Mathematical)\
**Tags:** complementarity\
**Created:** [September 26, 2022, 1:20pm UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810 "2022-09-26T13:20:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [September 26, 2022, 1:20pm UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/1 "2022-09-26T13:20:33Z")

</div>

I’m working to translate some economics models from GAMS into Julia using the Complementarity package and I’m running into an issue where a variable is defined on a larger set than its complementary equation. Here is a small example of the situation:

```julia
model = MCPModel()
A = [1,2,3,4]
B = [1,2,3]
@variable(model, X[A])
@mapping(model, Y[x = B], x^2)
@complementarity(model, Y, X)

```

Where B is a proper subset of A. Running this I get a KeyError because A contains elements that B does not.

```julia
KeyError: key 4 not found

1. <mark> **getindex** </mark>@*dict.jl:498* [inlined]
2. <mark> **getindex** </mark>@*DenseAxisArray.jl:51* [inlined]
3. <mark> **_getindex_recurse** </mark>@*DenseAxisArray.jl:309* [inlined]
4. <mark> **to_index** </mark>@*DenseAxisArray.jl:318* [inlined]
5. <mark> **getindex** (::JuMP.Containers.DenseAxisArray{JuMP.NonlinearExpression, 1, Tuple{Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, ::Int64)</mark>@*DenseAxisArray.jl:325*
6. <mark> **macro expansion** </mark>@*mcp.jl:435* [inlined]
7. <mark> **top-level scope** </mark>@*[Local: 7](http://localhost:1234/edit?id=7fb5cec0-3d9b-11ed-0a5a-91cf86764404#)* [inlined]

```

My question is: Is there a workaround for this or should I submit report to the complementarity package authors?

GAMS solves this by associating any keys not found to 0.

---

<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:** [September 26, 2022, 6:26pm UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/2 "2022-09-26T18:26:04Z")

</div>

The error message could be better, but the workatound is to not have mismatched keys?

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [September 27, 2022, 12:17am UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/3 "2022-09-27T00:17:16Z")

</div>

The error message is quite clear, there are extra keys in the variable that aren’t in the equation. The workaround can’t be to “not have mismatched keys” because I’m implementing a model where the mapping is only defined on a subset of the variable domain.

My temporary solution to is to implement a kronecker-delta function that “turns-off” the keys that aren’t needed. I’m not crazy about this; however, since it requires a special function for each mapping.

---

<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:** [September 27, 2022, 12:47am UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/4 "2022-09-27T00:47:55Z")

</div>

You should open an issue with a feature request at [GitHub - chkwon/Complementarity.jl: provides a modeling interface for mixed complementarity problems (MCP) and math programs with equilibrium problems (MPEC) via JuMP](https://github.com/chkwon/Complementarity.jl).

I don’t know if @chkwon is actively maintaining Complementarity.jl, but I’ll review a pull request if you want to dig into the details and make one.

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [September 27, 2022, 12:52pm UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/5 "2022-09-27T12:52:09Z")

</div>

Thanks, I’ll start digging through to see if I can get things working.

Thanks again for the suggestions.

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [September 27, 2022, 3:03pm UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/6 "2022-09-27T15:03:09Z")

</div>

I think the best solution is a macro for the Kronecker-Delta function, define the mapping on the entire set, and put the filter condition in X kron\_δ.

Something like:

```julia
macro kron_δ(x,X)
	x = esc(x)
	X = esc(X)
	quote
		$x in $X
	end
end

model = MCPModel()
A = [1,2,3,4]
B = [1,2,3]

@variable(model, X[A])
@mapping(model, Y[x = A], @δ(x,B)*X[x]^x)
@complementarity(model, Y, X)

```

I’m new to Julia, so this may not be the “best” method, but the macro seems to be the key.

Thanks again!

---

<div class="post-metadata">

**Author:** ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)\
**Post date:** [September 30, 2022, 8:02pm UTC](https://discourse.julialang.org/t/key-error-when-adding-a-complementarity-condition/87810/7 "2022-09-30T20:02:22Z")

</div>

I am maintaining Complementarity.jl in a minimal way 🙂

But I think the feature Mitch raised is very useful. I just saw a PR. Thanks!
