Week 4.1 - Genetic Algorithm
Aim:
To Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Proposals:
- 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.
Topics To Remember:
Genetic Alogoritm:
In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). 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.The genetic algorithm is a technique for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm constantly modifies the population size of individual solutions.
Stopping criteria:
It refers to conditions that must be reached in order to stop the execution of the algorithm. Some of the most common stopping conditions are: execution time, total number of iterations, non-improving iterations, optimal (lower bound for min, upper bound for max) solution found, etc.
Syntax for Genetic Algorithm:
Significance of running multiple iterations:
Iterative design allows designers to create and test ideas quickly. Those that show promise can be iterated rapidly until they take sufficient shape to be developed; those that fail to show promise can quickly be abandoned. It's a cost-effective approach which puts user experience at the heart of the design process.
IMP Equations Used In Stalagmite function:

Stalagmite Function Code:
function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = (sin(5.1*pi*x + 0.5))^6;
f1_y = (sin(5.1*pi*y + 0.5))^6;
t1 = ((x-0.0667)^2)/(0.64);
f2_x = exp(-4.*log(2)*t1);
t2 = ((y-0.0667)^2)/(0.64);
f2_y = exp(-4.*log(2)*t2);
f = -(f1_x.*f2_x.*f1_y.*f2_y);
end
Main Code:
clear all
close all
clc
%% PROVIDING THE SEARCH SPACE FOR THE FUNCTION
x=linspace(0,1.7,150);
y=linspace(0,1.7,150);
[X,Y]=meshgrid(x,y);
num_cases=50; %Number of itreation for which the function will evaluate the fitness
%% CREATING THE SURFACE PLOT OF STALAGEMITE FUNCTION
for i=1:length(X)
for j=1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)=stalagmite(input_vector); %values of function in given search space
end
end
surfc(X,Y,-f); %generating the surface
shading interp %To remove the lines produced by the mesh grid
%% STATISTICAL BEHAVIOUR (UNBOUNDED)
tic %calculating the study time through tic and toc command
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2); %calculating the optimized value of the function
xopt(i)=inputs(1); %at each iteration
yopt(i)=inputs(2);
end
study1_time=toc %displaying the time taken to evaluate
% PLOTING THE GRAPH
figure(1)
subplot(2,1,1)
surfc(X,Y,-f);
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('UNBOUNDED INPUTS')
xlabel('x');
ylabel('y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')
%% STATISTICAL BEHAVIOUR (BOUNDED)
tic %calculating the study time through tic and toc command
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); %calculating the optimized value of the function
xopt(j)=inputs(1); %at each iteration with boundation
yopt(j)=inputs(2);
end
study2_time=toc
%PLOTTING
figure(2)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('BOUNDED INPUTS')
xlabel('x');
ylabel('y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')
%% MODIFYING THE NUMBER OF POPULATION
options=optimoptions('ga','populationsize',300) %increaing the sixe of population by using
%optimoptions function
tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study3_time=toc
%PLOTTING
figure(3)
subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('BOUNDED INPUTS "with increased population size"')
xlabel('x');
ylabel('y');
grid on
subplot(2,1,2)
plot(-fopt);
xlabel('iterations')
ylabel('function min value')
Errors:

Reducing errors & Output on Workspace:

Breif Explanation:
Main body:
Firstly, creating the mesh from the two vectors which will be acting as a search space to find the maxima of the function.
Creating the mesh of these two vectors using the ‘mesh’ command and defining the no. of iterations as num_cases to iterate the values of the optimum values of the function.
Now calculating the values of fitness function at each point of the mesh grid formed.
Now creating its surface through ‘surfc’ command which will also provide the contour of the generated surface.
While using surfc applying the -ve sign to the function will generate the surface upside down which will be quite good to get the maxima of the function, as it will provide positive value for the maxima using the -ve sign.
STATISTICAL STUDY WITHOUT BOUNDATION:
In this evaluating the optimum value of fitness function at each iteration and calculating it using the above-mentioned syntax in the intro part (which gives the inputs ie. Values of x and y at which the stalagmite function is minimum)
Then plotting it into the surf command and also providing the optimum values of the function that how it is varying.
Also using the tic and tok command to evaluate the time used for optimizing the function.
"It is clear that the results are not that much accurate and giving a lot of variation in optimizing the function".
STATISTICAL STUDY WITH BOUNDATION:
The difference over here is in providing the upper and the lower limit in the ga so that the search space is reduced with the upper and lower boundary
And then plotting it as similar to the above study.
"It is clearly seen from the above that after providing the boundary limits the variation in the optimum value of the function has reduced".
STATISTICAL STUDY WITH INCREASE IN SIZE OF POPULATION:
In this study using the ‘optimoptions’ command to increase the size of population.
The benefit of that will be it will be working on the large set of numbers and which will be led to increase the possibilities of finding the better fitness of the function.
The drawback is that the time for evaluation will increase.
options=optimoptions(‘ga’,’populationsize’,300) %increaing the sixe of population by using.
Results:
STATISTICAL STUDY WITHOUT BOUNDATION:

STATISTICAL STUDY WITH BOUNDATION:

STATISTICAL STUDY WITH INCREASE IN SIZE OF POPULATION:

Eventough:
ga is useful for optimizing the function but still the result are not accurate as the maximum value for the function lies at -1.
to overcome that using the given programme shown below
clear
close all
clc
%% creating the mesh
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);
%% for plotting the surface of stalagmite
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)
shading interp
%% evalutaing the function
options=optimoptions('ga','PlotFcn',{@gaplotbestf,@gaplotstopping},'populationsize',170);
[x,fval]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],options);
xopt=x(1);
yopt=x(2);
figure(1)
hold on
plot3(xopt,yopt,-fval,'ro','markersize',5,'markerfacecolor','r')
Results:


Conclusion:
Hope That now it looks some what accurate (90%)
in Actual Point of view Genetic Algorithm does not plot 100% accuracy.