rllib/env/multi_agent_env.py
def make_multi_agent(
    env_name_or_creator: Union[str, EnvCreator],
) -> Type["MultiAgentEnv"]:
    """Convenience wrapper for any single-agent env to be converted into MA.

    Allows you to convert a simple (single-agent) `gym.Env` class
    into a `MultiAgentEnv` class. This function simply stacks n instances
    of the given ```gym.Env``` class into one unified ``MultiAgentEnv`` class
    and returns this class, thus pretending the agents act together in the
    same environment, whereas - under the hood - they live separately from
    each other in n parallel single-agent envs.

    Agent IDs in the resulting and are int numbers starting from 0
    (first agent).

    Args:
        env_name_or_creator: String specifier or env_maker function taking
            an EnvContext object as only arg and returning a gym.Env.

    Returns:
        New MultiAgentEnv class to be used as env.
        The constructor takes a config dict with `num_agents` key
        (default=1). The rest of the config dict will be passed on to the
        underlying single-agent env's constructor.

    Examples:
         >>> from ray.rllib.env.multi_agent_env import make_multi_agent
         >>> # By gym string:
         >>> ma_cartpole_cls = make_multi_agent("CartPole-v0") # doctest: +SKIP
         >>> # Create a 2 agent multi-agent cartpole.
         >>> ma_cartpole = ma_cartpole_cls({"num_agents": 2}) # doctest: +SKIP
         >>> obs = ma_cartpole.reset() # doctest: +SKIP
         >>> print(obs) # doctest: +SKIP
         {0: [...], 1: [...]}
         >>> # By env-maker callable:
         >>> from ray.rllib.examples.env.stateless_cartpole # doctest: +SKIP
         ...    import StatelessCartPole
         >>> ma_stateless_cartpole_cls = make_multi_agent( # doctest: +SKIP
         ...    lambda config: StatelessCartPole(config)) # doctest: +SKIP
         >>> # Create a 3 agent multi-agent stateless cartpole.
         >>> ma_stateless_cartpole = ma_stateless_cartpole_cls( # doctest: +SKIP
         ...    {"num_agents": 3}) # doctest: +SKIP
         >>> print(obs) # doctest: +SKIP
         {0: [...], 1: [...], 2: [...]}
    """

    class MultiEnv(MultiAgentEnv):
        def __init__(self, config=None):
            MultiAgentEnv.__init__(self)
            config = config or {}
            num = config.pop("num_agents", 1)
            if isinstance(env_name_or_creator, str):
                self.agents = [gym.make(env_name_or_creator) for _ in range(num)]
            else:
                self.agents = [env_name_or_creator(config) for _ in range(num)]
            self.dones = set()
            self.observation_space = self.agents[0].observation_space
            self.action_space = self.agents[0].action_space
            self._agent_ids = set(range(num))

        @override(MultiAgentEnv)
        def observation_space_sample(self, agent_ids: list = None) -> MultiAgentDict:
            if agent_ids is None:
                agent_ids = list(range(len(self.agents)))
            obs = {agent_id: self.observation_space.sample() for agent_id in agent_ids}

            return obs

        @override(MultiAgentEnv)
        def action_space_sample(self, agent_ids: list = None) -> MultiAgentDict:
            if agent_ids is None:
                agent_ids = list(range(len(self.agents)))
            actions = {agent_id: self.action_space.sample() for agent_id in agent_ids}

            return actions

        @override(MultiAgentEnv)
        def action_space_contains(self, x: MultiAgentDict) -> bool:
            if not isinstance(x, dict):
                return False
            return all(self.action_space.contains(val) for val in x.values())

        @override(MultiAgentEnv)
        def observation_space_contains(self, x: MultiAgentDict) -> bool:
            if not isinstance(x, dict):
                return False
            return all(self.observation_space.contains(val) for val in x.values())

        @override(MultiAgentEnv)
        def reset(self):
            self.dones = set()
            return {i: a.reset() for i, a in enumerate(self.agents)}

        @override(MultiAgentEnv)
        def step(self, action_dict):
            obs, rew, done, info = {}, {}, {}, {}
            for i, action in action_dict.items():
                obs[i], rew[i], done[i], info[i] = self.agents[i].step(action)
                if done[i]:
                    self.dones.add(i)
            done["__all__"] = len(self.dones) == len(self.agents)
            return obs, rew, done, info

        @override(MultiAgentEnv)
        def render(self, mode=None):
            return self.agents[0].render(mode)

    return MultiEnv
