DEV Community

Cover image for poke-env, a python library for Pokemon Reinforcement Learning
hsahovic
hsahovic

Posted on

poke-env, a python library for Pokemon Reinforcement Learning

In Spring 2019, I worked on a Reinforcement Learning university project centered around creating Pokemon battling bots for Pokemon Showdown, 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.

This observation led to an idea: what if instead of open-sourcing agents, I focused on the environment? And so, in August 2019, poke-envwas 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, and many cool features such as a built-in Open AI Gym API.

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

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 and continues to expand.

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

We have all the documentation and examples you might need to get rolling. You can also check out the project's repo!

Top comments (0)