Week 4.1 - Genetic Algorithm
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
MAIN FUNCTION OF STALAGMITE:
function [f] = stalagmite(input_vector);
x = input_vector(1);
y = input_vector(2);
term1 = pi*5.1*x;
term2 = pi*5.1*y;
term3 = ((x-0.0667)^2/0.64);
term4 = ((y-0.0667)^2/0.64);
term5 = (-4)*log(2);
f1x = (sin(term1+0.5)).^6;
f1y = (sin(term2+0.5)).^6;
f2x = exp(term5*term3);
f2y = exp(term5*term4);
f = -f1x*f1y*f2x*f2y
end

- TO EXCUTE THIS CODE CHECK THE BELOW FILE"stalagmite".
TO FIND THE GLOBAL MAXIMA CODE
%code for to find the global maxima
clear all
close all
clc
% defineing our search space
x = linspace(0,0.6,150); %sinle array
y = linspace(0,0.6,150); %single array
% creating 2d dimensional mesh
[xx yy] = meshgrid(x, y); %2dimesional array
% evaluate 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) %it shows the plot
shading interp %it removes gird on the surface of the plot
[inputs, f_val] = ga(@stalagmite,2)


- TO EXCUTE THIS CODE CHECK THE BELOW FILE"g_maxima"
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
Ans: The genetic algorithm(GA) is an optimization technique use dto solve non linaer optimization propblems. It uses concepts of evollutionary theory, which is produced by "CHARLES DARWIN",to find the global maxima for optimization a problem. an intial set of solutions is tested against the objective function.
GENETIC ALGORITHM has done 3 steps
1 Selection
2 Crossover
3 Mutation
SYNATX FOR GA:
- Make sure that your code gives the same output, even if it is made to run several times.
%code for global maxima at different cases
clear all
close all
clc
x = linspace(0,0.6,150); %x values
y = linspace(0,0.6,150); %y values
num_cases = 50 %value
[xx yy] = meshgrid(x, y); %2dimesional array
% evaluate the stalagmite function
for i = 1: length(xx) %for loop to count all values
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
tic %study time formula
%case1 study behaviour
for i = 1 : num_cases
[inputs,fopt(i)] = ga(@stalagmite,2)
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time = toc %study_time
figure(1) %plot 1
subplot(2,1,1)
hold on
surfc(x,y,f) %giving meshgrid
shading interp %removing shading on graph
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r') %plotting values
title('unbounded input') %title
subplot(2,1,2)
plot(fopt)
xlabel('iterations') %labels
ylabel('function minimum') %labels
tic %study_time2
%case 2 study-2 statical behaviour
for i = 1 : num_cases
[inputs,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
xopt2(i) = inputs(1);
yopt2(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,2)
hold on
surfc(x,y,f) %giving surf
shading interp %rmeoving shading on surf
plot3(xopt2,yopt2,fopt2,'marker','o','markersize',5,'markerfacecolor','r') %plotting
title('bounded input')
subplot(2,1,1)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
%study3 statical behaviour
options = optimoptions('ga'); %options counting
options = optimoptions(options,'PopulationSize',200); %giving option values
tic %study time
for i = 1 : num_cases %for loop
[inputs,fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt3(i) = inputs(1);
yopt3(i) = inputs(2);
end
study3_time = toc
figure(3) %plot3
subplot(2,1,2)
hold on
surfc(x,y,f) %giving surf
shading interp %rmeoving shading on surf
plot3(xopt3,yopt3,fopt3,'marker','o','markersize',5,'markerfacecolor','r') %plotting case3 values
title('bounded input')
subplot(2,1,1)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')




FOR ABOVE CODE CHECK THE BELOW FILE "ga_cases"