Challenge

Finding Global maxima in MATLAB

Aim:

To write a code that optimises stalagmite function and to find the global maxima of the function.

Objectives:

  • To Explain the concept and syntax of genetic algorithm in MATLAB.
  • To write the MATLAB code that finds the global maxima.

Genetic algorithm:

  • A genetic algorithm is a pursuit heuristic that is propelled by Charles Darwin's hypothesis of natural evolution.
  • This procedure of natural selection begins with the choice of the fittest people from a populace. They produce posterity which acquire the qualities of the guardians and will be added to the people to come. In the event that guardians have better wellness, their posterity will be superior to guardians and have a superior possibility at enduring. This procedure continues emphasizing and toward the end, a generation with the fittest people will be found.
  • So, this algorithm can be used in a search process where the stages included are: Initial population, Fitness function, Selection, Crossover, Mutation.
  • Initial population: The procedure starts with a lot of people which is known as a Population. Every individual is an answer for the difficult you need to settle. An individual is portrayed by a lot of parameters (factors) known as Genes. Qualities are joined into a string to frame a Chromosome (arrangement).
  • Fitness function: This function is used to decide the fitness of an individual i.e the capacity of the person to contend with others. The likelihood that an individual will be chosen for generation depends on its fitness score that is calculated previously.
  • Selection: The possibility of the selection stage is to choose the fittest people and let them pass their qualities to the people to come. Two sets of people i.e parents are chosen dependent on their fitness scores. People with high wellness have increased opportunity to be chosen for proliferation.
  • Crossover: Crossover is the process where parents having alluring attributes are chosen for making another arrangement from the current arrangement pool.
  • Mutation: In certain new posterity framed, a portion of their qualities can be exposed to a transformation with a low irregular likelihood. This infers a portion of the bits in the bit string can be flipped.
  • The process happens until the population converges i.e doesn't create posterity which are essentially not the same as the past generation. This is called Termination. So, then we can be assured that the genetic algorithm has produced solutions to the problem.
  • Syntax of ga in MATLAB: The ga function in MATLAB is used to find the minimum of the function.
  • X = ga(fun,nvars) is the syntax used for the case of the unbounded inputs (study 1) which is used for finding the local minima using the function defined earlier. Here, the 'fun' is a function that is defined earlier and 'nvars' is the dimension that is the number of variables for the function.
  • X = ga(fun,nvars,A,b,Aeq,beq,lb,ub) is the syntax used for the case of bounded inputs (study 2). 'A' and 'b' refers to the linear inequalities and 'Aeq', 'beq' are the linear equalities where in this case is not used. The 'lb' (lower bound) and 'ub' (upper bound) is used in this case.
  • X = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,Intcon,options) is the syntax used for the case of increased population size. Here, 'nonlcon' refers to non-linear constraints and the variables which are listed in Intcon can take the values of integers. In this options are manipulated by using optimoptions.

MATLAB Code:

Code for stalagmite function:

The stalagmite function is defined as given below.

The function code for the stalagmite function is generated as given below

function [f]=stalagmite_fn(input_vector)
x=input_vector(1);
y=input_vector(2);
f1x= (sin(5.1*pi*x+0.5))^6;%the stalagmite function is split into four terms for
f2x=exp(-4*log(2)* ((x-0.0667)^2)/(0.64));%understanding purpose
f1y=(sin(5.1*pi*y+0.5))^6;
f2y=exp(-4*log(2)* (y-0.0667)^2/(0.64));
f=(-1)*f1x*f2x*f1y*f2y;%-1 is multiplied for the finding of global maxima

The main code of all the three studies :

clear all
close all 
clc

%creating the search space
x=linspace(0,0.6,150);%will take 150 values in the range of 0 to 0.6
y=linspace(0,0.6,150);%will take 150 values in the range of 0 to 0.6

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

for i=1:length(xx)%for the inputs of stalagmite function
    for j=1:length(yy)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stalagmite_fn(input_vector);%input vector is 1*2 vector
    end
end

num_cases=50;%number of iterations


%study1- statistical behaviour

tic%The timer will start (for the calculation of running time of study-1)
for i=1:num_cases%loop for iterative calculations
    [inputs,fopt(i)]=ga(@stalagmite_fn,2);%Inputs are taken from random number generator
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1_time =toc%Timer will end

