[Optimization] artificial fish school algorithm AF matlab source code

1. Introduction

1.1 Foraging behavior
  refers to the behavior of fish swimming in the direction of more food. The artificial fish X i X_iXi randomly selects a state X j X_jXj in its field of view, and calculates their objective function values ​​for comparison. If found Y j Y_jYj​ is better than Y i Y_iYi​ (Y j Y_jYj​ and Y i Y_iYi​ are the fitness values ​​of X j X_jXj​ and X i X_iXi​, respectively), then Xi moves one step in the direction of Xj; otherwise, Xi X_iXi continues to select the state X j X_jXj in its field of view to determine whether the advancement condition is met. After repeatedly trying trynumber trynumbertrynumber, but the advancement condition is still not met, it will move randomly one step to make X i X_iXi reach a new state. The expression is as follows:
X j = X i + rand () ∗ visual (1) X_j=X_i+rand()*visual \tag{1}Xj​=Xi​+rand()∗visual(1)                
X next = X i + rand () ∗ step ∗ X j − X i ∣ ∣ X j − X i ∣ ∣ (2) X_(next)=X_i+rand() step \frac(X_j-X_i){\left | \left | X_j-X_i \right | \right |}\tag{2}Xnext​=Xi​+rand()∗step∗∣∣Xj​−Xi​∣∣Xj​−Xi​​(2)
X next = X i + rand () ∗ step (3) X_{next}=X_i+rand()*step \tag{3}Xnext​=Xi​+rand()∗step(3)                
  Where rand() is a random number between 0 and 1.
Insert picture description here
Visual description of artificial fish Visual description of artificial fish Visual description of artificial fish The
  frame diagram is as follows: The
Insert picture description here
pseudo code segment is as follows:

for i = 1:N
	for j = 1:Try_number
        Xj=x(i)+Visual.*rand();%人工鱼Xi按式(1)在其视野内随机选择一个状态Xj
        if f(Xj)<f(x(i))	   %比较Xj和Xi的适应度
            X_next= x(i)+rand()*step*(Xj-x(i))/norm(Xj-x(i)); %人工鱼Xi按式(2)朝着Xj方向移动一步,norm()函数表示二范数 
            break;
        else
        	X_next=x(i)+step*rand();
        end
    end
end

1.2 Group behavior

In the process of swimming, fish will naturally gather in groups in order to ensure their own survival and avoid harm. The artificial fish X i X_iXi​ searches for the number of partners in its field of view (dij <visual d_{ij}<visualdij​<visual) nf n_fnf​ and the center position X c X_cXc​, if Y c / nf <δ Y i Y_c/n_f <δY_iYc​/nf​<δYi​(The less than sign is used when seeking the minimum value, and the opposite is the case when seeking the maximum value; Y c Y_cYc​ and Y i Y_iYi​ are the adaptation of X c X_cXc​ and X i X_iXi​ Degree value), indicating that the partner's center position is in a better state and not too crowded, then X i X_iXi​ moves one step toward the partner's center position, otherwise the foraging behavior is performed; the
  frame diagram is as follows: the
Insert picture description here
pseudo code segment is as follows:

nf=0;X_inside=0;
for i = 1:N
    for j = 1:N   
        if norm(x(j)-x(i))<Visual 		% 求人工鱼Xi与其他人工鱼之间的距离
            nf = nf+1;                  %统计在视野范围内的鱼数量   
            X_inside= X_inside+x(j);    %将视野范围内的鱼进行累加
        end
         X_inside=X_inside-x(i);  		%需要去除Xi本身;因为在 一开始计算时,i=j,把中心的鱼也进行了一次计算
         nf=nf-1;   
         Xc = X_inside/nf; 				%此时Xc表示Xi感知范围其他伙伴的中心位置; 
         if  f(Xc)/nf < δ*f(x(i))
            x_next=x(i)+rand*Step*(Xc-x(i))/norm(Xc-x(i)); 
         else
             进行觅食行动
         end
    end
end

1.3 Rear-end behavior

