Skip to content
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

Add a few missing methods for AbstractJuMPScalar to support e.g. Distances.jl #3585

Merged
merged 4 commits into from
Nov 27, 2023

Commits on Nov 26, 2023

  1. Provide Base.length(::AbstractJuMPScalar)

    ```
    using JuMP, Ipopt, Distances
    model = Model(Ipopt.Optimizer)
    @variable(model, a)
    @variable(model, b)
    euclidean(a, b)
    ```
    Results in:
    ```
    ERROR: MethodError: no method matching length(::VariableRef)
    
    Closest candidates are:
      length(::Union{Base.KeySet, Base.ValueIterator})
       @ Base abstractdict.jl:58
      length(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S})
       @ LinearAlgebra /builddirs/julia/usr/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:295
      length(::Union{SparseArrays.FixedSparseVector{Tv, Ti}, SparseArrays.SparseVector{Tv, Ti}} where {Tv, Ti})
       @ SparseArrays /builddirs/julia/usr/share/julia/stdlib/v1.9/SparseArrays/src/sparsevector.jl:95
      ...
    
    ```
    LebedevRI committed Nov 26, 2023
    Configuration menu
    Copy the full SHA
    3b60aa9 View commit details
    Browse the repository at this point in the history
  2. Provide Base.IteratorEltype() / Base.eltype()

    ```
    julia> euclidean(a, b)
    ERROR: MethodError: no method matching oneunit(::Type{Any})
    
    Closest candidates are:
      oneunit(::Type{Union{Missing, T}}) where T
       @ Base missing.jl:105
      oneunit(::Type{T}) where T
       @ Base number.jl:370
      oneunit(::T) where T
       @ Base number.jl:369
      ...
    
    Stacktrace:
     [1] oneunit(#unused#::Type{Any})
       @ Base ./missing.jl:106
     [2] _eval_start(d::Euclidean, #unused#::Type{Any}, #unused#::Type{Any}, #unused#::Nothing)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:320
     [3] _eval_start(d::Euclidean, #unused#::Type{Any}, #unused#::Type{Any})
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:318
     [4] eval_start(d::Euclidean, a::VariableRef, b::VariableRef)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:317
     [5] _evaluate(d::Euclidean, a::VariableRef, b::VariableRef, #unused#::Nothing)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:236
     [6] (::Euclidean)(a::VariableRef, b::VariableRef)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:328
     [7] top-level scope
       @ REPL[10]:1
    
    ```
    LebedevRI committed Nov 26, 2023
    Configuration menu
    Copy the full SHA
    bc09f55 View commit details
    Browse the repository at this point in the history
  3. Provide Base.oneunit()

    ```
    julia> euclidean(a, b)
    ERROR: MethodError: no method matching VariableRef(::AffExpr)
    
    Closest candidates are:
      (::Type{GenericVariableRef{T}} where T)(::Any, ::Any)
       @ JuMP ~/.julia/packages/JuMP/h0lrf/src/variables.jl:251
      GenericVariableRef{T}(::ConstraintRef{GenericModel{T}, <:MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex}}) where T
       @ JuMP ~/.julia/packages/JuMP/h0lrf/src/variables.jl:520
      GenericVariableRef{T}(::GenericModel{T}) where T
       @ JuMP ~/.julia/packages/JuMP/h0lrf/src/variables.jl:494
    
    Stacktrace:
     [1] oneunit(#unused#::Type{VariableRef})
       @ Base ./number.jl:370
     [2] _eval_start(d::Euclidean, #unused#::Type{VariableRef}, #unused#::Type{VariableRef}, #unused#::Nothing)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:320
     [3] _eval_start(d::Euclidean, #unused#::Type{VariableRef}, #unused#::Type{VariableRef})
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:318
     [4] eval_start(d::Euclidean, a::VariableRef, b::VariableRef)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:317
     [5] _evaluate(d::Euclidean, a::VariableRef, b::VariableRef, #unused#::Nothing)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:236
     [6] (::Euclidean)(a::VariableRef, b::VariableRef)
       @ Distances ~/.julia/packages/Distances/PvoXa/src/metrics.jl:328
     [7] top-level scope
       @ REPL[8]:1
    
    ```
    LebedevRI committed Nov 26, 2023
    Configuration menu
    Copy the full SHA
    61ef5f5 View commit details
    Browse the repository at this point in the history

Commits on Nov 27, 2023

  1. Configuration menu
    Copy the full SHA
    0405c3c View commit details
    Browse the repository at this point in the history