figure(1)%A figure is created
subplot(2,1,1)%multiple plotting by splitting the plot i.e it will generate 2*1 matrix %wherein 3d plot is placed on the 1st row
hold on%to plot other plots at the same time
surfc(x,y,-f)%-f is used to plot the maxima 
shading interp%plot will be without the grids 
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')%to plot the %maximas by marking 
title('unbounded inputs')%to name the whole plot
xlabel('x-axis')%to label the x-axis of the first plot
ylabel('y-axis')%to label the y-axis of the first plot
subplot(2,1,2)%it will split the plot in 2*1 matrix form and plot the 2nd plot in the %2nd row
plot(-fopt)
xlabel('Iterations')%to label the x-axis of the second plot
ylabel('Function maximum')%to label the y-axis of the second plot

%Study-2 Statistical behaviour with upper and lower bounds

tic%The timer will start (for the calculation of running time of study-2)
for j=1:num_cases%loop for iterative calculations
    [inputs,fopt(j)]=ga(@stalagmite_fn,2,[],[],[],[],[0 0],[1 1]);
    xopt(j)=inputs(1);
    yopt(j)=inputs(2);
end
study2_time =toc%Timer will end

figure(2)%A figure is created
subplot(2,1,1)%multiple plotting by splitting the plot i.e it will generate 2*1 matrix %wherein 3d plot is placed on the 1st row
hold on%to plot other plots at the same time
surfc(x,y,-f)%-f is used to plot the maxima 
shading interp%plot will be without the grids 
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')%to plot the %maximas by marking 
title('bounded inputs')%to name the whole plot
xlabel('x-axis')%to label the x-axis of the first plot
ylabel('y-axis')%to label the y-axis of the first plot
subplot(2,1,2)%it will split the plot in 2*1 matrix form and plot the 2nd plot in the %2nd row
plot(-fopt)
xlabel('Iterations')%to label the x-axis of the second plot
ylabel('Function maximum')%to label the y-axis of the second plot


%Study-3 Increasing the iterations

options=optimoptions('ga');%to manipulate the options 'optimoptions' is used
options=optimoptions(options,'Populationsize',250);%in the options 'populationsize' %parameter is changed

tic%The timer will start (for the calculation of running time of study-3)
for k=1:num_cases%loop for iterative calculations
    [inputs,fopt(k)]=ga(@stalagmite_fn,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(k)=inputs(1);
    yopt(k)=inputs(2);
end
study3_time =toc%Timer will end

figure(3)%A figure is created
subplot(2,1,1)%multiple plotting by splitting the plot i.e it will generate 2*1 matrix %wherein 3d plot is placed on the 1st row
hold on%to plot other plots at the same time
surfc(x,y,-f)%-f is used to plot the maxima 
shading interp%plot will be without the grids
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')%to plot the %maximas by marking
title('bounded inputs - increase in population size')%to name the whole plot
xlabel('x-axis')%to label the x-axis of the first plot
ylabel('y-axis')%to label the y-axis of the first plot
subplot(2,1,2)%it will split the plot in 2*1 matrix form and plot the 2nd plot in the %2nd row
plot(-fopt)
xlabel('Iterations')%to label the x-axis of the second plot
ylabel('Function maximum')%to label the y-axis of the second plot

RESULTS:

Study-1:

The first study is done by unbounded inputs wherein the maximum value ranges from 0 to 1 with 50 iterations. It doesn't give a particular maximum either a definite range. So, this type of study is not useful.

Study-2:

Here, the syntax for ga is modified by adding the boundaries. This study gives maximum values in a range of 0.7 to 1. The run time taken for this is little more than the study-1. So, by this, a definite range is created but this is not enough to find the maxima.

Study-3:

Here, the population size is modified by using optimoptions in the ga function. This study takes the most time for optimization. A particular value for the maxima is found here by increasing the population size. So, the global maximum is found in this study accurately.

Errors:

No Complex errors were found during the simulation of the program. Only typing errors that can be corrected by seeing the command window.

Conclusion:

So, out of these studies, we can conclude that study-3 is accurate enough for finding global maxima with 50 iterations.

References:

  1. https://in.mathworks.com/help/gads/ga.html#d120e39200
  2. https://en.wikipedia.org/wiki/Genetic_algorithm
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.