首页 分享 【单目标优化算法】食肉植物优化算法(Matlab代码实现)

【单目标优化算法】食肉植物优化算法(Matlab代码实现)

来源:花匠小妙招 时间:2025-01-01 05:15

欢迎来到本博客❤️❤️

博主优势:博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。

⛳️座右铭:行百里者,半于九十。

本文目录如下:

目录

1 概述

1.1 初始化

1.2 分类和分组

1.3 生长

1.4 繁殖

1.5 重组过程

2 运行结果

3 参考文献

4 Matlab代码实现

1 概述

食肉植物算法(CPA)从随机初始化一组解决方案开始。然后将溶液分类为食肉植物和猎物,然后根据生长和繁殖过程进行分组。它们的适应值将更新,所有解决方案都将合并。该过程将继续,直到满足终止条件。每个过程的细节解释如下:

1.1 初始化

CPA是一种基于种群的优化算法,因此,它从初始化潜在问题的潜在解的种群开始。首先,在湿地中随机初始化由食肉植物和猎物组成的个体种群。食肉植物和猎物的数量分别表示为NCPLANT和NPREY。

1.2 分类和分组

接下来,根据其适应度值按升序对等式(2)中的每个个体进行排序(考虑最小化问题)。排序种群的顶部植物解被认为是食肉植物CP,而剩余解(nPrey)是猎物猎物。图2展示了食肉植物和猎物的可视化。排序适应度和排序总体的矩阵可描述为不等式。分组过程需要模拟每个食肉植物及其猎物的环境。在分组过程中,具有最佳适应值的猎物被分配给排名第一的食肉植物。类似地,第二和第三类猎物分别被分配到第二和第三类食肉植物。重复该过程,直到NCplanth级猎物被分配到NCplanth级食肉植物。然后,将第1级猎物分配给第一级食肉植物,以此类推。CPA中的分组过程如下图所示。3.这一分组对于减少有助于肉食性植物生长的许多劣质猎物的可能性至关重要,这对于提高肉食性植物的生存能力非常重要。

1.3 生长

由于土壤营养贫乏,肉食性植物为了生长而吸引、捕获和消化猎物。猎物被其甜美的气味吸引到植物身上,但可能会间歇性地成功逃脱食肉植物的魔爪。这里,引入了吸引率。对于每组,随机选择一个猎物。如果吸引率高于随机生成的数字,则食肉植物会捕获并消化猎物以进行生长。

1.4 繁殖

食肉植物从猎物身上吸收养分,并利用这些养分进行生长和繁殖。就繁殖而言,只有排名第一的食肉植物,即。E种群中最好的解决方案是允许繁殖。这是为了确保CPA的开发只关注最佳解决方案。可以避免对其他解决方案进行不必要的利用,从而节省计算成本。

1.5 重组过程

新产生的食肉植物和猎物与以前的种群相结合,形成一个新的种群[n nCPlant(group_iter)nCPlant]×d维度。更具体地说,个体、nCPlant(groupiter)个体和nCPlant个体分别是来自原始种群、生长过程和繁殖过程的个体。随后,根据适应度值按升序对这组新个体进行排序。然后从该组中选择排名前N的个体作为新的候选解决方案。因此,这种精英主义选择策略确保选择更合适的解决方案在下一代中进行繁殖。

2 运行结果

部分代码:


function [best,fmin]=CPA(Lb,Ub,Dim,opt,tol)
obj=zeros(1,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
for t=1: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);
    N_iter=N_iter+1;
   obj(t) =fmin;
   plot(obj(1:t),'r','LineWidth',2);
   xlabel('迭代次数')
   ylabel('最优解')
   title('食肉植物优化算法')
   drawnow
%     if N_iter >= 14000
%         break;
%     end
end

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 参考文献

部分理论来源于网络,如有侵权请联系删除。

[1]刘丁铨,高俊涛.基于食肉植物算法的状态序列搜索[J/OL].计算机系统应用:1-6[2023-02-01].DOI:10.15888/j.cnki.csa.008985.

4 Matlab代码实现

相关知识

【优化求解】基于萤火虫算法GSO求解最优目标matlab代码
基于花授粉算法优化实现SVM数据分类
花授粉优化算法及代码实现
实现花卉分类的 MATLAB 代码及实例
【优化覆盖】基于matlab入侵杂草和花授粉混合算法无线传感器覆盖优化问题【含Matlab源码 1328期】
改进的花朵授粉算法程序(Matlab)资源
2024年最新优化算法——苔藓生长优化算法,原理详解,MATLAB代码免费获取
遗传算法Matlab代码实现及其在推荐系统中的应用
【优化求解】基于动态全局搜索和柯西变异改进的花授粉算法matlab源码
MATLAB植物虫害识别

网址: 【单目标优化算法】食肉植物优化算法(Matlab代码实现) https://www.huajiangbk.com/newsview1392210.html

所属分类:花卉
上一篇: 智能优化算法
下一篇: 雅思OGtest7阅读passa

推荐分享