# LazyConstraintCallback not called at every solution?

**URL:** <https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi\
**Created:** [June 20, 2023, 2:27pm UTC](https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611 "2023-06-20T14:27:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eetu\_Reijonen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eetu_reijonen/32/50891_2.png) [@Eetu\_Reijonen](https://discourse.julialang.org/u/Eetu_Reijonen)\
**Post date:** [June 20, 2023, 2:27pm UTC](https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611/1 "2023-06-20T14:27:27Z")

</div>

The [MathOptInterface documentation](https://jump.dev/MathOptInterface.jl/stable/reference/callbacks/) states that:

_there is no guarantee that the callback is called at every feasible primal solution._

Is there a way to get around this using Gurobi? I have run into a problem where only a part of the necessary LazyConstraints get added before the callback function is never called again.

Example execution could be: finding an optimal solution → calling my callback function → adding a violated lazy constraint → finding new optimal solution

At this point Gurobi terminates, having found “the optimal solution”. However if the callback function was called again, it would find a new violated constraint and have to resolve the problem.

---

<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:** [June 20, 2023, 7:08pm UTC](https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611/2 "2023-06-20T19:08:03Z")

</div>

Do you have a reproducible example? This sounds like a bug. The final solution should be feasible for all possible lazy constraints

---

<div class="post-metadata">

**Author:** ![Eetu\_Reijonen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eetu_reijonen/32/50891_2.png) [@Eetu\_Reijonen](https://discourse.julialang.org/u/Eetu_Reijonen)\
**Post date:** [June 21, 2023, 8:49am UTC](https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611/3 "2023-06-21T08:49:37Z")

</div>

I do have an example but it is quite lengthy. Luckily most of the code needn’t be understood except for the LazyConstraintCallback part. I tried to explain the problem in a bit more detail in the README file. [Here](https://github.com/EetuReijonen/callback-demo) is a link to the repository with the code.

I think that the final solution is indeed feasible for all lazy constraints that are added. However, I add one violated constraint at a time (a single callback function call), and at some point the callback function is just not called anymore, and the solution process terminates having found a solution that satisfies all constraints added so far.

---

<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:** [June 21, 2023, 9:04am UTC](https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611/4 "2023-06-21T09:04:02Z")

</div>

I didn’t run your code, but there are a few things that stick out. This is probably a bug in your code, not in Gurobi.jl or in Gurobi.

The most likely are lines like this:

> <https://github.com/EetuReijonen/callback-demo/blob/ac707b66f5b84b4783d0e6529b98f31bd8f7a7cb/callback_const_gen.jl#L61-L62>

Gurobi may return solutions which are feasible only up to a certain tolerance. So even though `x_opt` is binary, it may return `0.999999`.

Use `isapprox` instead:

```julia
isapprox(x_opt[current_feat, current_splitpoint_index, 1; atol = 1e-5)

```

Note that this applies to programming more generally. It is _never_ safe to compare floating point numbers with `==` and `!=`.

Consider the classic:

```plaintext
julia> 0.3 == 10 / 3
false

```

You should _always_ use `isapprox` with some tolerance.

---

<div class="post-metadata">

**Author:** ![Eetu\_Reijonen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eetu_reijonen/32/50891_2.png) [@Eetu\_Reijonen](https://discourse.julialang.org/u/Eetu_Reijonen)\
**Post date:** [June 21, 2023, 11:36am UTC](https://discourse.julialang.org/t/lazyconstraintcallback-not-called-at-every-solution/100611/5 "2023-06-21T11:36:17Z")

</div>

Thank you very much. I rounded the current optimal solutions before using the values and set the callback to run only at integer solutions (before it was at all nodes). Now the solution is as expected.
