官术网_书友最值得收藏!

How to do it...

Let's simulate the Atari environments by following these steps:

  1. 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]'
  1. After installing the Atari dependencies, we import the gym library in Python:
>>> import gym
  1. Create an instance of the SpaceInvaders environment:
>>> env = gym.make('SpaceInvaders-v0')
  1. 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.

  1. 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).

  1. 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.

  1. 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}
主站蜘蛛池模板: 子洲县| 嘉兴市| 清徐县| 沙洋县| 米泉市| 垦利县| 彭山县| 盐津县| 霸州市| 临清市| 桃源县| 新兴县| 奈曼旗| 太和县| 韶山市| 泸定县| 乃东县| 天气| 西乌珠穆沁旗| 鲁山县| 文成县| 多伦县| 武威市| 开江县| 东莞市| 石台县| 汶上县| 福建省| 邢台县| 西安市| 澄迈县| 南通市| 垣曲县| 什邡市| 温州市| 湾仔区| 都匀市| 黄浦区| 中西区| 鹰潭市| 六枝特区|