forked from zoeyuchao/habitat-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_trajectory_sim.py
85 lines (69 loc) · 2.61 KB
/
test_trajectory_sim.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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import json
import os
import numpy as np
import pytest
from habitat import SimulatorActions
from habitat.config.default import get_config
from habitat.sims import make_sim
def init_sim():
config = get_config()
if not os.path.exists(config.SIMULATOR.SCENE):
pytest.skip("Please download Habitat test data to data folder.")
return make_sim(config.SIMULATOR.TYPE, config=config.SIMULATOR)
def test_sim_trajectory():
with open("test/data/habitat-sim_trajectory_data.json", "r") as f:
test_trajectory = json.load(f)
sim = init_sim()
sim.reset()
sim.set_agent_state(
position=test_trajectory["positions"][0],
rotation=test_trajectory["rotations"][0],
)
for i, action in enumerate(test_trajectory["actions"]):
action = SimulatorActions[action]
if i > 0: # ignore first step as habitat-sim doesn't update
# agent until then
state = sim.get_agent_state()
assert (
np.allclose(
np.array(
test_trajectory["positions"][i], dtype=np.float32
),
state.position,
)
is True
), "mismatch in position " "at step {}".format(i)
assert (
np.allclose(
np.array(
test_trajectory["rotations"][i], dtype=np.float32
),
np.array([*state.rotation.imag, state.rotation.real]),
)
is True
), "mismatch in rotation " "at step {}".format(i)
max_search_radius = 2.0
dist_to_obs = sim.distance_to_closest_obstacle(
state.position, max_search_radius
)
assert np.isclose(
dist_to_obs, test_trajectory["distances_to_obstacles"][i]
)
assert sim.action_space.contains(action)
sim.step(action)
if i == len(test_trajectory["actions"]) - 1: # STOP action
assert sim.is_episode_active is False
sim.close()
def test_sim_no_sensors():
config = get_config()
config.defrost()
config.SIMULATOR.AGENT_0.SENSORS = []
if not os.path.exists(config.SIMULATOR.SCENE):
pytest.skip("Please download Habitat test data to data folder.")
sim = make_sim(config.SIMULATOR.TYPE, config=config.SIMULATOR)
sim.reset()
sim.close()