Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heuristic_cost_estimate affects results #28

Closed
elektito opened this issue Mar 13, 2024 · 1 comment
Closed

heuristic_cost_estimate affects results #28

elektito opened this issue Mar 13, 2024 · 1 comment

Comments

@elektito
Copy link

I was under the impression that the implementation of heuristic_cost_estimate should only affect performance, not results. But in this case, I'm seeing one implementation causes the result to be wrong:

from astar import AStar

M = 8
n = 15
A = [1, 4, 3, 6, 7, 1, 2, 2, 5, 2, 3, 3, 2, 1, 7]
B = [3, 7, 2, 1, 1, 8, 6, 5, 5, 4, 1, 2, 4, 8, 2]

class MyAstar(AStar):
    def neighbors(self, node):
        return [i for i in range(1, n+1) if i != node]

    def distance_between(self, n1, n2):
        return (A[n1-1] + B[n2-1]) % M

    def heuristic_cost_estimate(self, current, goal):
        return goal - current
        #return self.distance_between(current, goal)

    def is_goal_reached(self, current, goal):
        return current == goal

a = MyAstar()
print(list(a.astar(1, 15)))

This would print [1, 15] which has a cost of 3. If I use the actual implementation of distance_between for heuristic_cost_estimate (the commented line), I get [1, 4, 15] which with a cost of 2 is the correct result.

@jrialland
Copy link
Owner

Your implementation of distance_between is wrong, (should most probably be a substraction, not an addition)

@jrialland jrialland closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants