[JuMP] Use ConstraintRef to add additional constraint

Hi,
I have an affine inequality constraint in a JuMP model (that was constructed off-line) and would like to add a new constraint using the expression from the ConstraintRef associated with the affine constraint. e.g.:

aff_con = all_constraints(model, list_of_constraint_types(prob)[2]...)[1]
@constraint(model, -aff_con)

In this particular case, the constraint of interest is f(x)<=0 and I’d simply like to enforce an equality constraint f(x)>=0 using the ConstraintRef handle.

One hacky way that seems to work is:

new_con = Symbol(replace(string(Symbol(aff_con)), "≤" => "≥"))
str_con = string("@constraint(prob, ", new_con, ")")
eval(Meta.parse(str_con))

But I wasn’t sure if there’s a cleaner approach using the attributes available to the ConstraintRef handle itself.

You want something like:

julia> using JuMP

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> @constraint(model, my_con, 2x >= 1)
my_con : 2 x ≥ 1.0

julia> con_obj = constraint_object(my_con)
ScalarConstraint{GenericAffExpr{Float64,VariableRef},MathOptInterface.GreaterThan{Float64}}(2 x, MathOptInterface.GreaterThan{Float64}(1.0))

julia> flip(x::MOI.GreaterThan) = MOI.LessThan(-x.lower)
flip (generic function with 1 method)

julia> flip(x::MOI.LessThan) = MOI.GreaterThan(-x.upper)
flip (generic function with 2 methods)

julia> @constraint(model, -con_obj.func in flip(con_obj.set))
-2 x ≤ -1.0
1 Like