Skip to content

Improve UnsupportedConstraint error #2530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 15 additions & 12 deletions test/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,29 @@ 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
Loading