-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
135 lines (104 loc) · 4.6 KB
/
analysis.py
File metadata and controls
135 lines (104 loc) · 4.6 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
import pandas as pd
import matplotlib.pyplot as plt
import re
def parse_results(file_path):
data = []
pattern = (r"Parameters file: (.*?) \| (.*?) \| Solution: (\d+) of profit.*?Elapsed time: ([\d.]+) s \| CPU time: "
r"([\d.]+) s \| Peak memory: (\d+) KB \| Number of visits: (\d+)")
with open(file_path, "r") as f:
for line in f:
match = re.search(pattern, line)
if match:
data.append({
"File": match.group(1),
"Mode": match.group(2).strip(),
"Profit": int(match.group(3)),
"Elapsed": float(match.group(4)),
"CPU": float(match.group(5)),
"Memory": int(match.group(6)),
"Visits": int(match.group(7))
})
return pd.DataFrame(data)
def analyze(file_path="results.txt"):
df = parse_results(file_path)
if df.empty:
print("No data found in results.txt")
return
##################################################
# Calculate statistics for elapsed time
stats = df.groupby("Mode")["Elapsed"].agg(['mean', 'std', 'min', 'max']).reset_index()
stats.columns = ["Algorithm", "Mean", "StdDev", "Min", "Max"]
stats = stats.sort_values("Mean", ascending=False) # Sort by mean time
print(f"{'ALGORITHM':<12} | {'MEAN (s)':<12} | {'STD DEV':<12} | {'MIN':<12} | {'MAX':<12}")
for _, row in stats.iterrows():
print(f"{row['Algorithm']:<12} | "
f"{row['Mean']:<12.5f} | "
f"{row['StdDev']:<12.5f} | "
f"{row['Min']:<12.5f} | "
f"{row['Max']:<12.5f}")
print("-" * 95)
##################################################
# Visits stats
v_stats = df.groupby("Mode")["Visits"].mean()
# Reference values
r_avg = v_stats.get("R", 1)
print(f"{'ALGORITHM':<12} | {'AVG VISITS':<15} | {'RED. vs R':<15} | {'% EXPLORED':<12}")
for mode in v_stats.sort_values(ascending=False).index:
visits = v_stats[mode]
# Calculation logic
red_r = f"{r_avg / visits:,.1f}x" if mode != "R" else "---"
pct = (visits / r_avg) * 100
print(f"{mode:<12} | {int(visits):<15,} | {red_r:<15} | {pct:.7f}%")
print("-" * 95)
##################################################
# Memory usage stats
m_stats = df.groupby("Mode")["Memory"].mean()
# Reference values for comparison
r_mem = m_stats.get("R", 1)
rtd_mem = m_stats.get("RTD", 1)
print(f"{'ALGORITHM':<12} | {'AVG MEM (KB)':<15} | {'vs BASELINE':<15} | {'vs RTD':<15}")
# Sort by memory usage descending
for mode in m_stats.sort_values(ascending=False).index:
mem = m_stats[mode]
vs_r = f"{mem / r_mem:.2f}x"
vs_rtd = f"{mem / rtd_mem:.3f}x"
print(f"{mode:<12} | {int(mem):<15,} | {vs_r:<15} | {vs_rtd:<15}")
# Plotting performance (log scale because first recursive is slower)
plt.figure(figsize=(12, 6))
modes = df["Mode"].unique()
for mode in modes:
mode_data = df[df["Mode"] == mode].sort_values("File")
plt.plot(mode_data["File"], mode_data["Elapsed"], marker='o', label=f"{mode} Elapsed")
plt.yscale('log')
plt.xticks(rotation=45, ha='right')
plt.ylabel("Time (seconds) - Log scale")
plt.title("Execution time comparison across implementations")
plt.legend()
plt.tight_layout()
plt.savefig("time_comparison.png")
plt.show()
# Average memory and visits comparison
comparison = df.groupby("Mode")[["Memory", "Visits"]].mean().reset_index()
# Sorting by memory
comparison_mem = comparison.sort_values("Memory", ascending=False)
# Bar chart for memory
plt.clf()
plt.bar(comparison_mem["Mode"], comparison_mem["Memory"], color='skyblue', edgecolor='black')
plt.ylabel("Average peak memory (kb)")
plt.title("Average peak memory usage by implementation")
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.savefig("memory_comparison.png")
# 2. Bar chart for Visits (log Scale because of first recursive implementation)
comparison_vis = comparison.sort_values("Visits", ascending=False)
plt.clf()
plt.bar(comparison_vis["Mode"], comparison_vis["Visits"], color='salmon', edgecolor='black')
plt.yscale('log')
plt.ylabel("Average number of nodes visited (Log scale)")
plt.title("Average search space exploration (node visits)")
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.savefig("visits_comparison.png")
print(comparison.to_string(index=False))
if __name__ == "__main__":
analyze()