0

"AttributeError: 'dict' object has no attribute 'flatten'".

I get this error when I run the following code:

import math
from gym import Env
from gym.spaces import Discrete, Box, Dict, Tuple, MultiBinary, MultiDiscrete

from stable_baselines3 import PPO
screen_width = 900

class GameEnv(Env):
    def __init__(self):
        self.action_space = Discrete(5)

        observation_positions = Box(low=0, high=screen_width, shape=(2,))

        self.observation_space = Dict({'observation_positions': observation_positions})

        self.state = self.observation_space.sample()

    def step(self, action):
        self.state = self.observation_space.sample()

    def render(self):
        pass

    def reset(self):
        return self.state


env = GameEnv()

model = PPO('MlpPolicy', env, verbose=1,)
model.learn(total_timesteps=1000)

What do I have to change?

msba
  • 141
  • 1
  • 8

1 Answers1

1

You may have to use MultiInputPolicy instead of MlpPolicy as the first parameter to the PPO class when using a Dict observation space:

model = PPO('MultiInputPolicy', env, verbose=1,)
cookiedealer
  • 381
  • 1
  • 6
  • 18