Week 4.1 - Genetic Algorithm
GENETIC ALGORITHM
a genetic algorithm is a natural selection that belongs to the larger class of evolutionary algorithms. Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems. Genetic algorithms simulate the process of natural selection which means those species who can adapt to changes in their environment are able to survive and reproduce and go to next generation
examples
optimizing decision trees for better performance
solving sudoku puzzles
hyperparameter optimization
clear all
close all
clc
% Inputs
x =linspace (0,0.6,150);
y =linspace (0,0.6,150);
num_cases = 60
% Storing in 2D array
[xx,yy]=meshgrid(x,y);
% calling the function
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) = gafunctioin(input_vector);
end
end
num_case = 50
%Study 1 behaviour
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@func,2);
xoption(i) = inputs(1);
yoption(i)=inputs(2);
end
studytimel=tic
% Unbounded inputs
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xoption, yoption, -fopt, 'Marker', 'o', 'Markersize',5, 'Markerfacecolor', 'r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%Study 2 behaviour
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@func,2,[],[],[],[],[0,0],[0.5,0.5]);
xoption(i)= inputs(1);
yoption(i)= inputs(2);
end
studytime2 tic
% Bounded inputs:
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt, yopt, -fopt, 'Marker', 'o', 'Markersize',5, 'Markerfacecolor', 'r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%Study 3 bheaviour
options=optimoptions('ga');
options=optimoptions (options,'Population size',200);
tic
for i=1:num cases
[inputs, fopt(i)]=ga(@func,2,[],[],[],[],[0;0],[0.5;0.5],[],[], options);
MAIN PROGRAMME
xoption(i) = inputs(1);
yoption(i)=inputs(2);
end
studytime3= toc
Optimization
figure(3)
subplot (2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xoption,yoption,-fopt, 'Marker', 'o', 'Markersize',5, 'Markerfacecolor', 'r')
title('Optimization')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
FUNCTION PROGRAMME
function [f] = gafunctioin(input_vector)
x = input_vector(1);
y = input_vector(2);
a = (sin(5.1*pi*x+ 0.5))^6;
b = (sin(5.1*pi*y + 0.5))^6
c = exp(-4*log (2)*((x-0.0667)^2/(0.64)))
d = exp(-4*log (2)*((y-0.0667)^2/(0.64)))
e = -(a*b*c*d);
endfunction
EXPLANATION
The code is used to get the value of the function at different grid points on the x-y plane which will be plotted in the z-direction. we have multiplied the -ve sign to convert the function into maxima since the genetic algorithm function can only calcualte the minima value for a function.
GA Syntax
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 nonicon,IntCon,options)
x = ga(problem)
errors are mainly occured due to spelling mistakes and forget to use paranthesis