A question about the simple genetic algorithm code
2 views (last 30 days)
Show older comments
This is a code I found of the genetic algorithm. I am a beginner in MATLAB but I really would like to understand the code. The CrossOver, Mutation and Select functions are written in separate m.files. I am reading the code linearly so it was all fine until I reached the line
Population=Initialize(NumberOfParameters, PopulationSize, LowerBounds, UpperBounds);
Is Initialize a saved function in MATLAB or is it a function the author is going to define later? I assumed it is goning to be defined later then when I proceed ..
Fitness(i,1)=getFitness(Population(:,i));
What is getFitness, Population?! they were not defined before. I thought he is defining a new function named getFitness but then he did not define what Population is!
Here is the main code and the CrossOver, Mutation and Select are functions defined in seprate m.files.
%This code carries out a real Genetic Algorithm
PopulationSize=100; %number of chromosomes
NumberOfParameters=2; %number of designable parameters
NumberOfGenerations=5; %number of generations
LowerBounds=[-10 -10]'; %lower bounds on parameters
UpperBounds=[10 10]'; %lower bounds on parameters
MutationProbability=0.01; %probability of mutation
Fitness=zeros(PopulationSize,1); %fitness of all chromosomes
BestFitness=zeros(NumberOfGenerations,1); %storage for best fit in every iteration
BestFitnessPoints=zeros(NumberOfParameters,NumberOfGenerations); %storage for best fit
%point in every iteration
%now we do initialization
Population=Initialize(NumberOfParameters, PopulationSize, LowerBounds, UpperBounds);
%This is the main loop
for GenerationCounter=1:NumberOfGenerations %repeat for all generations
for i=1:PopulationSize %repeat for all chromosomes
Fitness(i,1)=getFitness(Population(:,i)); %get fitness of the ith element
end
[MaxValue MaxValueIndex]= max(Fitness); %get best fitness and its index
BestFitness(GenerationCounter,1)= MaxValue%store the best fit in this generation
BestFitnessPoints(:,GenerationCounter)=Population(:,MaxValueIndex); %store best fit point
%now we create the mating pool using the population
MatingPool=Select(Population,Fitness); %select the fittest from inside this population
%now we do simply crossover
OffSprings=CrossOver(MatingPool); %do simple crossover
Population=Mutation(OffSprings, MutationProbability); %do mutation
end
BestFitness
BestFitnessPoints
Any help will be very appreciated.
Thank you!
3 Comments
Walter Roberson
on 22 Aug 2019
I do not know. I am not a subscriber to scribd (which does not appear to be an official copy anyhow) and official copies of the book are over $CDN 100 in electronic format (though I could get a used hardcover version for $CDN 70 and more than a week shipping time)
Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!