From 8b740e2f0435d86d4e0c20319eb7b7ea9b623345 Mon Sep 17 00:00:00 2001 From: Hgq <2757430053@qq.com> Date: Sat, 27 Sep 2025 15:54:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=A7=A3=E7=9A=84=E5=88=86=E5=B8=83=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index bce2691..3afb010 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,6 @@ from GA import GA from Instance import * from NSGA2 import NSGA2 - # 绘制甘特图 def Gantt(Machines): M = ['red', 'blue', 'yellow', 'orange', 'green', 'palegoldenrod', 'purple', 'pink', 'Thistle', 'Magenta', @@ -29,7 +28,6 @@ def Gantt(Machines): plt.savefig('优化后排程方案的甘特图.png') plt.show() - if __name__ == '__main__': # 初始化参数 g = GA() @@ -44,10 +42,18 @@ if __name__ == '__main__': Optimal_solutions = [] # 存储非支配解 Optimal_fit_values = [] # 存储非支配解的目标值 + # 新增:用于存储所有代的所有解 + all_solutions = [] # 存储所有个体(染色体) + all_fitnesses = [] # 存储所有个体的目标值 + for i in range(g.Max_Itertions): print(f"iter_{i} start!") Fit = g.fitness(C, J, Processing_time, M_num, O_num) + # 记录当前代的所有解 + all_solutions.extend(C) + all_fitnesses.extend(Fit) + # 非支配排序 rank = nsga2.fast_non_dominated_sort(Fit) current_non_dominated = [C[j] for j in range(len(C)) if rank[j] == 0] @@ -128,15 +134,23 @@ if __name__ == '__main__': print(f"\n选中解的目标值: (最大完工时间: {final_fitness[0]}, 负载标准差: {final_fitness[1]:.2f})") Gantt(d.Machines) - # 绘制帕累托前沿 + # 绘制帕累托前沿(非支配解) if Optimal_fit_values: - plt.figure() - cmax = [fit[0] for fit in Optimal_fit_values] - load_std = [fit[1] for fit in Optimal_fit_values] - plt.scatter(cmax, load_std, color='red') - plt.title('ParetoFront') - plt.xlabel('maxtime') - plt.ylabel('standard deviation') + plt.figure(figsize=(10, 6)) + cmax_nd = [fit[0] for fit in Optimal_fit_values] + load_std_nd = [fit[1] for fit in Optimal_fit_values] + plt.scatter(cmax_nd, load_std_nd, color='red', label='Non-dominated solutions', zorder=5) + + # 绘制所有解的点图(所有代的所有个体) + if all_fitnesses: + cmax_all = [fit[0] for fit in all_fitnesses] + load_std_all = [fit[1] for fit in all_fitnesses] + plt.scatter(cmax_all, load_std_all, color='blue', alpha=0.5, label='All solutions', zorder=1) + + plt.title('All Solutions and Pareto Front') + plt.xlabel('Max Completion Time (Cmax)') + plt.ylabel('Load Standard Deviation') + plt.legend() plt.grid(True) - plt.savefig('pareto.png') + plt.savefig('all_solutions_pareto.png') plt.show() \ No newline at end of file