|
| 1 | +# python3 |
| 2 | +# Copyright 2018 DeepMind Technologies Limited. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Wrapper that implements concatenation of observation fields.""" |
| 17 | + |
| 18 | +from typing import Sequence, Optional |
| 19 | + |
| 20 | +from acme import types |
| 21 | +from acme.jax import utils |
| 22 | +from acme.wrappers import base |
| 23 | +import dm_env |
| 24 | +import numpy as np |
| 25 | +import tree |
| 26 | + |
| 27 | + |
| 28 | +def _concat(values: types.NestedArray) -> np.ndarray: |
| 29 | + """Concatenates the leaves of `values` along the leading dimension. |
| 30 | +
|
| 31 | + Treats scalars as 1d arrays and expects that the shapes of all leaves are |
| 32 | + the same except for the leading dimension. |
| 33 | +
|
| 34 | + Args: |
| 35 | + values: the nested arrays to concatenate. |
| 36 | + Returns: |
| 37 | + The concatenated array. |
| 38 | + """ |
| 39 | + leaves = list(map(np.atleast_1d, tree.flatten(values))) |
| 40 | + return np.concatenate(leaves) |
| 41 | + |
| 42 | + |
| 43 | +class ConcatObservationWrapper(base.EnvironmentWrapper): |
| 44 | + """Wrapper that concatenates observation fields. |
| 45 | +
|
| 46 | + It takes an environment with nested observations and concatenates the fields |
| 47 | + in a single tensor. The orginial fields should be 1-dimensional. |
| 48 | + Observation fields that are not in name_filter are dropped. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, environment: dm_env.Environment, |
| 52 | + name_filter: Optional[Sequence[str]] = None): |
| 53 | + """Initializes a new ConcatObservationWrapper. |
| 54 | +
|
| 55 | + Args: |
| 56 | + environment: Environment to wrap. |
| 57 | + name_filter: Sequence of observation names to keep. None keeps them all. |
| 58 | + """ |
| 59 | + super().__init__(environment) |
| 60 | + observation_spec = environment.observation_spec() |
| 61 | + if name_filter is None: |
| 62 | + name_filter = list(observation_spec.keys()) |
| 63 | + self._obs_names = [x for x in name_filter if x in observation_spec.keys()] |
| 64 | + |
| 65 | + dummy_obs = utils.zeros_like(observation_spec) |
| 66 | + dummy_obs = self._convert_observation(dummy_obs) |
| 67 | + self._observation_spec = dm_env.specs.BoundedArray( |
| 68 | + shape=dummy_obs.shape, |
| 69 | + dtype=dummy_obs.dtype, |
| 70 | + minimum=-np.inf, |
| 71 | + maximum=np.inf, |
| 72 | + name='state') |
| 73 | + |
| 74 | + def _convert_observation(self, observation): |
| 75 | + obs = {k: observation[k] for k in self._obs_names} |
| 76 | + return _concat(obs) |
| 77 | + |
| 78 | + def step(self, action) -> dm_env.TimeStep: |
| 79 | + timestep = self._environment.step(action) |
| 80 | + return timestep._replace( |
| 81 | + observation=self._convert_observation(timestep.observation)) |
| 82 | + |
| 83 | + def reset(self) -> dm_env.TimeStep: |
| 84 | + timestep = self._environment.reset() |
| 85 | + return timestep._replace( |
| 86 | + observation=self._convert_observation(timestep.observation)) |
| 87 | + |
| 88 | + def observation_spec(self) -> types.NestedSpec: |
| 89 | + return self._observation_spec |
0 commit comments