Project

To calculate the Global Maxima of Stalagmite Function using MATLAB

Aim

To write a Program in Matlab to calculate the Global Maxima of Stalagmite Function .To study about Genetic Algorithm and to calculate the global maxima of stalagmite function using the Genetic

Objective

1.To define the stalagmite function using Matlab

2.To Calculate Global maxima

3.To find the optimum value of the stalagmite function through

1) Statistical behavior method
2) Statistical behavior with bounds method
3) Statistical behavior with bounds and population size method

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. We can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, non-differentiable, stochastic, or highly nonlinear.

The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-values.

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

i.Selection rulesselect the individuals, called parents, that contribute to the population at the next generation.

ii.Crossover rulescombine two parents to form children for the next generation.

iii.Mutation rules apply random changes to individual parents to form children.

Global Maxima:

A global maximum, also known as an absolute maximum, the largest overall value of a set, function, etc., over its entire range. It is impossible to construct an algorithm that will find a global maximum for an arbitrary function.

Stalagmite function:

This is mathematical model helps to generate local minima and maxima.

The equations are as follows,

Software used:

MATLAB R2018

CODE:

%Global Maxima of a Stalagmite Function Using MATLAB
clear all 
close all
clc

%setting up the search spcae
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_iteration = 50

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

%for loop for salagmite function 
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) = stalgamite_function(input_vector);
    end
end

figure
surfc(x,y,-f)
colorbar
%labelling the axis
xlabel('x-values')
ylabel('y-values')
zlabel('function-values')
title('stalagmite function')
shading interp

[x_output , fval] = ga(@stalgamite_function,2)

%Study 1 ( Statistical Behaviour )
tic
for i = 1:num_iteration
    [inputs , fopt(i)] = ga(@stalgamite_function,2);
    xopt1(i) = inputs(1);
    yopt1(i) = inputs(2);
end
Study1_Timing = toc         %time study
figure
subplot(2,1,1)              %creating subplot 
hold on
surfc(x,y,-f)
shading interp
plot3(xopt1,yopt1,-fopt,'marker','o','markersize',6,'markerfacecolor','g')
title('unbounded input')    %proving plot title
subplot(2,1,2)              %creating another subplot
plot(-fopt)
%labelling the axis
xlabel('iterations')
ylabel('function maximum')


%Study 2 ( Statistical Behaviour- With upper and Lower Bounds )
tic
for i = 1:num_iteration
    [inputs , fopt(i)] = ga(@stalgamite_function,2,[],[],[],[],[0;0],[1;1]);
    xopt2(i) = inputs(1);
    yopt2(i) = inputs(2);
end
Study2_Timing = toc        %time study
figure
subplot(2,1,1)             %creating subplot 
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')   %proving plot title
subplot(2,1,2)             %creating another subplot
plot(-fopt)
%labelling the axis
xlabel('iterations')
ylabel('function maximum')


%Study 3 ( increasing ga iterations )
options = optimoptions('ga')
options = optimoptions(options , 'populationsize', 200)
tic
for i = 1:num_iteration
    [inputs , fopt(i)] = ga(@stalgamite_function,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt3(i) = inputs(1);
    yopt3(i) = inputs(2);
end
Study3_Timing = toc         %time study
figure
subplot(2,1,1)              %creating subplot 
hold on
surfc(x,y,-f)
shading interp       
plot3(xopt3,yopt3,-fopt,'marker','o','markersize',6,'markerfacecolor','b') %plotting the values
title('unbounded input')    %proving plot title
subplot(2,1,2)              %creating another subplot
plot(-fopt)
%labelling the axis
xlabel('iterations')
ylabel('function maximum')

Code for Function:

%stalgamitefunction to generate local minima and maxima
function [f] = stalgamite_function(input_vector)

x = input_vector(1);
y = input_vector(2);

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)*(x - 0.0667)^2/0.64);

f = -(f1x*f1y*f2x*f2y);
end                  %end of function

Code Explanation:

1.First enter the basic commands clear all, close all and clc.

2.Then define the values of x and y from 0 to 0.6 in the range of 150 values using linspace cmd.

3.Then use Meshgrid command to create 2D array, the dimension of the array is 150*150.

i.e., xx= 150*150 and yy=150*150.

4.Now use for loop for calling stalagmite_function and the surfc command is used to plot 3D graphs.

5.The number of cases is set to 50 that means ga runs 50 times and give results.

Study 1 ( Statistical behavior):

1.The Number of cases is defined as 50 and for loop is used to get output of x and y.

2.The tic and toc command use for calculating time required for the operation.

3.Shading interp command is used to get a smooth plot.

4.The time taken for this study is less than the other 2 studies.

Study 2 (Statistical behavior with upper and lower bound):

1.The Number of cases is defined as 50 and for loop is used to get output of x and y.

ga(@stalagmite_function,2,[ ],[ ],[ ],[ ],[0;0],[1;1]) In this the first 2 empty bracket ([],[]) shows linear equality constraints and other 2 ([ ],[ ]) shows linear inequality constraints. Next([0;0],[1;1]) shows the upper and lower limits in which results are shown.

3.In this study the values are closer to global maxima

4.The time taken for this study is higher than the previous study

Study 3 (Incraesing ga iteration):

1.The Number of cases is defined as 50 and for loop is used to get output of x and y.

2.ga(@stalagmite_function,2,[ ],[ ],[ ],[ ],[0;0],[1;1]) In this the first 2 empty bracket ([ ],[ ])shows linear equality constraints and other 2 ([ ],[ ]) shows linear inequality constraints. Next ([0;0],[1;1]) shows the upper and lower limits in which results are shown.

3.[0;0] and [1;1] is the range of x and y and it is fixed.

4.The number of iterations is set to 380 iterations.

5.This study gives a much closer Value to the global maxima.

6.The time taken for this study is higher than the previous two studies.

Output:

For Stalagmite function:

For Study 1 ( Statistical behavior):

For Study 2 (Statistical behavior with upper and lower bound):

For Study 3 (Incraesing ga iteration):

Time taken for each study:

(Highlighted using marker in snipping tool)

Errors faced:

1)

Incorrectly enclosed the square brackets.

2)

Mentioned the cmd(surfc) incorrectly as "serfc".

References:

1.https://www.mathworks.com/discovery/genetic-algorithm.html

2.https://in.mathworks.com/help/gads/examples/genetic-algorithm-options.html

Conclusion:

The accuracy of the results that ga provides is directly proportional to the number of iterations. At the same time the more the number of iterations the more is the amount of time required to process the results.

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.