-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmujoco_all_sql.py
208 lines (175 loc) · 5.73 KB
/
mujoco_all_sql.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
import argparse
from rllab.envs.normalized_env import normalize
from rllab.envs.mujoco.swimmer_env import SwimmerEnv
from rllab.envs.mujoco.humanoid_env import HumanoidEnv
from rllab.misc.instrument import VariantGenerator
from softqlearning.misc.instrument import run_sql_experiment
from softqlearning.misc.utils import timestamp
from softqlearning.environments import GymEnv
""" Example script to perform soft Q-learning in the multigoal environment. """
from softqlearning.algorithms import SQL
from softqlearning.misc.kernel import adaptive_isotropic_gaussian_kernel
from softqlearning.replay_buffers import SimpleReplayBuffer
from softqlearning.value_functions import NNQFunction, DFunction,VFunction
from softqlearning.policies import StochasticNNPolicy
import numpy as np
import os, sys
import shutil
from distutils.dir_util import copy_tree
from time import gmtime, strftime
#os.environ['CUDA_VISIBLE_DEVICES']='0'
SHARED_PARAMS = {
'seed': [1,2,3],
'policy_lr': 3E-4,
'qf_lr': 3E-4,
'discount': 0.99,
'layer_size': 128,
'batch_size': 128,
'max_pool_size': 1E6,
'n_train_repeat': 1,
'epoch_length': 1000,
'dist':'beta',
'snapshot_mode': 'last',
'snapshot_gap': 2000,
}
ENV_PARAMS = {
'swimmer': { # 2 DoF
'prefix': 'swimmer',
'env_name': 'swimmer-rllab',
'max_path_length': 1000,
'n_epochs': 500,
'reward_scale': 100,
},
'hopper': { # 3 DoF
'prefix': 'hopper',
'env_name': 'Hopper-v1',
'max_path_length': 1000,
'n_epochs': 3000,
'reward_scale': 1,
},
'half-cheetah': { # 6 DoF
'prefix': 'half-cheetah',
'env_name': 'HalfCheetah-v1',
'max_path_length': 1000,
'n_epochs': 10000,
'reward_scale': 1,
'max_pool_size': 1E7,
},
'walker': { # 6 DoF
'prefix': 'walker',
'env_name': 'Walker2d-v1',
'max_path_length': 1000,
'n_epochs': 5000,
'reward_scale': 3,
},
'ant': { # 8 DoF
'prefix': 'ant',
'env_name': 'Ant-v1',
'max_path_length': 1000,
'n_epochs': 10000,
'reward_scale': 10,
},
'humanoid': { # 21 DoF
'prefix': 'humanoid',
'env_name': 'humanoid-rllab',
'max_path_length': 1000,
'n_epochs': 20000,
'reward_scale': 100,
},
}
DEFAULT_ENV = 'swimmer'
AVAILABLE_ENVS = list(ENV_PARAMS.keys())
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--env', type=str, choices=AVAILABLE_ENVS, 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 get_variants(args):
env_params = ENV_PARAMS[args.env]
params = SHARED_PARAMS
params.update(env_params)
vg = VariantGenerator()
for key, val in params.items():
if isinstance(val, list):
vg.add(key, val)
else:
vg.add(key, [val])
return vg
def run_experiment(variant):
if variant['env_name'] == 'humanoid-rllab':
env = normalize(HumanoidEnv())
elif variant['env_name'] == 'swimmer-rllab':
env = normalize(SwimmerEnv())
else:
env = normalize(GymEnv(variant['env_name']))
pool = SimpleReplayBuffer(
env_spec=env.spec,
max_replay_buffer_size=variant['max_pool_size'],
)
base_kwargs = dict(
min_pool_size=variant['max_path_length'],
epoch_length=variant['epoch_length'],
n_epochs=variant['n_epochs'],
max_path_length=variant['max_path_length'],
batch_size=variant['batch_size'],
n_train_repeat=variant['n_train_repeat'],
eval_render=False,
eval_n_episodes=1,
)
M = variant['layer_size']
qf = NNQFunction(env_spec=env.spec,hidden_layer_sizes=(M, M),)
df = DFunction(env_spec=env.spec, hidden_layer_sizes=[M, M]) # discriminator, input is the actions.
vf = VFunction(env_spec=env.spec, hidden_layer_sizes=[M, M])
policy = StochasticNNPolicy(env_spec=env.spec, hidden_layer_sizes=( M, M))
algorithm = SQL(
base_kwargs=base_kwargs,
env=env,
pool=pool,
qf=qf,
policy=policy,
kernel_fn=adaptive_isotropic_gaussian_kernel,
kernel_n_particles=16,
kernel_update_ratio=0.5,
value_n_particles=16,
td_target_update_interval=1000,
qf_lr=variant['qf_lr'],
policy_lr=variant['policy_lr'],
discount=variant['discount'],
reward_scale=variant['reward_scale'],
save_full_state=False,
df=df,
vf=vf,
df_lr=1e-3,
dist=variant['dist'],
)
algorithm.train()
def launch_experiments(variant_generator, args):
variants = variant_generator.variants()
for i, variant in enumerate(variants):
print('Launching {} experiments.'.format(len(variants)))
full_experiment_name = variant['prefix']
full_experiment_name += '-' + args.exp_name + '-' + str(i).zfill(2)
run_sql_experiment(
run_experiment,
mode=args.mode,
variant=variant,
exp_prefix=variant['prefix'] + '/' + args.exp_name,
exp_name=full_experiment_name,
n_parallel=1,
seed=variant['seed'],
terminate_machine=True,
log_dir=args.log_dir,
snapshot_mode=variant['snapshot_mode'],
snapshot_gap=variant['snapshot_gap'],
sync_s3_pkl=True,
)
def main():
args = parse_args()
variant_generator = get_variants(args)
launch_experiments(variant_generator, args)
if __name__ == '__main__':
main()