forked from haarnoja/sac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmujoco_all_sac.py
241 lines (205 loc) · 7.99 KB
/
mujoco_all_sac.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import argparse
import os
import tensorflow as tf
import numpy as np
from rllab.envs.normalized_env import normalize
from rllab.envs.mujoco.gather.ant_gather_env import AntGatherEnv
from rllab.envs.mujoco.swimmer_env import SwimmerEnv
# from rllab.envs.mujoco.ant_env import AntEnv
from rllab.envs.mujoco.humanoid_env import HumanoidEnv
from rllab.misc.instrument import VariantGenerator
from rllab import config
from sac.algos import SAC
from sac.envs import (
GymEnv,
MultiDirectionSwimmerEnv,
MultiDirectionAntEnv,
MultiDirectionHumanoidEnv,
CrossMazeAntEnv,
)
from sac.misc.instrument import run_sac_experiment
from sac.misc.utils import timestamp, unflatten
from sac.policies import GaussianPolicy, LatentSpacePolicy, GMMPolicy, UniformPolicy
from sac.misc.sampler import SimpleSampler
from sac.replay_buffers import SimpleReplayBuffer
from sac.value_functions import NNQFunction, NNVFunction
from sac.preprocessors import MLPPreprocessor
from examples.variants import parse_domain_and_task, get_variants
ENVIRONMENTS = {
'swimmer-gym': {
'default': lambda: GymEnv('Swimmer-v1'),
},
'swimmer-rllab': {
'default': SwimmerEnv,
'multi-direction': MultiDirectionSwimmerEnv,
},
'ant': {
'default': lambda: GymEnv('Ant-v1'),
'multi-direction': MultiDirectionAntEnv,
'cross-maze': CrossMazeAntEnv
},
'humanoid-gym': {
'default': lambda: GymEnv('Humanoid-v1')
},
'humanoid-rllab': {
'default': HumanoidEnv,
'multi-direction': MultiDirectionHumanoidEnv,
},
'hopper': {
'default': lambda: GymEnv('Hopper-v1')
},
'half-cheetah': {
'default': lambda: GymEnv('HalfCheetah-v1')
},
'walker': {
'default': lambda: GymEnv('Walker2d-v1')
},
'humanoid-standup-gym': {
'default': lambda: GymEnv('HumanoidStandup-v1')
}
}
DEFAULT_DOMAIN = DEFAULT_ENV = 'swimmer-rllab'
AVAILABLE_DOMAINS = set(ENVIRONMENTS.keys())
AVAILABLE_TASKS = set(y for x in ENVIRONMENTS.values() for y in x.keys())
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--domain',
type=str,
choices=AVAILABLE_DOMAINS,
default=None)
parser.add_argument('--task',
type=str,
choices=AVAILABLE_TASKS,
default='default')
parser.add_argument('--policy',
type=str,
choices=('gaussian', 'gmm', 'lsp'),
default='gaussian')
parser.add_argument('--env', type=str, default=DEFAULT_ENV)
parser.add_argument('--exp_name', type=str, default=timestamp())
parser.add_argument('--mode', type=str, default='local')
parser.add_argument('--log_dir', type=str, default=None)
args = parser.parse_args()
return args
def run_experiment(variant):
env_params = variant['env_params']
policy_params = variant['policy_params']
value_fn_params = variant['value_fn_params']
algorithm_params = variant['algorithm_params']
replay_buffer_params = variant['replay_buffer_params']
sampler_params = variant['sampler_params']
task = variant['task']
domain = variant['domain']
env = normalize(ENVIRONMENTS[domain][task](**env_params))
pool = SimpleReplayBuffer(env_spec=env.spec, **replay_buffer_params)
sampler = SimpleSampler(**sampler_params)
base_kwargs = dict(algorithm_params['base_kwargs'], sampler=sampler)
M = value_fn_params['layer_size']
qf1 = NNQFunction(env_spec=env.spec, hidden_layer_sizes=(M, M), name='qf1')
qf2 = NNQFunction(env_spec=env.spec, hidden_layer_sizes=(M, M), name='qf2')
vf = NNVFunction(env_spec=env.spec, hidden_layer_sizes=(M, M))
initial_exploration_policy = UniformPolicy(env_spec=env.spec)
if policy_params['type'] == 'gaussian':
policy = GaussianPolicy(
env_spec=env.spec,
hidden_layer_sizes=(M,M),
reparameterize=policy_params['reparameterize'],
reg=1e-3,
)
elif policy_params['type'] == 'lsp':
nonlinearity = {
None: None,
'relu': tf.nn.relu,
'tanh': tf.nn.tanh
}[policy_params['preprocessing_output_nonlinearity']]
preprocessing_hidden_sizes = policy_params.get('preprocessing_hidden_sizes')
if preprocessing_hidden_sizes is not None:
observations_preprocessor = MLPPreprocessor(
env_spec=env.spec,
layer_sizes=preprocessing_hidden_sizes,
output_nonlinearity=nonlinearity)
else:
observations_preprocessor = None
policy_s_t_layers = policy_params['s_t_layers']
policy_s_t_units = policy_params['s_t_units']
s_t_hidden_sizes = [policy_s_t_units] * policy_s_t_layers
bijector_config = {
'num_coupling_layers': policy_params['coupling_layers'],
'translation_hidden_sizes': s_t_hidden_sizes,
'scale_hidden_sizes': s_t_hidden_sizes,
}
policy = LatentSpacePolicy(
env_spec=env.spec,
squash=policy_params['squash'],
bijector_config=bijector_config,
reparameterize=policy_params['reparameterize'],
q_function=qf1,
observations_preprocessor=observations_preprocessor)
elif policy_params['type'] == 'gmm':
# reparameterize should always be False if using a GMMPolicy
policy = GMMPolicy(
env_spec=env.spec,
K=policy_params['K'],
hidden_layer_sizes=(M, M),
reparameterize=policy_params['reparameterize'],
qf=qf1,
reg=1e-3,
)
else:
raise NotImplementedError(policy_params['type'])
algorithm = SAC(
base_kwargs=base_kwargs,
env=env,
policy=policy,
initial_exploration_policy=initial_exploration_policy,
pool=pool,
qf1=qf1,
qf2=qf2,
vf=vf,
lr=algorithm_params['lr'],
scale_reward=algorithm_params['scale_reward'],
discount=algorithm_params['discount'],
tau=algorithm_params['tau'],
reparameterize=algorithm_params['reparameterize'],
target_update_interval=algorithm_params['target_update_interval'],
action_prior=policy_params['action_prior'],
save_full_state=False,
)
algorithm._sess.run(tf.global_variables_initializer())
algorithm.train()
def launch_experiments(variant_generator, args):
variants = variant_generator.variants()
# TODO: Remove unflatten. Our variant generator should support nested params
variants = [unflatten(variant, separator='.') for variant in variants]
num_experiments = len(variants)
print('Launching {} experiments.'.format(num_experiments))
for i, variant in enumerate(variants):
print("Experiment: {}/{}".format(i, num_experiments))
run_params = variant['run_params']
algo_params = variant['algorithm_params']
experiment_prefix = variant['prefix'] + '/' + args.exp_name
experiment_name = '{prefix}-{exp_name}-{i:02}'.format(
prefix=variant['prefix'], exp_name=args.exp_name, i=i)
run_sac_experiment(
run_experiment,
mode=args.mode,
variant=variant,
exp_prefix=experiment_prefix,
exp_name=experiment_name,
n_parallel=1,
seed=run_params['seed'],
terminate_machine=True,
log_dir=args.log_dir,
snapshot_mode=run_params['snapshot_mode'],
snapshot_gap=run_params['snapshot_gap'],
sync_s3_pkl=run_params['sync_pkl'],
)
def main():
args = parse_args()
domain, task = args.domain, args.task
if (not domain) or (not task):
domain, task = parse_domain_and_task(args.env)
variant_generator = get_variants(domain=domain, task=task, policy=args.policy)
launch_experiments(variant_generator, args)
if __name__ == '__main__':
main()