Use g value instead of f value to create sucessors in A* #268
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request fixes an error in the A* implementation and adds two separate test cases to prevent the issue I was facing in the future.
Fixes #267
Details
The A* implementation previously used these formulas to calculate the new g and f values for a node:
The correct equations however use the
previous.g
value instead. See here, here, or here for reference.g
is the cost to reach the node from the start,f
isg
plus a heuristic for the remaining cost to reach the destination.Using the cost + heuristic to calculate the cost for the next node does not make sense, the new
f
value would then contain the heuristic for the distance between the two nodes twice, resulting in a value that increases with the number of nodes in a path instead of the distance. This is why I added the more generic testcase.I also changed the way the cost is passed to the
add_successor
function. Instead of passingq.g + step_cost
, now only the step cost is passed. This is because q (and thus also q.g) is already passed to the function and the I think the new cost should conceptually be calculated insideadd_successor
instead of the call-site.