-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_experiments.py
More file actions
652 lines (598 loc) · 30.5 KB
/
Copy pathrun_experiments.py
File metadata and controls
652 lines (598 loc) · 30.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
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
"""
===============================================================================
run_experiments.py
多算法对比实验主入口 (完整优化版 + 论文级可视化)
===============================================================================
【文件职责】
- 统一调度所有算法在所有测试函数上运行
- 计算 MIGD / SP / MS 指标
- 执行 Wilcoxon 秩和检验
- 生成 15 张论文级可视化图表
- 可调用 kemm_dmoea_core.py 进行船舶规划
【论文来源】(知识库: MMTL-DMOEA, IEEE TCYB, 2020)
摘要:
"proposes a new memory-driven manifold TL-based evolutionary algorithm
for dynamic multiobjective optimization (MMTL-DMOEA)"
核心动机 (知识库 Section I):
"The motivation of this article is to accelerate DMOPs-solving
without deteriorating the solution quality"
实验设置 (知识库 Section IV):
"N=100; nt=10; τt=10; each algorithm ran 20 times"
"three metrics: IGD, SP, MS"
对比算法 (知识库 Section IV):
"MOEA/D-SVR, Tr-DMOEA, MOEA/D-KF, PPS, RI-MOEA/D"
【运行方式】
python run_experiments.py # 默认: 快速验证
python run_experiments.py --quick # 快速验证 (3问题×3次)
python run_experiments.py --full # 完整实验 (6问题×5次)
python run_experiments.py --ship-only # 仅船舶路径规划
python run_experiments.py --all # 完整实验 + 船舶规划
===============================================================================
"""
import sys
import time
import numpy as np
from typing import Dict, List
from collections import defaultdict
from benchmark_algorithms import (
DynamicTestProblems, PerformanceMetrics,
RI_DMOEA, MMTL_DMOEA, Tr_DMOEA, PPS_DMOEA, KF_DMOEA, SVR_DMOEA,
KEMM_DMOEA_Abstract
)
try:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
HAS_MPL = True
except ImportError:
HAS_MPL = False
# ╔═══════════════════════════════════════════════════════════════════════════╗
# 第 1 部分:实验配置
# ╚═══════════════════════════════════════════════════════════════════════════╝
class ExperimentConfig:
"""
来源: 知识库 Section IV
"N=100; nt=10; τt=10; each algorithm ran 20 times"
"""
POP_SIZE = 100
N_VAR = 10
N_OBJ = 2
NT = 10
TAU_T = 10
N_CHANGES = 10
GENS_PER_CHANGE = 20
N_RUNS = 5
SIGNIFICANCE = 0.05
PROBLEMS = ['FDA1', 'FDA2', 'FDA3', 'dMOP1', 'dMOP2', 'dMOP3']
ALGORITHMS = {
'RI': RI_DMOEA,
'PPS': PPS_DMOEA,
'KF': KF_DMOEA,
'SVR': SVR_DMOEA,
'Tr': Tr_DMOEA,
'MMTL': MMTL_DMOEA,
'KEMM': KEMM_DMOEA_Abstract,
}
# ╔═══════════════════════════════════════════════════════════════════════════╗
# 第 2 部分:实验运行器
# ╚═══════════════════════════════════════════════════════════════════════════╝
class ExperimentRunner:
def __init__(self, config: ExperimentConfig = None):
self.cfg = config or ExperimentConfig()
self.problems = DynamicTestProblems(nt=self.cfg.NT, tau_t=self.cfg.TAU_T)
self.metrics = PerformanceMetrics()
self.results = {}
self.igd_curves = {}
def run_all(self):
total = len(self.cfg.ALGORITHMS) * len(self.cfg.PROBLEMS) * self.cfg.N_RUNS
counter = 0
t_start = time.time()
for algo_name, algo_class in self.cfg.ALGORITHMS.items():
self.results[algo_name] = {}
self.igd_curves[algo_name] = {}
for prob_name in self.cfg.PROBLEMS:
self.results[algo_name][prob_name] = {
'MIGD': [], 'SP': [], 'MS': [], 'TIME': []
}
self.igd_curves[algo_name][prob_name] = []
obj_name]['MIGD'].append(result['migd'])
self.results[algo_name][prob_name]['SP'].append(result['sp'])
self.results[algo_name][prob_name]['MS'].append(result['ms'])
self.results[algo_name][prob_name]['TIME'].append(result['time'])
self.igd_curves[algo_name][prob_name].append(result['igd_curve'])
total_time = time.time() - t_start
print(f"\n All done in {total_time:.1f}s")
return self.results
def _run_single(self, algo_class, obj_func, pof_func, prob_name):
lb = np.zeros(self.cfg.N_VAR)
ub = np.ones(self.cfg.N_VAR)
lb[1:] = -1.0
ub[1:] = 1.0
algo = algo_class(self.cfg.POP_SIZE, self.cfg.N_VAR, self.cfg.N_OBJ, (lb, ub))
algo.initialize()
t0 = time.time()
igd_list, sp_list, ms_list = [], [], []
generation = 0
for ci in range(self.cfg.N_CHANGES):
t = self.problems.get_time(generation)
if ci == 0:
algo.fitness = algo.evaluate(algo.population, obj_func, t)
else:
algo.respond_to_change(obj_func, t)
for _ in range(self.cfg.GENS_PER_CHANGE):
algo.evolve_one_gen(obj_func, t)
generation += self.cfg.TAU_T
obtained = algo.get_pareto_front()
try:
true_pof = pof_func(t=t)
except TypeError:
true_pof = pof_func()
igd_list.append(self.metrics.igd(obtained, true_pof))
sp_list.append(self.metrics.spacing(obtained))
ms_list.append(self.metrics.maximum_spread(obtained, true_pof))
return {
'migd': self.metrics.migd(igd_list),
'sp': float(np.mean(sp_list)),
'ms': float(np.mean(ms_list)),
'time': time.time() - t0,
'igd_curve': igd_list,
}
# ╔═══════════════════════════════════════════════════════════════════════════╗
# 第 3 部分:统计检验
# ╚═══════════════════════════════════════════════════════════════════════════╝
def wilcoxon_test(ours, others, alpha=0.05):
if len(ours) < 3:
return '≈'
try:
from scipy.stats import ranksums
_, p = ranksums(ours, others)
if p < alpha:
return '+' if np.mean(ours) < np.mean(others) else '-'
return '≈'
except Exception:
return '≈'
# ╔═══════════════════════════════════════════════════════════════════════════╗
# 第 4 部分:结果展示 (论文级可视化, 15张图表)
# ╚═══════════════════════════════════════════════════════════════════════════╝
class ResultPresenter:
"""
论文级可视化 — 15张图表
来源: 知识库 Section IV 实验设计
"""
def __init__(self, results: Dict, config: ExperimentConfig,
igd_curves: Dict = None):
self.results = results
self.cfg = config
self.igd_curves = igd_curves or {}
self.our_algo = 'KEMM'
def print_tables(self):
our = self.our_algo
metrics_info = {'MIGD': 'smaller', 'SP': 'smaller', 'MS': 'larger'}
for metric, direction in metrics_info.items():
arrow = '↓' if direction == 'smaller' else '↑'
print(f"\n{'='*100}")
print(f" TABLE: {metric} {arrow} (Mean ± Std) [{self.cfg.N_RUNS} runs]")
print(f"{'='*100}")
algos = list(self.results.keys())
header = f"{'Prob':>6s}"
for a in algos:
header += f" | {a:>14s}"
print(header)
print("-" * len(header))
wins = defaultdict(int)
for prob in self.cfg.PROBLEMS:
row = f"{prob:>6s}"
means = {a: np.mean(self.results[a][prob][metric]) for a in algos}
best = (min if direction == 'smaller' else max)(means, key=means.get)
wins[best] += 1
for a in algos:
vals = self.results[a][prob][metric]
m, s = np.mean(vals), np.std(vals)
if a != our:
sig = wilcoxon_test(self.results[our][prob][metric], vals)
if direction == 'larger':
sig = {'+': '-', '-': '+', '≈': '≈'}[sig]
else:
sig = ' '
mark = '**' if a == best else ' '
row += f" | {mark}{m:.4f}±{s:.4f}{sig}"
print(row)
print("-" * len(header))
row = f"{'Wins':>6s}"
for a in algos:
row += f" | {wins[a]:>14d}"
print(row)
def print_time_table(self):
print(f"\n{'='*80}")
print(f" Running Time (seconds)")
print(f"{'='*80}")
algos = list(self.results.keys())
header = f"{'Prob':>6s}"
for a in algos:
header += f" | {a:>10s}"
print(header)
print("-" * len(header))
for prob in self.cfg.PROBLEMS:
row = f"{prob:>6s}"
for a in algos:
t = np.mean(self.results[a][prob]['TIME'])
row += f" | {t:>9.2f}s"
print(row)
def print_ranking(self):
print(f"\n{'='*60}")
print(f" OVERALL RANKING")
print(f"{'='*60}")
algos = list(self.results.keys())
all_ranks = {a: [] for a in algos}
for metric in ['MIGD', 'SP', 'MS']:
direction = 'smaller' if metric != 'MS' else 'larger'
for prob in self.cfg.PROBLEMS:
means = np.array([np.mean(self.results[a][prob][metric]) for a in algos])
if direction == 'larger':
means = -means
ranks = np.argsort(np.argsort(means)) + 1
for i, a in enumerate(algos):
all_ranks[a].append(ranks[i])
avg = {a: np.mean(r) for a, r in all_ranks.items()}
for rank, a in enumerate(sorted(avg, key=avg.get), 1):
marker = " ★" if a == self.our_algo else ""
print(f" #{rank}: {a:>6s} AvgRank = {avg[a]:.2f}{marker}")
def plot_all(self, prefix="out"):
if not HAS_MPL:
print(" [WARN] matplotlib not found")
return
print(f"\n Generating 15 publication-quality figures...")
self._plot_metric_bars(prefix)
self._plot_radar(prefix)
self._plot_igd_over_time(prefix)
self._plot_boxplots(prefix)
self._plot_heatmap(prefix)
self._plot_win_count(prefix)
self._plot_cd_diagram(prefix)
self._plot_speedup(prefix)
self._plot_metric_tradeoff(prefix)
self._plot_rank_evolution(prefix)
self._plot_pairwise_comparison(prefix)
print(f" All figures saved with prefix '{prefix}_*'")
# ── 图 1-3: 柱状图 ──
def _plot_metric_bars(self, prefix):
algos = list(self.results.keys())
n_a = len(algos)
base_colors = plt.cm.Set2(np.linspace(0, 1, n_a))
for metric, direction, ylabel in [
('MIGD', 'smaller', 'MIGD ↓'), ('SP', 'smaller', 'SP ↓'),
('MS', 'larger', 'MS ↑')]:
n_p = len(self.cfg.PROBLEMS)
nc = min(3, n_p)
nr = (n_p + nc - 1) // nc
fig, axes = plt.subplots(nr, nc, figsize=(6*nc, 5*nr))
axes = np.atleast_1d(axes).ravel()
for idx, prob in enumerate(self.cfg.PROBLEMS):
ax = axes[idx]
means = [np.mean(self.results[a][prob][metric]) for a in algos]
stds = [np.std(self.results[a][prob][metric]) for a in algos]
best_i = np.argmin(means) if direction == 'smaller' else np.argmax(means)
bc = []
for i, a in enumerate(algos):
if i == best_i: bc.append('gold')
elif a == self.our_algo: bc.append('#e74c3c')
else: bc.append(base_colors[i])
ax.bar(range(n_a), means, yerr=stds, capsize=3, color=bc, alpha=0.85, edgecolor='k', linewidth=0.5)
ax.set_title(prob, fontweight='bold')
ax.set_ylabel(ylabel, fontsize=9)
ax.set_xticks(range(n_a))
ax.set_xticklabels(algos, rotation=45, fontsize=8)
ax.grid(True, alpha=0.3, axis='y')
for k in range(n_p, len(axes)): axes[k].set_visible(False)
fig.suptitle(f'{metric} Comparison (gold=best, red=KEMM)', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(f'{prefix}_{metric.lower()}_bar.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_{metric.lower()}_bar.png")
# ── 图 4: 雷达图 ──
def _plot_radar(self, prefix):
algos = list(self.results.keys())
n_a = len(algos)
labels = ['MIGD↓', 'SP↓', 'MS↑', 'Speed↓']
scores = np.zeros((n_a, 4))
for j, m in enumerate(['MIGD', 'SP', 'MS', 'TIME']):
raw = [np.mean([np.mean(self.results[a][p][m]) for p in self.cfg.PROBLEMS]) for a in algos]
raw = np.array(raw)
scores[:, j] = raw / (np.max(raw) + 1e-12) if m == 'MS' else 1 - raw / (np.max(raw) + 1e-12)
angles = np.linspace(0, 2*np.pi, 4, endpoint=False).tolist() + [0]
fig, ax = plt.subplots(figsize=(9, 9), subplot_kw=dict(polar=True))
colors = plt.cm.Set2(np.linspace(0, 1, n_a))
for i, a in enumerate(algos):
v = scores[i].tolist() + [scores[i][0]]
lw = 3.5 if a == self.our_algo else 1.2
ls = '-' if a == self.our_algo else '--'
ax.plot(angles, v, linewidth=lw, linestyle=ls, label=a, color=colors[i])
if a == self.our_algo: ax.fill(angles, v, alpha=0.2, color=colors[i])
ax.set_xticks(angles[:-1]); ax.set_xticklabels(labels, fontsize=11)
ax.set_ylim(0, 1.15)
ax.legend(loc='upper right', bbox_to_anchor=(1.35, 1.05), fontsize=10)
ax.set_title('Multi-Metric Performance Radar', fontsize=14, fontweight='bold', pad=25)
plt.tight_layout()
plt.savefig(f'{prefix}_radar.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_radar.png")
# ── 图 5: IGD随时间变化 (论文Fig.3) ──
def _plot_igd_over_time(self, prefix):
if not self.igd_curves:
print(" [SKIP] No IGD curves"); return
algos = list(self.results.keys())
colors = plt.cm.Set1(np.linspace(0, 1, len(algos)))
markers = ['o', 's', '^', 'D', 'v', 'P', '*']
n_p = len(self.cfg.PROBLEMS); nc = min(3, n_p); nr = (n_p+nc-1)//nc
fig, axes = plt.subplots(nr, nc, figsize=(6*nc, 4.5*nr))
axes = np.atleast_1d(axes).ravel()
for idx, prob in enumerate(self.cfg.PROBLEMS):
ax = axes[idx]
for i, a in enumerate(algos):
curves = self.igd_curves.get(a, {}).get(prob, [])
if not curves: continue
ml = max(len(c) for c in curves)
padded = [c + [c[-1]]*(ml-len(c)) for c in curves]
mc = np.mean(padded, axis=0); sc = np.std(padded, axis=0)
x = np.arange(1, len(mc)+1)
lw = 2.5 if a == self.our_algo else 1.2
ls = '-' if a == self.our_algo else '--'
ax.plot(x, mc, marker=markers[i%7], linewidth=lw, linestyle=ls,
markersize=5, label=a, color=colors[i], alpha=0.9)
ax.fill_between(x, mc-sc, mc+sc, color=colors[i], alpha=0.08)
ax.set_xlabel('Change Index'); ax.set_ylabel('IGD')
ax.set_title(prob, fontweight='bold'); ax.grid(True, alpha=0.3)
ax.legend(fontsize=7)
for k in range(n_p, len(axes)): axes[k].set_visible(False)
fig.suptitle('IGD Over Time (Paper Fig.3 style)', fontsize=13, fontweight='bold')
plt.tight_layout()
plt.savefig(f'{prefix}_igd_over_time.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_igd_over_time.png")
# ── 图 6-8: 箱线图 ──
def _plot_boxplots(self, prefix):
algos = list(self.results.keys())
for metric in ['MIGD', 'SP', 'MS']:
n_p = len(self.cfg.PROBLEMS); nc = min(3, n_p); nr = (n_p+nc-1)//nc
fig, axes = plt.subplots(nr, nc, figsize=(6*nc, 5*nr))
axes = np.atleast_1d(axes).ravel()
for idx, prob in enumerate(self.cfg.PROBLEMS):
ax = axes[idx]
data = [self.results[a][prob][metric] for a in algos]
bp = ax.boxplot(data, labels=algos, patch_artist=True, widths=0.6,
showmeans=True, meanprops=dict(marker='D', markerfacecolor='red', markersize=5))
bc = plt.cm.Set3(np.linspace(0, 1, len(algos)))
for pi, (patch, a) in enumerate(zip(bp['boxes'], algos)):
patch.set_facecolor('#ff6b6b' if a == self.our_algo else bc[pi])
if a == self.our_algo: patch.set_linewidth(2)
ax.set_title(prob, fontweight='bold'); ax.set_ylabel(metric)
ax.tick_params(axis='x', rotation=45); ax.grid(True, alpha=0.3, axis='y')
for k in range(n_p, len(axes)): axes[k].set_visible(False)
fig.suptitle(f'{metric} Distribution (red=KEMM, ◆=mean)', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(f'{prefix}_{metric.lower()}_box.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_{metric.lower()}_box.png")
# ── 图 9: 热力图 ──
def _plot_heatmap(self, prefix):
algos = list(self.results.keys())
matrix = np.array([[np.mean(self.results[a][p]['MIGD']) for p in self.cfg.PROBLEMS] for a in algos])
fig, ax = plt.subplots(figsize=(max(10, len(self.cfg.PROBLEMS)*2), max(5, len(algos)*0.8)))
cmin = matrix.min(0, keepdims=True); cmax = matrix.max(0, keepdims=True)
nm = (matrix - cmin) / (cmax - cmin + 1e-12)
im = ax.imshow(nm, cmap='RdYlGn_r', aspect='auto', vmin=0, vmax=1)
ax.set_xticks(range(len(self.cfg.PROBLEMS))); ax.set_xticklabels(self.cfg.PROBLEMS, fontsize=11)
ax.set_yticks(range(len(algos))); ax.set_yticklabels(algos, fontsize=11)
for i in range(len(algos)):
for j in range(len(self.cfg.PROBLEMS)):
c = 'white' if nm[i,j] > 0.6 else 'black'
ax.text(j, i, f'{matrix[i,j]:.4f}', ha='center', va='center', fontsize=9, color=c, fontweight='bold')
plt.colorbar(im, ax=ax, label='Normalized MIGD (0=best)')
ax.set_title('MIGD Heatmap', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(f'{prefix}_migd_heatmap.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_migd_heatmap.png")
# ── 图 10: 胜负统计 ──
def _plot_win_count(self, prefix):
algos = list(self.results.keys())
md = {'MIGD': 'smaller', 'SP': 'smaller', 'MS': 'larger'}
wm = {a: {m: 0 for m in md} for a in algos}
for m, d in md.items():
for p in self.cfg.PROBLEMS:
means = {a: np.mean(self.results[a][p][m]) for a in algos}
best = (min if d == 'smaller' else max)(means, key=means.get)
wm[best][m] += 1
fig, ax = plt.subplots(figsize=(10, 6))
x = np.arange(len(algos)); w = 0.25
cm = ['#3498db', '#2ecc71', '#e74c3c']
for i, (m, c) in enumerate(zip(md.keys(), cm)):
counts = [wm[a][m] for a in algos]
bars = ax.bar(x + i*w, counts, w, label=m, color=c, alpha=0.8, edgecolor='k')
for b, cnt in zip(bars, counts):
if cnt > 0: ax.text(b.get_x()+b.get_width()/2, b.get_height()+0.1, str(cnt), ha='center', fontsize=10, fontweight='bold')
ax.set_xticks(x+w); ax.set_xticklabels(algos, fontsize=11)
ax.set_ylabel('Wins'); ax.set_title('Win Count by Metric', fontsize=14, fontweight='bold')
ax.legend(fontsize=11); ax.grid(True, alpha=0.3, axis='y'); ax.set_ylim(0, len(self.cfg.PROBLEMS)+1)
plt.tight_layout()
plt.savefig(f'{prefix}_win_count.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_win_count.png")
# ── 图 11: CD排名图 ──
def _plot_cd_diagram(self, prefix):
algos = list(self.results.keys())
ar = {a: [] for a in algos}
for m in ['MIGD', 'SP', 'MS']:
d = 'smaller' if m != 'MS' else 'larger'
for p in self.cfg.PROBLEMS:
means = np.array([np.mean(self.results[a][p][m]) for a in algos])
if d == 'larger': means = -means
ranks = np.argsort(np.argsort(means)).astype(float) + 1
for i, a in enumerate(algos): ar[a].append(ranks[i])
avg = {a: np.mean(r) for a, r in ar.items()}
sa = sorted(avg, key=avg.get)
fig, ax = plt.subplots(figsize=(12, 4))
cc = ['#e74c3c' if a == self.our_algo else '#3498db' for a in sa]
ax.barh(range(len(sa)), [avg[a] for a in sa], color=cc, alpha=0.8, edgecolor='k', height=0.6)
for i, a in enumerate(sa):
mk = " ★" if a == self.our_algo else ""
ax.text(avg[a]+0.05, i, f'{avg[a]:.2f}{mk}', va='center', fontsize=11, fontweight='bold')
ax.set_yticks(range(len(sa))); ax.set_yticklabels(sa, fontsize=12)
ax.set_xlabel('Average Rank (lower=better)'); ax.set_title('CD Ranking', fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3, axis='x')
plt.tight_layout()
plt.savefig(f'{prefix}_cd_rank.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_cd_rank.png")
# ── 图 12: 加速比 ──
def _plot_speedup(self, prefix):
algos = list(self.results.keys())
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
ax = axes[0]
at = [np.mean([np.mean(self.results[a][p]['TIME']) for p in self.cfg.PROBLEMS]) for a in algos]
bc = ['#e74c3c' if a == self.our_algo else '#95a5a6' for a in algos]
bars = ax.bar(algos, at, color=bc, alpha=0.8, edgecolor='k')
for b, t in zip(bars, at): ax.text(b.get_x()+b.get_width()/2, b.get_height()+0.02, f'{t:.2f}s', ha='center', fontsize=10)
ax.set_ylabel('Time (s)'); ax.set_title('(a) Running Time', fontweight='bold'); ax.grid(True, alpha=0.3, axis='y')
ax = axes[1]
eff = []
for a in algos:
migd = np.mean([np.mean(self.results[a][p]['MIGD']) for p in self.cfg.PROBLEMS])
t = np.mean([np.mean(self.results[a][p]['TIME']) for p in self.cfg.PROBLEMS])
eff.append(1.0 / (migd * t + 1e-12))
en = np.array(eff) / (max(eff) + 1e-12)
bc2 = ['#e74c3c' if a == self.our_algo else '#3498db' for a in algos]
bars = ax.bar(algos, en, color=bc2, alpha=0.8, edgecolor='k')
for b, e in zip(bars, en): ax.text(b.get_x()+b.get_width()/2, b.get_height()+0.02, f'{e:.2f}', ha='center', fontsize=10)
ax.set_ylabel('Efficiency'); ax.set_title('(b) 1/(MIGD×Time)', fontweight='bold'); ax.grid(True, alpha=0.3, axis='y')
fig.suptitle('Computational Efficiency', fontsize=13, fontweight='bold')
plt.tight_layout()
plt.savefig(f'{prefix}_speedup.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_speedup.png")
# ── 图 13: MIGD-Time权衡散点 ──
def _plot_metric_tradeoff(self, prefix):
algos = list(self.results.keys())
fig, ax = plt.subplots(figsize=(10, 7))
colors = plt.cm.Set1(np.linspace(0, 1, len(algos)))
markers = ['o', 's', '^', 'D', 'v', 'P', '*']
for i, a in enumerate(algos):
for j, p in enumerate(self.cfg.PROBLEMS):
migd = np.mean(self.results[a][p]['MIGD'])
t = np.mean(self.results[a][p]['TIME'])
sz = 200 if a == self.our_algo else 80
ec = 'black' if a == self.our_algo else 'none'
ax.scatter(t, migd, c=[colors[i]], s=sz, marker=markers[i%7],
edgecolors=ec, linewidths=1.5, alpha=0.8,
label=a if j == 0 else "")
ax.annotate('← Ideal', xy=(0.05, 0.05), xycoords='axes fraction', fontsize=12,
color='green', fontweight='bold', bbox=dict(boxstyle='round', facecolor='lightgreen', alpha=0.3))
ax.set_xlabel('Time (s)'); ax.set_ylabel('MIGD ↓')
ax.set_title('Quality vs Speed Tradeoff', fontsize=13, fontweight='bold')
ax.legend(fontsize=10); ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(f'{prefix}_tradeoff.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_tradeoff.png")
# ── 图 14: 排名稳定性 ──
def _plot_rank_evolution(self, prefix):
algos = list(self.results.keys())
fig, ax = plt.subplots(figsize=(12, 6))
colors = plt.cm.Set1(np.linspace(0, 1, len(algos)))
for i, a in enumerate(algos):
ranks = []
for p in self.cfg.PROBLEMS:
means = np.array([np.mean(self.results[al][p]['MIGD']) for al in algos])
ranks.append(np.argsort(np.argsort(means))[i] + 1)
lw = 3 if a == self.our_algo else 1.5
ls = '-' if a == self.our_algo else '--'
ax.plot(range(len(self.cfg.PROBLEMS)), ranks, marker='o', linewidth=lw, linestyle=ls,
markersize=10 if a == self.our_algo else 6, label=a, color=colors[i])
ax.set_xticks(range(len(self.cfg.PROBLEMS))); ax.set_xticklabels(self.cfg.PROBLEMS, fontsize=11)
ax.set_ylabel('Rank (1=best)'); ax.set_ylim(0.5, len(algos)+0.5); ax.invert_yaxis()
ax.set_title('MIGD Rank Stability', fontsize=14, fontweight='bold')
ax.legend(fontsize=10, loc='center left', bbox_to_anchor=(1, 0.5)); ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(f'{prefix}_rank_evolution.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_rank_evolution.png")
# ── 图 15: 两两对比矩阵 ──
def _plot_pairwise_comparison(self, prefix):
algos = list(self.results.keys())
na = len(algos)
wm = np.zeros((na, na))
for m in ['MIGD', 'SP']:
for p in self.cfg.PROBLEMS:
for i in range(na):
for j in range(na):
if i != j and np.mean(self.results[algos[i]][p][m]) < np.mean(self.results[algos[j]][p][m]):
wm[i, j] += 1
tp = len(self.cfg.PROBLEMS) * 2
fig, ax = plt.subplots(figsize=(9, 8))
im = ax.imshow(wm / max(tp, 1), cmap='Blues', aspect='equal', vmin=0, vmax=1)
ax.set_xticks(range(na)); ax.set_xticklabels(algos, fontsize=10, rotation=45)
ax.set_yticks(range(na)); ax.set_yticklabels(algos, fontsize=10)
for i in range(na):
for j in range(na):
if i == j: ax.text(j, i, '—', ha='center', va='center', fontsize=11)
else:
r = wm[i,j] / max(tp,1)
ax.text(j, i, f'{int(wm[i,j])}/{tp}', ha='center', va='center', fontsize=10,
color='white' if r > 0.5 else 'black', fontweight='bold')
plt.colorbar(im, ax=ax, label='Win Ratio')
ax.set_title('Pairwise Win Matrix (row beats column)', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(f'{prefix}_pairwise.png', dpi=150, bbox_inches='tight')
plt.close()
print(f" [PLOT] {prefix}_pairwise.png")
# ╔═══════════════════════════════════════════════════════════════════════════╗
# 第 5 部分:主程序
# ╚═══════════════════════════════════════════════════════════════════════════╝
def run_benchmark(quick=False):
print("=" * 70)
print(" KEMM-DMOEA Benchmark Experiments")
print(" Source: MMTL-DMOEA (IEEE TCYB, 2020)")
print("=" * 70)
cfg = ExperimentConfig()
if quick:
cfg.N_RUNS = 3; cfg.N_CHANGES = 5; cfg.GENS_PER_CHANGE = 10
cfg.PROBLEMS = ['FDA1', 'FDA3', 'dMOP2']
print(" [MODE] Quick")
print(f"\n Pop={cfg.POP_SIZE} Var={cfg.N_VAR} nt={cfg.NT} τt={cfg.TAU_T}")
print(f" Changes={cfg.N_CHANGES} Gens/change={cfg.GENS_PER_CHANGE} Runs={cfg.N_RUNS}")
print(f" Problems: {cfg.PROBLEMS}")
print(f" Algorithms: {list(cfg.ALGORITHMS.keys())}\n")
runner = ExperimentRunner(cfg)
results = runner.run_all()
presenter = ResultPresenter(results, cfg, igd_curves=runner.igd_curves)
print()
presenter.print_tables()
presenter.print_time_table()
presenter.print_ranking()
presenter.plot_all()
return results
def run_ship():
from kemm_dmoea_core import main_ship_planning
main_ship_planning()
def main():
args = sys.argv[1:]
if '--ship-only' in args:
run_ship()
elif '--full' in args:
run_benchmark(quick=False)
elif '--all' in args:
print("\n" + "█" * 70)
print(" PART 1: Benchmark")
print("█" * 70)
run_benchmark(quick=False)
print("\n" + "█" * 70)
print(" PART 2: Ship Planning")
print("█" * 70)
run_ship()
elif '--quick' in args:
run_benchmark(quick=True)
else:
run_benchmark(quick=True)
print("\n Options: --quick | --full | --ship-only | --all")
if __name__ == "__main__":
main()