Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Machine Learning by (19k points)

I have an assignment to make an AI Agent that will learn play a video game using ML. I want to create a new environment using OpenAI Gym because I don't want to use an existing environment. How can I create a new, custom, Environment?

Also, is there any other way that I can start to develop making AI Agent play a specific video game without the help of OpenAI Gym?

1 Answer

0 votes
by (33.1k points)

See my gym for an extremely small environment.

To create new environments

Create a new repository with a PIP-package structure

It should look like this




Looking at examples and at helps. Here is an example:

class FooEnv(gym.Env):

    metadata = {'render.modes': ['human']}

    def __init__(self):


    def _step(self, action):


        self.status = self.env.step()

        reward = self._get_reward()

        ob = self.env.getState()

        episode_over = self.status != hfo_py.IN_GAME

        return ob, reward, episode_over, {}

    def _reset(self):


    def _render(self, mode='human', close=False):


    def _take_action(self, action):


    def _get_reward(self):

        """ Reward is given for XY. """

        if self.status == FOOBAR:

            return 1

        elif self.status == ABC:

            return self.somestate ** 2


            return 0

Use your environment

import gym

import gym_foo

env = gym.make('MyEnv-v0')

The above code will create a gym environment in OpenAI.

Hope this answer helps.

Browse Categories