Skip to content

Commit

Permalink
Allow broadcasting in callbacks (#394)
Browse files Browse the repository at this point in the history
* Update MOI_callbacks.jl

* Add tests
  • Loading branch information
odow authored Feb 11, 2021
1 parent 812ffd3 commit b8168ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mutable struct CallbackData
end
Base.cconvert(::Type{Ptr{Cvoid}}, x::CallbackData) = x
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::CallbackData) = x.ptr::Ptr{Cvoid}
Base.broadcastable(x::CallbackData) = Ref(x)

mutable struct _CallbackUserData
model::Optimizer
Expand Down
15 changes: 15 additions & 0 deletions test/MOI/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,21 @@ function test_CallbackFunction_CallbackNodeStatus()
@test unknown_reached
end

function test_CallbackFunction_broadcast()
model, x, _ = callback_knapsack_model()
f(cb_data, x) = MOI.get(model, MOI.CallbackVariablePrimal(cb_data), x)
solutions = Vector{Float64}[]
MOI.set(model, Gurobi.CallbackFunction(), (cb_data, cb_where) -> begin
if cb_where == Gurobi.GRB_CB_MIPSOL
Gurobi.load_callback_variable_primal(cb_data, cb_where)
push!(solutions, f.(cb_data, x))
end
end)
MOI.optimize!(model)
@test length(solutions) > 0
@test length(solutions[1]) == length(x)
end

end # module TestCallbacks

runtests(TestCallbacks)

0 comments on commit b8168ae

Please sign in to comment.