Skip to content

Allow dot(::Vector{VariableIndex}, ::Real) #1380

Closed

Description

@dourouc05

To write bridges, I found myself using dot (from the standard package LinearAlgebra). However, MOI is currently missing the operation dot between individual elements for this to work out of the box (according to the docs: https://github.com/JuliaLang/julia/blob/v1.6.0/stdlib/LinearAlgebra/src/generic.jl#L846):

  MethodError: no method matching iterate(::MathOptInterface.VariableIndex)
  Closest candidates are:
    iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
    iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
    iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
    ...
  Stacktrace:
    [1] dot(x::MathOptInterface.VariableIndex, y::Int64)
      @ LinearAlgebra C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\generic.jl:876
    [2] dot(x::Vector{MathOptInterface.VariableIndex}, y::Vector{Int64})
      @ LinearAlgebra C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\generic.jl:914
    [3] macro expansion
      @ .\show.jl:955 [inlined]

I wrote the following code to make it work properly:

function LinearAlgebra.dot(variable::MOI.VariableIndex, coefficient::T) where {T}
    return MOI.ScalarAffineFunction(
        [MOI.ScalarAffineTerm(coefficient, variable)], 
        zero(T)
    )
end
function LinearAlgebra.dot(coefficient::T, variable::MOI.VariableIndex) where {T}
    return MOI.ScalarAffineFunction(
        [MOI.ScalarAffineTerm(coefficient, variable)], 
        zero(T)
    )
end

I guess this should go into Utilities/functions.jl, but shall I make a PR against MOI 0.9 or 0.10?

Activity

  1. odow commented on Jun 1, 2021

    @odow
    Member

    Is there any reason not to write out the new function explicitly? You could always define a dot locally.

    I'm hesitant about exposing more of MOI because it's too easy to accidentally introduce slow routes to code. For example, implementing what you suggest would be very slow because of the dot fallback: https://github.com/JuliaLang/julia/blob/f9720dc2ebd6cd9e3086365f281e62506444ef37/stdlib/LinearAlgebra/src/generic.jl#L896

  2. blegat commented on Jun 2, 2021

    @blegat
    Member

    MOI.VariableIndex is not an MOI.AbstractScalarFunction so it does not implement any arithmetic operation. If you use MOI.SingleVariable instead then it should work.

  3. odow commented on Jun 2, 2021

    @odow
    Member

    @dourouc05 there are a few different options:

    julia> using MathOptInterface
    
    julia> using LinearAlgebra
    
    julia> const MOI = MathOptInterface
    MathOptInterface
    
    julia> x = MOI.VariableIndex.(1:4)
    4-element Vector{MathOptInterface.VariableIndex}:
     MathOptInterface.VariableIndex(1)
     MathOptInterface.VariableIndex(2)
     MathOptInterface.VariableIndex(3)
     MathOptInterface.VariableIndex(4)
    
    julia> LinearAlgebra.dot(x, [1.0, 2.0, 3.0, 4.0])
    ERROR: MethodError: no method matching iterate(::MathOptInterface.VariableIndex)
    Closest candidates are:
      iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
      iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
      iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
      ...
    Stacktrace:
     [1] dot(x::MathOptInterface.VariableIndex, y::Float64)
       @ LinearAlgebra /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:876
     [2] dot(x::Vector{MathOptInterface.VariableIndex}, y::Vector{Float64})
       @ LinearAlgebra /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:914
     [3] top-level scope
       @ REPL[5]:1
    
    julia> LinearAlgebra.dot(MOI.VectorOfVariables(x), [1.0, 2.0, 3.0, 4.0])
    ERROR: MethodError: no method matching iterate(::MathOptInterface.VectorOfVariables)
    Closest candidates are:
      iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
      iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
      iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
      ...
    Stacktrace:
     [1] dot(x::MathOptInterface.VectorOfVariables, y::Vector{Float64})
       @ LinearAlgebra /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:876
     [2] top-level scope
       @ REPL[6]:1
    
    julia> LinearAlgebra.dot(MOI.SingleVariable.(x), [1.0, 2.0, 3.0, 4.0])
    MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1)), MathOptInterface.ScalarAffineTerm{Float64}(2.0, MathOptInterface.VariableIndex(2)), MathOptInterface.ScalarAffineTerm{Float64}(3.0, MathOptInterface.VariableIndex(3)), MathOptInterface.ScalarAffineTerm{Float64}(4.0, MathOptInterface.VariableIndex(4))], 0.0)
    
    julia> function foo(x, y)
               return LinearAlgebra.dot(MOI.SingleVariable.(x), y)
           end
    foo (generic function with 1 method)
    
    julia> function bar(x, y)
               return MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(y, x), 0.0)
           end
    bar (generic function with 1 method)
    
    julia> y = rand(4);
    
    julia> @time foo(x, y);
      0.016709 seconds (7.38 k allocations: 363.840 KiB, 99.75% compilation time)
    
    julia> @time foo(x, y);
      0.000027 seconds (20 allocations: 560 bytes)
    
    julia> @time bar(x, y);
      0.049849 seconds (126.76 k allocations: 7.172 MiB, 99.97% compilation time)
    
    julia> @time bar(x, y);
      0.000004 seconds (2 allocations: 176 bytes)

    I would go with implementing the bar method locally in your package for now.

  4. blegat commented on Jun 2, 2021

    @blegat
    Member

    Yes, dot uses the default LinearAlgebra implementation which does not exploit mutability, if you want it to be fast, use MA.operate(dot, x, y)

  5. dourouc05 commented on Jun 3, 2021

    @dourouc05
    ContributorAuthor

    My goal when opening this issue was really to have an excellent solution easily available at MOI level for new users, so that they have a nice API available (and with good performance, but that can always be improved on later on). Also, it lowers the probability of misuse of MOI (like a competing modelling system that compares to JuMP/MOI and uses a poor implementation).

    Regarding the fact that VariableIndex is not a function, there are already two functions that allow the user to build affine expressions directly off indices: https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Utilities/functions.jl#L2496-L2502, https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Utilities/functions.jl#L2597-L2606 (through fill_variable). I thought it could be useful to build upon them, to have a more pleasant (aka shorter) syntax on the user side.

    For now, I'll go with @odow's bar suggestion and keep it internal to my package.

  6. odow commented on Jun 16, 2021

    @odow
    Member

    Closing because we won't be adding this for now.

  7. blegat commented on Jun 16, 2021

    @blegat
    Member

    @dourouc05 I wasn't aware of https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Utilities/functions.jl#L2496-L2502, MOI.VariableIndex should be replaced by MOI.SingleVariable there. I opened an issue #1388

    it could be useful to build upon them, to have a more pleasant (aka shorter) syntax on the user side.

    You have a convenient syntax, the same one than with JuMP, but you need to use MOI.SingleVariable, not MOI.VariableIndex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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