Week 4 - Genetic Algorithm
AIM : To learn the concept of the GENETIC ALGORITHM and find out the global maxima of the stalagmitefunction.
THEORY: Gentic algorithm based on the darwin's theory of the evalutinory biology.
Genetic algorithms is used to solve the non-linear and non-differential optimization technique.It is used for both the constraint and unconstraint problem. Genetic algorithm use the diifferent point available for the objective function and optimize them , the process of optimization continue till the best solution is obtain. For the optimization of the function evry time genetic algorithm define the best set of the point and run them again and again and converse the set of the point to the best possible result .
fig.1 Process of the genetic algorithm
1. Initialization: Initialization means that first of all define the define the constrain or the set of data. for which the function is going to optimise .
2.Fitness Function: Fitness function is the mathematical equation which is define for the set of data . for the specific equation we have to find out the optimise value.If the fitness function change the optimise value also get change.
3.SELECTION:Selection ,eams that we have to select the parent to produce the off-springs . If we generalise the statement then it means that selection of the function to produce the more localised datafor the optimisation .
4. REPRODUCTION: In this step data are produce and it is stired for the next optimisation cycle.
SYNTAX OF GA IN MATLAB:
x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)
Description
function f=stalagmite(inputs)
x=inputs(1);
y=inputs(2);
f_1x=(sin(5.1*pi*x+0.5))^6;
f_1y=(sin(5.1*pi*y+0.5))^6;
f_2x=exp((-4.*log(2).*((x-0.0667)^2))/0.64);
f_2y=exp((-4.*log(2).*((y-0.0667)^2))/0.64);
f=-f_1x.*f_1y.*f_2x.*f_2y;
% (-)for the maximisation of the stalagmite function
end
clear all
close all
clc
% input
x=linspace(0,.6,150);
y=linspace(0,.6,150);
% create 2D array
[xx,yy]=meshgrid(x,y);
% input value for the calculation of the maximun function
for i=1:length(xx)
for j=1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
f(i,j)=stalagmite(inputs);
end
end
% no. of iteration
cases=50;
for i=1:cases
[value fopt(i)]=ga(@stalagmite,2);
xopt=inputs(1);
yopt=inputs(2);
end
% study 1
% time to run the code
figure(1)
subplot(10,5,5)
hold on
surf(x,y,-f)
shading interp
colorbar
plot(xopt,yopt,-fopt,'marker','o','markerfacecolor','g','markersize',5)
subplot(2,1,2)
plot(-fopt)
xlabel('no.of iteration')
ylabel('maximun value')
% study 2
for j=1:cases
[inputs,fopt(j)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(j)=inputs(1);
yopt(j)=inputs(2);
end
% plotting data
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt,'marker','o','markerfacecolor','r','markersize',5)
subplot(2,1,1)
plot(-fopt)