-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathhooks_test_utils.py
More file actions
323 lines (263 loc) · 12.1 KB
/
hooks_test_utils.py
File metadata and controls
323 lines (263 loc) · 12.1 KB
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
# Copyright 2019 The dm_control Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Utilities for testing environment hooks."""
import collections
import contextlib
import inspect
from dm_control import composer
from dm_control import mjcf
def add_bodies_and_actuators(mjcf_model, num_actuators):
if num_actuators % 2:
raise ValueError('num_actuators is not a multiple of 2')
for _ in range(num_actuators // 2):
body = mjcf_model.worldbody.add('body')
body.add('inertial', pos=[0, 0, 0], mass=1, diaginertia=[1, 1, 1])
joint_x = body.add('joint', axis=[1, 0, 0])
mjcf_model.actuator.add('position', joint=joint_x)
joint_y = body.add('joint', axis=[0, 1, 0])
mjcf_model.actuator.add('position', joint=joint_y)
class HooksTracker:
"""Helper class for tracking call order of callbacks."""
def __init__(self, test_case, physics_timestep, control_timestep,
*args, **kwargs):
super().__init__(*args, **kwargs)
self.tracked = False
self._test_case = test_case
self._call_count = collections.defaultdict(lambda: 0)
self._physics_timestep = physics_timestep
self._physics_steps_per_control_step = (
round(int(control_timestep / physics_timestep)))
mro = inspect.getmro(type(self))
self._has_super = mro[mro.index(HooksTracker) + 1] != object
def assertEqual(self, actual, expected, msg=''):
msg = '{}: {}: {!r} != {!r}'.format(type(self), msg, actual, expected)
self._test_case.assertEqual(actual, expected, msg)
def assertHooksNotCalled(self, *hook_names):
for hook_name in hook_names:
self.assertEqual(
self._call_count[hook_name], 0,
'assertHooksNotCalled: hook_name = {!r}'.format(hook_name))
def assertHooksCalledOnce(self, *hook_names):
for hook_name in hook_names:
self.assertEqual(
self._call_count[hook_name], 1,
'assertHooksCalledOnce: hook_name = {!r}'.format(hook_name))
def assertCompleteEpisode(self, control_steps):
self.assertHooksCalledOnce('initialize_episode_mjcf',
'after_compile',
'initialize_episode')
physics_steps = control_steps * self._physics_steps_per_control_step
self.assertEqual(self._call_count['before_step'], control_steps)
self.assertEqual(self._call_count['before_substep'], physics_steps)
self.assertEqual(self._call_count['after_substep'], physics_steps)
self.assertEqual(self._call_count['after_step'], control_steps)
def assertPhysicsStepCountEqual(self, physics, expected_count):
actual_count = int(round(physics.time() / self._physics_timestep))
self.assertEqual(actual_count, expected_count)
def reset_call_counts(self):
self._call_count = collections.defaultdict(lambda: 0)
def initialize_episode_mjcf(self, random_state):
"""Implements `initialize_episode_mjcf` Composer callback."""
if self._has_super:
super().initialize_episode_mjcf(random_state)
if not self.tracked:
return
self.assertHooksNotCalled('after_compile',
'initialize_episode',
'before_step',
'before_substep',
'after_substep',
'after_step')
self._call_count['initialize_episode_mjcf'] += 1
def after_compile(self, physics, random_state):
"""Implements `after_compile` Composer callback."""
if self._has_super:
super().after_compile(physics, random_state)
if not self.tracked:
return
self.assertHooksCalledOnce('initialize_episode_mjcf')
self.assertHooksNotCalled('initialize_episode',
'before_step',
'before_substep',
'after_substep',
'after_step')
# Number of physics steps is always consistent with `before_substep`.
self.assertPhysicsStepCountEqual(physics,
self._call_count['before_substep'])
self._call_count['after_compile'] += 1
def initialize_episode(self, physics, random_state):
"""Implements `initialize_episode` Composer callback."""
if self._has_super:
super().initialize_episode(physics, random_state)
if not self.tracked:
return
self.assertHooksCalledOnce('initialize_episode_mjcf',
'after_compile')
self.assertHooksNotCalled('before_step',
'before_substep',
'after_substep',
'after_step')
# Number of physics steps is always consistent with `before_substep`.
self.assertPhysicsStepCountEqual(physics,
self._call_count['before_substep'])
self._call_count['initialize_episode'] += 1
def before_step(self, physics, *args):
"""Implements `before_step` Composer callback."""
if self._has_super:
super().before_step(physics, *args)
if not self.tracked:
return
self.assertHooksCalledOnce('initialize_episode_mjcf',
'after_compile',
'initialize_episode')
# `before_step` is only called in between complete control steps.
self.assertEqual(
self._call_count['after_step'], self._call_count['before_step'])
# Complete control steps imply complete physics steps.
self.assertEqual(
self._call_count['after_substep'], self._call_count['before_substep'])
# Number of physics steps is always consistent with `before_substep`.
self.assertPhysicsStepCountEqual(physics,
self._call_count['before_substep'])
self._call_count['before_step'] += 1
def before_substep(self, physics, *args):
"""Implements `before_substep` Composer callback."""
if self._has_super:
super().before_substep(physics, *args)
if not self.tracked:
return
self.assertHooksCalledOnce('initialize_episode_mjcf',
'after_compile',
'initialize_episode')
# We are inside a partial control step, so `after_step` should lag behind.
self.assertEqual(
self._call_count['after_step'], self._call_count['before_step'] - 1)
# `before_substep` is only called in between complete physics steps.
self.assertEqual(
self._call_count['after_substep'], self._call_count['before_substep'])
# Number of physics steps is always consistent with `before_substep`.
self.assertPhysicsStepCountEqual(
physics, self._call_count['before_substep'])
self._call_count['before_substep'] += 1
def after_substep(self, physics, random_state):
"""Implements `after_substep` Composer callback."""
if self._has_super:
super().after_substep(physics, random_state)
if not self.tracked:
return
self.assertHooksCalledOnce('initialize_episode_mjcf',
'after_compile',
'initialize_episode')
# We are inside a partial control step, so `after_step` should lag behind.
self.assertEqual(
self._call_count['after_step'], self._call_count['before_step'] - 1)
# We are inside a partial physics step, so `after_substep` should be behind.
self.assertEqual(self._call_count['after_substep'],
self._call_count['before_substep'] - 1)
# Number of physics steps is always consistent with `before_substep`.
self.assertPhysicsStepCountEqual(
physics, self._call_count['before_substep'])
self._call_count['after_substep'] += 1
def after_step(self, physics, random_state):
"""Implements `after_step` Composer callback."""
if self._has_super:
super().after_step(physics, random_state)
if not self.tracked:
return
self.assertHooksCalledOnce('initialize_episode_mjcf',
'after_compile',
'initialize_episode')
# We are inside a partial control step, so `after_step` should lag behind.
self.assertEqual(
self._call_count['after_step'], self._call_count['before_step'] - 1)
# `after_step` is only called in between complete physics steps.
self.assertEqual(
self._call_count['after_substep'], self._call_count['before_substep'])
# Number of physics steps is always consistent with `before_substep`.
self.assertPhysicsStepCountEqual(
physics, self._call_count['before_substep'])
# Check that the number of physics steps is consistent with control steps.
self.assertEqual(
self._call_count['before_substep'],
self._call_count['before_step'] * self._physics_steps_per_control_step)
self._call_count['after_step'] += 1
class TrackedEntity(HooksTracker, composer.Entity):
"""A `composer.Entity` that tracks call order of callbacks."""
def _build(self, name):
self._mjcf_root = mjcf.RootElement(model=name)
@property
def mjcf_model(self):
return self._mjcf_root
@property
def name(self):
return self._mjcf_root.model
class TrackedTask(HooksTracker, composer.NullTask):
"""A `composer.Task` that tracks call order of callbacks."""
def __init__(self, physics_timestep, control_timestep, *args, **kwargs):
super().__init__(
physics_timestep=physics_timestep,
control_timestep=control_timestep,
*args,
**kwargs)
self.set_timesteps(
physics_timestep=physics_timestep, control_timestep=control_timestep)
add_bodies_and_actuators(self.root_entity.mjcf_model, num_actuators=4)
class HooksTestMixin:
"""A mixin for an `absltest.TestCase` to track call order of callbacks."""
def setUp(self):
"""Sets up the test case."""
super().setUp()
self.num_episodes = 5
self.steps_per_episode = 100
self.control_timestep = 0.05
self.physics_timestep = 0.002
self.extra_hooks = HooksTracker(physics_timestep=self.physics_timestep,
control_timestep=self.control_timestep,
test_case=self)
self.entities = []
for i in range(9):
self.entities.append(TrackedEntity(name='entity_{}'.format(i),
physics_timestep=self.physics_timestep,
control_timestep=self.control_timestep,
test_case=self))
########################################
# Make the following entity hierarchy #
# 0 #
# 1 2 3 #
# 4 5 6 7 #
# 8 #
########################################
self.entities[4].attach(self.entities[8])
self.entities[1].attach(self.entities[4])
self.entities[1].attach(self.entities[5])
self.entities[0].attach(self.entities[1])
self.entities[2].attach(self.entities[6])
self.entities[2].attach(self.entities[7])
self.entities[0].attach(self.entities[2])
self.entities[0].attach(self.entities[3])
self.task = TrackedTask(root_entity=self.entities[0],
physics_timestep=self.physics_timestep,
control_timestep=self.control_timestep,
test_case=self)
@contextlib.contextmanager
def track_episode(self):
tracked_objects = [self.task, self.extra_hooks] + self.entities
for obj in tracked_objects:
obj.reset_call_counts()
obj.tracked = True
yield
for obj in tracked_objects:
obj.assertCompleteEpisode(self.steps_per_episode)
obj.tracked = False