Closed hdavid16 closed 2 years ago
This will require rebuilding the model. We have no immediate plans to implement deletion of nonlinear constraints.
Yeah... that's what we'll have to do for now.
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
Awesome! Many thanks. I will start using your trick for the constraint reformulation.
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.
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.
we've discussed that offline
Ah cool. Just checking
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