Sie sind auf Seite 1von 60

AI Methods Group Assignment CT002-3-2-AIM

AI Methods – Cover Sheet


ASSIGNMENT

Instruction:
∙ Marks will be awarded for good presentation and thoroughness in your approach.
∙ Complete this cover sheet and attach it to your assignment.

Student declaration:
I declare that: ▪ I understand what is meant by plagiarism
▪ The implications of plagiarism have been explained by our lecturer
▪ This assignment is all my work and I have acknowledged any use of the published
or unpublished works of other people in documentation.

Signature: Date:

Name 1. Andrew Chung Hui Yue


2. Loo Chee Hin
3. Yeo Khang Hsien
4. Gan Eng Hung
TP No 1. TP046091
2. TP046360
3. TP051413
4. TP046343
Intake UC2F1905IS
Research Topic Solving Travelling Salesman Problem by using Genetic Algorithm

INTAKE UC2F1905IS
LECTURER Zailan Arabee bin Abdul Salam
WEEK ASSIGNED Week 2
DOCUMENTATION SUBMISSION Week 13 (9th August 2019)
PRESENTATION Week 14

1
AI Methods Group Assignment CT002-3-2-AIM

Contents
Solving Travelling Salesman Problem by using Genetic Algorithm...........................................2

Abstract........................................................................................................................................3

Index Terms..............................................................................................................................3
Introduction..................................................................................................................................4

Materials and Methods.................................................................................................................5

Materials....................................................................................................................................5
Methods.....................................................................................................................................7
Literature Review on topic......................................................................................................13
Algorithm Implementation......................................................................................................40
Results and Discussion...............................................................................................................42

3.1 Discussion on implementation..........................................................................................42


3.2 Results...............................................................................................................................51
Conclusion..................................................................................................................................55

References......................................................................................................................................56

2
AI Methods Group Assignment CT002-3-2-AIM

Solving Travelling Salesman Problem by using Genetic


Algorithm
Andrew Chung Hui Yue, Loo Chee Hin, Yeo Khang Hsien, Gan Eng Hung
Asia Pacific University of Technology & Innovation
andrew.chung2009@hotmail.com, johnsonloo19980828@gmail.com, yeokax@gmail.com,
enghunggan@gmail.com

Abstract

Traveling Salesman Problem (TSP) is a combinatorial problem in the computer science

field that emphasizes on getting the best result in the most efficient way which originated from

finding the best way for a person to travel all the cities without repeating them in the shortest

time, often being solved by heuristics approach such as Genetic Algorithm (GA). GA is part of

the evolutionary algorithm that derives chromosomes behaviour into data searching methods

which the term ‘fitness function’ refers to the scale of indifference compare to the optimal

solution. The selection process starts off from initialization where the population was randomly

generated, the first sets of best fitting chromosomes will undergo the repetitive process of

crossover and mutation to provide better results until reaching the termination condition. The

overall result concluded that it is suitable to implement GA in TSP as GA has the advantage of

time efficient besides having high accuracy and precision.

Index Terms

Genetic Algorithm, Traveling Salesman Problem, Crossover, Mutation, Initialization

3
AI Methods Group Assignment CT002-3-2-AIM

Introduction

The analogy of Travelling Salesman Problem (TSP) is a salesman must travel through all

the cities in the list to deliver the items. The key to solve the problem is to find an efficient way

to pass all the nodes in terms of time and distance. Lots of heuristics approach has been proposed

to provide solutions with each of them has their own strength and flaw. For instance, genetic

algorithm was introduced based on the genetic concept. Genetics refers to chromosomes which

undergoes mutation, crossover and selection. A population of candidate solutions are referred as

chromosomes, which each individual undergoes iteration process which is call a generation.

Each of the chromosomes will be evaluate based on their fitness function. Starting from

initialization, the population will be generated randomly to proceed to the selection phase.

Selection phase provides iterative solutions to search for the best-fitting chromosomes together

with crossover and mutation. Cross over between sets of best-fitting chromosomes will have a

high chance of producing better solution, the chromosomes will then undergo mutation which

swaps non-fitting chromosomes to a random chromosome which may provide better results.

Termination condition has been reached if the solution has been found either the best solution, or

a solution that satisfies the minimum criteria within allocated budget.

4
AI Methods Group Assignment CT002-3-2-AIM

Materials and Methods

Materials

Software IDE

Figure 1 Logo of PyCharm IDE[ CITATION Wik16 \l 1033 ]

Figure 1 is the most popular IDE used for Python scripting language. This IDE offers

some of the best features to users and developers in the aspects of code completion and

inspection, advanced debugging and support for web programming and frameworks such as

Django and Flask[ CITATION tut193 \l 1033 ].

Hardware Configuration

The hardware that used to conduct this research is the PC to run the Algorithm in Python

on TSP for an algorithm, Genetic Algorithms. The PC has the specification of the latest

Windows Operating System which is Windows 10, the CPU have the latest generation which is

9th generation of i7 processor, 16 Gigabyte of RAM, 512 Gigabyte of Solid State Drive, 1

Terabyte of Hard Disk Capacity with GeForce GTX-1080 Ti Graphic Card to run the Genetic

Algorithms.

5
AI Methods Group Assignment CT002-3-2-AIM

Programming Language

Figure 2 an example of syntax in Python script[ CITATION Wik07 \l 1033 ]

Python is an Interpreted, Object-Oriented, High-Level programming language with

dynamic semantics. Its high-level built in data structures, combined with dynamic typing and

dynamic binding, as well as for use as a scripting or glue language to connect existing

components together. The debugging program is easy, and it quickly execute the program and

the productivity of the interpreter is increased [ CITATION pyt19 \l 1033 ]. Python has Prebuild

Libraries like Numpy for scientific computation, Scipy for advanced computing and Pybrain for

machine learning, these libraries is supporting the development of Artificial Intelligence. NumPy

is used as a container for generic data comprising of an N-dimensional array object, Matplotlib is

a 2D plotting library creating publication quality figures, and some of the libraries like Scikit-

learn is an efficient tool for data analysis and any other relevant libraries which are related to

solve Artificial Intelligence problems with data [ CITATION Ruc16 \l 1033 ].

6
AI Methods Group Assignment CT002-3-2-AIM

Methods

Genetic

Figure 3 a sample of DNA[ CITATION jus17 \l 1033 ]

Genetic is the study of heredity, the biological process where a parent passes certain

genes onto their children or offspring. The inheritance occurs when each of the child inherits

genes from both their biological parents and produce the specific traits. Some of the traits may be

physically same to the parents. On the other hand, some of the genes also carry the risk of certain

diseases and disorders that may passes on from parents to their offspring [ CITATION Ana19 \l

1033 ].

Genetic is built around molecules called DNA which hold all the genetic information for

an organism. It provides cells with the information to perform the tasks that grow, survive and

reproduce. In a particular section of a DNA molecule that tells a cell to perform one specific task

is called gene. Heredity is the children inherits their parents’ features. During reproduction, DNA

is replicated and passed from a parent to their offspring [ CITATION Bas16 \l 1033 ].

In some case, the gene also can be affected by some events such as selection, crossover

and mutation. In the Genetic selection is the preferential survival and reproduction or preferential

elimination of individuals with certain genotype, it can be naturally or artificially controlling

factors[ CITATION Bit19 \l 1033 ]. For some case, crossover and mutation often occur during the

7
AI Methods Group Assignment CT002-3-2-AIM

genetic selection. Chromosome is the main subject and it has simply long sequences of DNA

made up of many genes. During replication process, the term “Crossing Over” is occurred when

the process by which a pair of chromosomes aligns closely to each other and swap segments of

DNA containing genes [ CITATION Mar181 \l 1033 ]. While a gene mutation is a permanent

alteration in the DNA sequence, and this make the sequence differs in physically appearance, it

can mutated in the range of size and affected anywhere on the segment of a chromosome might

including multiple genes [ CITATION NIH19 \l 1033 ].

Implementation of Genetic Algorithm

Figure 4 Genetic Algorithms in Binary Representation[ CITATION Vij17 \l 1033 ]

Genetic Algorithms are adaptive heuristic search algorithms that belong to the larger part

