Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for MOI.TimeLimitSec #146

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading