Repository navigation
Expand file tree
/
Copy pathanalysis_suite.py
More file actions
513 lines (420 loc) · 17 KB
/
Copy pathanalysis_suite.py
File metadata and controls
513 lines (420 loc) · 17 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
"""
analysis_suite.py
Advanced aggregated visualizations for HFFVRPTW solver performance analysis.
Extends the v7 plotter with:
1. Distributional Analysis: Violin plots showing full loss distributions
2. Aggregated Temporal Analysis: Median convergence with IQR bands
3. Advanced Narrative Integration: Pairwise dominance heatmaps
Uses the same aesthetic constants (ALGORITHM_LABELS, PALETTE_V7) as plotter.py
for visual consistency across the entire publication suite.
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
from scipy.interpolate import interp1d
import warnings
warnings.filterwarnings('ignore')
# --- Configuration ---
DATA_FILE = 'results.parquet'
OUTPUT_DIR = 'plots'
IMAGES_DIR = 'plots/images'
PDFS_DIR = 'plots/PDFs'
SUMMARIES_DIR = 'plots/summaries'
# Publication-quality typography (matching plotter.py)
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Source Serif Pro', 'Computer Modern Roman', 'Times New Roman', 'DejaVu Serif']
plt.rcParams['mathtext.fontset'] = 'cm'
# Algorithm names (from plotter.py)
ALGORITHM_LABELS = {
'alns_adaptive_sa': 'ALNS-SA',
'alns_greedy_lns': 'ALNS-Greedy',
'ts_tenure5': 'TS (tenure=5)',
'ts_tenure0': 'TS (tenure=0)'
}
# V7 4-Color Distinct Palette (from plotter.py)
PALETTE_V7 = {
'ALNS-SA': '#0072B2', # Dark Blue
'ALNS-Greedy': '#56B4E9', # Sky Blue
'TS (tenure=5)': '#D55E00', # Vermillion
'TS (tenure=0)': '#E69F00' # Orange
}
def ensure_output_directories():
"""Create output directories if they don't exist."""
Path(OUTPUT_DIR).mkdir(exist_ok=True)
Path(IMAGES_DIR).mkdir(parents=True, exist_ok=True)
Path(PDFS_DIR).mkdir(parents=True, exist_ok=True)
Path(SUMMARIES_DIR).mkdir(parents=True, exist_ok=True)
def load_and_prepare_data():
"""Load the parquet file and add Algorithm labels."""
print("="*80)
print("LOADING DATA")
print("="*80)
try:
df = pd.read_parquet(DATA_FILE)
print(f"✓ Loaded {len(df)} records from {DATA_FILE}")
# Apply algorithm labels
df['Algorithm'] = df['algorithm'].map(ALGORITHM_LABELS)
print(f"✓ Algorithms: {sorted(df['Algorithm'].unique())}")
print(f"✓ Customer sizes: {sorted(df['cust_size'].unique())}")
print(f"✓ Categories: {sorted(df['category'].unique())}")
return df
except FileNotFoundError:
print(f"ERROR: File '{DATA_FILE}' not found.")
print("Please run parser.py first to generate the data file.")
exit(1)
except Exception as e:
print(f"ERROR loading data: {e}")
exit(1)
# ============================================================================
# PART 1: DISTRIBUTIONAL ANALYSIS
# ============================================================================
def generate_violin_plot_by_size(df: pd.DataFrame):
"""
Create violin plots showing final_loss distribution by customer size.
Uses log scale on y-axis to handle different scales.
"""
print("\n[1.2] Generating Violin Plot: Final Loss Distribution by Customer Size")
print("-"*80)
# Create the plot using seaborn.catplot
g = sns.catplot(
data=df,
x='Algorithm',
y='final_loss',
hue='Algorithm',
col='cust_size',
col_wrap=2,
kind='violin',
palette=PALETTE_V7,
inner='box',
legend=False,
height=5,
aspect=1.2
)
# Set log scale on y-axis for all facets
for ax in g.axes.flat:
ax.set_yscale('log')
ax.set_xlabel('Algorithm', fontsize=11)
ax.set_ylabel('Final Loss (log scale)', fontsize=11)
ax.tick_params(axis='x', rotation=45)
ax.grid(True, which="major", axis='y', linestyle=':', linewidth=0.7, color='lightgray', alpha=0.7)
# Set main title
g.fig.suptitle('Final Loss Distribution by Customer Size',
fontsize=16, fontweight='bold', y=1.02)
# Adjust layout
plt.tight_layout()
# Save
filename = "distribution_by_size"
g.savefig(Path(IMAGES_DIR) / f"{filename}.png", dpi=300, bbox_inches='tight')
g.savefig(Path(PDFS_DIR) / f"{filename}.pdf", bbox_inches='tight')
plt.close()
print(f"✓ Saved: {filename}.png and {filename}.pdf")
def generate_violin_plot_by_category(df: pd.DataFrame):
"""
Create violin plots showing final_loss distribution by problem category.
Uses log scale on y-axis to handle different scales.
"""
print("\n[1.3] Generating Violin Plot: Final Loss Distribution by Problem Category")
print("-"*80)
# Create the plot using seaborn.catplot
g = sns.catplot(
data=df,
x='Algorithm',
y='final_loss',
hue='Algorithm',
col='category',
col_wrap=3,
kind='violin',
palette=PALETTE_V7,
inner='box',
legend=False,
height=4,
aspect=1.2
)
# Set log scale on y-axis for all facets
for ax in g.axes.flat:
ax.set_yscale('log')
ax.set_xlabel('Algorithm', fontsize=10)
ax.set_ylabel('Final Loss (log scale)', fontsize=10)
ax.tick_params(axis='x', rotation=45)
ax.grid(True, which="major", axis='y', linestyle=':', linewidth=0.7, color='lightgray', alpha=0.7)
# Set main title
g.fig.suptitle('Final Loss Distribution by Problem Category',
fontsize=16, fontweight='bold', y=1.02)
# Adjust layout
plt.tight_layout()
# Save
filename = "distribution_by_category"
g.savefig(Path(IMAGES_DIR) / f"{filename}.png", dpi=300, bbox_inches='tight')
g.savefig(Path(PDFS_DIR) / f"{filename}.pdf", bbox_inches='tight')
plt.close()
print(f"✓ Saved: {filename}.png and {filename}.pdf")
# ============================================================================
# PART 2: AGGREGATED TEMPORAL ANALYSIS
# ============================================================================
def prepare_aggregated_convergence_data(df: pd.DataFrame):
"""
Prepare interpolated data for aggregated convergence plots.
Creates a standardized time axis and interpolates all 944 runs.
"""
print("\n" + "="*80)
print("PART 2: AGGREGATED TEMPORAL ANALYSIS")
print("="*80)
print("\n[2.1] Preparing Aggregated Convergence Data")
print("-"*80)
print("Creating standardized time axis and interpolating all runs...")
# Define standardized time axis (log-spaced from 0.1s to 600s)
time_axis = np.logspace(np.log10(0.1), np.log10(600), 100)
all_interpolated_data = []
# Iterate through all rows
for idx, row in df.iterrows():
row_num = len(all_interpolated_data) + 1
if row_num % 100 == 0:
print(f" Processing row {row_num}/{len(df)}...", end='\r')
time_history = np.array(row['time_history'])
loss_history = np.array(row['loss_history'])
# Handle duplicate time values by keeping only the last (best) loss at each time
df_temp = pd.DataFrame({'time': time_history, 'loss': loss_history})
df_temp = df_temp.drop_duplicates(subset='time', keep='last')
time_history = df_temp['time'].values
loss_history = df_temp['loss'].values
# Create pandas Series for forward-fill interpolation
# This respects the step-function nature of "best-so-far" values
series = pd.Series(loss_history, index=time_history)
# Reindex with new time axis using forward-fill
interpolated = series.reindex(
series.index.union(time_axis)
).sort_index().ffill().reindex(time_axis)
# Create temporary DataFrame
temp_df = pd.DataFrame({
'Time': time_axis,
'Interpolated_Loss': interpolated.values,
'Algorithm': row['Algorithm'],
'cust_size': row['cust_size'],
'category': row['category']
})
all_interpolated_data.append(temp_df)
# Concatenate all DataFrames
aggregated_df = pd.concat(all_interpolated_data, ignore_index=True)
print(f"\n✓ Created aggregated dataset with {len(aggregated_df)} rows")
print(f" (100 time points × {len(df)} runs = {len(aggregated_df)} total)")
return aggregated_df
def generate_aggregated_convergence_by_size(aggregated_df: pd.DataFrame):
"""
Create aggregated convergence plots by customer size.
Shows median performance with IQR bands.
"""
print("\n[2.2] Generating Aggregated Convergence Plot by Customer Size")
print("-"*80)
# Create the plot using seaborn.relplot
g = sns.relplot(
data=aggregated_df,
x='Time',
y='Interpolated_Loss',
hue='Algorithm',
col='cust_size',
col_wrap=2,
kind='line',
palette=PALETTE_V7,
estimator=np.median,
errorbar=('pi', 50), # 25th to 75th percentile (IQR)
height=5,
aspect=1.2,
linewidth=2.5,
err_kws={'alpha': 0.3}
)
# Set log scale on both axes for all facets
for ax in g.axes.flat:
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Time (seconds)', fontsize=11)
ax.set_ylabel('Objective Value (log scale)', fontsize=11)
ax.grid(True, which="major", linestyle=':', linewidth=0.7, color='lightgray', alpha=0.7)
# Set main title
g.fig.suptitle('Aggregated Median Convergence by Customer Size (IQR Bands)',
fontsize=16, fontweight='bold', y=1.02)
# Move legend to better position (if legend exists)
if hasattr(g, '_legend') and g._legend is not None:
g._legend.set_bbox_to_anchor((1.05, 0.5))
g._legend.set_title('Algorithm')
# Adjust layout
plt.tight_layout()
# Save
filename = "convergence_aggregated_by_size"
g.savefig(Path(IMAGES_DIR) / f"{filename}.png", dpi=300, bbox_inches='tight')
g.savefig(Path(PDFS_DIR) / f"{filename}.pdf", bbox_inches='tight')
plt.close()
print(f"✓ Saved: {filename}.png and {filename}.pdf")
def generate_aggregated_convergence_by_category(aggregated_df: pd.DataFrame):
"""
Create aggregated convergence plots by problem category.
Shows median performance with IQR bands.
"""
print("\n[2.3] Generating Aggregated Convergence Plot by Category")
print("-"*80)
# Create the plot using seaborn.relplot
g = sns.relplot(
data=aggregated_df,
x='Time',
y='Interpolated_Loss',
hue='Algorithm',
col='category',
col_wrap=3,
kind='line',
palette=PALETTE_V7,
estimator=np.median,
errorbar=('pi', 50), # 25th to 75th percentile (IQR)
height=4,
aspect=1.2,
linewidth=2.5,
err_kws={'alpha': 0.3}
)
# Set log scale on both axes for all facets
for ax in g.axes.flat:
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Time (seconds)', fontsize=10)
ax.set_ylabel('Objective Value (log scale)', fontsize=10)
ax.grid(True, which="major", linestyle=':', linewidth=0.7, color='lightgray', alpha=0.7)
# Set main title
g.fig.suptitle('Aggregated Median Convergence by Problem Category (IQR Bands)',
fontsize=16, fontweight='bold', y=1.02)
# Move legend to better position (if legend exists)
if hasattr(g, '_legend') and g._legend is not None:
g._legend.set_bbox_to_anchor((1.05, 0.5))
g._legend.set_title('Algorithm')
# Adjust layout
plt.tight_layout()
# Save
filename = "convergence_aggregated_by_category"
g.savefig(Path(IMAGES_DIR) / f"{filename}.png", dpi=300, bbox_inches='tight')
g.savefig(Path(PDFS_DIR) / f"{filename}.pdf", bbox_inches='tight')
plt.close()
print(f"✓ Saved: {filename}.png and {filename}.pdf")
# ============================================================================
# PART 3: ADVANCED NARRATIVE INTEGRATION
# ============================================================================
def generate_pairwise_dominance_heatmap(df: pd.DataFrame):
"""
Create a pairwise dominance heatmap showing win rates.
Answers: "What percentage of the time does Algorithm i beat Algorithm j?"
"""
print("\n" + "="*80)
print("PART 3: ADVANCED NARRATIVE INTEGRATION")
print("="*80)
print("\n[3.1] Generating Pairwise Dominance Heatmap")
print("-"*80)
# Pivot to get final_loss for each algorithm in columns
pivoted = df.pivot_table(
index='instance',
columns='Algorithm',
values='final_loss'
)
print(f"Analyzing {len(pivoted)} instances...")
# Get sorted algorithm list
algorithms = sorted(pivoted.columns)
# Create win matrix
win_matrix = pd.DataFrame(
index=algorithms,
columns=algorithms,
dtype=float
)
# Calculate win rates
for row_alg in algorithms:
for col_alg in algorithms:
if row_alg == col_alg:
# Diagonal: algorithm vs itself = 50% (tie)
win_matrix.loc[row_alg, col_alg] = 50.0
else:
# Win rate: percentage where row_alg < col_alg (lower is better)
# Account for ties by giving 0.5 to each algorithm
wins = (pivoted[row_alg] < pivoted[col_alg]).mean()
ties = (pivoted[row_alg] == pivoted[col_alg]).mean()
win_rate = (wins + 0.5 * ties) * 100
win_matrix.loc[row_alg, col_alg] = win_rate
# Convert to numeric
win_matrix = win_matrix.astype(float)
# Create the heatmap
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(
win_matrix,
annot=True,
fmt=".1f",
cmap='vlag',
center=50.0,
vmin=0,
vmax=100,
square=True,
linewidths=0.5,
cbar_kws={'label': 'Win Rate (%)'},
ax=ax
)
ax.set_title('Pairwise Dominance Heatmap (% Wins)\nRow Algorithm beats Column Algorithm',
fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel('Column Algorithm (Opponent)', fontsize=12)
ax.set_ylabel('Row Algorithm', fontsize=12)
# Adjust layout
plt.tight_layout()
# Save
filename = "heatmap_dominance_overall"
plt.savefig(Path(IMAGES_DIR) / f"{filename}.png", dpi=300, bbox_inches='tight')
plt.savefig(Path(PDFS_DIR) / f"{filename}.pdf", bbox_inches='tight')
plt.close()
print(f"✓ Saved: {filename}.png and {filename}.pdf")
# Print summary
print("\nWin Rate Summary:")
print("-"*80)
for row_alg in algorithms:
avg_win_rate = win_matrix.loc[row_alg].drop(row_alg).mean()
print(f" {row_alg}: {avg_win_rate:.1f}% average win rate vs others")
return win_matrix
# ============================================================================
# MAIN EXECUTION
# ============================================================================
def main():
"""Main execution pipeline for advanced analysis suite."""
print("="*80)
print("ADVANCED ANALYSIS SUITE FOR HFFVRPTW SOLVER PERFORMANCE")
print("="*80)
print("\nExtending v7 plotter with:")
print(" 1. Distributional Analysis (Violin Plots)")
print(" 2. Aggregated Temporal Analysis (Median Convergence)")
print(" 3. Advanced Narrative Integration (Dominance Heatmaps)")
print("\n")
# Ensure directories exist
ensure_output_directories()
# Load data
df = load_and_prepare_data()
# PART 1: Distributional Analysis
generate_violin_plot_by_size(df)
generate_violin_plot_by_category(df)
# PART 2: Aggregated Temporal Analysis
aggregated_df = prepare_aggregated_convergence_data(df)
generate_aggregated_convergence_by_size(aggregated_df)
generate_aggregated_convergence_by_category(aggregated_df)
# PART 3: Advanced Narrative Integration
generate_pairwise_dominance_heatmap(df)
# Final summary
print("\n" + "="*80)
print("ADVANCED ANALYSIS SUITE COMPLETE!")
print("="*80)
print(f"\nGenerated files in {Path(OUTPUT_DIR).absolute()}:")
print(f"\nPNG files in {Path(IMAGES_DIR).absolute()}:")
print(" • distribution_by_size.png")
print(" • distribution_by_category.png")
print(" • convergence_aggregated_by_size.png")
print(" • convergence_aggregated_by_category.png")
print(" • heatmap_dominance_overall.png")
print(f"\nPDF files in {Path(PDFS_DIR).absolute()}:")
print(" • distribution_by_size.pdf")
print(" • distribution_by_category.pdf")
print(" • convergence_aggregated_by_size.pdf")
print(" • convergence_aggregated_by_category.pdf")
print(" • heatmap_dominance_overall.pdf")
print(f"\nCSV files in {Path(SUMMARIES_DIR).absolute()}:")
print(" • mean_vs_median_comparison.csv")
print("\nAll visualizations use v7 aesthetic constants for consistency.")
print("="*80)
if __name__ == "__main__":
main()