-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmujoco_reinforce_hl.py
79 lines (69 loc) · 2.28 KB
/
mujoco_reinforce_hl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import os
from collections.abc import Sequence
from typing import Literal
import torch
from sensai.util import logging
from sensai.util.logging import datetime_tag
from examples.mujoco.mujoco_env import MujocoEnvFactory
from tianshou.highlevel.config import SamplingConfig
from tianshou.highlevel.experiment import (
ExperimentConfig,
PGExperimentBuilder,
)
from tianshou.highlevel.params.lr_scheduler import LRSchedulerFactoryLinear
from tianshou.highlevel.params.policy_params import PGParams
def main(
experiment_config: ExperimentConfig,
task: str = "Ant-v4",
buffer_size: int = 4096,
hidden_sizes: Sequence[int] = (64, 64),
lr: float = 1e-3,
gamma: float = 0.99,
epoch: int = 100,
step_per_epoch: int = 30000,
step_per_collect: int = 2048,
repeat_per_collect: int = 1,
batch_size: int | None = None,
training_num: int = 10,
test_num: int = 10,
rew_norm: bool = True,
action_bound_method: Literal["clip", "tanh"] = "tanh",
lr_decay: bool = True,
) -> None:
log_name = os.path.join(task, "reinforce", str(experiment_config.seed), datetime_tag())
sampling_config = SamplingConfig(
num_epochs=epoch,
step_per_epoch=step_per_epoch,
batch_size=batch_size,
num_train_envs=training_num,
num_test_envs=test_num,
buffer_size=buffer_size,
step_per_collect=step_per_collect,
repeat_per_collect=repeat_per_collect,
)
env_factory = MujocoEnvFactory(
task,
train_seed=sampling_config.train_seed,
test_seed=sampling_config.test_seed,
obs_norm=True,
)
experiment = (
PGExperimentBuilder(env_factory, experiment_config, sampling_config)
.with_pg_params(
PGParams(
discount_factor=gamma,
action_bound_method=action_bound_method,
reward_normalization=rew_norm,
lr=lr,
lr_scheduler_factory=LRSchedulerFactoryLinear(sampling_config)
if lr_decay
else None,
),
)
.with_actor_factory_default(hidden_sizes, torch.nn.Tanh, continuous_unbounded=True)
.build()
)
experiment.run(run_name=log_name)
if __name__ == "__main__":
logging.run_cli(main)