Week 4.1 - Genetic Algorithm
Genetic Algorithm
- This algorithm reflects the process of natural selection, where the fittest individuals are selected for reproduction in order to produce offspring’s of the next generation.
- This can be applied to any engineering fields by doing many number if iterations to the final best solution.
Main steps of genetic algorithm:
- Initialization or initial population
- Fitness function
- Selection
- Cross over (Re combination)
- Terminating or stopping criterian
Initialization or initial population:
- Initial population consists of all the probable solutions of the given problem. Here an individual is characterized by a set of parameters known as genes.
- Genes are joined in a string to form a chromosome; chromosomes are basically strings of DNA.
Fitness function:
- After the initialization of population we determine the ability of the individual to compete with other individuals by using the fitness function.
- F(x)=-1/4x2 +2x+5 where x is the fitness function.
Selection:
- The selected individuals are then arranged in pairs of two to enhance reproduction, these individuals pass on their next genes to the next generation.
- The objective of this phase is to establish the region with high chances of generating the best solution to the problem.
Cross over:
- It is the most significant phase of the genetic algorithm, for each of parents to be mated; a cross over point is chosen at random form with in the genes.
Mutation:
- During their growth, there is some change in the gene of the children which makes them different from their parents.
Termination:
- If there is no improvement in the population for over X iterations.
- If we already have predefined an absolute number of generations to our algorithm.
- If our fitness function has reached a predefined value.
Syntax for Genetic algorithm (ga):
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
where,
fun- the objective function.
nvar- number of variables.
A,b – the linear equalities.
Aeq,beq – if the nonlinear in equalities exist.
Ub,Lb – the upper and lower bounds.
nonlcon-minimization of constrains defined using nonlcon.
Options- sets the population size using optimoptions.
clear all %clears all previous values in workspace.
close all % closes all the previous results.
clc % clears the command window.
%define the search space for the function
num_cases=50; %numer if cases is defined to be 50.
x=linspace(0,0.6,150); %x value is assigned to be 150 values from 0 to 0.6
y=linspace(0,0.6,150); %y value is assigned to be 150 values from 0 to 0.6
%creating 2D mesh
[xx,yy]=meshgrid(x,y); % meshgeid command is used to multiply vectors
%evaluating the stalagmite function.
for i=1:length(xx) % for loop i from 1 to length of xx
for j=1:length(yy) % for loop j from 1 to length of yy
input_vector(1)=xx(i,j); % input vector1 is being assigned as xx
input_vector(2)=yy(i,j); % input vector2 is being assigned as yy
%call the function
f(i,j)=stalagmite(input_vector); %call stalagmite function
end
end
%% study-1 satistical behaviour with unbounded inputs
tic % tic-toc command to find time taken.
num_cases=50;
for i=1:num_cases % for loop i from 1 to 50
[inputs1,fopt1(i)]=ga(@stalagmite,2); %staligmate is called
xopt1(i)=inputs1(1); % xopt1 is assigned to input 1(1)
yopt1(i)=inputs1(2); % yopt1 is assigned to input 1(2)
end
time_taken1=toc
%ploptting
figure (1);
subplot(2,1,1); % plots figure in top of same page
hold on % waits for next graph to save on same page
surfc(xx,yy,f); % surface of xx,yy, anf function is made
shading interp % to avoid the shaded lines in the plot
plot3(xopt1,yopt1,fopt1,'marker','+','markersize',5) % 3d plot is drawn
title('Behaviour with unbounder inputs'); % title is given to figure
subplot(2,1,2); % plots figure in bottom of same page
plot(fopt1); % function is being plotted
xlabel('Iterations'); %xlabel is being given
ylabel('Function minimum'); %ylabel is being given
%% study-2 Statistical behaviour with upper and lower bounds
tic
for i=1:num_cases % for loop i from 1 to 50
[inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); %staligmate is called with upper and lower bounds
xopt2(i)=inputs2(1); % xopt2 is assigned to input 2(1)
yopt2(i)=inputs2(2); % yopt2 is assigned to input 2(2)
end
time_taken2=toc
%plotting 2
figure(2)
subplot(2,1,1) % plots figure in top of same page
hold on % waits for next graph to save on same page
surfc(xx,yy,f); % surface of xx,yy, anf function is made
shading interp % to avoid the shaded lines in the plot
plot3(xopt2,yopt2,fopt2,'marker','+','markersize',3); % 3d plot is drawn
title('With bounded inputs') % title is given to figure
subplot(2,1,2) % plots figure in bottom of same page
plot(fopt2) % function is being plotted
xlabel('Iterations'); %xlabel is being given
ylabel('Function minimum'); %ylabel is being given
%% study-3 Introducing Population size
%have to use optimoptions to set the population size and curent search
%space is to 200
options =optimoptions('ga');
options=optimoptions(options,'populationsize',200)
tic
for i=1:num_cases
[inputs3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); %staligmate is called with upper and lower bounds and populations
xopt3(i)=inputs3(1); % xopt3 is assigned to input 3(1)
yopt3(i)=inputs3(2); % yopt3 is assigned to input 3(2)
end
time_taken3=toc
figure(3)
subplot(2,1,1) % plots figure in top of same page
hold on % waits for next graph to save on same page
surfc(xx,yy,f); % surface of xx,yy, anf function is made
shading interp % to avoid the shaded lines in the plot
plot3(xopt3,yopt3,fopt3,'marker','+','markersize',3); % 3d plot is drawn
title('With bounded inputs and population size') % title is given to figure
subplot(2,1,2) % plots figure in bottom of same page
plot(fopt3); % function is being plotted
xlabel('Iterations'); %xlabel is being given
ylabel('Function minimum'); %ylabel is being given