Skip to content

Commit

Permalink
Run lazy constraints callback at MIPNODE. Fixes #426 (#427)
Browse files Browse the repository at this point in the history
Executes the lazy constraints callback (if defined) when where=MIPNODE
so that the callback is executed at fractional values. Added a test
using callback_knapsack_model to ensure fractional solutions reach the
callback.
  • Loading branch information
simonbowly authored Nov 3, 2021
1 parent 374a569 commit 9d4eef6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/MOI_wrapper/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ function _default_moi_callback(model::Optimizer)
model.callback_state = _CB_HEURISTIC
model.heuristic_callback(cb_data)
end
if model.lazy_callback !== nothing
model.callback_state = _CB_LAZY
model.lazy_callback(cb_data)
end
end
model.callback_state = _CB_NONE
end
Expand Down
26 changes: 26 additions & 0 deletions test/MOI/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ function test_lazy_constraint_callback()
@test MOI.get(model, MOI.VariablePrimal(), y) == 2
end

function test_lazy_constraint_callback_fractional()
# callback_simple_model() is not large enough to see MIPNODE callbacks.
model, x, item_weights = callback_knapsack_model()
lazy_called_integer = false
lazy_called_fractional = false
MOI.set(
model,
MOI.LazyConstraintCallback(),
cb_data -> begin
status = MOI.get(
model,
MOI.CallbackNodeStatus(cb_data),
)::MOI.CallbackNodeStatusCode
if status == MOI.CALLBACK_NODE_STATUS_INTEGER
lazy_called_integer = true
elseif status == MOI.CALLBACK_NODE_STATUS_FRACTIONAL
lazy_called_fractional = true
end
end,
)
@test MOI.supports(model, MOI.LazyConstraintCallback())
MOI.optimize!(model)
@test lazy_called_integer
@test lazy_called_fractional
end

function test_lazy_constraint_callback_OptimizeInProgress()
model, x, y = callback_simple_model()
MOI.set(
Expand Down

0 comments on commit 9d4eef6

Please sign in to comment.