-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.py
More file actions
735 lines (591 loc) · 27.7 KB
/
benchmark.py
File metadata and controls
735 lines (591 loc) · 27.7 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#!/usr/bin/env python3
"""
Benchmark script: aggregates objective values and execution times from results/*/sol01..07.json
and produces professional academic-style matplotlib figures plus a CSV summary.
Features:
- Publication-ready plots with IEEE/ACM style
- Combined objective and time comparison plots
- Statistical analysis and performance metrics
- Multiple export formats (PNG, PDF)
- Performance ratio analysis
Usage (default scans ./results and saves into ./plots):
python benchmark.py
Options:
python benchmark.py --results-dir results --save-dir plots --no-show --log-objective --style academic
"""
from __future__ import annotations
import argparse
import json
import os
from typing import List, Optional, Tuple
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.gridspec import GridSpec
import numpy as np
import pandas as pd
from code.tools.utils import load_solution
# Methods and levels expected
DEFAULT_METHODS = ["exact", "alns", "pso", "sa"]
DEFAULT_LEVELS = [f"{i:02d}" for i in range(1, 8)] # 01..07
# Academic color palette (colorblind-friendly)
ACADEMIC_COLORS = {
'exact': '#0173B2', # Blue
'alns': '#DE8F05', # Orange
'pso': '#029E73', # Green
'sa': '#CC78BC', # Purple
}
# Marker styles for better distinction
MARKERS = {
'exact': 'o',
'alns': 's',
'pso': '^',
'sa': 'D',
}
def extract_metrics(sol: Optional[dict]) -> Tuple[Optional[float], Optional[float], Optional[str], Optional[bool]]:
"""Extract (objective, time, status, valid) from a loaded solution object.
- Prefer report.calculated_objective if present (more consistent with validation).
- If missing, fallback to top-level objective.
- Time is taken from top-level 'time' if present.
"""
if not sol:
return None, None, None, None
status = sol.get("status")
time_val = sol.get("time")
# Prefer validated objective when available
obj = None
report = sol.get("report")
valid = None
if isinstance(report, dict):
valid = report.get("valid")
calc = report.get("calculated_objective")
if isinstance(calc, (int, float)):
obj = float(calc)
if obj is None:
v = sol.get("objective")
obj = float(v) if isinstance(v, (int, float)) else None
return obj, (float(time_val) if isinstance(time_val, (int, float)) else None), status, (bool(valid) if isinstance(valid, bool) else None)
def collect_results(results_dir: str, methods: List[str], levels: List[str]) -> pd.DataFrame:
"""Collect metrics into a tidy DataFrame with columns:
['method', 'level', 'objective', 'time', 'status', 'valid']
"""
rows = []
for method in methods:
for level in levels:
path = os.path.join(results_dir, method, f"sol{level}.json")
sol = load_solution(path)
objective, time_val, status, valid = extract_metrics(sol)
rows.append(
{
"method": method,
"level": level,
"objective": objective,
"time": time_val,
"status": status,
"valid": valid,
"exists": sol is not None,
}
)
df = pd.DataFrame(rows)
return df
def ensure_dir(path: str) -> None:
os.makedirs(path, exist_ok=True)
def set_academic_style():
"""Configure matplotlib for academic publication style."""
plt.style.use('seaborn-v0_8-paper')
# Set font properties
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = ['Times New Roman', 'DejaVu Serif']
mpl.rcParams['font.size'] = 10
mpl.rcParams['axes.labelsize'] = 11
mpl.rcParams['axes.titlesize'] = 12
mpl.rcParams['xtick.labelsize'] = 9
mpl.rcParams['ytick.labelsize'] = 9
mpl.rcParams['legend.fontsize'] = 9
# Grid and axes
mpl.rcParams['grid.alpha'] = 0.3
mpl.rcParams['grid.linestyle'] = '--'
mpl.rcParams['axes.grid'] = True
mpl.rcParams['axes.axisbelow'] = True
# Line and marker properties
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.markersize'] = 7
# Figure properties
mpl.rcParams['figure.dpi'] = 100
mpl.rcParams['savefig.dpi'] = 300
mpl.rcParams['savefig.bbox'] = 'tight'
mpl.rcParams['savefig.pad_inches'] = 0.1
def plot_objectives(df: pd.DataFrame, methods: List[str], levels: List[str],
save_dir: str, use_log: bool, show: bool, style: str = 'standard') -> str:
"""Line plot of objectives across levels for each method with academic styling."""
ensure_dir(save_dir)
if style == 'academic':
set_academic_style()
fig, ax = plt.subplots(figsize=(8, 5))
x_ticks = np.arange(len(levels))
# Plot each method
for method in methods:
sub = df[(df.method == method) & (df.level.isin(levels))].copy()
sub_idx = sub.set_index("level").reindex(levels)
y_vals = sub_idx["objective"].values
# Select color and marker
color = ACADEMIC_COLORS.get(method, None) if style == 'academic' else None
marker = MARKERS.get(method, 'o') if style == 'academic' else 'o'
# Check for invalid solutions
label = method.upper()
valid_series = sub_idx.get("valid", pd.Series([True] * len(levels)))
has_invalid = valid_series.fillna(True).eq(False).any()
if has_invalid:
label = f"{method.upper()} (⚠ invalid)"
# Plot with error handling for missing values
mask = ~np.isnan(y_vals)
ax.plot(x_ticks[mask], y_vals[mask], marker=marker, label=label,
color=color, linewidth=2, markersize=7, markeredgewidth=0.5,
markeredgecolor='white', alpha=0.9)
# Mark best results with a star
if len(y_vals[mask]) > 0:
best_idx = np.nanargmin(y_vals)
if not np.isnan(y_vals[best_idx]):
ax.plot(x_ticks[best_idx], y_vals[best_idx], marker='*',
color=color, markersize=15, markeredgewidth=1,
markeredgecolor='gold', zorder=10)
# Formatting
ax.set_xticks(x_ticks)
ax.set_xticklabels([f"N{l}" for l in levels]) # N for Niveau
ax.set_xlabel("Niveau d'instance", fontweight='bold')
ax.set_ylabel("Valeur de l'objectif (Distance totale)", fontweight='bold')
title = "Comparaison de la qualité des solutions par niveau"
if use_log:
ax.set_yscale('log')
title += " (Échelle log)"
ax.set_title(title, fontweight='bold', pad=15)
ax.grid(True, linestyle='--', alpha=0.3, which='both')
ax.legend(loc='best', framealpha=0.95, edgecolor='gray', fancybox=True)
# Save in multiple formats
plt.tight_layout()
out_path_png = os.path.join(save_dir, "benchmark_objectives.png")
out_path_pdf = os.path.join(save_dir, "benchmark_objectives.pdf")
fig.savefig(out_path_png, dpi=300, bbox_inches='tight')
fig.savefig(out_path_pdf, bbox_inches='tight')
if show:
plt.show()
else:
plt.close()
return out_path_png
def plot_times(df: pd.DataFrame, methods: List[str], levels: List[str],
save_dir: str, show: bool, style: str = 'standard') -> str:
"""Grouped bar chart of runtime with academic styling."""
ensure_dir(save_dir)
if style == 'academic':
set_academic_style()
fig, ax = plt.subplots(figsize=(8, 5))
x = np.arange(len(levels))
width = 0.8 / max(1, len(methods))
# Plot bars for each method
for idx, method in enumerate(methods):
sub = df[(df.method == method) & (df.level.isin(levels))].copy()
sub_idx = sub.set_index("level").reindex(levels)
y_vals = sub_idx["time"].values
color = ACADEMIC_COLORS.get(method, None) if style == 'academic' else None
offset = (idx - (len(methods)-1)/2) * width
bars = ax.bar(x + offset, y_vals, width=width*0.9, label=method.upper(),
color=color, alpha=0.85, edgecolor='white', linewidth=0.8)
# Add value labels on top of bars
for bar, val in zip(bars, y_vals):
if not np.isnan(val):
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height,
f'{val:.1f}s' if val < 100 else f'{val:.0f}s',
ha='center', va='bottom', fontsize=7, rotation=0)
# Formatting
ax.set_xticks(x)
ax.set_xticklabels([f"N{l}" for l in levels]) # N for Niveau
ax.set_xlabel("Niveau d'instance", fontweight='bold')
ax.set_ylabel("Temps d'exécution (secondes)", fontweight='bold')
ax.set_title("Comparaison des temps de calcul par niveau",
fontweight='bold', pad=15)
ax.grid(True, axis='y', linestyle='--', alpha=0.3)
ax.legend(loc='best', framealpha=0.95, edgecolor='gray', fancybox=True, ncol=2)
# Save in multiple formats
plt.tight_layout()
out_path_png = os.path.join(save_dir, "benchmark_times.png")
out_path_pdf = os.path.join(save_dir, "benchmark_times.pdf")
fig.savefig(out_path_png, dpi=300, bbox_inches='tight')
fig.savefig(out_path_pdf, bbox_inches='tight')
if show:
plt.show()
else:
plt.close()
return out_path_png
def plot_quality_gap(df: pd.DataFrame, methods: List[str], levels: List[str],
save_dir: str, show: bool, style: str = 'standard') -> str:
"""Plot quality gap ratio compared to exact method (separate figure)."""
ensure_dir(save_dir)
if style == 'academic':
set_academic_style()
if 'exact' not in methods:
print("⚠️ La méthode 'exact' n'est pas disponible, impossible de calculer l'écart de qualité.")
return None
fig, ax = plt.subplots(figsize=(8, 5))
x_ticks = np.arange(len(levels))
exact_obj = df[df.method == 'exact'].set_index('level')['objective']
for method in [m for m in methods if m != 'exact']:
sub = df[(df.method == method) & (df.level.isin(levels))].copy()
sub_idx = sub.set_index("level").reindex(levels)
# Calculate ratio: method_obj / exact_obj
ratios = []
for lvl in levels:
if lvl in exact_obj.index and lvl in sub_idx.index:
exact_val = exact_obj.loc[lvl]
method_val = sub_idx.loc[lvl, 'objective']
if not np.isnan(exact_val) and not np.isnan(method_val) and exact_val > 0:
ratios.append(method_val / exact_val)
else:
ratios.append(np.nan)
else:
ratios.append(np.nan)
color = ACADEMIC_COLORS.get(method, None) if style == 'academic' else None
marker = MARKERS.get(method, 'o') if style == 'academic' else 'o'
mask = ~np.isnan(ratios)
ax.plot(x_ticks[mask], np.array(ratios)[mask], marker=marker,
label=method.upper(), color=color, linewidth=2, markersize=7,
markeredgewidth=0.5, markeredgecolor='white', alpha=0.9)
# Optimal line
ax.axhline(y=1.0, color='red', linestyle='--', linewidth=2, alpha=0.7, label='Optimal (Exact)')
# Formatting
ax.set_xticks(x_ticks)
ax.set_xticklabels([f"N{l}" for l in levels])
ax.set_xlabel("Niveau d'instance", fontweight='bold')
ax.set_ylabel("Ratio d'écart vs Exact", fontweight='bold')
ax.set_title("Écart de qualité des solutions par rapport à l'optimum", fontweight='bold', pad=15)
ax.grid(True, linestyle='--', alpha=0.3)
ax.legend(loc='best', framealpha=0.95, edgecolor='gray', fancybox=True)
# Add percentage labels on y-axis
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: f'{(y-1)*100:+.0f}%'))
# Save in multiple formats
plt.tight_layout()
out_path_png = os.path.join(save_dir, "benchmark_quality_gap.png")
out_path_pdf = os.path.join(save_dir, "benchmark_quality_gap.pdf")
fig.savefig(out_path_png, dpi=300, bbox_inches='tight')
fig.savefig(out_path_pdf, bbox_inches='tight')
if show:
plt.show()
else:
plt.close()
return out_path_png
def plot_statistics_summary(df: pd.DataFrame, methods: List[str], levels: List[str],
save_dir: str, show: bool, style: str = 'standard') -> str:
"""Create a statistics summary table as a separate figure."""
ensure_dir(save_dir)
if style == 'academic':
set_academic_style()
fig, ax = plt.subplots(figsize=(10, 6))
ax.axis('tight')
ax.axis('off')
# Calculate summary statistics
summary_data = []
for method in methods:
sub = df[df.method == method]
avg_obj = sub['objective'].mean()
std_obj = sub['objective'].std()
min_obj = sub['objective'].min()
max_obj = sub['objective'].max()
avg_time = sub['time'].mean()
std_time = sub['time'].std()
valid_count = sub['valid'].sum() if 'valid' in sub.columns else len(sub)
total_count = len(sub)
summary_data.append([
method.upper(),
f"{avg_obj:.1f}" if not np.isnan(avg_obj) else "N/A",
f"{std_obj:.1f}" if not np.isnan(std_obj) else "N/A",
f"{min_obj:.1f}" if not np.isnan(min_obj) else "N/A",
f"{max_obj:.1f}" if not np.isnan(max_obj) else "N/A",
f"{avg_time:.2f}s" if not np.isnan(avg_time) else "N/A",
f"{std_time:.2f}s" if not np.isnan(std_time) else "N/A",
f"{valid_count}/{total_count}"
])
# Create table
table = ax.table(cellText=summary_data,
colLabels=['Méthode', 'Obj. moy.', 'Écart-type', 'Min', 'Max',
'Temps moy.', 'Écart-type', 'Valides'],
cellLoc='center',
loc='center',
colWidths=[0.12, 0.12, 0.12, 0.12, 0.12, 0.13, 0.13, 0.14])
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1, 3)
# Style table header
for j in range(8):
cell = table[(0, j)]
cell.set_facecolor('#2C3E50')
cell.set_text_props(weight='bold', color='white')
cell.set_edgecolor('white')
# Style table rows
for i in range(1, len(methods) + 1):
method = methods[i-1]
base_color = ACADEMIC_COLORS.get(method, '#FFFFFF') if style == 'academic' else '#E8E8E8'
for j in range(8):
cell = table[(i, j)]
if j == 0:
cell.set_facecolor(base_color)
cell.set_alpha(0.7)
cell.set_text_props(weight='bold')
else:
# Alternate row colors
if i % 2 == 0:
cell.set_facecolor('#F8F9FA')
else:
cell.set_facecolor('#FFFFFF')
cell.set_edgecolor('#CCCCCC')
ax.set_title("Statistiques récapitulatives des performances",
fontsize=14, fontweight='bold', pad=30)
# Add footer note
fig.text(0.5, 0.05,
"Note: Moyenne et écart-type calculés sur tous les niveaux d'instances",
ha='center', fontsize=9, style='italic', alpha=0.7)
# Save in multiple formats
plt.tight_layout()
out_path_png = os.path.join(save_dir, "benchmark_statistics.png")
out_path_pdf = os.path.join(save_dir, "benchmark_statistics.pdf")
fig.savefig(out_path_png, dpi=300, bbox_inches='tight')
fig.savefig(out_path_pdf, bbox_inches='tight')
if show:
plt.show()
else:
plt.close()
return out_path_png
def plot_combined_analysis(df: pd.DataFrame, methods: List[str], levels: List[str],
save_dir: str, show: bool, style: str = 'standard') -> str:
"""Create a combined figure with objectives, times, and performance metrics."""
ensure_dir(save_dir)
if style == 'academic':
set_academic_style()
# Create figure with custom GridSpec
fig = plt.figure(figsize=(14, 10))
gs = GridSpec(3, 2, figure=fig, hspace=0.35, wspace=0.3)
# --- Plot 1: Objectives Line Chart ---
ax1 = fig.add_subplot(gs[0, :])
x_ticks = np.arange(len(levels))
for method in methods:
sub = df[(df.method == method) & (df.level.isin(levels))].copy()
sub_idx = sub.set_index("level").reindex(levels)
y_vals = sub_idx["objective"].values
color = ACADEMIC_COLORS.get(method, None) if style == 'academic' else None
marker = MARKERS.get(method, 'o')
mask = ~np.isnan(y_vals)
ax1.plot(x_ticks[mask], y_vals[mask], marker=marker, label=method.upper(),
color=color, linewidth=2, markersize=6, alpha=0.9)
ax1.set_xticks(x_ticks)
ax1.set_xticklabels([f"N{l}" for l in levels])
ax1.set_xlabel("Niveau d'instance", fontweight='bold')
ax1.set_ylabel("Valeur de l'objectif", fontweight='bold')
ax1.set_title("(a) Comparaison de la qualité des solutions", fontweight='bold', loc='left')
ax1.grid(True, linestyle='--', alpha=0.3)
ax1.legend(loc='best', ncol=4, framealpha=0.95)
# --- Plot 2: Execution Times Bar Chart ---
ax2 = fig.add_subplot(gs[1, :])
x = np.arange(len(levels))
width = 0.8 / max(1, len(methods))
for idx, method in enumerate(methods):
sub = df[(df.method == method) & (df.level.isin(levels))].copy()
sub_idx = sub.set_index("level").reindex(levels)
y_vals = sub_idx["time"].values
color = ACADEMIC_COLORS.get(method, None) if style == 'academic' else None
offset = (idx - (len(methods)-1)/2) * width
ax2.bar(x + offset, y_vals, width=width*0.9, label=method.upper(),
color=color, alpha=0.85, edgecolor='white', linewidth=0.8)
ax2.set_xticks(x)
ax2.set_xticklabels([f"N{l}" for l in levels])
ax2.set_xlabel("Niveau d'instance", fontweight='bold')
ax2.set_ylabel("Temps d'exécution (s)", fontweight='bold')
ax2.set_title("(b) Comparaison des temps de calcul", fontweight='bold', loc='left')
ax2.grid(True, axis='y', linestyle='--', alpha=0.3)
ax2.legend(loc='best', ncol=4, framealpha=0.95)
# --- Plot 3: Performance Ratio (vs exact method if available) ---
ax3 = fig.add_subplot(gs[2, 0])
if 'exact' in methods:
exact_obj = df[df.method == 'exact'].set_index('level')['objective']
for method in [m for m in methods if m != 'exact']:
sub = df[(df.method == method) & (df.level.isin(levels))].copy()
sub_idx = sub.set_index("level").reindex(levels)
# Calculate ratio: method_obj / exact_obj
ratios = []
for lvl in levels:
if lvl in exact_obj.index and lvl in sub_idx.index:
exact_val = exact_obj.loc[lvl]
method_val = sub_idx.loc[lvl, 'objective']
if not np.isnan(exact_val) and not np.isnan(method_val) and exact_val > 0:
ratios.append(method_val / exact_val)
else:
ratios.append(np.nan)
else:
ratios.append(np.nan)
color = ACADEMIC_COLORS.get(method, None)
marker = MARKERS.get(method, 'o')
mask = ~np.isnan(ratios)
ax3.plot(x_ticks[mask], np.array(ratios)[mask], marker=marker,
label=method.upper(), color=color, linewidth=2, markersize=6)
ax3.axhline(y=1.0, color='red', linestyle='--', linewidth=1.5, alpha=0.7, label='Optimal (Exact)')
ax3.set_xticks(x_ticks)
ax3.set_xticklabels([f"N{l}" for l in levels])
ax3.set_xlabel("Niveau d'instance", fontweight='bold')
ax3.set_ylabel("Ratio d'écart vs Exact", fontweight='bold')
ax3.set_title("(c) Écart de qualité des solutions", fontweight='bold', loc='left')
ax3.grid(True, linestyle='--', alpha=0.3)
ax3.legend(loc='best', framealpha=0.95, fontsize=8)
else:
ax3.text(0.5, 0.5, 'Résultats de la méthode\nexacte non disponibles',
ha='center', va='center', transform=ax3.transAxes, fontsize=12)
ax3.set_title("(c) Écart de qualité des solutions", fontweight='bold', loc='left')
# --- Plot 4: Summary Statistics Table ---
ax4 = fig.add_subplot(gs[2, 1])
ax4.axis('tight')
ax4.axis('off')
# Calculate summary statistics
summary_data = []
for method in methods:
sub = df[df.method == method]
avg_obj = sub['objective'].mean()
avg_time = sub['time'].mean()
valid_count = sub['valid'].sum() if 'valid' in sub else len(sub)
total_count = len(sub)
summary_data.append([
method.upper(),
f"{avg_obj:.1f}" if not np.isnan(avg_obj) else "N/A",
f"{avg_time:.2f}s" if not np.isnan(avg_time) else "N/A",
f"{valid_count}/{total_count}"
])
table = ax4.table(cellText=summary_data,
colLabels=['Méthode', 'Obj moy.', 'Temps moy.', 'Valides'],
cellLoc='center',
loc='center',
colWidths=[0.25, 0.25, 0.25, 0.25])
table.auto_set_font_size(False)
table.set_fontsize(9)
table.scale(1, 2.5)
# Style table
for i in range(len(methods) + 1):
for j in range(4):
cell = table[(i, j)]
if i == 0:
cell.set_facecolor('#E8E8E8')
cell.set_text_props(weight='bold')
else:
if j == 0:
method = methods[i-1]
color = ACADEMIC_COLORS.get(method, '#FFFFFF')
cell.set_facecolor(color)
cell.set_alpha(0.3)
ax4.set_title("(d) Statistiques récapitulatives", fontweight='bold', loc='left', pad=20)
# Overall title
fig.suptitle("Benchmark VRP : Analyse complète des performances",
fontsize=14, fontweight='bold', y=0.995)
# Save
out_path_png = os.path.join(save_dir, "benchmark_combined.png")
out_path_pdf = os.path.join(save_dir, "benchmark_combined.pdf")
fig.savefig(out_path_png, dpi=300, bbox_inches='tight')
fig.savefig(out_path_pdf, bbox_inches='tight')
if show:
plt.show()
else:
plt.close()
return out_path_png
def main():
parser = argparse.ArgumentParser(description="Benchmark plotting from results directory")
parser.add_argument("--results-dir", default="results", help="Directory containing per-method result folders")
parser.add_argument("--save-dir", default="plots", help="Directory to save generated figures")
parser.add_argument("--methods", nargs="*", default=DEFAULT_METHODS, help="Methods to include (match subdirectories under results)")
parser.add_argument("--levels", nargs="*", default=DEFAULT_LEVELS, help="Levels to include (e.g., 01 02 ...)")
parser.add_argument("--no-show", action="store_true", help="Do not display interactive plots, only save")
parser.add_argument("--log-objective", action="store_true", help="Use log scale for objective plot (useful with large ranges)")
parser.add_argument("--style", choices=['standard', 'academic'], default='academic',
help="Plot style: 'standard' or 'academic' (publication-ready)")
parser.add_argument("--combined", action="store_true", default=True,
help="Generate combined analysis figure (default: True)")
args = parser.parse_args()
methods = args.methods
levels = args.levels
results_dir = args.results_dir
save_dir = args.save_dir
show = not args.no_show
print("=" * 60)
print("VRP BENCHMARK ANALYSIS")
print("=" * 60)
print(f"Results directory: {results_dir}")
print(f"Output directory: {save_dir}")
print(f"Methods: {', '.join(methods)}")
print(f"Levels: {', '.join(levels)}")
print(f"Plot style: {args.style}")
print("=" * 60)
# Collect
print("\n📊 Collecting results...")
df = collect_results(results_dir, methods, levels)
# Persist a CSV summary for reference
ensure_dir(save_dir)
summary_csv = os.path.join(save_dir, "benchmark_summary.csv")
df.sort_values(["method", "level"]).to_csv(summary_csv, index=False)
print(f"✓ Summary CSV saved: {summary_csv}")
# Plot individual figures
print("\n📈 Generating plots...")
obj_path = plot_objectives(df, methods, levels, save_dir,
use_log=args.log_objective, show=show, style=args.style)
print(f"✓ Objective plot: {obj_path}")
print(f" └─ PDF version: {obj_path.replace('.png', '.pdf')}")
time_path = plot_times(df, methods, levels, save_dir, show=show, style=args.style)
print(f"✓ Time plot: {time_path}")
print(f" └─ PDF version: {time_path.replace('.png', '.pdf')}")
# Plot quality gap (separate)
gap_path = plot_quality_gap(df, methods, levels, save_dir, show=show, style=args.style)
if gap_path:
print(f"✓ Quality gap: {gap_path}")
print(f" └─ PDF version: {gap_path.replace('.png', '.pdf')}")
# Plot statistics summary (separate)
stats_path = plot_statistics_summary(df, methods, levels, save_dir, show=show, style=args.style)
print(f"✓ Statistics: {stats_path}")
print(f" └─ PDF version: {stats_path.replace('.png', '.pdf')}")
# Plot combined analysis
if args.combined:
combined_path = plot_combined_analysis(df, methods, levels, save_dir, show=show, style=args.style)
print(f"✓ Combined plot: {combined_path}")
print(f" └─ PDF version: {combined_path.replace('.png', '.pdf')}")
# Quick console summary
print("\n" + "=" * 60)
print("BEST OBJECTIVE PER LEVEL")
print("=" * 60)
best_rows = []
for lvl in levels:
sub = df[df.level == lvl]
candidates = sub.dropna(subset=["objective"]) if not sub.empty else sub
if not candidates.empty:
idx = candidates["objective"].idxmin()
best_method = candidates.loc[idx, "method"]
best_obj = candidates.loc[idx, "objective"]
best_time = candidates.loc[idx, "time"]
best_rows.append((lvl, best_method, best_obj, best_time))
# Format output with emoji
emoji = "🥇" if best_method == "exact" else "🎯"
print(f"Level {lvl}: {emoji} {best_method.upper():6s} → Obj: {best_obj:8.2f} Time: {best_time:6.2f}s")
print("=" * 60)
# Performance summary
print("\nPERFORMANCE SUMMARY")
print("=" * 60)
for method in methods:
sub = df[df.method == method]
avg_obj = sub['objective'].mean()
avg_time = sub['time'].mean()
valid_count = sub['valid'].sum() if 'valid' in sub.columns else len(sub)
total_count = len(sub)
print(f"{method.upper():6s}: Avg Obj = {avg_obj:8.2f} | Avg Time = {avg_time:6.2f}s | Valid: {valid_count}/{total_count}")
print("=" * 60)
print("\n✅ Analyse benchmark terminée !")
print(f"\n📁 Tous les résultats sauvegardés dans : {save_dir}/")
print("\nFichiers générés :")
print(f" • benchmark_objectives.png/pdf - Comparaison qualité des solutions")
print(f" • benchmark_times.png/pdf - Comparaison temps d'exécution")
print(f" • benchmark_quality_gap.png/pdf - Écart de qualité vs optimum")
print(f" • benchmark_statistics.png/pdf - Statistiques récapitulatives")
if args.combined:
print(f" • benchmark_combined.png/pdf - Analyse combinée complète")
print(f" • benchmark_summary.csv - Données brutes CSV")
print()
if __name__ == "__main__":
main()