Repository navigation
Expand file tree
/
Copy pathbenchmarking.py
More file actions
336 lines (266 loc) · 12.5 KB
/
Copy pathbenchmarking.py
File metadata and controls
336 lines (266 loc) · 12.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
import time
import random
import statistics
import typer
from typing import List, Tuple, Dict, Optional
from rich.console import Console
from rich.table import Table
from rich.progress import track
import networkx as nx
import math
from dataset import create_dataset
from bellman_ford import bellman_ford, extract_cycle
from validation import extract_nx_cycle
console = Console()
def create_random_dataset(num_currencies: int, num_transactions: int, insert_cycle: bool = True) -> Tuple[List[Tuple], int]:
"""
Create a random dataset for benchmarking
Args:
num_currencies: Number of currency nodes
num_transactions: Number of directed edges (transactions)
insert_cycle: Whether to insert a known negative cycle
Returns:
edges: List of tuples (u, v, weight) representing transactions
num_currencies: Number of vertices in the graph
"""
return create_dataset(num_currencies, num_transactions, insert_cycle)
def run_networkx_benchmark(edges: List[Tuple], num_vertices: int) -> Tuple[bool, List, float, float, float]:
"""
Run NetworkX Bellman-Ford implementation with timing
Args:
edges: List of tuples (u, v, weight) representing transactions
num_vertices: Number of vertices in the graph
Returns:
has_cycle: Whether a negative cycle was detected
cycle: The negative cycle (if any)
cycle_weight: Weight of the cycle
profit_factor: Profit factor of the cycle
execution_time: Time taken in seconds
"""
# Create NetworkX DiGraph
G = nx.DiGraph()
G.add_nodes_from(range(num_vertices))
# Add edges
for u, v, weight in edges:
if u < num_vertices and v < num_vertices:
G.add_edge(u, v, weight=weight)
# Time NetworkX implementation
start_time = time.time()
has_cycle = False
cycle = None
cycle_weight = 0
profit_factor = 1.0
try:
# Check for negative cycles
for source in range(min(5, num_vertices)):
nx.bellman_ford_predecessor_and_distance(G, source)
has_cycle = False
except nx.NetworkXUnbounded:
has_cycle = True
# Extract the negative cycle if one exists
cycle, cycle_weight = extract_nx_cycle(G)
if cycle and cycle_weight < 0:
profit_factor = math.exp(-cycle_weight)
end_time = time.time()
execution_time = end_time - start_time
return has_cycle, cycle, cycle_weight, profit_factor, execution_time
def run_our_implementation_benchmark(edges: List[Tuple], num_vertices: int) -> Tuple[bool, List, float, float, float]:
"""
Run our Bellman-Ford implementation with timing
Args:
edges: List of tuples (u, v, weight) representing transactions
num_vertices: Number of vertices in the graph
Returns:
has_cycle: Whether a negative cycle was detected
cycle: The negative cycle (if any)
profit: Profit factor of the cycle
total_weight: Total weight of the cycle
execution_time: Time taken in seconds
"""
# Time our implementation
start_time = time.time()
# Run our Bellman-Ford
_, pred, neg_cycle_start = bellman_ford(edges, num_vertices)
has_cycle = False
cycle = None
profit = 1.0
total_weight = 0.0
if neg_cycle_start is not None:
cycle, profit, total_weight, _ = extract_cycle(pred, neg_cycle_start, edges, num_vertices)
has_cycle = len(cycle) > 1 and profit > 1.0
end_time = time.time()
execution_time = end_time - start_time
return has_cycle, cycle, profit, total_weight, execution_time
def run_single_benchmark(dataset_id: int, num_currencies: int, num_transactions: int, insert_cycle: bool = True) -> Dict:
"""
Run a single benchmark comparison
Args:
dataset_id: Identifier for this benchmark run
num_currencies: Number of currencies in the dataset
num_transactions: Number of transactions in the dataset
insert_cycle: Whether to insert a known negative cycle
Returns:
Dictionary containing benchmark results
"""
# Create dataset
edges, num_currencies = create_random_dataset(num_currencies, num_transactions, insert_cycle)
# Run NetworkX benchmark
nx_has_cycle, nx_cycle, nx_cycle_weight, nx_profit_factor, nx_time = run_networkx_benchmark(edges, num_currencies)
# Run our implementation benchmark
our_has_cycle, our_cycle, our_profit, our_total_weight, our_time = run_our_implementation_benchmark(edges, num_currencies)
# Calculate speedup
speedup = nx_time / our_time if our_time > 0 else float('inf')
# Check agreement
cycles_agree = (nx_has_cycle and our_has_cycle) or (not nx_has_cycle and not our_has_cycle)
return {
'dataset_id': dataset_id,
'num_currencies': num_currencies,
'num_transactions': num_transactions,
'insert_cycle': insert_cycle,
'nx_has_cycle': nx_has_cycle,
'nx_time': nx_time,
'nx_profit_factor': nx_profit_factor,
'our_has_cycle': our_has_cycle,
'our_time': our_time,
'our_profit': our_profit,
'speedup': speedup,
'cycles_agree': cycles_agree
}
def format_time(time_seconds: float) -> str:
"""Format time with appropriate units"""
if time_seconds < 0.001:
return f"{time_seconds*1000000:.2f} μs"
elif time_seconds < 1:
return f"{time_seconds*1000:.2f} ms"
else:
return f"{time_seconds:.4f} s"
def create_benchmark_table(results: List[Dict]) -> Table:
"""Create a rich table for benchmark results"""
table = Table(title="Bellman-Ford Implementation Benchmark Results")
table.add_column("Dataset", style="cyan", no_wrap=True)
table.add_column("Currencies", style="magenta")
table.add_column("Edges", style="magenta")
table.add_column("NetworkX Time", style="green")
table.add_column("Our Time", style="green")
table.add_column("Speedup", style="yellow")
table.add_column("NX Cycle", style="red")
table.add_column("Our Cycle", style="red")
table.add_column("Agree", style="blue")
for result in results:
table.add_row(
f"#{result['dataset_id']}",
str(result['num_currencies']),
str(result['num_transactions']),
format_time(result['nx_time']),
format_time(result['our_time']),
f"{result['speedup']:.2f}x" if result['speedup'] != float('inf') else "∞",
"✓" if result['nx_has_cycle'] else "✗",
"✓" if result['our_has_cycle'] else "✗",
"✓" if result['cycles_agree'] else "✗"
)
return table
def create_summary_table(results: List[Dict]) -> Table:
"""Create a summary table with statistics"""
table = Table(title="Benchmark Summary Statistics")
# Extract timing data
nx_times = [r['nx_time'] for r in results]
our_times = [r['our_time'] for r in results]
speedups = [r['speedup'] for r in results if r['speedup'] != float('inf')]
# Calculate statistics
nx_mean = statistics.mean(nx_times)
nx_median = statistics.median(nx_times)
nx_min = min(nx_times)
nx_max = max(nx_times)
our_mean = statistics.mean(our_times)
our_median = statistics.median(our_times)
our_min = min(our_times)
our_max = max(our_times)
speedup_mean = statistics.mean(speedups) if speedups else 0
speedup_median = statistics.median(speedups) if speedups else 0
# Agreement statistics
agreements = sum(1 for r in results if r['cycles_agree'])
agreement_rate = agreements / len(results) * 100
# Cycle detection statistics
nx_cycles = sum(1 for r in results if r['nx_has_cycle'])
our_cycles = sum(1 for r in results if r['our_has_cycle'])
table.add_column("Metric", style="cyan", no_wrap=True)
table.add_column("NetworkX", style="green")
table.add_column("Our Implementation", style="green")
table.add_column("Speedup", style="yellow")
table.add_row("Mean Time", format_time(nx_mean), format_time(our_mean), f"{speedup_mean:.2f}x")
table.add_row("Median Time", format_time(nx_median), format_time(our_median), f"{speedup_median:.2f}x")
table.add_row("Min Time", format_time(nx_min), format_time(our_min), "-")
table.add_row("Max Time", format_time(nx_max), format_time(our_max), "-")
table.add_row("Cycles Detected", str(nx_cycles), str(our_cycles), "-")
table.add_row("Agreement Rate", f"{agreement_rate:.1f}%", f"{agreement_rate:.1f}%", "-")
return table
def main(
num_datasets: int = typer.Option(10, "--datasets", "-d", help="Number of datasets to benchmark"),
num_currencies: int = typer.Option(100, "--currencies", "-c", help="Number of currencies per dataset"),
num_transactions: int = typer.Option(1000, "--transactions", "-t", help="Number of transactions per dataset"),
insert_cycle: bool = typer.Option(True, "--insert-cycle/--no-insert-cycle", help="Whether to insert a known negative cycle"),
):
"""Main benchmarking function"""
console.print("\n" + "=" * 80, style="bold blue")
console.print(f"{'BELLMAN-FORD IMPLEMENTATION BENCHMARK':^80}", style="bold blue")
console.print("=" * 80, style="bold blue")
console.print(f"\nRunning benchmark on {num_datasets} random datasets:")
console.print(f" • Currencies per dataset: {num_currencies}")
console.print(f" • Transactions per dataset: {num_transactions}")
console.print(f" • Insert known cycle: {insert_cycle}")
console.print()
# Run benchmarks
results = []
for i in track(range(num_datasets), description="Running benchmarks..."):
result = run_single_benchmark(i + 1, num_currencies, num_transactions, insert_cycle)
results.append(result)
# Display results
console.print("\n" + "=" * 80, style="bold green")
console.print("DETAILED RESULTS", style="bold green")
console.print("=" * 80, style="bold green")
benchmark_table = create_benchmark_table(results)
console.print(benchmark_table)
console.print("\n" + "=" * 80, style="bold green")
console.print("SUMMARY STATISTICS", style="bold green")
console.print("=" * 80, style="bold green")
summary_table = create_summary_table(results)
console.print(summary_table)
# Performance analysis
console.print("\n" + "=" * 80, style="bold yellow")
console.print("PERFORMANCE ANALYSIS", style="bold yellow")
console.print("=" * 80, style="bold yellow")
# Calculate overall statistics
nx_times = [r['nx_time'] for r in results]
our_times = [r['our_time'] for r in results]
speedups = [r['speedup'] for r in results if r['speedup'] != float('inf')]
if speedups:
avg_speedup = statistics.mean(speedups)
if avg_speedup > 1:
console.print(f"✓ Our implementation is on average {avg_speedup:.2f}x faster than NetworkX", style="bold green")
else:
console.print(f"⚠ Our implementation is on average {1/avg_speedup:.2f}x slower than NetworkX", style="bold yellow")
else:
console.print("⚠ Unable to calculate average speedup", style="bold yellow")
# Agreement analysis
agreements = sum(1 for r in results if r['cycles_agree'])
agreement_rate = agreements / len(results) * 100
if agreement_rate == 100:
console.print("✓ Perfect agreement on cycle detection between implementations", style="bold green")
elif agreement_rate >= 90:
console.print(f"✓ Good agreement on cycle detection ({agreement_rate:.1f}%)", style="bold green")
elif agreement_rate >= 70:
console.print(f"⚠ Moderate agreement on cycle detection ({agreement_rate:.1f}%)", style="bold yellow")
else:
console.print(f"✗ Poor agreement on cycle detection ({agreement_rate:.1f}%)", style="bold red")
# Cycle detection analysis
nx_cycles = sum(1 for r in results if r['nx_has_cycle'])
our_cycles = sum(1 for r in results if r['our_has_cycle'])
console.print(f"\nCycle Detection Summary:")
console.print(f" • NetworkX detected cycles in {nx_cycles}/{num_datasets} datasets")
console.print(f" • Our implementation detected cycles in {our_cycles}/{num_datasets} datasets")
if nx_cycles == our_cycles:
console.print(" ✓ Both implementations detected the same number of cycles", style="bold green")
else:
console.print(" ⚠ Different number of cycles detected between implementations", style="bold yellow")
if __name__ == "__main__":
typer.run(main)