To Optimize the stalagmite function and find the global maxima of the function using Genetic Algorithm
GENETIC ALGORITHM(GA)
“It is a search base optimization technique based on Genetics and Natural selection, It is used to find the optimal value to difficult problem which otherwise would take life time to solve ’’
Natural Selection- Natural Selection is a mechanism of evolution,Organism that are more adapted to their environment are more likely to survive and pass on the genes that added their sucess.
Optimization-Optimization refer to finding the value of input in such a way that we get the best output values.
Fig-Optimization
BASIC STRUCTURE OF GENETIC ALGORITHIM
Selection of the fittest- The use of the solutions with high fitness to pass on to next generations, which is often carried out in terms of some form of selection of the best solutions.
Crossover- Swaping parts of the solution with another in chromosomes or solution representations. The main role is to provide mixing of the solutions and convergence in a subspace.
Mutation- The change of parts of one solution randomly, which increases the diversity of the population and provides a mechanism for escaping from a local optimum.
Governing Equation
Stalagmite Function:`f(x,y)=f1x,f1y,f2x,f2y`
where
`f1x=[sin(5.1pix+0.5)]^6`
`f1y=[sin(5.1piy+0.5)]^6`
`f2x=exp[-4ln(2)(x-0.0667)^2/(0.64)]`
`f2y=exp[-4ln(2)(y-0.0667)^2/(0.64)]`
Stalagmite Function
function f=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1x=(sin(5.1.*pi.*x+0.5))^6;
f1y=(sin(5.1.*pi.*y+0.5))^6;
f2x=exp(-4*log(2).*((x-0.0677)^2)/0.64);
f2y=exp(-4*log(2).*((y-0.0677)^2)/0.64);
f=-(f1x*f1y*f2x*f2y);
end
Main Program 1
close all
clear all
clc
%Creating Search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%Number of time GA will run
num_case=50;
%converting one dimensional array into 2
[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
%Study 1 for unbounded inputs
tic
for i=1:num_case
[inputs, fval(i)]= ga(@stalagmite,2);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
time1=toc
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
shading interp
plot3(xopt,yopt,-fval,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
hold on
xlabel('Iterations')
ylabel('Maximum')
Steps for Programing
Step 1-Create a search space using “linspace” command
Step 2-Use “meshgrid” command for two dimensional array
Step 3-Use “for loop” to insert Inputs to the Stalagmite function
Step 4-Then run the ga and find the minimum value
Step 5-plot the inputs and f using “surfc” command
Step 6- use “plot 3” command to plot optimal value(xopt,yopt,fval)
Step 7-plot fval against Iterations
Step 8- Use ‘tic-toc’ command
Program 2 (STUDY -2)
%Study 1 for bounded inputs
tic
for i=1:num_case
[inputs, fval(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
time1=toc;
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
shading interp
plot3(xopt,yopt,-fval,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fval)
hold on
xlabel('Iterations')
ylabel('Maximum')
Explation
Steps are same like program 1 but the syntax of the ga is changed
Ga syntax - x=ga(func,nvar,A,b,Aeq,beq,lb,ub)
Func-function
Nvar-Dimension or variables
A,b- subjected of linear inequalities A*x<=b
Lb & Ub - upper & lower bonds
So to bond our inputs we have use
[X,fval]=ga(@Stalagmite,2,[],[],[],[],[0;0],[1;1])
Program 3(Study-3)
%Study 1 for bounded inputs and population size
options=optimoptions('ga')
options=optimoptions(options,'populationSize',600);
for i=1:num_case
[inputs, fval(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1,1],[],[],options);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
time1=toc;
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
shading interp
plot3(xopt,yopt,-fval,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with poputation size')
subplot(2,1,2)
plot(-fval)
hold on
xlabel('Iterations')
ylabel('Maximum')
Explation of the code
Syntax used
[x,fval]=ga(@salagmite,2,[],[],[],[],[0;0],[1;1],[],[],option)
Create option using optimoptions
options=optimoptions('ga')
options=optimoptions(options,'populationSize',600);
Results
1.Study 1
2.Study 2
3.Study-3
Conclusion
Sucessfull optimise the stalagmite function and found the gloval maxima of function.
Refrences
https://www.sciencedirect.com/topics/computer-science/cross-over-mutation
https://in.mathworks.com/help/matlab/ref/subplot.html#btw1t4b-2_1