際際滷

際際滷Share 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

More from Xin-She Yang (20)

Cuckoo Search Algorithm: An Introduction
Cuckoo Search Algorithm: An IntroductionCuckoo Search Algorithm: An Introduction
Cuckoo Search Algorithm: An Introduction
Xin-She Yang
?
Metaheuristic Algorithms: A Critical Analysis
Metaheuristic Algorithms: A Critical AnalysisMetaheuristic Algorithms: A Critical Analysis
Metaheuristic Algorithms: A Critical Analysis
Xin-She Yang
?
Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms
Xin-She Yang
?
A Biologically Inspired Network Design Model
A Biologically Inspired Network Design ModelA Biologically Inspired Network Design Model
A Biologically Inspired Network Design Model
Xin-She Yang
?
Flower Pollination Algorithm (matlab code)
Flower Pollination Algorithm (matlab code)Flower Pollination Algorithm (matlab code)
Flower Pollination Algorithm (matlab code)
Xin-She Yang
?
Nature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic AlgorithmsNature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic Algorithms
Xin-She Yang
?
Metaheuristics and Optimiztion in Civil Engineering
Metaheuristics and Optimiztion in Civil EngineeringMetaheuristics and Optimiztion in Civil Engineering
Metaheuristics and Optimiztion in Civil Engineering
Xin-She Yang
?
A Biologically Inspired Network Design Model
A Biologically Inspired Network Design ModelA Biologically Inspired Network Design Model
A Biologically Inspired Network Design Model
Xin-She Yang
?
Introduction to Computational Mathematics (2nd Edition, 2015)
Introduction to Computational Mathematics (2nd Edition, 2015)Introduction to Computational Mathematics (2nd Edition, 2015)
Introduction to Computational Mathematics (2nd Edition, 2015)
Xin-She Yang
?
Memetic Firefly algorithm for combinatorial optimization
Memetic Firefly algorithm for combinatorial optimizationMemetic Firefly algorithm for combinatorial optimization
Memetic Firefly algorithm for combinatorial optimization
Xin-She Yang
?
Two-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential EvolutionTwo-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential Evolution
Xin-She Yang
?
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Xin-She Yang
?
Bat Algorithm for Multi-objective Optimisation
Bat Algorithm for Multi-objective OptimisationBat Algorithm for Multi-objective Optimisation
Bat Algorithm for Multi-objective Optimisation
Xin-She Yang
?
Are motorways rational from slime mould's point of view?
Are motorways rational from slime mould's point of view?Are motorways rational from slime mould's point of view?
Are motorways rational from slime mould's point of view?
Xin-She Yang
?
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Review of Metaheuristics and Generalized Evolutionary Walk AlgorithmReview of Metaheuristics and Generalized Evolutionary Walk Algorithm
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Xin-She Yang
?
Test Problems in Optimization
Test Problems in OptimizationTest Problems in Optimization
Test Problems in Optimization
Xin-She Yang
?
Engineering Optimisation by Cuckoo Search
Engineering Optimisation by Cuckoo SearchEngineering Optimisation by Cuckoo Search
Engineering Optimisation by Cuckoo Search
Xin-She Yang
?
A New Metaheuristic Bat-Inspired Algorithm
A New Metaheuristic Bat-Inspired AlgorithmA New Metaheuristic Bat-Inspired Algorithm
A New Metaheuristic Bat-Inspired Algorithm
Xin-She Yang
?
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Xin-She Yang
?
Fractals in Small-World Networks With Time Delay
Fractals in Small-World Networks With Time DelayFractals in Small-World Networks With Time Delay
Fractals in Small-World Networks With Time Delay
Xin-She Yang
?
Cuckoo Search Algorithm: An Introduction
Cuckoo Search Algorithm: An IntroductionCuckoo Search Algorithm: An Introduction
Cuckoo Search Algorithm: An Introduction
Xin-She Yang
?
Metaheuristic Algorithms: A Critical Analysis
Metaheuristic Algorithms: A Critical AnalysisMetaheuristic Algorithms: A Critical Analysis
Metaheuristic Algorithms: A Critical Analysis
Xin-She Yang
?
Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms
Xin-She Yang
?
A Biologically Inspired Network Design Model
A Biologically Inspired Network Design ModelA Biologically Inspired Network Design Model
A Biologically Inspired Network Design Model
Xin-She Yang
?
Flower Pollination Algorithm (matlab code)
Flower Pollination Algorithm (matlab code)Flower Pollination Algorithm (matlab code)
Flower Pollination Algorithm (matlab code)
Xin-She Yang
?
Nature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic AlgorithmsNature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic Algorithms
Xin-She Yang
?
Metaheuristics and Optimiztion in Civil Engineering
Metaheuristics and Optimiztion in Civil EngineeringMetaheuristics and Optimiztion in Civil Engineering
Metaheuristics and Optimiztion in Civil Engineering
Xin-She Yang
?
A Biologically Inspired Network Design Model
A Biologically Inspired Network Design ModelA Biologically Inspired Network Design Model
A Biologically Inspired Network Design Model
Xin-She Yang
?
Introduction to Computational Mathematics (2nd Edition, 2015)
Introduction to Computational Mathematics (2nd Edition, 2015)Introduction to Computational Mathematics (2nd Edition, 2015)
Introduction to Computational Mathematics (2nd Edition, 2015)
Xin-She Yang
?
Memetic Firefly algorithm for combinatorial optimization
Memetic Firefly algorithm for combinatorial optimizationMemetic Firefly algorithm for combinatorial optimization
Memetic Firefly algorithm for combinatorial optimization
Xin-She Yang
?
Two-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential EvolutionTwo-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential Evolution
Xin-She Yang
?
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Xin-She Yang
?
Bat Algorithm for Multi-objective Optimisation
Bat Algorithm for Multi-objective OptimisationBat Algorithm for Multi-objective Optimisation
Bat Algorithm for Multi-objective Optimisation
Xin-She Yang
?
Are motorways rational from slime mould's point of view?
Are motorways rational from slime mould's point of view?Are motorways rational from slime mould's point of view?
Are motorways rational from slime mould's point of view?
Xin-She Yang
?
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Review of Metaheuristics and Generalized Evolutionary Walk AlgorithmReview of Metaheuristics and Generalized Evolutionary Walk Algorithm
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Xin-She Yang
?
Test Problems in Optimization
Test Problems in OptimizationTest Problems in Optimization
Test Problems in Optimization
Xin-She Yang
?
Engineering Optimisation by Cuckoo Search
Engineering Optimisation by Cuckoo SearchEngineering Optimisation by Cuckoo Search
Engineering Optimisation by Cuckoo Search
Xin-She Yang
?
A New Metaheuristic Bat-Inspired Algorithm
A New Metaheuristic Bat-Inspired AlgorithmA New Metaheuristic Bat-Inspired Algorithm
A New Metaheuristic Bat-Inspired Algorithm
Xin-She Yang
?
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Xin-She Yang
?
Fractals in Small-World Networks With Time Delay
Fractals in Small-World Networks With Time DelayFractals in Small-World Networks With Time Delay
Fractals in Small-World Networks With Time Delay
Xin-She Yang
?

