Skip to content

Commit

Permalink
Fix support for MOI.TimeLimitSec (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Aug 17, 2023
1 parent 3a5bab7 commit ca53bb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
optimize_called::Bool
solve_time::Float64
# Work-around for upstream bug in Clp:
maximumSeconds::Float64
maximumSeconds::Union{Float64,Nothing}

function Optimizer()
model =
new(Clp_newModel(), ClpSolve_new(), Set{String}(), false, 0.0, -1.0)
model = new(
Clp_newModel(),
ClpSolve_new(),
Set{String}(),
false,
0.0,
nothing,
)
finalizer(model) do m
Clp_deleteModel(m)
ClpSolve_delete(m.solver_options)
Expand Down Expand Up @@ -112,7 +118,7 @@ function MOI.empty!(model::Optimizer)
MOI.set(model, MOI.RawOptimizerAttribute(key), value)
end
# Work-around for maximumSeconds
Clp_setMaximumSeconds(model, model.maximumSeconds)
Clp_setMaximumSeconds(model, something(model.maximumSeconds, -1.0))
return
end

Expand Down Expand Up @@ -220,11 +226,18 @@ MOI.get(model::Optimizer, ::MOI.Silent) = Clp_logLevel(model) == 0

MOI.supports(::Optimizer, ::MOI.TimeLimitSec) = true

function MOI.set(model::Optimizer, ::MOI.TimeLimitSec, value)
function MOI.set(model::Optimizer, ::MOI.TimeLimitSec, value::Real)
push!(model.options_set, "MaximumSeconds")
value = value === nothing ? -1.0 : value
Clp_setMaximumSeconds(model, value)
model.maximumSeconds = value
float_value = convert(Float64, value)
Clp_setMaximumSeconds(model, float_value)
model.maximumSeconds = float_value
return
end

function MOI.set(model::Optimizer, ::MOI.TimeLimitSec, ::Nothing)
delete!(model.options_set, "MaximumSeconds")
Clp_setMaximumSeconds(model, -1.0)
model.maximumSeconds = nothing
return
end

Expand Down
14 changes: 14 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ function test_options_after_empty!()
return
end

function test_attribute_TimeLimitSec()
model = Clp.Optimizer()
@test MOI.supports(model, MOI.TimeLimitSec())
value = MOI.get(model, MOI.TimeLimitSec())
MOI.set(model, MOI.TimeLimitSec(), 0.0)
@test MOI.get(model, MOI.TimeLimitSec()) == 0.0
MOI.set(model, MOI.TimeLimitSec(), nothing)
@test MOI.get(model, MOI.TimeLimitSec()) === nothing
MOI.set(model, MOI.TimeLimitSec(), 1.0)
@test MOI.get(model, MOI.TimeLimitSec()) == 1.0
MOI.set(model, MOI.TimeLimitSec(), value)
return
end

end # module TestMOIWrapper

TestMOIWrapper.runtests()

0 comments on commit ca53bb7

Please sign in to comment.