MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / Node

Class Node

graphs/bidirectional_a_star.py:28–75  ·  view source on GitHub ↗

>>> k = Node(0, 0, 4, 3, 0, None) >>> k.calculate_heuristic() 5.0 >>> n = Node(1, 4, 3, 4, 2, None) >>> n.calculate_heuristic() 2.0 >>> l = [k, n] >>> n == l[0] False >>> l.sort() >>> n == l[0] True

Source from the content-addressed store, hash-verified

26
27
28class Node:
29 """
30 >>> k = Node(0, 0, 4, 3, 0, None)
31 >>> k.calculate_heuristic()
32 5.0
33 >>> n = Node(1, 4, 3, 4, 2, None)
34 >>> n.calculate_heuristic()
35 2.0
36 >>> l = [k, n]
37 >>> n == l[0]
38 False
39 >>> l.sort()
40 >>> n == l[0]
41 True
42 """
43
44 def __init__(
45 self,
46 pos_x: int,
47 pos_y: int,
48 goal_x: int,
49 goal_y: int,
50 g_cost: int,
51 parent: Node | None,
52 ) -> None:
53 self.pos_x = pos_x
54 self.pos_y = pos_y
55 self.pos = (pos_y, pos_x)
56 self.goal_x = goal_x
57 self.goal_y = goal_y
58 self.g_cost = g_cost
59 self.parent = parent
60 self.h_cost = self.calculate_heuristic()
61 self.f_cost = self.g_cost + self.h_cost
62
63 def calculate_heuristic(self) -> float:
64 """
65 Heuristic for the A*
66 """
67 dy = self.pos_x - self.goal_x
68 dx = self.pos_y - self.goal_y
69 if HEURISTIC == 1:
70 return abs(dx) + abs(dy)
71 else:
72 return sqrt(dy**2 + dx**2)
73
74 def __lt__(self, other: Node) -> bool:
75 return self.f_cost < other.f_cost
76
77
78class AStar:

Callers 2

__init__Method · 0.70
get_successorsMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected