forked from GrauleM/somogym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_step.py
188 lines (140 loc) · 5.06 KB
/
test_step.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
import os
import sys
import pytest
import argparse
from copy import deepcopy
import numpy as np
import pybullet as p
import sorotraj
import yaml
from pathlib import Path
import gym
from stable_baselines3.common.utils import set_random_seed
path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, path)
from environments.utils.import_handler import import_environment
def somogym_step_tester(
env_name,
render=False,
debug=False,
total_env_steps=5,
):
run_config_file = (
Path(os.path.dirname(__file__))
/ "../environments"
/ env_name
/ "benchmark_run_config.yaml"
)
with open(run_config_file, "r") as config_file:
run_config = yaml.safe_load(config_file)
# prepare env
import_environment(env_name)
env = gym.make(
run_config["env_id"],
run_config=run_config,
run_ID=f"{env_name}-step_test",
render=render,
debug=debug,
)
run_config["seed"] = 10110
set_random_seed(run_config["seed"])
env.seed(run_config["seed"])
env.reset()
# run env for total_env_steps steps
for _ in range(total_env_steps):
env.step(env.action_space.sample()) # take a random action
# make sure seeding works correctly for this env
# seed once, reset, and take a step
set_random_seed(run_config["seed"])
env.seed(run_config["seed"])
env.reset()
action_a = env.action_space.sample()
step_result_a = env.step(action_a) # take a random action
# seed and reset again and take another step
set_random_seed(run_config["seed"])
env.seed(run_config["seed"])
env.reset()
action_b = env.action_space.sample()
step_result_b = env.step(action_b) # take a random action
# compare results
assert (
step_result_a[0] == step_result_b[0]
).all(), f"seeding does not work correctly for env {env_name}: observations are inconsistent"
assert (
step_result_a[1] == step_result_b[1]
), f"seeding does not work correctly for env {env_name}: rewards are inconsistent"
assert (
step_result_a[2] == step_result_b[2]
), f"seeding does not work correctly for env {env_name}: done flags are inconsistent"
assert (
step_result_a[3] == step_result_b[3]
), f"seeding does not work correctly for env {env_name}: info entries are inconsistent"
# finally, close the env
env.close()
# ANTIPODAL GRIPPER
def test_AntipodalGripper_step():
somogym_step_tester("AntipodalGripper")
@pytest.mark.gui
def test_AntipodalGripper_step_gui():
somogym_step_tester("AntipodalGripper", render=True, total_env_steps=100)
# IN-HAND MANIPULATION
def test_InHandManipulation_step():
somogym_step_tester("InHandManipulation")
@pytest.mark.gui
def test_InHandManipulation_step_gui():
somogym_step_tester("InHandManipulation", render=True, total_env_steps=100)
# IN-HAND MANIPULATION INVERTED
def test_InHandManipulationInverted_step():
somogym_step_tester("InHandManipulationInverted")
@pytest.mark.gui
def test_InHandManipulationInverted_step_gui():
somogym_step_tester("InHandManipulationInverted", render=True, total_env_steps=100)
# PEN SPINNER
def test_PenSpinner_step():
somogym_step_tester("PenSpinner")
@pytest.mark.gui
def test_PenSpinner_step_gui():
somogym_step_tester("PenSpinner", render=True, total_env_steps=100)
# PLANAR BLOCK PUSHING
def test_PlanarBlockPushing_step():
somogym_step_tester("PlanarBlockPushing")
@pytest.mark.gui
def test_PlanarBlockPushing_step_gui():
somogym_step_tester("PlanarBlockPushing", render=True, total_env_steps=100)
# PLANAR REACHING
def test_PlanarReaching_step():
somogym_step_tester("PlanarReaching")
@pytest.mark.gui
def test_PlanarReaching_step_gui():
somogym_step_tester("PlanarReaching", render=True, total_env_steps=100)
# PLANAR REACHING OBSTACLE
def test_PlanarReachingObstacle_step():
somogym_step_tester("PlanarReachingObstacle")
@pytest.mark.gui
def test_PlanarReachingObstacle_step_gui():
somogym_step_tester("PlanarReachingObstacle", render=True, total_env_steps=100)
# SNAKE LOCOMOTION DISCRETE
def test_SnakeLocomotionDiscrete_step():
somogym_step_tester("SnakeLocomotionDiscrete")
@pytest.mark.gui
def test_SnakeLocomotionDiscrete_step_gui():
somogym_step_tester("SnakeLocomotionDiscrete", render=True, total_env_steps=100)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "gui":
test_AntipodalGripper_step_gui()
test_InHandManipulation_step_gui()
test_InHandManipulationInverted_step_gui()
test_PenSpinner_step_gui()
test_PlanarBlockPushing_step_gui()
test_PlanarReaching_step_gui()
test_PlanarReachingObstacle_step_gui()
test_SnakeLocomotionDiscrete_step_gui()
else:
test_AntipodalGripper_step()
test_InHandManipulation_step()
test_InHandManipulationInverted_step()
test_PenSpinner_step()
test_PlanarBlockPushing_step()
test_PlanarReaching_step()
test_PlanarReachingObstacle_step()
test_SnakeLocomotionDiscrete_step()