# \[JuMP\] Use ConstraintRef to add additional constraint

**URL:** <https://discourse.julialang.org/t/jump-use-constraintref-to-add-additional-constraint/34046>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [January 31, 2020, 7:38pm UTC](https://discourse.julialang.org/t/jump-use-constraintref-to-add-additional-constraint/34046 "2020-01-31T19:38:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![acauligi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acauligi/32/4956_2.png) [@acauligi](https://discourse.julialang.org/u/acauligi)\
**Post date:** [January 31, 2020, 7:38pm UTC](https://discourse.julialang.org/t/jump-use-constraintref-to-add-additional-constraint/34046/1 "2020-01-31T19:38:31Z")

</div>

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.:

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [January 31, 2020, 8:09pm UTC](https://discourse.julialang.org/t/jump-use-constraintref-to-add-additional-constraint/34046/2 "2020-01-31T20:09:07Z")

</div>

You want something like:

```nohighlight
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

```
