File size: 2,242 Bytes
a03a89b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for MiniGrid observation text rendering."""

from types import SimpleNamespace

import numpy as np

from MiniGridEnv.env.grid_to_text import grid_to_text


def _empty_obs():
    grid = np.ones((7, 7, 3), dtype=np.int64)  # object type: empty
    grid[:, :, 1] = 0  # default color red (unused for empties)
    grid[:, :, 2] = 0
    return {"image": grid, "direction": 0, "mission": "go to the red ball"}


def test_empty_room_description():
    text = grid_to_text(_empty_obs())
    assert "Mission: go to the red ball" in text
    assert "You are facing east." in text
    assert "Notable objects: none visible." in text


def test_object_detection():
    obs = _empty_obs()
    obs["image"][4, 2, 0] = 6  # ball
    obs["image"][4, 2, 1] = 0  # red
    text = grid_to_text(obs)
    assert "red ball" in text


def test_door_states():
    obs = _empty_obs()
    obs["image"][5, 3, 0] = 4  # door ahead
    obs["image"][5, 3, 1] = 2  # blue
    obs["image"][5, 3, 2] = 1  # closed
    text = grid_to_text(obs)
    assert "closed blue door" in text


def test_wall_boundaries():
    obs = _empty_obs()
    obs["image"][5, 3, 0] = 2  # wall
    text = grid_to_text(obs)
    assert "Directly ahead: a wall." in text


def test_carrying_object():
    carrying = SimpleNamespace(type="key", color="blue")
    text = grid_to_text(_empty_obs(), carrying=carrying)
    assert "You are carrying: a blue key." in text


def test_egocentric_directions():
    obs = _empty_obs()
    obs["image"][1, 3, 0] = 5  # key
    obs["image"][1, 3, 1] = 1  # green
    text = grid_to_text(obs)
    assert "5 steps ahead" in text


def test_multiple_objects():
    obs = _empty_obs()
    obs["image"][5, 2, 0] = 5  # key
    obs["image"][5, 2, 1] = 2  # blue
    obs["image"][4, 4, 0] = 6  # ball
    obs["image"][4, 4, 1] = 0  # red
    obs["image"][5, 3, 0] = 4  # door
    obs["image"][5, 3, 1] = 4  # yellow
    obs["image"][5, 3, 2] = 0  # open
    text = grid_to_text(obs)
    assert "blue key" in text
    assert "red ball" in text
    assert "open yellow door" in text


def test_unseen_cells_ignored():
    obs = _empty_obs()
    obs["image"][1, 1, 0] = 0  # unseen
    text = grid_to_text(obs)
    assert "Notable objects: none visible." in text