CustomEnviorement AssertionError

Hi Guys,

I am just creating an OpenAI Gym RLLib project where the agent needs to learn to place small boxes into different big boxes. Within an observation/run the observation space won’t change but it will be that in every new run it will change. So within one run the observation space is discrete.
So I build the observation space dynamically as a DICT.

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

Now when I run it I get the following eror:

AssertionError: ERROR: elem (Box(0, 2, (2,), int16)) must be np.array, float or int!

I think it is based on that line:

gym.spaces.Box(low=np.array([0, 0]), high=np.array([highx, highy]), shape=(2,), dtype=int)

But even when I put: low=0 and high = 5 it produces the same error

I am totaly lost now and Internet and even ChatGPT are not helpful anymore.
I am absolute beginner to Python and OpenAI Gym/RLLib. This is my first project.
Thanks for any helpful reply.

Hi @Paulchen29 ,

there might be a couple of things that are missing in your environment. First of all the step() function is missing, second reset() is returning an observation not a space - your’s is returning a space.

Also, in this discussion board we prefer reproducable examples. If you provide reproducable examples, more people will probably jump on your issue.