Skip to content

Commit

Permalink
Fix optimizer_attribute with AbstractString names (#3127)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Nov 8, 2022
1 parent 31fd986 commit 8827a96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 8827a96

Please sign in to comment.