Convenience wrapper for any single-agent env to be converted into MA.

    Allows you to convert a simple (single-agent) `gym.Env` class
    into a `MultiAgentEnv` class. This function simply stacks n instances
    of the given ```gym.Env``` class into one unified ``MultiAgentEnv`` class
    and returns this class, thus pretending the agents act together in the
    same environment, whereas - under the hood - they live separately from
    each other in n parallel single-agent envs.

    Agent IDs in the resulting and are int numbers starting from 0
    (first agent).

    Args:
        env_name_or_creator: String specifier or env_maker function taking
            an EnvContext object as only arg and returning a gym.Env.

    Returns:
        New MultiAgentEnv class to be used as env.
        The constructor takes a config dict with `num_agents` key
        (default=1). The rest of the config dict will be passed on to the
        underlying single-agent env's constructor.

    Examples:
         >>> from ray.rllib.env.multi_agent_env import make_multi_agent
         >>> # By gym string:
         >>> ma_cartpole_cls = make_multi_agent("CartPole-v0") # doctest: +SKIP
         >>> # Create a 2 agent multi-agent cartpole.
         >>> ma_cartpole = ma_cartpole_cls({"num_agents": 2}) # doctest: +SKIP
         >>> obs = ma_cartpole.reset() # doctest: +SKIP
         >>> print(obs) # doctest: +SKIP
         {0: [...], 1: [...]}
         >>> # By env-maker callable:
         >>> from ray.rllib.examples.env.stateless_cartpole # doctest: +SKIP
         ...    import StatelessCartPole
         >>> ma_stateless_cartpole_cls = make_multi_agent( # doctest: +SKIP
         ...    lambda config: StatelessCartPole(config)) # doctest: +SKIP
         >>> # Create a 3 agent multi-agent stateless cartpole.
         >>> ma_stateless_cartpole = ma_stateless_cartpole_cls( # doctest: +SKIP
         ...    {"num_agents": 3}) # doctest: +SKIP
         >>> print(obs) # doctest: +SKIP
         {0: [...], 1: [...], 2: [...]}
    
Convenience wrapper for any single-agent env to be converted into MA.
Allows you to convert a simple (single-agent) `gym.Env` class
into a `MultiAgentEnv` class.

Agent IDs in the resulting and are int numbers starting from 0
(first agent).
Convenience wrapper for any single - agent env to be converted into MA . Allows you to convert a simple ( single - agent ) ` gym . Env ` class into a ` MultiAgentEnv ` class . Agent IDs in the resulting and are int numbers starting from 0 ( first agent ) .
def make_multi_agent(
    env_name_or_creator: Union[str, EnvCreator],
) -> Type["MultiAgentEnv"]:
    class MultiEnv(MultiAgentEnv):
        def __init__(self, config=None):
            MultiAgentEnv.__init__(self)
            config = config or {}
            num = config.pop("num_agents", 1)
            if isinstance(env_name_or_creator, str):
                self.agents = [gym.make(env_name_or_creator) for _ in range(num)]
            else:
                self.agents = [env_name_or_creator(config) for _ in range(num)]
            self.dones = set()
            self.observation_space = self.agents[0].observation_space
            self.action_space = self.agents[0].action_space
            self._agent_ids = set(range(num))
        @override(MultiAgentEnv)
        def observation_space_sample(self, agent_ids: list = None) -> MultiAgentDict:
            if agent_ids is None:
                agent_ids = list(range(len(self.agents)))
            obs = {agent_id: self.observation_space.sample() for agent_id in agent_ids}
            return obs
        @override(MultiAgentEnv)
        def action_space_sample(self, agent_ids: list = None) -> MultiAgentDict:
            if agent_ids is None:
                agent_ids = list(range(len(self.agents)))
            actions = {agent_id: self.action_space.sample() for agent_id in agent_ids}
            return actions
        @override(MultiAgentEnv)
        def action_space_contains(self, x: MultiAgentDict) -> bool:
            if not isinstance(x, dict):
                return False
            return all(self.action_space.contains(val) for val in x.values())
        @override(MultiAgentEnv)
        def observation_space_contains(self, x: MultiAgentDict) -> bool:
            if not isinstance(x, dict):
                return False
            return all(self.observation_space.contains(val) for val in x.values())
        @override(MultiAgentEnv)
        def reset(self):
            self.dones = set()
            return {i: a.reset() for i, a in enumerate(self.agents)}
        @override(MultiAgentEnv)
        def step(self, action_dict):
            obs, rew, done, info = {}, {}, {}, {}
            for i, action in action_dict.items():
                obs[i], rew[i], done[i], info[i] = self.agents[i].step(action)
                if done[i]:
                    self.dones.add(i)
            done["__all__"] = len(self.dones) == len(self.agents)
            return obs, rew, done, info
        @override(MultiAgentEnv)
        def render(self, mode=None):
            return self.agents[0].render(mode)
    return MultiEnv
