Week 4 - Genetic Algorithm
AIM:
To achieve the stalagmite function and to find the global maxima of the given function, using MATLAB SOFTWARE.
Objectives:
*Elaborating the GENETIC ALGORITHM concept, and explain the syntax for GENETIC ALGORITHM in MATLAB software.
*Plotting graphs for all 3 studies
Fmax vs no.of iterations.
Brief Introduction of GENETIC ALGORITHM:
A genetic Algorithm is a search experimental concept, which was initialized by CHARLES DARWIN theory of natural evolution. This algorithm reproduces the process of natural selection, where the individuals are selectedfor reproduction in order to produce offspring to the next generation.
The genetic algorithm is a concept for solving the both constrained and unconstrained optimization problems that are based on the natural selection.
It modifies the population size of the species.
Stages involved in genetic algorithm:
i) INITIALIZATION:
The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.
ii) Fitness Function:
The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
iii) Selection:
The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
iv) Crossover:
Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.

v) Offspring:
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

the new offspring are thus created.

vi) Mutation:
In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.

Mutation occurs to maintain diversity within the population and prevent premature convergence.

Input Function:

A function for stalagmite function to plot the graph in the main functions.
SYNTAX
x=ga(func,nvars)
x=ga(func,nvars,A,b)
x=ga(func,nvars,A,b,Aeq,beq)
x=ga(func,nvars,A,b,Aeq,beq,lb,ub)
x=ga(func,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x=ga(func,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x=ga(func,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options,IntCon)
x=ga(func,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options,IntCon,options)
x=ga(problem)
[x,fval] = ga(__)
[x,fval,exitflag,output] = ga(__)
[x,fval,exitflag,output,population,scores] = ga(__)
explanation:
x = ga(problem) fetches the MINIMUM value
[x,fval] = ga(__) returns fval,the value fitness of x
[x,fval,exitflag,output] = ga(__) also returns exitflag, which is known as an integer for the algorithm is terminated and 'output' contains output values regarding the performance of the algorithm.
function[f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1 = (sin(5.1*pi*x + 0.5)).^6;
f2 = exp(-4*log(2)*((x-0.0667).^2)./0.64);
f3 = (sin(5.1*pi*y + 0.5)).^6;
f4 = exp(-4*log(2)*((y-0.0667).^2)./0.64);
f = -(f1*f2*f3*f4);
end
Main Body:
First of all, we should create a mesh from the two vectors, which is to find the searching element for maxima of the function.
Using the 'mesh' command, and defining the number of iterations to iterate the values of the optimum values of the given function.
Calculate the values of the fitness function
Create the surface by giving 'surfc' command, which adds the color for the given surface.
By applying (-ve) sign, the function generate UPSIDE DOWN, which is good enough to get the maxima.
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2d mesh
[xx yy] = meshgrid(x,y);
rep = 50;
tic
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
study_time = toc
surfc(xx,yy,f)
shading interp
xlabel('x value')
ylabel('y value')
title('stalagmite function')
% STUDY 1
tic
for i = 1:rep
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time1 = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function max')
% STUDY 2
tic
for i = 1:rep
[ inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time2 = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g');
title('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function max')
% STUDY 3
options = optimoptions(@ga)
options = optimoptions(options,'populationsize',170);
tic
for i = 1:rep
[ inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time3 = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','magenta');
title('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function max')
global_maxima = -fopt







Conclusion:
Thus, the process of GENETIC ALGORITHM has been developed and executed by using MATLAB software.