Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable MIPDUALREDUCTIONS when callbacks are added #225

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading