forked from shariqiqbal2810/maddpg-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (120 loc) · 5.81 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
import argparse
import torch
import time
import os
import numpy as np
from pathlib import Path
from torch.autograd import Variable
from tensorboardX import SummaryWriter
from utils.make_env import make_env
from utils.buffer import ReplayBuffer
from utils.env_wrappers import SubprocVecEnv
from algorithms.maddpg import MADDPG
USE_CUDA = torch.cuda.is_available()
def make_parallel_env(env_id, n_rollout_threads, seed):
def get_env_fn(rank):
def init_env():
env = make_env(env_id)
env.seed(seed + rank * 1000)
np.random.seed(seed + rank * 1000)
return env
return init_env
return SubprocVecEnv([get_env_fn(i) for i in range(n_rollout_threads)])
def run(config):
model_dir = Path('./models') / config.env_id / config.model_name
if not model_dir.exists():
curr_run = 'run1'
else:
exst_run_nums = [int(str(folder).split('run')[1]) for folder in
model_dir.iterdir() if str(folder).startswith('run')]
if len(exst_run_nums) == 0:
curr_run = 'run1'
else:
curr_run = 'run%i' % (max(exst_run_nums) + 1)
run_dir = model_dir / curr_run
log_dir = run_dir / 'logs'
os.makedirs(log_dir)
logger = SummaryWriter(str(log_dir))
torch.manual_seed(config.seed)
np.random.seed(config.seed)
env = make_parallel_env(config.env_id, config.n_rollout_threads, config.seed)
maddpg = MADDPG.init_from_env(env, config.agent_alg, config.adversary_alg)
replay_buffer = ReplayBuffer(config.buffer_length, maddpg.nagents,
[obsp.shape[0] for obsp in env.observation_space],
[acsp.shape[0] for acsp in env.action_space])
for ep_i in range(config.n_episodes):
print("Episode %i of %i" % (ep_i + 1, config.n_episodes))
obs = env.reset()
# obs.shape = (n_rollout_threads, nagent)(nobs), nobs differs per agent so not tensor
if USE_CUDA:
maddpg.prep_rollouts(device='cpu')
explr_pct_remaining = max(0, config.n_exploration_eps - ep_i) / config.n_exploration_eps
maddpg.scale_noise(config.final_noise_scale + (config.init_noise_scale - config.final_noise_scale) * explr_pct_remaining)
maddpg.reset_noise()
for t_i in range(config.episode_length):
# rearrange observations to be per agent, and convert to torch Variable
torch_obs = [Variable(torch.Tensor(np.vstack(obs[:, i])),
requires_grad=False)
for i in range(maddpg.nagents)]
# get actions as torch Variables
torch_agent_actions = maddpg.step(torch_obs, training=True)
# convert actions to numpy arrays
agent_actions = [ac.data.numpy() for ac in torch_agent_actions]
# rearrange actions to be per environment
actions = [[ac[i] for ac in agent_actions] for i in range(config.n_rollout_threads)]
next_obs, rewards, dones, infos = env.step(actions)
if t_i == config.episode_length - 1:
dones[:] = True
replay_buffer.push(obs, agent_actions, rewards, next_obs, dones)
obs = next_obs
ep_rews = replay_buffer.get_average_rewards(
config.episode_length * config.n_rollout_threads)
for a_i, a_ep_rew in enumerate(ep_rews):
logger.add_scalar('agent%i/mean_episode_rewards' % a_i, a_ep_rew, ep_i)
if USE_CUDA:
maddpg.prep_training(device='gpu')
if len(replay_buffer) >= config.batch_size:
for u_i in range(config.updates_per_episode):
for a_i in range(maddpg.nagents):
sample = replay_buffer.sample(config.batch_size, to_gpu=USE_CUDA)
maddpg.update(sample, a_i, logger=logger)
maddpg.update_all_targets()
if ep_i % config.save_interval == 0 or ep_i == (config.n_episodes - 1):
os.makedirs(run_dir / 'incremental', exist_ok=True)
maddpg.save(run_dir / 'incremental' / ('model_ep%i.pt' % (ep_i + 1)))
maddpg.save(run_dir / 'model.pt')
env.close()
logger.export_scalars_to_json(str(log_dir / 'summary.json'))
logger.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("env_id", help="Name of environment")
parser.add_argument("model_name",
help="Name of directory to store " +
"model/training contents")
parser.add_argument("--seed",
default=1, type=int,
help="Random seed")
parser.add_argument("--n_rollout_threads", default=12)
parser.add_argument("--buffer_length", default=int(1e6), type=int)
parser.add_argument("--n_episodes", default=10000, type=int)
parser.add_argument("--episode_length", default=100, type=int)
parser.add_argument("--updates_per_episode", default=1, type=int)
parser.add_argument("--batch_size",
default=102400, type=int,
help="Batch size for model training")
parser.add_argument("--n_exploration_eps", default=10000, type=int)
parser.add_argument("--init_noise_scale", default=0.3)
parser.add_argument("--final_noise_scale", default=0.0)
parser.add_argument("--save_interval", default=100, type=int)
# parser.add_argument("--learning_rate",
# default=1e-4, type=float,
# help="Learning rate of the model")
parser.add_argument("--agent_alg",
default="MADDPG", type=str,
choices=['MADDPG', 'DDPG'])
parser.add_argument("--adversary_alg",
default="MADDPG", type=str,
choices=['MADDPG', 'DDPG'])
config = parser.parse_args()
run(config)