# Lazy Constraints on MIP problems

**URL:** https://discourse.julialang.org/t/lazy-constraints-on-mip-problems/100969
**Category:** General Usage
**Created:** [June 29, 2023, 11:07am UTC](https://discourse.julialang.org/t/lazy-constraints-on-mip-problems/100969 "2023-06-29T11:07:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ignacio](https://avatars.discourse-cdn.com/v4/letter/i/eb9ed0/32.png) [@Ignacio](https://discourse.julialang.org/u/Ignacio)
#### Post date: [June 29, 2023, 11:07am UTC](https://discourse.julialang.org/t/lazy-constraints-on-mip-problems/100969/1 "2023-06-29T11:07:38Z")

</div>

Hello all,  
I am trying trying to solve the following MIP with the Xpress solver (and I’ve also tried it with the GLPK solver) with lazy constraint. Variables of interest are x and y (non-integer), integer variable z is only added in order to force the solver to solve a MIP, since, for what I understand, for LPs lazy constraints are ignored.

I have tried to solve it in a variety of different ways and formulations, but the solver seems to freeze (as it appears on its log file) when adding the lazy constraint. Does anyone know how this issue can be resolved?

Thank you in advance for the help,

using Pkg  
Pkg.instantiate()  
Pkg.activate(“.”)  
Pkg.resolve()  
using JuMP  
using Xpress  
using MathOptInterface  
model = Model(()-\>Xpress.Optimizer(THREADS = 2))  
variable(model, x \>= 0)  
variable(model, y \>= 0)  
variable(model, z \>= 0, Int)  
variable(model, FO \>= 0)  
constraint(model, FO == x + y )  
constraint(model, x \<= 1 )  
constraint(model, y \<= 1 )  
objective(model, Max, FO)  
function lazy\_flow\_constraints(cb\_data)  
x\_val = callback\_value(cb\_data,x)  
y\_val = callback\_value(cb\_data,y)  
if x\_val + y\_val \> 1  
con = build\_constraint( (x + y)/sqrt(2) \<= 1 )  
MOI.submit(model, MOI.LazyConstraint(cb\_data), con)  
end  
end

MOI.set(model, MOI.LazyConstraintCallback(), lazy\_flow\_constraints)  
set\_optimizer\_attributes(model, “HEURSTRATEGY” =\> 0)  
set\_optimizer\_attributes(model, “MIPDUALREDUCTIONS” =\> 0)  
optimize!(model)  
info(" Execution is " \* string(termination\_status(model)))  
println(“x=”,JuMP.value(x))  
println(“y=”,JuMP.value(y))

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [June 29, 2023, 11:45am UTC](https://discourse.julialang.org/t/lazy-constraints-on-mip-problems/100969/2 "2023-06-29T11:45:48Z")

</div>

In the [documentation](https://jump.dev/JuMP.jl/stable/manual/callbacks/#Lazy-constraints), you’ll find a warning saying _Only add a lazy constraint if your primal solution violates the constraint._

I am isolating the potential region from your code:

```julia
function lazy_flow_constraints(cb_data)
    ...
    if x_val + y_val > 1
        con = build_constraint( (x + y)/sqrt(2) <= 1 )
        MOI.submit(model, MOI.LazyConstraint(cb_data), con)
    end
end

```

I think `x_val + y_val > 1` and `(x + y)/sqrt(2) <= 1` are not respecting the stated rule.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 29, 2023, 12:18pm UTC](https://discourse.julialang.org/t/lazy-constraints-on-mip-problems/100969/3 "2023-06-29T12:18:42Z")

</div>

In other words, you should test for `(x_val + y_val)/sqrt(2) > 1` instead of `x_val + y_val > 1` in the `if` condition.
