Skip to content

Commit

Permalink
Improve UnsupportedConstraint error (#2530)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Aug 3, 2024
1 parent a15b67f commit 083be27
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
36 changes: 31 additions & 5 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,47 @@ 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(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} <:
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.
$(err.message)
""",
)
return
end

"""
Expand Down
28 changes: 16 additions & 12 deletions test/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,30 @@ 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)
Expand Down

0 comments on commit 083be27

Please sign in to comment.