Project

OPTIMIZATION OF STALAGMITE FUNCTION USING GENETIC ALGORITHM

AIM:

To generate code for the Stalagmite function and optimize it using the ‘Genetic Algorithm’ function of MATLAB (‘ga’)

OBJECTIVES:

  • To understand the concept of Genetic Algorithm and how to utilize it to get desired result
  • To understand the functioning of the Stalagmite function
  • To understand the syntax of the ‘ga’ function in MATLAB

INTRODUCTION:

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.

Stalagmite function is a special type of function which depicts the structure of the stalagmite peaks. It is a rising from the bottom of a limestone cave due to the dripping of mineralized solutions and deposits of CaCO3.

The general syntax of ‘ga’ function:

  • [x, fval] = ga(@function_name, no. of variables);

This returns the optimizing value ‘fval’ at point x.

  • [x,fval] = ga(@function_name, no. of variables, [],[],[],[],[lb],[ub]);

lb corresponds to lower bound and ub corresponds to upper bound

  • [x,fval] = ga(@function_name, no. of variables, [],[],[],[],[lb],[ub],[],[],options);

To change certain parameters under ‘ga’ function, options is used. For more details, refer the code.

EQUATIONS USED:

The following equations are used to generate the stalagmite function:

  • `f_(1,x)` = `(sin(5.1pix + 0.5))^6`
  • `f_(1,y)` = `(sin(5.1piy + 0.5))^6`
  • `f_(2,x)` = `exp[-4ln(2)((x-0.0667)^2)/0.64]`
  • `f_(2,y)` = `exp[-4ln(2)((y-0.0667)^2)/0.64]`

`f(x,y)` = `f_(1,x)f_(1,y)f_(2,x)f_(2,y)`

Note: Here, we will change the orientation of the stalagmite function to get the maximum as the 'ga' function always gives the minimum of a function. Changing the orientation will result in the maximum of the value into the minimum of the function.

PROCEDURE OF COMPLETION:

  • First, the stalagmite function was defined in a separate function which can be called into the main script time and time again.
  • Then, the function was optimized by using 3 methods:
    • First, by normal optimization
    • Second, by bounding the values from 0 to 1
    • Third, by changing the ‘PopulationSize’ under options to 500
  • Now, the plots for all the 3 conditions were plotted with surface plots for better visualization.

The following code can be referred for better understanding:

%% OPTIMIZING STALAGMITE FUNCTION USING GENETIC ALGORITHM
% Genetic Algorithm is used to get the minimum of a function. But, here we
% will be maximizing the  Stalagmite function using it by changing the 
% orientation of the original function.
clear all;
close all;
clc;

%% Initialization of variables
x = linspace(0,0.5,200);
y = linspace(0,0.5,200);
n = 150;

% creating meshgrid
[xx,yy] = meshgrid(x,y);

% initializing the  stalagmite function
for i = 1:length(x)
   for j = 1:length(y)
      
       f_vector(1) = xx(i,j);
       f_vector(2) = yy(i,j);
       f(i,j)      = stal_func(f_vector);
       
   end
end



%% Case 1 : Unbounded values
for i = 1:n
    [f_vector1, f_val1(i)] = ga(@stal_func,2);
    x1(i)                  = f_vector1(1);
    y1(i)                  = f_vector1(2);

end



%% Plotting
figure(1), clf
subplot(211)
hold on
surf(x,y,f)
shading interp
title('CASE I : UNBOUNDED VALUES','fontsize',15)
plot3(x1,y1,f_val1,'linew',2,'marker','o')
subplot(212)
plot(f_val1,'linew',2)
xlabel('ITERATIONS','fontsize',12)
ylabel('FUNC MAX','fontsize',12)


%% Case 2 : Bounding values of x from 0 to 1
for j = 1:n
    [f_vector2, f_val2(j)] = ga(@stal_func,2,[],[],[],[],[0;0],[1;1]);
    x2(j)                  = f_vector2(1);
    y2(j)                  = f_vector2(2);

end



%% Plotting
figure(2), clf
subplot(211)
hold on
surf(x,y,f)
shading interp
title('CASE II : BOUNDED VALUES OF X(0-1)','fontsize',15)
plot3(x2,y2,f_val2,'linew',2,'marker','o')
subplot(212)
plot(f_val2,'linew',2)
xlabel('ITERATIONS','fontsize',12)
ylabel('FUNC MAX','fontsize',12)


%% Case 3 : Bounding the values and increasing the population size to 500
options = gaoptimset('PopulationSize',500);

for k = 1:n
    [f_vector3, f_val3(j)] = ga(@stal_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
    x3(j)                  = f_vector3(1);
    y3(j)                  = f_vector3(2);

end



%% Plotting

figure(3), clf
subplot(211)
hold on
surf(x,y,f)
shading interp
title('CASE III : BOUNDED VALUES WITH INCREASE IN POPULATION SIZE','fontsize',15)
plot3(x3,y3,f_val3,'linew',2,'marker','o')
subplot(212)
plot(f_val3,'linew',2)
xlabel('ITERATIONS','fontsize',12)
ylabel('FUNC MAX','fontsize',12)

The stalagmite function was defined as follows:

function [f] = stal_func(f_vector)
%% This function is defined to get the stalagmite function for which we are
%  finding the global maximum of.
%
%  INPUTS :
%           1. f_vector          x and y-coordinates     
%
%  OUTPUTS :
%           2. f                 stalagmite function
%
%
%% Iniitialization of vectors
x = f_vector(1);
y = f_vector(2);


%% Functions inside the stalagmite function
f1 = (sin(5.1*pi*x + 0.5)).^6;
f2 = exp(-4*log(2)*((x-0.0667).^2)/0.64);
f3 = (sin(5.1*pi*y + 0.5)).^6;
f4 = exp(-4*log(2)*((y-0.0667).^2)/0.64);

% stalagmite function
f  = -f1.*f2.*f3.*f4;


end

OBSERVATIONS:

  • Case 1 : Unbounded values
  • Case 2 : Bounded values of x between 0-1
  • Case 3 : Bounded values of x alongwith increment in population-size to 500

From the above pictures, we can conclude that the optimization can be better by increasing the population-size and bounding the values of x.

CONCLUSION:

Genetic Algorithm has been a revelation in the field of biology and other aspects of engineering in finding the optimum values of required functions. But, as in MATLAB it is in the global optimization toolbox, it takes a lot of processing time as it takes under consideration a lot of paramters. Hence, it is advisable to consider other methods of optimization instead of 'ga' function for smaller and faster calculations.

REFERENCES:

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.