-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.py
175 lines (157 loc) · 7.06 KB
/
main.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
"""
Main scripts to start experiments.
Takes a flag --env-type (see below for choices) and loads the parameters from the respective config file.
"""
import argparse
import warnings
import numpy as np
import torch
# get configs
from config.gridworld import \
args_grid_belief_oracle, args_grid_rl2, args_grid_varibad
from config.pointrobot import \
args_pointrobot_multitask, args_pointrobot_varibad, args_pointrobot_rl2, args_pointrobot_humplik
from config.mujoco import \
args_cheetah_dir_multitask, args_cheetah_dir_expert, args_cheetah_dir_rl2, args_cheetah_dir_varibad, \
args_cheetah_vel_multitask, args_cheetah_vel_expert, args_cheetah_vel_rl2, args_cheetah_vel_varibad, \
args_cheetah_vel_avg, \
args_ant_dir_multitask, args_ant_dir_expert, args_ant_dir_rl2, args_ant_dir_varibad, \
args_ant_goal_multitask, args_ant_goal_expert, args_ant_goal_rl2, args_ant_goal_varibad, \
args_ant_goal_humplik, \
args_walker_multitask, args_walker_expert, args_walker_avg, args_walker_rl2, args_walker_varibad, \
args_humanoid_dir_varibad, args_humanoid_dir_rl2, args_humanoid_dir_multitask, args_humanoid_dir_expert
from environments.parallel_envs import make_vec_envs
from learner import Learner
from metalearner import MetaLearner
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--env-type', default='gridworld_varibad')
args, rest_args = parser.parse_known_args()
env = args.env_type
# --- GridWorld ---
if env == 'gridworld_belief_oracle':
args = args_grid_belief_oracle.get_args(rest_args)
elif env == 'gridworld_varibad':
args = args_grid_varibad.get_args(rest_args)
elif env == 'gridworld_rl2':
args = args_grid_rl2.get_args(rest_args)
# --- PointRobot 2D Navigation ---
elif env == 'pointrobot_multitask':
args = args_pointrobot_multitask.get_args(rest_args)
elif env == 'pointrobot_varibad':
args = args_pointrobot_varibad.get_args(rest_args)
elif env == 'pointrobot_rl2':
args = args_pointrobot_rl2.get_args(rest_args)
elif env == 'pointrobot_humplik':
args = args_pointrobot_humplik.get_args(rest_args)
# --- MUJOCO ---
# - CheetahDir -
elif env == 'cheetah_dir_multitask':
args = args_cheetah_dir_multitask.get_args(rest_args)
elif env == 'cheetah_dir_expert':
args = args_cheetah_dir_expert.get_args(rest_args)
elif env == 'cheetah_dir_varibad':
args = args_cheetah_dir_varibad.get_args(rest_args)
elif env == 'cheetah_dir_rl2':
args = args_cheetah_dir_rl2.get_args(rest_args)
#
# - CheetahVel -
elif env == 'cheetah_vel_multitask':
args = args_cheetah_vel_multitask.get_args(rest_args)
elif env == 'cheetah_vel_expert':
args = args_cheetah_vel_expert.get_args(rest_args)
elif env == 'cheetah_vel_avg':
args = args_cheetah_vel_avg.get_args(rest_args)
elif env == 'cheetah_vel_varibad':
args = args_cheetah_vel_varibad.get_args(rest_args)
elif env == 'cheetah_vel_rl2':
args = args_cheetah_vel_rl2.get_args(rest_args)
#
# - AntDir -
elif env == 'ant_dir_multitask':
args = args_ant_dir_multitask.get_args(rest_args)
elif env == 'ant_dir_expert':
args = args_ant_dir_expert.get_args(rest_args)
elif env == 'ant_dir_varibad':
args = args_ant_dir_varibad.get_args(rest_args)
elif env == 'ant_dir_rl2':
args = args_ant_dir_rl2.get_args(rest_args)
#
# - AntGoal -
elif env == 'ant_goal_multitask':
args = args_ant_goal_multitask.get_args(rest_args)
elif env == 'ant_goal_expert':
args = args_ant_goal_expert.get_args(rest_args)
elif env == 'ant_goal_varibad':
args = args_ant_goal_varibad.get_args(rest_args)
elif env == 'ant_goal_humplik':
args = args_ant_goal_humplik.get_args(rest_args)
elif env == 'ant_goal_rl2':
args = args_ant_goal_rl2.get_args(rest_args)
#
# - Walker -
elif env == 'walker_multitask':
args = args_walker_multitask.get_args(rest_args)
elif env == 'walker_expert':
args = args_walker_expert.get_args(rest_args)
elif env == 'walker_avg':
args = args_walker_avg.get_args(rest_args)
elif env == 'walker_varibad':
args = args_walker_varibad.get_args(rest_args)
elif env == 'walker_rl2':
args = args_walker_rl2.get_args(rest_args)
#
# - HumanoidDir -
elif env == 'humanoid_dir_multitask':
args = args_humanoid_dir_multitask.get_args(rest_args)
elif env == 'humanoid_dir_expert':
args = args_humanoid_dir_expert.get_args(rest_args)
elif env == 'humanoid_dir_varibad':
args = args_humanoid_dir_varibad.get_args(rest_args)
elif env == 'humanoid_dir_rl2':
args = args_humanoid_dir_rl2.get_args(rest_args)
else:
raise Exception("Invalid Environment")
# warning for deterministic execution
if args.deterministic_execution:
print('Envoking deterministic code execution.')
if torch.backends.cudnn.enabled:
warnings.warn('Running with deterministic CUDNN.')
if args.num_processes > 1:
raise RuntimeError('If you want fully deterministic code, run it with num_processes=1.'
'Warning: This will slow things down and might break A2C if '
'policy_num_steps < env._max_episode_steps.')
# if we're normalising the actions, we have to make sure that the env expects actions within [-1, 1]
if args.norm_actions_pre_sampling or args.norm_actions_post_sampling:
envs = make_vec_envs(env_name=args.env_name, seed=0, num_processes=args.num_processes,
gamma=args.policy_gamma, device='cpu',
episodes_per_task=args.max_rollouts_per_task,
normalise_rew=args.norm_rew_for_policy, ret_rms=None,
tasks=None,
)
assert np.unique(envs.action_space.low) == [-1]
assert np.unique(envs.action_space.high) == [1]
# clean up arguments
if args.disable_metalearner or args.disable_decoder:
args.decode_reward = False
args.decode_state = False
args.decode_task = False
if hasattr(args, 'decode_only_past') and args.decode_only_past:
args.split_batches_by_elbo = True
# if hasattr(args, 'vae_subsample_decodes') and args.vae_subsample_decodes:
# args.split_batches_by_elbo = True
# begin training (loop through all passed seeds)
seed_list = [args.seed] if isinstance(args.seed, int) else args.seed
for seed in seed_list:
print('training', seed)
args.seed = seed
args.action_space = None
if args.disable_metalearner:
# If `disable_metalearner` is true, the file `learner.py` will be used instead of `metalearner.py`.
# This is a stripped down version without encoder, decoder, stochastic latent variables, etc.
learner = Learner(args)
else:
learner = MetaLearner(args)
learner.train()
if __name__ == '__main__':
main()