From f495d977a3189283ec0c74741a3da0dadbb3ddb9 Mon Sep 17 00:00:00 2001 From: hannes-uppman Date: Fri, 8 Sep 2023 01:30:27 +0200 Subject: [PATCH] Add support for MOI.AbsoluteGapTolerance and MOI.RelativeGapTolerance (#219) --- Project.toml | 2 +- src/MOI_wrapper/MOI_wrapper.jl | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 193fa7e..eb0686d 100644 --- a/Project.toml +++ b/Project.toml @@ -10,7 +10,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [compat] Cbc_jll = "=2.10.5, =200.1000.501, =200.1000.800" -MathOptInterface = "1" +MathOptInterface = "1.7" julia = "1.6" [extras] diff --git a/src/MOI_wrapper/MOI_wrapper.jl b/src/MOI_wrapper/MOI_wrapper.jl index cb2af0a..39f7cc7 100644 --- a/src/MOI_wrapper/MOI_wrapper.jl +++ b/src/MOI_wrapper/MOI_wrapper.jl @@ -181,6 +181,42 @@ function MOI.get(model::Optimizer, ::MOI.TimeLimitSec) return value === nothing ? value : parse(Float64, value) end +MOI.supports(::Optimizer, ::MOI.AbsoluteGapTolerance) = true + +function MOI.set(model::Optimizer, ::MOI.AbsoluteGapTolerance, value::Real) + MOI.set(model, MOI.RawOptimizerAttribute("allowableGap"), value) + return +end + +function MOI.set(model::Optimizer, ::MOI.AbsoluteGapTolerance, ::Nothing) + delete!(model.params, "allowableGap") + Cbc_setParameter(model, "allowableGap", "InvalidDoubleValue") + return +end + +function MOI.get(model::Optimizer, ::MOI.AbsoluteGapTolerance) + value = get(model.params, "allowableGap", nothing) + return value === nothing ? value : parse(Float64, value) +end + +MOI.supports(::Optimizer, ::MOI.RelativeGapTolerance) = true + +function MOI.set(model::Optimizer, ::MOI.RelativeGapTolerance, value::Real) + MOI.set(model, MOI.RawOptimizerAttribute("ratioGap"), value) + return +end + +function MOI.set(model::Optimizer, ::MOI.RelativeGapTolerance, ::Nothing) + delete!(model.params, "ratioGap") + Cbc_setParameter(model, "ratioGap", "InvalidDoubleValue") + return +end + +function MOI.get(model::Optimizer, ::MOI.RelativeGapTolerance) + value = get(model.params, "ratioGap", nothing) + return value === nothing ? value : parse(Float64, value) +end + MOI.get(::Optimizer, ::MOI.SolverName) = "COIN Branch-and-Cut (Cbc)" MOI.get(::Optimizer, ::MOI.SolverVersion) = unsafe_string(Cbc_getVersion())