-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_dqn.py
136 lines (121 loc) · 5.32 KB
/
run_dqn.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
import gym
from environment import TSCEnv
from world import World
from generator import LaneVehicleGenerator
from agent.dqn_agent import DQNAgent
from metric import TravelTimeMetric
import argparse
import os
import numpy as np
import logging
from datetime import datetime
# parse args
parser = argparse.ArgumentParser(description='Run Example')
parser.add_argument('config_file', type=str, help='path of config file')
parser.add_argument('--thread', type=int, default=1, help='number of threads')
parser.add_argument('--steps', type=int, default=3600, help='number of steps')
parser.add_argument('--action_interval', type=int, default=20, help='how often agent make decisions')
parser.add_argument('--episodes', type=int, default=200, help='training episodes')
parser.add_argument('--save_model', action="store_true", default=False)
parser.add_argument('--load_model', action="store_true", default=False)
parser.add_argument("--save_rate", type=int, default=20, help="save model once every time this many episodes are completed")
parser.add_argument('--save_dir', type=str, default="model/dqn", help='directory in which model should be saved')
parser.add_argument('--log_dir', type=str, default="log/dqn", help='directory in which logs should be saved')
args = parser.parse_args()
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
logger = logging.getLogger('main')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(os.path.join(args.log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + ".log"))
fh.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
logger.addHandler(fh)
logger.addHandler(sh)
# create world
world = World(args.config_file, thread_num=args.thread)
# create agents
agents = []
for i in world.intersections:
action_space = gym.spaces.Discrete(len(i.phases))
agents.append(DQNAgent(
action_space,
LaneVehicleGenerator(world, i, ["lane_count"], in_only=True, average=None),
LaneVehicleGenerator(world, i, ["lane_waiting_count"], in_only=True, average="all", negative=True),
i.id
))
if args.load_model:
agents[-1].load_model(args.save_dir)
# create metric
metric = TravelTimeMetric(world)
# create env
env = TSCEnv(world, agents, metric)
# train dqn_agent
def train(args, env):
total_decision_num = 0
for e in range(args.episodes):
last_obs = env.reset()
if e % args.save_rate == args.save_rate - 1:
env.eng.set_save_replay(True)
env.eng.set_replay_file("replay_%s.txt" % e)
else:
env.eng.set_save_replay(False)
episodes_rewards = [0 for i in agents]
episodes_decision_num = 0
i = 0
while i < args.steps:
if i % args.action_interval == 0:
actions = []
for agent_id, agent in enumerate(agents):
if total_decision_num > agent.learning_start:
#if True:
actions.append(agent.get_action(last_obs[agent_id]))
else:
actions.append(agent.sample())
rewards_list = []
for _ in range(args.action_interval):
obs, rewards, dones, _ = env.step(actions)
i += 1
rewards_list.append(rewards)
rewards = np.mean(rewards_list, axis=0)
for agent_id, agent in enumerate(agents):
agent.remember(last_obs[agent_id], actions[agent_id], rewards[agent_id], obs[agent_id])
episodes_rewards[agent_id] += rewards[agent_id]
episodes_decision_num += 1
total_decision_num += 1
last_obs = obs
for agent_id, agent in enumerate(agents):
if total_decision_num > agent.learning_start and total_decision_num % agent.update_model_freq == agent.update_model_freq - 1:
agent.replay()
if total_decision_num > agent.learning_start and total_decision_num % agent.update_target_model_freq == agent.update_target_model_freq - 1:
agent.update_target_network()
if all(dones):
break
if e % args.save_rate == args.save_rate - 1:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
for agent in agents:
agent.save_model(args.save_dir)
logger.info("episode:{}/{}, average travel time:{}".format(e, args.episodes, env.eng.get_average_travel_time()))
for agent_id, agent in enumerate(agents):
logger.info("agent:{}, mean_episode_reward:{}".format(agent_id, episodes_rewards[agent_id] / episodes_decision_num))
def test():
obs = env.reset()
for agent in agents:
agent.load_model(args.save_dir)
for i in range(args.steps):
if i % args.action_interval == 0:
actions = []
for agent_id, agent in enumerate(agents):
actions.append(agent.get_action(obs[agent_id]))
obs, rewards, dones, info = env.step(actions)
#print(rewards)
if all(dones):
break
logger.info("Final Travel Time is %.4f" % env.metric.update(done=True))
if __name__ == '__main__':
# simulate
# import os
# os.environ["CUDA_VISIBLE_DEVICES"] = '0, 1'
train(args, env)
#test()