Week 4 - Genetic Algorithm
Aim: To write a program in matlab to optimize the stalagmite function and find the global maxima of the function.
OBJECTIVES:
1) Write a matlab code to find the global maxima.
2) Explain the concept of genetic algorithm and the syntax for it.
INTRODUCTION:
Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover and selection. They’re often used in engineering (Jet Propulsion Lab, in particular) to optimize part design and other design aspects.
Genetic algorithms vary in their structure based on their purpose, but all of them share a few common components. The algorithm begins by initializing a population of individuals using default or random values. Then, it runs each member of that population through a fitness function. It selects the fittest members of the population to reproduce using a method defined in the reproduction function, and then repeats the evaluation and reproduction until a desired number of iterations have passed. At termination, the algorithm presents the best member or members of the population according to the fitness function.
GA SYNTAX:
To use the genetic algorithm , call the genetic algorithm function ga with the syntax
[x fval] = ga(@fitnessfun, nvars)
Where,
@fitnessfun is a handle to the fitness function.
nvars is the number of independent variables for the fitness function.
The results are given by,
x — Point at which the final value is attained
fval — Final value of the fitness function
EQUATIONS:
Equations that are used in the stalagmite functions are:
`f(x,y)=f1*f2*f3*f4`
`f_(1,x)=[sin(5.1.π.x+0.5)]^6`
`f_(1,y)=[sin(5.1.π.y+0.5)]^6`
`f_(2,x)=exp[-4.log(2).(x-0.0667)^2/0.64]`
`f_(2,y)=exp[-4.log(2).(y-0.0667)^2/0.64]`
BODY OF THE CONTENT:
Code for getting the function stalagmite(inputs):
function [F]=stalagmite(inputs)
x=inputs(1);
y=inputs(2);
term1=(sin(5.1*x*pi+0.5))^6;
term2=(sin(5.1*y*pi+0.5))^6;
term3=exp(-4*log(2)*((x-0.0667)^2/(0.64)));
term4=exp(-4*log(2)*((y-0.0667)^2/(0.64)));
F=-term1*term2*term3*term4;
end
Explanation of code:
In this to calculate the stalagmite function the formula is written, the formula is broken down in order to make the calculations much easier. There is negative sign added for the last two terms so as to get the global maxima instead of global minima.
Main code:
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2d mesh
[xx yy]= meshgrid(x,y);
%study statistical behaviour
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
tic
num_cases=50
%%study1 statistical behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbonded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%study 2 statistical behaviour with upper and lower bounds
tic
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0 0],[1 1]);
xopt(j)=inputs(1);
yopts(j)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%study 3 increasing Ga iterations
opti=gaoptimset('populationsize',400);
tic
for m=1:num_cases
[inputs,fopt(m)]=ga(@stalagmite,2,[],[],[],[],[0 0],[1 1],[],[],opti);
xopt(m)=inputs(1);
yopt(m)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markerfacecolor','r')
title('bounded inputs with increased population')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
Explanation of code:
- Initially we start with defining the inputs and then creating a 2d mesh of the inputs.
- Then we use a for loop to get the different input values and use them in the function stalagmite and calculate it.
- Now we have three different studies that we are going to use in the code
- In the first study we are specifying the number of iterations that the algorithm has to run.
- Then we use a for loop in which ‘ga’ function is used ,in ‘ga’ the stalagmite function and the number of variables (i.e 2) are being used .Then the output is being stored in xopt and yopt.
- The for loop has a timer which gives the time for getting the output.
- After getting the outputs the plotting is being done, in this figure (1) is used to create first figure then a subplot is defined to divide the workspace for multiple plots.
- Then further surfc( ) command is used to plot the surface of the stalagmite function where ‘ negative f ‘ is used to get maximum output.
- Then the ‘shading interp’ is used to get a smooth finish for the output. Shading interp sets the shading to interpolated. Interpolated shading, which is also known as Gouraud shading, is piecewise bilinear; the color in each segment or patch varies linearly and interpolates the end or corner values.
- Then the hold on command is used so that there is no overwriting of the old plot and we are able to see multiple plots and then the plot3 command is used to create a plot using the co-ordinates.
- Then again subplot is used and then the plot command is used to create plot for ‘-fopt ’
- Then in the second study we define the lower and upper bounds. Since ‘ga’ takes any random value and so as to limit it we define the bounds.
- Then the same process is followed as in study 1 and the plot is created.
- Then in third study we increase the population size using 'gaoptimset' so as to get better results. So we increase the size upto a value that gives acceptable result.
- Then after increasing the value we follow the same steps as in the above studies and create the plots.
ERRORS:
- The above image shows the error that i was constantly getting, So to overcome that I used gaoptimset ( ) instead of optimoptions( ) .
- After using the gaoptimset ( ) I was able to get the output.
- Apart from this there were small errors like typing mistake and I corrected it after I received the error message in the output.
OUTPUTS:
- Study 1:
In this study the ga function is called normally with given number of iterations.
In the above figure2 we can see that with change in iterations the value of maximum function changes but we are not able to get a clear indication of the maximum point
So due to unbounded inputs we can see the surface figure with so many points that is making it difficult to figure the maximum point. So we to try bound the area.
- Study 2
In this we have used lower and upper bounds to get better view of the plot
So after using the upper and lower bounds we may see that there is improvement in the plot. The first figure looks better than before and there are points that we can see which shows global maxima but we cannot confidently give the global maxima. So we try increasing the number of population size to optimize the results.
- Study 3
In this we have increased the population size and also used the upper and lower bounds.
So from the above figures we can see that we have got accurate global maxima .Though this study takes more time for completion, it gives the most accurate results.
- The study time for the above studies is as given below:
From the above three study time we can see that the study 3 is taking more time with respect to the other two studys.Even though it is taking more time we are able to get the global maxima precisely.
CONCLUSION:
So we were able to get the global maxima of the function, the three studies helped to get better and improved results and we also found that by increasing the population size we were able to get more precise results.
REFERENCES:
https://itectec.com/matlab/matlab-ga-code-problem-in-using-options/
https://www.mathworks.com/matlabcentral/answers/52240-how-to-find-max-fuction-with-genetic-algorithm