|
| 1 | +# Copyright 2019 The dm_control Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================ |
| 15 | + |
| 16 | +"""Tests for the action scale wrapper.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +# Internal dependencies. |
| 23 | +from absl.testing import absltest |
| 24 | +from absl.testing import parameterized |
| 25 | +from dm_control.rl import control |
| 26 | +from dm_control.suite.wrappers import action_scale |
| 27 | +from dm_env import specs |
| 28 | +import mock |
| 29 | +import numpy as np |
| 30 | + |
| 31 | + |
| 32 | +def make_action_spec(lower=(-1.,), upper=(1.,)): |
| 33 | + lower, upper = np.broadcast_arrays(lower, upper) |
| 34 | + return specs.BoundedArray( |
| 35 | + shape=lower.shape, dtype=float, minimum=lower, maximum=upper) |
| 36 | + |
| 37 | + |
| 38 | +def make_mock_env(action_spec): |
| 39 | + action_spec = action_spec |
| 40 | + env = mock.Mock(spec=control.Environment) |
| 41 | + env.action_spec.return_value = action_spec |
| 42 | + return env |
| 43 | + |
| 44 | + |
| 45 | +class ActionScaleTest(parameterized.TestCase): |
| 46 | + |
| 47 | + def assertStepCalledOnceWithCorrectAction(self, env, expected_action): |
| 48 | + # NB: `assert_called_once_with()` doesn't support numpy arrays. |
| 49 | + env.step.assert_called_once() |
| 50 | + actual_action = env.step.call_args_list[0][0][0] |
| 51 | + np.testing.assert_array_equal(expected_action, actual_action) |
| 52 | + |
| 53 | + @parameterized.parameters( |
| 54 | + { |
| 55 | + 'minimum': np.r_[-1., -1.], |
| 56 | + 'maximum': np.r_[1., 1.], |
| 57 | + 'scaled_minimum': np.r_[-2., -2.], |
| 58 | + 'scaled_maximum': np.r_[2., 2.], |
| 59 | + }, |
| 60 | + { |
| 61 | + 'minimum': np.r_[-2., -2.], |
| 62 | + 'maximum': np.r_[2., 2.], |
| 63 | + 'scaled_minimum': np.r_[-1., -1.], |
| 64 | + 'scaled_maximum': np.r_[1., 1.], |
| 65 | + }, |
| 66 | + { |
| 67 | + 'minimum': np.r_[-1., -1.], |
| 68 | + 'maximum': np.r_[1., 1.], |
| 69 | + 'scaled_minimum': np.r_[-2., -2.], |
| 70 | + 'scaled_maximum': np.r_[1., 1.], |
| 71 | + }, |
| 72 | + { |
| 73 | + 'minimum': np.r_[-1., -1.], |
| 74 | + 'maximum': np.r_[1., 1.], |
| 75 | + 'scaled_minimum': np.r_[-1., -1.], |
| 76 | + 'scaled_maximum': np.r_[2., 2.], |
| 77 | + }, |
| 78 | + ) |
| 79 | + def test_step(self, minimum, maximum, scaled_minimum, scaled_maximum): |
| 80 | + action_spec = make_action_spec(lower=minimum, upper=maximum) |
| 81 | + env = make_mock_env(action_spec=action_spec) |
| 82 | + wrapped_env = action_scale.Wrapper( |
| 83 | + env, minimum=scaled_minimum, maximum=scaled_maximum) |
| 84 | + |
| 85 | + time_step = wrapped_env.step(scaled_minimum) |
| 86 | + self.assertStepCalledOnceWithCorrectAction(env, minimum) |
| 87 | + self.assertIs(time_step, env.step(minimum)) |
| 88 | + |
| 89 | + env.reset_mock() |
| 90 | + |
| 91 | + time_step = wrapped_env.step(scaled_maximum) |
| 92 | + self.assertStepCalledOnceWithCorrectAction(env, maximum) |
| 93 | + self.assertIs(time_step, env.step(maximum)) |
| 94 | + |
| 95 | + @parameterized.parameters( |
| 96 | + { |
| 97 | + 'minimum': np.r_[-1., -1.], |
| 98 | + 'maximum': np.r_[1., 1.], |
| 99 | + }, |
| 100 | + { |
| 101 | + 'minimum': np.r_[0, 1], |
| 102 | + 'maximum': np.r_[2, 3], |
| 103 | + }, |
| 104 | + ) |
| 105 | + def test_correct_action_spec(self, minimum, maximum): |
| 106 | + original_action_spec = make_action_spec( |
| 107 | + lower=np.r_[-2., -2.], upper=np.r_[2., 2.]) |
| 108 | + env = make_mock_env(action_spec=original_action_spec) |
| 109 | + wrapped_env = action_scale.Wrapper(env, minimum=minimum, maximum=maximum) |
| 110 | + new_action_spec = wrapped_env.action_spec() |
| 111 | + np.testing.assert_array_equal(new_action_spec.minimum, minimum) |
| 112 | + np.testing.assert_array_equal(new_action_spec.maximum, maximum) |
| 113 | + |
| 114 | + @parameterized.parameters('reset', 'observation_spec', 'control_timestep') |
| 115 | + def test_method_delegated_to_underlying_env(self, method_name): |
| 116 | + env = make_mock_env(action_spec=make_action_spec()) |
| 117 | + wrapped_env = action_scale.Wrapper(env, minimum=0, maximum=1) |
| 118 | + env_method = getattr(env, method_name) |
| 119 | + wrapper_method = getattr(wrapped_env, method_name) |
| 120 | + out = wrapper_method() |
| 121 | + env_method.assert_called_once_with() |
| 122 | + self.assertIs(out, env_method()) |
| 123 | + |
| 124 | + def test_invalid_action_spec_type(self): |
| 125 | + action_spec = [make_action_spec()] * 2 |
| 126 | + env = make_mock_env(action_spec=action_spec) |
| 127 | + with self.assertRaisesWithLiteralMatch( |
| 128 | + ValueError, |
| 129 | + action_scale._ACTION_SPEC_MUST_BE_BOUNDED_ARRAY.format(action_spec)): |
| 130 | + action_scale.Wrapper(env, minimum=0, maximum=1) |
| 131 | + |
| 132 | + @parameterized.parameters( |
| 133 | + {'name': 'minimum', 'bounds': np.r_[np.nan]}, |
| 134 | + {'name': 'minimum', 'bounds': np.r_[-np.inf]}, |
| 135 | + {'name': 'maximum', 'bounds': np.r_[np.inf]}, |
| 136 | + ) |
| 137 | + def test_non_finite_bounds(self, name, bounds): |
| 138 | + kwargs = {'minimum': np.r_[-1.], 'maximum': np.r_[1.]} |
| 139 | + kwargs[name] = bounds |
| 140 | + env = make_mock_env(action_spec=make_action_spec()) |
| 141 | + with self.assertRaisesWithLiteralMatch( |
| 142 | + ValueError, |
| 143 | + action_scale._MUST_BE_FINITE.format(name=name, bounds=bounds)): |
| 144 | + action_scale.Wrapper(env, **kwargs) |
| 145 | + |
| 146 | + @parameterized.parameters( |
| 147 | + {'name': 'minimum', 'bounds': np.r_[1., 2., 3.]}, |
| 148 | + {'name': 'minimum', 'bounds': np.r_[[1.], [2.], [3.]]}, |
| 149 | + ) |
| 150 | + def test_invalid_bounds_shape(self, name, bounds): |
| 151 | + shape = (2,) |
| 152 | + kwargs = {'minimum': np.zeros(shape), 'maximum': np.ones(shape)} |
| 153 | + kwargs[name] = bounds |
| 154 | + action_spec = make_action_spec(lower=[-1, -1], upper=[2, 3]) |
| 155 | + env = make_mock_env(action_spec=action_spec) |
| 156 | + with self.assertRaisesWithLiteralMatch( |
| 157 | + ValueError, |
| 158 | + action_scale._MUST_BROADCAST.format( |
| 159 | + name=name, bounds=bounds, shape=shape)): |
| 160 | + action_scale.Wrapper(env, **kwargs) |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + absltest.main() |
0 commit comments