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---
|
| 7 |
+
class VenueLayout:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.graph = nx.DiGraph()
|
| 10 |
+
connections = [
|
| 11 |
+
('Main_Gate_A', 'Walkway_North', 2, 50),
|
| 12 |
+
('Main_Gate_B', 'Walkway_South', 2, 20), # Narrow gate
|
| 13 |
+
('Walkway_North', 'Food_Court_1', 3, 40),
|
| 14 |
+
('Walkway_South', 'Food_Court_2', 3, 40),
|
| 15 |
+
('Food_Court_1', 'Arena_Zone_X', 4, 60),
|
| 16 |
+
('Food_Court_2', 'Arena_Zone_Y', 4, 60),
|
| 17 |
+
('Walkway_North', 'Arena_Zone_X', 5, 80),
|
| 18 |
+
('Walkway_South', 'Arena_Zone_Y', 5, 80),
|
| 19 |
+
('Walkway_North', 'Walkway_South', 3, 20),
|
| 20 |
+
('Walkway_South', 'Walkway_North', 3, 20),
|
| 21 |
+
('Arena_Zone_X', 'Main_Exit_1', 3, 50),
|
| 22 |
+
('Arena_Zone_Y', 'Main_Exit_2', 3, 50),
|
| 23 |
+
]
|
| 24 |
+
for src, dest, weight, capacity in connections:
|
| 25 |
+
self.graph.add_edge(src, dest, weight=weight, capacity=capacity, current_load=0)
|
| 26 |
+
|
| 27 |
+
def get_dynamic_weight(self, src, dest):
|
| 28 |
+
edge = self.graph[src][dest]
|
| 29 |
+
load = edge['current_load']
|
| 30 |
+
capacity = edge['capacity']
|
| 31 |
+
base_time = edge['weight']
|
| 32 |
+
if load >= capacity:
|
| 33 |
+
return base_time * 10.0
|
| 34 |
+
elif load >= capacity * 0.75:
|
| 35 |
+
return base_time * 3.5
|
| 36 |
+
return base_time
|
| 37 |
+
|
| 38 |
+
# --- AGENT LOGIC ---
|
| 39 |
+
class PersonAgent:
|
| 40 |
+
def __init__(self, agent_id, origin, destination):
|
| 41 |
+
self.agent_id = agent_id
|
| 42 |
+
self.origin = origin
|
| 43 |
+
self.destination = destination
|
| 44 |
+
self.current_node = origin
|
| 45 |
+
self.route = []
|
| 46 |
+
self.route_index = 0
|
| 47 |
+
self.completed = False
|
| 48 |
+
|
| 49 |
+
def calculate_route(self, venue, use_rerouting):
|
| 50 |
+
try:
|
| 51 |
+
weight_param = venue.get_dynamic_weight if use_rerouting else 'weight'
|
| 52 |
+
self.route = nx.shortest_path(venue.graph, source=self.current_node, target=self.destination, weight=weight_param)
|
| 53 |
+
self.route_index = 0
|
| 54 |
+
except nx.NetworkNoPath:
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
def step(self):
|
| 58 |
+
if self.current_node == self.destination:
|
| 59 |
+
self.completed = True
|
| 60 |
+
return self.current_node, None
|
| 61 |
+
if self.route_index < len(self.route) - 1:
|
| 62 |
+
from_node = self.route[self.route_index]
|
| 63 |
+
to_node = self.route[self.route_index + 1]
|
| 64 |
+
self.current_node = to_node
|
| 65 |
+
self.route_index += 1
|
| 66 |
+
return from_node, to_node
|
| 67 |
+
self.completed = True
|
| 68 |
+
return self.current_node, None
|
| 69 |
+
|
| 70 |
+
# --- SIMULATION PIPELINE FOR GRADIO ---
|
| 71 |
+
def run_ui_simulation(crowd_size, use_ai_rerouting):
|
| 72 |
+
venue = VenueLayout()
|
| 73 |
+
agents = []
|
| 74 |
+
origins = ['Main_Gate_A', 'Main_Gate_B']
|
| 75 |
+
destinations = ['Arena_Zone_X', 'Arena_Zone_Y', 'Main_Exit_1', 'Main_Exit_2']
|
| 76 |
+
|
| 77 |
+
random.seed(42)
|
| 78 |
+
for i in range(int(crowd_size)):
|
| 79 |
+
agents.append(PersonAgent(i, random.choice(origins), random.choice(destinations)))
|
| 80 |
+
|
| 81 |
+
# Run for 5 timeline steps to accumulate traffic loads
|
| 82 |
+
for _ in range(5):
|
| 83 |
+
for u, v in venue.graph.edges():
|
| 84 |
+
venue.graph[u][v]['current_load'] = 0
|
| 85 |
+
active_agents = [a for a in agents if not a.completed]
|
| 86 |
+
if not active_agents:
|
| 87 |
+
break
|
| 88 |
+
for agent in active_agents:
|
| 89 |
+
agent.calculate_route(venue, use_ai_rerouting)
|
| 90 |
+
for agent in active_agents:
|
| 91 |
+
u, v = agent.step()
|
| 92 |
+
if v is not None:
|
| 93 |
+
venue.graph[u][v]['current_load'] += 1
|
| 94 |
+
|
| 95 |
+
# Generate Google-maps style Traffic Report Text
|
| 96 |
+
report = "📋 SYSTEM LIVE REPORT:\n"
|
| 97 |
+
bottlenecks_found = False
|
| 98 |
+
edge_colors = []
|
| 99 |
+
|
| 100 |
+
for u, v, data in venue.graph.edges(data=True):
|
| 101 |
+
load = data['current_load']
|
| 102 |
+
cap = data['capacity']
|
| 103 |
+
ratio = load / cap if cap > 0 else 0
|
| 104 |
+
|
| 105 |
+
if ratio >= 1.0:
|
| 106 |
+
report += f"🔴 CRITICAL BOTTLENECK: {u} -> {v} ({load}/{cap} people)\n"
|
| 107 |
+
edge_colors.append('red')
|
| 108 |
+
bottlenecks_found = True
|
| 109 |
+
elif ratio >= 0.75:
|
| 110 |
+
report += f"🟡 WARNING CONGESTION: {u} -> {v} ({load}/{cap} people)\n"
|
| 111 |
+
edge_colors.append('orange')
|
| 112 |
+
bottlenecks_found = True
|
| 113 |
+
else:
|
| 114 |
+
edge_colors.append('green')
|
| 115 |
+
|
| 116 |
+
if not bottlenecks_found:
|
| 117 |
+
report += "🟢 All routes operating smoothly. Crowd distributed successfully."
|
| 118 |
+
|
| 119 |
+
# Create Visual Map Plot
|
| 120 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 121 |
+
pos = nx.spring_layout(venue.graph, seed=42)
|
| 122 |
+
nx.draw_networkx_nodes(venue.graph, pos, node_size=700, node_color='skyblue', ax=ax)
|
| 123 |
+
nx.draw_networkx_labels(venue.graph, pos, font_size=8, font_weight='bold', ax=ax)
|
| 124 |
+
nx.draw_networkx_edges(venue.graph, pos, edge_color=edge_colors, width=3, arrowsize=15, ax=ax)
|
| 125 |
+
plt.title("Venue Crowd Traffic Density Layout Map")
|
| 126 |
+
plt.axis('off')
|
| 127 |
+
|
| 128 |
+
return fig, report
|
| 129 |
+
|
| 130 |
+
# --- GRADIO INTERFACE CONFIGURATION ---
|
| 131 |
+
interface = io.Interface(
|
| 132 |
+
fn=run_ui_simulation,
|
| 133 |
+
inputs=[
|
| 134 |
+
io.Slider(minimum=10, maximum=500, value=150, label="Expected Crowd Size"),
|
| 135 |
+
io.Checkbox(value=True, label="Enable Google Maps Style AI Rerouting")
|
| 136 |
+
],
|
| 137 |
+
outputs=[
|
| 138 |
+
io.Plot(label="Live Congestion Map Layout"),
|
| 139 |
+
io.Textbox(label="Analytics Report Console", lines=6)
|
| 140 |
+
],
|
| 141 |
+
title="🏢 Real-Time AI Crowd Flow Optimiser",
|
| 142 |
+
description="Simulate venue patterns and clear path networks automatically using dynamic weight calculations."
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
interface.launch()
|