of evolutionary algorithms. This algorithm is based on the ideas of natural selection and genetics.

It is an intelligent exploitation of random search provided with historical data and search the

better performance in solution space. It generates high-quality solutions to solve optimization

problems and search problems.

Genetic Algorithms simulate the process of natural selection which means the

spontaneous selection occurs to allow survive and reproduce for next generation when adapting

the changes of the environment. To be better understanding about the simulation, the ‘survival of

8
AI Methods Group Assignment CT002-3-2-AIM

the fittest’ is among individual of consecutive generation for solving problem, it chooses the best

of the best solution. Each generation consists of a population of individuals (genes) and each

individual represents a point in search space and possible solution. The presentation of the

individual can be many types and they know as the solution which analogous to the

Chromosome [ CITATION Gee193 \l 1033 ].

Methods in Algorithm

As the Genetic Algorithms is an evolutionary algorithm, the optimization is performed to

produce the better solution by the dynamic algorithms which can evolve over time. There are

three main characteristics that needed to be concerned when implementing the Genetic

Algorithms. Firstly, a population-based is needed to be initialized, the set of the current solution

is produced then it optimize the current solution to produce the better set of solutions. Next, the

fitness-oriented where the fitness value associated with each individual solution calculated from

a fitness function, such that the fitness value is determine the score of the solution. The lowest

the optimization difference, the better the solution. Last but not least, the variation-driven is used

to generate new better solutions when there is no acceptable solution in the current population.

Chromosome Representation and Evaluation

There are different representations available for the chromosome and the selection of the

proper representation is problem specific. The good representation is based on the size of the

search space, the smaller the search space, the more the easiest to search and the better solution

is produced.

The representations available for the chromosome including Binary representation is each

chromosome is represented as a string of “0” or “1”, Permutation representation is useful for

ordering problems, the famous problem that commonly known the Travelling Salesman Problem

9
AI Methods Group Assignment CT002-3-2-AIM

and the Value representation which the actual value is encoded as it is.

Initialization – This is the first step where the number of populations is initialized by selecting

the proper number of individuals within it.

Fitness Function – The fitness function is used as a constant to determine the score of each

chromosome. Assuming that the fitness function used as f ( x )=2 x +2 in the example, means that

{x} where is the chromosome value, the lowest the value, the better the solution.

Selection – This is the method where the chromosomes are select a number of individuals from

the population in the mating pool. The best individuals is based on the previously fitness value

calculation where is the selection of the best score into subsets.

After all the subsets is selected in the mating pool, the place where to the mating process is held

at, some parents are selected for mating. There is spontaneous changing of chromosome

occurred. As a result, the crossover and mutation are occurred, and the consecutive solutions are

affected [ CITATION Ahm18 \l 1033 ].

Figure 5 Crossover in Binary Representation [ CITATION Vij17 \l 1033 ]

Crossover – This is the most significance phase in a Genetic Algorithms. The crossover point

will be chosen the half from two of the chromosomes at random from within the genes. The

offspring are created by exchanging the genes of parents among themselves until the crossover

process is done. The new offspring are added to the subset of population.

10
AI Methods Group Assignment CT002-3-2-AIM

Figure 6 Mutation in Binary Representation [ CITATION Vij17 \l 1033 ]

Mutation – In certain new offspring formed, with very low random probability, there some

genes can be subjected to a mutation. 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[ CITATION Vij17 \l 1033 ].

Problem

Figure 7 Travelling Salesman Problem [ CITATION Mat19 \l 1033 ]

Travelling Salesman Problem is the most notorious computational problem. The problem

is a traveller is required to visit all cities from a list, where distances between all the cities are

known and determine the shortest possible route that the traveller can pass only one time per city

and returns to the initial city. This problem is a combinatorial optimization problem, given a

complete graph on n vertices and a weight function defined on the edges. The objective of this

problem is to construct a tour of minimum total weight. This is considered as hard combinatorial

optimization problem and the decision version of the problem is NP-complete [ CITATION neo19 \l

11
AI Methods Group Assignment CT002-3-2-AIM

1033 ]. There are (n-1)! Number of possibilities and the solution can be obtained in lesser time,

though there is no polynomial time algorithm.

Consider a graph Goal (G) = (set of city (v), weight of edge(e)), an edge represents that

vertices u and v are connected, e(initial city (u), final city (v)), the distance between vertex u

and v must not containing any negative value, d(u, v). The initial city is initialized as {1} and {j}

as the cities that visited from the pervious one as current position. Hence, this is a partial tour and

the next city which will be going to visit must in convenient way and terminate the pervious

searching to prevent repeating occurs. Hence, this is an appropriate sub-problem. For a subset of

cities S Є {1, 2, 3, ..., n} that include {1}, and j Є S, let c(S, j) be the length of the shortest path

visiting each node in S exactly one, initialize a {1} and ending at {j}. When |S| > 1, C(S, 1) = ∝ is

defined as the path invalid to start and end at {1}. Let express C(S, j) in terms of smaller sub-

problems and the starts at {1} and end at {j}. The selection of the next city as the

formula[ CITATION tut194 \l 1033 ]:

C(S,j)=minC(S−{j},i)+d(i,j)wherei∈Sandi≠jc(S,j)=minC(s−{j},i)+d(i,j)wherei∈Sandi≠j

12
AI Methods Group Assignment CT002-3-2-AIM

Literature Review on topic

Loo Chee Hin

Genetic Algorithm (GA) is an optimization search and this search approach is based on

the natural selection and natural genetics. Many Computer Science researchers are using GA to

solve various problems and the results of the algorithms that gain optimal solution on

combinatorial problem. Previously, there are some algorithms are unable to produce the best

quality of the solution to some particular problem (i.e. Travelling Salesman Problem, n-Queens

Problem and Knapsack Problem) and the calculus based optimization techniques and dynamic

programming, backtracking, branch and bound greedy method also not very useful for solving

problem (Yadav & Singh, 2016; Farhan, Tareq & Awad, 2015). However, the researcher K. D

Crawford (1992) has applied GA to solve n-Queens Problem and the ways of solving that

problem had discussed. In addition, there is no solution to the n-Queens Problem when Ant

Colony Optimization (ACO) applied and even a few constraints were added as it cannot be

directly to address the n-Queens Problem (Farhan, Tareq & Awad, 2015). The Non-

Deterministic Polynomial Hard (NP Hard) problem such as Travelling Salesman Problem

and Knapsack Problem is difficult to solve as it contains many solutions, but there is only one

can be efficiently drawing the best solution of all. The researcher chose GA as it is a

polynomial time algorithm and it can solve the combinatorial problem as big size as it can.

Furthermore, GA is also search and provide optimal and appropriate results which close to the

exact solution in the polynomial time (Yadav & Singh, 2016; Philip, Taofiki & Kehinde, 2011).

Hence, the GA can be solving different NP Hard problems with the techniques of polynomial

time algorithm with the biological studies methods, for instance, chromosome, mutation and

crossover, and it draws the conclusion of all the optimization process efficiently and effectively.

13
AI Methods Group Assignment CT002-3-2-AIM

Methodology

Travelling Salesman Problem Approach

The Travelling Salesman Problem (TSP) is a combinatorial optimization problem as it is

a simple conceptual problem but hard to solve due to it is an NP-complete problem. This

problem can be defined as a problem where starting from a node (as known as city) is required to

visit every other node only once in a way that the minimization of the total distance is covered

[ CITATION Ade11 \l 1033 ].

Figure 8 Formula for Travelling Salesman Problem

Constraints set (4), (5), (6) and (7), are used to eliminate any sub tour in the solution in

figure 8. The problem reduces to a simple assignment problem which can be solved as an Linear

Programming without binary constraints on [xij] and will produce the result in binary solution for

[xij] if without the additional constraints for sub tour elimination, but introducing of this measure

is to makes the problem a Mixed Integer Problem with n2 integer problem of size n, and it will

result of very difficult to solve for a moderate size of problem [ CITATION RBr05 \l 1033 ].

14
AI Methods Group Assignment CT002-3-2-AIM

Steps Of Genetic Algorithms

1. The initial population of individual string of the given TSP problem is created randomly and

