-
Notifications
You must be signed in to change notification settings - Fork 3
/
a_star_search.py
65 lines (47 loc) · 2.01 KB
/
a_star_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""A STAR SEARCH - PYTHON CODE
AIFA MULTI AGENT PATH FINDING """
class AStar():
def __init__(self, env):
self.agent_dict = env.agent_dict
self.admissible_heuristic = env.admissible_heuristic
self.is_at_goal = env.is_at_goal
self.get_neighbors = env.get_neighbors
def reconstruct_path(self, came_from, current):
total_path = [current]
while current in came_from.keys():
current = came_from[current]
total_path.append(current)
return total_path[::-1]
def search(self, agent_name):
"""
low level search
"""
initial_state = self.agent_dict[agent_name]["start"]
step_cost = 1
closed_set = set()
open_set = {initial_state}
came_from = {}
g_score = {}
g_score[initial_state] = 0
f_score = {}
f_score[initial_state] = self.admissible_heuristic(initial_state, agent_name)
while open_set:
temp_dict = {open_item:f_score.setdefault(open_item, float("inf")) for open_item in open_set}
current = min(temp_dict, key=temp_dict.get)
if self.is_at_goal(current, agent_name):
return self.reconstruct_path(came_from, current)
open_set -= {current}
closed_set |= {current}
neighbor_list = self.get_neighbors(current)
for neighbor in neighbor_list:
if neighbor in closed_set:
continue
tentative_g_score = g_score.setdefault(current, float("inf")) + step_cost
if neighbor not in open_set:
open_set |= {neighbor}
elif tentative_g_score >= g_score.setdefault(neighbor, float("inf")):
continue
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
f_score[neighbor] = g_score[neighbor] + self.admissible_heuristic(neighbor, agent_name)
return False