OPTIMISATION OF STALAGMITE FUNCTION USING GENETIC ALGORITHM IN MATLAB
AIM:
To optimise the stalagmite function and find the global maxima of the function by using genetic algorithm in MATLAB.
OBJECTIVE:
The objective of this report is to optimise the stalagmite function by coding the program with help of genetic algorithm in MATLAB and explain the concept of genetic algorithm and also explain the syntax for 'ga' in MATLAB.
THEORY:
STALAGMITE FUNCTION:
Stalagmite function is a mathematical model that helps to generate maximas and minimas.Global maxima is a maximum peak value in entire function and global minima is a minimum peak value in entire function. But local maxima and local minima are multiple number of values.
Optimisation is finding of maxima and minima in particular function but in genetic algorithm,its only to find minima .
here,we have to optimise the stalagmite function using genetic algorithm.
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.
In MATLAB,genetic algorithm generates a population of points at each iteration. The best point in the population approaches an optimal solution.
Selects the next population by computation which uses random number generators.
Genetic algorithm solves smooth or nonsmooth optimization problems with any types of constraints, including integer constraints. It is a stochastic, population-based algorithm that searches randomly by mutation and crossover among population members.
The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:
-
Selection rules select the individuals, called parents, that contribute to the population at the next generation.
-
Crossover rules combine two parents to form children for the next generation.
-
Mutation rules apply random changes to individual parents to form children.
SYNTAX FOR GA IN MATLAB:
fun |
functions |
nvars |
Number of design variables |
Aineq |
|
Bineq |
|
Aeq |
|
Beq |
|
lb |
Lower bound |
ub |
Upper bound |
nonlcon |
Nonlinear constraint function |
MATHEMATICAL EQUATION FOR STALAGMITE FUNCTION:
[f]=f1x*f1y*f2x*f2y
PROGRAM:
A function program of stalagmite function.
function[f]=stalagmite(input_vector);
x=input_vector(1);
y=input_vector(2);
f1_x=[sin(5.1*pi*x+0.5)]^6;
f1_y=[sin(5.1*pi*y+0.5)]^6;
f2_x=exp(-4*log(2)*(((x-0.667)^2)/(0.64)));
f2_y=exp(-4*log(2)*(((y-0.667)^2)/(0.64)));
f=-(f1_x*f1_y*+f2_x*f2_y);
end
A program to optimise the stalagmite function and finding of the global maxima of a function.
clear all
close all
clc
%defining our search space
x=linspace(0,0.7,200);
y=linspace(0,0.7,200);
%creating a 2D mesh
[xx yy]=meshgrid(x,y);
%evaluating stalagmite fumction
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)
xlabel('xvalue')
ylabel('yvalue')
shading interp
[inputs,fval]=ga(@stalagmite,2);
%study1-statistical behaviour
num_case=70
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2)
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_1=toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function maximum')
%study2_statistical behaviour with upper and lower bounds
tic
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[0.7:0.7]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_2=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function maximum')
%study3_statistical behaviour_increasing the ga iterations
tic
options=optimoptions('ga');
options=optimoptions(options,'populationsize',400)
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.7;0.7],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('bounded inputs with increased population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function maximum')
CODE EXPLANATION:
The above program is written to optimise the stalagmite function and finding of the global maxima of a function.
Taking linspace for bith the axis x and y from 0 to 0.7 for 200 values.
By giving num cases as 70,genetic algorithm runs upto 70 times and calculate study time for 70 iterations.
To compile this program, 3cases studies are carried out.
CASE STUDY 1:
In this case,we calculate the statistical behaviour of stalagmite function by using for loop as ga(@stalagmite) and calculate study time with tic and toc command and then subplot the figure and giving the surfc command to create 3D plot and shading with shading interp command.
To plot multiple set of co_ordinates we use plot3 command with (x,y,f) and titled as unbounded input commands and to further label the plot we use X label as iteration and Y label as function minimum and study time 1 has been calculated.
CASE STUDY 2:
In this case,we calculate the statistical behaviour of stalagmite function by setting up lower and upper bounds,where lb (x1,y1) is (0,0) and ob(x2,y2) is (1,,1).by this change we can notice that there is decrease in study time. Apart from this,same step to be followed as case study1.
CASE STUDY 3:
In this case we calculate the statistical behaviour of increasing the ga iterations. population size has been increased to 400.GA starts with random variable as there as 2 variables,we will be having 70 sets of 2 random variables.as we increase the population, we get global maxima.Apart from this,same step to be followed as case study 1 and 2
ERRORS ENCOUNTERED WHILE PROGRAMMING:
Error has been sorted out and cleared by help of mentioning options in mathwork help desk.
OUTPUT:
PLOT FOR GENERAL STALAGMITE FUNCTION:
PLOT FOR UNBOUNDED INPUTS:
PLOT FOR BOUNDED INPUTS:
PLOT FOR BOUNDED INPUT WITH INCREASED POPULATION SIZE:
WORKSPACE VALUES:
STOPPING CRETERIA:
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.
- The algorithm stops as soon as any one of these conditions is met.
-
The algorithm proceeds until the best solution during the process doesn't change to a better value for a predefined value of num cases and population. i.e. the algorithm reaches to a value of 400 at num cases 70, then this value doesn't changes, so the algorithm stops.
CONCLUSION:
When multiple number of iteration runs,ga becomes optimum or linear.The significance of num cases is to check over consistency of output values of ga whether it gives same output for all iterations,so that it can undergoes optimum value. i.e.,global maxima can be found.
If we provide less iteration and run the program,ga may or may not able to find global maxima.But as no. of iteration increases probability of finding global maxima can also increases.as when population size increases with iteration,probability of global maxima can be optimise.
RESULT:
Thus the global maxima of the stalagmite function is optimised using genetic algorithm in MATLAB.