首页 > 解决方案 > 如何在 openai-gym 环境中使用“离散”对象?

问题描述

我正在尝试为 openai-gym “Blackjack-v0”环境创建一个 Q-Learning 代理。我试图获得观察空间的大小,但它的形式是“元组”和“离散”对象。

我想要的只是返回“离散”对象的大小。当我打印“env.observation_space[0]”时,它返回“离散(32)”。我在 github 上找到了这个类(https://github.com/openai/gym/blob/master/gym/spaces/discrete.py),但没有显示如何返回整数“32”甚至值在说“env.observation_space[0][5]”。

是否有其他函数可以用来返回“离散”对象的大小以及某个索引处的值本身?

这是一些代码:

print(state_size[0]) # Discrete(32)
# I want it to print 32, not Discrete(32)
print(state_size[1]) # Discrete(11)
# I want it to print 11, not Discrete(11)
print(state_size[2]) # Discrete(2)
# I want it to print 2, not Discrete(2)

print(q_table[state_size[0][0]]) # TypeError: 'Discrete' object does not support indexing 
# I want to return the value of the "Discrete" object

标签: pythonopenai-gym

解决方案


在您的情况下,您可以使用nDiscrete 对象的属性。

例子:

env.observation_space[0].n >> 32

推荐阅读