SlideShare a Scribd company logo
% ======================================================== %
% Files of the Matlab programs included in the book: %
% Xin-She Yang, Nature-Inspired Metaheuristic Algorithms, %
% Second Edition, Luniver Press, (2010). www.luniver.com %
% ======================================================== %
% -------------------------------------------------------- %
% Bat-inspired algorithm for continuous optimization (demo)%
% Programmed by Xin-She Yang @Cambridge University 2010 %
% -------------------------------------------------------- %
% Usage: bat_algorithm([20 0.25 0.5]); %
function [best,fmin,N_iter]=bat_algorithm(para)
% Display help
help bat_algorithm.m
% Default parameters
if nargin<1, para=[10 0.25 0.5]; end
n=para(1); % Population size, typically 10 to 25
A=para(2); % Loudness (constant or decreasing)
r=para(3); % Pulse rate (constant or decreasing)
% This frequency range determines the scalings
Qmin=0; % Frequency minimum
Qmax=2; % Frequency maximum
% Iteration parameters
tol=10^(-5); % Stop tolerance
N_iter=0; % Total number of function evaluations
% Dimension of the search variables
d=3;
% Initial arrays
Q=zeros(n,1); % Frequency
v=zeros(n,d); % Velocities
% Initialize the population/solutions
for i=1:n,
Sol(i,:)=randn(1,d);
Fitness(i)=Fun(Sol(i,:));
end
% Find the current best
[fmin,I]=min(Fitness);
best=Sol(I,:);
% ====================================================== %
% Note: As this is a demo, here we did not implement the %
% reduction of loudness and increase of emission rates. %
% Interested readers can do some parametric studies %
% and also implementation various changes of A and r etc %
% ====================================================== %
% Start the iterations -- Bat Algorithm
while (fmin>tol)
% Loop over all bats/solutions
for i=1:n,
Q(i)=Qmin+(Qmin-Qmax)*rand;
v(i,:)=v(i,:)+(Sol(i,:)-best)*Q(i);
S(i,:)=Sol(i,:)+v(i,:);
% Pulse rate
if rand>r
S(i,:)=best+0.01*randn(1,d);
end
% Evaluate new solutions
Fnew=Fun(S(i,:));
% If the solution improves or not too loudness
if (Fnew<=Fitness(i)) & (rand<A) ,
Sol(i,:)=S(i,:);
Fitness(i)=Fnew;
end
% Update the current best
if Fnew<=fmin,
best=S(i,:);
fmin=Fnew;
end
end
N_iter=N_iter+n;
end
% Output/display
disp(['Number of evaluations: ',num2str(N_iter)]);
disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);
% Objective function -- Rosenbrock's 3D function
function z=Fun(u)
z=(1-u(1))^2+100*(u(2)-u(1)^2)^2+100*(u(3)-u(2)^2)^2;
%%%%% ============ end ====================================

More Related Content

Similar to Bat algorithm (demo) (20)

PPTX
Bat Algorithm_Basics
Designage Solutions
 
PDF
Nature-Inspired Metaheuristic Algorithms
Xin-She Yang
 
PDF
A Hybrid Bat Algorithm
Xin-She Yang
 
PDF
1004.4170v1
forticket
 
PDF
Bat Algorithm for Multi-objective Optimisation
Xin-She Yang
 
PDF
A New Metaheuristic Bat-Inspired Algorithm
Xin-She Yang
 
PPTX
DNA ppt for beginners , how dna works in out body
NishthaShah16
 
PDF
A New Metaheuristic Bat-Inspired Algorithm
Xin-She Yang
 
PPTX
batalgorithm-160501121237 (1).pptx
gopikahari7
 
PDF
Bat Algorithm: A Novel Approach for Global Engineering Optimization
Xin-She Yang
 
PPTX
Metaheuristics for software testing
Francisco de Melo Jr
 
PPTX
batalgorithm-170406072944 (4).pptx
gopikahari7
 
PPTX
batalgorithm-170406072944 (4).pptx
gopikahari7
 
