|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
import plotly.express as px |
|
|
from datetime import datetime, timedelta |
|
|
from prophet import Prophet |
|
|
import networkx as nx |
|
|
from eventlet import event |
|
|
|
|
|
|
|
|
def generate_data(): |
|
|
schools = [f"School_{i}" for i in range(1, 11)] |
|
|
time_range = [datetime.now() - timedelta(hours=i) for i in range(24)] |
|
|
|
|
|
data = [] |
|
|
for school in schools: |
|
|
for time in time_range: |
|
|
bandwidth_usage = np.random.normal(loc=50, scale=20) |
|
|
bandwidth_usage = max(1, min(150, bandwidth_usage)) |
|
|
data.append([school, time, bandwidth_usage]) |
|
|
|
|
|
|
|
|
for _ in range(10): |
|
|
school = np.random.choice(schools) |
|
|
time = np.random.choice(time_range) |
|
|
bandwidth_usage = np.random.choice([1, 160]) |
|
|
data.append([school, time, bandwidth_usage]) |
|
|
|
|
|
df = pd.DataFrame(data, columns=["School", "Timestamp", "Bandwidth_Usage"]) |
|
|
df.to_csv("network_data.csv", index=False) |
|
|
return df |
|
|
|
|
|
|
|
|
df = generate_data() |
|
|
|
|
|
def detect_anomalies(df): |
|
|
|
|
|
Q1 = df['Bandwidth_Usage'].quantile(0.25) |
|
|
Q3 = df['Bandwidth_Usage'].quantile(0.75) |
|
|
IQR = Q3 - Q1 |
|
|
anomalies = df[(df['Bandwidth_Usage'] < (Q1 - 1.5 * IQR)) | (df['Bandwidth_Usage'] > (Q3 + 1.5 * IQR))] |
|
|
return anomalies |
|
|
|
|
|
def train_prophet(df): |
|
|
df["Timestamp"] = pd.to_datetime(df["Timestamp"]) |
|
|
df_numeric = df[["Timestamp", "Bandwidth_Usage"]] |
|
|
df_prophet = df_numeric.groupby("Timestamp").mean().reset_index() |
|
|
df_prophet.columns = ["ds", "y"] |
|
|
|
|
|
model = Prophet() |
|
|
model.fit(df_prophet) |
|
|
|
|
|
future = model.make_future_dataframe(periods=5, freq="H") |
|
|
forecast = model.predict(future) |
|
|
|
|
|
return forecast |
|
|
|
|
|
def allocate_bandwidth(demand_predictions, total_bandwidth=500): |
|
|
total_demand = sum(demand_predictions.values()) |
|
|
allocation = {school: (demand / total_demand) * total_bandwidth for school, demand in demand_predictions.items()} |
|
|
return allocation |
|
|
|
|
|
def sdn_load_balancer(network_graph, demand_predictions): |
|
|
shortest_paths = {} |
|
|
for school in demand_predictions.keys(): |
|
|
shortest_paths[school] = nx.shortest_path(network_graph, source='Central_Node', target=school, weight='energy_efficiency') |
|
|
return shortest_paths |
|
|
|
|
|
|
|
|
graph = nx.Graph() |
|
|
|
|
|
graph.add_edges_from([ |
|
|
('Central_Node', 'Node_A', {'energy_efficiency': 1}), ('Central_Node', 'Node_B', {'energy_efficiency': 2}), |
|
|
('Node_A', 'School_1', {'energy_efficiency': 1}), ('Node_A', 'School_2', {'energy_efficiency': 1}), ('Node_A', 'Node_C', {'energy_efficiency': 2}), |
|
|
('Node_B', 'School_3', {'energy_efficiency': 1}), ('Node_B', 'School_4', {'energy_efficiency': 1}), ('Node_B', 'Node_D', {'energy_efficiency': 2}), |
|
|
('Node_C', 'School_5', {'energy_efficiency': 1}), ('Node_C', 'School_6', {'energy_efficiency': 1}), |
|
|
('Node_D', 'School_7', {'energy_efficiency': 1}), ('Node_D', 'School_8', {'energy_efficiency': 1}), |
|
|
('Node_A', 'Node_B', {'energy_efficiency': 1}), ('Node_C', 'Node_D', {'energy_efficiency': 1}), |
|
|
('Node_C', 'School_9', {'energy_efficiency': 1}), ('Node_D', 'School_10', {'energy_efficiency': 1}) |
|
|
]) |
|
|
|
|
|
|
|
|
forecast = train_prophet(df) |
|
|
demand_predictions = {f"School_{i}": np.random.randint(20, 100) for i in range(1, 11)} |
|
|
bandwidth_allocation = allocate_bandwidth(demand_predictions) |
|
|
network_routes = sdn_load_balancer(graph, demand_predictions) |
|
|
anomalies = detect_anomalies(df) |
|
|
|
|
|
|
|
|
st.set_page_config(layout="wide", page_title="Smart Network Allocation", page_icon="🌐") |
|
|
st.title("📡 Smart Network Resource Allocation with SDN Load Balancing") |
|
|
st.markdown("---") |
|
|
|
|
|
col1, col2 = st.columns([2, 1]) |
|
|
|
|
|
with col1: |
|
|
st.subheader("📊 Bandwidth Usage Over Time") |
|
|
fig = px.line(df, x="Timestamp", y="Bandwidth_Usage", color="School", template="plotly_dark") |
|
|
st.plotly_chart(fig, use_container_width=True) |
|
|
|
|
|
with col2: |
|
|
st.subheader("🔍 Predicted Bandwidth Allocations") |
|
|
st.dataframe(pd.DataFrame(list(bandwidth_allocation.items()), columns=["School", "Allocated Bandwidth (Mbps)"])) |
|
|
|
|
|
st.markdown("---") |
|
|
|
|
|
col3, col4 = st.columns([1, 1]) |
|
|
|
|
|
with col3: |
|
|
st.subheader("🔗 SDN-based Load Balancing Routes (Energy Efficient)") |
|
|
st.dataframe(pd.DataFrame([(school, ' -> '.join(path)) for school, path in network_routes.items()], columns=["School", "Route"])) |
|
|
|
|
|
with col4: |
|
|
st.subheader("⚠️ Detected Anomalies") |
|
|
if anomalies.empty: |
|
|
st.success("No anomalies detected") |
|
|
else: |
|
|
st.dataframe(anomalies) |
|
|
|
|
|
st.markdown("---") |
|
|
st.caption("Developed for optimized network performance, intelligent bandwidth allocation, and anomaly detection using machine learning and SDN principles ⚡") |
|
|
|