-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_max_pressure.py
80 lines (64 loc) · 2.17 KB
/
run_max_pressure.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
import gym
from environment import TSCEnv
from world import World
from agent import MaxPressureAgent
from metric import TravelTimeMetric, ThroughputMetric, SpeedScoreMetric, MaxWaitingTimeMetric
import utils as u
import argparse
#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('--delta_t', type=int, default=20, help='how often agent make decisions')
args = parser.parse_args()
#Agent options
options = {
'delta_t':args.delta_t
}
#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(MaxPressureAgent(action_space,options, i, world))
# Create metric
metric = [TravelTimeMetric(world), ThroughputMetric(world), SpeedScoreMetric(world), MaxWaitingTimeMetric(world)]
#Create env
env = TSCEnv(world, agents, metric)
obs = env.reset()
actions = []
steps = 0
episodes_rewards = 0
#Walk through the steps
while steps < args.steps:
actions = []
#Get the agents' actions
for agent_id, agent in enumerate(agents):
actions.append(agent.get_action(obs[agent_id]))
#Run steps
obs, rewards, dones, info = env.step(actions)
steps += 1
episodes_rewards += rewards[0]
#Check if it's over by flag "Done"
if all(dones) == True:
print(i)
break
print(f"\n--MaxPressure Results--")
print(f"Steps: {steps}")
print(f"Episodes Rewards: {episodes_rewards/steps:.4f}")
# for metric in env.metric:
# print(f"{metric.name}: {metric.eval():.4f}")
#start wandb
u.wand_init("TLC - Results C2",f"MaxPressure: {options['delta_t']}", "MaxPressure")
eval_dict = {}
eval_dict["epsilon"]=0
eval_dict["steps"]=steps
eval_dict["mean_episode_reward"]=episodes_rewards/steps
for metric in env.metric:
eval_dict[metric.name]=metric.eval()
print(f"{metric.name}: {metric.eval():.4f}")
for e in range(200):
eval_dict["episode"]=e
u.wand_log(eval_dict)