numpy OpenAI Gym RLLib AssertionError in CustomEnviorment

jdzmm42g  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(240)

我有一个自定义环境:

class PlacementEnv(gym.Env)

    def __init__(self, sb, bb):
        self.reward = None
        self.smallBoxes = sb
        self.bigBoxes = bb

        # Define the observation space

        i = 1
        space_to_observe = {}

        for smallbox in self.smallBoxes:
            highx = smallbox.pointMax_X
            highy = smallbox.pointMax_Y
            space_single_smallbox = {
                'points_smallbox' + str(i): gym.spaces.Box(low=np.array([0, 0]), high=np.array([highx, highy]),
                                                            shape=(2,), dtype=int),
                'bbID_smallbox' + str(i): gym.spaces.Discrete(len(self.bigBoxes))}
            space_to_observe = space_to_observe | space_single_smallbox
            i = i+1

        dict_space = gym.spaces.Dict(space_to_observe)
        self.observation_space = dict_space

        # Define action space
        self.action_space = spaces.Tuple((
            spaces.Discrete(len(self.bigBoxes)),
            spaces.Box(low=0, high=3000, shape=(2,), dtype=int), 
            spaces.Discrete(8))
        ))

    def reset(self):
        i = 0
        space_to_observe = {}

        for smallbox in self.smallBoxes:
            smallbox.bb_id = None
            smallbox.insertion_point = (0, 0)
            smallbox.rotation_angle = 0
            highx = smallbox.pointMax_X
            highy = smallbox.pointMax_Y
            space_single_smallbox = {
                'points_smallbox' + str(i): gym.spaces.Box(low=np.array([0, 0]), high=np.array([highx, highy]),
                                                            shape=(2,), dtype=int),
                'bbID_smallbox' + str(i): gym.spaces.Discrete(len(self.bigBoxes))}
            space_to_observe = space_to_observe | space_single_smallbox
            i = i+1
        dict_space = gym.spaces.Dict(space_to_observe)
        self.observation_space = dict_space
        return self.observation_space

当我运行代码时,我得到以下错误:
Assert错误:错误:elem(Box(0,2,(2,),int 16))必须是np.array,float或int!
我不明白为什么会出现这个错误。我看不出任何明显的暗示。
信息:对于每个人来说,这段代码似乎都很熟悉。这是我昨天的帖子,我现在已经删除了,因为我取得了一些进展(我认为:-))
我是Python和OpenAI Gym的绝对初学者。这是我的第一个项目。
感谢任何有用的回复。编辑:对不起,每次我想以“嗨”或“嗨伙计们”开始,当我发布问题时就被切断了。不想不礼貌!:)我在网上搜索了一下,没有成功

z18hc3ub

z18hc3ub1#

问题是reset应该返回来自环境的初始观察结果--而不是观察空间本身。因此,您应该根据问题返回一个有效的观察结果,或者执行类似return self.observation_space.sample()的操作

相关问题