I initialized an environment for my PPO agent and I am encountering with this error.
AssertionError: The algorithm only supports (<class 'gymnasium.spaces.box.Box'>, <class 'gymnasium.spaces.discrete.Discrete'>, <class 'gymnasium.spaces.multi_discrete.MultiDiscrete'>, <class 'gymnasium.spaces.multi_binary.MultiBinary'>) as action spaces but Discrete(3) was provided.
this was another statement in the error section. Is it a problem arising from the space.Descrete ?
here's the code:
class CustomEnv(gymnasium.Env):
def __init__(self, dataset, columns):
super(CustomEnv, self).__init__()
if dataset is None:
raise ValueError("The dataset must be provided.")
if columns is None:
raise ValueError("The columns must be provided.")
self.dataset = dataset
self.columns = columns
if not isinstance(dataset, pd.DataFrame):
raise ValueError("The dataset must be a DataFrame.")
self.dataset = dataset
self.initial_balance = 10000 # Initial balance for trading
self.current_step = 0 # Current step in the dataset
self.balance = self.initial_balance
self.holding = 0 # Number of units of the asset held by the agent
# Use "MPN5P" as the price column
self.price_column = "MPN5P"
self.current_price = self.dataset[self.price_column].iloc[self.current_step] # Current price of the asset
self.action_space = spaces.Discrete(3) # Three actions: 0 = buy, 1 = sell, 2 = hold
self.observation_space = spaces.Box(low=0, high=1, shape=(1,), dtype=np.float32)
self.columns = columns
I was expecting for the environment to initialize and train properly. But it's either resulting in giving NaN error or AssertionError.
I believe this should be some Gymnasium version issue.
Looking on their website, they define a discrete action space the same way according to this, with
spaces.Discrete(3).But from the error, we see that
'gymnasium.spaces.discrete.Discrete'is the correct class for your action space, hinting that you should probably usespaces.discrete.Discrete(3)instead. You can try this, according to the error it should work. It's surprising, here you can see it too that according to the documentation you don't have to put the 'discrete' part inbetween, so I'd guess you have an outdated version. In other case, check your Gymnasium version (not OpenAI gym) and see if it isn't too old.Otherwise, I could only think of some mistake that both OpenAI Gym and Farama's Gymnasium are present and cause some problems with the differences between them (but I find this unlikely).