def make_multi_agent ( env_name_or_creator : Union [ str , EnvCreator ] , ) -> Type [ "MultiAgentEnv" ] : class MultiEnv ( MultiAgentEnv ) : def __init__ ( self , config = None ) : MultiAgentEnv . __init__ ( self ) config = config or { } num = config . pop ( "num_agents" , 1 ) if isinstance ( env_name_or_creator , str ) : self . agents = [ gym . make ( env_name_or_creator ) for _ in range ( num ) ] else : self . agents = [ env_name_or_creator ( config ) for _ in range ( num ) ] self . dones = set ( ) self . observation_space = self . agents [ 0 ] . observation_space self . action_space = self . agents [ 0 ] . action_space self . _agent_ids = set ( range ( num ) ) @ override ( MultiAgentEnv ) def observation_space_sample ( self , agent_ids : list = None ) -> MultiAgentDict : if agent_ids is None : agent_ids = list ( range ( len ( self . agents ) ) ) obs = { agent_id : self . observation_space . sample ( ) for agent_id in agent_ids } return obs @ override ( MultiAgentEnv ) def action_space_sample ( self , agent_ids : list = None ) -> MultiAgentDict : if agent_ids is None : agent_ids = list ( range ( len ( self . agents ) ) ) actions = { agent_id : self . action_space . sample ( ) for agent_id in agent_ids } return actions @ override ( MultiAgentEnv ) def action_space_contains ( self , x : MultiAgentDict ) -> bool : if not isinstance ( x , dict ) : return False return all ( self . action_space . contains ( val ) for val in x . values ( ) ) @ override ( MultiAgentEnv ) def observation_space_contains ( self , x : MultiAgentDict ) -> bool : if not isinstance ( x , dict ) : return False return all ( self . observation_space . contains ( val ) for val in x . values ( ) ) @ override ( MultiAgentEnv ) def reset ( self ) : self . dones = set ( ) return { i : a . reset ( ) for i , a in enumerate ( self . agents ) } @ override ( MultiAgentEnv ) def step ( self , action_dict ) : obs , rew , done , info = { } , { } , { } , { } for i , action in action_dict . items ( ) : obs [ i ] , rew [ i ] , done [ i ] , info [ i ] = self . agents [ i ] . step ( action ) if done [ i ] : self . dones . add ( i ) done [ "__all__" ] = len ( self . dones ) == len ( self . agents ) return obs , rew , done , info @ override ( MultiAgentEnv ) def render ( self , mode = None ) : return self . agents [ 0 ] . render ( mode ) return MultiEnv
Convenience wrapper for any single-agent env to be converted into MA.
Convenience wrapper for any single - agent env to be converted into MA .
"""Convenience wrapper for any single-agent env to be converted into MA.

    Allows you to convert a simple (single-agent) `gym.Env` class
    into a `MultiAgentEnv` class. This function simply stacks n instances
    of the given ```gym.Env``` class into one unified ``MultiAgentEnv`` class
    and returns this class, thus pretending the agents act together in the
    same environment, whereas - under the hood - they live separately from
    each other in n parallel single-agent envs.

    Agent IDs in the resulting and are int numbers starting from 0
    (first agent).

    Args:
        env_name_or_creator: String specifier or env_maker function taking
            an EnvContext object as only arg and returning a gym.Env.

    Returns:
        New MultiAgentEnv class to be used as env.
        The constructor takes a config dict with `num_agents` key
        (default=1). The rest of the config dict will be passed on to the
        underlying single-agent env's constructor.

    Examples:
         >>> from ray.rllib.env.multi_agent_env import make_multi_agent
         >>> # By gym string:
         >>> ma_cartpole_cls = make_multi_agent("CartPole-v0") # doctest: +SKIP
         >>> # Create a 2 agent multi-agent cartpole.
         >>> ma_cartpole = ma_cartpole_cls({"num_agents": 2}) # doctest: +SKIP
         >>> obs = ma_cartpole.reset() # doctest: +SKIP
         >>> print(obs) # doctest: +SKIP
         {0: [...], 1: [...]}
         >>> # By env-maker callable:
         >>> from ray.rllib.examples.env.stateless_cartpole # doctest: +SKIP
         ...    import StatelessCartPole
         >>> ma_stateless_cartpole_cls = make_multi_agent( # doctest: +SKIP
         ...    lambda config: StatelessCartPole(config)) # doctest: +SKIP
         >>> # Create a 3 agent multi-agent stateless cartpole.
         >>> ma_stateless_cartpole = ma_stateless_cartpole_cls( # doctest: +SKIP
         ...    {"num_agents": 3}) # doctest: +SKIP
         >>> print(obs) # doctest: +SKIP
         {0: [...], 1: [...], 2: [...]}
    """
