LightGraphs.jl icon indicating copy to clipboard operation
LightGraphs.jl copied to clipboard

[BUG] a_star fails on SimpleWeightedDiGraph

Open paulmelis opened this issue 4 years ago • 3 comments

Description of bug

From what I can tell from the docs and other examples online this should work, yet it fails:

julia> using LightGraphs, SimpleWeightedGraphs

julia> g = SimpleWeightedDiGraph(3)
{3, 0} directed simple Int64 graph with Float64 weights

julia> add_edge!(g, 1, 2, 0.5)
true

julia> add_edge!(g, 2, 3, 0.8)
true

julia> add_edge!(g, 1, 3, 2.0)
true

julia> a_star(g, 1, 3)
ERROR: MethodError: no method matching SimpleWeightedEdge{Int64, Float64}(::Int64, ::Int64)
Closest candidates are:
  SimpleWeightedEdge{T, U}(::Any, ::Any, ::Any) where {T<:Integer, U<:Real} at /home/melis/.julia/packages/SimpleWeightedGraphs/IDzOp/src/simpleweightededge.jl:7
Stacktrace:
 [1] reconstruct_path!(total_path::Vector{SimpleWeightedEdge{Int64, Float64}}, came_from::Vector{Int64}, end_idx::Int64, g::SimpleWeightedDiGraph{Int64, Float64})
   @ LightGraphs ~/.julia/packages/LightGraphs/IgJif/src/shortestpaths/astar.jl:14
 [2] a_star_impl!(g::SimpleWeightedDiGraph{Int64, Float64}, goal::Int64, open_set::DataStructures.PriorityQueue{Integer, Float64, Base.Order.ForwardOrdering}, closed_set::Vector{Bool}, g_score::Vector{Float64}, f_score::Vector{Float64}, came_from::Vector{Int64}, distmx::LinearAlgebra.Adjoint{Float64, SparseArrays.SparseMatrixCSC{Float64, Int64}}, heuristic::LightGraphs.var"#101#102"{Float64})
   @ LightGraphs ~/.julia/packages/LightGraphs/IgJif/src/shortestpaths/astar.jl:36
 [3] a_star(g::SimpleWeightedDiGraph{Int64, Float64}, s::Int64, t::Int64, distmx::LinearAlgebra.Adjoint{Float64, SparseArrays.SparseMatrixCSC{Float64, Int64}}, heuristic::LightGraphs.var"#101#102"{Float64})
   @ LightGraphs ~/.julia/packages/LightGraphs/IgJif/src/shortestpaths/astar.jl:92
 [4] a_star (repeats 2 times)
   @ ~/.julia/packages/LightGraphs/IgJif/src/shortestpaths/astar.jl:73 [inlined]
 [5] top-level scope
   @ REPL[11]:1

Version information

julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4* (2021-07-14 15:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, haswell)

(@v1.6) pkg> status LightGraphs SimpleWeightedGraphs
      Status `~/.julia/environments/v1.6/Project.toml`
  [093fc24a] LightGraphs v1.3.5
  [47aef6b3] SimpleWeightedGraphs v1.1.1

paulmelis avatar Jul 23 '21 08:07 paulmelis

SimpleWeightedEdge will require a weight but only src and dst are passed https://github.com/JuliaGraphs/LightGraphs.jl/blob/ca41a2e8f6962f1a232637a446bd326676559a92/src/shortestpaths/astar.jl#L14 Instead of using graph's EdgeType, use a SimpleEdge https://github.com/JuliaGraphs/LightGraphs.jl/blob/2a644c2b15b444e7f32f73021ec276aa9fc8ba30/src/SimpleGraphs/simpleedge.jl#L6 instead.

abhinavmehndiratta avatar Jul 30 '21 08:07 abhinavmehndiratta

You could also convert you weighted graph to a SimpleGraph and pass the distmx https://github.com/JuliaGraphs/LightGraphs.jl/blob/ca41a2e8f6962f1a232637a446bd326676559a92/src/shortestpaths/astar.jl#L70

abhinavmehndiratta avatar Jul 30 '21 08:07 abhinavmehndiratta

SimpleWeightedEdge will require a weight but only src and dst are passed

https://github.com/JuliaGraphs/LightGraphs.jl/blob/ca41a2e8f6962f1a232637a446bd326676559a92/src/shortestpaths/astar.jl#L14 Instead of using graph's EdgeType, use a SimpleEdge

https://github.com/JuliaGraphs/LightGraphs.jl/blob/2a644c2b15b444e7f32f73021ec276aa9fc8ba30/src/SimpleGraphs/simpleedge.jl#L6 instead.

Thanks for the comment, but I'm not sure if this is meant for me (reporter) of for the devs? As I'm not doing anything with these edge types myself.

paulmelis avatar Jul 30 '21 08:07 paulmelis