hdavid16 / DisjunctiveProgramming.jl

A JuMP extension for Generalized Disjunctive Programming
MIT License
27 stars 3 forks source link

Need to delete NLconstraint after reformulating its respective perspective function when using CHR #7

Closed hdavid16 closed 2 years ago

hdavid16 commented 3 years ago

This will require rebuilding the model or waiting until delete of an NLconstraint is supported: see https://github.com/jump-dev/JuMP.jl/issues/2355

odow commented 3 years ago

This will require rebuilding the model. We have no immediate plans to implement deletion of nonlinear constraints.

hdavid16 commented 3 years ago

Yeah... that's what we'll have to do for now.

blegat commented 2 years ago

Here is a hacky workaround:

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> c = @NLconstraint(model, exp(x + 1) <= 2)
exp(x + 1.0) - 2.0 ≤ 0

julia> println(model)
Feasibility
Subject to
 exp(x + 1.0) - 2.0 ≤ 0

No suppose I want to replace constraint c by a constraint where x is substituted by x / z where z is a variable. This can be achieved as follows:

julia> @variable(model, z)
z

julia> expr = :(exp($x / $z + 1.0) - 2.0)
:(exp(x / z + 1.0) - 2.0)

julia> con = JuMP._NonlinearConstraint(JuMP._NonlinearExprData(model, expr), -Inf, 0.0)
JuMP._NonlinearConstraint(JuMP._NonlinearExprData(JuMP._Derivatives.NodeData[JuMP._Derivatives.NodeData(JuMP._Derivatives.CALL, 2, -1), JuMP._Derivatives.NodeData(JuMP._Derivatives.CALLUNIVAR, 12, 1), JuMP._Derivatives.NodeData(JuMP._Derivatives.CALL, 1, 2), JuMP._Derivatives.NodeData(JuMP._Derivatives.CALL, 5, 3), JuMP._Derivatives.NodeData(JuMP._Derivatives.MOIVARIABLE, 1, 4), JuMP._Derivatives.NodeData(JuMP._Derivatives.MOIVARIABLE, 2, 4), JuMP._Derivatives.NodeData(JuMP._Derivatives.VALUE, 1, 3), JuMP._Derivatives.NodeData(JuMP._Derivatives.VALUE, 2, 1)], [1.0, 2.0]), -Inf, 0.0)

julia> model.nlp_data.nlconstr[c.index.value] = con
JuMP._NonlinearConstraint(JuMP._NonlinearExprData(JuMP._Derivatives.NodeData[JuMP._Derivatives.NodeData(JuMP._Derivatives.CALL, 2, -1), JuMP._Derivatives.NodeData(JuMP._Derivatives.CALLUNIVAR, 12, 1), JuMP._Derivatives.NodeData(JuMP._Derivatives.CALL, 1, 2), JuMP._Derivatives.NodeData(JuMP._Derivatives.CALL, 5, 3), JuMP._Derivatives.NodeData(JuMP._Derivatives.MOIVARIABLE, 1, 4), JuMP._Derivatives.NodeData(JuMP._Derivatives.MOIVARIABLE, 2, 4), JuMP._Derivatives.NodeData(JuMP._Derivatives.VALUE, 1, 3), JuMP._Derivatives.NodeData(JuMP._Derivatives.VALUE, 2, 1)], [1.0, 2.0]), -Inf, 0.0)

julia> println(model)
Feasibility
Subject to
 exp(x / z + 1.0) - 2.0 ≤ 0
hdavid16 commented 2 years ago

Awesome! Many thanks. I will start using your trick for the constraint reformulation.

odow commented 2 years ago

I don't know how much @blegat has tested this or looked into it, but to clarify:

doing so is extremely unsupported and may break in any future JuMP release.

I'm also unsure if this breaks any internal assumptions JuMP makes around resolving nonlinear models, so this may silently return incorrect solutions.

blegat commented 2 years ago

I cannot guarantee that this will work, it just seems that it might; we've discussed that offline and tried to worked this through at the INFORMS Annual Meeting. At the moment, this issue blocking for this package so it might be worth trying even if it using internal JuMP function and hence might break.

odow commented 2 years ago

we've discussed that offline

Ah cool. Just checking