赞
踩
注:参数结果设置需要一样,种群大小、迭代次数,父代数量、交叉变异概率
改进前通过nsga2_test获取得到的
- NSGA2_zdt1.mat
- NSGA2_zdt2.mat
- NSGA2_zdt3.mat
- NSGA2_zdt4.mat
- NSGA2_zdt6.mat
改进后通过my_nsga2_test获取得到的
- MyNSGA2_zdt1.mat
- MyNSGA2_zdt2.mat
- MyNSGA2_zdt3.mat
- MyNSGA2_zdt4.mat
- MyNSGA2_zdt6mat
代码中还包含了对SRN,和TNK函数测试结过的分析代码,可以去掉(SRN,和TNK是比较简单的带约束函数,改进前的结果已经很好了,就不做更多的分析了)
- readfile.m:加载对应的数据,x表示对应的测试函数
- Plot.m:画图函数,动态画出改进前与改进后每一代前沿的变化情况
- plotTestPareto.m:根据所有测试次数结果中的最小GD画出前沿结果,加入真实前沿结果对比
- plotTurePareto.m:根据已知获取的理想前沿结果画图
- SaveOnlyGdSp.m:根据测试保存得到的数据,从里面抽取出GD和SP的数据,方便分析以及画图
- constrictionGdSp.m:获取达到最大的迭代次数的最小GD时的测试的数据,画出改进前后GD、SP收敛情况对比图
- StatisticsGdSp.m:统计测试结果中迭代到最大次数时的GD、SP【Best Worst Mean Std】,并将结果写入到excel中
function [MyNSGA2,NSGA2] = readfile(x)
%组建文件名
if x<=5
if x == 5
x = x +1;
end
filename1 = ['MyNSGA2_','zdt',num2str(x),'.mat'];
filename2 = ['NSGA2_','zdt',num2str(x),'.mat'];
%filename = ['ZDT/ZDT',num2str(x),'.txt'];
variable1 = ['MyNSGA2_','zdt',num2str(x)];
variable2 = ['NSGA2_','zdt',num2str(x)];
else
switch x
case 6
filename1 = ['MyNSGA2_','srn','.mat'];
filename2 = ['NSGA2_','srn','.mat'];
variable1 = ['MyNSGA2_','srn'];
variable2 = ['NSGA2_','srn'];
case 7
filename1 = ['MyNSGA2_','tnk','.mat'];
filename2 = ['NSGA2_','tnk','.mat'];
variable1 = ['MyNSGA2_','tnk'];
variable2 = ['NSGA2_','tnk'];
end
end
% 加载对应的文件名,并修改变量名
temp1 = load(filename1);
temp2 = load(filename2);
MyNSGA2 = temp1.(variable1);
NSGA2 = temp2.(variable2);
% 加载真实的前沿数据
%y = importdata(filename);
clc
clear
n = input('输入需要函数序号:');
[MyNSGA2,NSGA2] = readfile(n);
MyNSGA2_pop = MyNSGA2.testPop;
NSGA2_pop = NSGA2.testPop;
[~,x,y,z] = size(MyNSGA2.testPop);
MyNSGA2 = zeros(y,2);
NSGA2 = zeros(y,2);
for i = 1:x
MyNSGA2(:,:) = MyNSGA2_pop(1,i,:,z-3:z-2);
subplot(2,1,1)
plot(MyNSGA2(:,1),MyNSGA2(:,2),'*')
title(['改进的NSGA2测试第',num2str(n),'个函数第 ',num2str(i),' 代结果'])
xlabel('f1(x)');
ylabel('f2(x)');
legend('改进NSGA2测试前沿');
NSGA2(:,:) = NSGA2_pop(1,i,:,z-3:z-2);
subplot(2,1,2)
plot(NSGA2(:,1),NSGA2(:,2),'*')
title(['传统的NSGA2测试第',num2str(n),'个函数第 ',num2str(i),' 代结果'])
xlabel('f1(x)');
ylabel('f2(x)');
legend('传统NSGA2测试前沿');
pause(0.001)
end
function plotTestPareto()
clc
clear
x = input('输入需要函数序号:');
[MyNSGA2,NSGA2] = readfile(x);
% 获取在500代的时候距离指标GD
MyNSGA2_GD = MyNSGA2.testGD(:,end);
NSGA2_GD = NSGA2.testGD(:,end);
% 得到最小距离指标,和测试的id
[MyNSGA2_min_GD,test_id1] = min(MyNSGA2_GD); %最小距离指标
[NSGA2_min_GD,test_id2] = min(NSGA2_GD);
% 打印30次测试所得的最小GD
disp(['改进与传统的最佳GD:',num2str([MyNSGA2_min_GD,NSGA2_min_GD])]);
%得到数据
[~,~,z,w] = size(MyNSGA2.testPop); % 获取纬度和种群信息
MyNSGA2_pop = zeros(z,2); % 初始化数据
NSGA2_pop = zeros(z,2);
MyNSGA2_pop(:,:) = MyNSGA2.testPop(test_id1,end,:,w-3:w-2); % 获取最佳种群
NSGA2_pop(:,:) = NSGA2.testPop(test_id2,end,:,w-3:w-2);
% 画出最后的Pareto前沿
figure(1)
plot(MyNSGA2_pop(:,1),MyNSGA2_pop(:,2),'*')
grid on
xlabel('f1(x)');
ylabel('f2(x)');
legend('改进NSGA2测试前沿');
figure(2)
plot(NSGA2_pop(:,1),NSGA2_pop(:,2),'*')
grid on
xlabel('f1(x)');
ylabel('f2(x)');
legend('传统NSGA2测试前沿');
% 加入真实前沿图
plotTurePareto(x)
function plotTurePareto(x)
% 获取文件名
if x<=5
if x == 5
x = x +1;
end
filename = ['ZDT/ZDT',num2str(x),'.txt'];
end
% 读取数据
y = importdata(filename);
y = sortrows(y,[1,2]);
% 加入真实前沿图
figure(1)
hold on
plot(y(:,1),y(:,2),'-','LineWidth',3)
legend('改进NSGA2测试前沿','已知前沿')
figure(2)
hold on
plot(y(:,1),y(:,2),'-','LineWidth',3)
legend('传统NSGA2测试前沿','已知前沿')
clc
clear
MyNSGA2_GdSp.GD = [];
MyNSGA2_GdSp.SP = [];
NSGA2_GdSp.GD = [];
NSGA2_GdSp.SP = [];
for x = 1:7
disp(x)
if x<=5
i = x;
if x == 5
i = x +1;
end
filename1 = ['MyNSGA2_','zdt',num2str(i),'.mat'];
filename2 = ['NSGA2_','zdt',num2str(i),'.mat'];
%filename = ['ZDT/ZDT',num2str(x),'.txt'];
variable1 = ['MyNSGA2_','zdt',num2str(i)];
variable2 = ['NSGA2_','zdt',num2str(i)];
else
switch x
case 6
filename1 = ['MyNSGA2_','srn','.mat'];
filename2 = ['NSGA2_','srn','.mat'];
variable1 = ['MyNSGA2_','srn'];
variable2 = ['NSGA2_','srn'];
case 7
filename1 = ['MyNSGA2_','tnk','.mat'];
filename2 = ['NSGA2_','tnk','.mat'];
variable1 = ['MyNSGA2_','tnk'];
variable2 = ['NSGA2_','tnk'];
end
end
% 加载对应的文件名,并修改变量名
temp1 = load(filename1);
temp2 = load(filename2);
MyNSGA2 = temp1.(variable1);
NSGA2 = temp2.(variable2);
MyNSGA2_GdSp(x).GD = MyNSGA2.testGD;
MyNSGA2_GdSp(x).SP = MyNSGA2.testSP;
NSGA2_GdSp(x).GD = NSGA2.testGD;
NSGA2_GdSp(x).SP = NSGA2.testSP;
end
save('MyNSGA2_GdSp.mat','MyNSGA2_GdSp')
save('NSGA2_GdSp.mat','NSGA2_GdSp')
function constrictionGdSp()
% 函数序号:
% 1、ZDT1
% 2、ZDT2
% 3、ZDT3
% 4、ZDT4
% 5、ZDT6
% 6、SRN
% 7、TNK
x = input('输入需要函数序号:');
load('MyNSGA2_GdSp.mat')
load('NSGA2_GdSp.mat')
MyNSGA_GD = MyNSGA2_GdSp(x).GD;
MyNSGA_SP = MyNSGA2_GdSp(x).SP;
NSGA_GD = NSGA2_GdSp(x).GD;
NSGA_SP = NSGA2_GdSp(x).SP;
% 获取达到最大的迭代次数的最小GD时的测试的数据
[~,id1] = min(MyNSGA_GD(:,end));
[~,id2] = min(NSGA_GD(:,end));
x = 1:500;
figure(1)
plot(x,MyNSGA_GD(id1,:),x,NSGA_GD(id2,:));
grid on
xlabel('进化代数');
ylabel('GD');
legend('改进的NSGA2的距离指标','传统的NSGA2的距离指标')
title('距离指标GD收敛情况对比图');
figure(2)
plot(x,MyNSGA_SP(id1,:),x,NSGA_SP(id2,:));
grid on
xlabel('进化代数');
ylabel('SP');
legend('改进的NSGA2的分布性指标','传统的NSGA2的分布性指标')
title('分布性指标SP收敛情况对比图');
function StatisticsGdSp()
% 统计30次测试结果中迭代到最大次数时的距离指标和分布性指标
% Best Worst Mean Std
% 将统计的结果写入Excel文件
load('MyNSGA2_GdSp.mat')
load('NSGA2_GdSp.mat')
% 测试次数30,共7个函数
MyNSGA2_Gd = zeros(30,7);
MyNSGA2_Sp = zeros(30,7);
NSGA2_Gd = zeros(30,7);
NSGA2_Sp = zeros(30,7);
for i = 1:7
MyNSGA2_Gd(:,i) = MyNSGA2_GdSp(i).GD(:,end);
MyNSGA2_Sp(:,i) = MyNSGA2_GdSp(i).SP(:,end);
NSGA2_Gd(:,i) = NSGA2_GdSp(i).GD(:,end);
NSGA2_Sp(:,i) = NSGA2_GdSp(i).SP(:,end);
end
% 合并两个算法的GD和SP,前为传统,后为改进
% 分别计算每个算法的Best Worst Mean Std
% GD2:改进,GD1:传统
[MyNSGA2_Gd_Best,~] = min(MyNSGA2_Gd);
[MyNSGA2_Gd_Worst,~] = max(MyNSGA2_Gd);
MyNSGA2_Gd_Mean = mean(MyNSGA2_Gd);
MyNSGA2_Gd_Std = std(MyNSGA2_Gd);
GD2 = [MyNSGA2_Gd_Best;MyNSGA2_Gd_Worst;MyNSGA2_Gd_Mean;MyNSGA2_Gd_Std];
[NSGA2_Gd_Best,~] = min(NSGA2_Gd);
[NSGA2_Gd_Worst,~] = max(NSGA2_Gd);
NSGA2_Gd_Mean = mean(NSGA2_Gd);
NSGA2_Gd_Std = std(NSGA2_Gd);
GD1 = [NSGA2_Gd_Best;NSGA2_Gd_Worst;NSGA2_Gd_Mean;NSGA2_Gd_Std];
% 计算每个的SP
% SP2:改进,SP1:传统
[MyNSGA2_Sp_Best,~] = min(MyNSGA2_Sp);
[MyNSGA2_Sp_Worst,~] = max(MyNSGA2_Sp);
MyNSGA2_Sp_Mean = mean(MyNSGA2_Sp);
MyNSGA2_Sp_Std = std(MyNSGA2_Sp);
SP2 = [MyNSGA2_Sp_Best;MyNSGA2_Sp_Worst;MyNSGA2_Sp_Mean;MyNSGA2_Sp_Std];
[NSGA2_Sp_Best,~] = min(NSGA2_Sp);
[NSGA2_Sp_Worst,~] = max(NSGA2_Sp);
NSGA2_Sp_Mean = mean(NSGA2_Sp);
NSGA2_Sp_Std = std(NSGA2_Sp);
SP1 = [NSGA2_Sp_Best;NSGA2_Sp_Worst;NSGA2_Sp_Mean;NSGA2_Sp_Std];
disp('开始写入数据')
% ZDT 函数
% 数据写入Excel
GD = [GD1(:,1:5);GD2(:,1:5)];
SP = [SP1(:,1:5);SP2(:,1:5)];
xlswrite('StatisticsGdSp.xls',GD,'Sheet1','C2:G9');
xlswrite('StatisticsGdSp.xls',SP,'Sheet2','C2:G9');
% % 对srn、tnk函数的写入
GD = [GD1(:,6:7)',GD2(:,6:7)'];
SP = [SP1(:,6:7)',SP2(:,6:7)'];
xlswrite('StatisticsGdSp.xls',GD,'Sheet3','B3:I4');
xlswrite('StatisticsGdSp.xls',SP,'Sheet4','B3:I4');
disp('数据已写入')
画zdt4函数为例:会画出整个的迭代过程种群情况
画zdt4函数为例:得到测试前言与已知前沿的对比
先运行SaveOnlyGdSp,得到MyNSGA2_GdSp.mat和NSGA2_GdSp.mat之后,再执行constrictionGdSp
结果可以参考
基于NSGA-II算法的研究和改进
画zdt1函数为例:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。