-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
190 lines (148 loc) · 5.82 KB
/
agent.py
File metadata and controls
190 lines (148 loc) · 5.82 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from aStar import Node
def get_state(game):
# gets current state of the game: location of head, location of body, location of food (return an array depicting the game)
snake = game.snake
head = snake[0]
body = snake[1:]
food = game.food
start = (head.x, head.y) # start node
end = (food.x, food.y)
boundary = []
for i in body:
boundary.append((i.x, i.y))
return start, end, boundary
def path_finding(size, start, end, boundaries):
# start/end = coords, size = tuple of width and height of game
open_list = []
closed_list = []
start_node = Node(None, start)
start_node.g = 0
start_node.f = 0
start_node.h = 0
end_node = Node(None, end)
end_node.g = 0
end_node.f = 0
end_node.h = 0
open_list.append(start_node)
while open_list:
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_node.f:
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
# back track to get path
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1] # reversed path
children = []
adjacent = [(0, -20), (0, 20), (-20, 0), (20, 0)]
for x in adjacent:
node_position = tuple(map(lambda i, j: i - j, current_node.position, x)) # generate adjacent coordinates
# check if node is within boundaries
if node_position[0] > size[0] or node_position[0] < 0 or node_position[1] > size[1] or node_position[1] < 0:
continue
# check for obstacles/walls
for body in boundaries:
if node_position[0] == body[0] and node_position[1] == body[1]:
break
else:
child_node = Node(current_node, node_position) # create child node
children.append(child_node)
for child in children:
for closed in closed_list: # check if child in closed list
if child == closed:
break
else:
child.g = current_node.g + 20
child.h = abs(child.position[0] - end_node.position[0]) + abs(child.position[1] - end_node.position[1]) # manhattan distance
child.f = child.g + child.h
for open_node in open_list:
if child == open_node:
if child.g >= open_node.g:
break
else:
open_list.append(child)
def longest_path(size, start, end, boundaries):
# start/end = coords
open_list = []
closed_list = []
all_paths = []
final_path = None
start_node = Node(None, start)
start_node.g = 0
start_node.f = 0
start_node.h = 0
end_node = Node(None, end)
end_node.g = 0
end_node.f = 0
end_node.h = 0
open_list.append(start_node)
counter = 0
while open_list:
counter += 1
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list): # get the largest f score to be the current node
if item.f > current_node.f:
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
# back track to get path
while current is not None:
path.append(current.position)
current = current.parent
all_paths.append(path[::-1]) # reversed path
children = []
adjacent = [(0, -20), (0, 20), (-20, 0), (20, 0)]
# generate adjacent coordinates
for x in adjacent:
node_position = tuple(map(lambda i, j: i - j, current_node.position, x))
hit = False
# check if node is within boundaries
if node_position[0] > size[0] or node_position[0] < 0 or node_position[1] > size[1] or node_position[1] < 0:
continue
# check for obstacles/walls
for body in boundaries:
if node_position[0] == body[0] and node_position[1] == body[1]:
hit = True
break
# create child node
if not hit:
child_node = Node(current_node, node_position)
children.append(child_node)
for child in children:
for closed in closed_list: # check if child in closed list
if child == closed:
break
else:
child.g = current_node.g + 40
child.h = abs(child.position[0] - end_node.position[0]) + abs(child.position[1] - end_node.position[1]) # manhattan distance
child.f = child.g + child.h
present = False # check if node is already present in open_node
for open_node in open_list:
if child == open_node:
if child.g < open_node.g:
present = True
if child == open_node:
if child.g >= open_node.g:
ind = open_list.index(open_node)
open_list[ind] = child
if not present:
open_list.append(child)
for possible in all_paths:
if not final_path:
final_path = possible
elif len(possible) >= len(final_path):
final_path = possible
return final_path[1:]