Let's simulate the Atari environments by following these steps:
- To run any atari environment for the first time, we need to install the atari dependencies by running this command in the Terminal:
pip install gym[atari]
Alternatively, if you used the second approach in the previous recipe to install gym, you can run the following command instead:
pip install -e '.[atari]'
- After installing the Atari dependencies, we import the gym library in Python:
>>> import gym
- Create an instance of the SpaceInvaders environment:
>>> env = gym.make('SpaceInvaders-v0')
- Reset the environment:
>>> env.reset()
array([[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
...,
[80, 89, 22],
[80, 89, 22],
[80, 89, 22]]], dtype=uint8)
As you can see, this also returns the initial state of the environment.
- Render the environment:
>>> env.render()
True
You will see a small window popping up, as follows:
As you can see from the game window, the spaceship starts with three lives (the red spaceships).
- Randomly pick one possible move and execute the action:
>>> action = env.action_space.sample()
>>> new_state, reward, is_done, info = env.step(action)
The step() method returns what happens after an action is taken, including the following:
- New state: The new observation.
- Reward: The reward associated with that action in that state.
- Is done: A flag indicating whether the game ends. In a SpaceInvaders environment, this will be True if the spaceship has no more lives left or all the aliens are gone; otherwise, it will remain False.
- Info: Extra information pertaining to the environment. This is about the number of lives left in this case. This is useful for debugging.
Let’s take a look at the is_done flag and info:
>>> print(is_done)
False
>>> print(info)
{'ale.lives': 3}
Now we render the environment:
>>> env.render()
True
The game window becomes the following:
You won't notice much difference in the game window, because the spaceship just made a move.
- Now, let's make a while loop and let the agent perform as many actions as it can:
>>> is_done = False
>>> while not is_done:
... action = env.action_space.sample()
... new_state, reward, is_done, info = env.step(action)
... print(info)
... env.render()
{'ale.lives': 3}
True
{'ale.lives': 3}
True
……
……
{'ale.lives': 2}
True
{'ale.lives': 2}
True
……
……
{'ale.lives': 1}
True
{'ale.lives': 1}
True
Meanwhile, you will see that the game is running, and the spaceship keeps moving and shooting, and so do the aliens. And it is pretty fun to watch, too. At the end, when the game ends, the window looks like the following:
As you can see, we scored 150 points in this game. You may get a higher or lower score than this because the actions the agent performs are all randomly selected.
We also confirm that no lives are left with the last piece of info:
>>> print(info)
{'ale.lives': 0}