Tianshou/examples/mujoco/mujoco_sac_hl.py

88 lines
2.5 KiB
Python
Raw Normal View History

2023-09-20 09:29:34 +02:00
#!/usr/bin/env python3
import os
from collections.abc import Sequence
from examples.mujoco.mujoco_env import MujocoEnvFactory
2023-10-06 13:50:23 +02:00
from tianshou.highlevel.config import SamplingConfig
2023-09-20 13:15:06 +02:00
from tianshou.highlevel.experiment import (
2023-10-06 13:50:23 +02:00
ExperimentConfig,
SACExperimentBuilder,
2023-09-20 09:29:34 +02:00
)
from tianshou.highlevel.params.alpha import AutoAlphaFactoryDefault
from tianshou.highlevel.params.policy_params import SACParams
from tianshou.utils import logging
from tianshou.utils.logging import datetime_tag
2023-09-20 09:29:34 +02:00
def main(
2023-10-06 13:50:23 +02:00
experiment_config: ExperimentConfig,
Feature/dataclasses (#996) This PR adds strict typing to the output of `update` and `learn` in all policies. This will likely be the last large refactoring PR before the next release (0.6.0, not 1.0.0), so it requires some attention. Several difficulties were encountered on the path to that goal: 1. The policy hierarchy is actually "broken" in the sense that the keys of dicts that were output by `learn` did not follow the same enhancement (inheritance) pattern as the policies. This is a real problem and should be addressed in the near future. Generally, several aspects of the policy design and hierarchy might deserve a dedicated discussion. 2. Each policy needs to be generic in the stats return type, because one might want to extend it at some point and then also extend the stats. Even within the source code base this pattern is necessary in many places. 3. The interaction between learn and update is a bit quirky, we currently handle it by having update modify special field inside TrainingStats, whereas all other fields are handled by learn. 4. The IQM module is a policy wrapper and required a TrainingStatsWrapper. The latter relies on a bunch of black magic. They were addressed by: 1. Live with the broken hierarchy, which is now made visible by bounds in generics. We use type: ignore where appropriate. 2. Make all policies generic with bounds following the policy inheritance hierarchy (which is incorrect, see above). We experimented a bit with nested TrainingStats classes, but that seemed to add more complexity and be harder to understand. Unfortunately, mypy thinks that the code below is wrong, wherefore we have to add `type: ignore` to the return of each `learn` ```python T = TypeVar("T", bound=int) def f() -> T: return 3 ``` 3. See above 4. Write representative tests for the `TrainingStatsWrapper`. Still, the black magic might cause nasty surprises down the line (I am not proud of it)... Closes #933 --------- Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de> Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-12-30 11:09:03 +01:00
task: str = "Ant-v4",
buffer_size: int = 1000000,
2023-09-20 09:29:34 +02:00
hidden_sizes: Sequence[int] = (256, 256),
actor_lr: float = 1e-3,
critic_lr: float = 1e-3,
gamma: float = 0.99,
tau: float = 0.005,
alpha: float = 0.2,
auto_alpha: bool = False,
alpha_lr: float = 3e-4,
start_timesteps: int = 10000,
epoch: int = 200,
step_per_epoch: int = 5000,
step_per_collect: int = 1,
update_per_step: int = 1,
n_step: int = 1,
batch_size: int = 256,
training_num: int = 1,
test_num: int = 10,
) -> None:
log_name = os.path.join(task, "sac", str(experiment_config.seed), datetime_tag())
2023-09-20 09:29:34 +02:00
2023-10-06 13:50:23 +02:00
sampling_config = SamplingConfig(
num_epochs=epoch,
step_per_epoch=step_per_epoch,
num_train_envs=training_num,
num_test_envs=test_num,
buffer_size=buffer_size,
batch_size=batch_size,
step_per_collect=step_per_collect,
update_per_step=update_per_step,
start_timesteps=start_timesteps,
start_timesteps_random=True,
)
env_factory = MujocoEnvFactory(
task,
train_seed=sampling_config.train_seed,
test_seed=sampling_config.test_seed,
obs_norm=False,
)
2023-09-20 09:29:34 +02:00
experiment = (
SACExperimentBuilder(env_factory, experiment_config, sampling_config)
.with_sac_params(
SACParams(
tau=tau,
gamma=gamma,
alpha=AutoAlphaFactoryDefault(lr=alpha_lr) if auto_alpha else alpha,
estimation_step=n_step,
actor_lr=actor_lr,
critic1_lr=critic_lr,
critic2_lr=critic_lr,
),
)
.with_actor_factory_default(
hidden_sizes,
continuous_unbounded=True,
continuous_conditioned_sigma=True,
)
.with_common_critic_factory_default(hidden_sizes)
.build()
2023-09-20 09:29:34 +02:00
)
experiment.run(run_name=log_name)
2023-09-20 09:29:34 +02:00
if __name__ == "__main__":
logging.run_cli(main)