Week 4.1 - Genetic Algorithm
Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function-
Global Maximua Function:
function output=global_maximua(inputs)
x= inputs(1);
y= inputs(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))^8;
f4=exp(-4*log(2)*(((y-0.0667)^2)/0.64));
output=-(f1*f2*f3*f4);
end
Stalagmite code in MATLAB:
close all
clear all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx, yy]=meshgrid(x,y);
num_cases=50;
for i =1:length(xx)
for j=1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
s1(i,j)=global_maximua(inputs);
end
end
i) Study_1
%study_1
for i=1:num_cases
[inp,fopt(i)]=ga(@global_maximua,2)
xopt(i)=inp(1)
yopt(i)=inp(2)
end
figure(1)
subplot(2,1,1)
surfc(x,y,s1)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('study-1')
xlabel('F_maximum')
ylabel('no. of iteration')
subplot(2,1,2)
plot(fopt)
title('study-1')
xlabel('F_maximum')
ylabel('no. of iteration')
legend
ii)Study_2
study_2
for i=1:num_cases
[inp,fopt(i)]=ga(@global_maximua,2,[],[],[],[],[0;0],[0.8,0.8])
xopt(i)=inp(1)
yopt(i)=inp(2)
end
figure(1)
subplot(2,1,1)
surfc(x,y,s1)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
iii) Study_3
%study_3
option=optimoptions('ga');
options=optimoptions(option,'populationsize',170);
for i=1:num_cases
[inp,fopt(i)]=ga(@global_maximua,2,[],[],[],[],[0;0],[0.8,0.8],[],[],options)
xopt(i)=inp(1)
yopt(i)=inp(2)
end
figure(1)
subplot(2,1,1)
surfc(x,y,s1)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
The Concept of Genetic Algorithm:
Genatic Algorithm is used to find optimum solution when "N " number of solution is availiable.GA mainly works on the principle of biologocal reproduction.For example ,if there is N number of chromosome ,by mutation & crossover , it will build up Chromosome that is most fittest among the other."Survival of the fittest ."Similary GA find out optimised solution which is best fit by doing mutation,crossover.
Syntax for GA in MATLAB :
In the above challenge
- "fun" is "global_maximua" function.
- "nvar"=number variables(which is 2;x,y)
- "x"="[inp]"
GA itself take the value of variable(x,y) and find the optimised fopt value.
- "fval"="[fopt]"
value of of function at "[inp]"
ii)[x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
The problem with above syntax is that it gives different value of optimised function value when we run the code again.
So to get more optimised solution,we provide the boundary for varibles(x,y) in the above syntax.
If we do not boundary "GA" do not know the limit of (x,y) so it may give result out of the range of (x,y).
iii)[x,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
"nonlcon"=in the above challenge it is zero.
"options"= to give theany interger value or population size .
Make sure that your code gives the same output, even if it is made to run several times:
After the study_3 same output is coming means optimised value of global maximua is same not changeing.Output_3 will show that same output value is coming.
Note:all the plot is attached.