best_first.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from .a_star import AStarFinder, MAX_RUNS, TIME_LIMIT
  2. from pathfinding.core.diagonal_movement import DiagonalMovement
  3. class BestFirst(AStarFinder):
  4. """
  5. Similar to the default A* algorithm from a_star.
  6. """
  7. def __init__(self, heuristic=None, weight=1,
  8. diagonal_movement=DiagonalMovement.never,
  9. time_limit=TIME_LIMIT,
  10. max_runs=MAX_RUNS):
  11. """
  12. find shortest path using BestFirst algorithm
  13. :param heuristic: heuristic used to calculate distance of 2 points
  14. (defaults to manhatten)
  15. :param weight: weight for the edges
  16. :param diagonal_movement: if diagonal movement is allowed
  17. (see enum in diagonal_movement)
  18. :param time_limit: max. runtime in seconds
  19. :param max_runs: max. amount of tries until we abort the search
  20. (optional, only if we enter huge grids and have time constrains)
  21. <=0 means there are no constrains and the code might run on any
  22. large map.
  23. """
  24. super(BestFirst, self).__init__(
  25. heuristic=heuristic,
  26. weight=weight,
  27. diagonal_movement=diagonal_movement,
  28. time_limit=time_limit,
  29. max_runs=max_runs)
  30. self.weighted = False
  31. def apply_heuristic(self, node_a, node_b, heuristic=None):
  32. return super(BestFirst, self).apply_heuristic(
  33. node_a, node_b, heuristic) * 1000000