[Optimization] Function optimization analysis based on matlab bacterial foraging algorithm [including Matlab source code 217]

1. Introduction

Real life needs promote the development of optimization methods. For nearly half a century, due to the shortcomings of traditional optimization methods, some evolutionary algorithms with global optimization performance and strong versatility have received wide attention and applications in various fields due to their efficient optimization performance and no need to accurately describe the problem. . The earliest and most representative evolutionary algorithm is the genetic algorithm (GA) derived from Darwin's theory of natural selection and Mendelian genetic variation theory in the 1970s. In recent years, people have produced a series of intelligent optimization algorithms based on the behavior of natural organisms. For example, Dorigo et al. proposed the Ant Colony Optimization (ACO) in 1991 by simulating the path-finding behavior of ants; Eberhart and Kennedy adopted In 1995, the Particle Swarm Optimization (PSO) was proposed to simulate the predation behavior of birds. These algorithms are widely used in the engineering field and have achieved remarkable results. With the vigorous development of swarm intelligence optimization algorithms, Passino proposed a Bacteria Foraging Optimization Algorithm (BFOA) in 2002 that simulates the foraging behavior of human E. coli, adding a new member to the family of biomimetic evolutionary algorithms. This chapter will focus on introducing the most basic bacteria foraging algorithm to the majority of programming enthusiasts. Each programming researcher can improve on the algorithm in this chapter and apply it to actual cases.
1 Standard bacterial foraging optimization algorithm
Insert picture description here
2 Chemotaxis operation
Insert picture description here
3 Swarming operation
Insert picture description here
4 Reproduction operation
Insert picture description here
5 Elimination and Dispersal
The local area where bacteria live in the actual environment may occur Gradual changes (such as food exhaustion) or sudden changes (such as a sudden increase in temperature). This may cause the bacterial populations living in this local area to migrate to a new area or be collectively killed by external forces. Simulating this phenomenon in the BFO algorithm is called migratory operation.
6 BFO algorithm flow
Insert picture description here

Second, the source code

%%%%%%%%%%%%-----BF0算法-----%%%%%%%%%%%%%%%
clc; clear;  close all
warning off
feature jit off  % 加速代码执行
%-----初始化参数-----
bounds = [-5.12, 5.12;-5.12, 5.12]; % 函数变量范围
p = 2;   % 搜索范围的维度
s = 26;  % 细菌的个数
Nc = 50; % 趋化的次数
Ns = 4;  % 趋化操作中单向运动的最大步数
C(:,1) = 0.001*ones(s,1);    % 翻转选定方向后,单个细菌前进的步长
Nre = 4;     % 复制操作步骤数
Ned = 2;     % 驱散(迁移)操作数
Sr = s/2;    % 每代复制(分裂)数
Ped = 0.25;  % 细菌驱散(迁移)概率
d_attract = 0.05;         % 吸引剂的数量
ommiga_attract = 0.05;    % 吸引剂的释放速度
h_repellant = 0.05;       % 排斥剂的数量
ommiga_repellant = 0.05;  % 排斥剂的释放速度
for i = 1:s               % 产生初始细菌个体的位置
    P(1,i,1,1,1) = -5.12 + rand*10.24;
    P(2,i,1,1,1) = -5.12 + rand*10.24;
