-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsoastar.py
153 lines (128 loc) · 3.95 KB
/
alsoastar.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import matrix
import random as r
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Point(x, y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class Directions:
N = Point(0, -1)
S = Point(0, 1)
E = Point(1, 0)
W = Point(-1, 0)
NE = Point(1, -1)
SE = Point(1, 1)
NW = Point(-1, -1)
SW = Point(-1, 1)
All = [N, NE, E, SE, S, SW, W, NW]
Cardinal = [N, S, E, W]
Diagonal = [NE, NW, SE, SW]
class Node:
def __init__(self, pos=None, parent=None):
self.pos = pos
self.parent = parent
self.g = 0
self.f = 0
self.h = 0
def __eq__(self, other):
return self.pos == other.pos
def heuristic(node1, node2):
dX = abs(node2.pos.x - node1.pos.x)
dY = abs(node2.pos.y - node1.pos.y)
if dX > dY: return dX
else: return dY
def getLowestF(ls):
current = ls[0]
for i in ls:
if i.f < current.f:
current = i
return current
def isOut(pos, maze):
if pos.x < 0 or pos.x >= len(maze[0]): return True
if pos.y < 0 or pos.y >= len(maze): return True
return False
def neighbors(node, maze):
neighbors = []
for direction in Directions.All: # change into 'Directions.Cardinal' for 4-way movement
newPos = node.pos + direction
if isOut(newPos, maze) or maze[newPos.y][newPos.x] != 0: continue
newNode = Node(newPos, node)
if direction in Directions.Diagonal:
newNode.g = node.g + 1.414
else:
newNode.g = node.g + 1
neighbors.append(newNode)
return neighbors
def constructPath(node):
path = [node.pos,]
while node.parent is not None:
node = node.parent
path.append(node.pos)
return reversed(path)
def aStar(start, goal, maze):
start.g = 0
start.h = heuristic(start, goal)
start.f = start.g + start.h
openList = [start,]
closedList = []
while len(openList) > 0:
current = getLowestF(openList)
if current == goal:
return constructPath(current)
openList.remove(current)
closedList.append(current)
for neighbor in neighbors(current, maze):
if neighbor not in closedList:
neighbor.h = heuristic(neighbor, goal)
neighbor.f = neighbor.g + neighbor.h
if neighbor not in openList:
openList.append(neighbor)
else:
for openNeighbor in openList:
if neighbor.pos == openNeighbor.pos:
if neighbor.g < openNeighbor.g:
openNeighbor.g = neighbor.g
openNeighbor.parent = neighbor.parent
maze = [[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,0,1],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]]
def validPoints(maze):
points = []
for y in range(len(maze)):
for x in range(len(maze[0])):
if maze[y][x] == 0:
points.append(Point(x,y))
return points
points = validPoints(maze)
startPos = r.choice(points)
endPos = r.choice(points)
start = Node(startPos, None)
end = Node(endPos, None)
path = aStar(start, end, maze)
for y in range(len(maze)):
for x in range(len(maze[0])):
if maze[y][x] == 0:
maze[y][x] = '.'
else:
maze[y][x] = '#'
for i in path:
maze[i.y][i.x] = 'X'
matrix.print_matrix(maze)
# https://www.growingwiththeweb.com/2012/06/a-pathfinding-algorithm.html