>>> k = Node(0, 0, 4, 5, 0, None) >>> k.calculate_heuristic() 9 >>> n = Node(1, 4, 3, 4, 2, None) >>> n.calculate_heuristic() 2 >>> l = [k, n] >>> n == l[0] False >>> l.sort() >>> n == l[0] True
| 38 | |
| 39 | |
| 40 | class Node: |
| 41 | """ |
| 42 | >>> k = Node(0, 0, 4, 5, 0, None) |
| 43 | >>> k.calculate_heuristic() |
| 44 | 9 |
| 45 | >>> n = Node(1, 4, 3, 4, 2, None) |
| 46 | >>> n.calculate_heuristic() |
| 47 | 2 |
| 48 | >>> l = [k, n] |
| 49 | >>> n == l[0] |
| 50 | False |
| 51 | >>> l.sort() |
| 52 | >>> n == l[0] |
| 53 | True |
| 54 | """ |
| 55 | |
| 56 | def __init__( |
| 57 | self, |
| 58 | pos_x: int, |
| 59 | pos_y: int, |
| 60 | goal_x: int, |
| 61 | goal_y: int, |
| 62 | g_cost: float, |
| 63 | parent: Node | None, |
| 64 | ): |
| 65 | self.pos_x = pos_x |
| 66 | self.pos_y = pos_y |
| 67 | self.pos = (pos_y, pos_x) |
| 68 | self.goal_x = goal_x |
| 69 | self.goal_y = goal_y |
| 70 | self.g_cost = g_cost |
| 71 | self.parent = parent |
| 72 | self.f_cost = self.calculate_heuristic() |
| 73 | |
| 74 | def calculate_heuristic(self) -> float: |
| 75 | """ |
| 76 | The heuristic here is the Manhattan Distance |
| 77 | Could elaborate to offer more than one choice |
| 78 | """ |
| 79 | dx = abs(self.pos_x - self.goal_x) |
| 80 | dy = abs(self.pos_y - self.goal_y) |
| 81 | return dx + dy |
| 82 | |
| 83 | def __lt__(self, other) -> bool: |
| 84 | return self.f_cost < other.f_cost |
| 85 | |
| 86 | def __eq__(self, other) -> bool: |
| 87 | return self.pos == other.pos |
| 88 | |
| 89 | |
| 90 | class GreedyBestFirst: |
no outgoing calls
no test coverage detected