Skip to content

Commit e99dff8

Browse files
authored
[FileFormats] fix writing unsupported variable types (#2654)
1 parent 339d769 commit e99dff8

File tree

7 files changed

+108
-9
lines changed

7 files changed

+108
-9
lines changed

src/FileFormats/LP/LP.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ MOI.Utilities.@model(
4646
(MOI.VectorAffineFunction,)
4747
)
4848

49+
function MOI.supports_constraint(
50+
::Model,
51+
::Type{MOI.VariableIndex},
52+
::Type{<:Union{MOI.Parameter,MOI.Semicontinuous,MOI.Semiinteger}},
53+
)
54+
return false
55+
end
56+
4957
function MOI.supports_constraint(
5058
::Model{T},
5159
::Type{MOI.VectorAffineFunction{T}},

src/FileFormats/MPS/MPS.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ function MOI.supports_constraint(
7474
return false
7575
end
7676

77+
function MOI.supports_constraint(
78+
::Model,
79+
::Type{MOI.VariableIndex},
80+
::Type{<:Union{MOI.Parameter,MOI.Semicontinuous,MOI.Semiinteger}},
81+
)
82+
return false
83+
end
84+
7785
function MOI.supports_constraint(
7886
::Model{T},
7987
::Type{MOI.VectorOfVariables},

test/FileFormats/CBF/CBF.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,23 @@ function test_supports_quadratic_objective()
699699
return
700700
end
701701

702+
function test_unsupported_variable_types()
703+
model = CBF.Model()
704+
@test_throws(
705+
MOI.UnsupportedConstraint,
706+
MOI.add_constrained_variable(model, MOI.Parameter(2.0)),
707+
)
708+
@test_throws(
709+
MOI.UnsupportedConstraint,
710+
MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)),
711+
)
712+
@test_throws(
713+
MOI.UnsupportedConstraint,
714+
MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)),
715+
)
716+
return
717+
end
718+
702719
end # module
703720

704721
TestCBF.runtests()

test/FileFormats/LP/LP.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,23 @@ function test_unable_to_parse_bound()
11131113
return
11141114
end
11151115

1116+
function test_unsupported_variable_types()
1117+
model = LP.Model()
1118+
@test_throws(
1119+
MOI.UnsupportedConstraint,
1120+
MOI.add_constrained_variable(model, MOI.Parameter(2.0)),
1121+
)
1122+
@test_throws(
1123+
MOI.UnsupportedConstraint,
1124+
MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)),
1125+
)
1126+
@test_throws(
1127+
MOI.UnsupportedConstraint,
1128+
MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)),
1129+
)
1130+
return
1131+
end
1132+
11161133
end # module
11171134

11181135
TestLP.runtests()

test/FileFormats/MPS/MPS.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,23 @@ function test_malformed_indicator()
15331533
return
15341534
end
15351535

1536+
function test_unsupported_variable_types()
1537+
model = MPS.Model()
1538+
@test_throws(
1539+
MOI.UnsupportedConstraint,
1540+
MOI.add_constrained_variable(model, MOI.Parameter(2.0)),
1541+
)
1542+
@test_throws(
1543+
MOI.UnsupportedConstraint,
1544+
MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)),
1545+
)
1546+
@test_throws(
1547+
MOI.UnsupportedConstraint,
1548+
MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)),
1549+
)
1550+
return
1551+
end
1552+
15361553
end # TestMPS
15371554

15381555
TestMPS.runtests()

test/FileFormats/NL/NL.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66

77
module TestNLModel
88

9+
using Test
10+
911
import MathOptInterface as MOI
10-
const NL = MOI.FileFormats.NL
12+
import MathOptInterface.FileFormats: NL
1113

12-
using Test
14+
function runtests()
15+
for name in names(@__MODULE__; all = true)
16+
if startswith("$(name)", "test_")
17+
@testset "$(name)" begin
18+
getfield(@__MODULE__, name)()
19+
end
20+
end
21+
end
22+
return
23+
end
1324

1425
function _test_nlexpr(
1526
expr::NL._NLExpr,
@@ -1352,14 +1363,18 @@ function test_copy_name_issue_2445()
13521363
return
13531364
end
13541365

1355-
function runtests()
1356-
for name in names(@__MODULE__; all = true)
1357-
if startswith("$(name)", "test_")
1358-
@testset "$(name)" begin
1359-
getfield(@__MODULE__, name)()
1360-
end
1361-
end
1366+
function test_unsupported_variable_types()
1367+
for set in (
1368+
MOI.Parameter(2.0),
1369+
MOI.Semicontinuous(2.0, 3.0),
1370+
MOI.Semiinteger(2.0, 3.0),
1371+
)
1372+
src = MOI.Utilities.Model{Float64}()
1373+
MOI.add_constrained_variable(src, set)
1374+
dest = NL.Model()
1375+
@test_throws MOI.UnsupportedConstraint MOI.copy_to(dest, src)
13621376
end
1377+
return
13631378
end
13641379

13651380
end

test/FileFormats/SDPA/SDPA.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,23 @@ function test_integer_before_variables()
363363
return
364364
end
365365

366+
function test_unsupported_variable_types()
367+
model = SDPA.Model()
368+
@test_throws(
369+
MOI.UnsupportedConstraint,
370+
MOI.add_constrained_variable(model, MOI.Parameter(2.0)),
371+
)
372+
@test_throws(
373+
MOI.UnsupportedConstraint,
374+
MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)),
375+
)
376+
@test_throws(
377+
MOI.UnsupportedConstraint,
378+
MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)),
379+
)
380+
return
381+
end
382+
366383
end # module
367384

368385
TestSDPA.runtests()

0 commit comments

Comments
 (0)