Skip to content

Commit cd98737

Browse files
authored
Fix multi heuristic astar algo (TheAlgorithms#4612)
1 parent d668c17 commit cd98737

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

graphs/multi_heuristic_astar.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import numpy as np
44

5+
TPos = tuple[int, int]
6+
57

68
class PriorityQueue:
79
def __init__(self):
@@ -53,24 +55,24 @@ def get(self):
5355
return (priority, item)
5456

5557

56-
def consistent_heuristic(P, goal):
58+
def consistent_heuristic(P: TPos, goal: TPos):
5759
# euclidean distance
5860
a = np.array(P)
5961
b = np.array(goal)
6062
return np.linalg.norm(a - b)
6163

6264

63-
def heuristic_2(P, goal):
65+
def heuristic_2(P: TPos, goal: TPos):
6466
# integer division by time variable
6567
return consistent_heuristic(P, goal) // t
6668

6769

68-
def heuristic_1(P, goal):
70+
def heuristic_1(P: TPos, goal: TPos):
6971
# manhattan distance
7072
return abs(P[0] - goal[0]) + abs(P[1] - goal[1])
7173

7274

73-
def key(start, i, goal, g_function):
75+
def key(start: TPos, i: int, goal: TPos, g_function: dict[TPos, float]):
7476
ans = g_function[start] + W1 * heuristics[i](start, goal)
7577
return ans
7678

@@ -117,7 +119,7 @@ def do_something(back_pointer, goal, start):
117119
quit()
118120

119121

120-
def valid(p):
122+
def valid(p: TPos):
121123
if p[0] < 0 or p[0] > n - 1:
122124
return False
123125
if p[1] < 0 or p[1] > n - 1:
@@ -215,7 +217,6 @@ def make_common_ground():
215217
(18, 1),
216218
(19, 1),
217219
]
218-
blocks_no = []
219220
blocks_all = make_common_ground()
220221

221222

@@ -233,7 +234,7 @@ def make_common_ground():
233234
t = 1
234235

235236

236-
def multi_a_star(start, goal, n_heuristic):
237+
def multi_a_star(start: TPos, goal: TPos, n_heuristic: int):
237238
g_function = {start: 0, goal: float("inf")}
238239
back_pointer = {start: -1, goal: -1}
239240
open_list = []
@@ -243,8 +244,8 @@ def multi_a_star(start, goal, n_heuristic):
243244
open_list.append(PriorityQueue())
244245
open_list[i].put(start, key(start, i, goal, g_function))
245246

246-
close_list_anchor = []
247-
close_list_inad = []
247+
close_list_anchor: list[int] = []
248+
close_list_inad: list[int] = []
248249
while open_list[0].minkey() < float("inf"):
249250
for i in range(1, n_heuristic):
250251
# print(open_list[0].minkey(), open_list[i].minkey())

0 commit comments

Comments
 (0)