Skip to content
Anurag Koul edited this page Jul 9, 2019 · 25 revisions

Basics

Let's begin by importing the basic packages

>>> import gym
>>> import ma_gym 

We have registered all the new multi agent environments

>>> env = gym.make('Switch-v0')

How many agents does this environment has?

>>> env.n_agents
>>> 2

We get a list of action_space of each agent.

>>> env.action_space
>>> [Discrete(5), Discrete(5)]

Following samples an action for each agent ( much like open-ai gym)

>>> env.action_space.sample()
>>> [0, 2]
>>> env.reset()
>>> [[0.3333333333333333, 0.2857142857142857],[0.3333333333333333, 0.8571428571428571]]

Let's step into the environment with a random action

>>> obs_n, reward_n, done_n, info = env.step(env.action_space.sample())
>>> obs_n
>>> [[0.6666666666666666, 0.2857142857142857], [0.3333333333333333, 1.0]]
>>> reward_n
>>> [-0.1, -0.1]

An episode is considered to be done when all agents die.

>>> episode_terminate = all(done_n)

Also, team reward is simply sum of all local rewards

>>> team_reward = sum(reward_n)

Customizing an environment

import gym

gym.envs.register(
    id='MySwitch2-v0',
    entry_point='gym.envs.classic_control:MountainCarEnv',
kwargs={'n_agents': 2, 'full_observable': False, 'step_cost': -0.2} # It has a step cost of -0.2 now
)
env = gym.make('MySwitch2-v0')

For more usage details , refer to : https://github.com/koulanurag/ma-gym/blob/master/ma_gym/init.py

Monitoring

Please note that the following Monitor package is imported from ma_gym

>>> from ma_gym.wrappers import Monitor
>>> env = gym.make('Switch2-v0')
>>> env = Monitor(env, directory='recordings', force=True)

This helps in saving video files in the recordings folder

Clone this wiki locally