1 内容介绍
在这项研究中,受肉食植物如何适应在恶劣环境中生存的启发,提出了一种新颖的元启发式算法,即肉食植物算法(CPA)。 CPA 首先在 30 个具有不同特征的知名基准函数和 7 个 CEC 2017 测试函数上进行了评估。 对其收敛特性和计算时间进行了分析,并与七种广泛使用的元启发式算法进行了比较,并使用Wilcoxon符号秩检验验证了其优越性。 CPA 的适用性在机械工程设计问题和控制五自由度机械臂方向的现实世界具有挑战性的应用中得到了进一步检验。 实验模拟证明了 CPA 在解决全局优化问题方面的优势。
2 仿真代码
function [best,fmin] = mainfile()
clc
clear all
close all
disp(['CPA is solving 100D Step test function.'])
disp(['Please wait...'])
disp(blanks(1)');
tic
%100D step test function variables
d=100;
Lb=-5.12*ones(1,d);
Ub=5.12*ones(1,d);
opt=zeros(d,1);
tol=1e-05;
%Carnivorous Plant Algorithm
[best,fmin]=CPA(Lb,Ub,d,opt,tol);
function [best,fmin]=CPA(Lb,Ub,Dim,opt,tol)
%Define CPA parameters values
group_iter = 2;
attraction_rate = 0.8;
growth_rate = 2;
reproduction_rate = 1.8;
nCP = 10;
nPrey = 20;
nPop=nCP+nPrey;
% Initialize Carnivorous Plants and their surrounding food
ini.position=[];
ini.cost=[];
life=repmat(ini,nPop,1);
NewCP=repmat(ini,nCP*group_iter+nCP,1);
life=CreateInitialPopulation(life,Dim,Ub,Lb);
%Find the current best
for x=1:nCP+nPrey
costs(x,:)=life(x).cost;
end
[fmin,I]=min(costs);
best=life(I).position;
%Find the distance between best and optima solution for 1st row
distance=dist(best,opt);
N_iter=1;
%Start the iterations
while fmin>tol
% Group the Carnivorous Plants with their nearby surrounding foods
% based on distance.
[CP,life]=CPA_Grouping(life,nCP,nPrey,group_iter,N_iter);
% Grow new Carnivorous Plants by hunting preys and birth new preys
% if it is not being hunted.
[NewCP,a]=CPA_Growth(CP,NewCP,Dim,attraction_rate,growth_rate,group_iter);
% Mating between Carnivorous Plants and the best Carnivorous Plants
NewCP=CPA_Reproduction(CP,NewCP,Dim,reproduction_rate,a);
% Check the position and update the cost of NewCP
NewCP=CPA_UpdateCost(NewCP,Dim,Lb,Ub);
% Combine NewCP and pop
[life,BestIndex]=CPA_Combine(NewCP,life);
% Update Best Solution Ever Found
BestSol=life(BestIndex);
%Find current global best
fmin=BestSol.cost;
best=BestSol.position;
%Find the distance between best and optima solution for the rest of
%the row
distance=dist(best,opt);
yy(N_iter)=fmin;
N_iter=N_iter+1;
if N_iter >= 14000
break;
end
end
figure
plot(yy)
xlabel('迭代次数')
ylabel('适应度值')
ElapsedTime=toc;
% Output/display the final result
disp(['Total computational time used is ',num2str(ElapsedTime),' seconds'])
disp(['Total number of function evaluation = ',num2str(nPop+(N_iter-1)*(nCP*group_iter+nCP))])
disp(['Best solution = ',num2str(best,'% .8f')])
disp(['Fmin = ',num2str(fmin,'% .8f')])
function [CP,pop]=CPA_Grouping(pop,nCP,nPrey,Group_Iter,it)
empty_Prey.position=[];
empty_Prey.cost=[];
Select=reshape(1:nPrey,nCP,[]);
% First, sort the population
if it==1
for x=1:nCP+nPrey
costs(x,:)=pop(x).cost;
end
else
for x=1:(2+Group_Iter)*nCP+nPrey
costs(x,:)=pop(x).cost;
end
end
for i=1:nCP+nPrey
[~, I]=min(costs);
index(i)=I;
costs(I)=10^30;
end
pop=pop(index);
Plant=pop(1:nCP);
Prey=pop(nCP+1:end);
empty_CP.Plant=[];
empty_CP.Prey=repmat(empty_Prey,0,1);
empty_CP.nPrey=0;
CP=repmat(empty_CP,nCP,1);
% Group the Carnivorous Plants with their nearby Preys
for q=1:nCP
CP(q).Plant=Plant(q);
for r=1:nPrey/nCP
CP(q).Prey=[CP(q).Prey
Prey(Select(q,r))];
CP(q).nPrey=CP(q).nPrey+1;
end
end
function [NewCP,a]=CPA_Growth(CP,NewCP,Dim,HuntingChance,alpha,Group_Iter)
a=1;
nCP=numel(CP);
for Grp=1:nCP
for Group_cycle=1:Group_Iter
v=randi(CP(Grp).nPrey);
if HuntingChance>rand %Growth of Carnivorous Plant by hunting prey
Step=alpha.*rand(1,Dim);
NewCP(a).position=Step.*CP(Grp).Plant.position...
+(1-Step).*CP(Grp).Prey(v).position;
a=a+1;
else %Mating of Prey
%To ensure prey v and prey j are different
u=v;
while v==u
u=randi(CP(Grp).nPrey);
end
Step=alpha.*rand(1,Dim);
if CP(Grp).Prey(v).cost<CP(Grp).Prey(u).cost
Step=1-Step; %So that it moves to good prey
end
NewCP(a).position=Step.*CP(Grp).Prey(u).position...
+(1-Step).*CP(Grp).Prey(v).position;
a=a+1;
end
end
end
function NewCP=CPA_Reproduction(CP,NewCP,Dim,alpha,a)
%Mating of Carnivorous Plant
for i=1:numel(CP)
for j=1:Dim
%To ensure plant j and plant v are different
v=i;
while v==i
v=randi(numel(CP));
end
Step=CP(i).Plant.position(j)-CP(v).Plant.position(j);
if CP(v).Plant.cost<CP(i).Plant.cost
Step=-Step; %So that it moves to good plant
end
NewCP(a).position(j)=CP(1).Plant.position(j)+alpha.*rand.*(Step);
end
a=a+1;
end
function NewCP=CPA_UpdateCost(NewCP,Dim,Lb,Ub)
n=numel(NewCP);
for i=1:n
Flag4ub=NewCP(i).position>Ub;
Flag4lb=NewCP(i).position<Lb;
NewCP(i).position=(NewCP(i).position.*(~(Flag4ub+Flag4lb)))...
+(rand(1,Dim).*(Ub-Lb)+Lb).*(Flag4ub+Flag4lb);
NewCP(i).cost=Fun(NewCP(i).position,Dim);
end
function [pop,BestIndex]=CPA_Combine(NewCP,pop)
pop=[pop
NewCP];
for i=1:size(pop,1)
costs(i,:)=pop(i).cost;
end
[~,BestIndex]=min(costs);
3 运行结果
编辑
4 参考文献
[1] Ong K M , Ong P , Sia C K . A carnivorous plant algorithm for solving global optimization problems[J]. Applied Soft Computing, 2020, 98(April):106833.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。