-
Notifications
You must be signed in to change notification settings - Fork 15
/
convergent_learner.py
106 lines (81 loc) · 3.25 KB
/
convergent_learner.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
# python=3.6 requires using Qt4Agg backend for animation saving
import matplotlib
# matplotlib.use('Qt4Agg')
import models
import navigation as nav
from networkx import NetworkXNoPath
import numpy as np
import osmnx as ox
import pandas as pd
def init_custom_agent(graph, n=1, car_id=None, alternate_route=None):
"""
This function initializes a singular car with custom origin and destination
:param graph: list
:param n: int
:param car_id: None or int
:param alternate_route: None or list
:return cars_frame: DataFrame
"""
origin = 53085387
dest = 53082621
path = nav.get_init_path(graph.G, origin=origin, destination=dest)
route = nav.get_route(graph.G, origin=origin, destination=dest)
x, y = nav.get_position_of_node(graph.G, origin)
car = {'object': 'car',
'x': x,
'y': y,
'vx': 0,
'vy': 0,
'route-time': 0,
'origin': origin,
'destination': dest,
'route': route,
'xpath': np.array([path[i][0] for i in range(len(path))]),
'ypath': np.array([path[i][1] for i in range(len(path))]),
'distance-to-car': 0,
'distance-to-node': 0,
'distance-to-red-light': 0}
cars_data = [car]
if alternate_route:
cars_data[car_id]['route'], cars_data[car_id]['xpath'], cars_data[car_id]['ypath'] = alternate_route
cars_frame = pd.DataFrame(cars_data)
# determine binning and assign bins to cars
cars_frame['xbin'], cars_frame['ybin'] = models.determine_bins(graph.fig, cars_frame)
return cars_frame
def init_custom_lights(fig_axis, prescale=None):
"""
traffic lights are initialized here
:param fig_axis: list
:param prescale: int
:return lights: list
"""
epsilon = 0.1 # a factor which forces the positions of the light faces to be close to the intersection
lights_data = []
node_id = 53119168
try:
out_vectors = np.array(nav.determine_pedigree(node_id))
except NetworkXNoPath or ValueError:
raise('Could not determine pedigree for light at node {}'.format(node_id))
degree = len(out_vectors)
x, y = nav.get_position_of_node(node_id)
go = [False, True] * degree * 2
go = go[:degree]
light = {'object': 'light',
'node': node_id,
'degree': degree,
'x': x,
'y': y,
'switch-counter': 0,
'switch-time': models.determine_traffic_light_timer(degree)
}
light['out-xpositions'] = [x + epsilon * out_vectors[j][0] for j in range(light['degree'])]
light['out-ypositions'] = [y + epsilon * out_vectors[j][1] for j in range(light['degree'])]
light['out-xvectors'] = [out_vectors[j][0] for j in range(light['degree'])]
light['out-yvectors'] = [out_vectors[j][1] for j in range(light['degree'])]
light['go-values'] = np.array([go[j] for j in range(light['degree'])])
lights_data.append(light)
lights = pd.DataFrame(lights_data)
# determine binning and assign bins to lights
lights['xbin'], lights['ybin'] = models.determine_bins(fig_axis, lights)
# print('Number of traffic lights: {}'.format(len(lights)))
return lights