a matrix representation of the cost of the path between two cities is created.

2. Each chromosome is assigned the fitness in the population using fitness criteria measure.

F(x) = 1/x where x represents the total cost of the string. The selection criteria is when the

value of string is close to some threshold value.

3. The new offspring population is created from two existing chromosomes in the parent

population by applying crossover operator.

4. The resultant offspring is optional mutate but it still required.

5. An optimal solution to the problem is obtained during the step 3 and 4 is repeated.

Figure 9 Flowchart of Applying GA to TSP


The operators that needed to be considered and applied to the algorithm to create new, a

hopefully, better populations as successive generations. The first operator is reproduction where

the generations are produced from the copy of strings with some probability based on their

15
AI Methods Group Assignment CT002-3-2-AIM

respective objective function value. Crossover as the second operator where the selected pairs of

string are randomly mated, and this is to create a new strings population. Last operator is

mutation, and this is occasional random alteration of the value at some segments of string

position either single or multiple value. The first and second operators merge together is the most

powerful process in the Genetic Algorithms search while the last operator diversifies the search

space and protects form loss of genetic material as the reproduction and crossover process might

be affected the genetic material. In this case, the mutation rate is set very low, whereas the

probability of crossover is set very high [ CITATION Var12 \l 1033 ].

For the TSP, solution is typically represented by chromosome of length as the number of

nodes in the problem. Each gene labelled as node such that no node can appear twice in the

same chromosome. In representing tour of the TSP, adjacency representation and path

representation where the path representation for a tour which simply lists the label of nodes. For

instance, let {6, 7, 8, 9, 10} as nodes in 5 node instances, then a tour {6, 8, 10, 7, 9, 6}, the result

will be represented as (6, 8, 10, 7, 9).

Fitness Function

The GAs are used for maximization problem as this problem have the fitness function

which is same as the objective function. F(x) = 1/f(x) where f(x) as objective function. TSP is a

minimization problem; by applying the fitness function, where f(x) calculates cost (or value) of

the tour represented by a chromosome.

Selection Process

In this process, chromosomes are copied into next generation with a probability

associated with their fitness value. A higher portion of the highly fit chromosomes is assigned to

next generation, this is mimic the Darwinian “survival of the fittest” in natural phenomena. The

16
AI Methods Group Assignment CT002-3-2-AIM

Elitism method is used for selection which first copies are the best chromosome or may be a few

amounts to new population. The remaining is done in classical way. The reason of using Elitism

are it can very rapidly increase the performance of GA as it prevents losing the best found

solution.

Crossover Operator

The search of the solution space is done by creating new chromosomes from old

generation. This operator is significance to GA. Initially, a pair of parents is randomly selected

from the mating pool. Next, a crossover point along their common length of the crossover site is

selected. Before crossover point, a sequential constructive crossover operator is introduced and

the information after the crossover site of the two parents’ strings are swapped. In some case, a

gene has already had copied into the offspring, then it will replace that gene by unvisited gene,

thys creating two new children.

Steps for the Crossover Operator:

1. Start from the node p(the first node in parents P1 and P2).

2. A sequential search is done for both parent chromosomes and consider the first legitimate

node appeared after the node 1 in both P1 and P2. Suppose the node x and node y are found

in P1 and P2 respectively. The crossover point is selected after 2nd node in both parents P1

and P2 is considered.

3. If Cpx < Cpy, then select node x, otherwise node y will be the next node and concatenate it to

the partially constructed offspring chromosome.

4. If node x is selected as the next string in partially constructed offspring chromosome, the

remaining genes is copied from P2, otherwise copy from P1.

5. A gene supposedly been copied into the offspring then replace that gene by unvisited gene.

17
AI Methods Group Assignment CT002-3-2-AIM

The cost matrix

An illustration the crossover technique is given in figure 10. Let a pair of selected chromosomes

be P1:(1, 3, 6, 4, 5, 7, 2) and P2:(1, 5, 4, 2, 6, 3, 7) with values 365 and 349 respectively.

Figure 10 Cost Matrix


Select first node in parents P1 and P2 are node 1. The legitimate node after node 1 in both

the parents P1 and P2 are node 3 and node 5 respectively. The crossover point will be after node 3

and node 5 in parents P1 and P2 respectively. A calculation will be done for the value of cost of

the path as C3=99 and C5=35. As the result C5 < C3, the node 5 will be accepted as the next node

in partially constructed chromosome and it selected from the parent P 2 so the rest of the bits from

P1 are added. The partially constructed chromosome is (1, 5, 6, 4, 5, 7, 2) and the node 5 has

already been copied into offspring so the node 5 by unvisited node 3 is replaced. Thus, the

complete offspring will be (1, 5, 6, 4, 5, 7, 2) with value 263 which is less than value of both the

parent chromosomes. The crossover is shown in the figure 11 as the parents as (a) and (b), while

the offspring is shown as (c) [ CITATION Var12 \l 1033 ].

18
AI Methods Group Assignment CT002-3-2-AIM

Figure 11 Result of TSP

Result of implementing GA in Travelling Salesman Problem

The example of 7 cities graph that shown in figure through a matrix representation and on

the basis of calculation, an experimental result that new crossover operator is capable of

calculating an approximately optimal path for TSP using Genetic Algorithm with less time. The

sequential constructive crossover (SCX) is a sequential search for each gene in a chromosome

and hence comparing it with another chromosome parent genes thereby resulting in a child

chromosome. In this method might lead to time consuming and higher cost is occurring. The cost

matrix method is better as it is only a single search for crossover point and after that the child

chromosome is developed. Hence, the time taken to complete is shorten and the cost is lower as a

result to produce a better optimal solution for the given TSP problem.

19
AI Methods Group Assignment CT002-3-2-AIM

n-Queens Problem Approach

The n-Queens Problem is a combinatorial problem which consist of n x n chessboard

where the n is indicating the number of queens can be placed to ensure that no queen can attack

the other queens. The classical chessboard which consist of 8 x 8 with 8 queens and all the

queen’s problem was generalized as putting n queen such that all the queens cannot attack each

other. For the first eight n different ways are (1 , 0, 0, 2, 10, 4, 40, 92) where the number of the

ways that non-attacking queen on the board are placed [ CITATION Slo07 \l 1033 ].

Figure 12 N-tuple notation

By using the solution representation as figure 12 as n-tuple (q 1, q2, …, qn) that are

permutation of n-tuple (1, 2, …, n) to guaranteeing no rook attacks means the leads to reduce the

rate of complexity of problem such a way that the part of the conflict will be solved in the

process. Figure 1 is a demonstration a 4-tuples for the 4-queens problem and both are the

solution is shown.

Figure 13 The "left" and "right" diagonal as attack in 3 steps and 2 steps

Since the n-tuple representation eliminates row and column conflicts, the diagonal attacks

between have to be concerned. The fitness function 3 should count diagonal attacks accordingly.

As shown in figure 13, the 2n-1 “left” and 2n-1 “right” diagonals have to be checked, and the

20
AI Methods Group Assignment CT002-3-2-AIM

consideration of all the diagonals cannot be conflicted such diagonals consist of only one queen

so that the algorithm is able to check 2n-3 “left” and 2n-3 “right” diagonals in which the conflict

will founding [ CITATION Bož06 \l 1033 ].

The fitness function will return value “0” if and only if there is no conflicts and the

solution is correct. A queen that occupies i-th column and qi-th row is located on the i+qi-1 left

and n-i+qi right diagonal. i-th and j-th queens share a diagonal when

i – qi = j – qi for the represents “left diagonal” is equal to “right diagonal”, thus, leads to O(n)

complexity of the fitness function means that each new solution will be differs from the new

value of the fitness function can be by observing the 8 diagonals which will be directly found the

change depending in the number of queens.

In GA of the search and optimization procedures, whish based on 3 main principles, first

selection in selecting certain number of individuals that will survive into next generation

somewhat biased, favouring “better” individuals, then crossover is selecting selected individuals

act as parents that are3 combined using crossover operator to create children and then mutation is

applied on new individuals where it randomly changes few individuals but the mutation

probability is usually very low. A random population of individuals is created as potential

