gym env笔记

Posted by ZhY on October 20, 2018
import gym
from gym import wrappers

###--------------------------   设置env   ----------------------------###
env = gym.make('Venture-v0')
###--------------------------   列出该env名称   ----------------------------###
print(env.spec.id)
###--------------------------   设置随机种子   ----------------------------###
env.seed(0)
###--------------------------   得到所有action的含义   ----------------------------###
print("action meanings:", env.env.get_action_meanings())

print("action num:", env.action_space.n)

print("sample actions:")
for _ in range(4):
    print(env.action_space.sample())

###--------------------------   重置env   ----------------------------###
env.reset()

action = env.action_space.sample()
obs, reward, done, info = env.step(action)
###--------------------------   env可视化   ----------------------------###
env.render()


print(env.observation_space.shape)
print(env.reward_range)


###--------------------------   列出所有的env   ----------------------------###
envids = [spec.id for spec in gym.envs.registry.all()]
for envid in sorted(envids):
    print(envid)


###--------------------------   得到该游戏的video   ----------------------------###
### 需要先进行   sudo apt-get install ffmpeg
outdir = 'random-agent-results'
env = wrappers.Monitor(env, directory=outdir, force=True)
done = False
for i in range(100):
    ob = env.reset()
    while True:
        action = env.action_space.sample()
        ob, reward, done, _ = env.step(action)
        if done:
            break
env.close()