Skip to content

nikashahabi/TSP-with-genetic-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Using Genetic Algorithm to Solve the Traveling Salesman Problem

Here, I have used the Genetic Algorithm to solve the popular TSP. You will need to give the map to the geneticAlgorithm method in form of a nested list. You can change the maximum number of iterations(number of populations), population size, probability of mutation and you can also mannually set the initial population. The algorithm terminates whenever it exceeds the iteration limit, or when the average fitness of a population does not get better after creating a new generation of population. In each iteration, two individuals of the current population are chosen based on a weighted random choice. Needless to say that their probability to be chosen is based on their fitness. Fitter they are, the less is their fit attribute(cost of the salesman search) and the higher is their chance to be chosen. Next, the two selected individuals create an offspring, using partially mapped crossover. The mentioned offspring will then go through a random mutation with a small probability and is finally ready to be added to the next population.