-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.py
89 lines (73 loc) · 2.93 KB
/
A.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from NodeA import *
from manhatan import *
import numpy as numpy
fringe = []
closed_list = []
goalNode = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]]).reshape(4, 4)
rootNode = numpy.array([[1, 2, 3, 0], [5, 6, 7, 4], [9, 10, 11, 8], [13, 14, 15, 12]]).reshape(4, 4)
def Search_in_closedList(newNode):
for index in closed_list:
if newNode is index:
return False
return True
def A_star(data):
root_node = NodeA(data, None,0 ,0)
manhatan = manhattan_distance(root_node)
root_node.hn = manhatan
fringe.append(root_node)
closed_list.append(data.tolist())
while fringe:
currentNode = min(fringe, key=getFn)
fringe.remove(currentNode)
if (currentNode.data.tolist() == goalNode.tolist()):
receive_goal(currentNode)
return 0
move1 = moveUp(currentNode.data)
if move1 is not None:
next_data = move1
if next_data.tolist() not in closed_list:
new_node = NodeA(next_data, currentNode, currentNode.gn + 7, 0)
manhatan = manhattan_distance(new_node)
new_node.hn = manhatan
closed_list.append(next_data.tolist())
fringe.append(new_node)
move2 = moveDown(currentNode.data)
if move2 is not None:
next_data = move2
if next_data.tolist() not in closed_list:
new_node = NodeA(next_data, currentNode, currentNode.gn + 1, 0)
manhatan = manhattan_distance(new_node)
new_node.hn = manhatan
closed_list.append(next_data.tolist())
fringe.append(new_node)
move3 = moveLeft(currentNode.data)
if move3 is not None:
next_data = move3
if next_data.tolist() not in closed_list:
new_node = NodeA(next_data, currentNode, currentNode.gn + 5, 0)
manhatan = manhattan_distance(new_node)
new_node.hn = manhatan
closed_list.append(next_data.tolist())
fringe.append(new_node)
move4 = moveRight(currentNode.data)
if move4 is not None:
next_data = move4
if next_data.tolist() not in closed_list:
new_node = NodeA(next_data, currentNode, currentNode.gn + 6, 0)
manhatan = manhattan_distance(new_node)
new_node.hn = manhatan
closed_list.append(next_data.tolist())
fringe.append(new_node)
def receive_goal(currentNode):
arr_path = []
arr_path.append(currentNode.data)
temp = currentNode.parent
while temp != None:
arr_path.append(temp.data)
temp = temp.parent
arr_path.reverse()
for index in arr_path:
print(index.tolist())
import time
A_star(rootNode)
# print("close " ,closed_list )