v2d / rl /v2d_rl /algorithms /ppo.py
hk239's picture
Upload v2d project (excluding data and .venv) (part 12)
c1a4584 verified
Raw History Blame Contribute Delete
1.24 kB
"""RSL-RL PPO trainer stub (Isaac Lab ``OnPolicyRunner``).
Hyperparameters come from the Gym task's ``rsl_rl_cfg_entry_point``.
Swap this class when we lock the algorithm (plain PPO vs joint residual, etc.).
"""
from __future__ import annotations
from typing import Any
from .base import Algorithm
class RslRlPpoTrainer(Algorithm):
def __init__(self, agent_cfg: Any):
self.agent_cfg = agent_cfg
self._runner = None
def train(self, env: Any, log_dir: str) -> None:
from isaaclab.utils import class_to_dict
from rsl_rl.runners import OnPolicyRunner
self._runner = OnPolicyRunner(
env, class_to_dict(self.agent_cfg), log_dir=log_dir, device=str(env.unwrapped.device)
)
self._runner.learn(
num_learning_iterations=self.agent_cfg.max_iterations,
init_at_random_ep_len=True,
)
def load(self, checkpoint: str) -> None:
if self._runner is None:
raise RuntimeError("call train() or construct a runner before load()")
self._runner.load(checkpoint)
def act(self, obs: Any) -> Any:
if self._runner is None:
raise RuntimeError("no runner")
return self._runner.alg.act(obs)