poke-env, a python library for Pokemon Reinforcement Learning
Tags: python, machinelearning, opensource, octograd2020
Published: 2020-05-20 05:52:14

In Spring 2019, I worked on a [Reinforcement Learning university project](https://github.com/hsahovic/reinforcement-learning-pokemon-bot) centered around creating Pokemon battling bots for [Pokemon Showdown](https://play.pokemonshowdown.com/), a popular and open-source simulator. Reinforcement Learning (RL) is a branch of AI whose goal is to design _agents_ interacting in an _environment_, with the goal of maximizing a given _reward_. Games are a natural fit for RL, and creating the very best Pokemon agent sounded like a fun challenge.

Surprisingly, the hardest part of this project was not related to reinforcement learning. In the end, the RL-related code we wrote only made up a small fraction of our project.

The real difficulty lied in implementing everything else, which ranged from communicating with the simulation server via Websockets, parsing and formatting simulation protocol message and keeping track of the states of the battles the agents were playing.

By the end of the project, I was tempted to continue working on it, and maybe open-source it. At the same time, I realized that we were not alone in wanting to create Pokemon battling bots: [they were dozens of somewhat similar projects](https://github.com/search?q=pokemon+showdown+bot).

This observation led to an idea: what if instead of open-sourcing agents, I focused on the environment? And so, in August 2019, `poke-env`was born. 

`poke-env` is a python package that takes care of everything you need to create agents, and lets you focus on actually creating battling bots. Today, it offers a simple API, [comprehensive documentation and examples](https://poke-env.readthedocs.io/en/stable/), and many cool features such as a built-in [Open AI Gym](https://gym.openai.com/) API.

What does it look like? Well, creating an agent can be as simple as this:

```python
class YourFirstAgent(Player):
    def choose_move(self, battle):
        for move in battle.available_moves:
            if move.base_power > 90:
                # A powerful move! Let's use it
                return self.create_order(move)

        # No available move? Let's switch then!
        for switch in battle.available_switches:
            if switch.current_hp_fraction > battle.active_pokemon.current_hp_fraction:
                # This other pokemon has more HP left... Let's switch it in?
                return self.create_order(switch)

        # Not sure what to do?
        return self.choose_random_move(battle)
```

The project is now being used by a growing number of researchers, students and AI Pokemon enthusiasts around the world. It gave birth to [multiple projects](https://github.com/dem1995/pokemon-evolutionary-agents) and continues to expand.

Interested in trying it out? Getting started is a simple `pip install poke-env` away :)

We have [all the documentation](https://poke-env.readthedocs.io/en/stable/) and [examples](https://poke-env.readthedocs.io/en/stable/examples.html) you might need to get rolling. You can also check out the [project's repo](https://github.com/hsahovic/poke-env)!