Key error when adding a complementarity condition

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:

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.

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.

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

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.

1 Like

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.

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.

1 Like

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

Thanks again for the suggestions.

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:

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!

I am maintaining Complementarity.jl in a minimal way :slight_smile:

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

1 Like