Skip to content

Cannot use custom attribute type #1127

@ilancoulon

Description

@ilancoulon

Not sure if this is related to #1092

I am trying to add my own attributes to my optimizer. The issue is that it seems I cannot use my own types, and I don't understand why. To be clearer, I can use my own MOI.AbstractOptimizerAttribute, but when I try do define a MOI.set function, it seems the third argument must be a standard type (actually, from this list: Union{Nothing, DataType, Number, Enum, AbstractString, MOI.AnyAttribute, MOI.AbstractSet, Function, MOI.ModelLike}).

Here is the code triggering an error:

using JuMP
using MathOptInterface
const MOI = MathOptInterface

struct MyOwnAttributeType <: MOI.AbstractOptimizerAttribute end

struct MyOwnAttribute end

struct OwnOptimizer <: MOI.AbstractOptimizer end

MOI.supports(::OwnOptimizer, ::MyOwnAttributeType) = true

MOI.is_empty(::OwnOptimizer) = true

function MOI.set(model::OwnOptimizer, ::MyOwnAttributeType, attr::MyOwnAttribute)
    println(attr)
end

function fail_set_attribute()
    model = Model(OwnOptimizer)
    MOI.set(model, MyOwnAttributeType(), MyOwnAttribute())

end

When launching fail_set_attribute(), I get the following error:

julia> fail_set_attribute()
ERROR: MethodError: no method matching map_indices(::MathOptInterface.Utilities.var"#7#8"{MathOptInterface.Utilities.IndexMap}, ::MyOwnAttribute)
Closest candidates are:
  map_indices(::AbstractDict{T,T}, ::Any) where T<:Union{MathOptInterface.VariableIndex, MathOptInterface.ConstraintIndex} at C:\Users\ilanc\.julia\packages\MathOptInterface\bygN7\src\Utilities\functions.jl:73
  map_indices(::Function, ::Union{Nothing, AbstractString, DataType, Function, Number, Tuple{Vararg{Union{Nothing, AbstractString, DataType, Function, Number, MathOptInterface.AbstractConstraintAttribute, MathOptInterface.AbstractModelAttribute, MathOptInterface.AbstractOptimizerAttribute, MathOptInterface.AbstractSet, MathOptInterface.AbstractVariableAttribute, MathOptInterface.ModelLike, Enum},N} where N}, MathOptInterface.AbstractConstraintAttribute, MathOptInterface.AbstractModelAttribute, MathOptInterface.AbstractOptimizerAttribute, MathOptInterface.AbstractSet, MathOptInterface.AbstractVariableAttribute, MathOptInterface.ModelLike, AbstractArray{#s61,N} where N where #s61<:Union{Nothing, AbstractString, DataType, Function, Number, Tuple{Vararg{Union{Nothing, AbstractString, DataType, Function, Number, MathOptInterface.AbstractConstraintAttribute, MathOptInterface.AbstractModelAttribute, MathOptInterface.AbstractOptimizerAttribute, MathOptInterface.AbstractSet, MathOptInterface.AbstractVariableAttribute, MathOptInterface.ModelLike, Enum},N} where N}, MathOptInterface.AbstractConstraintAttribute, MathOptInterface.AbstractModelAttribute, MathOptInterface.AbstractOptimizerAttribute, MathOptInterface.AbstractSet, MathOptInterface.AbstractVariableAttribute, MathOptInterface.ModelLike, Enum}, AbstractArray{#s60,N} where N where #s60<:(AbstractArray{#s14,N} where N where #s14<:Union{Nothing, AbstractString, DataType, Function, Number, Tuple{Vararg{Union{Nothing, AbstractString, DataType, Function, Number, MathOptInterface.AbstractConstraintAttribute, MathOptInterface.AbstractModelAttribute, MathOptInterface.AbstractOptimizerAttribute, MathOptInterface.AbstractSet, MathOptInterface.AbstractVariableAttribute, MathOptInterface.ModelLike, Enum},N} where N}, MathOptInterface.AbstractConstraintAttribute, MathOptInterface.AbstractModelAttribute, MathOptInterface.AbstractOptimizerAttribute, MathOptInterface.AbstractSet, MathOptInterface.AbstractVariableAttribute, MathOptInterface.ModelLike, Enum}), Enum}) at C:\Users\ilanc\.julia\packages\MathOptInterface\bygN7\src\Utilities\functions.jl:80
  map_indices(::Function, ::MathOptInterface.VariableIndex) at C:\Users\ilanc\.julia\packages\MathOptInterface\bygN7\src\Utilities\functions.jl:82
  ...
Stacktrace:
 [1] map_indices(::MathOptInterface.Utilities.IndexMap, ::MyOwnAttribute) at C:\Users\ilanc\.julia\packages\MathOptInterface\bygN7\src\Utilities\functions.jl:73
 [2] set(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, ::MyOwnAttributeType, ::MyOwnAttribute) at C:\Users\ilanc\.julia\packages\MathOptInterface\bygN7\src\Utilities\cachingoptimizer.jl:626
 [3] set(::Model, ::MyOwnAttributeType, ::MyOwnAttribute) at C:\Users\ilanc\.julia\packages\JuMP\YXK4e\src\JuMP.jl:892
 [4] fail_set_attribute() at C:\Users\ilanc\Dropbox\Stage-MTL\julia\jump_attribute_type.jl:21
 [5] top-level scope at REPL[3]:1
 [6] eval(::Module, ::Any) at .\boot.jl:331
 [7] eval_user_input(::Any, ::REPL.REPLBackend) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\REPL\src\REPL.jl:86
 [8] run_backend(::REPL.REPLBackend) at C:\Users\ilanc\.julia\packages\Revise\BqeJF\src\Revise.jl:1184
 [9] top-level scope at REPL[1]:0

It would be pretty useful to have my own type as an attribute, do you think it is possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions