From 2eb0d31d0a38ae7444e83927e3d90ed1e6bcd3b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 21 Feb 2020 21:21:42 +0100 Subject: [PATCH] Update test template with instantiate --- docs/src/apimanual.md | 47 ++++++++++++++++++++----------------------- src/instantiate.jl | 2 +- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/docs/src/apimanual.md b/docs/src/apimanual.md index b97afc2c78..ee7b3a6f69 100644 --- a/docs/src/apimanual.md +++ b/docs/src/apimanual.md @@ -1310,66 +1310,63 @@ const MOIU = MOI.Utilities const MOIB = MOI.Bridges import FooBar -const optimizer = FooBar.Optimizer() -MOI.set(optimizer, MOI.Silent(), true) +const OPTIMIZER_CONSTRUCTOR = MOI.OptimizerWithAttributes(FooBar.Optimizer, MOI.Silent() => true) +const OPTIMIZER = MOI.instantiate(OPTIMIZER_CONSTRUCTOR) @testset "SolverName" begin - @test MOI.get(optimizer, MOI.SolverName()) == "FooBar" + @test MOI.get(OPTIMIZER, MOI.SolverName()) == "FooBar" end @testset "supports_default_copy_to" begin - @test MOIU.supports_default_copy_to(optimizer, false) + @test MOIU.supports_default_copy_to(OPTIMIZER, false) # Use `@test !...` if names are not supported - @test MOIU.supports_default_copy_to(optimizer, true) + @test MOIU.supports_default_copy_to(OPTIMIZER, true) end -const bridged = MOIB.full_bridge_optimizer(optimizer, Float64) -const config = MOIT.TestConfig(atol=1e-6, rtol=1e-6) +const BRIDGED = MOI.instantiate(OPTIMIZER_CONSTRUCTOR, with_bridge_type = Float64) +const CONFIG = MOIT.TestConfig(atol=1e-6, rtol=1e-6) @testset "Unit" begin - MOIT.unittest(bridged, config) + MOIT.unittest(BRIDGED, CONFIG) end @testset "Modification" begin - MOIT.modificationtest(bridged, config) + MOIT.modificationtest(BRIDGED, CONFIG) end @testset "Continuous Linear" begin - MOIT.contlineartest(bridged, config) + MOIT.contlineartest(BRIDGED, CONFIG) end @testset "Continuous Conic" begin - MOIT.contlineartest(bridged, config) + MOIT.contlineartest(BRIDGED, CONFIG) end @testset "Integer Conic" begin - MOIT.intconictest(bridged, config) + MOIT.intconictest(BRIDGED, CONFIG) end ``` -The optimizer `bridged` constructed with [`Bridges.full_bridge_optimizer`](@ref) -automatically bridges constraints that are not supported by `optimizer` +The optimizer `BRIDGED` constructed with [`instantiate`](@ref) +automatically bridges constraints that are not supported by `OPTIMIZER` using the bridges listed in [Bridges](@ref). It is recommended for an implementation of MOI to only support constraints that are natively supported by the solver and let bridges transform the constraint to the appropriate form. -For this reason it is expected that tests may not pass if `optimizer` is used -instead of `bridged`. +For this reason it is expected that tests may not pass if `OPTIMIZER` is used +instead of `BRIDGED`. To test that a specific problem can be solved without bridges, a specific test can -be run with `optimizer` instead of `bridged`. For instance +be run with `OPTIMIZER` instead of `BRIDGED`. For instance ```julia @testset "Interval constraints" begin - MOIT.linear10test(optimizer, config) + MOIT.linear10test(OPTIMIZER, CONFIG) end ``` -checks that `optimizer` implements support for +checks that `OPTIMIZER` implements support for [`ScalarAffineFunction`](@ref)-in-[`Interval`](@ref). -If the wrapper does not support building the model incrementally (i.e. with `add_variable` and `add_constraint`), then `supports_default_copy_to` can be replaced by `supports_allocate_load` if appropriate (see [Implementing copy](@ref)) and the line `const bridged = ...` can be replaced with -```julia -const cache = MOIU.UniversalFallback(MOIU.Model{Float64}()) -const cached = MOIU.CachingOptimizer(cache, optimizer) -const bridged = MOIB.full_bridge_optimizer(cached, Float64) -``` +If the wrapper does not support building the model incrementally (i.e. with `add_variable` and `add_constraint`), +then `supports_default_copy_to` can be replaced by `supports_allocate_load` if +appropriate (see [Implementing copy](@ref)). ### Benchmarking diff --git a/src/instantiate.jl b/src/instantiate.jl index 544a8c3467..483130603c 100644 --- a/src/instantiate.jl +++ b/src/instantiate.jl @@ -99,7 +99,7 @@ problem incrementally or does not support names and `with_names` is `true` (see [`Utilities.supports_default_copy_to`](@ref)) then a [`Utilities.CachingOptimizer`](@ref) is added to store a cache of the bridged model. -Hence set `with_names` to `true` if names might be set. +Hence set `with_names` to `true` if names might be set. """ function instantiate( optimizer_constructor; with_bridge_type::Union{Nothing, Type}=nothing,