agents icon indicating copy to clipboard operation
agents copied to clipboard

wrappers don't work together with gym

Open ThorAvaTahr opened this issue 5 years ago • 2 comments

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'>)

ThorAvaTahr avatar Feb 25 '21 15:02 ThorAvaTahr

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)       

ThorAvaTahr avatar Feb 27 '21 17:02 ThorAvaTahr

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.

ThorAvaTahr avatar Mar 02 '21 11:03 ThorAvaTahr