Genetic Algorithm
clear all
close all
clc
%function[ans]= stal(input)
%x=input(1);
%y=input(2);
%x1=(sin(((5.1*pi).*x)+0.5)).^6;
%x2=exp((((x-0.0667).^2).*(-4*log(2)))./0.64);
%y1=(sin(((5.1*pi).*y)+0.5)).^6;
%y2=exp((((y-0.0667).^2).*(-4*log(2)))./0.64);
%ans=-(x1*x2*y1*y2);
%end
%defining search space
x=linspace(0,1,150);
y=linspace(0,1,150);
%crerating 2d array
[xx yy] =meshgrid(x,y);
no_cases= 50;
%evaluating the stalagmite function
for i= 1:length(xx)
for j=1:length(yy)
input_v(1)=xx(i,j);
input_v(2)=yy(i,j);
f(i,j)=stal(input_v);
end
end
%case1- ga trend
tic
for i=1:no_cases
[input,f1(i)]= ga(@stal,2);
x1(i)=input(1);
y1(i)=input(2);
end
time1=toc
figure(1)
subplot(2,1,1)
hold on
surface(xx,yy,-f)
shading interp
plot3(x1,y1,-f1,\'color\',\'r\',\'marker\',\'o\',\'markersize\',5)
title(\'case1\')
subplot(2,1,2)
hold on
plot(-f1)
xlabel(\'interations\')
ylabel(\'maximum\')
%ga case2
tic
for i=1:no_cases
[input,f2(i)]= ga(@stal,2,[],[],[],[],[0 0],[1 1]);
x2(i)=input(1);
y2(i)=input(2);
end
time2=toc
figure(2)
subplot(2,1,1)
hold on
surface(xx,yy,-f)
shading interp
plot3(x2,y2,-f2,\'color\',\'r\',\'marker\',\'o\',\'markersize\',5)
title(\'case2\')
subplot(2,1,2)
hold on
plot(-f2)
xlabel(\'interations\')
ylabel(\'maximum\')
%ga case3
options=optimoptions(\'ga\');
options=optimoptions(options,\'populationsize\',300);
tic
for i=1:no_cases
[input,f3(i)]= ga(@stal,2,[],[],[],[],[0;0],[1;1],[],[],options);
x3(i)=input(1);
y3(i)=input(2);
end
time1=toc
figure(3)
subplot(2,1,1)
hold on
surface(xx,yy,-f)
shading interp
plot3(x3,y3,-f3,\'color\',\'r\',\'marker\',\'o\',\'markersize\',5)
title(\'case3\')
subplot(2,1,2)
hold on
plot(-f3)
xlabel(\'interations\')
ylabel(\'maximum\')