From 7b8171cd211ed0f6858961c87bf5134a30b242bd Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 2 Aug 2024 11:46:37 +1200 Subject: [PATCH 1/5] Improve UnsupportedConstraint error --- src/constraints.jl | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index 9cee1cbcfa..e8f13bed29 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -26,21 +26,42 @@ function supports_constraint( end """ - struct UnsupportedConstraint{F<:AbstractFunction, S<:AbstractSet} <: UnsupportedError - message::String # Human-friendly explanation why the attribute cannot be set + struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: UnsupportedError + message::String end An error indicating that constraints of type `F`-in-`S` are not supported by the model, that is, that [`supports_constraint`](@ref) returns `false`. + +```jldoctest +julia> import MathOptInterface as MOI + +julia> showerror(MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.ZeroOne}()) +``` """ struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: UnsupportedError - message::String # Human-friendly explanation why the attribute cannot be set + # Human-friendly explanation why the attribute cannot be set + message::String end + UnsupportedConstraint{F,S}() where {F,S} = UnsupportedConstraint{F,S}("") -function element_name(::UnsupportedConstraint{F,S}) where {F,S} - return "`$F`-in-`$S` constraint" +function Base.showerror(io::IO, err::UnsupportedConstraint{F,S}) where {F,S} + print( + io, + """ + UnsupportedConstraint: `$F`-in-`$S` constraints are not supported by the + solver you have chosen, and we could not reformulate your model into a + form that is supported. + + To fix this error you must choose a different solver. + """, + ) + if !isempty(err.message) + print(io, "\n", err.message) + end + return end """ From 5c041abcf3f1fc65525544d99098f339810311c0 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 2 Aug 2024 11:57:29 +1200 Subject: [PATCH 2/5] Update --- test/errors.jl | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/test/errors.jl b/test/errors.jl index 459e010eb2..ac91a6c888 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -46,26 +46,28 @@ function test_errors_inconsistent_vectorscalar() return end -function _test_errors_UnsupportedConstraint(f) +function test_errors_UnsupportedConstraint() model = DummyModel() vi = MOI.VariableIndex(1) - @test_throws(MOI.UnsupportedConstraint, f(model, vi, MOI.EqualTo(0)),) + @test_throws( + MOI.UnsupportedConstraint, + MOI.add_constraint(model, vi, MOI.EqualTo(0)), + ) + msg = """ + UnsupportedConstraint: `$(MOI.VariableIndex)`-in-`$(MOI.EqualTo{Int})` constraints are not supported by the + solver you have chosen, and we could not reformulate your model into a + form that is supported. + + To fix this error you must choose a different solver. + """ try - f(model, vi, MOI.EqualTo(0)) + MOI.add_constraint(model, vi, MOI.EqualTo(0)) catch err - @test sprint(showerror, err) == - "$(MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.EqualTo{Int}}):" * - " `$MOI.VariableIndex`-in-`$MOI.EqualTo{$Int}` constraint is" * - " not supported by the model." + @test sprint(showerror, err) == msg end return end -function test_errors_UnsupportedConstraint() - _test_errors_UnsupportedConstraint(MOI.add_constraint) - return -end - function test_errors_UnsupportedConstraint_shortcut() model = DummyModel() vi = MOI.VariableIndex(1) From 564d3f9df461a8c897f31aa7bd7dda9ecb2cd8c4 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 2 Aug 2024 11:58:34 +1200 Subject: [PATCH 3/5] Update constraints.jl --- src/constraints.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/constraints.jl b/src/constraints.jl index e8f13bed29..ffc1e4a42d 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -36,7 +36,12 @@ the model, that is, that [`supports_constraint`](@ref) returns `false`. ```jldoctest julia> import MathOptInterface as MOI -julia> showerror(MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.ZeroOne}()) +julia> showerror(stdout, MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.ZeroOne}()) +UnsupportedConstraint: `MathOptInterface.VariableIndex`-in-`MathOptInterface.ZeroOne` constraints are not supported by the +solver you have chosen, and we could not reformulate your model into a +form that is supported. + +To fix this error you must choose a different solver. ``` """ struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: From d044badaa46a3fb84d586decc0d24ae9b257ecc2 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 2 Aug 2024 15:48:10 +1200 Subject: [PATCH 4/5] Update --- src/constraints.jl | 6 +++--- test/errors.jl | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index ffc1e4a42d..03550dca77 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -42,6 +42,7 @@ solver you have chosen, and we could not reformulate your model into a form that is supported. To fix this error you must choose a different solver. + ``` """ struct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: @@ -61,11 +62,10 @@ function Base.showerror(io::IO, err::UnsupportedConstraint{F,S}) where {F,S} form that is supported. To fix this error you must choose a different solver. + + $(err.message) """, ) - if !isempty(err.message) - print(io, "\n", err.message) - end return end diff --git a/test/errors.jl b/test/errors.jl index ac91a6c888..4a3226d93b 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -59,6 +59,7 @@ function test_errors_UnsupportedConstraint() form that is supported. To fix this error you must choose a different solver. + """ try MOI.add_constraint(model, vi, MOI.EqualTo(0)) From 28bb805e6a6298b76a79510e6f6f5c6b504b9735 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 2 Aug 2024 15:57:47 +1200 Subject: [PATCH 5/5] Update test/errors.jl --- test/errors.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/errors.jl b/test/errors.jl index 4a3226d93b..baeedd87b1 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -60,6 +60,7 @@ function test_errors_UnsupportedConstraint() To fix this error you must choose a different solver. + """ try MOI.add_constraint(model, vi, MOI.EqualTo(0))