Skip to content

Fix multi heuristic astar algo #4612

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions graphs/multi_heuristic_astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import numpy as np

TPos = tuple[int, int]


class PriorityQueue:
def __init__(self):
Expand Down Expand Up @@ -53,24 +55,24 @@ def get(self):
return (priority, item)


def consistent_heuristic(P, goal):
def consistent_heuristic(P: TPos, goal: TPos):
# euclidean distance
a = np.array(P)
b = np.array(goal)
return np.linalg.norm(a - b)


def heuristic_2(P, goal):
def heuristic_2(P: TPos, goal: TPos):
# integer division by time variable
return consistent_heuristic(P, goal) // t


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


def key(start, i, goal, g_function):
def key(start: TPos, i: int, goal: TPos, g_function: dict[TPos, float]):
ans = g_function[start] + W1 * heuristics[i](start, goal)
return ans

Expand Down Expand Up @@ -117,7 +119,7 @@ def do_something(back_pointer, goal, start):
quit()


def valid(p):
def valid(p: TPos):
if p[0] < 0 or p[0] > n - 1:
return False
if p[1] < 0 or p[1] > n - 1:
Expand Down Expand Up @@ -215,7 +217,6 @@ def make_common_ground():
(18, 1),
(19, 1),
]
blocks_no = []
blocks_all = make_common_ground()


Expand All @@ -233,7 +234,7 @@ def make_common_ground():
t = 1


def multi_a_star(start, goal, n_heuristic):
def multi_a_star(start: TPos, goal: TPos, n_heuristic: int):
g_function = {start: 0, goal: float("inf")}
back_pointer = {start: -1, goal: -1}
open_list = []
Expand All @@ -243,8 +244,8 @@ def multi_a_star(start, goal, n_heuristic):
open_list.append(PriorityQueue())
open_list[i].put(start, key(start, i, goal, g_function))

close_list_anchor = []
close_list_inad = []
close_list_anchor: list[int] = []
close_list_inad: list[int] = []
while open_list[0].minkey() < float("inf"):
for i in range(1, n_heuristic):
# print(open_list[0].minkey(), open_list[i].minkey())
Expand Down