Project

Optimize the stalagmite function and calculate the global maxima of the function

AIM:

To write a MATLAB program to optimise the stalagmite function and to find the global maxima of the function.

THEORY:

GENETIC ALGORITHM (GA): A Genetic Algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. It 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.

The genetic algorithm can be used to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.

  • GA always searches for a Global Minimum to an optimization problem.
  • It starts with an initial generation of candidate solutions that are tested against he objective function.
  • Subsequent generations evolve from the 1st generation through selection, cross-over and mutation.

Five phases in Genetic Algorithm (GA) are:

  1. Initial Population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation

Syntax for genetic algorithm:

  1. x=ga(fun,nvars). Here \'fun\' refers to the function (stalagmite function) and \'nvars\' refers to dimension of the function. It finds a local unconstrained minimum, \'x\', to the objective function, \'fun\' Example:
    [input_vector,fopt(i)]=ga(@stalagmite,2)​
  2. x=ga(fun,nvars,A,b,Aeq,beq,lb,ub). It defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub (Aeq=[] and beq=[] if no linear equalities exists). Example:
    [input_vector,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
  3. ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options). Here \'nonlcon\' is a function that accepts a vector or array x and returns two arrays, c(x) and ceq(x). Each value in \'IntCon\' represents an x component that is integer-valued. \'options\' refers to optimization options\'. Example:

    [input_vector,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)

Fig 1. Shows optimization workflow

STALAGMITE FUNCTION:

PROGRAM:

Main Program:

%Program to optimise the Stalagmite function and find the global maxima
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
num_cases=50;
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
tic
%Study 1- Statistical Behaviour
for i=1:num_cases
    [input_vector,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=input_vector(1);
    yopt(i)=input_vector(2);
end
study1_time=toc;
%Plotting the figure for study 1
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
title(\'Unbounded Inputs\')
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
tic
%Study 2 - Statistical Behaviour-with upper and lower bounds
for i=1:num_cases
    [input_vector,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=input_vector(1);
    yopt(i)=input_vector(2);
end
study2_time=toc;
%Plotting the figure for study 2
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
title(\'Bounded Inputs\')
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
%Study 3 - Increasing genetic algorithm iterations
options=optimoptions(\'ga\');
options=optimoptions(options,\'PopulationSize\',250);
tic
for i=1:num_cases
    [input_vector,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i)=input_vector(1);
    yopt(i)=input_vector(2);
end
study3_time=toc;
%Plotting the figure for study 3
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
subplot(2,1,2)
plot(fopt)
title(\'Bounded Inputs\')
xlabel(\'Iterations\')
ylabel(\'Function Maximum\')
%Display the Time Taken for the Study
disp(\'Time taken for Study 1=\')
disp(study1_time)
disp(\'Time taken for Study 2=\')
disp(study2_time)
disp(\'Time taken for Study 3=\')
disp(study3_time)
max=min(input_vector);
%Display the point of global maximum
disp(\'Global Maxima is at x=\')
disp(max)

Function:

%Creating function \'stalagmite\'
function f = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
%Defining f1x,f1y,f2x and f2y
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);
f=-f1x.*f2x.*f1y.*f2y;
end

CODE EXPLANATION:

Main Program code:

  • \'clear all\' is used to clear all the variables in the workspace, \'close all\' is used to close all the plots and figures and \'clc\' is used to clear the command window.
  • The linspace function generates linearly spaced vectors.
  • \'linspace(0,0.6,150)\' generates 150 values between 0 and 0.6 which are equally spaced. The arrays are 1D.
  • The \'meshgrid\' command transforms the domain specified by vectors x and y into arrays X and Y (2D).
  • The number of cases is set as 50.
  • \'for\' loop is executed to get function values at different co-ordinates.
  • The function \'stalagmite\' is called.
  • In Study 1, generic algorithm is used inside the for loop.
  • \'tic and toc\' function is used to calculate the study time. \'tic\' starts a stopwatch timer and \'toc\' prints the elapsed time since tic was used.
  • The \'subplot\' command is used to plot two plots in a single figure window. One \'subplot\' command is used to display the surface curve for the \'stalagmite\' function along with the optimization points. The 2nd \'subplot\' command is used to display the plot of number of iterations versus function maximum.
  • The \'surfc\' command is used to generate the 3D surface.
  • The \'shading interp\' command is used to remove the lines from the 3D surface and create a smooth shading.
  • The \'plot3\' function displays a three-dimensional plot of a set of data points.
  • \'title\' command is used to give title name to the figure.
  • The commands \'xlabel\' and \'ylabel\' are used to name the x-axis and y-axis.

Function code:

  • The function \'stalagmite\' is defined.
  • f1x, f2x, f1y and f2y are defined.
  • f which is a product of f1x, f2x, f1y and f2y is defined.
  • \'-ve\' sign is used as the genetic algorithm returns the global minima but we need the global maxima.

RESULTS:

Fig 2. Shows the plot for Study 1

Fig 3. Shows the plot for Study 2

Fig 4. Shows the plot for Study 3

Fig 5. Shows the results displayed in the command window

ERRORS:

  • Error in entering the subplot command. Instead of entering \'subplot(2,1,2)\', entered \'subplot(2,1,1)\'.

REFERENCES:

  1. https://in.mathworks.com/discovery/genetic-algorithm.html
  2. https://in.mathworks.com/help/gads/ga.html
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.