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

Code implicitly assumes that the heuristic output is of the same type as the graph weights.

Open p-w-rs opened this issue 3 years ago • 2 comments

When using A* I was attempting to use a heuristic function that return a float, but the default weights of the graph were ints.

Here is the example

using Graphs, LinearAlgebra, Random
Random.seed!(0)

xyz = rand(4000, 3)
g = random_regular_graph(4000, 4)
src = rand(1:4000)
dst = rand(1:4000)
h(v) = Int(round(norm(xyz[v, :] - xyz[dst, :])))
edges = a_star(g, src, dst, weights(g), h)

# The above works since I convert to int, below does not, I get InexactError: Int64(1.7553136893610342)

h(v) = norm(xyz[v, :] - xyz[dst, :])
edges = a_star(g, src, dst, weights(g), h)

p-w-rs avatar Jun 08 '22 17:06 p-w-rs

Maybe we can gather an output of the heuristic, and use the output type to determine the type of the PriorityQueue (by assuming that the heuristic is type stable). Not the cleanest possible. I explored an other option in this discussion, but it seems hard to achieve...

etiennedeg avatar Jun 17 '22 11:06 etiennedeg

I think it is the most reasonable option for now. The heuristic has to be computed on s first, so we might as well use that to choose the type of priority values

gdalle avatar Jun 19 '22 20:06 gdalle

Going back to this, I just realized that basing the priority queue type on the output of a call to heuristic prevents static type inference. Better to add a recommendation to the docs I think.

gdalle avatar Feb 17 '23 09:02 gdalle