Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

GENETIC ALGORITHM FOR PATH PLANNING OF MOBILE ROBOTS

LAB NO.6

GROUP MEMBERS:

SYED AHSAN RAZA SHERAZI (150570)

MUHAMMAD SALMAN (150600)

ABDULLAH AHMAD (150693)

Bachelor of Mechatronics Engineering

(2019)

LAB SUPERVISOR

Engr. Haroon Khan

AIR UNIVERSITY, ISLAMABAD


LAB 6,7,8

Genetic Algorithm for Path Planning of a Mobile Robot

Objective:
To move from starting point to the endpoint on permissible while avoiding
collisions with obstacles and minimizing total distance travelled, time taken or
energy consumed

Stochastic Methods:
It are based on laws of probability for sampling of random events and have
the advantage that they can handle large problems.
Genetic Algorithms (GA):
It is applicable to both static and dynamic environments. In static
environment all coordinates (MR, obstacles, goal) are input into the onboard
computer system of MR while in dynamic environment sensors need to be used.

Flow Chart:
Formulas:
𝐹𝑖𝑡𝑛𝑒�(𝑖𝑃𝑎𝑡ℎ)=1.0/𝐷𝑖�𝑡𝑎𝑛𝑐𝑒𝐼𝑛𝑃𝑎𝑡ℎ(𝑖𝑃𝑎𝑡ℎ)
CHROMOSOME_LENGTH=(NOBS+2)*NBITS

Pdf = Fitness(i)/sum of all fitnesses


Cdf= pdf + sum of previous

Code
clc;
clear all;
x = [1 1 3 3 5 6 6 8 10 10 10 11 13 12 14 14];
y = [7 11 14 1 8 11 4 4 1 7 11 14 12 2 3 8];
z = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16];
links = [1 2 4 5 0;
2 3 1 0 0;
3 2 6 12 0;
4 1 7 9 0;
5 1 7 6 0;
6 3 5 11 0;
7 4 5 8 0;
8 7 10 14 0;
9 4 14 0 0;
10 8 11 16 0;
11 6 10 12 0;
12 3 11 13 0;
13 12 16 0 0;
14 8 9 15 16;
15 14 16 0 0;
16 10 13 14 15];

populationSize = 500;
itterations = 500;
%list Size Decalration

Gen = zeros(populationSize,9);
nextGen = zeros(populationSize,9);
fitList = zeros(populationSize,1);

%Generation Production
for g = 1:populationSize

%Generating First Random Path


Path = [1 randi(16) randi(16) randi(16) randi(16) randi(16) randi(16)
randi(16) 16];

%disp(Path);

distance = 0;
flag = 0;

%Calculating Distance
for i = 2:9
distance = distance + ((x(Path(i))-x(Path(i-1)))^2 + (y(Path(i))-y(Path(i-
1)))^2)^0.5;
if ~ismember(Path(i-1),links(Path(i),:))
flag = flag + 1;
end
end

%Calculating Fitness
fitness = 1/(distance+(flag*100));

%Assigning Generation Fitness Lists


Gen(g,:) = Path;
fitList(g,:) = fitness;
end
%Generating Probability
pdf = fitList ./ sum(sum(fitList));

%Generating Cummulative Probability


Sum = 0;
cuProb = pdf;
for cubit = 1:populationSize
Sum = Sum + pdf(i);
cuProb(cubit) = Sum;
end

%First Population Generated.

%Starting itterations
for itteration = 1:itterations
clc;
disp(["Itterations: " , itteration]);
nextGen = Gen;

%Selecting Best for Parents


%Selecting Father
[val,index] = max(pdf);
fatherIndex = index;
father = nextGen(fatherIndex,:);
disp("Father is:");
disp(father);

%Selecting Mother
while max(pdf) == val
[~,index] = max(pdf);
pdf(index) = 0;
end
[val,index] = max(pdf);
motherIndex = index;
mother = nextGen(motherIndex,:);
disp("Mother is:");
disp(mother);
%Selecting Third Best
while max(pdf) == val
[~,index] = max(pdf);
pdf(index) = 0;
end
[~,index] = max(pdf);
thirdbestIndex = index;
thirdbest = nextGen(thirdbestIndex,:);
disp("Third fittest:");
disp(thirdbest);
%Parensts Selected

%Generating New Population


for i = 1:populationSize
chance = rand(1);
for iSub = 1:populationSize
if iSub == 1
if (chance <= cuProb(iSub))
nextGen(i,:) = Gen(iSub,:);
end
else
if (chance > cuProb(iSub-1) && chance <= cuProb(iSub))
nextGen(i,:) = Gen(iSub,:);
end
end
end
end
%Calculating Fitness and distances of new Generation
Gen = nextGen;
for g = 1:populationSize
distance = 0;
flag = 0;

%Calculating Distance
for i = 2:9
distance = distance + ((x(Gen(g,i))-x(Gen(g,i-1)))^2 + (y(Gen(g,i))-
y(Gen(g,i-1)))^2)^0.5;
if ~ismember(Gen(g,i-1),links(Gen(g,i),:))
flag = flag + 1;
end
end
%Calculating Fitness
fitness = 1/(distance+(flag*100));

%UpdatingFitList
fitList(g,:) = fitness;
end
Gen = [Gen pdf];
nextGen = sortrows(Gen,10);
nextGen = nextGen(:,1:9);
Gen = nextGen;

%Reserving the best ones


nextGen(1,:) = father;
nextGen(2,:) = mother;
nextGen(3,:) = thirdbest;

%Crossover and Mutation


for i = 4:populationSize
if i <= populationSize/10
crop = randi(9);
nextGen(i,:) = [father(1:crop-1) mother(crop:9)];
nextGen(i,1+randi(7)) = randi(16);
else
if i<=2*(populationSize/10)
crop = randi(9);
nextGen(i,:) = [mother(1:crop-1) father(crop:9)];
nextGen(i,1+randi(7)) = randi(16);
else
if i<=3*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
else
if i<=4*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
else
if i<=5*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
else
if i<=6*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
else
if i<=7*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
else
if i<=8*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
else
if i<=9*(populationSize/10)
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
else
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
nextGen(i,1+randi(7)) = randi(16);
end
end
end
end
end
end
end
end
end
end
%next Population Generated

disp("=====================");
%disp("New Generation is:");
%disp(nextGen);
%Calculating Fitness and distances of new Generation
Gen = nextGen;
for g = 1:populationSize
distance = 0;
flag = 0;

%Calculating Distance
for i = 2:9
distance = distance + ((x(Gen(g,i))-x(Gen(g,i-1)))^2 + (y(Gen(g,i))-
y(Gen(g,i-1)))^2)^0.5;
if ~ismember(Gen(g,i-1),links(Gen(g,i),:))
flag = flag + 1;
end
end

%Calculating Fitness
fitness = 1/(distance+(flag*100));
%UpdatingFitList
fitList(g,:) = fitness;
end
%Generating Probability
pdf = fitList ./ sum(sum(fitList));
%Generating Cummulative Probability
Sum = 0;
cuProb = pdf;
for cubit = 1:populationSize
Sum = Sum + pdf(i);
cuProb(cubit) = Sum;
end
end
Output:

Conclusion:

In genetic algorithm ,by using a practice it is getting better and better


and give above ninety percent accuracy. It is very useful technique In
path optimization.

You might also like