Implementation of Genetic Algorithm in MATLAB Programming
Aim:- To calculate global maxima of a stalagmite function by MATLAB programming
Governing Equations:- Equations those play the main role in this project:
Basically in this project stalagmite function is used to implement the equations.
f(x,y)=f1x*f1y*f2x*f2y
f1x=(sin(5.1`pi`x +0.5))^6
`f2x=exp(-4ln2(x-0.0667)^2/0.64)`
f1y=(sin(5.1`pi`y+0.5))^6
`f2y=exp(-4ln2(y-0.0667)^2/0.64)`
Objective:- The main objective of this project is to implement the genetic algorithm and solve it by calculating the global maxima of a stalagmite function
Brief theory:-
Genetic Algorithm(GA):-
The concept of genetic algorithm is taken from natural genetics theory.ga is based on the principle of natural genetics mechanics and it is a revolutionary algorithm. It is a method to solve constrained and unconstrained optimization problems of natural selection. The genetic algorithm is used for the robust search of complex spaces.
Stalagmite Function:- Basically stalagmite is a rock rising from the cave floor, a stalagmite function is used to make graph plots that look like natural stalagmite and the program is used to get global maxima.
Global Maxima:- Here the largest value of the stalagmite function is called global maxima.
Workflow:-
- The population is initialized.
- The 2-dimensional mesh is created.
- Stalagmite function is evaluated.
- The function is written at last.
- Plotting is completed.
Brief content of this project:-
This project has been done with MATLAB programming. The code and function have been attached below.
Program code:-
%Solution of Genetic Algorithm by MATLAB programming
clear all
close all
clc
%finding maxima using Genetic Algorithm
%taking inputs
x=linspace(0,0.6,150)
y=linspace(0,0.6,150);
%creation of 2d mesh
[XX YY] = meshgrid(x,y);
%using stalagmite function
for i=1:length (XX)
for j=1:length(YY)
vector1=XX(i,j);
vector2=YY(i,j);
func(i,j)=stalagmite(vector);
end
end
figure(12)
surfc(XX,YY,-func)
xlabel('x value')
ylabel('y value')
shading interp
[input fval]=ga(@stalagmite,2);
%Statistical behaviour (study1)
caseno=50
tic
for i=1:caseno
[input,fopt(i)]= ga(@stalagmite,2);
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
study_time1=toc
figure(1)
subplot(2,1,1);
hold on
surfc(x,y,func)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize','5','markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
%statistical behaviour (upper and lower bound) study2
for i=1:caseno
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time2=toc
figure(2)
subplot (2,1,1)
hold on
surfc(x,y,func)
shading interp
plot3(xopt,yopt,fopt,'marker','o','msrkersize','5','color','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
%ga iteration increment study3
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',270);
for i=1:caseno
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
study_time2=toc
figure(3)
subplot (2,1,1)
hold on
surfc(x,y,func)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize','5','markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
Function Code:-
function[func]=stalagmite(vector);
a=vector1
b=vector2
f1x=[sin(5.1*pi*a+0.5)]^6
f1y=[sin(5.1*pi*b+0.5)]^6
f2x=exp(-4*log(2)*(((a-0.0667)^2)/(0.64)));
f2y=exp(-4*log(2)*(((b-0.0667)^2)/(0.64)));
func=-(f1x*f1y*f2x*f2y)
end
Plots:-
Figure1: Generated 3d surface (random sampling)
Figure2: Generated 3d plot(unbounded inputs)
Figure3: Generated plot (upper and lower bound)
Figure4: Generated 3d plot (increased population size)
Algorithm of the program:-
1)Firstly, the screen is cleared
2)The linspace command has been used to set a data range between two numbers.
3)The array has been created by the meshgrid command.
4)For loop has been created and the vectors store the XX and YY matrix values.
5)The surface plot has been created using surfc command.
6)Axis is given using label and label command
7)Using shading interp command grids have been hidden.
8)Here caseno has been used for iteration of the loop.
9)The calculation of running command time has been done by tic-toc command.
Algorithm for the function:-
1)The function has been named first.
2)vector1 and vector2 have been assigned.
3)f1x.f1y,f2x,f2y formulas have been written.
4) The final formula has been put.
5) The end of the program.
Conclusion:-
Through this whole project, we have learned the method to implement genetic algorithm to plot necessary graphs by Matlab programming. The genetic algorithm is very useful in industrial several works. Finally, our aim for this project through the programming has been fulfilled.