-
Notifications
You must be signed in to change notification settings - Fork 215
/
simple_agent.py
448 lines (371 loc) · 17 KB
/
simple_agent.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
'''The base simple agent use to train agents.
This agent is also the benchmark for other agents.
'''
from collections import defaultdict
import queue
import random
import numpy as np
from . import BaseAgent
from .. import constants
from .. import utility
class SimpleAgent(BaseAgent):
"""This is a baseline agent. After you can beat it, submit your agent to
compete.
"""
def __init__(self, *args, **kwargs):
super(SimpleAgent, self).__init__(*args, **kwargs)
# Keep track of recently visited uninteresting positions so that we
# don't keep visiting the same places.
self._recently_visited_positions = []
self._recently_visited_length = 6
# Keep track of the previous direction to help with the enemy standoffs.
self._prev_direction = None
def act(self, obs, action_space):
def convert_bombs(bomb_map):
'''Flatten outs the bomb array'''
ret = []
locations = np.where(bomb_map > 0)
for r, c in zip(locations[0], locations[1]):
ret.append({
'position': (r, c),
'blast_strength': int(bomb_map[(r, c)])
})
return ret
my_position = tuple(obs['position'])
board = np.array(obs['board'])
bombs = convert_bombs(np.array(obs['bomb_blast_strength']))
enemies = [constants.Item(e) for e in obs['enemies']]
ammo = int(obs['ammo'])
blast_strength = int(obs['blast_strength'])
items, dist, prev = self._djikstra(
board, my_position, bombs, enemies, depth=10)
# Move if we are in an unsafe place.
unsafe_directions = self._directions_in_range_of_bomb(
board, my_position, bombs, dist)
if unsafe_directions:
directions = self._find_safe_directions(
board, my_position, unsafe_directions, bombs, enemies)
return random.choice(directions).value
# Lay pomme if we are adjacent to an enemy.
if self._is_adjacent_enemy(items, dist, enemies) and self._maybe_bomb(
ammo, blast_strength, items, dist, my_position):
return constants.Action.Bomb.value
# Move towards an enemy if there is one in exactly three reachable spaces.
direction = self._near_enemy(my_position, items, dist, prev, enemies, 3)
if direction is not None and (self._prev_direction != direction or
random.random() < .5):
self._prev_direction = direction
return direction.value
# Move towards a good item if there is one within two reachable spaces.
direction = self._near_good_powerup(my_position, items, dist, prev, 2)
if direction is not None:
return direction.value
# Maybe lay a bomb if we are within a space of a wooden wall.
if self._near_wood(my_position, items, dist, prev, 1):
if self._maybe_bomb(ammo, blast_strength, items, dist, my_position):
return constants.Action.Bomb.value
else:
return constants.Action.Stop.value
# Move towards a wooden wall if there is one within two reachable spaces and you have a bomb.
direction = self._near_wood(my_position, items, dist, prev, 2)
if direction is not None:
directions = self._filter_unsafe_directions(board, my_position,
[direction], bombs)
if directions:
return directions[0].value
# Choose a random but valid direction.
directions = [
constants.Action.Stop, constants.Action.Left,
constants.Action.Right, constants.Action.Up, constants.Action.Down
]
valid_directions = self._filter_invalid_directions(
board, my_position, directions, enemies)
directions = self._filter_unsafe_directions(board, my_position,
valid_directions, bombs)
directions = self._filter_recently_visited(
directions, my_position, self._recently_visited_positions)
if len(directions) > 1:
directions = [k for k in directions if k != constants.Action.Stop]
if not len(directions):
directions = [constants.Action.Stop]
# Add this position to the recently visited uninteresting positions so we don't return immediately.
self._recently_visited_positions.append(my_position)
self._recently_visited_positions = self._recently_visited_positions[
-self._recently_visited_length:]
return random.choice(directions).value
@staticmethod
def _djikstra(board, my_position, bombs, enemies, depth=None, exclude=None):
assert (depth is not None)
if exclude is None:
exclude = [
constants.Item.Fog, constants.Item.Rigid, constants.Item.Flames
]
def out_of_range(p_1, p_2):
'''Determines if two points are out of rang of each other'''
x_1, y_1 = p_1
x_2, y_2 = p_2
return abs(y_2 - y_1) + abs(x_2 - x_1) > depth
items = defaultdict(list)
dist = {}
prev = {}
Q = queue.Queue()
my_x, my_y = my_position
for r in range(max(0, my_x - depth), min(len(board), my_x + depth)):
for c in range(max(0, my_y - depth), min(len(board), my_y + depth)):
position = (r, c)
if any([
out_of_range(my_position, position),
utility.position_in_items(board, position, exclude),
]):
continue
prev[position] = None
item = constants.Item(board[position])
items[item].append(position)
if position == my_position:
Q.put(position)
dist[position] = 0
else:
dist[position] = np.inf
for bomb in bombs:
if bomb['position'] == my_position:
items[constants.Item.Bomb].append(my_position)
while not Q.empty():
position = Q.get()
if utility.position_is_passable(board, position, enemies):
x, y = position
val = dist[(x, y)] + 1
for row, col in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
new_position = (row + x, col + y)
if new_position not in dist:
continue
if val < dist[new_position]:
dist[new_position] = val
prev[new_position] = position
Q.put(new_position)
elif (val == dist[new_position] and random.random() < .5):
dist[new_position] = val
prev[new_position] = position
return items, dist, prev
def _directions_in_range_of_bomb(self, board, my_position, bombs, dist):
ret = defaultdict(int)
x, y = my_position
for bomb in bombs:
position = bomb['position']
distance = dist.get(position)
if distance is None:
continue
bomb_range = bomb['blast_strength']
if distance > bomb_range:
continue
if my_position == position:
# We are on a bomb. All directions are in range of bomb.
for direction in [
constants.Action.Right,
constants.Action.Left,
constants.Action.Up,
constants.Action.Down,
]:
ret[direction] = max(ret[direction], bomb['blast_strength'])
elif x == position[0]:
if y < position[1]:
# Bomb is right.
ret[constants.Action.Right] = max(
ret[constants.Action.Right], bomb['blast_strength'])
else:
# Bomb is left.
ret[constants.Action.Left] = max(ret[constants.Action.Left],
bomb['blast_strength'])
elif y == position[1]:
if x < position[0]:
# Bomb is down.
ret[constants.Action.Down] = max(ret[constants.Action.Down],
bomb['blast_strength'])
else:
# Bomb is down.
ret[constants.Action.Up] = max(ret[constants.Action.Up],
bomb['blast_strength'])
return ret
def _find_safe_directions(self, board, my_position, unsafe_directions,
bombs, enemies):
def is_stuck_direction(next_position, bomb_range, next_board, enemies):
'''Helper function to do determine if the agents next move is possible.'''
Q = queue.PriorityQueue()
Q.put((0, next_position))
seen = set()
next_x, next_y = next_position
is_stuck = True
while not Q.empty():
dist, position = Q.get()
seen.add(position)
position_x, position_y = position
if next_x != position_x and next_y != position_y:
is_stuck = False
break
if dist > bomb_range:
is_stuck = False
break
for row, col in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
new_position = (row + position_x, col + position_y)
if new_position in seen:
continue
if not utility.position_on_board(next_board, new_position):
continue
if not utility.position_is_passable(next_board,
new_position, enemies):
continue
dist = abs(row + position_x - next_x) + abs(col + position_y - next_y)
Q.put((dist, new_position))
return is_stuck
# All directions are unsafe. Return a position that won't leave us locked.
safe = []
if len(unsafe_directions) == 4:
next_board = board.copy()
next_board[my_position] = constants.Item.Bomb.value
for direction, bomb_range in unsafe_directions.items():
next_position = utility.get_next_position(
my_position, direction)
next_x, next_y = next_position
if not utility.position_on_board(next_board, next_position) or \
not utility.position_is_passable(next_board, next_position, enemies):
continue
if not is_stuck_direction(next_position, bomb_range, next_board,
enemies):
# We found a direction that works. The .items provided
# a small bit of randomness. So let's go with this one.
return [direction]
if not safe:
safe = [constants.Action.Stop]
return safe
x, y = my_position
disallowed = [] # The directions that will go off the board.
for row, col in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
position = (x + row, y + col)
direction = utility.get_direction(my_position, position)
# Don't include any direction that will go off of the board.
if not utility.position_on_board(board, position):
disallowed.append(direction)
continue
# Don't include any direction that we know is unsafe.
if direction in unsafe_directions:
continue
if utility.position_is_passable(board, position,
enemies) or utility.position_is_fog(
board, position):
safe.append(direction)
if not safe:
# We don't have any safe directions, so return something that is allowed.
safe = [k for k in unsafe_directions if k not in disallowed]
if not safe:
# We don't have ANY directions. So return the stop choice.
return [constants.Action.Stop]
return safe
@staticmethod
def _is_adjacent_enemy(items, dist, enemies):
for enemy in enemies:
for position in items.get(enemy, []):
if dist[position] == 1:
return True
return False
@staticmethod
def _has_bomb(obs):
return obs['ammo'] >= 1
@staticmethod
def _maybe_bomb(ammo, blast_strength, items, dist, my_position):
"""Returns whether we can safely bomb right now.
Decides this based on:
1. Do we have ammo?
2. If we laid a bomb right now, will we be stuck?
"""
# Do we have ammo?
if ammo < 1:
return False
# Will we be stuck?
x, y = my_position
for position in items.get(constants.Item.Passage):
if dist[position] == np.inf:
continue
# We can reach a passage that's outside of the bomb strength.
if dist[position] > blast_strength:
return True
# We can reach a passage that's outside of the bomb scope.
position_x, position_y = position
if position_x != x and position_y != y:
return True
return False
@staticmethod
def _nearest_position(dist, objs, items, radius):
nearest = None
dist_to = max(dist.values())
for obj in objs:
for position in items.get(obj, []):
d = dist[position]
if d <= radius and d <= dist_to:
nearest = position
dist_to = d
return nearest
@staticmethod
def _get_direction_towards_position(my_position, position, prev):
if not position:
return None
next_position = position
while prev[next_position] != my_position:
next_position = prev[next_position]
return utility.get_direction(my_position, next_position)
@classmethod
def _near_enemy(cls, my_position, items, dist, prev, enemies, radius):
nearest_enemy_position = cls._nearest_position(dist, enemies, items,
radius)
return cls._get_direction_towards_position(my_position,
nearest_enemy_position, prev)
@classmethod
def _near_good_powerup(cls, my_position, items, dist, prev, radius):
objs = [
constants.Item.ExtraBomb, constants.Item.IncrRange,
constants.Item.Kick
]
nearest_item_position = cls._nearest_position(dist, objs, items, radius)
return cls._get_direction_towards_position(my_position,
nearest_item_position, prev)
@classmethod
def _near_wood(cls, my_position, items, dist, prev, radius):
objs = [constants.Item.Wood]
nearest_item_position = cls._nearest_position(dist, objs, items, radius)
return cls._get_direction_towards_position(my_position,
nearest_item_position, prev)
@staticmethod
def _filter_invalid_directions(board, my_position, directions, enemies):
ret = []
for direction in directions:
position = utility.get_next_position(my_position, direction)
if utility.position_on_board(
board, position) and utility.position_is_passable(
board, position, enemies):
ret.append(direction)
return ret
@staticmethod
def _filter_unsafe_directions(board, my_position, directions, bombs):
ret = []
for direction in directions:
x, y = utility.get_next_position(my_position, direction)
is_bad = False
for bomb in bombs:
bomb_x, bomb_y = bomb['position']
blast_strength = bomb['blast_strength']
if (x == bomb_x and abs(bomb_y - y) <= blast_strength) or \
(y == bomb_y and abs(bomb_x - x) <= blast_strength):
is_bad = True
break
if not is_bad:
ret.append(direction)
return ret
@staticmethod
def _filter_recently_visited(directions, my_position,
recently_visited_positions):
ret = []
for direction in directions:
if not utility.get_next_position(
my_position, direction) in recently_visited_positions:
ret.append(direction)
if not ret:
ret = directions
return ret