Source code for rofunc.learning.RofuncRL.trainers.ppo_trainer
# Copyright (C) 2024, Junjia Liu
#
# This file is part of Rofunc.
#
# Rofunc is licensed under the GNU General Public License v3.0.
# You may use, distribute, and modify this code under the terms of the GPL-3.0.
#
# Additional Terms for Commercial Use:
# Commercial use requires sharing 50% of net profits with the copyright holder.
# Financial reports and regular payments must be provided as agreed in writing.
# Non-compliance results in revocation of commercial rights.
#
# For more details, see <https://www.gnu.org/licenses/>.
# Contact: skylark0924@gmail.com
from rofunc.learning.RofuncRL.agents.online.ppo_agent import PPOAgent
from rofunc.learning.RofuncRL.trainers.base_trainer import BaseTrainer
from rofunc.learning.RofuncRL.utils.memory import RandomMemory
import torch
[docs]class PPOTrainer(BaseTrainer):
def __init__(self, cfg, env, device, env_name, inference=False):
super().__init__(cfg, env, device, env_name, inference=inference)
self.memory = RandomMemory(memory_size=self.rollouts, num_envs=self.env.num_envs, device=device)
self.agent = PPOAgent(cfg.train, self.env.observation_space, self.env.action_space, self.memory,
device, self.exp_dir, self.rofunc_logger)
[docs] def pre_interaction(self):
# self.env.reset_done()
pass
[docs] def post_interaction(self):
self._rollout += 1
# Update agent
if not self._rollout % self.rollouts and self._step >= self.start_learning_steps:
self.agent.update_net()
self._update_times += 1
self.rofunc_logger.info(f'Update {self._update_times} times.', local_verbose=False)
super().post_interaction()