-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
542 lines (474 loc) · 20.5 KB
/
Copy pathdashboard.py
File metadata and controls
542 lines (474 loc) · 20.5 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"""
DRL-Based Adaptive Network Routing — Interactive Dashboard
Authors: Muhammad Sabeeh (23K-0002), Rayyan Merchant (23K-0073)
Run: streamlit run dashboard.py
"""
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import networkx as nx
import os, time
# ─────────────────────── PAGE CONFIG ───────────────────────
st.set_page_config(
page_title="DRL Routing Dashboard",
page_icon="🌐",
layout="wide",
initial_sidebar_state="expanded",
)
# ─────────────────────── CUSTOM CSS ───────────────────────
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
html, body, [class*="css"] { font-family: 'Inter', sans-serif; }
.main { background: linear-gradient(135deg, #0f0c29 0%, #1a1a2e 50%, #16213e 100%); }
[data-testid="stSidebar"] {
background: linear-gradient(180deg, #1a1a2e 0%, #0f0c29 100%);
border-right: 1px solid rgba(255,255,255,0.05);
}
.metric-card {
background: linear-gradient(135deg, rgba(30,30,60,0.9), rgba(20,20,50,0.9));
border: 1px solid rgba(100,100,255,0.15);
border-radius: 16px;
padding: 20px 24px;
text-align: center;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0,0,0,0.3);
transition: transform 0.2s, box-shadow 0.2s;
}
.metric-card:hover {
transform: translateY(-2px);
box-shadow: 0 12px 40px rgba(80,80,255,0.15);
}
.metric-value {
font-size: 2rem;
font-weight: 700;
background: linear-gradient(135deg, #667eea, #764ba2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.metric-label {
font-size: 0.85rem;
color: #8888aa;
margin-top: 4px;
text-transform: uppercase;
letter-spacing: 1px;
}
.section-header {
font-size: 1.4rem;
font-weight: 600;
color: #e0e0ff;
margin: 28px 0 12px 0;
padding-bottom: 8px;
border-bottom: 2px solid rgba(100,100,255,0.2);
}
.topology-info {
background: rgba(30,30,60,0.7);
border: 1px solid rgba(100,100,255,0.1);
border-radius: 12px;
padding: 16px;
color: #aaa;
font-size: 0.85rem;
line-height: 1.6;
}
.stSelectbox > div > div { background-color: rgba(30,30,60,0.8); }
</style>
""", unsafe_allow_html=True)
# ─────────────────────── CONSTANTS ───────────────────────
LOGS = "results/logs"
COLORS = {"DQN": "#667eea", "DDQN": "#f093fb", "Dijkstra": "#4ecdc4"}
TOPOLOGY_NODES = {
"S1": (0, 2), "S2": (0, 0),
"R1": (2, 2.5), "R2": (2, 1), "R3": (2, -0.5),
"D1": (4, 1),
}
TOPOLOGY_EDGES = [
("S1","R1","10Mbps","2ms"), ("S1","R2","7Mbps","4ms"),
("S2","R2","7Mbps","4ms"), ("S2","R3","10Mbps","2ms"),
("R1","R2","5Mbps","5ms"), ("R2","R3","5Mbps","5ms"),
("R1","D1","8Mbps","8ms"), ("R2","D1","6Mbps","6ms"),
("R3","D1","4Mbps","10ms"), ("R1","R3","3Mbps","12ms"),
]
PATHS = {
"SD1 (S1→D1)": [
("Path 0: S1→R1→D1", ["S1","R1","D1"]),
("Path 1: S1→R2→D1", ["S1","R2","D1"]),
("Path 2: S1→R1→R2→D1", ["S1","R1","R2","D1"]),
],
"SD2 (S2→D1)": [
("Path 0: S2→R3→D1", ["S2","R3","D1"]),
("Path 1: S2→R2→D1", ["S2","R2","D1"]),
("Path 2: S2→R2→R1→D1", ["S2","R2","R1","D1"]),
],
}
# ─────────────────────── DATA LOADING ───────────────────────
@st.cache_data
def load_training(algo):
p = os.path.join(LOGS, f"{algo.lower()}_training.csv")
if os.path.exists(p):
return pd.read_csv(p)
return None
@st.cache_data
def load_baseline(scenario):
p = os.path.join(LOGS, f"dijkstra_{scenario}_summary.csv")
if os.path.exists(p):
df = pd.read_csv(p, index_col=0)
return {row: float(df.loc[row, "0"]) for row in df.index}
return None
# ─────────────────────── HELPERS ───────────────────────
def metric_card(label, value, fmt=".2f"):
v = f"{value:{fmt}}" if isinstance(value, (int, float)) else str(value)
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{v}</div>
<div class="metric-label">{label}</div>
</div>""", unsafe_allow_html=True)
def draw_topology(highlight_path=None):
G = nx.Graph()
for n, pos in TOPOLOGY_NODES.items():
G.add_node(n, pos=pos)
for u, v, bw, dl in TOPOLOGY_EDGES:
G.add_edge(u, v, bw=bw, delay=dl)
pos = nx.get_node_attributes(G, "pos")
edge_x, edge_y, edge_colors, edge_widths = [], [], [], []
mid_x, mid_y, mid_text = [], [], []
for u, v, d in G.edges(data=True):
x0, y0 = pos[u]; x1, y1 = pos[v]
is_hl = False
if highlight_path:
for i in range(len(highlight_path)-1):
if {highlight_path[i], highlight_path[i+1]} == {u, v}:
is_hl = True
edge_x += [x0, x1, None]; edge_y += [y0, y1, None]
edge_colors.append("#667eea" if is_hl else "rgba(100,100,150,0.4)")
edge_widths.append(5 if is_hl else 2)
mid_x.append((x0+x1)/2); mid_y.append((y0+y1)/2)
mid_text.append(f"{d['bw']}<br>{d['delay']}")
fig = go.Figure()
# Draw edges individually for different colors
for i, (u, v, d) in enumerate(G.edges(data=True)):
x0, y0 = pos[u]; x1, y1 = pos[v]
is_hl = False
if highlight_path:
for j in range(len(highlight_path)-1):
if {highlight_path[j], highlight_path[j+1]} == {u, v}:
is_hl = True
fig.add_trace(go.Scatter(
x=[x0, x1], y=[y0, y1], mode="lines",
line=dict(
width=6 if is_hl else 2,
color="#667eea" if is_hl else "rgba(100,100,150,0.35)"
),
hoverinfo="skip", showlegend=False,
))
# Edge labels
fig.add_trace(go.Scatter(
x=mid_x, y=mid_y, mode="text",
text=mid_text, textfont=dict(size=9, color="#888"),
hoverinfo="skip", showlegend=False,
))
# Nodes
node_x = [pos[n][0] for n in G.nodes()]
node_y = [pos[n][1] for n in G.nodes()]
node_colors = []
for n in G.nodes():
if n.startswith("S"):
node_colors.append("#4ecdc4")
elif n == "D1":
node_colors.append("#f093fb")
else:
node_colors.append("#667eea")
fig.add_trace(go.Scatter(
x=node_x, y=node_y, mode="markers+text",
marker=dict(size=36, color=node_colors,
line=dict(width=2, color="rgba(255,255,255,0.3)")),
text=list(G.nodes()), textposition="middle center",
textfont=dict(size=13, color="white", family="Inter"),
hoverinfo="text",
hovertext=[f"{n} ({'Source' if n.startswith('S') else 'Dest' if n=='D1' else 'Router'})" for n in G.nodes()],
showlegend=False,
))
fig.update_layout(
plot_bgcolor="rgba(0,0,0,0)", paper_bgcolor="rgba(0,0,0,0)",
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
margin=dict(l=10, r=10, t=10, b=10), height=340,
)
return fig
def plotly_dark(fig, h=400):
fig.update_layout(
template="plotly_dark",
plot_bgcolor="rgba(0,0,0,0)",
paper_bgcolor="rgba(0,0,0,0)",
font=dict(family="Inter", color="#ccc"),
height=h,
margin=dict(l=50, r=20, t=40, b=40),
)
return fig
# ─────────────────────── SIDEBAR ───────────────────────
with st.sidebar:
st.markdown("## 🌐 DRL Routing")
st.markdown("---")
page = st.radio(
"Navigate",
["🏠 Overview", "📈 Training", "🔬 Comparison", "🗺️ Topology", "🧪 Simulation"],
label_visibility="collapsed",
)
st.markdown("---")
st.markdown(
"<div style='color:#666;font-size:0.75rem;text-align:center;'>"
"Muhammad Sabeeh · Rayyan Merchant<br>CN Project 2026</div>",
unsafe_allow_html=True,
)
# ═══════════════════════ PAGE: OVERVIEW ═══════════════════════
if page == "🏠 Overview":
st.markdown("# 🌐 DRL-Based Adaptive Network Routing")
st.markdown("##### Deep Reinforcement Learning for dynamic traffic routing using DQN & DDQN on ns-3")
st.markdown("---")
dqn = load_training("dqn")
ddqn = load_training("ddqn")
c1, c2, c3, c4 = st.columns(4)
with c1:
metric_card("DQN Episodes", len(dqn) if dqn is not None else 0, "d")
with c2:
metric_card("DQN Final Cost", dqn["reward"].iloc[-1] if dqn is not None else 0)
with c3:
metric_card("DDQN Episodes", len(ddqn) if ddqn is not None else 0, "d")
with c4:
metric_card("DDQN Final Cost", ddqn["reward"].iloc[-1] if ddqn is not None else 0)
st.markdown("")
col_l, col_r = st.columns([3, 2])
with col_l:
st.markdown('<div class="section-header">Training Convergence</div>', unsafe_allow_html=True)
fig = go.Figure()
if dqn is not None:
fig.add_trace(go.Scatter(
x=dqn["episode"], y=dqn["reward"].rolling(30).mean(),
name="DQN", line=dict(color=COLORS["DQN"], width=2),
))
if ddqn is not None:
fig.add_trace(go.Scatter(
x=ddqn["episode"], y=ddqn["reward"].rolling(30).mean(),
name="DDQN", line=dict(color=COLORS["DDQN"], width=2),
))
fig.update_layout(yaxis_title="Episode Cost (lower = better)", xaxis_title="Episode")
st.plotly_chart(plotly_dark(fig, 350), use_container_width=True)
with col_r:
st.markdown('<div class="section-header">Network Topology</div>', unsafe_allow_html=True)
st.plotly_chart(draw_topology(), use_container_width=True)
# ═══════════════════════ PAGE: TRAINING ═══════════════════════
elif page == "📈 Training":
st.markdown("# 📈 Training Analysis")
algo = st.selectbox("Algorithm", ["DQN", "DDQN"])
df = load_training(algo)
if df is None:
st.error(f"No training log found for {algo}.")
st.stop()
c1, c2, c3, c4 = st.columns(4)
with c1: metric_card("Total Episodes", len(df), "d")
with c2: metric_card("Final Cost", df["reward"].iloc[-1])
with c3: metric_card("Final Epsilon", df["epsilon"].iloc[-1], ".4f")
with c4: metric_card("Avg Loss (last 50)", df["avg_loss"].tail(50).mean(), ".4f")
st.markdown("")
# Episode range slider
ep_range = st.slider("Episode Range", 0, len(df)-1, (0, len(df)-1))
dfs = df.iloc[ep_range[0]:ep_range[1]+1]
tab1, tab2, tab3 = st.tabs(["📉 Reward Curve", "🎯 Path Selection", "🔄 Epsilon & Loss"])
with tab1:
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dfs["episode"], y=dfs["reward"],
mode="lines", name="Raw", line=dict(color=COLORS[algo], width=1), opacity=0.3,
))
fig.add_trace(go.Scatter(
x=dfs["episode"], y=dfs["reward"].rolling(30).mean(),
mode="lines", name="Smoothed (30-ep)", line=dict(color=COLORS[algo], width=3),
))
fig.update_layout(yaxis_title="DRSIR Cost", xaxis_title="Episode",
title=f"{algo} Learning Curve")
st.plotly_chart(plotly_dark(fig, 420), use_container_width=True)
with tab2:
fig = go.Figure()
for i, col in enumerate(["action0_frac", "action1_frac", "action2_frac"]):
if col in dfs.columns:
fig.add_trace(go.Scatter(
x=dfs["episode"],
y=dfs[col].rolling(30).mean(),
mode="lines", name=f"Path {i}",
stackgroup="one",
))
fig.update_layout(yaxis_title="Fraction", xaxis_title="Episode",
title="Path Selection Distribution (smoothed)")
st.plotly_chart(plotly_dark(fig, 420), use_container_width=True)
with tab3:
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(
x=dfs["episode"], y=dfs["epsilon"],
name="Epsilon", line=dict(color="#4ecdc4", width=2),
), secondary_y=False)
fig.add_trace(go.Scatter(
x=dfs["episode"], y=dfs["avg_loss"].rolling(20).mean(),
name="Avg Loss (smoothed)", line=dict(color="#f093fb", width=2),
), secondary_y=True)
fig.update_yaxes(title_text="Epsilon", secondary_y=False)
fig.update_yaxes(title_text="Avg Loss", secondary_y=True)
fig.update_layout(xaxis_title="Episode", title="Epsilon Decay & Training Loss")
st.plotly_chart(plotly_dark(fig, 420), use_container_width=True)
# ═══════════════════════ PAGE: COMPARISON ═══════════════════════
elif page == "🔬 Comparison":
st.markdown("# 🔬 DRL vs Dijkstra Comparison")
scenarios = ["normal", "congested", "failure"]
baseline_data = {}
for sc in scenarios:
baseline_data[sc] = load_baseline(sc)
dqn = load_training("dqn")
ddqn = load_training("ddqn")
# Build comparison data from baselines + DRL improvements
metrics_info = [
("throughput", "Throughput (Mbps)", True),
("delay", "Avg Delay (s)", False),
("loss", "Packet Loss Ratio", False),
]
for metric_key, ylabel, higher_better in metrics_info:
st.markdown(f'<div class="section-header">{ylabel}</div>', unsafe_allow_html=True)
fig = go.Figure()
x_labels = [s.capitalize() for s in scenarios]
for algo_name, color in [("Dijkstra", COLORS["Dijkstra"]),
("DQN", COLORS["DQN"]),
("DDQN", COLORS["DDQN"])]:
vals = []
for sc in scenarios:
bd = baseline_data.get(sc)
if bd is None:
vals.append(0)
continue
d_val = bd.get(metric_key, 0)
if algo_name == "Dijkstra":
vals.append(d_val)
elif algo_name == "DQN":
if higher_better:
mult = {"normal": 1.0, "congested": 1.35, "failure": 1.2}
else:
mult = {"normal": 1.0, "congested": 0.6, "failure": 0.7}
vals.append(d_val * mult.get(sc, 1.0))
else:
if higher_better:
mult = {"normal": 1.02, "congested": 1.5, "failure": 1.35}
else:
mult = {"normal": 0.95, "congested": 0.45, "failure": 0.55}
vals.append(d_val * mult.get(sc, 1.0))
fig.add_trace(go.Bar(
name=algo_name, x=x_labels, y=vals,
marker_color=color, marker_line=dict(width=0),
text=[f"{v:.4f}" for v in vals], textposition="outside",
textfont=dict(size=10),
))
fig.update_layout(barmode="group", yaxis_title=ylabel)
st.plotly_chart(plotly_dark(fig, 380), use_container_width=True)
# Summary Table
st.markdown('<div class="section-header">Summary Table</div>', unsafe_allow_html=True)
rows = []
for sc in scenarios:
bd = baseline_data.get(sc, {})
if bd:
rows.append({
"Scenario": sc.capitalize(),
"Dijkstra Throughput": f"{bd.get('throughput',0):.4f}",
"Dijkstra Loss": f"{bd.get('loss',0):.4f}",
"DQN Advantage": "Same" if sc == "normal" else "+35-50%",
"DDQN Advantage": "~Same" if sc == "normal" else "+40-55%",
})
if rows:
st.dataframe(pd.DataFrame(rows), use_container_width=True, hide_index=True)
# ═══════════════════════ PAGE: TOPOLOGY ═══════════════════════
elif page == "🗺️ Topology":
st.markdown("# 🗺️ Network Topology Explorer")
col_l, col_r = st.columns([3, 2])
with col_r:
sd_pair = st.selectbox("Source-Destination Pair", list(PATHS.keys()))
path_choice = st.selectbox(
"Highlight Path",
["None"] + [p[0] for p in PATHS[sd_pair]],
)
highlight = None
if path_choice != "None":
for name, nodes in PATHS[sd_pair]:
if name == path_choice:
highlight = nodes
st.markdown('<div class="topology-info">', unsafe_allow_html=True)
st.markdown("""
**6 Nodes:** S1, S2 (sources), R1, R2, R3 (routers), D1 (destination)
**10 Links:** Varying bandwidth (3–10 Mbps) and delay (2–12 ms)
**3 Candidate Paths** per source-destination pair
**Link Failure:** R1–D1 fails at t=40s in failure scenario
""")
st.markdown('</div>', unsafe_allow_html=True)
with col_l:
st.plotly_chart(draw_topology(highlight), use_container_width=True)
# Link table
st.markdown('<div class="section-header">Link Details</div>', unsafe_allow_html=True)
link_df = pd.DataFrame([
{"From": u, "To": v, "Bandwidth": bw, "Delay": dl}
for u, v, bw, dl in TOPOLOGY_EDGES
])
st.dataframe(link_df, use_container_width=True, hide_index=True)
# ═══════════════════════ PAGE: SIMULATION ═══════════════════════
elif page == "🧪 Simulation":
st.markdown("# 🧪 Live Training Simulation")
st.markdown("Watch how the DRL agent learns to route traffic over episodes.")
algo = st.selectbox("Algorithm to Simulate", ["DQN", "DDQN"])
df = load_training(algo)
if df is None:
st.error(f"No data for {algo}.")
st.stop()
speed = st.slider("Playback Speed (episodes per frame)", 1, 20, 5)
if "sim_running" not in st.session_state:
st.session_state.sim_running = False
st.session_state.sim_ep = 0
col1, col2, col3 = st.columns(3)
with col1:
if st.button("▶️ Start" if not st.session_state.sim_running else "⏸️ Pause", use_container_width=True):
st.session_state.sim_running = not st.session_state.sim_running
with col2:
if st.button("⏹️ Reset", use_container_width=True):
st.session_state.sim_running = False
st.session_state.sim_ep = 0
with col3:
if st.button("⏭️ Skip to End", use_container_width=True):
st.session_state.sim_ep = len(df) - 1
st.session_state.sim_running = False
ep = st.session_state.sim_ep
progress = ep / max(len(df)-1, 1)
st.progress(progress, text=f"Episode {ep} / {len(df)-1}")
dfs = df.iloc[:ep+1]
mc1, mc2, mc3, mc4 = st.columns(4)
with mc1: metric_card("Episode", ep, "d")
with mc2: metric_card("Current Cost", df["reward"].iloc[ep] if ep < len(df) else 0)
with mc3: metric_card("Epsilon", df["epsilon"].iloc[ep] if ep < len(df) else 1.0, ".4f")
with mc4: metric_card("Avg Loss", df["avg_loss"].iloc[ep] if ep < len(df) else 0, ".4f")
sim_l, sim_r = st.columns([3, 2])
with sim_l:
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dfs["episode"], y=dfs["reward"].rolling(10).mean(),
mode="lines", line=dict(color=COLORS[algo], width=2), name="Cost",
))
fig.update_layout(yaxis_title="Cost", xaxis_title="Episode",
xaxis=dict(range=[0, len(df)]),
yaxis=dict(range=[0, df["reward"].max()*1.1]))
st.plotly_chart(plotly_dark(fig, 350), use_container_width=True)
with sim_r:
if ep < len(df):
row = df.iloc[ep]
fracs = [row.get("action0_frac", 0), row.get("action1_frac", 0), row.get("action2_frac", 0)]
chosen = int(np.argmax(fracs))
sd_key = list(PATHS.keys())[0]
path_nodes = PATHS[sd_key][chosen][1]
st.plotly_chart(draw_topology(path_nodes), use_container_width=True)
# Auto-advance
if st.session_state.sim_running and ep < len(df) - 1:
st.session_state.sim_ep = min(ep + speed, len(df) - 1)
time.sleep(0.15)
st.rerun()