Skip to content

Commit

Permalink
Update test coverage (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 27, 2024
1 parent 0424af2 commit b0eba28
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/Cbc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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
Expand All @@ -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()

0 comments on commit b0eba28

Please sign in to comment.