Refers to a behavior of a fish moving in the optimal direction within its field of view. Artificial fish X i X_iXi​Search for the individual X j X_jXj with the highest fitness in their visual field (dij <visual d_{ij}<visualdij​<visual), and their fitness value is Y j Y_jYj​, and explore the artificial fish X j X_jXj​The number of partners in the field of view nf n_fnf​, if Y j / nf <δ Y i Y_j/n_f< δY_iYj​/nf​<δYi​, indicating that X j X_jXj​ is in good condition and not too crowded, then X i X_iXi ​Move one step to the position of X j X_jXj​, otherwise perform foraging behavior; the
  frame diagram is as follows: the
Insert picture description here
pseudo code segment is as follows:

Y_max=inf;nf=0;
for i = 1:N    
    %搜索人工鱼Xi视野范围内的最高适应度个体Xj
    for j = 1:N     
        if norm(x(j)-x(i))<Visual && f(x(j))<Y_max		% 求人工鱼Xi与其他人工鱼之间的距离
            X_max=x(j);        
            Y_max=f(x(j));
        end
    end
    %搜索人工鱼Xj视野范围内的伙伴数量
    for j = 1:N        
        if(norm(x(j)-X_max)<Visual)       
            nf=nf+1;
        end
    end
    nf=nf-1;%去掉他本身
    if Y_max/nf<delta*f(x(i))
        x_next= x(i,:)+rand*Step.*(temp_maxX-x(i,:))./norm(temp_maxX-x(i,:));
    else
        进行觅食行为;
    end
end

1.4 Algorithm overview

In summary, the algorithm will perform clustering and rear-end collision at the same time during the operation. Foraging behavior belongs to these two behaviors. When the crowded object is found or the crowding near the tail-end object is too large, the artificial fish chooses the behavior mode. If during the foraging process, no artificial fish with higher fitness than its own is found, then Move randomly according to the step size. Finally, the fitness values ​​obtained from clustering behavior and rear-end collision behavior are compared, and excellent artificial fish are selected as the next generation of individuals. The overall framework diagram is as follows:
Insert picture description here
3 Analysis of the crowdedness factor δ δδ
3.1 The value of the crowdedness factor

In the problem of finding the minimum value: δ = α nmax, α ∈ (0, 1] δ=αn_{max}, α∈(0,1]δ=αnmax​,α∈(0,1]
  is seeking the maximum In the value problem: δ = 1 α nmax, α ∈ (0, 1] δ=\frac{1}{αn_{max}},α∈(0,1]δ=αnmax​1​,α∈(0, 1]
  Where α αα is the extreme close level, and nmax n_{max}nmax​ is the maximum number of artificial fishes expected to gather in the neighborhood.

3.2 The mechanism of congestion factor

The description of rear-end collision behavior
Insert picture description here
. In the figure, af0 is the optimal artificial fish of the artificial fish af1-5 in their respective field of view. The physical concentration is Y j Y_jYj​, and C1 is the circle with af0 as the center and the field of view as the radius. The farthest distance to detect af0, the closer the artificial fish is to af0, the better the state.
  In the case of finding the maximum value: when δ nf ≤ 1 δn_f\leq 1δnf​≤1, all artificial fish af1-5 perform rear-end collision and swim towards af0;
δ = 1 α nmax δ=\frac{1}{ max {[delta]}} αn_ αnmax. 1 =
[delta] NF NF = δn_f. 1 ≤ Nmax of [alpha] = \ {FRAC n_F max {} {}} αn_ \ Leq = αnmax 1δnf NF ≦ 1
  when the α αα = 1 At that time, it can be clearly seen that nf ≤ nmax n_f \leq n_{max}nf​≤nmax​, which means that the artificial fish field of vision is not crowded.

When δ nf > 1 δn_f > 1δnf​>1, if the food concentration of C2 is the isometric food circle of Y j δ nf \frac{Y_j}{δn_f }δnf​Yj​​, the artificial fish between C2 and C1 Af1, af2, and af3 perform rear-end collisions and swim towards af0, while artificial fish af4 and af5 perform foraging behavior. At this time, the larger the δnf is, the fewer artificial fish will perform the rear-end collision, and vice versa.

3.2 The influence of crowding factor

