Challenge

Week 4.1 - Genetic Algorithm

%1. To find the global minima

%Stalagmite Mathematical model

%Writing a program for a global optimization problem
%Optimization in Engineering can be ICE or Aircraft MEG. Certain things
%which is mandatory should be satisfied (like the airfoil should be a
%proper lift and minimize drag. For different flight condition the drag
%value should be minimum).

%Optimization is a type of study that deals with how to minimize or
%maximize a particular function, which depends on multiple input
%parameters.

%Stalagmite function is actually a mathematical model which helps us in
%generating local minmas and maximas. It gives us a 3D result where we have
%a lot of local maximas (the tiny peaks is the local maxima) and the point which is the highest
%in the plot gives us the global maxima.
%The objetcive of the Optimization is to determine the global maxima
%without having any prior knowledge of the particular plot/field.

%We always start with a known function. If a function is known then we know
%where the optimum point is. So with the function we move towards the
%optimum point.

%For this particular problem we are going to use 'genetic algorithm', this
%is based on the evolution theory that was proposed by Charles Darwin.
%In MATLAB we have inbuilt function to do 'genetic algorithm'.


clear all
close all
clc

%Defining our search space (Therefore creating one dimensional arrays)
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%Creating a 2D array
[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);
input_vector(2) = yy(i,j);
f(i,j) = stalagmite(input_vector);
end
end


%Creating a surface plot
surfc(xx, yy, f)
xlabel('x value')
ylabel('y value')
shading interp %Avoids the grid lines on the peaks (which are auto generated in MATLAB)


