13
13
(40.752 , - 73.987 ),
14
14
]
15
15
16
+
16
17
class VehicleAgent (GeoAgent ):
17
18
def __init__ (self , unique_id , model , shape , start_node , end_node , crs ):
18
19
super ().__init__ (unique_id , model , shape , crs )
19
20
self .start_node = start_node
20
21
self .end_node = end_node
21
22
try :
22
- self .route = nx .shortest_path (model .graph , source = start_node , target = end_node , weight = 'length' )
23
+ self .route = nx .shortest_path (
24
+ model .graph , source = start_node , target = end_node , weight = "length"
25
+ )
23
26
except nx .NetworkXNoPath :
24
27
self .route = [] # Empty route if no path found
25
28
self .current_step = 0
@@ -28,14 +31,17 @@ def step(self):
28
31
if self .current_step < len (self .route ) - 1 :
29
32
self .current_step += 1
30
33
node = self .route [self .current_step ]
31
- self .shape = Point ((self .model .graph .nodes [node ]['x' ], self .model .graph .nodes [node ]['y' ]))
34
+ self .shape = Point (
35
+ (self .model .graph .nodes [node ]["x" ], self .model .graph .nodes [node ]["y" ])
36
+ )
37
+
32
38
33
39
class TrafficModel (Model ):
34
40
def __init__ (self , num_vehicles , north , south , east , west , steps ):
35
41
super ().__init__ ()
36
42
self .num_vehicles = num_vehicles
37
43
self .steps = steps
38
- self .graph = ox .graph_from_bbox (north , south , east , west , network_type = ' drive' )
44
+ self .graph = ox .graph_from_bbox (north , south , east , west , network_type = " drive" )
39
45
self .schedule = SimultaneousActivation (self )
40
46
self .space = GeoSpace (crs = "epsg:4326" )
41
47
@@ -44,8 +50,12 @@ def __init__(self, num_vehicles, north, south, east, west, steps):
44
50
for i in range (self .num_vehicles ):
45
51
start_node = random .choice (list (self .graph .nodes ))
46
52
end_node = random .choice (list (self .graph .nodes ))
47
- start_point = Point ((self .graph .nodes [start_node ]['x' ], self .graph .nodes [start_node ]['y' ]))
48
- vehicle = VehicleAgent (i , self , start_point , start_node , end_node , crs = "epsg:4326" )
53
+ start_point = Point (
54
+ (self .graph .nodes [start_node ]["x" ], self .graph .nodes [start_node ]["y" ])
55
+ )
56
+ vehicle = VehicleAgent (
57
+ i , self , start_point , start_node , end_node , crs = "epsg:4326"
58
+ )
49
59
if vehicle .route : # Only add vehicles with valid routes
50
60
self .space .add_agents (vehicle )
51
61
self .schedule .add (vehicle )
@@ -55,7 +65,9 @@ def simulate_congestion(self, critical_points):
55
65
node = ox .distance .nearest_nodes (self .graph , X = point [1 ], Y = point [0 ])
56
66
if node in self .graph .nodes :
57
67
for u , v , key , data in self .graph .edges (node , keys = True , data = True ):
58
- data ['length' ] *= 5 # Simulating congestion by increasing travel time
68
+ data ["length" ] *= (
69
+ 5 # Simulating congestion by increasing travel time
70
+ )
59
71
60
72
def step (self ):
61
73
self .schedule .step ()
0 commit comments