end
%----细菌趋药性算法循环开始
%---- 驱散(迁移)操作开始
for l = 1:Ned
	%-----复制操作开始
    for k = 1:Nre
        %-----趋化操作(翻转或游动)开始
        for j = 1:Nc
            %-----对每一个细菌分别进行以下操作
            for i = 1:s
                %-----计算函数J(i,j,k,l),表示第i个细菌在第l次驱散第k次
                %--------复制第j次趋化时的适应度值
                J(i,j,k,l) = fitness(P(:,i,j,k,l));
                %-----修改函数,加上其它细菌对其的影响
                Jcc = sum(-d_attract*exp(-ommiga_attract*((P(1,i,j,k,l)-...
                    P(1,1:26,j,k,l)).^2+(P(2,i,j,k,l)-P(2,1:26,j,k,l)).^2)))+...
                    sum(h_repellant*exp(-ommiga_repellant*((P(1,i,j,k,l)-...
                    P(1,1:26,j,k,l)).^2+(P(2,i,j,k,l)-P(2,1:26,j,k,l)).^2)));
                J(i,j,k,l) = J(i,j,k,l) + Jcc;
                %----保存细菌目前的适应度值,直到找到更好的适应度值取代之
                Jlast = J(i,j,k,l);
                %-----翻转,产生一个随机向量C(i),代表翻转后细菌的方向
                Delta(:,i) = (2*round(rand(p,1))-1).*rand(p,1);
                % PHI表示翻转后选择的一个随机方向上前进
                PHI = Delta(:,i)/sqrt(Delta(:,i)'*Delta(:,i));
                %-----移动,向着翻转后细菌的方向移动一个步长,并且改变细菌的位置
                P(:,i,j+1,k,l) = P(:,i,j,k,l) + C(i,k)*PHI;
                %-----计算细菌当前位置的适应度值
                J(i,j+1,k,l) = fitness(P(:,i,j+1,k,l));
                %-----游动-----
                m = 0;         % 给游动长度计数器赋初始值
                while(m < Ns)  % 未达到游动的最大长度,则循环
                    m = m + 1;
                    % 新位置的适应度值是否更好?如果更好,将新位置的适应度值
                    % 存储为细菌i目前最好的适应度值
                    if(J(i,j+1,k,l)<Jlast)
                        Jlast = J(i,j+1,k,l);  % 保存更好的适应度值
                        % 在该随机方向上继续游动步长单位,修改细菌位置
                        P(:,i,j+1,k,l) = P(:,i,j+1,k,l) + C(i,k)*PHI;
                        % 重新计算新位置上的适应度值
                        J(i,j+1,k,l) = fitness(P(:,i,j+1,k,l));
                    else
                        % 否则,结束此次游动
                        m = Ns;
                    end
                end

Three, running results

Insert picture description here

Four, remarks

Complete code or writing add QQ1564658423 past review
>>>>>>
[Optimization] based on matlab particle swarm optimization gray wolf algorithm [including Matlab source code 006]
[Optimization] based on matlab multi-objective gray wolf optimization algorithm MOGWO [including Matlab source code issue 007]
[optimized solution] optimal layout of charging stations based on matlab particle swarm algorithm [including Matlab source code 012]
[optimized solution] multi-traveling salesman problem based on matlab genetic algorithm [including Matlab source code 016]
[optimized solution 】Find the shortest path based on matlab genetic algorithm [including Matlab source code 023]
[Optimization solution] 3D packing problem based on matlab genetic and simulated annealing [including Matlab source code 031]
[Optimization solution] Solve the optimization of vehicle departure interval based on matlab genetic algorithm Problem [Including Matlab source code 132]
[Optimizing solution] Krill swarm algorithm [Including matlab source code 133]
[Optimizing solution] Differential evolution algorithm [Including Matlab source code 134]
[Optimizing solution] Penalty function method based on matlab constraint optimization [ Include Matlab source code 163 period]
[Optimization solution] Based on matlab improved gray wolf algorithm to solve the heavy oil pyrolysis model [Include Matlab source code 164 period]
[Optimized solution] Based on matlab ant colony algorithm distribution network fault location [Include Matlab source code 165 period]
[ Optimization solution based on matalb genetic algorithm to solve the island material replenishment optimization problem [including Matlab source code 172]
[Optimization solution] Coronavirus population immune optimization algorithm (CHIO) [including Matlab source code 186]
[Optimization solution] Golden Eagle optimization algorithm (GEO) )【Include Matlab source code 187 period】
[Multi-objective optimization solution] Multi-objective optimization solution based on matlab golden eagle algorithm (MOGEO) [including Matlab source code 188]
[Optimization solution] BP neural network optimization solution based on matlab GUI interface [Matlab source code 208]
[Optimization solution] Genetic algorithm optimization solution based on matlab GUI interface [including Matlab source code 209]
[Optimization solution] Numerical approximation optimization analysis based on matlab immune algorithm [including Matlab source code 211]
[Optimization solution] Function optimization analysis based on matlab heuristic algorithm [ Include Matlab source code 212]
[Optimization solution] Urban traffic signal optimization based on matalb improved genetic algorithm (GA+IGA) [Include Matlab source code 213 period]
[Optimization solution] Urban traffic signal optimization based on matalb improved genetic algorithm GA [ Including Matlab source code 214 period]

[Optimization solution] Urban traffic signal optimization based on matalb improved genetic algorithm IGA [including Matlab source code 215]
[Optimization solution] Matlab penalty function-based particle swarm optimization function optimization [including Matlab source code 216]

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/113642565