|
| 1 | +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +from types import SimpleNamespace |
| 7 | + |
| 8 | +import gymnasium as gym |
| 9 | +import torch |
| 10 | + |
| 11 | +from isaaclab.envs.utils.marl import multi_agent_to_single_agent |
| 12 | + |
| 13 | + |
| 14 | +class _FakeMultiAgentEnv: |
| 15 | + possible_agents = ["agent_0", "agent_1"] |
| 16 | + observation_spaces = { |
| 17 | + "agent_0": gym.spaces.Box(low=-1.0, high=1.0, shape=(2,)), |
| 18 | + "agent_1": gym.spaces.Box(low=-1.0, high=1.0, shape=(1,)), |
| 19 | + } |
| 20 | + action_spaces = { |
| 21 | + "agent_0": gym.spaces.Box(low=-1.0, high=1.0, shape=(1,)), |
| 22 | + "agent_1": gym.spaces.Box(low=-1.0, high=1.0, shape=(1,)), |
| 23 | + } |
| 24 | + render_mode = None |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + self.unwrapped = self |
| 28 | + self.cfg = SimpleNamespace(state_space=2) |
| 29 | + self.state_space = gym.spaces.Box(low=-1.0, high=1.0, shape=(2,)) |
| 30 | + self.sim = object() |
| 31 | + self.scene = SimpleNamespace(num_envs=2) |
| 32 | + self.episode_length_buf = torch.tensor([1, 2]) |
| 33 | + self.obs_dict = { |
| 34 | + "agent_0": torch.tensor([[1.0, 2.0], [3.0, 4.0]]), |
| 35 | + "agent_1": torch.tensor([[5.0], [6.0]]), |
| 36 | + } |
| 37 | + |
| 38 | + def reset(self, seed=None, options=None): |
| 39 | + return self.obs_dict, {} |
| 40 | + |
| 41 | + def state(self): |
| 42 | + return torch.tensor([[7.0, 8.0], [9.0, 10.0]]) |
| 43 | + |
| 44 | + def close(self): |
| 45 | + pass |
| 46 | + |
| 47 | + |
| 48 | +def test_multi_agent_to_single_agent_reset_concatenates_agents(): |
| 49 | + """The adapter reset should concatenate the agents' observations.""" |
| 50 | + env = multi_agent_to_single_agent(_FakeMultiAgentEnv()) |
| 51 | + |
| 52 | + observations, _ = env.reset() |
| 53 | + |
| 54 | + torch.testing.assert_close(observations["policy"], torch.tensor([[1.0, 2.0, 5.0], [3.0, 4.0, 6.0]])) |
| 55 | + |
| 56 | + |
| 57 | +def test_multi_agent_to_single_agent_reset_can_use_state(): |
| 58 | + """The adapter reset should support the state-as-observation mode.""" |
| 59 | + env = multi_agent_to_single_agent(_FakeMultiAgentEnv(), state_as_observation=True) |
| 60 | + |
| 61 | + observations, _ = env.reset() |
| 62 | + |
| 63 | + torch.testing.assert_close(observations["policy"], torch.tensor([[7.0, 8.0], [9.0, 10.0]])) |
| 64 | + |
| 65 | + |
| 66 | +def test_multi_agent_to_single_agent_forwards_episode_lengths(): |
| 67 | + """RSL-RL episode randomization should update the wrapped environment buffer.""" |
| 68 | + source_env = _FakeMultiAgentEnv() |
| 69 | + env = multi_agent_to_single_agent(source_env) |
| 70 | + episode_lengths = torch.tensor([3, 4]) |
| 71 | + |
| 72 | + env.episode_length_buf = episode_lengths |
| 73 | + |
| 74 | + assert env.episode_length_buf is episode_lengths |
| 75 | + assert source_env.episode_length_buf is episode_lengths |
| 76 | + |
| 77 | + |
| 78 | +def test_multi_agent_to_single_agent_exposes_latest_observations(): |
| 79 | + """The public observation buffer should reflect the wrapped environment's buffer.""" |
| 80 | + env = multi_agent_to_single_agent(_FakeMultiAgentEnv()) |
| 81 | + |
| 82 | + torch.testing.assert_close(env.obs_buf["policy"], torch.tensor([[1.0, 2.0, 5.0], [3.0, 4.0, 6.0]])) |
0 commit comments