-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_star.py
58 lines (43 loc) · 1.54 KB
/
A_star.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
from Model import *
from queue import *
class AStar:
def __init__(self):
self.goal_state = None
self.io_handler = IO()
self.init_state = None
self.expanded_nodes = 0
self.created_nodes = 1
self.frontier = PriorityQueue()
self.explored = set()
def search(self):
self.init_state = self.io_handler.read()
self.frontier.put_nowait(self.init_state)
if self.init_state.goal_test():
self.goal_state = self.init_state
self.io_handler = IO(self.goal_state, self.expanded_nodes, self.created_nodes)
self.io_handler.write()
return 1
while True:
if self.frontier.empty():
print("Failure :( ")
return -1
state = self.frontier.get_nowait()
if state in self.explored:
continue
self.expanded_nodes += 1
if state.goal_test():
self.goal_state = state
self.io_handler = IO(self.goal_state, self.expanded_nodes, self.created_nodes)
self.io_handler.write()
return 1
self.explored.add(state)
childes = state.next_states()
for child in childes:
if child is None:
continue
if not(child in self.explored):
self.created_nodes += 1
self.frontier.put_nowait(child)
if __name__ == "__main__":
astar_search = AStar()
astar_search.search()