Skip to content

Commit fc1999a

Browse files
authored
Change heuristic callback status to UNKNOWN instead of REJECTED (#495)
1 parent 44101cc commit fc1999a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/MOI_wrapper/MOI_callbacks.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,15 @@ function MOI.submit(
299299
objP = Ref{Cdouble}()
300300
ret = GRBcbsolution(cb.callback_data, solution, objP)
301301
_check_ret(model, ret)
302-
return objP[] < GRB_INFINITY ? MOI.HEURISTIC_SOLUTION_ACCEPTED :
303-
MOI.HEURISTIC_SOLUTION_REJECTED
302+
if objP[] < GRB_INFINITY
303+
return MOI.HEURISTIC_SOLUTION_ACCEPTED
304+
end
305+
if cb.callback_data.cb_where == GRB_CB_MIPNODE
306+
# In a MIPNODE callback, Gurobi will process the solution immediately.
307+
return MOI.HEURISTIC_SOLUTION_REJECTED
308+
end
309+
# Although not accepted at present, Gurobi may cache the solution and use it
310+
# later in the optimization process.
311+
return MOI.HEURISTIC_SOLUTION_UNKNOWN
304312
end
305313
MOI.supports(::Optimizer, ::MOI.HeuristicSolution{CallbackData}) = true

test/MOI/MOI_callbacks.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,19 @@ function test_CallbackFunction_callback_HeuristicSolution()
496496
model, x, item_weights = callback_knapsack_model()
497497
solution_accepted = false
498498
solution_rejected = false
499+
solution_unknown = false
499500
cb_calls = Int32[]
500501
MOI.set(
501502
model,
502503
Gurobi.CallbackFunction(),
503504
(cb_data, cb_where) -> begin
504505
push!(cb_calls, cb_where)
506+
if cb_where == Gurobi.GRB_CB_MIP
507+
attr = MOI.HeuristicSolution(cb_data)
508+
status = MOI.submit(model, attr, x, fill(2.0, length(x)))
509+
solution_unknown |=
510+
(status == MOI.HEURISTIC_SOLUTION_UNKNOWN)
511+
end
505512
if cb_where != Gurobi.GRB_CB_MIPNODE
506513
return
507514
end
@@ -539,6 +546,7 @@ function test_CallbackFunction_callback_HeuristicSolution()
539546
MOI.optimize!(model)
540547
@test solution_accepted
541548
@test solution_rejected
549+
@test solution_unknown
542550
@test Gurobi.GRB_CB_MIPNODE in cb_calls
543551
end
544552

0 commit comments

Comments
 (0)