欢迎访问GreenSim团队主页→http://blog.sina.com.cn/greensim 邮箱:greensim@163.com hold on end
title('遗传算法第40代') hold on
for i=1:(length(R)-1) x1=XY1(i,1); y1=XY1(i,2); x2=XY1(i+1,1); y2=XY1(i+1,2);
plot([x1,x2],[y1,y2],'k','LineWidth',1); hold on end end
if counter==50
PPP=[0.5,Xp,0.5]'; PPPP=1-PPP;
X=PPP.*X1+PPPP.*X2; Y=PPP.*Y1+PPPP.*Y2; XY2=[X,Y]; figure(7) PlotGraph; hold on
for i=1:(length(R)-1) x1=XY2(i,1); y1=XY2(i,2); x2=XY2(i+1,1); y2=XY2(i+1,2); plot([x1,x2],[y1,y2],'k'); hold on end
title('遗传算法第50代') hold on for i=1:(length(R)-1) x1=XY1(i,1); y1=XY1(i,2); x2=XY1(i+1,1); y2=XY1(i+1,2);
plot([x1,x2],[y1,y2],'k','LineWidth',1); hold on end end
LC2(counter+1)=Yp;
LC1(counter+1)=meanfitness; %% 第五步:变异
第6页
欢迎访问GreenSim团队主页→http://blog.sina.com.cn/greensim 邮箱:greensim@163.com for i=1:N
if Pm>rand&&pos(1)~=i AA=farm{i}; AA(POS)=rand; farm{i}=AA; end end
counter=counter+1; disp(counter); end
%% 输出遗传算法的优化结果 PPP=[0.5,Xp,0.5]'; PPPP=1-PPP;
X=PPP.*X1+PPPP.*X2; Y=PPP.*Y1+PPPP.*Y2; XY2=[X,Y]; L2=Yp;
%% 绘制Fig3 figure(8) PlotGraph; hold on hold on
for i=1:(length(R)-1) x1=XY1(i,1); y1=XY1(i,2); x2=XY1(i+1,1); y2=XY1(i+1,2);
plot([x1,x2],[y1,y2],'k','LineWidth',1); hold on end
for i=1:(length(R)-1) x1=XY2(i,1); y1=XY2(i,2); x2=XY2(i+1,1); y2=XY2(i+1,2); plot([x1,x2],[y1,y2],'k'); hold on end
title('遗传算法最终结果') figure(9) PlotGraph; hold on
for i=1:(length(R)-1) x1=XY1(i,1);
第7页
欢迎访问GreenSim团队主页→http://blog.sina.com.cn/greensim 邮箱:greensim@163.com y1=XY1(i,2); x2=XY1(i+1,1); y2=XY1(i+1,2);
plot([x1,x2],[y1,y2],'k','LineWidth',1); hold on end hold on
for i=1:(length(R)-1) x1=XY2(i,1); y1=XY2(i,2); x2=XY2(i+1,1); y2=XY2(i+1,2);
plot([x1,x2],[y1,y2],'k','LineWidth',2); hold on end
title('遗传算法优化前后结果比较') %% 绘制Fig4 figure(10); plot(LC1); hold on plot(LC2);
xlabel('迭代次数'); title('收敛曲线');
源代码运行结果展示
第8页
欢迎访问GreenSim团队主页→http://blog.sina.com.cn/greensim 邮箱:greensim@163.com
第9页