Skip to content

Commit

Permalink
Disable MIPDUALREDUCTIONS when callbacks are added (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Mar 7, 2024
1 parent 87b6cd8 commit 02e4fa8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/MOI/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,17 @@ function MOI.get(
end

# ==============================================================================
# MOI.UserCutCallback & MOI.LazyConstraint
# MOI.UserCutCallback & MOI.LazyConstraint
# ==============================================================================

function MOI.set(model::Optimizer, ::MOI.UserCutCallback, cb::Function)
MOI.set(model, MOI.RawOptimizerAttribute("MIPDUALREDUCTIONS"), 0)
model.user_cut_callback = cb
return
end

function MOI.set(model::Optimizer, ::MOI.LazyConstraintCallback, cb::Function)
MOI.set(model, MOI.RawOptimizerAttribute("MIPDUALREDUCTIONS"), 0)
model.lazy_callback = cb
return
end
Expand Down Expand Up @@ -253,4 +255,4 @@ MOI.supports(::Optimizer, ::MOI.HeuristicSolution{CallbackData}) = true
function cache_exception(model::Optimizer, e::Exception)
model.cb_exception = e
return
end
end
40 changes: 39 additions & 1 deletion test/MathOptInterface/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ end
cb_calls = Int32[]
MOI.set(model, Xpress.CallbackFunction(), (cb_data) -> begin
push!(cb_calls)

if Xpress.get_control_or_attribute(cb_data.model, Xpress.Lib.XPRS_CALLBACKCOUNT_OPTNODE) > 1
return
end
Expand Down Expand Up @@ -385,3 +385,41 @@ end
MOI.optimize!(model)
@test unknown_reached
end

function test_lazy_constraint_dual_reductions()
model = Xpress.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 3)
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
MOI.add_constraint.(model, x[1:2], MOI.Integer())
MOI.add_constraint(
model,
1.0 * x[1] + 1.0 * x[2] - 1.0 * x[3],
MOI.EqualTo(0.0),
)
MOI.add_constraint(model, 1.0 * x[1] + 1.0 * x[2], MOI.LessThan(220.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = 1.0 * x[3]
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
function lazy_flow_constraints(cb_data)
x_val = MOI.get.(model, MOI.CallbackVariablePrimal(cb_data), x)
if x_val[1] + x_val[2] > 10
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
1.0 * x[1] + 1.0 * x[2],
MOI.LessThan(10.0),
)
end
end
MOI.set(model, MOI.LazyConstraintCallback(), lazy_flow_constraints)
MOI.optimize!(model)
x_val = MOI.get(model, MOI.VariablePrimal(), x)
@test x_val[1] + x_val[2] <= 10.0 + 1e-4
@test x_val[1] + x_val[2] x_val[3]
return
end

@testset "Xpress.test_lazy_constraint_dual_reductions" begin
test_lazy_constraint_dual_reductions()
end

0 comments on commit 02e4fa8

Please sign in to comment.