Skip to content

Commit e2f53cb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 1a64095 commit e2f53cb

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

gis/Traffic_Simulation/Server.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from mesa.visualization.ModularVisualization import ModularServer
88
from flask import Flask
99

10+
1011
def vehicle_portrayal(agent):
1112
if agent is None:
1213
return
@@ -16,11 +17,12 @@ def vehicle_portrayal(agent):
1617
"Color": "red",
1718
"Filled": "true",
1819
"Layer": 1,
19-
"r": 3
20+
"r": 3,
2021
}
2122

2223
return portrayal
2324

25+
2426
def road_portrayal(road):
2527
return {
2628
"Shape": "line",
@@ -30,16 +32,19 @@ def road_portrayal(road):
3032
"coordinates": [(coord[1], coord[0]) for coord in road.shape.coords],
3133
}
3234

35+
3336
class RoadAgent(GeoAgent):
3437
def __init__(self, unique_id, model, shape, crs):
3538
super().__init__(unique_id, model, shape, crs)
3639

40+
3741
def create_road_agents(model):
3842
edges = ox.graph_to_gdfs(model.graph, nodes=False, edges=True)
3943
for i, row in edges.iterrows():
40-
road = RoadAgent(i, model, row['geometry'], crs="epsg:4326")
44+
road = RoadAgent(i, model, row["geometry"], crs="epsg:4326")
4145
model.space.add_agents(road)
4246

47+
4348
map_module = mg.visualization.MapModule(
4449
portrayal_method=vehicle_portrayal,
4550
view=[40.755, -73.987], # Center view on a critical point
@@ -58,6 +63,7 @@ def create_road_agents(model):
5863
"steps": mesa.visualization.Slider("Simulation Steps", 10, 1, 100, 1),
5964
}
6065

66+
6167
class CustomModularServer(ModularServer):
6268
def __init__(self, *args, **kwargs):
6369
super().__init__(*args, **kwargs)
@@ -84,11 +90,9 @@ def simulate(self):
8490
self.model.step()
8591
self.model.running = False
8692

93+
8794
server = CustomModularServer(
88-
TrafficModel,
89-
[map_module],
90-
"Traffic Simulation",
91-
model_params
95+
TrafficModel, [map_module], "Traffic Simulation", model_params
9296
)
9397

9498
server.port = 8522 # Change to a different port if necessary
@@ -119,7 +123,7 @@ def simulate(self):
119123
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
120124
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
121125
}).addTo(map);
122-
126+
123127
var simulate = function() {
124128
fetch('/simulate', { method: 'POST' });
125129
};

gis/Traffic_Simulation/model.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
(40.752, -73.987),
1414
]
1515

16+
1617
class VehicleAgent(GeoAgent):
1718
def __init__(self, unique_id, model, shape, start_node, end_node, crs):
1819
super().__init__(unique_id, model, shape, crs)
1920
self.start_node = start_node
2021
self.end_node = end_node
2122
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+
)
2326
except nx.NetworkXNoPath:
2427
self.route = [] # Empty route if no path found
2528
self.current_step = 0
@@ -28,14 +31,17 @@ def step(self):
2831
if self.current_step < len(self.route) - 1:
2932
self.current_step += 1
3033
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+
3238

3339
class TrafficModel(Model):
3440
def __init__(self, num_vehicles, north, south, east, west, steps):
3541
super().__init__()
3642
self.num_vehicles = num_vehicles
3743
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")
3945
self.schedule = SimultaneousActivation(self)
4046
self.space = GeoSpace(crs="epsg:4326")
4147

@@ -44,8 +50,12 @@ def __init__(self, num_vehicles, north, south, east, west, steps):
4450
for i in range(self.num_vehicles):
4551
start_node = random.choice(list(self.graph.nodes))
4652
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+
)
4959
if vehicle.route: # Only add vehicles with valid routes
5060
self.space.add_agents(vehicle)
5161
self.schedule.add(vehicle)
@@ -55,7 +65,9 @@ def simulate_congestion(self, critical_points):
5565
node = ox.distance.nearest_nodes(self.graph, X=point[1], Y=point[0])
5666
if node in self.graph.nodes:
5767
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+
)
5971

6072
def step(self):
6173
self.schedule.step()

0 commit comments

Comments
 (0)