diff --git a/src/Cbc.jl b/src/Cbc.jl index 6ea6adb..24ea845 100644 --- a/src/Cbc.jl +++ b/src/Cbc.jl @@ -5,12 +5,11 @@ module Cbc -import Cbc_jll -import MathOptInterface +import Cbc_jll: libcbcsolver +import MathOptInterface as MOI import SparseArrays function __init__() - global libcbcsolver = Cbc_jll.libcbcsolver version_str = unsafe_string(Cbc_getVersion()) version = if version_str == "devel" # Support un-released versions of Cbc. These may differ in C API diff --git a/src/MOI_wrapper/MOI_wrapper.jl b/src/MOI_wrapper/MOI_wrapper.jl index 1360d0e..2264549 100644 --- a/src/MOI_wrapper/MOI_wrapper.jl +++ b/src/MOI_wrapper/MOI_wrapper.jl @@ -3,8 +3,6 @@ # Use of this source code is governed by an MIT-style license that can be found # in the LICENSE.md file or at https://opensource.org/licenses/MIT. -const MOI = MathOptInterface - MOI.Utilities.@product_of_sets( _LPProductOfSets, MOI.EqualTo{T}, @@ -129,9 +127,6 @@ function MOI.set( param::MOI.RawOptimizerAttribute, value::String, ) - if !MOI.supports(model, param) - throw(MOI.UnsupportedAttribute(param)) - end model.params[param.name] = value if param.name == "threads" && Sys.iswindows() @warn( @@ -389,7 +384,8 @@ function MOI.copy_to(dest::Optimizer, src::OptimizerCache) for ci in MOI.get(src, attr) Cbc_setInteger(dest, Cint(ci.value - 1)) end - if MOI.VariableName() in MOI.get(src, MOI.ListOfVariableAttributesSet()) + if MOI.VariableName() in MOI.get(src, MOI.ListOfVariableAttributesSet()) && + MOI.get(dest, SetVariableNames())::Bool for x in MOI.get(src, MOI.ListOfVariableIndices()) name = MOI.get(src, MOI.VariableName(), x) if !isempty(name) && isascii(name) @@ -556,7 +552,13 @@ end ### VariableName ### -MOI.supports(::Optimizer, ::MOI.VariableName, ::Type{MOI.VariableIndex}) = true +function MOI.supports( + model::Optimizer, + ::MOI.VariableName, + ::Type{MOI.VariableIndex}, +) + return model.set_names +end function MOI.set( model::Optimizer, diff --git a/test/MOI_wrapper.jl b/test/MOI_wrapper.jl index 92912e6..9344d6d 100644 --- a/test/MOI_wrapper.jl +++ b/test/MOI_wrapper.jl @@ -91,6 +91,7 @@ function test_params() ) model = Cbc.Optimizer() MOI.set(model, MOI.RawOptimizerAttribute("maxSol"), 1) + @test MOI.get(model, MOI.RawOptimizerAttribute("maxSol")) == "1" MOI.set(model, MOI.RawOptimizerAttribute("presolve"), "off") MOI.set(model, MOI.RawOptimizerAttribute("cuts"), "off") MOI.set(model, MOI.RawOptimizerAttribute("heur"), "off") @@ -104,6 +105,11 @@ function test_params() MOI.optimize!(model) @test MOI.get(model, MOI.TerminationStatus()) == MOI.SOLUTION_LIMIT @test MOI.get(model, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT + @test MOI.get(model, MOI.RelativeGap()) >= 0 + @test MOI.is_set_by_optimize(Cbc.Status()) + @test MOI.get(model, Cbc.Status()) == 1 + @test MOI.is_set_by_optimize(Cbc.SecondaryStatus()) + @test MOI.get(model, Cbc.SecondaryStatus()) == 6 return end @@ -167,7 +173,9 @@ function test_issue_187() MOI.set(model, MOI.Silent(), true) x = MOI.add_variables(model, 2) MOI.add_constraint.(model, x, MOI.ZeroOne()) + @test MOI.get.(model, MOI.VariablePrimalStart(), x) == [nothing, nothing] MOI.set.(model, MOI.VariablePrimalStart(), x, 0.0) + @test MOI.get.(model, MOI.VariablePrimalStart(), x) == [0.0, 0.0] y = MOI.add_variables(model, 2) MOI.add_constraint.(model, y, MOI.ZeroOne()) @@ -439,7 +447,9 @@ function test_variable_name() x = MOI.add_variable(model) MOI.set(model, MOI.VariableName(), x, name) cbc = Cbc.Optimizer() + @test !MOI.supports(cbc, MOI.VariableName(), MOI.VariableIndex) MOI.set(cbc, Cbc.SetVariableNames(), true) + @test MOI.supports(cbc, MOI.VariableName(), MOI.VariableIndex) index_map = MOI.copy_to(cbc, model) @test MOI.get(cbc, MOI.VariableName(), index_map[x]) == inner end @@ -466,6 +476,17 @@ function test_segfault() return end +function test_get_objective_sense() + for sense in (MOI.MIN_SENSE, MOI.MAX_SENSE, MOI.FEASIBILITY_SENSE) + model = Cbc.Optimizer() + src = MOI.Utilities.Model{Float64}() + MOI.set(src, MOI.ObjectiveSense(), sense) + MOI.copy_to(model, src) + @test MOI.get(model, MOI.ObjectiveSense()) == sense + end + return +end + end TestMOIWrapper.runtests()