Genetic algorithm
In computer science and operation research, a genetic algorithm is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithm(EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover andselection.
main code for Genetic algorithm..
clear all
close all
clc
%inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_case = 150;
for i= 1:lengt(x)
for j = 1:length(y)
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);
F(i,j) = stalagmite(input_vector);
end
end
%study_1
tic
for i = 1:num_case
[inputs,Fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_1 = toc;
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel(\'y data\');
xlabel(\'x data\');
plot3(xopt,yopt,Fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'k\')
title(\'unbounded Inputs\')
subplot(2,1,2)
plot(Fopt)
xlabel(\'iterations\')
ylabel(\'Function maximum\')
%study 2
tic
for i = 1:num_case
[inputs,Fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_2 = toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,F)
shading interp
xlabel(\'x data\')
ylabel(\'y data\')
plot3(xopt,yopt,Fopt,\'marker\',o\',\'markersize\',5,\'markerfacecolor\',\'k\')
title(\'Bounded Inputs\')
subplot(2,1,2)
plot(Fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
%study 3
options = optimoptions(\'ga\')
options = optimoptions(options,\'populationsize\',450);
tic
for i = 1:num_case
[inputs,Fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_3 = toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,F)
shading interp
xlabel(\'x data\')
ylabel(\'y data\')
plot3(xopt,yopt,Fopt,\'marker\',o\',\'markersize\',5,\'markerfacecolor\',\'k\')
title(\'Bounded Inputs with Increased population size\');
subplot(2,1,2)
plot(Fopt)
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
max_value = [Fopt];
Function code for Genetic algorithm
function[f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f4=exp(-4*log(2)*((y-0.0667)^2)/0.64);
f=(f1*f2*f3*f4);
f=1/(1+f);
end
In the main code , The stalagmite function is classified into 3 sectors,
they are
1-unbounded inputs.
2-bounded inputs.
3-bounded inputs with increased population
In the unbounded inputs ,they dont have any limits ,the values are distributed outside the limits.
In the bounded inputs, they have limits, so the values are distributed inside the limits.
In the bounded inputs with increased population, the number of iteration are increased .
plot for unbounded inputs
plot for bounded inputs
plot for bounded inputs with increased population