Week 4.1 - Genetic Algorithm
AIM
To write a code in to optimise the stalagmite function and find the global maxima of the function.
OBJECTIVE
To understand the concept of genetic algorithm
To plot the graph for F maxmum vs iterations
THEORY
Genetic algorithm:genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution.
Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.
function f = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin(((5.1*pi).*x)+0.5)).^6;
f1y = (sin(((5.1*pi).*y)+0.5)).^6;
f2x = exp((((x-0.0667).^2).*(-4*log(2)))./0.64);
f2y = exp((((y-0.0667).^2).*(-4*log(2)))./0.64);
f = -f1x*f1y*f2x*f2y;
end
FUNCTION CODE EXPLANATION
The stalagmite function is defined and it is later used in main ga program
function command is used to call the function followed by file name(stalagmite is function name) and the input inside the brackets(input_vector).
close all
clear all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50
%f=stalagmite(x,y)
[xx,yy]=meshgrid(x,y)
for i = 1: length(xx)
for j =1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
f(i,j)=stalagmite(inputs);
end
end
%study 1 statistical behavior
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,10);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=tic;
figure(1)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function')
tic
%study2 statistical behavior with upper bound and lower bound
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=tic;
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function')
%study 3 | statistical behavior
options=optimoptions('ga');
options=optimoptions(options,'populationsize',450);
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=tic;
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function')
WORKSPACE

SYNTAX OF ga and explanation
OUTPUT FOR 1st STUDY

in this case it can be seen that since the inputs are boundaries ga algorithm has mapped
local maxima points outside our stalagmite function .also we cannot infer a particular value for global maxima from the above garph
OUTPUT FOR 2nd STUDY

the above graph is more clear and optimized compared to the 1st graph the bounds here were defined and we could get only the values within the range .we are now more clear on the range within our maxima lies
OUTPUT FOR 3rd STUDY

in this case we are getting the optimised value of global maxima with accurate range since we increased the number of iterations and population size
the study time is high compared when compared to the other two.
CONCLUSION
GA gives more accurate results in case of particular search space for ga,the ga doesnt run the inside the search space .only in cases of bounded inputs we can get interferable results