Genetic Algorithm
% how genetic algoritm works
First the algoritm begins by creating a random initial population. Then it creates a sequence of new population.
At each step, the genetic algorithm uses the current population to create the children that make up the next generation.
The algorithm selects a group of individuals in the current population, called parents, who contribute their genes (the entries of their vectors)to their children. The algorithm usually selects individuals that have better fitness values as parents.
The genetic algorithm creates three types of children for the next generation:
* Elite children are the individuals in the current generation with the best fitness values. These individuals automatically survive to the next generation.
* Crossover children are created by combining the vectors of a pair of parents.
* Mutation children are created by introducing random changes, or mutations, to a single parent.
**************************************************************************
% main code
clc
close all
clear all
% defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 100;
% creating 2D mesh
[xx , yy] = meshgrid(x,y);
for i=1:length(xx)
for j=1:length(yy)
vector_input(1) = xx(i,j);
vector_input(2) = yy(i,j);
f(i,j) = stalagmite(vector_input);
end
end
options = optimoptions(\'ga\',\'populationsize\',170);
tic
for k=1:num_cases
[inputs,fopt(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(k) = inputs(1);
yopt(k) = inputs(2);
end
study_time3 = toc
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markerfacecolor\',\'r\');
xlabel(\' x axis\');
ylabel(\'y axis\');
zlabel(\'z axis\');
title(\'bounded inputs with altered population size\')
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\');
ylabel(\'function minimum\')
**************************************************************************
% stalagmite function
function f= stalagmite(vector_input)
x = vector_input(1);
y = vector_input(2);
f1x = (sin(5.1*pi*x + 0.5))^6;
f1y = (sin(5.1*pi*y + 0.5))^6;
f2x = exp((-4)*log(2)*((x-0.0667)^2/0.64));
f2y = exp(-4*log(2)*(y-0.0667)^2/0.64);
f = -(f1x*f1y*f2x*f2y);
end