Skip to content

Commit 7c895b4

Browse files
authored
Update Readme
1 parent 6634cd0 commit 7c895b4

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,109 @@
1-
# TSP-using-Genetic-Algorithm
1+
# TSP-using-Genetic-Algorithm.
2+
23
A basic implementation of genetic algorithm for traveling salesman problem
4+
35
Article about the notebook is published on https://medium.com/thecyphy/travelling-salesman-problem-using-genetic-algorithm-130ab957f165
6+
7+
8+
9+
---
10+
11+
**Travelling Salesman Problem using Genetic Algorithm**
12+
13+
14+
---
15+
16+
Travelling salesman problem is a combinatorial optimization problem. Which in terms of problem classification falls into NP-hard problem. A general problem of TSP is "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?". Here we will be solving this problem using a genetic algorithm in python. It's kind of basic implementation of genetic algorithm.
17+
18+
First task to import libraries.
19+
```python
20+
import numpy as np, random, operator, pandas as pd
21+
import matplotlib.pyplot as plt
22+
```
23+
Creating CityList i.e (X, Y) for every city
24+
```python
25+
cityList = []
26+
for i in range(0,25):
27+
x=int(random.random() * 200)
28+
y=int(random.random() * 200)
29+
cityList.append((x,y))
30+
```
31+
From here the genetic algorithm starts
32+
1. Creating a starting population of solution. In order to create a starting population, we need to create individual members.
33+
34+
```python
35+
def create_starting_population(size,Number_of_city):
36+
'''Method create starting population
37+
size= No. of the city
38+
Number_of_city= Total No. of the city
39+
'''
40+
population = []
41+
42+
for i in range(0,size):
43+
population.append(create_new_member(Number_of_city))
44+
45+
return population
46+
```
47+
2. Now ranking the routes i.e finding the best fit. Fitness function is being used to find the fitness of an individual route.
48+
```python
49+
def fitness(route,CityList):
50+
'''Individual fitness of the routes is calculated here
51+
route= 1d array
52+
CityList = List of the cities
53+
'''
54+
#Calculate the fitness and return it.
55+
score=0
56+
#N_=len(route)
57+
for i in range(1,len(route)):
58+
k=int(route[i-1])
59+
l=int(route[i])
60+
61+
score = score + distance(CityList[k],CityList[l])
62+
63+
64+
return score
65+
def rankRoutes(population,City_List):
66+
fitnessResults = {}
67+
for i in range(0,len(population)):
68+
fitnessResults[i] = fitness(population[i],City_List)
69+
return sorted(fitnessResults.items(), key = operator.itemgetter(1), reverse = False)
70+
```
71+
3. Selecting the best fit among the population.
72+
```python
73+
def selection(popRanked, eliteSize):
74+
selectionResults=[]
75+
result=[]
76+
for i in popRanked:
77+
result.append(i[0])
78+
for i in range(0,eliteSize): selectionResults.append(result[i])
79+
return selectionResults
80+
```
81+
4. Creating a mating pool so that the best individual among the population can breed and produce a better solution.
82+
```python
83+
def matingPool(population, selectionResults):
84+
matingpool = []
85+
for i in range(0, len(selectionResults)):
86+
index = selectionResults[i]
87+
matingpool.append(population[index])
88+
return matingpool
89+
```
90+
5. Making them breed to produce offsprings.
91+
```python
92+
def breedPopulation(mating_pool):
93+
children=[]
94+
for i in range(len(mating_pool)-1):
95+
children.append(crossover(mating_pool[i],mating_pool[i+1]))
96+
return children
97+
```
98+
6. Mutation of population
99+
```python
100+
def mutatePopulation(children,mutation_rate):
101+
new_generation=[]
102+
for i in children:
103+
muated_child=mutate(i,mutation_rate)
104+
new_generation.append(muated_child)
105+
return new_generation
106+
```
107+
7. Finally, we have one generation of routes. Repeat it until the solution converges.
108+
I have provided the necessary function that is required for a genetic algorithm.
109+
Full Notebook of the code is available here

0 commit comments

Comments
 (0)