Getting Started
Pre-requisites
- Python 3.6.0+ (including
pip
) - Docker (only needed for
DockerAgent
) - virtualenv (optional, for isolated Python environment)
Installation
- OPTIONAL: Setup an isolated virtual Python environment by running the following commands
$ virtualenv ~/venv
This environment needs to be activated for usage. Any package installations will now persist in this virtual environment folder only.
source ~/venv/bin/activate
- Clone the repository
$ git clone https://github.com/MultiAgentLearning/playground ~/playground
- Install the
pommerman
package. This needs to be done every time the code is updated to get the latest modules
$ cd ~/playground
$ pip install -U .
Examples
Free-For-All
The code below runs a sample Free-For-All game with two SimpleAgent's and two RandomAgent's on the board.
#!/usr/bin/python """A simple Free-For-All game with Pommerman.""" import pommerman from pommerman import agents def main(): """Simple function to bootstrap a game""" # Print all possible environments in the Pommerman registry print(pommerman.REGISTRY) # Create a set of agents (exactly four) agent_list = [ agents.SimpleAgent(), agents.RandomAgent(), agents.SimpleAgent(), agents.RandomAgent(), # agents.DockerAgent("pommerman/simple-agent", port=12345), ] # Make the "Free-For-All" environment using the agent list env = pommerman.make('PommeFFACompetition-v0', agent_list) # Run the episodes just like OpenAI Gym for i_episode in range(1): state = env.reset() done = False while not done: env.render() actions = env.act(state) state, reward, done, info = env.step(actions) print('Episode {} finished'.format(i_episode)) env.close() if __name__ == '__main__': main()
Docker Agent
The above example can be extended to use DockerAgent instead of a RandomAgent. The code below wraps a SimpleAgent inside Docker.
#!/usr/bin/python """Implementation of a simple deterministic agent using Docker.""" from pommerman import agents from pommerman.runner import DockerAgentRunner class MyAgent(DockerAgentRunner): """An example Docker agent class""" def __init__(self): self._agent = agents.SimpleAgent() def act(self, observation, action_space): return self._agent.act(observation, action_space) def main(): """Inits and runs a Docker Agent""" agent = MyAgent() agent.run() if __name__ == "__main__": main()
- We will build a docker image with the name
pommerman/simple-agent
using theDockerfile
provided.
$ cd ~/playground
$ docker build -t pommerman/simple-agent -f examples/docker-agent/Dockerfile .
- The agent list seen in the previous example can now be updated. Note that a
port
argument (of an unoccupied port) is needed to expose the HTTP server.
#!/usr/bin/python agent_list = [ agents.SimpleAgent(), agents.RandomAgent(), agents.SimpleAgent(), agents.DockerAgent("pommerman/simple-agent", port=12345) ]
Playing an interactive game
You can also play the game! See below for an example where one PlayerAgent controls with the Arrow
keys and the other with the WASD
keys.
#!/usr/bin/python agent_list = [ agents.SimpleAgent(), agents.PlayerAgent(agent_control="arrows"), # Arrows = Move, Space = Bomb agents.SimpleAgent(), agents.PlayerAgent(agent_control="wasd"), # W,A,S,D = Move, E = Bomb ]
Useful information
- Two agents cannot move to the same cell. They will bounce back to their prior places if they try. The same applies to bombs. If an agent and a bomb both try to move to the same space, then the agent will succeed but the bomb will bounce back.
- If an agent with the can_kick ability moves to a cell with a bomb, then the bomb is kicked in the direction from which the agent came. The ensuing motion will persist until the bomb hits a wall, another agent, or the edge of the grid.
- When a bomb explodes, it immediately reaches its full blast radius. If there is an agent or a wall in the way, then it prematurely ends and destroys that agent or wall.
- If a bomb is in the vicinity of an explosion, then it will also go off. In this way, bombs can chain together.
- The SimpleAgent is very useful as a barometer for your own efforts. Four SimpleAgents playing against each other have a win rate of ~18% each with the remaining ~28% of the time being a tie. Keep in mind that it can destroy itself. That can skew your own results if not properly understood.