Take the maximum value for example (the minimum value is just the opposite of the maximum value). The larger δ δδ, the smaller the allowable degree of congestion, and the stronger the artificial fish’s ability to get rid of the local optimum; but the speed of convergence will be The slowdown is mainly due to the fact that while the artificial fish is approaching the extreme value, they will randomly walk away or be repelled by other artificial fish to avoid overcrowding and cannot accurately approach the extreme point. It can be seen that the introduction of δ δδ prevents artificial fish from being overcrowded and falling into local extreme values. On the other hand, this parameter will cause mutual repulsion between artificial fish located near the extreme point, and it is difficult to accurately approximate the extreme point. Therefore, for some specific problems where the local extremum is not very serious, the crowding factor can be ignored, thus simplifying the algorithm, but also speeding up the convergence speed of the algorithm and improving the accuracy of the result.

Second, the source code

clc,clear,close all
warning off
tic
figure(1);hold on

%% 参数设置
fishnum=100; % 生成100只人工鱼
MAXGEN=50;   % 最多迭代次数
try_number=100;  % 最多试探次数
visual=1;    % 感知距离
delta=0.618; % 拥挤度因子
step=0.1;    % 步长

%% 初始化鱼群
lb_ub=[-3,3,2;];
X=AF_init(fishnum,lb_ub);  % 初始化
LBUB=[];
for i=1:size(lb_ub,1)
    LBUB=[LBUB;repmat(lb_ub(i,1:2),lb_ub(i,3),1)]; 
end
gen=1;
BestY = -1*ones(1,MAXGEN); % 每步中最优的函数值
BestX = -1*ones(2,MAXGEN); % 每步中最优的自变量
besty = -100;              % 最优函数值
Y=AF_foodconsistence(X);   % 待优化目标函数
while gen<=MAXGEN
    disp(['迭代步数:  ',num2str(gen)])
    
    for i=1:fishnum
        % 聚群行为
        [Xi1,Yi1]=AF_swarm(X,i,visual,step,delta,try_number,LBUB,Y); 
        % 追尾行为
        [Xi2,Yi2]=AF_follow(X,i,visual,step,delta,try_number,LBUB,Y);
        if Yi1>Yi2
            X(:,i)=Xi1;
            Y(1,i)=Yi1;
        else
            X(:,i)=Xi2;
            Y(1,i)=Yi2;
        end
    end
    
    [Ymax,index]=max(Y);
    figure(1);
    plot(X(1,index),X(2,index),'.','color',[gen/MAXGEN,0,0])
    if Ymax>besty
        besty=Ymax;
        bestx=X(:,index);
        BestY(gen)=Ymax;
        [BestX(:,gen)]=X(:,index);
    else
        BestY(gen)=BestY(gen-1);
        [BestX(:,gen)]=BestX(:,gen-1);
    end
    gen=gen+1;
    
end

Three, running results

Insert picture description here
Insert picture description here
Insert picture description here

Four, remarks

Complete code or write on behalf of adding QQ1575304183

Past review>>>>>>

[Optimization solution] Chaos particle swarm matlab source code

[Optimization] Coyote algorithm matlab source code

[Optimization] based on chaos reverse learning to improve gray wolf algorithm matlab source code

[Optimization] Particle swarm optimization gray wolf algorithm matlab source code

[Optimization solution] Improved gray wolf algorithm for solving heavy oil pyrolysis model matlab source code

[Multi-objective optimization solution] Multi-objective gray wolf optimization algorithm MOGWOmatlab source code

[Multi-objective optimization solution] Multi-objective optimization solution based on Golden Eagle algorithm (MOGEO) matlab source code

[Optimized solution] Mayfly algorithm matlab source code

[Optimization solution] balance optimizer algorithm matlab source code

[Optimization solution] Sparrow algorithm matlab source code

[Optimization solution] Pathfinder optimization algorithm matlab source code

[Optimization solution] Improved firefly algorithm matlab source code

[Optimization] Krill Herd Algorithm (KHA)

[Optimization] Differential Evolution (Differential Evolution) matlab source code

[Optimization] Coronavirus group immune optimization algorithm (CHIO) matlab source code

[Optimization solution] Golden Eagle optimization solution algorithm (GEO) matlab source code

[Optimization] based on the golden sine algorithm GoldSA intelligent optimization algorithm matlab source code

[Optimization solution] genetic optimization membership function matlab source code

[Optimization solution] Multi-objective intelligent algorithm optimization solution matlab toolbox

Guess you like

Origin blog.csdn.net/qq_34763204/article/details/113746356