Genetic Algorithm
Genetic algorithm is an optimization technique which is used to determine the best value of an objective function from the given range of input variables. It is used to find the global maxima or minima of the function.
In Matlab, we get the minimized value of function by default. The 'ga' function is coded in such way.
Working of Genetic Algorithm:
It first selects a random initial population. The algorithm then generates a new set of population. This new set is generated on the basis of:
1) Selecting members from current population which gives higher values of fitness function.
2) They are called parents. Children are produced from parents by performing mutation, crossover.
3) Current poulation is replace by childrens.
This process is carried out until the required fitness value of function is obtained or until the number of generations generated are very high.
Program to carry iterations:
clear all
close all
clc
iterations=linspace(0,100,100);
tic
for i=1:length(iterations)
f(i)=genetic_algorithm();
end
figure(2)
plot(iterations,f)
axis([0 100 -0.5 0.5])
total_time=toc
genetic_algorithm function:
function[fval]=genetic_algorithm()
clear all
close all
clc
%Stalagmite function
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[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
%Genetic algorithm
options=gaoptimset('ga');
options = gaoptimset('PopulationSize',500);
[x,fval,exitflag,output,population,scores]=ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
x
fval
a(1,:)=population(:,1);
b(1,:)=population(:,2);
c(1,:)=scores;
figure(1)
surfc(xx,yy,f)
shading interp
hold on
plot3(a,b,c,'marker','o','markersize',5)
end
stalagmite function:
function[f]=stalagmite(input_vector)
a=(sin((16.02*input_vector(1))+0.5))^6;
b=(sin((16.02*input_vector(2))+0.5))^6;
m=(-4*log(2))/0.64;
c=exp(m*(input_vector(1)-0.0667)^2);
d=exp(m*(input_vector(2)-0.0667)^2);
f=-a*b*c*d;
end
To find the maxima of any function, just replace function f by -f. The obtained value of function will be negative. So the maximum will be the modulus of it.
In this problem, actual answer if function is shown as -1, but the maxima is +1 observed at
Below images are output from the above function. As ypu can see, by increasing initial population size to 500, the results obtained remained constant for 100 iterations.