From 0f5971d2fbe32ee8461d2d7a2e6e3c8d4fad0836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 21 Feb 2020 17:41:27 +0100 Subject: [PATCH] Update RawParameters handling --- src/MOI_wrapper.jl | 22 ++++++++++++++++++---- test/MOI_wrapper.jl | 13 +++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index 8e0a8fb..79fd3aa 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -28,7 +28,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer blockmatrix(), nothing, blockmatrix(), nothing, blockmatrix(), -1, NaN, NaN, NaN, false, Dict{Symbol, Any}()) for (key, value) in kwargs - MOI.set(optimizer, MOI.RawParameter(key), value) + MOI.set(optimizer, MOI.RawParameter(String(key)), value) end # May need to call `free_loaded_prob` and `free_loading_prob`. finalizer(MOI.empty!, optimizer) @@ -39,17 +39,31 @@ end varmap(optimizer::Optimizer, vi::MOI.VariableIndex) = optimizer.varmap[vi.value] function MOI.supports(optimizer::Optimizer, param::MOI.RawParameter) - return param.name in ALLOWED_OPTIONS + return Symbol(param.name) in ALLOWED_OPTIONS end function MOI.set(optimizer::Optimizer, param::MOI.RawParameter, value) + if !(param.name isa String) + Base.depwarn( + "passing `$(param.name)` to `MOI.RawParameter` as type " * + "`$(typeof(param.name))` is deprecated. Use a string instead.", + Symbol("MOI.set") + ) + end if !MOI.supports(optimizer, param) throw(MOI.UnsupportedAttribute(param)) end - optimizer.options[param.name] = value + optimizer.options[Symbol(param.name)] = value end function MOI.get(optimizer::Optimizer, param::MOI.RawParameter) # TODO: This gives a poor error message if the name of the parameter is invalid. - return optimizer.options[param.name] + if !(param.name isa String) + Base.depwarn( + "passing `$(param.name)` to `MOI.RawParameter` as type " * + "`$(typeof(param.name))` is deprecated. Use a string instead.", + Symbol("MOI.set") + ) + end + return optimizer.options[Symbol(param.name)] end MOI.supports(::Optimizer, ::MOI.Silent) = true diff --git a/test/MOI_wrapper.jl b/test/MOI_wrapper.jl index cc807b5..b8d3894 100644 --- a/test/MOI_wrapper.jl +++ b/test/MOI_wrapper.jl @@ -7,8 +7,8 @@ const MOIU = MOI.Utilities const MOIB = MOI.Bridges import CSDP -const optimizer = CSDP.Optimizer() -MOI.set(optimizer, MOI.Silent(), true) +const optimizer_constructor = MOI.OptimizerWithAttributes(CSDP.Optimizer, MOI.Silent() => true) +const optimizer = MOI.instantiate(optimizer_constructor) @testset "SolverName" begin @test MOI.get(optimizer, MOI.SolverName()) == "CSDP" @@ -19,16 +19,13 @@ end @test !MOIU.supports_allocate_load(optimizer, true) end -# UniversalFallback is needed for starting values, even if they are ignored by CSDP -const cache = MOIU.UniversalFallback(MOIU.Model{Float64}()) -const cached = MOIU.CachingOptimizer(cache, optimizer) -const bridged = MOIB.full_bridge_optimizer(cached, Float64) +const bridged = MOI.instantiate(optimizer_constructor, with_bridge_type=Float64) const config = MOIT.TestConfig(atol=1e-4, rtol=1e-4) @testset "Options" begin - param = MOI.RawParameter(:bad_option) + param = MOI.RawParameter("bad_option") err = MOI.UnsupportedAttribute(param) - @test_throws err CSDP.Optimizer(bad_option = 1) + @test_throws err MOI.set(optimizer, MOI.RawParameter("bad_option"), 0) end @testset "Unit" begin