Skip to content

Commit

Permalink
Allow broadcasting in callbacks (#172)
Browse files Browse the repository at this point in the history
* Allow broadcasting in callbacks

* Add tests
  • Loading branch information
odow authored Feb 12, 2021
1 parent d33702a commit 748e38d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ mutable struct CallbackData
exception::Union{Nothing, Exception}
end

Base.broadcastable(x::CallbackData) = Ref(x)

# Dummy callback function for internal use only. Responsible for updating the
# objective bound, saving the mip gap, and calling the user's callback.
#
Expand Down
14 changes: 14 additions & 0 deletions test/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,18 @@ end
@test GLPK.GLP_IHEUR in cb_calls
end
end

@testset "broadcasting" begin
model, x, _ = callback_knapsack_model()
f(cb_data, x) = MOI.get(model, MOI.CallbackVariablePrimal(cb_data), x)
solutions = Vector{Float64}[]
MOI.set(model, GLPK.CallbackFunction(), (cb_data) -> begin
if GLPK.glp_ios_reason(cb_data.tree) == GLPK.GLP_IHEUR
push!(solutions, f.(cb_data, x))
end
end)
MOI.optimize!(model)
@test length(solutions) > 0
@test length(solutions[1]) == length(x)
end
end

0 comments on commit 748e38d

Please sign in to comment.