-
Notifications
You must be signed in to change notification settings - Fork 0
/
baselines.py
187 lines (144 loc) · 6.58 KB
/
baselines.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 22 15:04:19 2023
@author: kh-ryu
"""
from garage.sampler import RaySampler
from garage.torch.algos import PPO, TRPO
from garage.torch.policies import GaussianMLPPolicy
from policy.DiscretePolicy import DiscreteMLPPolicy
from garage.torch.value_functions import GaussianMLPValueFunction
import torch
from garage import wrap_experiment
from garage.envs import GymEnv
from garage.experiment.deterministic import set_seed
from garage.trainer import Trainer
@wrap_experiment(log_dir = './experiments/MountainCar/PPO_baseline', archive_launch_repo=False)
def PPO_MountainCar(ctxt=None, seed=1):
"""Train PPO with sparse reward on SparseMountainCar-v0.
Args:
ctxt (garage.experiment.ExperimentContext): The experiment
configuration used by Trainer to create the snapshotter.
seed (int): Used to seed the random number generator to produce
determinism.
"""
set_seed(seed)
env = GymEnv('Sparse_MountainCar-v0')
trainer = Trainer(ctxt)
policy = GaussianMLPPolicy(env.spec,
hidden_sizes=[128, 128],
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
value_function = GaussianMLPValueFunction(env_spec=env.spec,
hidden_sizes=(128, 128),
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
sampler = RaySampler(agents=policy,
envs=env,
max_episode_length=500)
algo = PPO(env_spec = env.spec,
policy=policy,
value_function=value_function,
discount=0.99,
sampler = sampler)
trainer.setup( algo = algo, env = env)
trainer.train(n_epochs = 200, batch_size = 1000)
@wrap_experiment(log_dir = './experiments/MountainCar/TRPO_baseline', archive_launch_repo=False)
def TRPO_MountainCar(ctxt=None, seed=1):
"""Train TRPO with sparse reward on SparseMountainCar-v0.
Args:
ctxt (garage.experiment.ExperimentContext): The experiment
configuration used by Trainer to create the snapshotter.
seed (int): Used to seed the random number generator to produce
determinism.
"""
set_seed(seed)
env = GymEnv('Sparse_MountainCar-v0')
trainer = Trainer(ctxt)
policy = GaussianMLPPolicy(env.spec,
hidden_sizes=[128, 128],
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
value_function = GaussianMLPValueFunction(env_spec=env.spec,
hidden_sizes=(128, 128),
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
sampler = RaySampler(agents=policy,
envs=env,
max_episode_length=500)
algo = TRPO(env_spec = env.spec,
policy=policy,
value_function=value_function,
discount=0.99,
sampler = sampler,
center_adv=False)
trainer.setup( algo = algo, env = env)
trainer.train(n_epochs = 200, batch_size = 1000)
@wrap_experiment(log_dir = './experiments/CartpoleSwingup/PPO_baseline', archive_launch_repo=False)
def PPO_CartPoleSwingUP(ctxt=None, seed=1):
"""Train PPO with sparse reward on SCartpoleSwingup-v1.
Args:
ctxt (garage.experiment.ExperimentContext): The experiment
configuration used by Trainer to create the snapshotter.
seed (int): Used to seed the random number generator to produce
determinism.
"""
set_seed(seed)
env = GymEnv('CartPoleSwingUp-v1')
trainer = Trainer(ctxt)
policy = DiscreteMLPPolicy(env.spec,
hidden_sizes=[128, 128],
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
value_function = GaussianMLPValueFunction(env_spec=env.spec,
hidden_sizes=(128, 128),
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
sampler = RaySampler(agents=policy,
envs=env,
max_episode_length=500)
algo = PPO(env_spec = env.spec,
policy=policy,
value_function=value_function,
discount=0.95,
sampler = sampler)
trainer.setup( algo = algo, env = env)
trainer.train(n_epochs = 500, batch_size = 1000)
@wrap_experiment(log_dir = './experiments/CartpoleSwingup/TRPO_baseline', archive_launch_repo=False)
def TRPO_CartPoleSwingUp(ctxt=None, seed=1):
"""Train TRPO with sparse reward on CartpoleSwingup-v1.
Args:
ctxt (garage.experiment.ExperimentContext): The experiment
configuration used by Trainer to create the snapshotter.
seed (int): Used to seed the random number generator to produce
determinism.
"""
set_seed(seed)
env = GymEnv('CartPoleSwingUp-v1')
trainer = Trainer(ctxt)
policy = DiscreteMLPPolicy(env.spec,
hidden_sizes=[128, 128],
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
value_function = GaussianMLPValueFunction(env_spec=env.spec,
hidden_sizes=(128, 128),
hidden_nonlinearity=torch.tanh,
output_nonlinearity=None)
sampler = RaySampler(agents=policy,
envs=env,
max_episode_length=500)
algo = TRPO(env_spec = env.spec,
policy=policy,
value_function=value_function,
discount=0.95,
sampler = sampler,
center_adv=False)
trainer.setup( algo = algo, env = env)
trainer.train(n_epochs = 500, batch_size = 1000)
# PPO_CartPoleSwingUP(seed=1)
# TRPO_CartPoleSwingUp(seed=1)
# PPO_CartPoleSwingUP(seed=2)
# TRPO_CartPoleSwingUp(seed=2)
# PPO_CartPoleSwingUP(seed=3)
# TRPO_CartPoleSwingUp(seed=3)