# Adding lazy cuts to an LP using JuMP.@build\_constraint

**URL:** <https://discourse.julialang.org/t/adding-lazy-cuts-to-an-lp-using-jump-build-constraint/127764>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi\
**Created:** [April 6, 2025, 4:02pm UTC](https://discourse.julialang.org/t/adding-lazy-cuts-to-an-lp-using-jump-build-constraint/127764 "2025-04-06T16:02:11Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [April 7, 2025, 6:30am UTC](https://discourse.julialang.org/t/adding-lazy-cuts-to-an-lp-using-jump-build-constraint/127764/3 "2025-04-07T06:30:22Z")

</div>

You omitted my innermost `break`? Don’t quite understand.  
I take your point on not using `@build_constraint`.  
It is a folklore that `Dict` can be slow ([here](https://discourse.julialang.org/t/naming-elements-in-a-matrix/127553/9)), therefore I thought use Array could be faster. (`#undef` will not occupy my memory?)  
And I use the name `exists_violation` because I may execute many blocks of this lazy-cut (thus it is not necessarily `feasible` after only one batch).  
Finally, I suspect that `findall` might be a bit lengthy

> <https://github.com/JuliaLang/julia/blob/b627f5bb2592cb7120487ca40c149eb933b32dba/base/bitarray.jl#L1686>

Therefore I think it might be better to loop directly.  
In a 2-dimensional case, the revised code might look like this

```julia
is_cd_idle, cd_ref = trues(M, N), Matrix{JuMP.ConstraintRef}(undef, M, N)
while true
    exists_violation = false
    for i in 1:is_cd_idle.dims[1], j in 1:is_cd_idle.dims[2]
        is_cd_idle[i, j] || continue
        R_x[i, j] + DI[i] - DJ[j] < -COT || continue
        exists_violation = true
        is_cd_idle[i, j], cd_ref[i, j] = false, JuMP.@constraint(lpr, R_x[i, j] + lpr_DI[i] - lpr_DJ[j] >= 0)
        solve_to_normality(lpr)
        DI, DJ = JuMP.value.(lpr_DI), JuMP.value.(lpr_DJ)
        break
    end
    exists_violation || break # add how many cuts = sum(.!(is_cd_idle))
end
@info "after adding $(count((.!is_cd_idle))) cuts, there is no more violation"

```

---

_[View the full topic](https://discourse.julialang.org/t/adding-lazy-cuts-to-an-lp-using-jump-build-constraint/127764)._
