Genetic Algorithm
Working of Genetic Algorithm:
- Random initial population is created first.
- A sequence of new populations is created by the algorithm. The individuals in current population are used to create new population.
- The algorith stops when the stopping criteria is met.
Code to optimize stalagmite function :
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy] = meshgrid(x,y)
num_cases = 100
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
options = optimoptions(\'ga\');
options = optimoptions(options,\'PopulationSize\',170);
for i = 1:num_cases
[inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
xlabel(\'x values\')
ylabel(\'y values\')
plot3(xopt,yopt,fval,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(fval)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')
% function for stalagmite
function[fxy] = 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);
fxy = -f1x*f1y*f2x*f2y;
inputs =
0.0668 0.0668
fval =
-1.0000
So the maximum value of the function is 1 and lies on (0.0668,0.0668).