FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
dm_env/dm_env/test_utils_test.py at master · google-deepmind/dm_env · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
google-deepmind
/
dm_env
Public
Notifications
You must be signed in to change notification settings
Fork
57
Star
406
Code
Issues
5
Pull requests
3
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
dm_env
/
dm_env
/
test_utils_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (195 loc) · 6.77 KB
Breadcrumbs
dm_env
/
dm_env
/
test_utils_test.py
Copy path
File metadata and controls
235 lines (195 loc) · 6.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# pylint: disable=g-bad-file-header
# Copyright 2019 The dm_env Authors. All Rights Reserved.
#
# 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.
# ============================================================================
"""Tests for dm_env.test_utils."""
import
itertools
from
absl
.
testing
import
absltest
import
dm_env
from
dm_env
import
specs
from
dm_env
import
test_utils
import
numpy
as
np
REWARD_SPEC
=
specs
.
Array
(
shape
=
(),
dtype
=
float
)
DISCOUNT_SPEC
=
specs
.
BoundedArray
(
shape
=
(),
dtype
=
float
,
minimum
=
0
,
maximum
=
1
)
OBSERVATION_SPEC
=
specs
.
Array
(
shape
=
(
2
,
3
),
dtype
=
float
)
ACTION_SPEC
=
specs
.
BoundedArray
(
shape
=
(),
dtype
=
int
,
minimum
=
0
,
maximum
=
2
)
REWARD
=
REWARD_SPEC
.
generate_value
()
DISCOUNT
=
DISCOUNT_SPEC
.
generate_value
()
OBSERVATION
=
OBSERVATION_SPEC
.
generate_value
()
FIRST
=
dm_env
.
restart
(
observation
=
OBSERVATION
)
MID
=
dm_env
.
transition
(
reward
=
REWARD
,
observation
=
OBSERVATION
,
discount
=
DISCOUNT
)
LAST
=
dm_env
.
truncation
(
reward
=
REWARD
,
observation
=
OBSERVATION
,
discount
=
DISCOUNT
)
class
MockEnvironment
(
dm_env
.
Environment
):
def
__init__
(
self
,
timesteps
):
self
.
_timesteps
=
timesteps
self
.
_iter_timesteps
=
itertools
.
cycle
(
self
.
_timesteps
)
def
reset
(
self
):
self
.
_iter_timesteps
=
itertools
.
cycle
(
self
.
_timesteps
)
return
next
(
self
.
_iter_timesteps
)
def
step
(
self
,
action
):
return
next
(
self
.
_iter_timesteps
)
def
reward_spec
(
self
):
return
REWARD_SPEC
def
discount_spec
(
self
):
return
DISCOUNT_SPEC
def
action_spec
(
self
):
return
ACTION_SPEC
def
observation_spec
(
self
):
return
OBSERVATION_SPEC
def
_make_test_case_with_expected_failures
(
name
,
timestep_sequence
,
expected_failures
):
class
NewTestCase
(
test_utils
.
EnvironmentTestMixin
,
absltest
.
TestCase
):
def
make_object_under_test
(
self
):
return
MockEnvironment
(
timestep_sequence
)
for
method_name
,
exception_type
in
expected_failures
:
def
wrapped_method
(
self
,
method_name
=
method_name
,
exception_type
=
exception_type
):
super_method
=
getattr
(
super
(
NewTestCase
,
self
),
method_name
)
with
self
.
assertRaises
(
exception_type
):
return
super_method
()
setattr
(
NewTestCase
,
method_name
,
wrapped_method
)
NewTestCase
.
__name__
=
name
return
NewTestCase
TestValidTimestepSequence
=
_make_test_case_with_expected_failures
(
name
=
'TestValidTimestepSequence'
,
timestep_sequence
=
[
FIRST
,
MID
,
MID
,
LAST
],
expected_failures
=
[],
)
# Sequences where the ordering of StepTypes is invalid.
TestTwoFirstStepsInARow
=
_make_test_case_with_expected_failures
(
name
=
'TestTwoFirstStepsInARow'
,
timestep_sequence
=
[
FIRST
,
FIRST
,
MID
,
MID
,
LAST
],
expected_failures
=
[
(
'test_longer_action_sequence'
,
AssertionError
),
(
'test_step_after_reset'
,
AssertionError
),
(
'test_step_on_fresh_environment'
,
AssertionError
),
],
)
TestStartsWithMid
=
_make_test_case_with_expected_failures
(
name
=
'TestStartsWithMid'
,
timestep_sequence
=
[
MID
,
MID
,
LAST
],
expected_failures
=
[
(
'test_longer_action_sequence'
,
AssertionError
),
(
'test_reset'
,
AssertionError
),
(
'test_step_after_reset'
,
AssertionError
),
(
'test_step_on_fresh_environment'
,
AssertionError
),
],
)
TestMidAfterLast
=
_make_test_case_with_expected_failures
(
name
=
'TestMidAfterLast'
,
timestep_sequence
=
[
FIRST
,
MID
,
LAST
,
MID
],
expected_failures
=
[
(
'test_longer_action_sequence'
,
AssertionError
),
],
)
TestFirstAfterMid
=
_make_test_case_with_expected_failures
(
name
=
'TestFirstAfterMid'
,
timestep_sequence
=
[
FIRST
,
MID
,
FIRST
],
expected_failures
=
[
(
'test_longer_action_sequence'
,
AssertionError
),
],
)
# Sequences where one or more TimeSteps have invalid contents.
TestFirstStepHasReward
=
_make_test_case_with_expected_failures
(
name
=
'TestFirstStepHasReward'
,
timestep_sequence
=
[
FIRST
.
_replace
(
reward
=
1.0
),
# Should be None.
MID
,
MID
,
LAST
,
],
expected_failures
=
[
(
'test_reset'
,
AssertionError
),
(
'test_step_after_reset'
,
AssertionError
),
(
'test_step_on_fresh_environment'
,
AssertionError
),
(
'test_longer_action_sequence'
,
AssertionError
),
]
)
TestFirstStepHasDiscount
=
_make_test_case_with_expected_failures
(
name
=
'TestFirstStepHasDiscount'
,
timestep_sequence
=
[
FIRST
.
_replace
(
discount
=
1.0
),
# Should be None.
MID
,
MID
,
LAST
,
],
expected_failures
=
[
(
'test_reset'
,
AssertionError
),
(
'test_step_after_reset'
,
AssertionError
),
(
'test_step_on_fresh_environment'
,
AssertionError
),
(
'test_longer_action_sequence'
,
AssertionError
),
]
)
TestInvalidReward
=
_make_test_case_with_expected_failures
(
name
=
'TestInvalidReward'
,
timestep_sequence
=
[
FIRST
,
MID
.
_replace
(
reward
=
False
),
# Should be a float.
MID
,
LAST
,
],
expected_failures
=
[
(
'test_step_after_reset'
,
ValueError
),
(
'test_step_on_fresh_environment'
,
ValueError
),
(
'test_longer_action_sequence'
,
ValueError
),
]
)
TestInvalidDiscount
=
_make_test_case_with_expected_failures
(
name
=
'TestInvalidDiscount'
,
timestep_sequence
=
[
FIRST
,
MID
.
_replace
(
discount
=
1.5
),
# Should be between 0 and 1.
MID
,
LAST
,
],
expected_failures
=
[
(
'test_step_after_reset'
,
ValueError
),
(
'test_step_on_fresh_environment'
,
ValueError
),
(
'test_longer_action_sequence'
,
ValueError
),
]
)
TestInvalidObservation
=
_make_test_case_with_expected_failures
(
name
=
'TestInvalidObservation'
,
timestep_sequence
=
[
FIRST
,
MID
.
_replace
(
observation
=
np
.
zeros
((
3
,
4
))),
# Wrong shape.
MID
,
LAST
,
],
expected_failures
=
[
(
'test_step_after_reset'
,
ValueError
),
(
'test_step_on_fresh_environment'
,
ValueError
),
(
'test_longer_action_sequence'
,
ValueError
),
]
)
TestMismatchingObservationStructure
=
_make_test_case_with_expected_failures
(
name
=
'TestInvalidObservation'
,
timestep_sequence
=
[
FIRST
,
MID
.
_replace
(
observation
=
[
OBSERVATION
]),
# Wrong structure.
MID
,
LAST
,
],
expected_failures
=
[
(
'test_step_after_reset'
,
AssertionError
),
(
'test_step_on_fresh_environment'
,
AssertionError
),
(
'test_longer_action_sequence'
,
AssertionError
),
]
)
if
__name__
==
'__main__'
:
absltest
.
main
()
Back
|
FazBrowse Home
|
New Git URL