Challenge

MATLAB Program: Genetic Algorithm

Aim: To study stalagmite function and calculate global maxima using Genetic Algorithm in MATLAB.

Objective:

  • Study Genetic Algorithm and Stalagmite function
  • Determination of global maxima by writing a MATLAB code
  • Plotting graph on studies done on Genetic Algorithm

Theory:

Stalagmite Function:

Stalagmite function is a mathematical model which helps to generate local minima and maxima. It is a three dimensional function and the way the function is designed such that it contains many local maximas and minimas and one global maxima and minima. The visualisation of this function resembles the stalagmite formation inside the cave; hence the name stalagmite function.

A Genetic Algorithm:

In computer science and operations research, a genetic algorithm is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms. 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. John holland introduced genetic algorithms in 1960 based on the concept of Darvin's theory of evolution, and his student David E Goldberg further extended GA in 1989. Hence, genetic algorithms simulate the process of natural selection.

Program:

For this problem, we need to define stalagmite function and call it in the genetic algorithm.

The genetic algorithm is as follows:

%GENETIC ALGORITHM
clear all
close all
clc

%Define search space
x = linspace(0,0.6,150);    %x-array
y = linspace(0,0.6,150);    %y-array
n = 50;                     %number of cases

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

%Stalagmite evaluation
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);
  endfor
endfor

%Study-1: Statistical Behaviour
tic
for i = 1:n
  [inputs,fopt(i)] = ga(@stalagmite,2);
  xopt(i) = inputs(1);
  yopt(i) = inputs(2);
endfor
study1_time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp1xlabel('X-axis')
ylabel('Y-axis')
title('Statistical Behaviour With Unbound inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No.of Iterations')
ylabel('Function maximum')


%Study-2: Statistical Behaviour With Upper and Lower Bounds
tic
for i = 1:n
  [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
  xopt(i) = inputs(1);
  yopt(i) = inputs(2);
endfor
study2_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical behaviour with upper and lower bounds')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfaecolour','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')


%Study-3: Statistical Bhaviour With Increasing Iterations of GA
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);

tic
for i = 1:n
  [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
  xopt(i) = inputs(1);
  yopt(i) = inputs(2);
endfor
study3_time = toc

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X-axis')
ylabel('Y-axis')
title('Statistical Behaviour with increasing iterations')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No.of Iterations')
ylabel('Funtion maximum')

The stalagmite function is as follows:

%Stalagmite Function

function [f] = stalagmite(input_vector)
  x = input_vector(1);
  y = input_vector(2);
  
  t1 = (sin((5.1*pi*x)+0.5))^6;
  t2 = (sin((5.1*pi*y)+0.5))^6;
  t3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
  t4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
  
  f = -(t1*t2*t3*t4);
endfunction

Working of code:

1. The code begins with clear all, close all, clc which means clear the workspace, close all figure windows and clear the console respectively.

2. Two linearly spaced arrays, namely "x-array" and "y-array" are created using linspace command having a range of 0-0.6 and having 150 intervals.

3. meshgrid command is used to create a 2D mesh from the above defined array and number of iterations are defined. The number of iterations are set to 50.

4. For evaluation of stalagmite function, nested for loop is used. Input vector 'xx' and 'yy' are provided. This forms our stalagmite function.

5. Now we study the three cases for the function and derive their respective plots. All this uses ga syntax.

6. In first case there aren't any restrictions, in second case we provide the bound for algorithm to run and in last case we increase the number of iterations.

7. For evaluation of time taken for the iteration to complete, we use "tic" and "toc" command.

8. The function derived in the loop is visulised by using surfc command, shading interp is used to neglect gridlines and obtaind a smooth and finished curve. plot3 cammand is used to generate a 3D plot of xopt, yopt and optimum value of function with 'o' marker. subplot command places these plots in single window.

9. Results are then studied.

Output and Result:

Study-1: Statistical behaviour with unbound inputs; this gives a unique output with each evaluation.

Study-2: Statistical behaviour with lower and upper bounds; this results in covergence of function maxima.

Study-3: Statistical behaviour with increasing iterations; further convergence leads to the solution, global maxima is found.

Hence, the global maxima value is obtained at `x=0.06683` and `y=0.06683` and optimum value (fopt) is approximately 1.

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.