wrappers don't work together with gym
I was trying to add a history to the observation of a gym environment (in this example taken to be Cartpole). However I did not get any combination of wrappers to work (note below is only one of the combinations that does not work)
train_env_gym= gym.make('CartPole-v0')
train_env_suited = suite_gym.wrap_env(train_env_gym, discount=GAMMA) # wrap the gym environment
#train_env = tf_py_environment.TFPyEnvironment(train_env_suited) # training environment
train_env = HistoryWrapper(train_env_suited) # training environment
train_env = FlattenObservationsWrapper(train_env)
This particular one fails because with the following error
AttributeError: 'BoundedArraySpec' object has no attribute 'values' In call to configurable 'FlattenObservationsWrapper' (<class 'tf_agents.environments.wrappers.FlattenObservationsWrapper'>)
Ok, found it. The wrappers work on the PyEnvironment, not on the TFPyEnvironment. So the order is important here, first apply all wrappers and only at the end convert to TFPyEnvironment.
train_env_gym= gym.make('CartPole-v0')
train_env_suited = suite_gym.wrap_env(train_env_gym, discount=GAMMA) # wrap the gym environment
train_env = HistoryWrapper(train_env_suited) # training environment
train_env = FlattenObservationsWrapper(train_env) train_env = tf_py_environment.TFPyEnvironment(train_env_suited)
it would be graceful to add a check to ensure that train_env is a PyEnvironment and not a TFPyEnvironment and break with an informative error. Would have save me a few hours.