PPTX
Bat algorithm
Rohit Gujar
 
PDF
Bat Algorithm: Literature Review and Applications
Xin-She Yang
 
PDF
Nature-Inspired Optimization Algorithms
Xin-She Yang
 
PDF
Hybrid bat-ant colony optimization algorithm for rule-based feature selection...
IJECEIAES
 
PPTX
Natural-Inspired_Amany_Final.pptx
amanyarafa1
 
PDF
How to easily find the optimal solution without exhaustive search using Genet...
Viach Kakovskyi
 
PDF
Nature Inspired Metaheuristic Algorithms
IRJET Journal
 
Bat Algorithm_Basics
Designage Solutions
 
Nature-Inspired Metaheuristic Algorithms
Xin-She Yang
 
A Hybrid Bat Algorithm
Xin-She Yang
 
1004.4170v1
forticket
 
Bat Algorithm for Multi-objective Optimisation
Xin-She Yang
 
A New Metaheuristic Bat-Inspired Algorithm
Xin-She Yang
 
DNA ppt for beginners , how dna works in out body
NishthaShah16
 
A New Metaheuristic Bat-Inspired Algorithm
Xin-She Yang
 
batalgorithm-160501121237 (1).pptx
gopikahari7
 
Bat Algorithm: A Novel Approach for Global Engineering Optimization
Xin-She Yang
 
Metaheuristics for software testing
Francisco de Melo Jr
 
batalgorithm-170406072944 (4).pptx
gopikahari7
 
batalgorithm-170406072944 (4).pptx
gopikahari7
 
Bat algorithm
Rohit Gujar
 
Bat Algorithm: Literature Review and Applications
Xin-She Yang
 
Nature-Inspired Optimization Algorithms
Xin-She Yang
 
Hybrid bat-ant colony optimization algorithm for rule-based feature selection...
IJECEIAES
 
Natural-Inspired_Amany_Final.pptx
amanyarafa1
 
How to easily find the optimal solution without exhaustive search using Genet...
Viach Kakovskyi
 
Nature Inspired Metaheuristic Algorithms
IRJET Journal
 

More from Xin-She Yang (20)

PDF
Cuckoo Search Algorithm: An Introduction
Xin-She Yang
 
PDF
Metaheuristic Algorithms: A Critical Analysis
Xin-She Yang
 
PDF
A Biologically Inspired Network Design Model
Xin-She Yang
 
TXT
Flower Pollination Algorithm (matlab code)
Xin-She Yang
 
PDF
Metaheuristics and Optimiztion in Civil Engineering
Xin-She Yang
 
PDF
A Biologically Inspired Network Design Model
Xin-She Yang
 
PDF
Introduction to Computational Mathematics (2nd Edition, 2015)
Xin-She Yang
 
PDF
Memetic Firefly algorithm for combinatorial optimization
Xin-She Yang
 
PDF
Two-Stage Eagle Strategy with Differential Evolution
Xin-She Yang
 
PDF
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Xin-She Yang
 
PDF
Are motorways rational from slime mould's point of view?
Xin-She Yang
 
PDF
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Xin-She Yang
 
PDF
Test Problems in Optimization
Xin-She Yang
 
PDF
Engineering Optimisation by Cuckoo Search
Xin-She Yang
 
PDF
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Xin-She Yang
 
PDF
Fractals in Small-World Networks With Time Delay
Xin-She Yang
 
PDF
Chaos in Small-World Networks
Xin-She Yang
 
PDF
Cellular Automata, PDEs and Pattern Formation
Xin-She Yang
 
PDF
Biology-Derived Algorithms in Engineering Optimization
Xin-She Yang
 
PDF
Harmony Search as a Metaheuristic Algorithm
Xin-She Yang
 
Cuckoo Search Algorithm: An Introduction
Xin-She Yang
 
Metaheuristic Algorithms: A Critical Analysis
Xin-She Yang
 
A Biologically Inspired Network Design Model
Xin-She Yang
 
