Project

WEEK – 4 FINDING THE GLOBAL MAXIMA FOR THE STALAGMITE FUNCTION USING GENETIC ALGORITHM

AIM:

Write a program in MATLAB to find the global maxima for the stalagmite function using a genetic algorithm.

OBJECTIVE:

  • Code the stalagmite function in MATLAB.
  • Optimize the stalagmite function in MATLAB and find the global maxima.
  • Study 3 case of options of Genetic Algorithm ‘ ga ’ in MATLAB.

GOVERNING EQUATIONS AND THEORY:

  • The genetic algorithm solves smooth or nonsmooth optimization problems with any type of constraints, including integer constraints. It is a population-based algorithm that searches randomly by mutation and cross-over among population members.
  • Solver ‘ga’ finds the minimum of the function using a genetic algorithm.

HOW DOES GENETIC ALGORITHM WORKS:

  • The algorithm begins by creating the random initial population.
  • The algorithm then creates the sequence of the new populations.
  • At each step, the algorithm takes the very new population as the current population and generates the next population.

  • Select members, called parents are based on their expectations.
  • Some of the individuals in the current population that have lower fitness are selected as ‘ELITE’ members. These elite members are passed to the next population.
  • Formation of children from parents. The children are produced from parents by making random changes to the single parent – mutation or by combining the two separate parents by cross-over method.
  • Replacing the current population with children and forming the new parent to form the next generation.
  • The algorithm stops when one of the stop criteria is met.

FLOW CHART OF GENETIC ALGORITHM:

STALAGMITES:

  • A Stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from the ceiling
  • A close identical geometric shape can be obtained by using the mathematical function.
  • Following is the mathematical function for the stalagmite model.

GOVERNING EQUATIONS:

INPUTS:

[CODE]

OPTIMIZATION FUNCTION:

%% Optimization function for finding the global maxima
clc
clear all
close all


%% CASE 1
%% Defining workspace

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50;

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

%% Optimizing the Stalagmite function using Genetic Algorithm


tic
for i = 1:n
    [input,fun_opt(i)] = ga(@stalagmite,2);
    x_opt(i) = input(1);
    y_opt(i) = input(2);
end
study1_time = toc

figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_opt,y_opt,-fun_opt,'Marker','o','Markersize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fun_opt)
xlabel('ITERATIONS');
ylabel('GLOBAL MAXIMA');
title('Global Maxima Vs Iterations');
grid on

%% CASE 2

tic
for i = 1:n
    [input,fun_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
    x_opt(i) = input(1);
    y_opt(i) = input(2);
end
study2_time = toc

figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_opt,y_opt,-fun_opt,'Marker','o','Markersize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fun_opt)
xlabel('ITERATIONS');
ylabel('GLOBAL MAXIMA');
title('Global Maxima Vs Iterations');
grid on

%% CASE 3

% options = optimoptions(ga);
options = optimoptions('ga','Populationsize',250);

tic
for i = 1:n
    [input,fun_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
    x_opt(i) = input(1);
    y_opt(i) = input(2);
end
study3_time = toc

figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_opt,y_opt,-fun_opt,'Marker','o','Markersize',3,'MarkerFaceColor','r')
subplot(2,1,2)
plot(fun_opt)
xlabel('ITERATIONS');
ylabel('GLOBAL MAXIMA');
title('Global Maxima Vs Iterations');
grid on

STALAGMITE FUNCTION:

function [stalagmite] = 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((-4*log(2)*(x-0.0667)^2)/0.64);
 f2y = exp((-4*log(2)*(y-0.0667)^2)/0.64);

 stalagmite = -(f1x*f2x*f1y*f2y);

end

EXPLANATION:

The software used for the programming purpose is MATLAB R2019b.

  • The program starts with clc, clear all, close all
    • clc helps in clearing the command window.
    • Clear all clears all the data in the workspace
    • Close all closes all the window of figure opened in the background.

  • The main code is divided into three parts, in which certain commands are altered.
  • For initialization of the program, the values of X and Y are defined using LINSPACE
  • n is the population value initially defined; genetic algorithm randomly takes values between 1 to 50.
  • The array values of X and Y are used to make a mesh using MESHGRID
  • Using the for loop, up to the length of X and Y, the stalagmite function is evaluated.

Optimization of stalagmite function using genetic algorithm

In Case 1,

  • In case 1, using the for loop up to the length of the initial 50 population, the genetic algorithm optimizes the stalagmite function and generates the random 50 sets of population.
  • These input values are plotted using the plot command.
  • By using the tic-toc command, the time taken by the genetic algorithm to optimize the stalagmite function is can be determined.

Now, the nature of the genetic algorithm to is predict the minima of the function, hence to find the maxima a negative sign is introduced in the stalagmite function so the maxima are found.

The similar above steps are repeated in the next Case 2 and Case 3, and by increasing the population size and bounding the genetic algorithm to the limited no. of population, the optimization can be converged to find the global maxima.

OUTPUTS:

Workspace

Case 1:

Case 2:

Case 3:

CONCLUSION:

  • Hence, by using the Genetic Algorithm, we have optimized the stalagmite function and found the global maxima for the function.
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.