Calculate the global maxima of a stalagmite function using Matlab
OBJECTIVE:
Write code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
WHAT IS GENETIC ALGORITHM?
GA was proposed by CHALS DARWIN
in Matlab it\'s an inbuilt function
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation.
You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms
Such as discontinuous, nondifferentiable, stochastic, or highly nonlinear
WHY WE NEED TO USE GENETIC ALGORITHM :
Optimization is required in ic engine in aircraft mig airfoil for a proper lift at a different angle of attack and to minimize drag
Different flight condition required different drag value these should be minimum
It\'s a lengthy process
OPTIMIZATION: area of study of minimizing and maximize of function depends on input parameters
STALAGMITE FUNCTION: It is a mathematical model which helps us generate local minima and maxima
it gives a 3d model by which we can get the maxima and minima
To calculate the values of [x,y] and minimum value of the function as GA calculates the minimum values
we have a formula we need to convert it into a function
as stalagmite function
function akashmhaske = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1 = (sin(5.1*pi*x + 0.5)).^6;
f2 = exp(-4*log(2)*((x-0.0667).^2/(0.64)));
f3 = (sin(5.1*pi*y + 0.5)).^6;
f4 = exp(-4*log(2)*((y-0.0667).^2/(0.64)));
akashmhaske = (f1*f2*f3*f4);
end
as by staring the program by
clear all
close all
clc
define search space, we have defined values of \'x\' from 0 to 0.6 in a range of 150 values same for \'y\'
Mesh grid command used to create a gride of these values means 150*150 dimension
xx =150*150
yy =150*150
for calling the stalagmite function we need to use
FOR loop to define the values in a range of [xx] and [yy]
so that the loop should run for each value
as i = 1 : length(xx)
j =1:length(yy)
calling the function we get values of \'f\' in a 150*150 dimension
plot the graph of (xx, yy,f) ie inputs of xx, yy and function we got
we get a global maximum and local maximum plot but we only get the value of the minimum
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150)
% creating a 2dimension mesh
[xx, yy] = meshgrid(x, y);
% evaluating the stalagmite 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)= stalagmite(input_vector);
end
end
surfc(xx,yy,f)
shading interp
xlabel(\'x value\')
ylabel(\'y value\')
[inputs,fval] = ga(@stalagmite,2)
MAIN PROGRAM FOR CALCULATING THE GA BUT ONLY GET MINIMUM VALUES
FIG.2 MESH GRID VALUES
FIG.3 GLOBAL AND LOCAL MAXIMA PLOT WITHOUT VALUES
FIG.4 VALUES OF INPUT VARIABLE AND FUNCTION VALUE MINIUM
TO GET THE GLOBAL MAXIMA WE NEED TO SOLVE GA BY CHECKING THREE CONDITIONS
Using the same function but this time minus sign when we multiply the f1,f2,f3,f4
define the range of x and y as GA takes random values and its a constrain the values of x and y to exceed beyond there range as Matlab doesn\'t understand it
function akashmhaske =get_stalagmite (xx,yy)
f1 = (sin(5.1*pi.*xx + 0.5)).^6;
f2 = exp(-4*log(2)*((xx-0.0667).^2/(0.64)));
f3 = (sin(5.1*pi.*yy + 0.5)).^6;
f4 = exp(-4*log(2)*((yy-0.0667).^2/(0.64)));
akashmhaske = -(f1.*f2.*f3.*f4);
end
giving num_cases = 50 means that the genetic algorithm will run for 50 times to give results
calling the function using get_stalagmite(xx,yy)
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150)
% creating a 2dimension mesh
[xx, yy] = meshgrid(x, y);
num_cases = 50
f = get_stalagmite(xx,yy);
% STUDY 1 - SATISTICAL BEHAVIOR
tic
for i =1 : num_cases
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time1 = toc
figure(01)
subplot(2,1,1)
surfc(-xx,-yy,-f)
hold on
shading interp
plot3(-xopt,-yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'UNBOUNDED INPUTS\')
subplot(2 ,1 ,2)
plot(-fopt)
tic
% STUDY 2 - statisticaal behaviour - with upper and lower bonds
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_time2 = toc
figure(02)
subplot(2,1,1)
surfc(-xx,-yy,-f)
hold on
shading interp
plot3(-xopt,-yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'BOUNDED INPUTS\')
subplot(2 ,1 ,2)
plot(-fopt)
% STUDY 3 increasing GA iterations
options = optimoptions(\'ga\');
options = optimoptions(options,\'Populationsize\',370);
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time3 = toc
figure(03)
subplot(2,1,1)
surfc(-xx,-yy,-f)
hold on
shading interp
plot3(-xopt,-yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs increase the population size\')
subplot(2 ,1 ,2)
plot(-fopt)
FIRST STUDY:
- We define FOR loop for a range of num_cases and get output for x and y
- By getting the output we can compare them with the function and plotting them in a subplot
- Using of shading interp so that we do not get lines over the graph and we get a smooth plot of GA
- Calculate the time for how many secs the operation was carried out by using tic toc command
FIG.5 FIRST CASE PLOT OF DIFFERENT RANGE VALUES OF X, Y, FUNCTION
In the first study, we can see the maximum of the values is not been properly define we are unable to find it
lots of local maxima are been defined so to this plot is not proper we need to try another method
global maxima lie between the range of 0 to 1 but we can not see as there are lots of variation in subplot 2
SECOND STUDY:
- We define FOR loop for a range of num_cases and get output for x and y
- now we are using GA in and fixing the range of x and y like 0 to 1
- And the empty brackets for them to read the LINEAR EQUALITY AND LINER INEQUALITY
- LINEAR EQUALITY = less than equal to values
- LINER INEQUALITY = equal values
- By getting the output we can compare them with the function and plotting them in a subplot
- Using of shading interp so that we do not get lines over the graph and we get a smooth plot of GA
- Calculate the time for how many secs the operation was carried out by using tic toc command
- this takes much run time as compared to the first study
- values we get are close to the global maxima
- this problem can be solved using increasing the number of iteration
FIG.6 SECOND CASE PLOT OF DIFFERENT RANGE VALUES OF X, Y, FUNCTION
RESULT ARE IMPROVED
THIRD STUDY:
- We define the range of the population to 370 (population size )
- As the size is more the GA will take much time to give the result as compared to first and second
- We define FOR loop for a range of num_cases and get output for x and y
- Now we are using GA in and fixing the range of x and y like 0 to 1
- ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
- The first four empty brackets for them to read the LINEAR EQUALITY AND LINER INEQUALITY
- LINEAR EQUALITY = less than equal to values
- LINER INEQUALITY = equal values
- [0;0],[1;1] is the range of x and y should be fixed
- another [],[] are for the linear equation with a degree and second bracket is for integers
- It takes thrice the time of the first case
- By getting the output we can compare them with the function and plotting them in a subplot
- Using of shading interp so that we do not get lines over the graph and we get a smooth plot of GA
- Calculate the time for how many secs the operation was carried out by using tic toc command
- This takes much run time as compared to the first study
- Values we get are close to the global maxima
- This problem can be solved using increasing the number of iteration
- We get the global maxima by this and time in which is finished is study time 112 sec
FIG.7 FIRST CASE PLOT OF DIFFERENT RANGE VALUES OF X, Y, FUNCTION
GOT THE GLOBAL MAXIMA NEGLECTING THE LOCAL MAXIMA
FIG.9 STUDY TIME OF OPERATION
CONCLUSION :
- The first case required less time but does not give an accurate result
- second also do the same
- As no of iteration is increased the GA shows the accurate value
- But we need to fix the range under which the input and output be defined