Flower Pollination Algorithm (matlab code)
Xin-She Yang
 
Metaheuristics and Optimiztion in Civil Engineering
Xin-She Yang
 
A Biologically Inspired Network Design Model
Xin-She Yang
 
Introduction to Computational Mathematics (2nd Edition, 2015)
Xin-She Yang
 
Memetic Firefly algorithm for combinatorial optimization
Xin-She Yang
 
Two-Stage Eagle Strategy with Differential Evolution
Xin-She Yang
 
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Xin-She Yang
 
Are motorways rational from slime mould's point of view?
Xin-She Yang
 
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Xin-She Yang
 
Test Problems in Optimization
Xin-She Yang
 
Engineering Optimisation by Cuckoo Search
Xin-She Yang
 
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Xin-She Yang
 
Fractals in Small-World Networks With Time Delay
Xin-She Yang
 
Chaos in Small-World Networks
Xin-She Yang
 
Cellular Automata, PDEs and Pattern Formation
Xin-She Yang
 
Biology-Derived Algorithms in Engineering Optimization
Xin-She Yang
 
Harmony Search as a Metaheuristic Algorithm
Xin-She Yang
 
Ad

Recently uploaded (20)

PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Ad

Bat algorithm (demo)

  • 1. % ======================================================== % % Files of the Matlab programs included in the book: % % Xin-She Yang, Nature-Inspired Metaheuristic Algorithms, % % Second Edition, Luniver Press, (2010). www.luniver.com % % ======================================================== % % -------------------------------------------------------- % % Bat-inspired algorithm for continuous optimization (demo)% % Programmed by Xin-She Yang @Cambridge University 2010 % % -------------------------------------------------------- % % Usage: bat_algorithm([20 0.25 0.5]); % function [best,fmin,N_iter]=bat_algorithm(para) % Display help help bat_algorithm.m % Default parameters if nargin<1, para=[10 0.25 0.5]; end n=para(1); % Population size, typically 10 to 25 A=para(2); % Loudness (constant or decreasing) r=para(3); % Pulse rate (constant or decreasing) % This frequency range determines the scalings Qmin=0; % Frequency minimum Qmax=2; % Frequency maximum % Iteration parameters tol=10^(-5); % Stop tolerance N_iter=0; % Total number of function evaluations % Dimension of the search variables d=3; % Initial arrays Q=zeros(n,1); % Frequency v=zeros(n,d); % Velocities % Initialize the population/solutions for i=1:n, Sol(i,:)=randn(1,d); Fitness(i)=Fun(Sol(i,:)); end % Find the current best [fmin,I]=min(Fitness); best=Sol(I,:); % ====================================================== % % Note: As this is a demo, here we did not implement the % % reduction of loudness and increase of emission rates. % % Interested readers can do some parametric studies % % and also implementation various changes of A and r etc % % ====================================================== % % Start the iterations -- Bat Algorithm while (fmin>tol) % Loop over all bats/solutions for i=1:n, Q(i)=Qmin+(Qmin-Qmax)*rand; v(i,:)=v(i,:)+(Sol(i,:)-best)*Q(i); S(i,:)=Sol(i,:)+v(i,:); % Pulse rate if rand>r S(i,:)=best+0.01*randn(1,d); end % Evaluate new solutions Fnew=Fun(S(i,:)); % If the solution improves or not too loudness if (Fnew<=Fitness(i)) & (rand<A) ,
  • 2. Sol(i,:)=S(i,:); Fitness(i)=Fnew; end % Update the current best if Fnew<=fmin, best=S(i,:); fmin=Fnew; end end N_iter=N_iter+n; end % Output/display disp(['Number of evaluations: ',num2str(N_iter)]); disp(['Best =',num2str(best),' fmin=',num2str(fmin)]); % Objective function -- Rosenbrock's 3D function function z=Fun(u) z=(1-u(1))^2+100*(u(2)-u(1)^2)^2+100*(u(3)-u(2)^2)^2; %%%%% ============ end ====================================