Genetic algorithm
Genetic algorithm is an optimisation algorithm.It is used to find the extremum values of a function.It is based on the evolution theory of charles darwin, meaning only the fittest of the individuals will survive. The algorithm consists of several stages namely,
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
The initial population is the search space for the algorithm to find the solution.Initially, every member of the population is a solution to the problem. The fitness function is the function which determines how much fit an individual is to compete with other individuals. The fitness score is the criteria which is taken into account when selecting an individual for reproduction.
Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.
In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
Main program
clear all
close all
clc
%Defining our search space
x = linspace(0,0.6,500);
y = linspace(0,0.6,500);
%Creating a 2D mesh
[xx yy] = meshgrid(x,y);
%Evaluating the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end
surfc(xx,yy,f)
title(\'Stalagmite function\\\')
xlabel(\'x values\\\')
ylabel(\'y values\\\')
zlabel(\'fitness function values\\\')
shading interp
[x, fval] = ga( @stalagmite, 2)
Stalagmite function
% stalagmite function
function [f] = stalagmite(input_vector)
f1x = (sin((5.1*pi*input_vector(1))+0.5))^6;
f1y = (sin((5.1*pi*input_vector(2))+0.5))^6;
f2x = exp(-4*log(2)*((input_vector(1)-0.0667)^2)/0.64);
f2y = exp(-4*log(2)*((input_vector(2)-0.0667)^2)/0.64);
f = -f1x*f1y*f2x*f2y;
end