forked from mila-iqia/babyai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vis_mask_grid.py
49 lines (41 loc) · 1.52 KB
/
vis_mask_grid.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
from gym_minigrid.minigrid import Grid
import numpy as np
class VisMaskGrid(Grid):
def render(
self,
tile_size,
agent_pos=None,
agent_dir=None,
highlight_mask=None,
vis_mask=None,
):
"""
Render this grid at a given scale
:param r: target renderer object
:param tile_size: tile size in pixels
"""
if vis_mask is None:
vis_mask = np.ones(shape=(self.width, self.height), dtype=np.bool)
if highlight_mask is None:
highlight_mask = np.zeros(shape=(self.width, self.height), dtype=np.bool)
# Compute the total grid size
width_px = self.width * tile_size
height_px = self.height * tile_size
img = np.zeros(shape=(height_px, width_px, 3), dtype=np.uint8)
# Render the grid
for j in range(0, self.height):
for i in range(0, self.width):
cell = self.get(i, j)
agent_here = np.array_equal(agent_pos, (i, j))
tile_img = Grid.render_tile(
cell,
agent_dir=agent_dir if agent_here else None,
highlight=highlight_mask[i, j],
tile_size=tile_size
)
ymin = j * tile_size
ymax = (j+1) * tile_size
xmin = i * tile_size
xmax = (i+1) * tile_size
img[ymin:ymax, xmin:xmax, :] = tile_img * vis_mask[i, j] + (1 - vis_mask[i, j]) * 50
return img