%Performing the 'genetic algorithm'. the function 'ga' is inbuilt in MATLAB
%Genetic Algorithm in MATLAB always minimizes the function.
[inputs, fval, exitFlag, Output] = ga(@stalagmite, 2);
%there are two values on the rhs within the function. the 1st one is we are
%calling the function which has to be minimized (in this case it is
%@stalagmite). The 2nd value is no. of variables in the function (from the
%shown equation we can see that there are two variables i.e., f(x,y).

%Searched 'Genetic Algorithm options' in help section.
%The Genetic Algorithm works on a population using a set of operators that
%are applied to the population. A population is a set of points in the
%design space. The initial population is generated randomly by default.
%The next generation of the population is computed using the fitness of the
%individuals in the current generation.

%if we type in inputs in the command window it gives us 2 values (i.e., x
%and y values at which our function (fval) has a minimum value of 0). This is 0
%because we are trying to minimize (using ga function) (So, this type of problem can be used to
%optimize cost, optimize transportation or to optimize the drag froce on an aerodynamic designed
%cars and so on).
fprintf('The number of generations was : %d\n', Output.generations);
%To display the number of generations in the command window
fprintf('The number of function evaluations was : %d\n', Output.funccount);
%To display the number of function evalutions in the command window
fprintf('The best function value found was : %g\n', fval);
%Displays the best fval found.
%Since we are using printf command it will display the values obtained on
%the screen.

%Adding visualization
%It is used to visualize the performance of the solver at run time.
opts = optimoptions(@ga,'PlotFcn',{@gaplotbestf,@gaplotstopping});
[x,Fval,exitFlag,Output]= ga(@stalagmite,2,[],[],[],[],[],[],[],opts);
%Here we use optimoptions (inbuilt function) to select two plot functions.
%The first plot function is gaplotbestf, which plots the best and mean score
%of the population at every generation. The second plot function is
%gaplotstopping, which plots the percentage of stopping criteria satisfied.

 %Function file

%Creating the Stalagmite function
function [fx_y] = 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;

f2_x = exp(-4*log(2).*((x-0.0667).^2/0.64));
f2_y = exp(-4*log(2).*((y-0.0667).^2/0.64));

fx_y = (f1_x.*f1_y.*f2_x.*f2_y);
end

%Now to find the Global Maxima

%Stalagmite Mathematical model

%Writing a program for a global optimization problem
%Optimization in Engineering can be ICE or Aircraft MEG. Certain things
%which is mandatory should be satisfied (like the airfoil should be a
%proper lift and minimize drag. For different flight condition the drag
%value should be minimum).

%Optimization is a type of study that deals with how to minimize or
%maximize a particular function, which depends on multiple input
%parameters.

%Stalagmite function is actually a mathematical model which helps us in
%generating local minmas and maximas. It gives us a 3D result where we have
%a lot of local maximas (the tiny peaks is the local maxima) and the point which is the highest
%in the plot gives us the global maxima.
%The objetcive of the Optimization is to determine the global maxima
%without having any prior knowledge of the particular plot/field.

%We always start with a known function. If a function is known then we know
%where the optimum point is. So with the function we move towards the
%optimum point.

%For this particular problem we are going to use 'genetic algorithm', this
%is based on the evolution theory that was proposed by Charles Darwin.
%In MATLAB we have inbuilt function to do 'genetic algorithm'. It is always
%used to find the minimal optimum value


clear all
close all
clc

%Defining our search space (Therefore creating one dimensional arrays)
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%Creating a 2D array
[xx yy] = meshgrid(x,y);

%Searched Maximizing vs Minimizing under > Global optimization toolbox >
%optimization problem setup. It says to maximize f(x) we need to perform
%the minimization of -f(x). Therefore we need to get the points of
%stalagmite as it undergoes for loop it stores 1 value during each
%iteration. Therefore we have to get points of stalagmite function itself.
% f = stalagmite(input_vector);
% F = -f;
%By doing the above we just get a single value of stalagmite, therefore we
%need to create another for loop to assign the value (So I have created a
%seperate function file.
f = F_stalagmite(x,y);
F = -f;
Alog = 50; %Indicating the no. of algorithm to be performed

for i = 1:Alog
[inputs, Max(i)] = ga(@stalagmite, 2); %the max optimized value should be taken as an array, since we use for loop
end

%if we type in inputs in the command window it gives us 2 values (i.e., x
%and y values at which our function (fval) has a minimum value of 0). This is 0
%because we are trying to minimize (using ga function) (So, this type of problem can be used to
%optimize cost, optimize transportation or to optimize the drag froce on an aerodynamic designed
%cars and so on).

Max = -Max;
%Plotting
surfc(xx,yy,F)
xlabel('x value')
ylabel('y value')
shading interp
figure

plot(Max)
xlabel('No. of Iterations')
ylabel('F Maximum')
%The highest or the global maxima I could reach out of 50 algorithm is
%0.989


%Performing the 'genetic algorithm'. the function 'ga' is inbuilt in MATLAB
%Genetic Algorithm in MATLAB always minimizes the function.
% [inputs, fval, exitFlag, Output] = ga(@stalagmite, 2);
%there are two values on the rhs within the function. the 1st one is we are
%calling the function which has to be minimized (in this case it is
%@stalagmite). The 2nd value is no. of variables in the function (from the
%shown equation we can see that there are two variables i.e., f(x,y).

%Searched 'Genetic Algorithm options' in help section.
%The Genetic Algorithm works on a population using a set of operators that
%are applied to the population. A population is a set of points in the
%design space. The initial population is generated randomly by default.
%The next generation of the population is computed using the fitness of the
%individuals in the current generation.
%It means that the 1st algorithm generates random values and the following
%generation tries to fit the initial population.


%Adding visualization
%It is used to visualize the performance of the solver at run time.
opts = optimoptions(@ga,'PlotFcn',{@gaplotbestf,@gaplotstopping});
[x,Fval,exitFlag,Output]= ga(@stalagmite,2,[],[],[],[],[],[],[],opts);
%Here we use optimoptions (inbuilt function) to select two plot functions.
%The first plot function is gaplotbestf, which plots the best and mean score
%of the population at every generation. The second plot function is
%gaplotstopping, which plots the percentage of stopping criteria satisfied.

%Function files

%Creating the Stalagmite function
function [fx_y] = 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;

f2_x = exp(-4*log(2).*((x-0.0667).^2/0.64));
f2_y = exp(-4*log(2).*((y-0.0667).^2/0.64));

fx_y = -(f1_x.*f1_y.*f2_x.*f2_y);
end

%Function file to get the array values

%Creating the F_Stalagmite function
function [f] = F_stalagmite(x,y)
[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
end

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.