Week 4.1 - Genetic Algorithm
clear all
close all
clc
%input
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
[xx, yy]= meshgrid(x,y);
%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)
hold on
[x ,fval]= ga(@stalagmite,2)
xlabel('x')
ylabel('y')
hold off
shading interp
FUNCTION CODE-
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);
end
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
num_cases=50;
f =get_stalagmite(xx,yy);
tic
%1st study
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc;
%plot
figure(1)
subplot(2,1,1)
hold on
disp(f)
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('interations')
ylabel('min. function')
tic
%2nd study
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
disp(f)
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('interations')
ylabel('min. function')
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',170)
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],options)
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc;
figure(3)
subplot(2,1,1)
hold on
disp(f)
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5)
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('interations')
ylabel('min. function')
FUNCTION CODE
function [f]= get_stalagmite(x,y)
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
f1= (sin(5.1*pi.*xx+0.5)).^6;
f2= (sin(5.1*pi.*yy+0.5)).^6;
f3= exp(-4*log(2).*(xx-0.0667).^2/0.64);
f4= exp(-4*log(2).*(yy-0.0667).^2/0.64);
f= -(f1.*f2.*f3.*f4);
end
STALAGMITE CODE TO DETERMINE MAXIMUM VALUE.
using meshgrid to find matrix of xx yy
using double for loop and function code in it to plot the values.
plotting the results.

Genetic algorithm-basically they are a form of adaptive search algorithms that combine random search and historical data along with the process of natural selection to optimize the search
STUDY1-

STUDY 2-
STUDY 3-