Skip to content

Commit b0eba28

Browse files
authored
Update test coverage (#230)
1 parent 0424af2 commit b0eba28

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/Cbc.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
module Cbc
77

8-
import Cbc_jll
9-
import MathOptInterface
8+
import Cbc_jll: libcbcsolver
9+
import MathOptInterface as MOI
1010
import SparseArrays
1111

1212
function __init__()
13-
global libcbcsolver = Cbc_jll.libcbcsolver
1413
version_str = unsafe_string(Cbc_getVersion())
1514
version = if version_str == "devel"
1615
# Support un-released versions of Cbc. These may differ in C API

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Use of this source code is governed by an MIT-style license that can be found
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

6-
const MOI = MathOptInterface
7-
86
MOI.Utilities.@product_of_sets(
97
_LPProductOfSets,
108
MOI.EqualTo{T},
@@ -129,9 +127,6 @@ function MOI.set(
129127
param::MOI.RawOptimizerAttribute,
130128
value::String,
131129
)
132-
if !MOI.supports(model, param)
133-
throw(MOI.UnsupportedAttribute(param))
134-
end
135130
model.params[param.name] = value
136131
if param.name == "threads" && Sys.iswindows()
137132
@warn(
@@ -389,7 +384,8 @@ function MOI.copy_to(dest::Optimizer, src::OptimizerCache)
389384
for ci in MOI.get(src, attr)
390385
Cbc_setInteger(dest, Cint(ci.value - 1))
391386
end
392-
if MOI.VariableName() in MOI.get(src, MOI.ListOfVariableAttributesSet())
387+
if MOI.VariableName() in MOI.get(src, MOI.ListOfVariableAttributesSet()) &&
388+
MOI.get(dest, SetVariableNames())::Bool
393389
for x in MOI.get(src, MOI.ListOfVariableIndices())
394390
name = MOI.get(src, MOI.VariableName(), x)
395391
if !isempty(name) && isascii(name)
@@ -556,7 +552,13 @@ end
556552
### VariableName
557553
###
558554

559-
MOI.supports(::Optimizer, ::MOI.VariableName, ::Type{MOI.VariableIndex}) = true
555+
function MOI.supports(
556+
model::Optimizer,
557+
::MOI.VariableName,
558+
::Type{MOI.VariableIndex},
559+
)
560+
return model.set_names
561+
end
560562

561563
function MOI.set(
562564
model::Optimizer,

test/MOI_wrapper.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function test_params()
9191
)
9292
model = Cbc.Optimizer()
9393
MOI.set(model, MOI.RawOptimizerAttribute("maxSol"), 1)
94+
@test MOI.get(model, MOI.RawOptimizerAttribute("maxSol")) == "1"
9495
MOI.set(model, MOI.RawOptimizerAttribute("presolve"), "off")
9596
MOI.set(model, MOI.RawOptimizerAttribute("cuts"), "off")
9697
MOI.set(model, MOI.RawOptimizerAttribute("heur"), "off")
@@ -104,6 +105,11 @@ function test_params()
104105
MOI.optimize!(model)
105106
@test MOI.get(model, MOI.TerminationStatus()) == MOI.SOLUTION_LIMIT
106107
@test MOI.get(model, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
108+
@test MOI.get(model, MOI.RelativeGap()) >= 0
109+
@test MOI.is_set_by_optimize(Cbc.Status())
110+
@test MOI.get(model, Cbc.Status()) == 1
111+
@test MOI.is_set_by_optimize(Cbc.SecondaryStatus())
112+
@test MOI.get(model, Cbc.SecondaryStatus()) == 6
107113
return
108114
end
109115

@@ -167,7 +173,9 @@ function test_issue_187()
167173
MOI.set(model, MOI.Silent(), true)
168174
x = MOI.add_variables(model, 2)
169175
MOI.add_constraint.(model, x, MOI.ZeroOne())
176+
@test MOI.get.(model, MOI.VariablePrimalStart(), x) == [nothing, nothing]
170177
MOI.set.(model, MOI.VariablePrimalStart(), x, 0.0)
178+
@test MOI.get.(model, MOI.VariablePrimalStart(), x) == [0.0, 0.0]
171179
y = MOI.add_variables(model, 2)
172180
MOI.add_constraint.(model, y, MOI.ZeroOne())
173181

@@ -439,7 +447,9 @@ function test_variable_name()
439447
x = MOI.add_variable(model)
440448
MOI.set(model, MOI.VariableName(), x, name)
441449
cbc = Cbc.Optimizer()
450+
@test !MOI.supports(cbc, MOI.VariableName(), MOI.VariableIndex)
442451
MOI.set(cbc, Cbc.SetVariableNames(), true)
452+
@test MOI.supports(cbc, MOI.VariableName(), MOI.VariableIndex)
443453
index_map = MOI.copy_to(cbc, model)
444454
@test MOI.get(cbc, MOI.VariableName(), index_map[x]) == inner
445455
end
@@ -466,6 +476,17 @@ function test_segfault()
466476
return
467477
end
468478

479+
function test_get_objective_sense()
480+
for sense in (MOI.MIN_SENSE, MOI.MAX_SENSE, MOI.FEASIBILITY_SENSE)
481+
model = Cbc.Optimizer()
482+
src = MOI.Utilities.Model{Float64}()
483+
MOI.set(src, MOI.ObjectiveSense(), sense)
484+
MOI.copy_to(model, src)
485+
@test MOI.get(model, MOI.ObjectiveSense()) == sense
486+
end
487+
return
488+
end
489+
469490
end
470491

471492
TestMOIWrapper.runtests()

0 commit comments

Comments
 (0)