From d67c9ce3da5cc2fe72cc83e189cb4bf2dd024617 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 24 Feb 2025 09:55:41 +1300 Subject: [PATCH] [FileFormats] fix writing unsupported variable types --- src/FileFormats/LP/LP.jl | 8 ++++++++ src/FileFormats/MPS/MPS.jl | 8 ++++++++ test/FileFormats/CBF/CBF.jl | 17 +++++++++++++++++ test/FileFormats/LP/LP.jl | 17 +++++++++++++++++ test/FileFormats/MPS/MPS.jl | 17 +++++++++++++++++ test/FileFormats/NL/NL.jl | 33 ++++++++++++++++++++++++--------- test/FileFormats/SDPA/SDPA.jl | 17 +++++++++++++++++ 7 files changed, 108 insertions(+), 9 deletions(-) diff --git a/src/FileFormats/LP/LP.jl b/src/FileFormats/LP/LP.jl index 0e9794ea10..bfea31233e 100644 --- a/src/FileFormats/LP/LP.jl +++ b/src/FileFormats/LP/LP.jl @@ -46,6 +46,14 @@ MOI.Utilities.@model( (MOI.VectorAffineFunction,) ) +function MOI.supports_constraint( + ::Model, + ::Type{MOI.VariableIndex}, + ::Type{<:Union{MOI.Parameter,MOI.Semicontinuous,MOI.Semiinteger}}, +) + return false +end + function MOI.supports_constraint( ::Model{T}, ::Type{MOI.VectorAffineFunction{T}}, diff --git a/src/FileFormats/MPS/MPS.jl b/src/FileFormats/MPS/MPS.jl index 3936284f55..ae2512dfff 100644 --- a/src/FileFormats/MPS/MPS.jl +++ b/src/FileFormats/MPS/MPS.jl @@ -74,6 +74,14 @@ function MOI.supports_constraint( return false end +function MOI.supports_constraint( + ::Model, + ::Type{MOI.VariableIndex}, + ::Type{<:Union{MOI.Parameter,MOI.Semicontinuous,MOI.Semiinteger}}, +) + return false +end + function MOI.supports_constraint( ::Model{T}, ::Type{MOI.VectorOfVariables}, diff --git a/test/FileFormats/CBF/CBF.jl b/test/FileFormats/CBF/CBF.jl index ad40971891..943a67f1d6 100644 --- a/test/FileFormats/CBF/CBF.jl +++ b/test/FileFormats/CBF/CBF.jl @@ -699,6 +699,23 @@ function test_supports_quadratic_objective() return end +function test_unsupported_variable_types() + model = CBF.Model() + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Parameter(2.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)), + ) + return +end + end # module TestCBF.runtests() diff --git a/test/FileFormats/LP/LP.jl b/test/FileFormats/LP/LP.jl index 13d7b9e052..58ed321d11 100644 --- a/test/FileFormats/LP/LP.jl +++ b/test/FileFormats/LP/LP.jl @@ -1113,6 +1113,23 @@ function test_unable_to_parse_bound() return end +function test_unsupported_variable_types() + model = LP.Model() + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Parameter(2.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)), + ) + return +end + end # module TestLP.runtests() diff --git a/test/FileFormats/MPS/MPS.jl b/test/FileFormats/MPS/MPS.jl index b7eb6af276..dbe230d0dd 100644 --- a/test/FileFormats/MPS/MPS.jl +++ b/test/FileFormats/MPS/MPS.jl @@ -1533,6 +1533,23 @@ function test_malformed_indicator() return end +function test_unsupported_variable_types() + model = MPS.Model() + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Parameter(2.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)), + ) + return +end + end # TestMPS TestMPS.runtests() diff --git a/test/FileFormats/NL/NL.jl b/test/FileFormats/NL/NL.jl index fcdb676a46..12fc463056 100644 --- a/test/FileFormats/NL/NL.jl +++ b/test/FileFormats/NL/NL.jl @@ -6,10 +6,21 @@ module TestNLModel +using Test + import MathOptInterface as MOI -const NL = MOI.FileFormats.NL +import MathOptInterface.FileFormats: NL -using Test +function runtests() + for name in names(@__MODULE__; all = true) + if startswith("$(name)", "test_") + @testset "$(name)" begin + getfield(@__MODULE__, name)() + end + end + end + return +end function _test_nlexpr( expr::NL._NLExpr, @@ -1352,14 +1363,18 @@ function test_copy_name_issue_2445() return end -function runtests() - for name in names(@__MODULE__; all = true) - if startswith("$(name)", "test_") - @testset "$(name)" begin - getfield(@__MODULE__, name)() - end - end +function test_unsupported_variable_types() + for set in ( + MOI.Parameter(2.0), + MOI.Semicontinuous(2.0, 3.0), + MOI.Semiinteger(2.0, 3.0), + ) + src = MOI.Utilities.Model{Float64}() + MOI.add_constrained_variable(src, set) + dest = NL.Model() + @test_throws MOI.UnsupportedConstraint MOI.copy_to(dest, src) end + return end end diff --git a/test/FileFormats/SDPA/SDPA.jl b/test/FileFormats/SDPA/SDPA.jl index afd28e3d9f..4e3dcd4c8f 100644 --- a/test/FileFormats/SDPA/SDPA.jl +++ b/test/FileFormats/SDPA/SDPA.jl @@ -363,6 +363,23 @@ function test_integer_before_variables() return end +function test_unsupported_variable_types() + model = SDPA.Model() + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Parameter(2.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)), + ) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)), + ) + return +end + end # module TestSDPA.runtests()