This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudentv2.py
417 lines (308 loc) · 11.7 KB
/
studentv2.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Diogo Daniel Soares Ferreira
# Luís Davide Leira
# Student Agent
import heapq
from snake import Snake
from constants import *
from game import SnakeGame
from functools import reduce
class Snake2:
def __init__(self):
self.body = []
self.head = []
self.points = 0
self.tail = []
self.last_body = []
self.last_head_positions = []
class StudentPlayer2(Snake):
def __init__(self, game, body=[(0,0)], direction=(1,0)):
super().__init__(body,direction,name="Salta Pocinhas")
self.game = game
self.jumps = []
self.jps = None
# Adds two points in the map
def add(self,a,b):
added_d = (a[0]+b[0])%(self.mapsize[0]+1),(a[1]+b[1])%(self.mapsize[1]+1)
return added_d
# Updates game data
def update(self,points=None, mapsize=None, count=None, agent_time=None):
self.mapsize=mapsize
self.count=count
self.agent_time = agent_time
def updateDirection(self,maze):
self.jps = JPS(self, self.body[0], maze.foodpos, maze)
self.path = self.jps.run("JPS")
self.game.paint([e.pos for e in self.path], (128,50,50))
self.direction=[1,0]
def get_path(self, posd1, posd2, maze):
# No Way
if posd1.pos==posd2.pos:
return None
path = []
p = []
dic = {}
validdir=[self.add(dir,posd1.pos) for dir in directions if not (self.add(posd1.pos,dir) in maze.obstacles or self.add(posd1.pos,dir) in self.body)]
if self.add(self.jps.get_dir(posd2.pos, posd1.pos),posd1.pos) in validdir:
return [self.add(self.jps.get_dir(posd2.pos, posd1.pos),posd1.pos)]
elif self.jps.get_dir(posd2.pos, posd1.pos)==[1,1]:
if self.add(posd1.pos, [1,0]) not in maze.obstacles:
return [self.add(posd1.pos, [1,0]), self.add(posd1.pos, [1,1])]
if self.add(posd1.pos, [0,1]) not in maze.obstacles:
return [self.add(posd1.pos, [0,1]), self.add(posd1.pos, [1,1])]
elif self.jps.get_dir(posd2.pos, posd1.pos)==[-1,-1]:
if self.add(posd1.pos, [-1,0]) not in maze.obstacles:
return [self.add(posd1.pos, [-1,0]), self.add(posd1.pos, [-1,-1])]
if self.add(posd1.pos, [0,-1]) not in maze.obstacles:
return [self.add(posd1.pos, [0,-1]), self.add(posd1.pos, [-1,-1])]
elif self.jps.get_dir(posd2.pos, posd1.pos)==[1,-1]:
if self.add(posd1.pos, [1,0]) not in maze.obstacles:
return [self.add(posd1.pos, [1,0]), self.add(posd1.pos, [1,-1])]
if self.add(posd1.pos, [0,-1]) not in maze.obstacles:
return [self.add(posd1.pos, [0,-1]), self.add(posd1.pos, [1,-1])]
elif self.jps.get_dir(posd2.pos, posd1.pos)==[-1,1]:
if self.add(posd1.pos, [-1,0]) not in maze.obstacles:
return [self.add(posd1.pos, [-1,0]), self.add(posd1.pos, [-1,1])]
if self.add(posd1.pos, [0,1]) not in maze.obstacles:
return [self.add(posd1.pos, [0,1]), self.add(posd1.pos, [-1,1])]
return None
# If food is too far away, reach next square
def get_goal(self, maze, pos, food):
n=30
# Get n reference points
hor = [i*self.mapsize[0]/n for i in range(n)]
vert = [i*self.mapsize[1]/n for i in range(n)]
goals = [ (h, v) for h in hor for v in vert if (h,v) not in maze.obstacles+[maze.foodpos]+self.body ]
if self.heuristic(pos, food)<self.mapsize[0]/n+self.mapsize[1]/n:
return food
heur = [(self.heuristic(pos, goal),goal) for goal in goals if self.heuristic(pos, goal)<self.heuristic(pos, food) and self.heuristic(goal, food)<self.heuristic(pos, food)]
if len(heur)==0:
return food
self.game.paint([min(heur, key=lambda x: x[0])[1]],(128,0,0))
return min(heur, key=lambda x: x[0])[1]
class JPS:
def __init__(self, snake, start, goal, maze):
self.snake = snake
self.start = start
self.goal = goal
self.maze = maze
self.all_list = {} # PosDir to best travel-cost
self.queue = [] # Priority queue on traveled + estimate
self.visited = []
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx != 0 or dy != 0:
self.add_node(self.start[0], self.start[1], (dx, dy), 0)
# Manhattan Distance, as read here: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
# Also read for breaking ties (here with 'p' variable)
def heuristic(self, pos, dir):
if dir!=None:
pos = self.snake.add(pos, dir)
best_x_move = min(abs(pos[0]-self.goal[0]), pos[0]+self.snake.mapsize[0]+1-self.goal[0], self.goal[0]+self.snake.mapsize[0]+1-pos[0])
best_y_move = min(abs(pos[1]-self.goal[1]), pos[1]+self.snake.mapsize[1]+1-self.goal[1], self.goal[1]+self.snake.mapsize[1]+1-pos[1])
# Used for breaking ties
# p <(minimum cost of taking one step)/(expected maximum path length)
p=1/(self.snake.mapsize[0]+self.snake.mapsize[1]+3)
return best_x_move+best_y_move+p
def get_dir(self, a, b):
dir = [1,0]
dir[0] = (a[0]-b[0])
if dir[0]<-1:
dir[0] = 1
elif dir[0]>1:
dir[0] = -1
dir[1] = (a[1]-b[1])
if dir[1]<-1:
dir[1] = 1
elif dir[1]>1:
dir[1] = -1
return dir
def add_node(self, x, y, dir, dist):
pd = PosDir((x, y), dir)
current = self.all_list.get(pd)
if current is None or current > dist:
total = dist + self.heuristic(pd.pos, dir)
self.all_list[pd] = dist
self.add_open(total, pd, dist)
return pd
def get_closed_node(self, x, y, dir, dist):
pd = PosDir((x, y), dir)
current = self.all_list.get(pd)
if current is not None and current <= dist:
return pd
self.all_list[pd] = dist
return pd
def add_open(self, total, pd, dist):
heapq.heappush(self.queue, (total, pd, dist))
def get_open(self):
while True:
if len(self.queue) == 0:
return None, None, None
total, pd, dist = heapq.heappop(self.queue)
current = self.all_list.get(pd)
if dist == current:
return total, pd, dist
def step(self, dist, elm):
if elm.pos == self.goal:
return elm
hor_dir, vert_dir = elm.dir
if hor_dir != 0 and vert_dir != 0:
nodes = self.search_diagonal(elm.pos, hor_dir, vert_dir, dist)
elif hor_dir != 0:
nodes = self.search_hor(elm.pos, hor_dir, dist)
else:
nodes = self.search_vert(elm.pos, vert_dir, dist)
for nd in nodes:
nd.set_parent(elm)
return None
def search_hor(self, pos, hor_dir, dist):
x0, y0 = pos
while True:
#print("H")
x1 = x0 + hor_dir
#x1 = self.snake.add(pos,[hor_dir,0])[0]
# CHANGE THIS IF RULE
if x1<0 or y0<0 or x1>self.snake.mapsize[0] or y0>self.snake.mapsize[0]:
return []
if (x1, y0) in self.maze.obstacles+self.snake.body:
return []
if (x1, y0) == self.goal:
return [self.add_node(x1, y0, None, dist + 1)]
dist = dist + 1
x2 = x1 + hor_dir
#x2 = self.snake.add([x1,pos[1]],[hor_dir,0])[0]
nodes = []
#if self.snake.add([x1,y0],[0,-1]) in self.maze.obstacles and not self.snake.add([x2,y0],[0,-1]) in self.maze.obstacles:
if (x1, y0 - 1) in self.maze.obstacles+self.snake.body and not (x2, y0 - 1) in self.maze.obstacles+self.snake.body:
nodes.append(self.add_node(x1, y0, (hor_dir, -1), dist))
#if self.snake.add([x1,y0],[0,1]) in self.maze.obstacles and not self.snake.add([x2,y0],[0,1]) in self.maze.obstacles:
if (x1, y0 + 1) in self.maze.obstacles+self.snake.body and not (x2, y0 + 1) in self.maze.obstacles+self.snake.body:
nodes.append(self.add_node(x1, y0, (hor_dir, 1), dist))
if len(nodes) > 0:
nodes.append(self.add_node(x1, y0, (hor_dir, 0), dist))
return nodes
x0 = x1
def search_vert(self, pos, vert_dir, dist):
x0, y0 = pos
while True:
#print("V")
y1 = y0 + vert_dir
#y1 = self.snake.add(pos,[0,vert_dir])[1]
# CHANGE THIS IF RULE
if x0<0 or y1<0 or x0>self.snake.mapsize[0] or y1>self.snake.mapsize[0]:
return []
if (x0, y1) in self.maze.obstacles+self.snake.body:
return []
if (x0, y1) == self.goal:
return [self.add_node(x0, y1, None, dist + 5)]
dist = dist + 1
# y2 = y1 + vert_dir
y2 = self.snake.add([pos[0],y1],[0,vert_dir])[1]
nodes = []
#if self.snake.add([x0,y1],[-1,0]) in self.maze.obstacles and not self.snake.add([x0,y2],[-1,0]) in self.maze.obstacles:
if (x0 - 1, y1) in self.maze.obstacles+self.snake.body and not (x0 - 1, y2) in self.maze.obstacles+self.snake.body:
nodes.append(self.add_node(x0, y1, (-1, vert_dir), dist))
#if self.snake.add([x0,y1],[1,0]) in self.maze.obstacles and not self.snake.add([x0,y2],[1,0]) in self.maze.obstacles:
if (x0 + 1, y1) in self.maze.obstacles+self.snake.body and not (x0 + 1, y2) in self.maze.obstacles+self.snake.body:
nodes.append(self.add_node(x0, y1, (1, vert_dir), dist))
if len(nodes) > 0:
nodes.append(self.add_node(x0, y1, (0, vert_dir), dist))
return nodes
y0 = y1
def search_diagonal(self, pos, hor_dir, vert_dir, dist):
x0, y0 = pos
while True:
#print("D")
x1, y1 = x0 + hor_dir, y0 + vert_dir
#x1, y1 = self.snake.add(pos, [hor_dir, vert_dir])
# CHANGE THIS IF
if x1<0 or y1<0 or x1>self.snake.mapsize[0] or y1>self.snake.mapsize[0]:
return []
if (x1, y1) in self.maze.obstacles+self.snake.body:
return []
if (x1, y1) == self.goal:
return [self.add_node(x1, y1, None, dist + 1)]
# Open space at (x1, y1)
dist = dist + 2
x2, y2 = x1 + hor_dir, y1 + vert_dir
#x2, y2 = self.snake.add([x1, y1], [hor_dir, vert_dir])
nodes = []
if (x0, y1) in self.maze.obstacles+self.snake.body and not (x0, y2) in self.maze.obstacles+self.snake.body:
nodes.append(self.add_node(x1, y1, (-hor_dir, vert_dir), dist))
if (x1, y0) in self.maze.obstacles+self.snake.body and not (x2, y0) in self.maze.obstacles+self.snake.body:
nodes.append(self.add_node(x1, y1, (hor_dir, -vert_dir), dist))
hor_done, vert_done = False, False
if len(nodes) == 0:
sub_nodes = self.search_hor((x1, y1), hor_dir, dist)
hor_done = True
if len(sub_nodes) > 0:
# Horizontal search ended with a spawn point.
pd = self.get_closed_node(x1, y1, (hor_dir, 0), dist)
for sub in sub_nodes:
sub.set_parent(pd)
nodes.append(pd)
if len(nodes) == 0:
sub_nodes = self.search_vert((x1, y1), vert_dir, dist)
vert_done = True
if len(sub_nodes) > 0:
# Vertical search ended with a spawn point.
pd = self.get_closed_node(x1, y1, (0, vert_dir), dist)
for sub in sub_nodes:
sub.set_parent(pd)
nodes.append(pd)
if len(nodes) > 0:
if not hor_done:
nodes.append(self.add_node(x1, y1, (hor_dir, 0), dist))
if not vert_done:
nodes.append(self.add_node(x1, y1, (0, vert_dir), dist))
nodes.append(self.add_node(x1, y1, (hor_dir, vert_dir), dist))
return nodes
# Tile done, move to next tile.
x0, y0 = x1, y1
def run(self, alg_name):
while True:
total, pd, dist = self.get_open()
if total is None:
break
pd = self.step(dist, pd)
if pd is not None:
break
while True:
total, pd, dist = self.get_open()
if total is None:
break
jumps = self.getJumps(self.goal)
#self.snake.game.paint([e.pos for e in self.getJPSPath(self.getJumps(self.goal))],(128,100,100))
return self.getJumps(self.goal)
def getJumps(self, pos):
e1 = [elem.parent for elem in self.all_list if elem.pos==pos and elem.parent!=None]
if len(e1)==0:
return []
elem2 = reduce(lambda r,h: h if self.all_list[h]<self.all_list[r] else r, e1)
if elem2==None or elem2.pos==self.start:
return [elem2]
return [elem2]+list(set(self.getJumps(elem2.pos)))
class PosDir:
def __init__(self, pos, dir):
self.pos = pos
self.dir = dir
self.parent = None
def set_parent(self, parent):
assert self.parent is None
self.parent = parent
def __eq__(self, other):
if not isinstance(other, PosDir):
return False
return self.pos == other.pos and self.dir == other.dir
def __lt__(self, other):
if not isinstance(other, PosDir):
return False
if self.pos != other.pos:
return self.pos < other.pos
return self.dir < other.dir
def __hash__(self):
return hash(self.pos) + hash(self.dir) * 737
def __repr__(self):
if self.parent!=None:
return "PosDir({}, {}, {})".format(self.pos, self.dir, self.parent.pos)
else:
return "PosDir({}, {}, None)".format(self.pos, self.dir)