sbromberger / SimpleWeightedGraphs.jl

Simple weighted graphs. Requires LightGraphs.jl.
Other
49 stars 22 forks source link

distance calculation with a_star fails with SimpleWeightedGraph and SimpleWeightedDiGraph #69

Open henrik-wolf opened 3 years ago

henrik-wolf commented 3 years ago

I was trying to calculate the distance between two nodes using the A* algorithm implemented in LightGraphs. For example

using LightGraphs, SimpleWeightedGraphs
t = SimpleWeightedGraph(3)
add_edge!(t, 1, 2, 0.5)
add_edge!(t, 2, 3, 0.8)
add_edge!(t, 1, 3, 2.0)
a_star(t, 1,  3)

throws an error:

MethodError: no method matching SimpleWeightedGraphs.SimpleWeightedEdge{Int64,Float64}(::Int64, ::Int64)

So far I have worked out, that the problem arises, when the a_star algorithm tries to call the constructor of the SimpleWeightedEdge type in the a_start.jl file. I would have believed that the definition of

SimpleWeightedEdge(x, y) = SimpleWeightedEdge(x, y, one(Float64))

in line 18 of simpleweightededge.jl would exactly match this case. Am I missing something, or is this a bug?

mforets commented 3 years ago

The problem is that with E = edgetype(g) in reconstruct_path!, the full type parameters are used, but that's more specific than SimpleWeightedEdge(x, y).

A fix would be to define SimpleWeightedGraphs.SimpleWeightedEdge{T, U}(x::T, y::T) where T<:Integer where U<:Real = SimpleWeightedEdge(x, y, one(U)).