Project

CALCULATING THE GLOBAL MAXIMA OF A STALAGMITE FUNCTION USING GENETIC ALGORITHM

AIM :

To Write a code to Calculate the Global Maxima of a stalagmite function using Genetic Algorithm.

OBJECTIVE :

In MATLAB, Using Equation of Stalagmite Function and Genetic Algorithm Syntax to calculate the Global Maxima.

GENETIC ALGORITHM :

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 for reproduction in order to produce offspring of the next generation.

USING OF GENETIC ALGORITHM :

Optimization − Genetic Algorithms are most commonly used in optimization problems wherein we have to maximize or minimize a given objective function value under a given set of constraints.

CONCEPT OF GENETIC ALGORITHM :

The following outline summarizes how the genetic algorithm works:

A.The algorithm begins by creating a random initial population.

B.The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:

  • Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.

  • Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.

  • Selects members, called parents, based on their expectation.

  • Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.

  • Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.

  • Replaces the current population with the children to form the next generation.

The algorithm stops when one of the stopping criteria is met.

STOPPING CRITERIA FOR GENETIC ALGORITHM :

The genetic algorithm uses the following conditions to determine when to stop:

  • Generations — The algorithm stops when the number of generations reaches the value of Generations.

  • Time limit — The algorithm stops after running for an amount of time in seconds equal to Time limit.

  • Fitness limit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to Fitness limit.

  • Stall generations — The algorithm stops when the average relative change in the fitness function value over Stall generations is less than Function tolerance.

  • Stall time limit — The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to Stall time limit.

  • Stall test — The stall condition is either average change or geometric weighted. For geometric weighted, the weighting function is 1/2n, where n is the number of generations prior to the current. Both stall conditions apply to the relative change in the fitness function over Stall generations.

  • Function tolerance — The algorithm runs until the average relative change in the fitness function value over Stall generations is less than Function tolerance.

  • Constraint tolerance — The Constraint tolerance is not used as stopping criterion. It is used to determine the feasibility with respect to nonlinear constraints. Also, max(sqrt(eps),ConstraintTolerance) determines feasibility with respect to linear constraints.

The algorithm stops as soon as any one of these conditions is met. You can specify the values of these criteria in the Stopping criteria pane in the Optimization app.

GENETIC ALGORITHM - SYNTAX :

1. x = ga(fun,nvars)

fun - function name

nvars - number of variables

In this we using ga to find the local minima value. In MATLAB ga always gives local minima value.

2. x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub)

The above syntax defines a set of lower and upper bounds on the designed variables x, so that a solution is found in the range lb(lowerbounds) ≤ x ≤ ub(upperbounds).

3. x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,Intcon,options)

The above syntax minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist) .Create options using optimoptions.

GLOBAL MAXIMA :

A global maximum, also known as an absolute maximum, the largest overall value of a set, function, etc., over its entire range.

GLOBAL MINIMA :

A global minimum, also known as an absolute minimum, the smallest overall value of a set, function, etc., over its entire range.

STALAGMITE FUNCTION :

A stalagmite function is a natural phenomena which occurs when a upward growing mound of mineral is deposited by precipitation from water dripping onto the floor of a cave. The function we have considered GA optimization is similar to stalagmite, hence the name stalagmite function.

The equation of stalagmite function is given below,

FUNCTION PROGRAM FOR A STALAGMITE FUNCTION :

function f = stalagmite(input_vector)
        x = input_vector(1);
        y = input_vector(2);
x1 = (sin((5.1*pi*x)+0.5))^6;
y1 = (sin((5.1*pi*y)+0.5))^6;
x2 = exp (-4*log(2)*(x-0.667)^2/0.64);
y2 = exp (-4*log(2)*(y-0.667)^2/0.64);
f = -(x1*x2*y1*y2);
end

EXPLANATION FOR A STALAGMITE FUNCTION PROGRAM :

Step 1 : Create function program for a stalagmite function.

function f = stalagmite(input_vector)

Step 2 : Give Inputs value to stalagmite function to calculate the values in formula.

x = input_vector (1);

y = input_vector(2);

Step 3 : Use equation of stalagmite function in a program to find the function value.

x1 = (sin((5.1*pi*x)+0.5))^6;
y1 = (sin((5.1*pi*y)+0.5))^6;
x2 = exp (-4*log(2)*(x-0.667)^2/0.64);
y2 = exp (-4*log(2)*(y-0.667)^2/0.64);
f = -(x1*x2*y1*y2);

Step 4 : Finally we will find the f value for the stalagmite function and end the function program.

PROGRAM FOR CALCULATING A GLOBAL MAXIMA OF A STALAGMITE FUNCTION USING GENETIC ALGORITHM :

clear all
close all
clc

x = linspace(0,1,150);
y = linspace(0,1,150);

[xx,yy] = meshgrid(x,y);

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
figure(1)
surfc(xx,yy,-f)
shading interp
xlabel('x-value')
ylabel('y-value')
colorbar

[inputs,fval] = ga(@stalagmite,2)
num_cases = 50;

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


    figure(2)
    subplot(2,1,1)
    hold on
    surfc(xx, yy,- f)
    shading interp
    xlabel('x-value')
    ylabel('y-value')
    plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',4,'markerfacecolor','r')
    title('Unbounded inputs')
    subplot(2,1,2)
    shading interp
    plot(-fopt)
    xlabel('Iterations')
    ylabel('Function maximum')
    colorbar
    
