-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.py
More file actions
161 lines (131 loc) · 5.58 KB
/
Network.py
File metadata and controls
161 lines (131 loc) · 5.58 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
import pandas as pd
import networkx as nx
from itertools import combinations
import pandas as pd
# Step 1: Load Network from File
def load_network(csv_path):
df = pd.read_csv("/Users/matthewkolakowski/Documents/network_data.csv")
G = nx.DiGraph() # Use DiGraph to model bi-directional links
for _, row in df.iterrows():
# Add both directions since the network is bi-directional
G.add_edge(row['Start'], row['End'], capacity=row['Capacity'], weight=row['Weight'])
G.add_edge(row['End'], row['Start'], capacity=row['Capacity'], weight=row['Weight'])
return G
# Step 2: Determine Paths through Network
def find_shortest_path(graph, source, destination):
return nx.dijkstra_path(graph, source, destination, weight='weight')
# Step 3: Load Traffic Data
def load_traffic(csv_path):
return pd.read_csv(csv_path)
# Step 4: Apply Traffic Flow and Model Traffic Load
def model_traffic_load(graph, traffic_df):
link_load = {edge: 0 for edge in graph.edges()}
for _, demand in traffic_df.iterrows():
path = find_shortest_path(graph, demand['Source'], demand['Destination'])
# Add demand to each link in the path
for i in range(len(path)-1):
link_load[(path[i], path[i+1])] += demand['Demand']
return link_load
return link_load
# Step 5: Determine Worst Case Failure
def worst_case_failure(graph, traffic_df):
original_graph = graph.copy()
worst_case = {'link': None, 'unroutable': float('inf'), 'over_capacity': {}}
for edge in original_graph.edges():
graph = original_graph.copy()
graph.remove_edge(*edge)
link_load = model_traffic_load(graph, traffic_df)
over_capacity = {e: load for e, load in link_load.items() if load > graph[e[0]][e[1]]['capacity']}
unroutable = sum(1 for load in over_capacity.values())
# Check if this failure scenario is worse than the previous worst case
if unroutable < worst_case['unroutable'] or \
(unroutable == worst_case['unroutable'] and len(over_capacity) > len(worst_case['over_capacity'])):
worst_case = {'link': edge, 'unroutable': unroutable, 'over_capacity': over_capacity}
return worst_case
# Usage Example
network_csv = '/Users/matthewkolakowski/Documents/network_data.csv'
# Define load_traffic function
def load_traffic(csv_path):
return pd.read_csv(csv_path)
# Load network and traffic
# Define model_traffic_load function
def model_traffic_load(graph, traffic_df):
link_load = {edge: 0 for edge in graph.edges()}
for _, demand in traffic_df.iterrows():
path = find_shortest_path(graph, demand['Source'], demand['Destination'])
# Add demand to each link in the path
for i in range(len(path)-1):
link_load[(path[i], path[i+1])] += demand['Demand']
return link_load
G = load_network(network_csv)
traffic_csv = '/Users/matthewkolakowski/Documents/traffic_data.csv'
traffic_df = load_traffic(traffic_csv)
# Model traffic load
link_load = model_traffic_load(G, traffic_df)
print("Link Load:")
for link, load in link_load.items():
print(f"{link}: {load} units")
# Determine worst case failure
wcf = worst_case_failure(G, traffic_df)
print("\nWorst Case Failure:")
print(f"Link: {wcf['link']}")
print(f"Unroutable: {wcf['unroutable']}")
print(f"Link Loads during WCF:")
# Define model_traffic_load function
def model_traffic_load(graph, traffic_df):
link_load = {edge: 0 for edge in graph.edges()}
for _, demand in traffic_df.iterrows():
path = find_shortest_path(graph, demand['Source'], demand['Destination'])
# Add demand to each link in the path
for i in range(len(path)-1):
link_load[(path[i], path[i+1])] += demand['Demand']
return link_load
# Load network and traffic
def load_traffic(csv_path):
return pd.read_csv(csv_path)
traffic_csv = '/Users/matthewkolakowski/Documents/traffic_data.csv'
G = load_network(network_csv)
traffic_df = load_traffic(traffic_csv)
# Model traffic load
link_load = model_traffic_load(G, traffic_df)
print("Link Load:")
for link, load in link_load.items():
print(f"{link}: {load} units")
# Determine worst case failure
wcf = worst_case_failure(G, traffic_df)
for link, load in wcf['over_capacity'].items():
print(f"{link}: {load} units")
print("\nWorst Case Failure:")
print(f"Link: {wcf['link']}")
print(f"Unroutable: {wcf['unroutable']}")
print(f"Link Loads during WCF:")
def load_traffic(csv_path):
return pd.read_csv(csv_path)
traffic_csv = '/Users/matthewkolakowski/Documents/traffic_data.csv'
# Load network and traffic
G = load_network(network_csv)
traffic_df = load_traffic(traffic_csv)
# Define model_traffic_load function
def model_traffic_load(graph, traffic_df):
link_load = {edge: 0 for edge in graph.edges()}
for _, demand in traffic_df.iterrows():
path = find_shortest_path(graph, demand['Source'], demand['Destination'])
# Add demand to each link in the path
for i in range(len(path)-1):
link_load[(path[i], path[i+1])] += demand['Demand']
return link_load
# Model traffic load
link_load = model_traffic_load(G, traffic_df)
print("Link Load:")
for link, load in link_load.items():
print(f"{link}: {load} units")
for link, load in wcf['over_capacity'].items():
print(f"{link}: {load} units")
# Determine worst case failure
wcf = worst_case_failure(G, traffic_df)
print("\nWorst Case Failure:")
print(f"Link: {wcf['link']}")
print(f"Unroutable: {wcf['unroutable']}")
print(f"Link Loads during WCF:")
for link, load in wcf['over_capacity'].items():
print(f"{link}: {load} units")