forked from cts198859/deeprl_network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (141 loc) · 5.88 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
"""
Main function for training and evaluating MARL algorithms in NMARL envs
@author: Tianshu Chu
"""
import argparse
import configparser
import logging
import tensorflow as tf
import threading
from envs.cacc_env import CACCEnv
from envs.large_grid_env import LargeGridEnv
from envs.real_net_env import RealNetEnv
from agents.models import IA2C, IA2C_FP, IA2C_CU, MA2C_NC, MA2C_IC3, MA2C_DIAL
from utils import (Counter, Trainer, Tester, Evaluator,
check_dir, copy_file, find_file,
init_dir, init_log, init_test_flag,
plot_evaluation, plot_train)
def parse_args():
default_base_dir = '/Users/tchu/Documents/rl_test/deeprl_dist/ia2c_grid_0.9'
default_config_dir = './config/config_ia2c_grid.ini'
parser = argparse.ArgumentParser()
parser.add_argument('--base-dir', type=str, required=False,
default=default_base_dir, help="experiment base dir")
subparsers = parser.add_subparsers(dest='option', help="train or evaluate")
sp = subparsers.add_parser('train', help='train a single agent under base dir')
sp.add_argument('--config-dir', type=str, required=False,
default=default_config_dir, help="experiment config path")
sp = subparsers.add_parser('evaluate', help="evaluate and compare agents under base dir")
sp.add_argument('--evaluation-seeds', type=str, required=False,
default=','.join([str(i) for i in range(2000, 2500, 10)]),
help="random seeds for evaluation, split by ,")
sp.add_argument('--demo', action='store_true', help="shows SUMO gui")
args = parser.parse_args()
if not args.option:
parser.print_help()
exit(1)
return args
def init_env(config, port=0):
scenario = config.get('scenario')
if scenario.startswith('atsc'):
if scenario.endswith('large_grid'):
return LargeGridEnv(config, port=port)
else:
return RealNetEnv(config, port=port)
else:
return CACCEnv(config)
def init_agent(env, config, total_step, seed):
if env.agent == 'ia2c':
return IA2C(env.n_s_ls, env.n_a_ls, env.neighbor_mask, env.distance_mask, env.coop_gamma,
total_step, config, seed=seed)
elif env.agent == 'ia2c_fp':
return IA2C_FP(env.n_s_ls, env.n_a_ls, env.neighbor_mask, env.distance_mask, env.coop_gamma,
total_step, config, seed=seed)
elif env.agent == 'ma2c_nc':
return MA2C_NC(env.n_s_ls, env.n_a_ls, env.neighbor_mask, env.distance_mask, env.coop_gamma,
total_step, config, seed=seed)
elif env.agent == 'ma2c_ic3':
# this is actually CommNet
return MA2C_IC3(env.n_s_ls, env.n_a_ls, env.neighbor_mask, env.distance_mask, env.coop_gamma,
total_step, config, seed=seed)
elif env.agent == 'ma2c_cu':
return IA2C_CU(env.n_s_ls, env.n_a_ls, env.neighbor_mask, env.distance_mask, env.coop_gamma,
total_step, config, seed=seed)
elif env.agent == 'ma2c_dial':
return MA2C_DIAL(env.n_s_ls, env.n_a_ls, env.neighbor_mask, env.distance_mask, env.coop_gamma,
total_step, config, seed=seed)
else:
return None
def train(args):
base_dir = args.base_dir
dirs = init_dir(base_dir)
init_log(dirs['log'])
config_dir = args.config_dir
copy_file(config_dir, dirs['data'])
config = configparser.ConfigParser()
config.read(config_dir)
# init env
env = init_env(config['ENV_CONFIG'])
logging.info('Training: a dim %r, agent dim: %d' % (env.n_a_ls, env.n_agent))
# init step counter
total_step = int(config.getfloat('TRAIN_CONFIG', 'total_step'))
test_step = int(config.getfloat('TRAIN_CONFIG', 'test_interval'))
log_step = int(config.getfloat('TRAIN_CONFIG', 'log_interval'))
global_counter = Counter(total_step, test_step, log_step)
# init centralized or multi agent
seed = config.getint('ENV_CONFIG', 'seed')
model = init_agent(env, config['MODEL_CONFIG'], total_step, seed)
# disable multi-threading for safe SUMO implementation
summary_writer = tf.summary.FileWriter(dirs['log'])
trainer = Trainer(env, model, global_counter, summary_writer, output_path=dirs['data'])
trainer.run()
# save model
final_step = global_counter.cur_step
logging.info('Training: save final model at step %d ...' % final_step)
model.save(dirs['model'], final_step)
def evaluate_fn(agent_dir, output_dir, seeds, port, demo):
agent = agent_dir.split('/')[-1]
if not check_dir(agent_dir):
logging.error('Evaluation: %s does not exist!' % agent)
return
# load config file
config_dir = find_file(agent_dir + '/data/')
if not config_dir:
return
config = configparser.ConfigParser()
config.read(config_dir)
# init env
env = init_env(config['ENV_CONFIG'], port=port)
env.init_test_seeds(seeds)
# load model for agent
model = init_agent(env, config['MODEL_CONFIG'], 0, 0)
if model is None:
return
model_dir = agent_dir + '/model/'
if not model.load(model_dir):
return
# collect evaluation data
evaluator = Evaluator(env, model, output_dir, gui=demo)
evaluator.run()
def evaluate(args):
base_dir = args.base_dir
if not args.demo:
dirs = init_dir(base_dir, pathes=['eva_data', 'eva_log'])
init_log(dirs['eva_log'])
output_dir = dirs['eva_data']
else:
output_dir = None
# enforce the same evaluation seeds across agents
seeds = args.evaluation_seeds
logging.info('Evaluation: random seeds: %s' % seeds)
if not seeds:
seeds = []
else:
seeds = [int(s) for s in seeds.split(',')]
evaluate_fn(base_dir, output_dir, seeds, 1, args.demo)
if __name__ == '__main__':
args = parse_args()
if args.option == 'train':
train(args)
else:
evaluate(args)