Week 4.1 - Genetic Algorithm
EPLANATION OF THE GENETIC ALGORITHM (GA)
The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions.
In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization
In a genetic algorithm, a population of candidate solutions to an optimization problem is evolved toward better solutions.The evolution usually starts from a population of randomly generated individuals, and is an iterative process, with the population in each iteration called a generation.
EXPLANATION OF THE CODE
% Defining our search space
x,y are one dimensional arrays
x = linspace(0,0.6,150);. here linspace is defind as The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points.
y = linspace(0,0.6,150);. linspace used to divided the 150 values between the 0 to 0.6.
% creating a 2Dimensional mesh
xx,yy are two dimensional arrays
[xx yy] = meshgrid(x,y);. meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing
% Evaluating the stalagmite function
for i = 1:length(xx). A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.. i is 1 to the length of xx.
for j = 1:length(yy). j is 1 to the length of yy.
input vector(1) = xx(i,j); for each x and y we are creating a array called 'input vector', input vector is a row vector it as two colunms
input vector(2) = yy(i,j);
f(i,j) = stalagmite(input_vector); stalagmite is a function separatly done by the formula
(function [f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x = (sin(5.1*pi*x + 0.5)).^6;
f1_y = (sin(5.1*pi*y + 0.5)).^6;
t1 = ((x-0.0667).^2)/(0.64);
f2_x = exp(-4.*log(2)*t1);
t2 = ((y-0.0667).^2)/(0.64);
f2_y = exp(-4.*log(2)*t2);
f = -(f1_x.*f2_x.*f1_y.*f2_y);
end) the above function is done using the stalagmite formula
end
end
surfc(xx,yy,f). surfc is used to visualize the function
xlabel('x value')
ylabel('y value')
shading interp. it is used to shade the plot
[inputs, fval] = ga(@stalagmite, 2). ga is inbuilt function in matlab. ga is used for finding the minimum value