-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
160 lines (152 loc) · 8.8 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
import argparse
import os
import gym
import numpy as np
import tensorflow as tf
import time
from algorithms.DDPG import DDPG
from algorithms.QRDDPG import QRDDPG
from algorithms.D3PG import D3PG
from algorithms.QRDQN import QRDQN
from algorithms.QRA2C import QRA2C
from algorithms.A2C import A2C
from algorithms.PPO import PPO
from algorithms.QRPPO import QRPPO
from algorithms.SQRPPO import SQRPPO
from algorithms.common import Replay_Memory
from customized_mujoco import PartiallyObservableEnv
from utils import plot, append_summary
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--env', default='HandReach-v0', type=str,
help='[FetchReach-v1, FetchSlide-v1, FetchPush-v1, FetchPickAndPlace-v1,'
'HandReach-v0, HandManipulateBlock-v0, HandManipulateEgg-v0, HandManipulatePen-v0]')
parser.add_argument('--model', default='QRDDPG', type=str, help='[DDPG, D3PG, QRDDPG]')
parser.add_argument('--eval', default=False, action='store_true',
help='Set this to False when training and True when evaluating.')
parser.add_argument('--restore', default=False, action='store_true', help='Restore training')
parser.add_argument('--reward-type', default='sparse', help='[sparse, dense]')
parser.add_argument('--hidden-dims', default=[64, 64], type=int, nargs='+', help='Hidden dimension of network')
parser.add_argument('--gamma', default=0.99, type=float, help='Reward discount')
parser.add_argument('--entropy-scale', default=0.2, type=float, help='Reward discount')
parser.add_argument('--lambd', default=0.95, type=float, help='discount for gae')
parser.add_argument('--tau', default=1, type=float, help='Soft parameter update tau')
parser.add_argument('--kappa', default=1, type=float, help='Kappa used in quantile Huber loss')
parser.add_argument('--n-quantile', default=100, type=int, help='Number of quantile to approximate distribution')
parser.add_argument('--actor-lr', default=3e-4, type=float, help='Actor learning rate')
parser.add_argument('--critic-lr', default=3e-4, type=float, help='Critic learning rate')
parser.add_argument('--quantile', default=0.5, type=float, help='Quantile for SQRPPO')
parser.add_argument('--n-atom', default=51, type=int, help='Number of atoms used in D3PG')
parser.add_argument('--batch-size', default=64, type=int)
parser.add_argument('--horrizon', default=2048, type=int)
parser.add_argument('--step', default=10, type=int, help='Number of gradient descent steps per episode')
parser.add_argument('--epsilon', default=0.2, type=float, help='Exploration noise, fixed in D4PG')
parser.add_argument('--train-episodes', default=2000, type=int, help='Number of episodes to train')
parser.add_argument('--train-steps', default=-1, type=int, help='Number of episodes to train')
parser.add_argument('--save-episodes', default=100, type=int, help='Number of episodes to save model')
parser.add_argument('--memory-size', default=1000000, type=int, help='Size of replay memory')
parser.add_argument('--apply-her', default=False, action='store_true', help='Use HER or not')
parser.add_argument('--n-goals', default=10, type=int, help='Number of goals to sample for HER')
parser.add_argument('--C', default=1, type=int, help='Number of episodes to copy critic network to target network')
parser.add_argument('--N', default=1, type=int, help='N step returns.')
parser.add_argument('--plot-dir', default='plot', type=str, )
parser.add_argument('--model-dir', default='model', type=str)
parser.add_argument('--log-dir', default='log', type=str)
parser.add_argument('--progress-file', default='progress.csv', type=str)
parser.add_argument('--device', default=-1, type=int, help='GPU device number')
return parser.parse_args()
if __name__ == '__main__':
args = parse_arguments()
if not os.path.exists(args.plot_dir):
os.makedirs(args.plot_dir)
if not os.path.exists(args.model_dir):
os.makedirs(args.model_dir)
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
model_path = os.path.join(os.path.join(args.model_dir, args.model + '_' + args.env), 'model.ckpt')
log_path = os.path.join(args.log_dir, args.model + '_' + args.env)
progress_file = os.path.join(log_path, args.progress_file)
# Setting the session to allow growth, so it doesn't allocate all GPU memory.
is_env_pool = False
if args.device >= 0:
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.device)
device = '/gpu:0'
else:
device = '/cpu:0'
if args.env in ['FetchReach-v1', 'FetchSlide-v1', 'FetchPush-v1', 'FetchPickAndPlace-v1',
'HandReach-v0', 'HandManipulateBlock-v0', 'HandManipulateEgg-v0', 'HandManipulatePen-v0']:
environment = gym.make(args.env, reward_type=args.reward_type)
elif args.env in ['PartiallyObservableHalfCheetah','PartiallyObservableAnt', 'PartiallyObservableWalker2d']:
environment = PartiallyObservableEnv(env_name=args.env, file_buffer_name="{}_{}.xml".format(args.env, int(time.time()))).make()
else:
environment = gym.make(args.env)
is_env_pool = False
tf.reset_default_graph()
with tf.device(device):
if args.eval:
replay_memory = None
else:
replay_memory = Replay_Memory(memory_size=args.memory_size)
if args.model == 'DDPG':
agent = DDPG(environment, args.hidden_dims, replay_memory=replay_memory, gamma=args.gamma,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N)
elif args.model == 'QRDDPG':
agent = QRDDPG(environment, args.hidden_dims, replay_memory=replay_memory, gamma=args.gamma,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, kappa=args.kappa,
n_quantile=args.n_quantile)
elif args.model == 'D3PG':
# Need a better way for setting v_min and v_max
agent = D3PG(environment, args.hidden_dims, replay_memory=replay_memory, gamma=args.gamma,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, n_atom = args.n_atom,
v_min=-100, v_max=100)
elif args.model == 'QRDQN':
agent = QRDQN(environment, args.hidden_dims, replay_memory=replay_memory, gamma=args.gamma,
lr=args.actor_lr, tau=args.tau, N=args.N, kappa=args.kappa,n_quantile=args.n_quantile)
elif args.model == 'QRA2C':
agent = QRA2C(environment, args.hidden_dims, gamma=args.gamma,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, kappa=args.kappa,
n_quantile=args.n_quantile)
elif args.model == 'A2C':
agent = A2C(environment, args.hidden_dims, gamma=args.gamma,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, is_env_pool=args.is_env_pool)
elif args.model == 'PPO':
agent = PPO(environment, args.hidden_dims, gamma=args.gamma, lambd=args.lambd,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, horrizon=args.horrizon, is_env_pool=is_env_pool)
elif args.model == 'QRPPO':
agent = QRPPO(environment, args.hidden_dims, gamma=args.gamma, lambd=args.lambd,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, kappa=args.kappa,
n_quantile=args.n_quantile, horrizon=args.horrizon, is_env_pool=is_env_pool, entropy_scale=args.entropy_scale)
elif args.model == 'SQRPPO':
agent = SQRPPO(environment, args.hidden_dims, gamma=args.gamma, lambd=args.lambd,
actor_lr=args.actor_lr, critic_lr=args.critic_lr, tau=args.tau, N=args.N, kappa=args.kappa,
quantile=args.quantile, horrizon=args.horrizon, is_env_pool=is_env_pool)
else:
raise NotImplementedError
gpu_ops = tf.GPUOptions(per_process_gpu_memory_fraction=0.25, allow_growth=True)
config = tf.ConfigProto(gpu_options=gpu_ops, allow_soft_placement=True)
saver = tf.train.Saver()
summary_writer = tf.summary.FileWriter(log_path, graph=tf.get_default_graph())
with tf.Session(config=config) as sess:
if args.eval or args.restore:
saver.restore(sess, model_path)
if not args.eval:
progress_fd = open(progress_file, 'r')
start_episode = len(progress_fd.readlines()) - 1
progress_fd.close()
progress_fd = open(progress_file, 'a')
else:
progress_fd = open(progress_file, 'w')
append_summary(progress_fd, 'episode, avg-reward, n_step')
progress_fd.flush()
start_episode = 0
tf.global_variables_initializer().run()
if not args.eval:
total_rewards = agent.train(
sess, saver, summary_writer, progress_fd, model_path, batch_size=args.batch_size, step=args.step,
train_episodes=args.train_episodes, start_episode=start_episode, save_episodes=args.save_episodes,
epsilon=args.epsilon, apply_her=args.apply_her, n_goals=args.n_goals, train_steps=args.train_steps)
progress_fd.close()
plot(os.path.join(args.plot_dir, args.model + '_' + args.env), np.array(total_rewards) + 1e-10)
else:
states, actions, rewards = agent.generate_episode(render=True)
print(states)