-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_data.py
More file actions
151 lines (98 loc) · 3.65 KB
/
gen_data.py
File metadata and controls
151 lines (98 loc) · 3.65 KB
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
import multiprocessing
import time
import cityflow
from dataclasses import dataclass
from typing import AnyStr, Dict, List, Any, Optional
import argparse
from agent import Agent, MaxPressureAgent
from roadnet_graph import RoadnetGraph
from utils import store_json, load_json, Point, store_pkl
import random
N_STEPS = 100_000
@dataclass
class Args:
cfg_file: AnyStr
out_file: AnyStr
shuffle_file: Optional[AnyStr]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--cfg-file", "-c", type=str, required=True)
parser.add_argument("--out-file", "-o", type=str, required=True)
parser.add_argument("--shuffled-out-file", "-s", type=str)
parsed = parser.parse_args()
return Args(
parsed.cfg_file,
parsed.out_file,
parsed.shuffled_out_file
)
@dataclass
class CarInfo:
id: str
closest_intersection_id: str
@staticmethod
def from_engine(engine: cityflow.Engine, graph: RoadnetGraph, vh_id: str) -> 'CarInfo':
vh_info = engine.get_vehicle_info(vh_id)
road_id = vh_info["road"]
road = graph.road_dict()[road_id]
distance = vh_info["distance"]
if float(distance) < road.length() / 2:
cl_i = road.start_intersection_id
else:
cl_i = road.end_intersection_id
return CarInfo(
vh_id,
cl_i
)
def gather_step_data(engine: cityflow.Engine, graph: RoadnetGraph, agents: Optional[List[Agent]]=None) -> dict:
vh_counts = engine.get_lane_vehicle_count()
lane_vhs = engine.get_lane_vehicles()
lane_vh_infos = {}
for lane_id, vhs in lane_vhs.items():
car_infos = (CarInfo.from_engine(engine, graph, vh_id) for vh_id in vhs)
lane_vh_infos[lane_id] = [{"id": ci.id, "closestIntersection": ci.closest_intersection_id} for ci in car_infos]
intersection_phases = {}
if agents is not None:
for agent in agents:
intersection_phases[agent.get_intersection().id] = agent.get_prev_phase()
return {
"laneCounts": vh_counts,
"laneVehicleInfos": lane_vh_infos,
"intersectionPhases": intersection_phases
}
def collect_data(engine: cityflow.Engine, graph: RoadnetGraph, n_steps: int, agents: Optional[List[Agent]]=None, reset_pre=True, reset_post=True, print_info=True) -> List[Dict[str, Any]]:
if reset_pre:
engine.reset()
data = []
try:
for i_step in range(n_steps):
step_data = gather_step_data(engine, graph, agents=agents)
for agent in agents:
agent.act(engine, step_data)
engine.next_step()
if print_info:
print(f"\r i: {i_step}, avg travel time: " + str(engine.get_average_travel_time()), end="")
data.append(step_data)
if len(engine.get_vehicles(True)) == 0:
break
if reset_post:
engine.reset()
except KeyboardInterrupt:
pass
return data
def main(args: Args = None):
if args is None:
args = parse_args()
cityflow_cfg = load_json(args.cfg_file)
graph = RoadnetGraph(cityflow_cfg["roadnetFile"])
use_rl = cityflow_cfg["rlTrafficLight"]
engine = cityflow.Engine(config_file=args.cfg_file, thread_num=multiprocessing.cpu_count())
agents = None
if use_rl:
agents = [MaxPressureAgent(intersection) for intersection in graph.intersection_list()]
data = collect_data(engine, graph, N_STEPS, agents=agents)
store_json(data, args.out_file)
if args.shuffle_file is not None:
random.shuffle(data)
store_json(data, args.shuffle_file)
if __name__ == '__main__':
main()