Week 4.1 - Genetic Algorithm
* Genetic Algorithm(GA):
GA is an search based optimization technique based on the principles of genetics and natural selection, it is frequently used to find optimal or near optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research and in machine learning.
* Stopping Criteria:
Stopping criteria determine what causes the algorithm to terminate when needed.
You can use some of the following options:
1) FitnessLimit: The algorithm stops if the best fitness value is less than or equal to th value of FitnessLimit. Does not apply to gamultiobj.
2) MaxGenerations: Specifies the maximum number of iterations for the genetic algorithm to perform. The default is 100*numberofVariables.
* Syntax for ga:
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,noncon,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(___);
* Stalagmite Function:
function [F] =stalagmitefunction(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.0667)^2)/0.64);
f2y=exp((-4*log(2)*(y-0.0667)^2)/0.64);
F= -(f1x*f1y*f2x*f2y);
end
Main Program:
% Start of the Program
% Program to optimise stalagmite function.
clear all
close all
clc
x=linspace(0,1,100);
y=linspace(0,1,100);
[xx,yy]=meshgrid(x,y);
for i=1:length(xx)
for j=1:length(yy)
inputs(1)=xx(i,j);
inputs(2)=yy(i,j);
F(i,j)=stalagmitefunction(inputs);
end
end
num=100;
tic
%case1
for i=1:num
[inputs,Fopt(i)]=ga(@stalagmitefunction,2);
xout(i)=inputs(1);
yout(i)=inputs(2);
end
casel_time=toc
figure(1)
subplot(2,1,1)
surfc(xx,yy,-F)
shading interp
hold on
plot3(xout,yout,-Fopt,'marker','*','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('Iterations')
ylabel('Function Maximum')
tic
%case2:
for i=1:num
[inputs,Fopt(i)]=ga(@stalagmitefunction,2,[],[],[],[],[0;0],[0.6;0.6],[],[]);
xout(i)=inputs(1);
yout(i)=inputs(2);
end
case2_time=toc;
figure(2)
subplot(2,1,1)
surfc(x,y,-F)
shading interp
hold on
plot3(xout,yout,-Fopt,'marker','*','markersize',5,'markerfacecolor','g')
title('Bounded Inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('Iterations')
ylabel('Function Maximum')
tic
%case3: Increasing Iterations
options=optimoptions('ga');
options=optimoptions(options,'populationsize',300);
for i=1:num
[inputs,Fopt(i)]=ga(@stalagmitefunction,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xout(i)=inputs(1);
yout(i)=inputs(2);
end
case3_time=toc;
figure(3)
subplot(2,1,1)
surfc(x,y,-F)
shading interp
hold on
plot3(xout,yout,-Fopt,'marker','*','markersize',5,'markerfacecolor','b')
title('Bounded Inputs with Increased Polulation size')
subplot(2,1,2)
plot(-Fopt)
xlabel('Iterations')
ylabel('Function Maximum')
% End pf program
Explanation:
1) Start the coding with 'clear all', 'close all' and 'clc' commands.
2) Create a X and Y vector of 100 valuesb/w 0 to 1.
3) use meshgrid to create a 2D vector.
4) Give the inputs for statistical behaviour.
5) Give the inputs for statistical behaviour with upper bound and lower bound.
6) Give the inputs for increasing population.
7) Give proper inputs for figure1, figure2 and figure3 repectively.