Week 4 - Genetic Algorithm
Aim:
Optimization using genetic algorithm and to determine the global maximum function using MATLAB.
Objectives:
- Clearly explaining the concept of genetic algorithm.
- Explaining the syntax for ga in Matlab.
- Codes 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.
Theory :
OPTIMIZATION
An act, process, or methodology of making something (such as a design, system, or decision) as fully perfect, functional, or effective as possible specifically : the mathematical procedures (such as finding the maximum of a function) involved in this
Genetic Algorithms (GAs) come under the category of evolutionary algorithms with working principle based on the mechanics of natural genetics. The basic objective in natural genetics is the retention of the fit genes & discard of the redundant. New generations created by manipulating the genetic code using the tools such as selection, crossover & mutation. GA also works in similar manner with the objective to search appropriate solution for the problems involving either minimization or maximization of the objective function. GA use similar tools as selection, crossover & mutation applied to a population of binary strings generated randomly. In every generation, a new set of artificial species or strings is created using bits and pieces of the fittest among old; an occasional new part may be tried for good measure. Genetic algorithms are proven to yield robust search in complex spaces.
STEPS USED IN THE GENETIC ALGORITHM (GA)
- Initialize population
- selection according to fitness
- crossover between the selected chromosomes
- perform the mutation
- repeat cycle till the condition of the stop if true.
CODE :
MAIN CODE :
%MATLAB PROGRAM TO FIND THE MAXIMUM VALUE OF A STALAGMITE FUNCTION USING GENETIC ALGORITH
clear all %clear the workspace
close all %clear all the figure window
clc %clear the command window
%INPUTS
%Defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Creating a 2-Dimensional 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); %Assigning the XX(i,j) element to input_vector(1)
input_vector(2) = YY(i,j); %Assigning the YY(i,j) element to input_vector(2)
F(i,j) = stalagmite(input_vector); %Calling the stalagmite function and assigning the values in F(i,j)
end
end
%PLOTTING
figure(1) %Creating figure-1
surfc(XX,YY,-F) %creating a three-dimensional surface plot for the stalagmite function with a contour plot. The surface plot is a three-dimensional surface that has solid edge colors and solid face colors.
title('Stalagmite Function','fontweight','bold','fontsize',16) %writing the title
xlabel('x value') %Labeling the X axis
ylabel('y value') %Labeling the Y axis
shading interp %Shading the plot & removing the grid lines
axis tight
[inputs,fval]= ga(@stalagmite,2); %Finding the minimum value using ga() and assigning the value to fval and also assigning the inputs for which we got the min value to inputs as an array
%STUDY-1: STATISTICAL BEHAVIOR
num_case = 50; %Assigning 50 to num_case to run a for loop
tic %starting a stopwatch timer
for i = 1:num_case %Running a for loop
[inputs,fopt(i)] = ga(@stalagmite,2); %Finding the minimum value using ga() and assigning the value to fopt(i) and also assigning the inputs for which we got the min value to inputs as an array
xopt(i) = inputs(1); %Assigning the inputs(1) element to xopt matrix as an ith element
yopt(i) = inputs(2); %Assigning the inputs(2) element to yopt matrix as an ith element
end %ending the for loop
study_time_1 = toc; %assigning the elapsed time since tic was used to study_time_1
%PLOTTING
figure(2) %creating the figure-2
%Creating a 2*1 grids for subplotting in figure-2
%Subplot-1
subplot(2,1,1); %Creating a subplot on grid no-1 for plotting the 3D surface
hold on %Holding on the graph to plot on the same graph
surfc(x,y,-F) %creating a three-dimensional surface plot of the -ve stalagmite funtion with a contour plot. The surface plot is a three-dimensional surface that has solid edge colors and solid face colors.
shading interp %Shading the plot & removing the grid lines
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r') %Plotting the minimum values which we have got from ga() function
title('Unbounded inputs','fontweight','bold','fontsize',16) %Writting te title
axis tight
%Subplot-2
subplot(2,1,2) %Creating a subplot on grid no-2 for plotting Function minimum vs iterating
plot(-fopt) %Plotting the Function minimum vs iterating
xlabel('Iterations') %Labeling the X axis
ylabel('Function minimum') %Labeling the Y axis
%STUDY-2 : STATISTICAL BEHAVIOR WITH UPPER AND LOWER BOUNDS
tic %starting a stopwatch timer
for i = 1:num_case %Running a for loop
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); %Finding the minimum value using ga() fixing the upper & lower bound and assigning the value to fopt(i) and also assigning the inputs for which we got the min value to inputs as an array
xopt(i) = inputs(1); %Assigning the inputs(1) element to xopt matrix as an ith element
yopt(i) = inputs(2); %Assigning the inputs(2) element to yopt matrix as an ith element
end %ending the for loop
study_time_2 = toc; %assigning the elapsed time since tic was used to study_time_2
%PLOTTING
figure(3) %creating the figure-3
%Creating a 2*1 grids for subplotting in figure-3
%Subplot-1
subplot(2,1,1) %Creating a subplot on grid no-1 for plotting the 3D surface
hold on %Holding on the graph to plot on the same graph
surfc(x,y,-F) %creating a three-dimensional surface plot of the -ve stalagmite funtion with a contour plot. The surface plot is a three-dimensional surface that has solid edge colors and solid face colors.
shading interp %Shading the plot & removing the grid lines
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r') %Plotting the minimum values which we have got from ga() function
title('Bounded inputs','fontweight','bold','fontsize',16) %Writting te title
axis tight
%Subplot-2
subplot(2,1,2) %Creating a subplot on grid no-2 for plotting Function minimum vs iterating
plot(-fopt) %Plotting the Function minimum vs iterating
xlabel('Iterations') %Labeling the X axis
ylabel('Function minimum') %Labeling the Y axis
%STUDY -3 : INCREASING THE GA ITERATIONS
options = optimoptions('ga'); %creating options using the function optimoptions.It returns options with the default values for its fields. ga uses these default values if you do not pass in options as an input argument.
options = optimoptions('ga','populationSize',170); %Creating options with all values set to their defaults except for PopulationSize, which is set to 170.
tic %starting a stopwatch timer
for i = 1:num_case %Running a for loop
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); %Finding the minimum value using ga() fixing the upper & lower bound and assigning the value to fopt(i) and also assigning the inputs for which we got the min value to inputs as an array. Here ga() runs the genetic algorithm with a population size of 170.
xopt(i) = inputs(1); %Assigning the inputs(1) element to xopt matrix as an ith element
yopt(i) = inputs(2); %Assigning the inputs(2) element to yopt matrix as an ith element
end %ending the for loop
study3_time = toc; %assigning the elapsed time since tic was used to study_time_3
%PLOTTING
figure(4) %creating the figure-4
%Creating a 2*1 grids for subplotting in figure-3
%Subplot-1
subplot(2,1,1) %Creating a subplot on grid no-1 for plotting the 3D surface
hold on %Holding on the graph to plot on the same graph
surfc(x,y,-F) %creating a three-dimensional surface plot of the -ve stalagmite funtion with a contour plot. The surface plot is a three-dimensional surface that has solid edge colors and solid face colors.
shading interp %Shading the plot & removing the grid lines
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r') %Plotting the minimum values which we have got from ga() function
title('Increasing Iteration of Genetic Algorithm','fontweight','bold','fontsize',16) %Writting the title
axis tight
%Subplot-2
subplot(2,1,2) %Creating a subplot on grid no-2 for plotting Function minimum vs iterating
plot(-fopt) %Plotting the Function minimum vs iterating
xlabel('iterations') %Labeling the X axis
ylabel('function minimum') %Labeling the Y axis
Matlab code for the stalagmite function :
function [F] = stalagmite(input_vector)
a = input_vector(1);
b = input_vector(2);
f1_x = [sin(5.1*pi*a +0.5)]^6 ;
f1_y = [sin(5.1*pi*b +0.5)]^6 ;
f2_x = exp(-4*log(2)*(((a-0.0667)^2)/(0.64)));
f2_y = exp(-4*log(2)*(((b-0.0667)^2)/(0.64)));
F = (f1_x * f1_y* f2_x * f2_y );
F = -F; %To find the maximum value we change the sign of the function
end
3.3 EXPLANATION OF THE MATLAB CODE:-
- The code is started with three command.
Clear all – clear the workspace
Close all – clear all the figure window
clc – clear the command window
- We define the values of x and y using the linspace().
- A meshgrid [XX YY] is created using x and y matrix.
- Then we evaluate the stalagmite function running a for-loop and. Here we call the stalagmite function.
- Plot the stalagmite function in as a 3D surface.
- Study of Genetic Algorithm for the random sampling
- We run find the maximum value by running the ga() for 50 times.
- Also we calculate the running time.
- Then we plot the stalagmite function along with the maximum values which we have got from multiple iterations. Also we plot function maximum vs iteration on the same figure.
- Study of Genetic Algorithm with upper and lower bound sampling
- Here we find the maximum value by running the ga() for 50 times, specifying the upper and lower bounds of the inputs.
- Also we calculate the running time.
- Then we plot the stalagmite function along with the maximum values which we have got from multiple iterations. Also we plot function maximum vs iteration on the same figure.
- Study of Genetic Algorithm with upper and lower bound and increase the population size
- Here we find the maximum value by running the ga() for 50 times, specifying the upper and lower bounds and increasing the population size to 170.
- Also we calculate the running time.
- Then we plot the stalagmite function along with the maximum values which we have got from multiple iterations. Also we plot function maximum vs iteration on the same figure.
- Then we define the stalagmite function based in a different script (but on the same folder).
Here we assign the negative stalagmite function value as an output for this function because when we perform genetic algorithm it will give the minimum value (i.e. the largest -ve value) which will be the maximum value for stalagmite function when we change the sign.
OUTPUT :
3.4 OUTPUT:-
- Stalagmite function
- Study-1: Genetic Algorithm for the random and unbounded sampling
- Study-2: Genetic Algorithm with upper and lower bound sampling
- Study-3: Genetic Algorithm with upper and lower bound and increase the population size
CONCLUSION:-
We can clearly see that we get more accurate values in case in study-3.
Thus we can conclude that – specifying the upper & lower bound and increasing the population size we get the most accurate values.