Week 4.1 - Genetic Algorithm
Genetic Algorithm is one of the machine learning algorithms that is used to get optimized results. It is widely used today in business, scientific and engineering cicrles.
Genetic Operators-
- Selection Operators - Process of selecting two or more parents from the population for crossing. Purpose of selection is to emphasize filter individuals in the population in hope that the offsprings will have higher fitness values. Widely used method is Roulette wheel method. Roulette wheel method - Depending on the percentage contribution to the total population fitness string is selected for mating to form the next generation.
- Crossover Operator - Can be of either one-point or two-point crossover. In on-point crossover, selected pair of string is cut at some random position and these segments are swapped to form new pair of strings. In two-point crossover there will be two break points.
- Mutation - Applied to each child individually after crossover. Bits are changed from 0 to 1 or 1 to 0 at randomly chosen position of randomly selected strings.
Source - https://www.youtube.com/watch?v=svzIUmTcW6w&t=2s
https://www.youtube.com/watch?v=Bf6f-QVcnZY&t=6s
Help taken in code for Case - 3 from Skill lync project to learn about Options function.
%Matlab code for Genetic Algorith Assignment
clear all
close all
clc
%Creating Population
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Converting the 1D matrix into 2D matrix
[xx,yy] = meshgrid(x,y);
%Running the loops to evaluate each value of i with all values of j
for i =1:length(xx)
for j=1:length(yy)
a(1) = xx(i,j);
a(2) = yy(i,j);
%Calling the function which we want to optimize
f(i,j) = stalagmite(a);
end
end
%%
%Case 1 - unbound values
%Creating a loop to get optimized values
for i=1:100
[p, pval(i)] = ga(@stalagmite, 2);
%No of iterations is being stored in pval
%Storing the values in seperate variables for x and y coordinates
newx(i) = p(1);
newy(i) = p(2);
end
%Creating plots for surface which is our Stalagmite function
%and creating lines to show the optimised values we got from GA
figure(1)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
title('Unbounded Values')
hold on
%Used plot3 command to plot lines and coordinates in 3D space,i.e. our surface plot
plot3(newx,newy,pval,'marker','o','markersize',5)
hold off
subplot(2,1,2)
plot(pval)
xlabel('No of Iterations')
ylabel('Value of Stalagmite')
%Giving the output values we received from Stalagmite function
%X represents the x and y coordinates for global minima and fval represents
%the minimum value obtained.
%2 is mentioned as there are two variables in our Stalagmite equation
[x, fval] = ga(@stalagmite, 2)
%%
%Case 2 - Bounded values
for i=1:100
%Here we have taken lower bound and upper bound values to get same values for n no of iterations.
%GA can give multiple outputs and based on the output required we can
%skip others by giving blank []
%Since our x and y axes in graph is in the range of 0 - 0.6 we can give
%the upper and lower bound accordingly
[p, pval(i)] = ga(@stalagmite, 2,[],[],[],[],[0,0],[0.6,0.6]);
newx(i) = p(1);
newy(i) = p(2);
end
figure(2)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
title('Bounded Values')
hold on
plot3(newx,newy,pval,'marker','o','markersize',5)
hold off
subplot(2,1,2)
plot(pval)
xlabel('No of Iterations')
ylabel('Value of Stalagmite')
%In order to get the same output again and again we can limit the search
%within a range to obtain the same minimum value if it the code is run for
%sufficient iteration
[x, fval] = ga(@stalagmite, 2,[],[],[],[],[0,0],[0.6,0.6])
%%
%Case 3 - By increasing the population
options = optimoptions('ga')
options = optimoptions(options,'populationsize',200)
%Options are used to get optimization options which is also a part of
%output from ga function
for i=1:100
%here we are trying to increase the population size and check how the
%value differs with respect to no of iterations when the population
%size is increased
[p, pval(i)] = ga(@stalagmite, 2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
newx(i) = p(1);
newy(i) = p(2);
end
figure(3)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
hold on
plot3(newx,newy,pval,'marker','o','markersize',5)
title('Bounded Values with Increased Population')
hold off
subplot(2,1,2)
plot(pval)
xlabel('No of Iterations')
ylabel('Value of Stalagmite')
[x, fval] = ga(@stalagmite, 2,[],[],[],[],[0,0],[0.6,0.6],[],[],options)
%Function for Stalagmite
function [output] = stalagmite(x)
a = (sin((5.1*pi.*x(1))+0.5)).^6;
b = (sin((5.1*pi.*x(2))+0.5)).^6;
c = exp((-4*log(2)).*(((x(1)-0.0667).^2)/0.64));
d = exp((-4*log(2)).*(((x(2)-0.0667).^2)/0.64));
output = -1*(a.*b.*c.*d);
end