Search code examples
pythonreinforcement-learningchainer

How to extend an agent class in ChainerRL in Python


I want to extend the PPO agent class in ChainerRL. I did the following:

class exPPO(chainerrl.agents.PPO):
    
    def act_and_train(self, obs, reward):
        action = chainerrl.agents.PPO(self, obs, reward)
        print("this is my exPPO act and train")
        return action

I tried with cartpole env of gym, but when doing

obs, reward = env.step(action)

it just crashes with the following output

this is my exPPO act and train
Traceback (most recent call last):
  File "C:\personal_if\extendPPO.py", line 184, in <module>
    obs, reward, done, _ = env.step(action)
  File "C:\Users\PareekHi\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\wrappers\time_limit.py", line 16, in step
    observation, reward, done, info = self.env.step(action)
  File "C:\Users\PareekHi\AppData\Local\Programs\Python\Python37\lib\site-packages\gym\envs\classic_control\cartpole.py", line 104, in step
    assert self.action_space.contains(action), err_msg
AssertionError: <chainerrl.agents.ppo.PPO object at 0x000002A986D2F508> (<class 'chainerrl.agents.ppo.PPO'>) invalid

Please help how I can extend PPO class here.


Solution

  • action = super().act_and_train(obs, reward)
    

    https://docs.python.org/3/library/functions.html#super