solutions and all individuals are evaluated using fitness function. All the solution is known as

chromosome in an individual that are estimated using fitness function [ CITATION Dav89 \l 1033 ].

Steps of Genetic Algorithms

Individual representation and fitness function for n-Queen problem were presented in n-

tuple representation. By solving 8-queen problem, the GA is implemented to find all 92 correct

solution. The algorithm starts by the first random solution. Each chromosome is represented by

one-dimension array with size equal to 8 and the reason of doing this to reduce the space search.

21
AI Methods Group Assignment CT002-3-2-AIM

Each position refers to row in 8 x 8 chessboard and the value containing in the position is

represent the value of each column where number 2 is in the queen location is [0,2] and for

Column 2 in the array position is equal to 0. The one of the array is shown in figure 14.

2 5 3 7 6 4 1 0
Figure 14 The array of position
1. Generate a randomly with 92 number of solutions. This was done by initializing 92

chromosomes with length of 8 and act as first population for GA.

2. The fitness function is always computing in all the solution in generation by generation. The

selection of individual will occur when the fitness value is ranked.

3. The fitness value is ranked for each chromosome, the higher the fitness valuer will be close

to halt condition where the combination process is formed to produce a new offspring.

22
AI Methods Group Assignment CT002-3-2-AIM

4. The crossover and mutation of chromosomes is begun. Mutation operator is very simple, it

performs randomly in a given tuple, then select two positions and change the numbers

between themselves, finally a new individual is formed which will be similar to the original

chromosome and the tuple is preserved as in figure 15.

Figure 15 Mutation Operator


Another crossover operator is Partially mapped crossover (PMX crossover), similar to the

two-point binary crossover. The figure 16 is random selection of two positions within

chromosomes and exchange of genetic material:

Figure 16 PMX Crossover - First Step


This will result in invalid tuples in most cases, the numbers in a tuple must be unique.

Figure 17 in PMX crossover eliminates duplications as figure, number 2 occurs at position 1

and 4 in the first offspring. The 2 at position 4 is newer from the crossover, as the result of

changing the 2 at position 1 into 3 that was at position 4 before the crossover [ CITATION Eri98

\l 1033 ].

23
AI Methods Group Assignment CT002-3-2-AIM

Figure 17 PMX Crossover - Second Step


The third operator as shown in figure 18 is designed for 3-way tournament selection

means that parents are compared, and equivalent positions are copied the offspring. The

offspring tuple at other positions are filled in randomly, but in order to preserve tuple

validity, caring the offspring must be taken. If parents are equivalent, one of them is replaced

by a randomly created tuple to avoid chromosome duplication.

Figure 18 3-way Tournament Crossover


5. Repeat the steps 2 to 4 until reach to the stop condition. The fitness is calculated to be

founding the number of crossovers between the queens and the stop condition.

Result of implementing GA in n-Queens Problem

24
AI Methods Group Assignment CT002-3-2-AIM

The results of applying the Genetic Algorithm to find the 92 solutions for the 8-Queen in

table, with the number of iterations that are required to achieve each solution.

Figure 19 Result of n-Queens Problem

From figure 19, there is a solution founded by repeating the algorithm 13 times, as going

on, the newest result is faster than the rest of 92 solution had founded.

Knapsack Problem Approach

Knapsack Problem consider an optimal solution. The aim to fill the knapsack, the total

25
AI Methods Group Assignment CT002-3-2-AIM

weight of each item does not exceed the capacity of knapsack, maximized the total profit of the

contain objects. Pi as the item having a weight and profit of each item and C as the capacity of

knapsack. In this problem, the problem is declared as 0-1 problem because each item has been

has taken in the knapsack either receive, reject or ignore. 0-1 knapsack problem is also known a

binary value and also represent a vector value. Condition is (1) and the subject to contain and

total profit maximize ( 1 ≤i ≤n ) is (2) in figure 20.

Figure 20 Formula for Vector and Maximize Total Profit

Item A B C
Profit (pi) 10 8 30
Weight (wi) 3 2 4
Table 1 Knapsack Table

The prosecute to maximize the total profit (3) and the subject to the constraints are made (4) in

figure 21.

Figure 21 Calculation based on Knapsack Table

(3) (4)

x1 x2 x3 wi pi
0 0 0 0 0
0 0 1 4 30
0 1 1 6 38
1 0 0 3 10
1 1 0 5 18
1 0 1 7 40
0 1 0 2 8
1 1 1 9 48
Table 2 Outcomes of all three possibilities

In table 2, the total possible solution is 2 n. In order to find the optimal solution for the

26
AI Methods Group Assignment CT002-3-2-AIM

exemplification of 0-1 knapsack problem is 40 for the given constraints (w=7) where x 1 and x3 is

selected for the total weight is 7 and the selection of x2 is aborted in object of C, as the result is

40. With the constraint of w, the total weight of each item cannot be exceeded the capacity of

knapsack (7) and the maximized total profit of the contain objects is 40, and the maximum

possible solution is eight.

GAs is a searching and maximize real time function algorithms and it excel in taking big,

search and genetic, and navigation them found for maximize association or mixture compound of

thing and results on not find in a lifetime of its capacity. The fitness chromosome in current

population (parents) is taken to decode in order to find the optimal fitness value for production,

and new population will be inheriting this step. If the fitness value of chromosome is very less,

the value will be neglected, and selection method, crossover and mutation are introduced as

below [ CITATION Ade11 \l 1033 ].

Steps of Genetic Algorithms

1. Generate an initial random population chromosome. Given have a constraint in equation (4),

two functions are defined to solve the problem and used to generate population which is fully

feasible to solution.

2. The population of chromosomes is evaluated by using an appropriate fitness function and

constraint function.

3. Selection is done when select the subset of chromosomes with better fitness value as parents.

4. Assume a Knapsack Problem of four items has a full feasible random population. A

crossover the pairs of parents with given probability (Pc) to produce offspring. This is

performing the step of using two feasible solution as parents then generate to children in

figure 22.

27
AI Methods Group Assignment CT002-3-2-AIM

Parent 1 0 0 1 1 Feasible
Parent 2 1 0 0 1 Feasible
Crossover
Child 1 0 0 0 1 Feasible
Child 2 1 0 1 1 Infeasible
Figure 22 Crossover Operator
5. The chromosomes of offspring with probability (Pm) to avoid early trap onto local solutions

is mutated in figure 23. This step also may lead next solution to infeasibility.

Feasible 1 0 0 1
Mutation
Infeasible 1 1 0 0
Figure 23 Mutation Operator
6. The fitness values of offspring are re-evaluated. The figure and figure are a conventional

Genetic Algorithm, due to the crossover and mutation operations leads the next population to

be inefficient and takes a much time to be converged to best solution. The new population is

made by adding a small function to conventional GA such that after the end of mutation

operator, the function named as “fcheck” is added, and this function is to check the

population and find all the infeasible solutions and replace all infeasible solutions to

generated new solutions thus the new population randomly which is fully efficient in terms of

feasibility. Below is the Pseudocode of how it works in figure 24:

