Project

Global optimization of Stalagmite function using Genetic Algorithm

ABSTRACT:

The main aim of this project is to optimize and find global maxima for the stalagmite function using the genetic algorithm in Matlab using the ga function. Here, different studies have been carried out to improve the results by modifying the syntax for the ga function

INTRODUCTION:

A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected. It is a method for solving both constrained and unconstrained optimization problems that is based on natural selection.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  1. Selection rules select the individuals, that contribute to the population at the next generation.
  2. Crossover rules combine two parents to form children for the next generation.
  3. Mutation rules apply random changes to individual parents to form children.

ga Function & its syntax:

F = ga(fun,nvars,A,b,Aeq,beq,nonlcon,Intcon,lb,ub,options)

where,

F is vector containing the optimum value of the function and its corresponding input values

nvars is number if variables

A,b are linear inequality constraints

Aeq,beq are linear equality constraints

lb, ub are lower and upper bounds respectively

nonlcon is for the use of nonlinear constraints

Intcon is to get the solution in integer

options to minimize with nondefault options

Stalagmite Function:

Give below is the stalagmite function:

Using the Matlab code a function has been created to call the stalagmite function. It is attached below:

% Creating a stalagmite function
function f = stalagmite(a)
       x = a(1);
       y = a(2);
       term1 = (sin(5.1*pi*x+0.5)).^6;
       term2 = (sin(5.1*pi*y+0.5)).^6;
       term3 = (-4*log(2)*(x-0.0667).^2)/0.64;
       term4 = exp(term3);
       term5 = (-4*log(2)*(y-0.0667).^2)/0.64;
       term6 = exp(term5);
       
       f = -term1.*term2.*term4.*term6;
end
  • This function has x and y as input values
  • Usually, ga function will evaluate the minimum value of the function, to get the maximum value minus sign has been attached to the function
  • Entire function is split into four parts and multiplied as a whole.

MATLAB CODE FOR OPTIMIZATION OF FUNCTION:

%Optimizing genetic algorithm

close all
clear all
clc

%Defining work space
x = linspace(0,0.6,250);
y = linspace(0,0.6,250);

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

%Evaluating stalagmite function
for i = 1:length(xx)
    for j = 1:length(yy)
        inputvector(1) = xx(i,j);
        inputvector(2) = yy(i,j);
        f(i,j) = stalagmite(inputvector);
    end
end

%Study_1 Assigning number cases for statistical behaviour
tic
num_cases = 150;
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time = toc

%Plotting the minimum values in figure 1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',10,'markerfacecolor','r')
xlabel('x-axis')
ylabel('y-axis')
title('Study-1: Inputs without limits')
subplot(2,1,2)
plot(fopt)
xlabel('No. of Iterations')
ylabel('Maxiumum value of function')

%Study_2 Statistical behaviour with lower and upper limits
tic
num_cases = 150;
for i = 1:num_cases
[inputs1,fopt1(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.8;0.8]);
xopt1(i) = inputs1(1);
yopt1(i) = inputs1(2);
end
study2_time = toc

%Plotting the minimum values in figure2
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt1,yopt1,fopt1,'marker','o','markersize',10,'markerfacecolor','r')
xlabel('x-axis')
ylabel('y-axis')
title('Study-2: Inputs with limits')
subplot(2,1,2)
plot(fopt1)
xlabel('No. of Iterations')
ylabel('Maxiumum value of function')

%Study_3 Statistical behaviour with increasing GA iterations
options = gaoptimset('PopulationSize',250);

tic
num_cases = 120;
for i = 1:num_cases
[inputs2,fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
study3_time = toc

%Plotting the minimum values in figure2
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt2,yopt2,fopt2,'marker','o','markersize',10,'markerfacecolor','r')
xlabel('x-axis')
ylabel('y-axis')
title('Study-3: Increasing GA iterations')
subplot(2,1,2)
plot(fopt2)
xlabel('No. of Iterations')
ylabel('Maxiumum value of function')

Steps Involved:

  • Here, three studies have been carried using the ga function to optimize the function i.e. with and without limits and by the increasing number of iterations.
  • X and Y arrays were defined using the linspace function and a 2D mesh has been created using meshgrid function.
  • Using for loops the stalagmite function is evaluated for different positions at x and y and the same has been plotted using surfc function.
  • Time for each study has been measured using tic and toc functions. As each time the ga function is evaluated it will give different value, so cases have been taken for study and each optimized value has been stored in an array
  • The minimum value in each case has been marked in the surface plot using plot3 function. For identifying the trend in minimum value a separate subplot has been drawn for function minimum values corresponding to the number of cases.
  • As many of the minimum values come in the negative region, the limits have been fixed for the inputs x and y. In this study 2, the results have been improved but the time has increased.
  • In study 3 using gaoptimset option, the number of iterations has been increased, this has further increased the time but has given the best results out of the three studies.

RESULTS AND PLOTS:

a) Study-1: Statistical behavior with unbounded inputs

  1. Some of the inputs for the optimization were negative and the values were fluctuating.
  2. Time taken by this study is 13.142 seconds

b) Study-2: Statistical behavior with bounded inputs

  1. Improved results when compared to study 1 with less varied results
  2. Time taken by this study is 24.10 seconds

c) Study-3: Bounded inputs with increased iterations

  1. Further optimized results with only varying at two points
  2. Time taken by this study is 185.5 seconds
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.