%Study 2 - Statistical Behaviour - with Upper and Lower bounds
tic
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time = toc


    figure(3)
    subplot(2,1,1)
    hold on
    surfc(xx, yy,- f)
    shading interp
    xlabel('x-value')
    ylabel('y-value')
    plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',4,'markerfacecolor','r')
    title('Bounded inputs')
    subplot(2,1,2)
    shading interp
    plot(-fopt)
    xlabel('Iterations')
    ylabel('Function maximum')
    colorbar

%Study 3 - Increasing GA iterations
tic
for i=1:num_cases
    options = optimoptions('ga');
    options = optimoptions(options,'populationsize',350);
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_time = toc


    figure(4)
    subplot(2,1,1)
    hold on
    surfc(xx, yy, -f)
    shading interp
    xlabel('x-value')
    ylabel('y-value')
    plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',4,'markerfacecolor','r')
    title('Bounded inputs')
    subplot(2,1,2)
    shading interp
    plot(-fopt)
    xlabel('Iterations')
    ylabel('Function maximum')
    colorbar
    fprintf(' The Function value')
    disp(-fopt(num_cases));

EXPLANATION FOR CALCULATING A GLOBAL MAXIMA PROGRAM ::

Step 1 : Give the range of x and y value in 'linspace' command to get a value in a matrix.

Step 2 : Use the 'meshgrid' command to get a matrix value of xx and yy.

Step 3 : Use 'for loop' to finding the stalagmite function value for x and y values.

Step 4 : In for loop to calling the 'stalagmite function program' for finding the f value (use -ve symbol to find the global maxima).

Step 5 : After finding the function value (f value), use 'surfc' to plot the values.

Step 6 : Use 'shading interp' to hide the lines in the stalagmite plot.

Step 7 : Use ga to find the global maxima & minima,local maxima & minima in the stalagmite function.In MATLAB ga will give only minima value.

[inputs,fval] = ga(@stalagmite,nvars);

Step 8 : Give 'num_case' value to run the number of ga iterations .

Step 9 : In Study 1 - Statistical Behaviour we get 'Unbounded inputs' using the ga syntax of x = ga(fun,nvars); and finding the xopt,yopt values using 'for loop'.

Step 10 : Use 'subplot' command to get two plots in a single figure(1) window.

Step 11 : Use 'plot3' command to plot three values as (xopt,yopt,-fopt) in a single plot.

Step 12 : Use another 'subplot' command to get another plot in a above fingure(1) window.

Step 13 : Use 'plot' command to plot the (-fopt) value in the figure(1) window.

Step 14 : Use 'xlabel','ylabel' command to display the name of x-value and y-value.

Step 15 : Use 'tic' and 'toc' command to calculate the time of getting output of Study 1.

Step 16 : In Study 2 - Statistical Behaviour - with upper and lower bounds we get the 'Bounded Inputs' using the ga syntax of x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub); and finding the xopt,yopt values using 'for loop'.

Step 17 : Use 'subplot', 'plot3', 'plot', 'xlabel', 'ylabel', 'tic' and 'toc' commands (similar to study 1 - statistical behaviour) and get the output of study 2.

Step 18 : In Study 3 - Increasing GA Iterations we get the 'Global Maxima' using the ga syntax of x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,Intcon,options); and finding the xopt, yopt values using 'for loop'.

Step 19 : Use 'subplot', 'plot3', 'plot', 'xlabel', 'ylabel', 'tic' and 'toc' commands (similar to study 1 - statistical behaviour & Study 2 - Statistical Behaviour - with upper and lower bounds) and get the output of Study 3.

Step 20 : Use 'fprintf' command to get the function value and the function value must be in positive(+ve), because we are finding the global maxima value.

Step 21 : Finally Press 'Run' button to run the program and getting the output of Global Maxima.

ERROR AND CORRECTIONS :

EXPLANATION : In the above window, error is showing in the line "f(i,j) = stalagmite(input);" this line is used for calling a stalagmite function program, so give the input parameters which is already assigned in the program. In this Program assigned input parameters are

input_vector(1) = xx(i,j);

input_vector(2) = yy(i,j);

Use "f(i,j) = stalagmite(input_vector);" for calling a function. By using "input_vector" instead of "input" error has been cleared.

OUTPUT FOR CALCULATING A GLOBAL MAXIMA OF A STALAGMITE FUNCTION USING GENETIC ALGORITHM :

FIGURE 1 : Considering Input Range

FIGURE 2 : Study 1 - Statistical Behaviour

Study_time = 3.6460

FIGURE 3 : Study 2 - Statistical Behaviour - with upper and lower bounds

Study_time = 5.5116

FIGURE 4 : Study 3 - Increasing GA Iterations by giving population size

Study_time = 29.5020

FIGURE 5 : Function value of a Global Maxima

CONCLUSION :

Global Maxima of a stalagmite function is calculated by using GA - Genetic Algorithm. In MATLAB GA will always give minimum value, but for finding maxima value we want to inverse the function and we will get the Global Maxima of a stalagmite function.

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.