lead/frame_plot/pareto_front_visualization.py
2025-03-17 16:40:01 +08:00

69 lines
2.1 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.font_manager as fm
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 生成随机种群
np.random.seed(42)
n_points = 50
points = np.random.rand(n_points, 2) * 10
# 识别帕累托前沿
def is_pareto_efficient(costs):
is_efficient = np.ones(costs.shape[0], dtype=bool)
for i, c in enumerate(costs):
if is_efficient[i]:
is_efficient[is_efficient] = np.any(costs[is_efficient] < c, axis=1) # Remove dominated points
is_efficient[i] = True
return is_efficient
# 获取帕累托前沿点
pareto_front_mask = is_pareto_efficient(points)
pareto_front = points[pareto_front_mask]
# 创建图形
plt.figure(figsize=(10, 8))
# 绘制非帕累托前沿点
plt.scatter(points[~pareto_front_mask, 0], points[~pareto_front_mask, 1],
c='lightgray', s=100, label='种群个体')
# 绘制帕累托前沿点
plt.scatter(pareto_front[:, 0], pareto_front[:, 1],
c='red', s=100, label='帕累托前沿个体')
# 连接帕累托前沿点
sorted_front = pareto_front[np.argsort(pareto_front[:, 0])]
plt.plot(sorted_front[:, 0], sorted_front[:, 1], 'r--', alpha=0.5)
# 添加密度分布示意
for point in pareto_front:
circle = plt.Circle(point, 0.5, color='blue', fill=False, alpha=0.2)
plt.gca().add_patch(circle)
# 添加箭头和标注
plt.arrow(8, 8, -0.5, -0.5, head_width=0.2, head_length=0.2, fc='k', ec='k')
plt.text(8.2, 8.2, '优化方向', fontsize=20)
# 设置标题和标签
# plt.title('帕累托前沿维护策略示意图', fontsize=14, pad=20)
plt.xlabel('算法性能指标', fontsize=20)
plt.ylabel('运行时间', fontsize=20)
# 添加图例
plt.legend(loc='upper right', fontsize=20)
# 添加网格
plt.grid(True, linestyle='--', alpha=0.3)
# 设置坐标轴范围
plt.xlim(-0.5, 11)
plt.ylim(-0.5, 11)
# 保存图片
plt.savefig('pareto_front_strategy.png', dpi=300, bbox_inches='tight')
plt.close()