Errors
When an MOI call fails on a model, precise errors should be thrown when possible instead of simply calling error with a message. The docstrings for the respective methods describe the errors that the implementation should throw in certain situations. This error-reporting system allows code to distinguish between internal errors (that should be shown to the user) and unsupported operations which may have automatic workarounds.
When an invalid index is used in an MOI call, an InvalidIndex is thrown:
MathOptInterface.InvalidIndex — Typestruct InvalidIndex{IndexType<:Index} <: Exception
index::IndexType
endAn error indicating that the index index is invalid.
When an invalid result index is used to retrieve an attribute, a ResultIndexBoundsError is thrown:
MathOptInterface.ResultIndexBoundsError — Typestruct ResultIndexBoundsError{AttrType} <: Exception
attr::AttrType
result_count::Int
endAn error indicating that the requested attribute attr could not be retrieved, because the solver returned too few results compared to what was requested. For instance, the user tries to retrieve VariablePrimal(2) when only one solution is available, or when the model is infeasible and has no solution.
See also: check_result_index_bounds.
MathOptInterface.check_result_index_bounds — Functioncheck_result_index_bounds(model::ModelLike, attr)This function checks whether enough results are available in the model for the requested attr, using its result_index field. If the model does not have sufficient results to answer the query, it throws a ResultIndexBoundsError.
As discussed in JuMP mapping, for scalar constraint with a nonzero function constant, a ScalarFunctionConstantNotZero exception may be thrown:
MathOptInterface.ScalarFunctionConstantNotZero — Typestruct ScalarFunctionConstantNotZero{T, F, S} <: Exception
constant::T
endAn error indicating that the constant part of the function in the constraint F-in-S is nonzero.
Some VariableIndex constraints cannot be combined on the same variable:
MathOptInterface.LowerBoundAlreadySet — TypeLowerBoundAlreadySet{S1, S2}Error thrown when setting a VariableIndex-in-S2 when a VariableIndex-in-S1 has already been added and the sets S1, S2 both set a lower bound, that is, they are EqualTo, GreaterThan, Interval, Semicontinuous or Semiinteger.
MathOptInterface.UpperBoundAlreadySet — TypeUpperBoundAlreadySet{S1, S2}Error thrown when setting a VariableIndex-in-S2 when a VariableIndex-in-S1 has already been added and the sets S1, S2 both set an upper bound, that is, they are EqualTo, LessThan, Interval, Semicontinuous or Semiinteger.
As discussed in AbstractCallback, trying to get attributes inside a callback may throw:
MathOptInterface.OptimizeInProgress — Typestruct OptimizeInProgress{AttrType<:AnyAttribute} <: Exception
attr::AttrType
endError thrown from optimizer when MOI.get(optimizer, attr) is called inside an AbstractCallback while it is only defined once optimize! has completed. This can only happen when is_set_by_optimize(attr) is true.
Trying to submit the wrong type of AbstractSubmittable inside an AbstractCallback (for example, a UserCut inside a LazyConstraintCallback) will throw:
MathOptInterface.InvalidCallbackUsage — Typestruct InvalidCallbackUsage{C, S} <: Exception
callback::C
submittable::S
endAn error indicating that submittable cannot be submitted inside callback.
For example, UserCut cannot be submitted inside LazyConstraintCallback.
The rest of the errors defined in MOI fall in two categories represented by the following two abstract types:
MathOptInterface.UnsupportedError — TypeUnsupportedError <: ExceptionAbstract type for error thrown when an element is not supported by the model.
MathOptInterface.NotAllowedError — TypeNotAllowedError <: ExceptionAbstract type for error thrown when an operation is supported but cannot be applied in the current state of the model.
The different UnsupportedError and NotAllowedError are the following errors:
MathOptInterface.UnsupportedAttribute — Typestruct UnsupportedAttribute{AttrType} <: UnsupportedError
attr::AttrType
message::String
endAn error indicating that the attribute attr is not supported by the model, that is, that supports returns false.
MathOptInterface.GetAttributeNotAllowed — Typestruct GetAttributeNotAllowed{AttrType} <: NotAllowedError
attr::AttrType
message::String
endAn error indicating that the attribute attr cannot be got for some reason (see the error string).
MathOptInterface.SetAttributeNotAllowed — Typestruct SetAttributeNotAllowed{AttrType} <: NotAllowedError
attr::AttrType
message::String
endAn error indicating that the attribute attr is supported (see supports) but cannot be set for some reason (see the error string).
MathOptInterface.AddVariableNotAllowed — Typestruct AddVariableNotAllowed <: NotAllowedError
message::String # Human-friendly explanation why the attribute cannot be set
endAn error indicating that variables cannot be added to the model.
MathOptInterface.UnsupportedConstraint — Typestruct UnsupportedConstraint{F<:AbstractFunction,S<:AbstractSet} <: UnsupportedError
message::String
endAn error indicating that constraints of type F-in-S are not supported by the model, that is, that supports_constraint returns false.
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.
MathOptInterface.AddConstraintNotAllowed — Typestruct AddConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet} <: NotAllowedError
message::String # Human-friendly explanation why the attribute cannot be set
endAn error indicating that constraints of type F-in-S are supported (see supports_constraint) but cannot be added.
MathOptInterface.ModifyConstraintNotAllowed — Typestruct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet,
C<:AbstractFunctionModification} <: NotAllowedError
constraint_index::ConstraintIndex{F, S}
change::C
message::String
endAn error indicating that the constraint modification change cannot be applied to the constraint of index ci.
MathOptInterface.ModifyObjectiveNotAllowed — Typestruct ModifyObjectiveNotAllowed{C<:AbstractFunctionModification} <: NotAllowedError
change::C
message::String
endAn error indicating that the objective modification change cannot be applied to the objective.
MathOptInterface.DeleteNotAllowed — Typestruct DeleteNotAllowed{IndexType <: Index} <: NotAllowedError
index::IndexType
message::String
endAn error indicating that the index index cannot be deleted.
MathOptInterface.UnsupportedSubmittable — Typestruct UnsupportedSubmittable{SubmitType} <: UnsupportedError
sub::SubmitType
message::String
endAn error indicating that the submittable sub is not supported by the model, that is, that supports returns false.
MathOptInterface.SubmitNotAllowed — Typestruct SubmitNotAllowed{SubmitTyp<:AbstractSubmittable} <: NotAllowedError
sub::SubmitType
message::String
endAn error indicating that the submittable sub is supported (see supports) but cannot be added for some reason (see the error string).
MathOptInterface.UnsupportedNonlinearOperator — TypeUnsupportedNonlinearOperator(head::Symbol[, message::String]) <: UnsupportedErrorAn error thrown by optimizers if they do not support the operator head in a ScalarNonlinearFunction.
Example
julia> throw(MOI.UnsupportedNonlinearOperator(:black_box))
ERROR: MathOptInterface.UnsupportedNonlinearOperator: The nonlinear operator `:black_box` is not supported by the model.
Stacktrace:
[...]Note that setting the ConstraintFunction of a VariableIndex constraint is not allowed:
MathOptInterface.SettingVariableIndexNotAllowed — TypeSettingVariableIndexNotAllowed()Error type that should be thrown when the user calls set to change the ConstraintFunction of a VariableIndex constraint.