New MultiAgentEnv class to be used as env.
The constructor takes a config dict with `num_agents` key
(default=1). The rest of the config dict will be passed on to the
underlying single-agent env's constructor.
New MultiAgentEnv class to be used as env . The constructor takes a config dict with ` num_agents ` key ( default = 1 ) . The rest of the config dict will be passed on to the underlying single - agent env ' s constructor .
env_name_or_creator
Union[str, EnvCreator]
String specifier or env_maker function taking
an EnvContext object as only arg and returning a gym.Env.
String specifier or env_maker function taking an EnvContext object as only arg and returning a gym . Env .
examples
>>> from ray.rllib.env.multi_agent_env import make_multi_agent
>>> # By gym string:
>>> ma_cartpole_cls = make_multi_agent("CartPole-v0") # doctest: +SKIP
>>> # Create a 2 agent multi-agent cartpole.
>>> ma_cartpole = ma_cartpole_cls({"num_agents": 2}) # doctest: +SKIP
>>> obs = ma_cartpole.reset() # doctest: +SKIP
>>> print(obs) # doctest: +SKIP
{0: [...], 1: [...]}
>>> # By env-maker callable:
>>> from ray.rllib.examples.env.stateless_cartpole # doctest: +SKIP
import StatelessCartPole
>>> ma_stateless_cartpole_cls = make_multi_agent( # doctest: +SKIP
lambda config: StatelessCartPole(config)) # doctest: +SKIP
>>> # Create a 3 agent multi-agent stateless cartpole.
>>> from ray . rllib . env . multi_agent_env import make_multi_agent >>> # By gym string : >>> ma_cartpole_cls = make_multi_agent ( " CartPole - v0 " ) # doctest : + SKIP >>> # Create a 2 agent multi - agent cartpole . >>> ma_cartpole = ma_cartpole_cls ( { " num_agents " : 2 } ) # doctest : + SKIP >>> obs = ma_cartpole . reset () # doctest : + SKIP >>> print ( obs ) # doctest : + SKIP { 0 : [ ... ] 1 : [ ... ] } >>> # By env - maker callable : >>> from ray . rllib . examples . env . stateless_cartpole # doctest : + SKIP import StatelessCartPole >>> ma_stateless_cartpole_cls = make_multi_agent ( # doctest : + SKIP lambda config : StatelessCartPole ( config )) # doctest : + SKIP >>> # Create a 3 agent multi - agent stateless cartpole .