Recently uploaded (20)

move curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptx
move curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptxmove curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptx
move curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptx
CarlosFernandez289274
?
lec2cct computational cmplexity theory.pptx
lec2cct computational cmplexity theory.pptxlec2cct computational cmplexity theory.pptx
lec2cct computational cmplexity theory.pptx
Rajesh481733
?
7.Fractional distillation(3 classes).pptx
7.Fractional distillation(3 classes).pptx7.Fractional distillation(3 classes).pptx
7.Fractional distillation(3 classes).pptx
abdulsamad296609
?
METAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFET
METAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFETMETAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFET
METAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFET
punithaece
?
UNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdf
UNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdfUNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdf
UNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdf
2022EE30SadeedUrRehm
?
Presentation Session 1 - Introduction to Agentic.pdf
Presentation Session 1 - Introduction to Agentic.pdfPresentation Session 1 - Introduction to Agentic.pdf
Presentation Session 1 - Introduction to Agentic.pdf
Rohit Radhakrishnan
?
Angelika Dorosz - BIM School Expert course - Poland, Krakow
Angelika Dorosz - BIM School Expert course - Poland, KrakowAngelika Dorosz - BIM School Expert course - Poland, Krakow
Angelika Dorosz - BIM School Expert course - Poland, Krakow
bim.edu.pl
?
Introduction-to-Stack-Instruction.pptxpptppt
Introduction-to-Stack-Instruction.pptxpptpptIntroduction-to-Stack-Instruction.pptxpptppt
Introduction-to-Stack-Instruction.pptxpptppt
suhas060606
?
Fault_Detection_Using_ANNs_Presentation.pptx
Fault_Detection_Using_ANNs_Presentation.pptxFault_Detection_Using_ANNs_Presentation.pptx
Fault_Detection_Using_ANNs_Presentation.pptx
JeveshMagnani
?
Application of Artificial Neural Networks.pdf
Application of Artificial Neural Networks.pdfApplication of Artificial Neural Networks.pdf
Application of Artificial Neural Networks.pdf
JeveshMagnani
?
Vernier Calipers an details discussion to measure the
Vernier Calipers an details discussion to measure theVernier Calipers an details discussion to measure the
Vernier Calipers an details discussion to measure the
Dr Mohd Aslam
?
J111111111111111111111111111111111111111query.pptx
J111111111111111111111111111111111111111query.pptxJ111111111111111111111111111111111111111query.pptx
J111111111111111111111111111111111111111query.pptx
dkmishra2407
?
Machine Design Basic Concepts.pptx
Machine Design      Basic  Concepts.pptxMachine Design      Basic  Concepts.pptx
Machine Design Basic Concepts.pptx
anjali681362
?
Smart Manufacturing with Unified Namespace
Smart Manufacturing with Unified NamespaceSmart Manufacturing with Unified Namespace
Smart Manufacturing with Unified Namespace
Ponraj RK
?
Chapter 2.pdf Smith Chart and Impedance Matching
Chapter 2.pdf Smith Chart and Impedance MatchingChapter 2.pdf Smith Chart and Impedance Matching
Chapter 2.pdf Smith Chart and Impedance Matching
dathoang3243
?
HISTORY Module (Revised).pdf fershman student module
HISTORY Module (Revised).pdf fershman student moduleHISTORY Module (Revised).pdf fershman student module
HISTORY Module (Revised).pdf fershman student module
ABERE YINEBEB
?
芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models
芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models
芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models
鰻粥京晦粥皆幄塀氏芙
?
b29e51b5-c830-4877-a978-a6b308ea8c5f.ppt
b29e51b5-c830-4877-a978-a6b308ea8c5f.pptb29e51b5-c830-4877-a978-a6b308ea8c5f.ppt
b29e51b5-c830-4877-a978-a6b308ea8c5f.ppt
dashrimi0
?
Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...
Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...
Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...
Faizal Ayyoob
?
study of impact behaviour of dual material for energy absorption
study of impact behaviour of dual material for energy absorptionstudy of impact behaviour of dual material for energy absorption
study of impact behaviour of dual material for energy absorption
AmitChauhan352669
?
move curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptx
move curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptxmove curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptx
move curriculum guidennnnnnnnnnnnnnnnnnnnnnnoriginal.pptx
CarlosFernandez289274
?
lec2cct computational cmplexity theory.pptx
lec2cct computational cmplexity theory.pptxlec2cct computational cmplexity theory.pptx
lec2cct computational cmplexity theory.pptx
Rajesh481733
?
7.Fractional distillation(3 classes).pptx
7.Fractional distillation(3 classes).pptx7.Fractional distillation(3 classes).pptx
7.Fractional distillation(3 classes).pptx
abdulsamad296609
?
METAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFET
METAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFETMETAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFET
METAL OXIDE FIELD EFFECT SEMICONDUCTOR-MOSFET
punithaece
?
UNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdf
UNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdfUNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdf
UNIT 03 Alan_Morris_EMI_Chap_03 Measurement Uncertainty.pdf
2022EE30SadeedUrRehm
?
Presentation Session 1 - Introduction to Agentic.pdf
Presentation Session 1 - Introduction to Agentic.pdfPresentation Session 1 - Introduction to Agentic.pdf
Presentation Session 1 - Introduction to Agentic.pdf
Rohit Radhakrishnan
?
Angelika Dorosz - BIM School Expert course - Poland, Krakow
Angelika Dorosz - BIM School Expert course - Poland, KrakowAngelika Dorosz - BIM School Expert course - Poland, Krakow
Angelika Dorosz - BIM School Expert course - Poland, Krakow
bim.edu.pl
?
Introduction-to-Stack-Instruction.pptxpptppt
Introduction-to-Stack-Instruction.pptxpptpptIntroduction-to-Stack-Instruction.pptxpptppt
Introduction-to-Stack-Instruction.pptxpptppt
suhas060606
?
Fault_Detection_Using_ANNs_Presentation.pptx
Fault_Detection_Using_ANNs_Presentation.pptxFault_Detection_Using_ANNs_Presentation.pptx
Fault_Detection_Using_ANNs_Presentation.pptx
JeveshMagnani
?
Application of Artificial Neural Networks.pdf
Application of Artificial Neural Networks.pdfApplication of Artificial Neural Networks.pdf
Application of Artificial Neural Networks.pdf
JeveshMagnani
?
Vernier Calipers an details discussion to measure the
Vernier Calipers an details discussion to measure theVernier Calipers an details discussion to measure the
Vernier Calipers an details discussion to measure the
Dr Mohd Aslam
?
J111111111111111111111111111111111111111query.pptx
J111111111111111111111111111111111111111query.pptxJ111111111111111111111111111111111111111query.pptx
J111111111111111111111111111111111111111query.pptx
dkmishra2407
?
Machine Design Basic Concepts.pptx
Machine Design      Basic  Concepts.pptxMachine Design      Basic  Concepts.pptx
Machine Design Basic Concepts.pptx
anjali681362
?
Smart Manufacturing with Unified Namespace
Smart Manufacturing with Unified NamespaceSmart Manufacturing with Unified Namespace
Smart Manufacturing with Unified Namespace
Ponraj RK
?
Chapter 2.pdf Smith Chart and Impedance Matching
Chapter 2.pdf Smith Chart and Impedance MatchingChapter 2.pdf Smith Chart and Impedance Matching
Chapter 2.pdf Smith Chart and Impedance Matching
dathoang3243
?
HISTORY Module (Revised).pdf fershman student module
HISTORY Module (Revised).pdf fershman student moduleHISTORY Module (Revised).pdf fershman student module
HISTORY Module (Revised).pdf fershman student module
ABERE YINEBEB
?
芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models
芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models
芙坪茶氏Y創_Data-Centric AI in The Age of Large Language Models
鰻粥京晦粥皆幄塀氏芙
?
b29e51b5-c830-4877-a978-a6b308ea8c5f.ppt
b29e51b5-c830-4877-a978-a6b308ea8c5f.pptb29e51b5-c830-4877-a978-a6b308ea8c5f.ppt
b29e51b5-c830-4877-a978-a6b308ea8c5f.ppt
dashrimi0
?
Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...
Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...
Faizal E Ayyoob - Architectural and Finishing QA/QC Inspector for high end lu...
Faizal Ayyoob
?
study of impact behaviour of dual material for energy absorption
study of impact behaviour of dual material for energy absorptionstudy of impact behaviour of dual material for energy absorption
study of impact behaviour of dual material for energy absorption
AmitChauhan352669
?

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 ====================================