My Python pathfinding function returns different results if I use a memoizing decorator. It returns a correct value on its own, but after being memoized, it returns an incorrect value.
The function I'm talking about looks like this:
@functools.lru_cache(maxsize=None)
def recursiveTraversal(originIndex, target, steps):
startingVertex = data[originIndex]
if startingVertex["ID"] == target:
if steps == 0:
return Path(0, [])
else:
return None
else:
if steps == 0:
return None
else:
potentialPaths = []
for i in startingVertex["Edges"]:
nextVertex = data[i]
nextPath = recursiveTraversal(i, target, steps - 1)
if nextPath == None:
continue
nextPath.weight += int(nextVertex["Weight"])
nextPath.vertices.append(i)
potentialPaths.append(nextPath)
if len(potentialPaths) > 0:
minPath = min(potentialPaths, key=lambda x: x.weight)
return minPath
else:
return None
A complete runnable example can be found here. The upper part of the file is all data, and the code is at the bottom. To reproduce this, just comment out line 15, and observe that the output is different.
How can I get the memoized version to output the same thing as the normal version?