for i = 1 to population size {

check the current solution;

if(infeasible) {

replace by random feasible solution;

else {

pass to new population;

28
AI Methods Group Assignment CT002-3-2-AIM

}
Figure 24 Pseudocode

The modified GA is works as given in the flowchart figure 24:

Figure 25 Flowchart
7. If the stopping criteria are satisfied, the algorithm is terminated, and all runs are successfully

executed.

Result of implementing GA in Knapsack Problem

As the result of using the proposed Genetic Algorithm is much efficient than the

conventional Genetic Algorithm. The computation time for the conventional GA is higher than

29
AI Methods Group Assignment CT002-3-2-AIM

the proposed GA, the Generation of the algorithm for conventional is higher than then the

proposed one as well as the iterations went through in a running algorithm in conventional is

higher than the proposed one[ CITATION Meg13 \l 1033 ].

Conclusion for all the Approaches

Based on the results from the all approaches, the GA is quite efficient to solve many

problems especially for optimization and search problem. All the problems are combinatorial

problem which has more than 1 possible solution to make an easy task to solve become more

difficult. This kind of problem, we can know as a Non-deterministic Polynomial Hard (NP-Hard)

problem which needed the Polynomial Time Algorithm to solve it, by breaking down as many

generations as it could in order to find the optimal and best solution to the given problem. The

fitness value also generated to very high, the higher the fitness value, the shorter the distance by

reaching the optimum solution. The mutation rate often set in very low value (0.1) this is to

prevent going back to the parent’s chromosome and the new generation is distinctive to previous

generation. The swapping of genes is by the process of crossover which will exchange the part of

the solution until its meet to the best solution as the end result. The runs of the Genetic

Algorithm must test for multiple time and produce the mean result. Every runs of the program

will produce a different result from before. This is symbolizing how the Genetic Algorithm is

working like natural phenomena with give the different result, generation by generation to

produce the best offspring. As human, the uniqueness of all living things is produced by the code

of the genes; as the genes always mutating and crossing over, the end result will be much

30
AI Methods Group Assignment CT002-3-2-AIM

different from the previous one. Last but not least, by using this algorithm, it can solve many

kind of problems, the swarm algorithm such as Ant Colony Optimization and Artificial Bee

Colony Algorithm and the statistical algorithm such as Decision Networks and Probabilistic

Reasoning cannot be efficiently solve the combinatorial problem because the problem have to be

solved in natural way to obtain the best result.

2.1.2 Andrew Chung Hui Yue

The traveling salesman problem (TSP) is an algorithmic problem that became one of the

most widely studied. There are many algorithms have been created and used in computer science

to find the most efficient route for data to travel between various nodes. There are many

researchers in various fields such as artificial intelligence, biology, mathematics, physics, and

operation research devotes themselves to trying to find the efficient methods for solving TSP

such as genetic algorithms (GAs), ant colony optimization (ACO), simulated annealing (SA),

neural networks, particle swarm optimization (PSO), evolutionary algorithms (EA), memetic

computing, etc[CITATION Yan12 \l 1033 ].

Table 3 Optimal results comparison between algorithms

31
AI Methods Group Assignment CT002-3-2-AIM

Each Test Cases is carried out 10 times to ensure that the result between algorithms is consistent.

According to table showed above, Particle Swarm optimization (PSO) have shorter distance in

every test case. Particle swarm optimization (PSO) able to find more optimal results compared to

Genetic Algorithms (GA). Previously, traditional algorithms such as Simulated Annealing (SA)

and Tabu-Search (TS) algorithms are less efficient for solving difficult optimization problems

and remain inefficient to find the optimal solution to TSPs [ CITATION Gup13 \l 1033 ]. However,

designing TSP-specific operators, incorporating local searches, and maintaining population

diversity are considered the promising way to solve TSP.

Table 4 Performance Comparison between algorithms

Simulated Annealing (SA) is the oldest probabilistic meta-heuristic algorithm. According

to the result of the journal article had clearly showed that using the Simulated Annealing (SA)

able to find the optimization route but Simulated Annealing (SA) could not be the best

algorithms that is able to find the global optimum. The Simulated Annealing (SA) also required

longer processing time as SA is designed to find an approximate global optimum is more

important than finding a precise local optimum in a fixed amount of time. Hence, there is a need

of study the influences of different algorithms which are nature-inspired by using time and

optimal solution of different nature-inspired algorithms.

32
AI Methods Group Assignment CT002-3-2-AIM

33
AI Methods Group Assignment CT002-3-2-AIM

2.1.3 Yeo Khang Hsien

Table 5 The Problem Size with Time (ms) in solving TSP

Figure 26 The Graphic Representation of the Problem Size Against the Time (ms)

Figure 27 Initialized all cities

Figure 28 Final Result of all connecting cities

34
AI Methods Group Assignment CT002-3-2-AIM

Table 6 The result of TSP

An ideal algorithm from the perspective of artificial intelligence community is to provide

the optimal solution to a specific problem with minimum cost. Traveling salesman problem

(TSP) is a combinatorial optimization problem and being studied intensively in the area of

optimization to solve it using different approaches such as implementing Ant Colony

Optimization, Greedy Algorithm, and Genetic Algorithm. The concept of TSP is finding a

Hamiltonian Cycle, which is to pass through all the cities (nodes) only once (Ejim, 2019; Singh,

Mehta and Katiyar, 2014). A greedy algorithm has a greedy nature of making the best decision at

each stage that yields locally optimal solutions that approximate global optimal solution in a

reasonable time in most cases. Applying greedy algorithm in TSP will result in a path that

proceeds to the nearest city (node) at every stage (Ejim, 2019). The time taken for greedy

approach increases slightly as the problem size increase based on experimental result from table

5, figure 26, figure 27, figure 28 (Ejim, 2019). Ant Colony Optimisation (ACO) simulates the

nature of ants in finding food following the trails of pheromones left earlier which evaporates by

time. Shorter distance will result in more concentrated pheromones trails as more ants are

leaving the pheromones while passing by. Local updating occur to modify the trails of

pheromones before completing the tour and then perform global updating after completing the

35
AI Methods Group Assignment CT002-3-2-AIM

tour to shorten the distance of the tour by adding an amount of pheromone trail which is

inversely proportional to the tour length (Singh, Mehta and Katiyar, 2014). The result figure 26

(Singh, Mehto and Katiyar, 2014) had proof the quality of solutions depends on the number of

ants. Ants in low number changes path faster individually whereas ants in high number leads to

higher accumulation of pheromone on edges in TSP, and thus an individual keeps the path with

higher concentration of pheromone with a high probability (Singh, Mehta and Katiyar, 2014).

ACO provides result in a short time interval having a low number of iterations. Comparing ACO

and greedy approach will give a conclusion that ACO is better than greedy approach in terms of

optimal result whereas on the other hand greedy approach is better than ACO in terms of time as

ACO runs several iterations, but greedy approach only run once.

36
AI Methods Group Assignment CT002-3-2-AIM

2.1.4 Gan Eng Hung

Table 7 Experiments results comparison (100runs for each case)

Figure 29 Main Operations of Water Flow-like Algorithm in TSP

37
AI Methods Group Assignment CT002-3-2-AIM

Figure 30 The comparison of the average computational time of WFA-TSP and ACS for
small data set.

Figure 31 The comparison of the average computational time of WFA-TSP and ACS for
large data set.
The first journal stated that using genetic algorithm (GA) to solve travelling salesman

problem (TSP) are one of the most used combinatorial optimization research so this method is

easily trap into a local optimum (Yan et al., 2012). So by using particle swarm optimization

algorithm (PSO) instead of the genetic algorithm, we can produce new results and determine

whether which algorithm is better for travelling salesman problem (TSP). It is a surprise to find

that the convergence speed of the swarm particle is higher than that of genetic algorithm as

shown in Table 1. Table 1 is the result of convergence time in terms of the best solutions in these

38
AI Methods Group Assignment CT002-3-2-AIM

100 runs. As table 1 shown, the results of swarm is better than genetic algorithm. The second

journal talked about water flow-like algorithm (WFA). The internal workings of WFA is shown

as figure 1. In terms of scalability, both WFA and ACS are scalable in small and medium size

data sets(Srour et al., 2014). As figure 2 and figure 3 shown, the computational time of both

algorithms increase almost linearly. This means as the data size gets larger, ACS computational

time increase as the problem got larger and more complex (Srour et al., 2014). For WFA, its

scalability is smoother compared to ACS. In terms of performance, WFA outperforms ACS by a

large margin when it comes to Large data sets. In conclusion WFA presents the best performance

algorithm when compared with the recent algorithm methodology are better than the others, PSO

is better than GA, WFA is better than ACS. There are enough data from both journals to support

their results thus not contradicting the data and conclusions.

Algorithm Implementation
Purpose

39
AI Methods Group Assignment CT002-3-2-AIM

As the Non-deterministic Polynomial Hard (NP-Hard) problem is difficult to solve

because of it has many solutions but one the fittest solution can be the optimal solution for

Travelling Salesman Problem (TSP), a optimization and search have to be done in order to

produce the best solution of all the previous generations of the solution. As mentioned earlier for

TSP is a combinatorial problem which has possible large space state to search and optimize all

the solutions. All solutions are competing each other because they have the common goal which

is “survive of the fittest” concept, which of the solutions have the highest fitness value and it can

be the global optimal solutions.

In Genetic Algorithm, the term of chromosome is used to indicating the solution of the

problem as it can be known as individual and inside the chromosomes consists of many genes

which means it has many possible search probability have to be done and needed to be optimized

to find the best solution. The first step of GA is initialized the amount of population that produce

randomized solution with the respect of the population. Then, next is proceed to the selection

process which means that the many of the solutions will be randomly selected two as parents and

all the remaining are following this process.

The best things of GA are it has some operators that avoid duplication of the solutions,

they are mutation operator and crossover operator. The purpose of mutation operator is changing

the subset of the solution then produce the new offspring which is distinct to the parent’s

generations. This measure is taken to avoid same solution from the previous generation, also

make the search more efficient, and maintain the diversity in population to avoid the premature

convergence. The purpose of crossover operator is the mating process is done by exchanging

some of the section of the subset of the solution from parent 1 to parent 2 and all parents are

chosen randomly. This measure is taken to generate new offspring with distinctive properties

40
AI Methods Group Assignment CT002-3-2-AIM

from the parents to produce better solution than the parents. As the result of their purposes, the

successive generations will gain more higher fitness value than the preceded generations.

All the solutions are evaluated by the fitness score. The lower fitness value is generally

given more preference to proceed the selection process until it meets the global optimal solution.

GA also reduce the local optimal solution and searched all the solutions. The bad fitness score of

the solution will be terminated because it is used to optimize the findings solution.

41
AI Methods Group Assignment CT002-3-2-AIM

Results and Discussion


3.1 Discussion on implementation

import random
import copy
import os
import time
import math
import csv

try:
from tkinter import *
from tkinter.ttk import *
except Exception as e:
print("[ERROR]: {0}".format(e))
import Tkinter

list_of_cities = []

# probability that an individual Route will mutate


k_mut_prob = 0.4

# Number of generations to run for


k_n_generations = 100
# Population size of 1 generation (RoutePop)
k_population_size = 100

# Size of the tournament selection.


tournament_size = 7

# If elitism is True, the best from one generation will carried over to the next.

Figure 32 Import modules and declare value

42
AI Methods Group Assignment CT002-3-2-AIM

First and foremost, there are few things needed to be imported which are “random” that

allow the system automatically to generate random number, “copy” to creates a new variable that

shares the reference of the original object, “os” to use operating system dependent functionality,

“time” to handle various operations regarding time, “math” for using mathematical functions,

“csv” to handle CSV files. The “try” and “catch” is mainly to put exception on the functions that

are not found. The “from” and “import” used together to allow access “tkinder” directly. The

“tkinder” is the standard Python interface to the Tk GUI toolkit. Adding the “.ttk” behind the

“tkinder” is to access to the Tk themed widget set. The “print” is a function to print text and the

condition is if error occurred. Set the variable called “list_of_cities” as the array. Set the

probability as 0.4 in the variable called “k_mus_prob.” The variable named “k_n_generation” is

the number of generations to run for which is 100 and “k_pupulation_size” is the population size

of 1 generation which is 100. The “tournament_size” is size of the tournament selection which

the size is 7. Set “elitism” as the Boolean values which is True if it is True then the best of one

generation will carried over to the next. Set “csv_cities” to False as no data to be readed. Create a

variable called “csv_name” to allow the system to find the CSV file in a later time.

Class RoutePop

43
AI Methods Group Assignment CT002-3-2-AIM

class RoutePop(object):
"""
Contains a list of route objects and provides info on them.
self.rt_pop: list of Route objects
self.size: lenth of rt_pop - specified upon __init__
self.fittest: Route() object with shortest length from self.rt_pop
self.get_fittest(): Calcualtes fittest route, sets self.fittest to it, and returns
the Route. Use if routes have changed manually.
"""

def __init__(self, size, initialise):


self.rt_pop = []
self.size = size
# If we want to initialise a population.rt_pop:
if initialise:
for x in range(0, size):
new_rt = Route()
self.rt_pop.append(new_rt)
self.get_fittest()

def get_fittest(self):
'''
self --> Route()

Figure 33 Class RoutePop


Create a function block called “__init__” and get parameters. Create “rt_pop” as array

and size equals to size. Create a if statement with the condition “initialise” is true then execute

for loop with the variable x in range where start is 0 and end is the size. Generate a variable

new_rt equals to new “Route” and add element of “new_rt” into “rt_pop.” Update the fittest

value.

Create a function block called “get_fittest” and get parameters from the “__init__.”

Create a variable “sorted_list” equals the result of sorted “rt_pop”, set “key” as variable equals

lambda arguments “x” and expression is the length of “x” and set “reverse” equals to false. Set

the fittest values equals to the element 0 of the array called “sorted_list.” Return the values of the

fittest.

44
AI Methods Group Assignment CT002-3-2-AIM

Class GA

def crossover(self, parent1, parent2):

# new child Route()


child_rt = Route()

for x in range(0, len(child_rt.route)):


child_rt.route[x] = None

# Two random integer indices of the parent1:


start_pos = random.randint(0, len(parent1.route))
end_pos = random.randint(0, len(parent1.route))

#### takes the sub-route from parent one and sticks it in itself:
# if the start position is before the end:
if start_pos < end_pos:
# do it in the start-->end order
for x in range(start_pos, end_pos):
child_rt.route[x] = parent1.route[x] # set the values to eachother
# if the start position is after the end:
elif start_pos > end_pos:
# do it in the end-->start order
for i in range(end_pos, start_pos):
child_rt.route[i] = parent1.route[i] # set the values to eachother

# Cycles through the parent2. And fills in the child_rt


# cycles through length of parent2:
for i in range(len(parent2.route)):
# if parent2 has a city that the child doesn't have yet:
if not parent2.route[i] in child_rt.route:
# it puts it in the first 'None' spot and breaks out of the loop.
for x in range(len(child_rt.route)):
if child_rt.route[x] == None:

Figure 34 def statement (crossover)


Create function block with the function named crossover and get parameters. Create a

new route called “child_rt.” A for loop will loop from the sequence 0 and return the “child_rt” as

string. Put the “child_rt” to connect to the variable “x” and “child_rt” must be equals to nothing.

The “start_pos” will be equals to the random integers of returned string called “parent1” starting

from sequence 0. “end_pos” will be equals to the random integers of returned string called

“parent1” starting from sequence 0. Generate a if statement with the condition of “start_pos”

smaller than “end_pos” then a for loop with the variable “x” in order to set the values to

“child_rt” and “parent1.” Else if the condition of if statement where “start_pos” larger than

“end_pos” then run a for loop with the variable called “i” set the values to “child_rt” and

“parent1.” The next is generate a for loop to cycle through length of “parent2.” Create an if

45
AI Methods Group Assignment CT002-3-2-AIM

statement to check whether the “parent2” have the cities that “child_rt” did not have. If the

condition is true, then create a for loop with the variable “x” and inside the loop consist another

if statement which to check whether “child_rt” is equals to none. If it is equals to none then

“child_rt” is equals to “parent2” and break the loop when all the cities are in child route. Return

the values of “child_rt .”

def mutate(self, route_to_mut):


'''
Route() --> Route()
Swaps two random indexes in route_to_mut.route. Runs k_mut_prob*100 % of the time
'''
# k_mut_prob %
if random.random() < k_mut_prob:

# two random indices:


mut_pos1 = random.randint(0, len(route_to_mut.route) - 1)
mut_pos2 = random.randint(0, len(route_to_mut.route) - 1)

# if they're the same, skip to the chase


if mut_pos1 == mut_pos2:
return route_to_mut

# Otherwise swap them:


city1 = route_to_mut.route[mut_pos1]
city2 = route_to_mut.route[mut_pos2]

Figure 35 def statement (mutate)


Create a function block called “mutate” and get the parameters. Create if statement with

the condition random numbers is smaller than the “k_mut_prob” then “mut_pos1” must equal to

random integers started from sequence 0 and return “route_to_mut” as string. “mus_pos2” also

must equal to random integers started from sequence 0 and return “route_to_mut” as string.

Create another if statement where condition is “mut_pos1” must equal to “mus_pos2.” If the

condition is true, then return the value of “route_to_mut.” Declare “city1” equal to

“route_to_mut” with the list “mut_pos1” and “city2” equal to “route_to_mut” with the list

46
AI Methods Group Assignment CT002-3-2-AIM

“mut_pose2.” Otherwise swap the “route_to_mut” with the list “mut_pos2” is equal to “city1”

and “route_to_mut” with the list “mut_pos1” equal to “city2.” Recalculate the length of

“route_to_mut” and update its length. After updating, return the values of “route_to_mut.”

def mutate_2opt(route_to_mut):
'''
Route() --> Route()
Swaps two random indexes in route_to_mut.route. Runs k_mut_prob*100 % of the time
'''
# k_mut_prob %
if random.random() < k_mut_prob:

for i in range(len(route_to_mut.route)):
for ii in range(len(route_to_mut.route)): # i is a, i + 1 is b, ii is c,
ii+1 is d
if (route_to_mut.route[i].distance_to[route_to_mut.route[i -
len(route_to_mut.route) + 1].name]
+ route_to_mut.route[ii].distance_to[
route_to_mut.route[ii - len(route_to_mut.route) +
>
route_to_mut.route[i].distance_to[route_to_mut.route[ii].name]
+ route_to_mut.route[i - len(route_to_mut.route) +
1].distance_to[
route_to_mut.route[ii - len(route_to_mut.route) +
1].name]):
c_to_swap = route_to_mut.route[ii]
b_to_swap = route_to_mut.route[i - len(route_to_mut.route) +

Figure 36 def statement (mutate_2opt)


Create function block called “mutate_2opt” and get parameters. Create an if statement

with the condition random numbers must be smaller than “k_mut_prob.” A for loop is generated

with the variable “i” and return the “route_to_mut” as string. Create another for loop with the

variable “ii” and return the “route_to_mut” as string. Create a if statement with the result

“route_to_mut” with the list that consists variable “i” will evaluate the list of the “route_to_mut”

with the list of variable “i” minus the returned string “route_to_mut” and increased by 1. Plus

“route_to_mut” with the list that consists variable “ii” evaluate to the list that consists

“route_to_mut” with the list that consist variable “ii” minus returned string “route_to_mut” and

47
AI Methods Group Assignment CT002-3-2-AIM

increased by 1. The result must more than the “route_to_mut” with the list of variables “i”

evaluate with the list “route_to_mut” with list that consists variable “ii.” Plus “route_to_mut”

with the list of variables “i” minus returned string “route_to_mut” and increased by 1 will

evaluate with the list “route_to_mut” with the list of variables “ii” minus returned string

“route_to_mut” and increased by 1. Declare a variable called “c_to_swap” is equals to

“route_to_mut” with the list of variables “ii.” Another variable called “b_to_swap” had been

declared is equals to “route_to_mut” with the list of variables “i” minus “route_to_mut” and

increased by 1. Otherwise, “route_to_mut” with the list of variables “i” minus “route_to_mut”

and increased by 1 is equals to the variable “c_to_swap.” “route_to_mut” with the list of

variables “ii” is equals to a variable called “b_to_swap”. Recalculate the length of

“route_to_mut” and update its length. Finally, returned the values of “route_to_mut.”

def tournament_select(self, population):


'''
RoutePop() --> Route()
Randomly selects tournament_size amount of Routes() from the input population.
Takes the fittest from the smaller number of Routes().
Principle: gives worse Routes() a chance of succeeding, but favours good Routes()
'''

# New smaller population (not intialised)


tournament_pop = RoutePop(size=tournament_size, initialise=False)

Figure 37 def statement (tournament_select)


Create a function block called “tournament_select” and get parameters. Create a new

smaller population called “tournament_pop.” The “tournament_pop” consists the values of the

class called “RoutePop” with the parameter which size is equals to the tournament size and

initialize must equals to false as it should not be initialized. Fill the tournament_pop with random

individuals using for loop. Create for loop with the condition where variable called “i” and stop

48
AI Methods Group Assignment CT002-3-2-AIM

the integers when “tournament_size” contains 6 integers as the default value is 7 integers. The

“rt_pop” will have the list of “tournament_pop” objects and increase the length by the values of

list of “population.” Return the fittest value of the “tournament_pop.”

def evolve_population(self, init_pop):


'''
RoutePop() --> RoutePop()
Takes a population and evolves it then returns the new population.
'''

# makes a new population:


descendant_pop = RoutePop(size=init_pop.size, initialise=True)

# Elitism offset (amount of Routes() carried over to new population)


elitismOffset = 0

# if we have elitism, set the first of the new population to the fittest of the
old
if elitism:
descendant_pop.rt_pop[0] = init_pop.fittest
elitismOffset = 1

# Goes through the new population and fills it with the child of two tournament
winners from the previous populatio
for x in range(elitismOffset, descendant_pop.size):
# two parents:
tournament_parent1 = self.tournament_select(init_pop)
tournament_parent2 = self.tournament_select(init_pop)

# A child:
tournament_child = self.crossover(tournament_parent1, tournament_parent2)

# Fill the population up with children


descendant_pop.rt_pop[x] = tournament_child

# Mutates all the routes (mutation with happen with a prob p = k_mut_prob)
for route in descendant_pop.rt_pop:
if random.random() < 0.3:

Figure 38 def statement (evolve_population)


Create a function block called “evolve_population” and get the parameters. Create a new

population named “descendant_pop” that is equals to the values of the class named “RoutePop”

where size is equals to number of the elements in “init_pop” and set initialize equals to true.

49
AI Methods Group Assignment CT002-3-2-AIM

Create a variable called “elitismOffset” and give it a value which is 0. Create an if statement with

the condition where “elitism” is equals to true then execute “descendant_pop” into array named

“rt_pop” with the values from element 0 of the array must equals to the fittest value of

“init_pop” and set the “elitismOffset” equals to 1. Create a for loop with the variable “x” in the

range that starting from the values of “elitismOffset” and once reach the number of lengths of

“descendant_pop.” Set the tournament_parent1 equals to the values of “tournament_select” and

tournament_parent2 equals to the values of “tournament_select.” Create a variable called

“tournament_child” is equals to the result of crossover using the input values of

“tournament_parent1” and “tournament_parent2.” Fill the “descendant_pop” with the values

“tournament_child.” Create another for loop with the variable “route” in population which is

“descendant_pop.” Generate an if statement where random integers must not be less than 0.3

then mutate the values of “route.” Update fittest route in the variable “descendant_pop.” Return

the values of “descendant_pop.”

50
AI Methods Group Assignment CT002-3-2-AIM

3.2 Results
To evaluate the performance of the Genetic Algorithm for the Traveling Salesman

Problem. We used python and ran the Genetic Algorithm source code with a sample of 24 cities

with a hundred generations for a few times. This is to determine if the genetic algorithm would

take the same path every time it starts and ends.

Figure 39 First Run Result

51
AI Methods Group Assignment CT002-3-2-AIM

This is the first run result in figure 39, this is the best route for the first run, the initial

length is at 2261.65 and the best run is at 1075.07. It took the first run about 20.3 seconds. The

best route is stated above.

Figure 40 Second Run Result


This is the result of the second run in figure 40. With the initial distance at 2274.65 and

the best distance at 1142.50. It took the second run about 27.6 seconds. The best route for this

run is different from the first run.

52
AI Methods Group Assignment CT002-3-2-AIM

Figure 41 Third Run Result


This is the result of the third run in figure 41. With the initial distance at 2294.97 and the

53
AI Methods Group Assignment CT002-3-2-AIM

best distance at 1059.32. It took the second run about 25.6 seconds. The best route this time is

not the same as the first and second runs.

As the three results stated, all three runs have different initial distance, best distance and

elapsed time. This concludes that the source code does not take the same path every time the

source code starts and ends by the hundredth generation. From this result we can guarantee that

the next run and it’s following runs will mostly be different. This is because the combination of

24 cities are limited and not infinite. The possible combinations of 24 cities are 16,777,215 and

will not run 16,777,215 times just to get the shortest possible route from these 24 cities. If we are

to run 16,777,215 times it would consume a lot of time and we could have used the time going

taking action rather than get the best possible route to generate. For example, I generate 3 times

and take figure 4.3 as the best possible route then I proceed to take action and assumption that I

would take 2 weeks for the distance 1142.50. Or I generate at least 16,777,215 times for the best

possible route, the generation would take at least a week to finish and get the best possible route,

but the final best distance difference would not be that large and I would still need to take 2

weeks to travel all 24 cities. It would be a waste of time and decrease my efficiency.

54
AI Methods Group Assignment CT002-3-2-AIM

55
AI Methods Group Assignment CT002-3-2-AIM

Conclusion
Trying to solve a problem is like going through every trial and error to yield acceptable

results depending on the method used. In such a way that implementing GA in TSP by utilizing

the concept of swapping the combination of candidates in many ways to get closer to or

eventually become the optimal solution. The reliability of search algorithm depends on time and

cost. There are algorithms that will always get the best result but very time consuming such as

simulated annealing, or the other way is the greedy approach. Hence, every search algorithm is

advisable to be used only if it reaches the minimum requirement of the problem.

56
AI Methods Group Assignment CT002-3-2-AIM

References
Ahmed S. Farhan, W. Z. T. F. H. A., 2015. Solving N Queen Problem using Genetic Algorithm.
Solving N Queen Problem using Genetic Algorithm, 122(International Journal of Computer
Applications (0975 – 8887) ), p. 4.

Biology, B., 2016. INTRODUCTION TO GENETICS. [Online]


Available at: https://basicbiology.net/biology-101/introduction-to-genetics

Bitannica, 2019. Selection. [Online]


Available at: https://www.britannica.com/science/selection

Božiković, M. G., 2006. Faculty of Electrical Engineering and Computing. "paralleling genetic
algorithm",.

Braune, R., Wagner, S. & Affenzeller, M., 2005. Institute of Systems Theory and Simulation
Johannes Kepler University. Applying Genetic Algorithms to the Optimization of Production
Planning in a real world Manufacturing Environment.

Cantú-Paz, E., 1998. University of Illinois at UrbanaChampaign, cantupaz@illigal.ge.uiuc.edu..


"A survey of parallel genetic algorithms, Computer Science Department and The Illinois Genetic
Algorithms Laboratory".

Dwivedi, V., Chauhan, T., Saxena, S. & Agrawal, P., 2012. National Conference on
Development of Reliable Information Systems, Techniques and Related Issues (DRISTI) 2012.
Travelling Salesman Problem using Genetic Algorithm, pp. 25-30.

Gad, A., 2018. Introduction to Optimization with Genetic Algorithm. [Online]


Available at: https://www.kdnuggets.com/2018/03/introduction-optimization-with-genetic-
algorithm.html

GeeksforGeeks, 2019. Genetic Algorithms. [Online]


Available at: https://www.geeksforgeeks.org/genetic-algorithms/

57
AI Methods Group Assignment CT002-3-2-AIM

Goldberg, D. E., 1989. Addison-Wesley Publishing Company Inc., Reading, MA, 1989. Genetic
algori-thms in search, optimization and machine learning.

Guide, n., 2019. Combinatorial Optimization. [Online]


Available at: https://neos-guide.org/content/combinatorial-optimization

Gupta, D., 2013. Solving TSP Using Various Meta-Heuristic Algorithms. Maharaja Surajmal
Institute Of Technology, New Delhi, India, 1(2), p. 22.

Gupta, M., 2013. International Journal of Digital Application & Contemporary research (Volume
1, Issue 6, January 2013). A Fast and Efficient Genetic Algorithm to Solve 0-1 Knapsack
Problem.

justscience, 2017. As men age, do they pass on genetic abnormalities to their children?. [Online]

Available at: http://www.justscience.in/articles/men-age-pass-genetic-abnormalities-


children/2017/11/11

Mallawaarachchi, V., 2017. Introduction to Genetic Algorithms — Including Example Code.


[Online]
Available at: https://towardsdatascience.com/introduction-to-genetic-algorithms-including-
example-code-e396e98d8bf3

Mandal, A., 2019. What is Genetics?. [Online]


Available at: https://www.news-medical.net/life-sciences/What-is-Genetics.aspx

MathWorks, 2019. Traveling Salesman Problem: Solver-Based. [Online]


Available at: https://www.mathworks.com/help/optim/ug/travelling-salesman-problem.html

NIH, 2019. What is a gene mutation and how do mutations occur?. [Online]
Available at: https://ghr.nlm.nih.gov/primer/mutationsanddisorders/genemutation

Philip, A., Taofiki, A. A. & Kehinde, O., 2011. (IJACSA) International Journal of Advanced
Computer Science and Applications, Vol. 2, No.1. A Genetic Algorithm for Solving Travelling
Salesman Problem, pp. 26-29.

58
AI Methods Group Assignment CT002-3-2-AIM

python, 2019. What is Python? Executive Summary. [Online]


Available at: https://www.python.org/doc/essays/blurb/

Sloane, N. J. A., 2007. The On-Line Encyclopedia of Integer Sequences id:A000170. Number of
ways of placing n non attacking queens on n x n board.

tutorialspoint, 2019. DAA - Travelling Salesman Problem. [Online]


Available at:
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_alg
orithms_travelling_salesman_problem.htm

tutorialspoint, 2019. Pycharm - Introduction. [Online]


Available at: https://www.tutorialspoint.com/pycharm/pycharm_introduction.htm

Urdhwareshe, R., 2016. Role of Python in Artificial Intelligence (AI). [Online]


Available at: https://www.cuelogic.com/blog/role-of-python-in-artificial-intelligence

Veenu Yadav, M. S., 2016. A Review Paper on Solving 0-1 knapsack Problem with Genetic
Algorithms. A Review Paper on Solving 0-1 knapsack Problem with Genetic Algorithms, 7 (2)
(Veenu Yadav et al, / (IJCSIT) International Journal of Computer Science and Information
Technologies), p. 3.

Wikipedia, 2007. Syntax (programming languages). [Online]


Available at: https://en.wikipedia.org/wiki/Syntax_(programming_languages)

Wikipedia, 2016. PyCharm. [Online]


Available at: https://en.wikipedia.org/wiki/PyCharm

Wolfe, M., 2018. What Is Crossing Over in Genetics?. [Online]


Available at: https://sciencing.com/calculate-centimorgans-8493492.html

Yan, Zhang, Luo, Li, Chen, Liu, 2012. Solve Traveling Salesman Problem Using Particle
Swarm. IJCSI International Journal of Computer Science Issues, 9(6), p. 265.

Ejim, S. (2019). Implementation of Greedy Algorithm in Travel Salesman Problem. [ebook]

59
AI Methods Group Assignment CT002-3-2-AIM

Abubakar Tafawa Balewa University, Bauchi: Samson Ejim, p.8. Available at:
https://www.researchgate.net/publication/307856959_Implementation_of_Greedy_Algorithm_in
_Travel_Salesman_Problem [Accessed 29 Jun. 2019].
Singh, G., Mehta, R. and Katiyar, S. (2014). Implementation of Travelling Salesman Problem

Using ant Colony Optimization. 3rd ed. [ebook] ABES Institute of Technology, NH-24, Vijay

Nagar, Ghaziabad (UP) 201009,: Sapna Katiyar, p.5. Available at:

https://pdfs.semanticscholar.org/8cf4/f0b9a1d1e0e9be29b97b45048c9bedd84400.pdf [Accessed

29 Jun. 2019].

Yan, X., Zhang, C., Luo, W., Li, W., Chen, W. and Liu, I. (2012). [online]

Pdfs.semanticscholar.org. Available at:

https://pdfs.semanticscholar.org/d4d3/fa4eadc8b3c4b5989960688a3f2833c984cf.pdf [Accessed

3 Aug. 2019].

Srour, A., Othman, Z. and Hamdan, A. (2014). A Water Flow-Like Algorithm for the Travelling

Salesman Problem. [online] Downloads.hindawi.com. Available at:

http://downloads.hindawi.com/archive/2014/436312.pdf [Accessed 3 Aug. 2019].

60

Das könnte Ihnen auch gefallen