Skip to main content
. 2025 Jun 7;10(6):379. doi: 10.3390/biomimetics10060379
clc
clear all
pack
%%%%%%%%%% n is dimension of objective function
n = 30;
%%%%%%%%%% Define the Range of Variables
Xmax = ones(1,n) ×100;
Xmin = ones(1,n)*(−100);
%%%%%%%%%% n is the Population Size
N = 150;
%%%%%%%%%% Maximum Numbe od Iteration
Iter_max = 500;
%%%%%%%%%% Define the GBFIO prameters
f = 2.5;
eta = 1.25;
Zeta = 0.3;
fishing_number = 25;
%%%%%%%%%% Generate the Initial Populations
for i = 1:N
  Ipop(i,:) = rand(1,n).*(Xmax − Xmin) + Xmin;
  cost(i,:) = sum(Ipop(i,:).^2);
end
%%%%%%%%%% Sort the Initial Populations Based on Cost Function
Ipop_mix = [Ipop,cost];
Ipop_sort = sortrows(Ipop_mix,n + 1);
%%%%%%%%%% Specify the Fish, Honey, Body, Shell, and Plants
Fish = Ipop_sort(1,:);
Honey = Ipop_sort(2,:);
Body = Ipop_sort(3,:);
Shell = Ipop_sort(4,:);
Plants = Ipop_sort(5,:);
%%%%%%%%%% Start the Iterationa for Updating Populations
for iter = 1:Iter_max
  for i = 1:N
    %%%%%%%%%% Start the Searching Phase
    r1 = rand(1,n);
    deltax_fish = (2.*r1.*( (r1).^0.5)).*(Fish(1,[1:n]) − Ipop(i,:));
    deltax_honey = (r1.*((r1).^0.5)).*(Honey(1,[1:n]) − Ipop(i,:));
    deltax_body = (0.5.*r1.*((r1).^0.5)).*(Body(1,[1:n]) − Ipop(i,:));
    deltax_shell = (0.25.*r1.*((r1).^0.5)).*(Shell(1,[1:n]) − Ipop(i,:));
    deltax_plants = (0.125.*r1.*((r1).^0.5)).*(Plants(1,[1:n]) − Ipop(i,:)); Xnew_search = Ipop(i,:) + deltax_fish + deltax_honey + deltax_body + deltax_shell + deltax_plants;
    Xnew_search = min(Xnew_search,Xmax);
    Xnew_search = max(Xnew_search,Xmin);
cost_S = sum(Xnew_search.^2);
    %%%%%%%%%% Update the Population
    if cost_S < cost(i,1)
      Ipop(i,:) = Xnew_search;
      cost(i,1) = cost_S;
    end
    %%%%%%%%%% End the Searching Phase
    %%%%%%%%%% Start the Hunting Phase
    %%%%% Start the Hunting Phase: Bear Hunting
    Ipop_mix = [Ipop,cost];
    Ipop_sort = sortrows(Ipop_mix,n + 1);
    Prey = Ipop_sort(1,:);
    A = (f*(1-iter/Iter_max)).*(2.*rand(1,n) − 1);
    D = abs(((2.*rand(1,n)).*Ipop(i,:))-(rand(1,n).*Prey(1,[1:n])));
    X_hunt = Ipop(i,:)-A.*D;
    X_hunt = min(X_hunt,Xmax);
    X_hunt = max(X_hunt,Xmin);
    cost_hunt = sum(X_hunt.^2);
    %%%%% Start the Hunting Phase: Coyote Hunting
    L = randperm(N);
    LL = find(L~ = i);
    LLL = L(LL);
    J1 = LL(1);
    J2 = LL(2);
    J3 = LL(3);
    I_child = [Ipop(J1,:),cost(J1,1);Ipop(J2,:),cost(J2,1);Ipop(J3,:),cost(J3,1)];
    I_chsort = sortrows(I_child,n + 1);
    A1 = (eta*(1-iter/Iter_max)).*(2.*rand(1,n) − 1);
    A2 = (eta*(1-iter/Iter_max)).*(2.*rand(1,n) − 1);
    D1 = abs(((2.*rand(1,n)).*I_chsort(2,[1:n])) − (rand(1,n).*I_chsort(1,[1:n])));
    D2 = abs(((2.*rand(1,n)).*I_chsort(3,[1:n])) − (rand(1,n).*I_chsort(1,[1:n])));
    X_ch = Ipop(i,:) − (A1.*D1 + A2.*D2);
    X_ch = min(X_ch,Xmax);
    X_ch = max(X_ch,Xmin);
    cost_ch = sum(X_ch.^2);
    %%%%%%%%%% Update the Population
    G = rand(1,1);
    if G < 0.75
      if cost_hunt < cost(i,1)
        Ipop(i,:) = X_hunt;
        cost(i,1) = cost_hunt;
      end
    else
      if cost_ch < cost(i,1)
        Ipop(i,:) = X_ch;
        cost(i,1) = cost_ch;
      end
    end
    %%%%%%%%%% End the Hunting Phase
    %%%%%%%%%% Start the Fishing Phase
    for kk = 1:fishing_number
      Z = Zeta*(1 − iter/Iter_max)*cos(2*pi*rand(1,1));
      X_fish = (1 + Z).*Ipop(i,:);
      X_fish = min(X_fish,Xmax);
      X_fish = max(X_fish,Xmin);
      cost_fishing = sum(X_fish.^2);
      %%%%%%%%%% Update the Population
      if cost_fishing < cost(i,1)
        Ipop(i,:) = X_fish;
        cost(i,1) = cost_fishing;
      end
    end
    %%%%%%%%%% Specify the Fish, Honey, Body, Shell, and Plants for
    Ipop_mix = [Ipop,cost];
    Ipop_sort = sortrows(Ipop_mix,n + 1);
    Fish = Ipop_sort(1,:);
    Honey = Ipop_sort(2,:);
    Body = Ipop_sort(3,:);
    Shell = Ipop_sort(4,:);
    Plants = Ipop_sort(5,:);
  end
  %%%%%%%%%% Specify the Parameters of Convergence Curve
  Convergence_Curve(iter,1) = iter;
  Convergence_Curve(iter,2) = Ipop_sort(1,n + 1);
end
%%%%%%%%%% Print the Best Soultion
Best = Ipop_sort(1,:)
plot(Convergence_Curve(:,1),Convergence_Curve(:,2))