-
Notifications
You must be signed in to change notification settings - Fork 0
/
personalPathfinding.py
132 lines (111 loc) · 3.29 KB
/
personalPathfinding.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import math
def caluclateHeuristic(a, b):
dx = b[0] - a[0]
dy = b[1] - a[1]
return(math.sqrt(dx * dx + dy * dy))
size = (width, height) = 800, 400
cols, rows = 40, 40
grid = []
openSet, closeSet = [], []
path = []
w = width//cols
h = height//rows
class Node:
def __init__(self, x, y):
self.x = x
self.y = y
self.neighbors = []
self.prev = None
self.wall = False
self.g = 0
self.f = 1000
def addNeighbors(self, grid):
if self.x < cols - 1:
self.neighbors.append(grid[self.x+1][self.y])
if self.x > 0:
self.neighbors.append(grid[self.x-1][self.y])
if self.y < rows - 1:
self.neighbors.append(grid[self.x][self.y+1])
if self.y > 0:
self.neighbors.append(grid[self.x][self.y-1])
def createWall(x, y):
grid[x][y].wall = True
for x in range(cols):
arr = []
for y in range(rows):
arr.append(Node(x, y))
grid.append(arr)
for i in range(cols):
for j in range(rows):
grid[i][j].addNeighbors(grid)
def aStar(ghostPos, pacPos):
start = grid[ghostPos[0]][ghostPos[1]]
goal = grid[pacPos[0]][pacPos[1]]
cur = start
cur.f = caluclateHeuristic([cur.x, cur.y], [goal.x, goal.y])
openSet.append(start)
iter = 0
while(cur != goal):
curF = 1000
for neighbors in cur.neighbors:
iter+= 1
if(neighbors in closeSet or neighbors.wall):
continue
else:
neighbors.g = cur.g + 1
neighbors.h = caluclateHeuristic([neighbors.x, neighbors.y], [goal.x, goal.y])
neighbors.f = neighbors.g + neighbors.h
if(curF > neighbors.f):
best = neighbors
curF = neighbors.f
if(best != cur):
openSet.append(best)
best.prev = cur
#print(cur.x, cur.y)
openSet.remove(cur)
closeSet.append(cur)
if(openSet != []):
cur = openSet[0]
while(cur.prev != None):
path.append(cur.prev)
cur = cur.prev
print(iter)
return path
createWall(4, 5)
createWall(4, 4)
p = aStar([0, 0], [21, 21])
for j in p:
print(j.x, j.y)
def aStar(ghostPos, pacPos):
start = grid[ghostPos[0]][ghostPos[1]]
goal = grid[pacPos[0]][pacPos[1]]
cur = start
cur.f = caluclateHeuristic([cur.x, cur.y], [goal.x, goal.y])
openSet.append(start)
iter = 0
while(cur != goal):
curF = 1000
for neighbors in cur.neighbors:
iter+= 1
if(neighbors in closeSet or neighbors.wall):
continue
else:
neighbors.g = cur.g + 1
neighbors.h = caluclateHeuristic([neighbors.x, neighbors.y], [goal.x, goal.y])
neighbors.f = neighbors.g + neighbors.h
if(curF > neighbors.f):
best = neighbors
curF = neighbors.f
if(best != cur):
openSet.append(best)
best.prev = cur
#print(cur.x, cur.y)
openSet.remove(cur)
closeSet.append(cur)
if(openSet != []):
cur = openSet[0]
while(cur.prev != None):
path.append(cur.prev)
cur = cur.prev
print(iter)
return path