Week 4.1 - Genetic Algorithm
Report writing for stalagmite function using genetic algorithm
Aim:
To write a matlab program on stalamite function using genetic algorithm.
Governing equations If any:
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.667)^2/0.64));
f2y=exp((-4)*log(2)*((y-0.667)^2/0.64));
f=-f1x*f1y*f2x*f2y;
Objectives of the project:
To know how the genetic algorithm works
To find the maxima value by the known minimum value
Code:
clear all
close all
clc
%defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2d dimesnional mesh
[xx,yy]=meshgrid(x,y);
%evaluating the 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
surf(xx,yy,f)
shading interp
[inputs,fval]=ga(@stalagmite,2)
function[f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
%fitness determining criterias
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.667)^2/0.64));
f2y=exp((-4)*log(2)*((y-0.667)^2/0.64));
f=-f1x*f1y*f2x*f2y;
end
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
f=get_stalagmite(x,y)
%study 1-satistical behaviour
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
title('unbounded inputs')
hold off
subplot(2,1,2)
plot(fopt)
xlabel('lterations')
ylabel('function minimum')
%study 2 satistical behaviour
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
title('unbounded inputs')
hold off
subplot(2,1,2)
plot(fopt)
xlabel('lterations')
ylabel('function minimum')
%study 3 satistical behaviour
options=optimoptions('ga');
options=optimoptions(options,'populationsize',200);
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(3)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
title('unbounded inputs')
hold off
subplot(2,1,2)
plot(fopt)
xlabel('lterations')
ylabel('function minimum')
function[f]=get_stalagmite(x,y)
%1st column of input vector has x values
[xx,yy]=meshgrid(x,y);
%evaluating the 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
end
Code explanation:
- In the first program we have created a stalagmite program,where it takes the row and column which is known as ‘input_vector’
- In the input_vectors the values are given as x and y
- And by applying the formula creating a program
- To find the maxiumum value of function we need to change the sign
- Second program is where we get maxiumum value using ga
- The search is used between 0,0.6,150
- It takes much time for the iterations so we have given the limit to 50
- To convert the vector we use mechgrid
- Then we use forloops to calculate the f
- Then we take three cases to find the value
- In first run it is taken randomly so system takes up to 50
- In second we resrtict upper and lower [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
- In third case we used up to 200
- Where we given via optimoptions
- [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
- Where at last plot is made to for excuited program
plot images:
for main stalagmite

for study1:

for stuyd2:

for study3:

conclusion:
genetic algorithm was made sucessfully
study of graph 3 has plotted for the maximum no of itreations