完整可以运行的数值优化遗传算法源代码
[CrossOverPop]=CrossOver(selectpop,pCross,OptsCrossOver(eranum,:));%交叉
[MutationPop]=Mutation(CrossOverPop,fit,pMutation,VarNum,OptsMutation(eranum,:)); %变异
[InversionPop]=Inversion(MutationPop,pInversion);%倒位
%更新种群
if options(1)==1
Pop=Lb+InversionPop.*(Ub-Lb);%还原Pop
else
Pop=InversionPop;
end
pMutation=pm0+(eranum^3)*(pCross/2-pm0)/(eranum^4); %逐步增大变异率至1/2交叉率
percent=num2str(round(100*eranum/MaxEranum));
waitbar(eranum/MaxEranum,H,['Evolution complete ',percent,'%']);
eranum=eranum+1;
end
close(H);
% 格式化输出进化结果和解的变化情况
t=1:MaxEranum;
plot(t,Trace,t,Meanfit);
legend('解的变化','种群的变化');
title('函数优化的遗传算法');
xlabel('进化世代数');
ylabel('每一代最优适应度');
[MaxFval,MaxFvalIn]=max(Trace);
if options(1)==1|options(1)==3
X=BestPop(MaxFvalIn,:);
elseif options(1)==0
X=b2f(BestPop(MaxFvalIn,:),bounds,bits);
end
hold on;
plot(MaxFvalIn,MaxFval,'*');
text(MaxFvalIn+5,MaxFval,['FMAX=' num2str(MaxFval)]);
str1=sprintf(' Best generation:\n %d\n\n Best X:\n %s\n\n MaxFval\n %f\n',...
MaxFvalIn,num2str(X),MaxFval);
disp(str1);
% -计时
T2=clock;
elapsed_time=T2-T1;
if elapsed_time(6)<0
elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1;
end
if elapsed_time(5)<0
elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1;
end
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库遗传算法Matlab源代码(3)在线全文阅读。
相关推荐: