本文主要是介绍【优化求解】基于蜉蝣算法MA(mayfly algorithm)求解单目标问题matlab源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 简介
2 部分代码
%% clc; clear; close all; %% Problem Definition % Objective Function ANSWER=listdlg('PromptString','Choose Objective Function','SelectionMode','single', 'ListString', {'1. Sphere', '2. Rastrigin'}); if eq(ANSWER,1); ObjectiveFunction=@(x) Sphere(x); funcname='Sphere'; elseif eq(ANSWER,2); ObjectiveFunction=@(x) Rastrigin(x); funcname='Rastrigin'; else; disp('Terminated'); return end ProblemSize=[1 50]; % Decision Variables Size LowerBound=-10; % Decision Variables Lower Bound UpperBound= 10; % Decision Variables Upper Bound %% Mayfly Parameters methname='Mayfly Algorithm'; MaxIt=2000; % Maximum Number of Iterations nPop=20; nPopf=20; % Population Size (males and females) g=0.8; % Inertia Weight gdamp=1; % Inertia Weight Damping Ratio a1=1.0; % Personal Learning Coefficient a2=1.5; a3=1.5; % Global Learning Coefficient beta=2; % Distance sight Coefficient dance=5; % Nuptial Dance fl=1; % Random flight dance_damp=0.8; % Damping Ratio fl_damp=0.99; % Mating Parameters nc=20; % Number of Offsprings (also Parnets) nm=round(0.05*nPop); % Number of Mutants mu=0.01; % Mutation Rate % Velocity Limits VelMax=0.1*(UpperBound-LowerBound); VelMin=-VelMax; %% Initialization empty_mayfly.Position=[]; empty_mayfly.Cost=[]; empty_mayfly.Velocity=[]; empty_mayfly.Best.Position=[]; empty_mayfly.Best.Cost=[]; Mayfly=repmat(empty_mayfly,nPop,1); % Males Mayflyf=repmat(empty_mayfly,nPopf,1); % Females GlobalBest.Cost=inf; funccount=0; for i=1:nPop% Initialize Position of MalesMayfly(i).Position=unifrnd(LowerBound,UpperBound,ProblemSize);% Initialize VelocityMayfly(i).Velocity=zeros(ProblemSize);% EvaluationMayfly(i).Cost=ObjectiveFunction(Mayfly(i).Position);% Update Personal BestMayfly(i).Best.Position=Mayfly(i).Position;Mayfly(i).Best.Cost=Mayfly(i).Cost;funccount=funccount+1;% Update Global Bestif Mayfly(i).Best.Cost<GlobalBest.CostGlobalBest=Mayfly(i).Best;end end for i=1:nPopf% Initialize Position of FemalesMayflyf(i).Position=unifrnd(LowerBound,UpperBound,ProblemSize);Mayflyf(i).Velocity=zeros(ProblemSize);Mayflyf(i).Cost=ObjectiveFunction(Mayflyf(i).Position);funccount=funccount+1;% Update Global Best (Uncomment if you use the PGB-IMA version)%if Mayflyf(i).Best.Cost<GlobalBest.Cost% GlobalBest=Mayflyf(i).Best;%end end BestSolution=zeros(MaxIt,1); %% Mayfly Main Loop for it=1:MaxItfor i=1:nPopf% Update Femalese=unifrnd(-1,+1,ProblemSize);rmf=(Mayfly(i).Position-Mayflyf(i).Position);if Mayflyf(i).Cost>Mayfly(i).CostMayflyf(i).Velocity = g*Mayflyf(i).Velocity ...+a3*exp(-beta.*rmf.^2).*(Mayfly(i).Position-Mayflyf(i).Position);elseMayflyf(i).Velocity = g*Mayflyf(i).Velocity+fl*(e);disp([methname ' on the ' funcname ' Function: Iteration = ' num2str(it) ', ' funcname ', Evaluations = ' num2str(funccount) '. Best Cost = ' num2str(BestSolution(it))]);g=g*gdamp;dance = dance*dance_damp;fl = fl*fl_damp; end %% Results figure; plot(BestSolution,'LineWidth',2); semilogy(BestSolution,'LineWidth',2); xlabel('Iterations'); ylabel('Objective function'); grid on; %%img =gcf; %获取当前画图的句柄 print(img, '-dpng', '-r600', './img.png') %即可得到对应格式和期望dpi的图像
3 仿真结果
4 参考文献
[1]陈伟超, and 符强. "基于倒位变异的蜉蝣优化算法." 计算机系统应用 30.8:7.
这篇关于【优化求解】基于蜉蝣算法MA(mayfly algorithm)求解单目标问题matlab源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!