Skip to content

Improve text of showerror for NotAllowedError #2757

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 4 commits into from
May 13, 2025
Merged

Improve text of showerror for NotAllowedError #2757

merged 4 commits into from
May 13, 2025

Conversation

odow
Copy link
Member

@odow odow commented May 13, 2025

Closes #2756

I now get

julia> import MathOptInterface as MOI

julia> begin
           struct Model2756 <: MOI.ModelLike
               variables::Vector{Float64}
               Model2756() = new(Float64[])
           end
           MOI.is_empty(model::Model2756) = isempty(model.variables)
           MOI.empty!(model::Model2756) = (empty!(model.variables); nothing)
           MOI.supports_incremental_interface(::Model2756) = true
           function MOI.supports_constraint(
               ::Model2756,
               ::Type{MOI.VariableIndex},
               ::Type{MOI.GreaterThan{Float64}},
           )
               return true
           end
           function MOI.add_variable(model::Model2756)
               push!(model.variables, -Inf)
               return MOI.VariableIndex(length(model.variables))
           end
           function MOI.add_constraint(
               model::Model2756,
               x::F,
               s::S,
           ) where {F<:MOI.VariableIndex,S<:MOI.GreaterThan}
               model.variables[x.value] = s.lower
               return MOI.ConstraintIndex{F,S}(x.value)
           end
           model = MOI.instantiate(Model2756; with_bridge_type = Float64)
           x = MOI.add_variables(model, 2)
           MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.Nonnegatives(2))
           MOI.Bridges.print_active_bridges(model)
       end
ERROR: MathOptInterface.GetAttributeNotAllowed{MathOptInterface.ObjectiveFunctionType}:

## Cause

Getting attribute MathOptInterface.ObjectiveFunctionType() cannot be performed because:

Model2756 does not support getting the attribute MathOptInterface.ObjectiveFunctionType().

## Fixing this error

An `MOI.NotAllowedError` error occurs when you have tried to do something that
is not implemented by the solver.

The most common way to fix this error is to wrap the optimizer in a
`MOI.Utilities.CachingOptimizer`.

For example, if you are using `JuMP.Model` or `JuMP.set_optimizer`, do:
```julia
model = JuMP.Model(optimizer; with_cache_type = Float64)
model = JuMP.GenericModel{T}(optimizer; with_cache_type = T)
JuMP.set_optimizer(model, optimizer; with_cache_type = Float64)
```
Similarly, if you are using `MOI.instantiate`, do:
```julia
model = MOI.instantiate(optimizer; with_cache_type = Float64)
```

Stacktrace:
 [1] get_fallback(model::Model2756, attr::MathOptInterface.ObjectiveFunctionType)
   @ MathOptInterface ~/.julia/dev/MathOptInterface/src/attributes.jl:409
 [2] get(::Model2756, ::MathOptInterface.ObjectiveFunctionType)
   @ MathOptInterface ~/.julia/dev/MathOptInterface/src/attributes.jl:390
 [3] get
   @ ~/.julia/dev/MathOptInterface/src/Bridges/bridge_optimizer.jl:1177 [inlined]
 [4] print_active_bridges(io::Base.TTY, b::MathOptInterface.Bridges.LazyBridgeOptimizer{Model2756})
   @ MathOptInterface.Bridges ~/.julia/dev/MathOptInterface/src/Bridges/debug.jl:544
 [5] print_active_bridges(::MathOptInterface.Bridges.LazyBridgeOptimizer{Model2756})
   @ MathOptInterface.Bridges ~/.julia/dev/MathOptInterface/src/Bridges/debug.jl:556
 [6] top-level scope
   @ REPL[42]:31

@blegat
Copy link
Member

blegat commented May 13, 2025

Couldn't be clearer, I like it

@odow odow merged commit 1d5ee59 into master May 13, 2025
31 checks passed
@odow odow deleted the od/not-allowed branch May 13, 2025 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Improve error message of print_active_bridges
2 participants