From 8827a96f558363bb1a49de26efff7683f259cb8d Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 8 Nov 2022 13:16:30 +1300 Subject: [PATCH] Fix optimizer_attribute with AbstractString names (#3127) --- src/JuMP.jl | 11 +++++++++++ test/model.jl | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/JuMP.jl b/src/JuMP.jl index 7252fe83554..99421c85685 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -804,6 +804,12 @@ function set_optimizer_attribute(model::Model, name::String, value) return end +# This method is needed for string types like String15 coming from a DataFrame. +function set_optimizer_attribute(model::Model, name::AbstractString, value) + set_optimizer_attribute(model, String(name), value) + return +end + """ set_optimizer_attribute( model::Model, @@ -878,6 +884,11 @@ function get_optimizer_attribute(model::Model, name::String) return get_optimizer_attribute(model, MOI.RawOptimizerAttribute(name)) end +# This method is needed for string types like String15 coming from a DataFrame. +function get_optimizer_attribute(model::Model, name::AbstractString) + return get_optimizer_attribute(model, String(name)) +end + """ get_optimizer_attribute( model::Model, attr::MOI.AbstractOptimizerAttribute diff --git a/test/model.jl b/test/model.jl index 11c6fbd5d23..79f7e664296 100644 --- a/test/model.jl +++ b/test/model.jl @@ -462,6 +462,17 @@ function test_get_optimizer_attribute() @test JuMP.get_optimizer_attribute(model, "aaa") == "bbb" end +function test_optimizer_attribute_abstract_string() + mock = MOIU.UniversalFallback(MOIU.Model{Float64}()) + model = Model(() -> MOIU.MockOptimizer(mock)) + attribute = "abc" + abstract_str = @views attribute[1:end] + JuMP.set_optimizer_attribute(model, abstract_str, "x") + @test JuMP.get_optimizer_attribute(model, abstract_str) == "x" + @test JuMP.get_optimizer_attribute(model, attribute) == "x" + return +end + function test_set_retrieve_time_limit() mock = MOIU.UniversalFallback(MOIU.Model{Float64}()) model = Model(() -> MOIU.MockOptimizer(mock))