Project

Generic algorithm for stalagmite function

Generic algorithm:

Generic algorith has a broad scope of meaning in various domains like biology and AI , where as in mechanical engineering we are using this technique to find the min value of a function with multiple variables.

problem statement:

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Make sure that your code gives the same output, even if it is made to run several times.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.

\"\"

function z = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
x1=(sin(5.1*3.14.*x+0.5)).^6
y1=(sin(5.1*3.14.*y+0.5)).^6
x2=exp(-4*2.303*log(2)*(x-0.0667).^2./0.64)
y2=exp(-4*2.303*log(2)*(y-0.0667).^2./0.64)
z= -(x1.*x2.*y1.*y2)
end
clear all
close all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

[xx yy]=meshgrid(x,y);

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
[x fval] = ga(@stalagmite,2)

first the 1D array is converted to 2D array by means of using meshgrid .then for each value of x and each value of y the fuction is performed then the values are simulated using surface plots and then by using GA algorith the pair of variable (x,y) for the which the value is minimum is found out.

the disadvantage of this method is it throws output based on approimation

please find the plot below

\"\"

now to optimize the value we are going to perform staistical way to find appromixate value

tic
for i=1:50;
    [inputs fopt(i)] = ga(@stalagmite,2)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
    
end
figure(1)
study_time1= toc
subplot(2,1,1)
hold on 
surf(xx,yy,f)
xlabel(\'xvalue\')
ylabel(\'yvalue\')
zlabel(\'fvalue\')
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'unbounded_inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\')
ylabel(\'min_func_value\')

plot :

\"\"

method 2 : bounded inputs

%study2 - stastical behaviour with upper and lower bound
tic
for i = 1:50
    [inputs fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
 study_time2=toc  
 figure(2)
 subplot(2,1,1)
 hold on 
 surf(xx,yy,f)
 xlabel(\'xvalue\')
ylabel(\'yvalue\')
zlabel(\'fvalue\')
 shading interp
 plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'r\')
title(\'bounded_inputs\')
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\')
ylabel(\'min_func_value\')
   
%statistical behaviour with population size
options=optimoptions(\'ga\');
options=optimoptions(options,\'populationsize\',170);

plot:

\"\"

method 3: population method

%statistical behaviour with population size
options=optimoptions(\'ga\');
options=optimoptions(options,\'populationsize\',170);

tic
for i = 1:50
    [inputs fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
 study_time_3=toc  
 figure(3)
 subplot(2,1,1)
 hold on 
 surf(xx,yy,f)
 xlabel(\'xvalue\')
ylabel(\'yvalue\')
zlabel(\'fvalue\')
 shading interp
 plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'r\')
title(\'population size\')
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\')
ylabel(\'min_func_value\')

plot:

\"\"

Finally the population method gives perfect value

method time taken X y f
no iteration 15 0.0491 0.0781 -0.7068
statistical 211 0.0769 0.0832 -0.7497
bounded 148 0.06846 0.07651 -0.928
population 464 0.06687 0.06686 -1
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.