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

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}
主站蜘蛛池模板: 西华县| 双流县| 三明市| 松江区| 陇西县| 泰宁县| 顺昌县| 许昌市| 象山县| 龙里县| 长岭县| 金昌市| 扎鲁特旗| 莱州市| 磐安县| 镇江市| 额济纳旗| 定兴县| 永济市| 鹤壁市| 拜泉县| 建平县| 鄂尔多斯市| 布拖县| 鹤峰县| 南昌市| 防城港市| 景德镇市| 龙井市| 天津市| 清新县| 晋中市| 黄平县| 儋州市| 亚东县| 丹江口市| 兴文县| 赤城县| 分宜县| 西昌市| 黔南|