为什么不同种子的破发环境是一样的?

问题描述 投票:0回答:1
import gymnasium as gym 
import numpy as np
for s in [0,1,2,3,4]:  
    env=gym.make("BreakoutNoFrameskip-v4")
    observation, info = env.reset(seed=s)
    print(s, np.sum(observation))

我尝试了以下代码,发现不同种子的突破环境的初始状态是相同的。我想知道为什么?以及如何获得不同的初始状态?

openai-gym
1个回答
0
投票

首先,它不安全...

首先,通过比较像素值之和来检查状态差异是不安全的,特别是对于 Breakout 环境。

这是因为球和球拍的运动无法通过像素之和来捕捉。球拍始终为红色,具有固定的宽度和高度,球也是如此。从下面代码的结果中你可以很容易地看出:

import gymnasium as gym 
import numpy as np
from matplotlib import pyplot as plt

env = gym.make("BreakoutNoFrameskip-v4")

num_repeat = 10

plt.figure()
fig, ax = plt.subplots(2,5)

for seed in range(10):
    s0, _ = env.reset(seed=seed)
    for i in range(num_repeat):
        s1, *_ = env.step(env.action_space.sample())
    print(seed, np.sum(s0), np.sum(s1))
    ax[seed//5, seed%5].imshow(s1, interpolation='nearest')

plt.show()

生成的 10 张图像可能略有不同,但像素值之和全部相等。


回到你的问题,

看来Breakout环境本身没有随机因素,即它是确定性环境。您可以试探性地检查这一点:

num_repeat = 50
predetermined_actions = np.random.choice(env.action_space.n, num_repeat)

plt.figure()
fig, ax = plt.subplots(2,5)

for seed in range(10):
    s0, _ = env.reset(seed=seed)
    for i in range(num_repeat):
        s1, *_ = env.step(predetermined_actions[i])
    print(seed, np.sum(s0), np.sum(s1))
    ax[seed//5, seed%5].imshow(s1, interpolation='nearest')

plt.show()

无论您设置什么

seed
,如果动作序列完全相同,则输出状态始终相同。

那么,在已经确定的Breakout环境下,如何正确使用

seed
呢?

    s0, _ = env.reset()
    env.action_space.seed(seed)

文档中所述,您可以将种子设置为操作空间。

© www.soinside.com 2019 - 2024. All rights reserved.