LoginSignup
42
33

More than 5 years have passed since last update.

Open AI GymのCartPoleコードをいじりながら仕組みを学ぶ(1)

Last updated at Posted at 2016-09-06

過去6回で、Ubuntu14.04、CUDA、chainer、dqn、LIS、Tensorflow、Open AI Gymを順次インストールした。特に前回はOpen AI Gymのモデルをいくつか試してみた。
http://qiita.com/masataka46/items/227fbfbda98a901f6720

今回はOpen AI GymのHPに載ってるCartPoleゲームのサンプルコードをいじりながら、仕組みを学んでいく。前回と同様、公式HP
https://gym.openai.com/docs
に従って進める。

以下のCartPoleコード(test06.py)をいじりながら仕組みを学ぶ

import gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        print(observation)
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break

reset()の戻り値を出力する

まずtest06.py 4行目。

observation = env.reset()

このreset()関数でゲームが始まる。戻り値は初期状態のobservation。よって、この段階のobservationを出力してみる。

print "initial observation = ",
print observation

結果

initial observation =  [ 0.01497125  0.03562966 -0.0273448  -0.01692216]
[ 0.01497125  0.03562966 -0.0273448  -0.01692216]

初期状態のobservationと1 step目のobservationは同じになった。よくみたら7行目でそのまま出力されてるので当たり前。

action_spaceを出力する

まずenvクラス
https://github.com/openai/gym/blob/master/gym/core.py
を見ると、env.action_spaceはspaceクラスのオブジェクトで、有効なactionを表しているそう。
test06.py 8行目のenv.action_spaceがそれ。よって

if i_episode == 0 and t == 0:
    print(env.action_space)

として確かめてみる。結果、

Discrete(2)

と出力された。Discreteクラスは
https://github.com/openai/gym/blob/master/gym/spaces/discrete.py
に書かれている。spaceクラスの子クラス。discrete(2)は、取りうるべきactionが0と1の2つを意味するそう。

observation_spaceを出力する

env.observation_spaceは
https://github.com/openai/gym/blob/master/gym/core.py
にはspaceクラスのオブジェクトで、有効なobservationを表すそう。このobservation_spaceを出力してみる。

if i_episode == 0 and t == 0:
    print(env.observation_space)

結果、

Box(4,)

と出た。Boxクラスはここ
https://github.com/openai/gym/blob/master/gym/spaces/box.py
に書かれている。やはりspaceクラスの子クラス。有効なobservationは4次元だそう。

さらにBoxクラスにはhighとかlowとかの属性があるので、それを出力してみる。

if i_episode == 0 and t == 0:
    print("env.observation_space.high is "),
    print(env.observation_space.high)
    print("env.observation_space.low is "),
    print(env.observation_space.low)

結果、

env.observation_space.high is 
[  4.80000000e+00   3.40282347e+38   4.18879020e-01   3.40282347e+38]
env.observation_space.low is 
[ -4.80000000e+00  -3.40282347e+38  -4.18879020e-01  -3.40282347e+38]

などと出力された。

42
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
33