chkwon / Complementarity.jl

provides a modeling interface for mixed complementarity problems (MCP) and math programs with equilibrium problems (MPEC) via JuMP
Other
75 stars 20 forks source link

Return value of JuMP.VariableRef #39

Closed hhoeschle closed 3 years ago

hhoeschle commented 5 years ago

Hallo,

I would like to use the same script to evaluate my model results obtained by an MCP reformulation and a constraint optimization. Unfortunately, I would need to swap between JuMP.value and Complementarity.result_value.

I did the following to overcome this:

import JuMP
import Complementarity
value(x::JuMP.VariableRef) = haskey(owner_model(x).ext,:MCP) ? Complementarity.result_value(x) : JuMP.value(x)
value(x::Number) = x
value(x::JuMP.NonlinearExpression) = JuMP.value(x::JuMP.NonlinearExpression, value) # use function `value` to evaluate JuMP.VariableRef in expressions
value(x) = JuMP.value(x) # Fall back on default for rest

using JuMP

using Complementarity
m = MCPModel()
@variable(m, x >= 0)
a = @NLexpression(m, x+2)
@mapping(m, F, a)
@complementarity(m, F, x)
status = solveMCP(m)
@show value(a)
@show value(x)

using Clp

m2 = Model()
@variable(m2, y>=0)
b = @expression(m2, y+1)
@constraint(m2, b <= 2)
@objective(m2, Max, y)
optimize!(m2, with_optimizer(Clp.Optimizer))
@show value(y)
@show value(b)

Maybe it can be integrated somehow.

Greetings, Hanspeter

chkwon commented 3 years ago

Closing for the new version of PATHSolver.jl https://github.com/chkwon/PATHSolver.jl/pull/44