The BBO (Biogeography-Based Optimization) algorithm is a nature-inspired optimization technique that draws its principles from the field of biogeography, which studies the distribution of species across different environments. Introduced by Dan Simon in 2008, BBO simulates the migration and colonization processes observed in nature to solve optimization problems. By modeling the exchange of information between candidate solutions as the migration of species between habitats, BBO effectively balances exploration and exploitation to find optimal solutions in complex search spaces. Its adaptability, simplicity, and ability to handle various types of optimization problems make BBO a popular choice in engineering, computational biology, and other fields. In this lesson, a simple MATLAB Code for BBO Algorithm is outlined. Before working on the code, don’t forget to save helper functions.


clc;
clear;
close all;
%% Problem Definition
CostFunction=@(x) sphere_func(x);        % Cost Function
nVar=30;             % Number of Decision Variables
VarSize=[1 nVar];   % Decision Variables Matrix Size
VarMin=-10;         % Decision Variables Lower Bound
VarMax= 10;         % Decision Variables Upper Bound
%% BBO Parameters
MaxIt=800;          % Maximum Number of Iterations
nPop=50;            % Number of Habitats (Population Size)
KeepRate=0.2;                   % Keep Rate
nKeep=round(KeepRate*nPop);     % Number of Kept Habitats
nNew=nPop-nKeep;                % Number of New Habitats
% Migration Rates
mu=linspace(1,0,nPop);          % Emmigration Rates
lambda=1-mu;                    % Immigration Rates
alpha=0.9;
pMutation=0.1;
sigma=0.02*(VarMax-VarMin);
%% Initialization
% Empty Habitat
habitat.Position=[];
habitat.Cost=[];
% Create Habitats Array
pop=repmat(habitat,nPop,1);
% Initialize Habitats
for i=1:nPop
    pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
    pop(i).Cost=CostFunction(pop(i).Position);
end
% Sort Population
[~, SortOrder]=sort([pop.Cost]);
pop=pop(SortOrder);
% Best Solution Ever Found
BestSol=pop(1);
% Array to Hold Best Costs
BestCost=zeros(MaxIt,1);
%% BBO Main Loop
for it=1:MaxIt
    
    newpop=pop;
    for i=1:nPop
        for k=1:nVar
            % Migration
            if rand<=lambda(i)
                % Emmigration Probabilities
                EP=mu;
                EP(i)=0;
                EP=EP/sum(EP);
                
                % Select Source Habitat
                j=RouletteWheelSelection(EP);
                
                % Migration
                newpop(i).Position(k)=pop(i).Position(k) ...
                    +alpha*(pop(j).Position(k)-pop(i).Position(k));
                
            end
            
            % Mutation
            if rand<=pMutation
                newpop(i).Position(k)=newpop(i).Position(k)+sigma*randn;
            end
        end
        
        % Apply Lower and Upper Bound Limits
        newpop(i).Position = max(newpop(i).Position, VarMin);
        newpop(i).Position = min(newpop(i).Position, VarMax);
        
        % Evaluation
        newpop(i).Cost=CostFunction(newpop(i).Position);
    end
    
    % Sort New Population
    [~, SortOrder]=sort([newpop.Cost]);
    newpop=newpop(SortOrder);
    
    % Select Next Iteration Population
    pop=[pop(1:nKeep)
         newpop(1:nNew)];
     
    % Sort Population
    [~, SortOrder]=sort([pop.Cost]);
    pop=pop(SortOrder);
    
    % Update Best Solution Ever Found
    BestSol=pop(1);
    
    % Store Best Cost Ever Found
    BestCost(it)=BestSol.Cost;
    
    % Show Iteration Information
    disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
    
end

In the above MATLAB Code for BBO Algorithm, you have to save the following codes in a separate file. First, save the code of Roulette Wheel Selection, and then save the code for the sphere function.

%Roullete Wheel Selection
function j=RouletteWheelSelection(P)
    r=rand;
    C=cumsum(P);
    j=find(r<=C,1,'first');
end

Here is the code for the sphere function. This is a simple objective function, you can modify this objective function according to your needs, and MATLAB Code for the BBO Algorithm should work just fine.

function result = sphere_func(x)
    % x is a vector of input values
    % result is the value of the sphere function at x
    
    % Compute the sum of squares of elements in x
    result = sum(x.^2);
end

Don’t forget to post your thoughts about this. If you need any help, just put your question in the comment box and our team will try to help you. Happy Learning.

Leave a Reply

Your email address will not be published. Required fields are marked *