|
| 1 | +# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 2 | +# Licensed under the Apache License, Version 2.0 (the “License”); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +import gymnasium as gym |
| 17 | +from gymnasium import Wrapper |
| 18 | +from gymnasium.core import ActType, ObsType, WrapperObsType |
| 19 | +from gymnasium.spaces import Dict, Space, Text, Tuple |
| 20 | + |
| 21 | + |
| 22 | +class TaskWrapper(Wrapper[WrapperObsType, ActType, ObsType, ActType]): |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + env: gym.Env[ObsType, ActType], |
| 26 | + task: Task, |
| 27 | + *, |
| 28 | + dict_task_key: str = "task", |
| 29 | + ): |
| 30 | + super().__init__(env) |
| 31 | + self.env = env |
| 32 | + self.task = task |
| 33 | + |
| 34 | + task_space = Text(500) |
| 35 | + |
| 36 | + # Observation space in different situations |
| 37 | + if isinstance(env.observation_space, Dict): |
| 38 | + assert dict_task_key not in env.observation_space.keys() |
| 39 | + observation_space = Dict( |
| 40 | + {dict_task_key: task_space, **env.observation_space.spaces} |
| 41 | + ) |
| 42 | + self._append_data_func = lambda obs, task: {dict_task_key: task, **obs} |
| 43 | + elif isinstance(env.observation_space, Tuple): |
| 44 | + observation_space = Tuple(env.observation_space.spaces + (task_space,)) |
| 45 | + self._append_data_func = lambda obs, task: obs + (task,) |
| 46 | + else: |
| 47 | + observation_space = Dict(obs=env.observation_space, task=task_space) |
| 48 | + self._append_data_func = lambda obs, task: {"obs": obs, "task": task} |
| 49 | + |
| 50 | + self.observation_space: gym.Space[WrapperObsType] = observation_space |
| 51 | + |
| 52 | + def reset( |
| 53 | + self, *, seed: int | None = None, options: dict[str, Any] | None = None |
| 54 | + ) -> tuple[Dict, dict[str, Any]]: |
| 55 | + """Modifies the :attr:`env` after calling :meth:`reset`, returning a modified |
| 56 | + observation using :meth:`self.observation`.""" |
| 57 | + obs, info = self.env.reset(seed=seed, options=options) |
| 58 | + return self.observation(obs), info |
| 59 | + |
| 60 | + def step( |
| 61 | + self, action: ActType |
| 62 | + ) -> tuple[WrapperObsType, float, bool, bool, dict[str, Any]]: |
| 63 | + observation, reward, terminal, truncated, info = self.step(action) |
| 64 | + reward = self.task.evaluate(self.env) |
| 65 | + return self.observation(observation), reward, terminal, truncated, info |
| 66 | + |
| 67 | + def observation(self, observation: ObsType): |
| 68 | + """Returns a modified observation. |
| 69 | +
|
| 70 | + Args: |
| 71 | + observation: The :attr:`env` observation |
| 72 | +
|
| 73 | + Returns: |
| 74 | + The modified observation |
| 75 | + """ |
| 76 | + return self._append_data_func(observation, self.task.description) |
0 commit comments