Skip to content

Commit

Permalink
Implement MOI.default_cache (#130)
Browse files Browse the repository at this point in the history
Co-authored-by: Benoît Legat <[email protected]>
  • Loading branch information
odow and blegat authored Mar 1, 2022
1 parent 6a6ebbf commit dd6309b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"

[compat]
Clp_jll = "=1.17.6, ~100.1700.600"
MathOptInterface = "1"
MathOptInterface = "1.1"
julia = "1.6"

[extras]
Expand Down
7 changes: 7 additions & 0 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const OptimizerCache = MOI.Utilities.GenericModel{
},
}

Base.show(io::IO, ::Type{OptimizerCache}) = print(io, "Clp.OptimizerCache")

const SCALAR_SETS = Union{
MOI.GreaterThan{Float64},
MOI.LessThan{Float64},
Expand Down Expand Up @@ -72,6 +74,10 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
end
end

function MOI.default_cache(::Optimizer, ::Type{Float64})
return MOI.Utilities.UniversalFallback(OptimizerCache())
end

Base.cconvert(::Type{Ptr{Cvoid}}, model::Optimizer) = model
function Base.unsafe_convert(::Type{Ptr{Cvoid}}, model::Optimizer)
return model.inner::Ptr{Cvoid}
Expand Down Expand Up @@ -335,6 +341,7 @@ function MOI.copy_to(
dest::Optimizer,
src::MOI.Utilities.UniversalFallback{OptimizerCache},
)
MOI.Utilities.throw_unsupported(src)
return MOI.copy_to(dest, src.model)
end

Expand Down
24 changes: 14 additions & 10 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ function test_supports_default_copy_to()
end

function test_runtests()
model = MOI.Bridges.full_bridge_optimizer(
MOI.Utilities.CachingOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
Clp.Optimizer(),
),
Float64,
# This is what JuMP would construct
model = MOI.Utilities.CachingOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
MOI.instantiate(Clp.Optimizer; with_bridge_type = Float64),
)
@test model.optimizer.model.model_cache isa
MOI.Utilities.UniversalFallback{Clp.OptimizerCache}
# `Variable.ZerosBridge` makes dual needed by some tests fail.
MOI.Bridges.remove_bridge(
model.optimizer,
MathOptInterface.Bridges.Variable.ZerosBridge{Float64},
)
MOI.set(model, MOI.Silent(), true)
MOI.Test.runtests(
Expand All @@ -50,10 +55,9 @@ function test_runtests()
end

function test_Nonexistant_unbounded_ray()
model = MOI.Utilities.CachingOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
Clp.Optimizer(),
)
inner = Clp.Optimizer()
model =
MOI.Utilities.CachingOptimizer(MOI.default_cache(inner, Float64), inner)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 5)
MOI.set(
Expand Down

2 comments on commit dd6309b

@odow
Copy link
Member Author

@odow odow commented on dd6309b Mar 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/55753

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" dd6309bcd04749bb26810f4fae639a368add9bb1
git push origin v1.0.0

Please sign in to comment.