-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNearestNeighborHeuristic.py
56 lines (45 loc) · 1.5 KB
/
NearestNeighborHeuristic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from City import *
import random
import sys
class NearestNeighborHeuristic():
def __init__(self, cities):
self.cities = cities
self.numberOfCities = len(cities)
def GenerateSolutions(self, numberOfSolutions):
solutions = []
for i in range(numberOfSolutions):
solution = []
isCityAdded = []
firstCityIndex = 0
lastCityAdded = None
self.InitTabWithValue(isCityAdded, self.numberOfCities, False)
firstCityIndex = random.randint(0, self.numberOfCities - 1)
# solution.append(self.cities[firstCityIndex])
solution.append(firstCityIndex)
lastCityAdded = self.cities[firstCityIndex]
isCityAdded[firstCityIndex] = True
while self.AllCitiesAdded(isCityAdded) == False:
bestDistance = sys.maxsize
tmpDistance = 0
bestCityIndex = 0
for i in range(self.numberOfCities):
if isCityAdded[i] == False:
tmpDistance = self.cities[i].GetDistanceToCity(lastCityAdded)
if tmpDistance < bestDistance:
bestDistance = tmpDistance
bestCityIndex = i
# solution.append(self.cities[bestCityIndex])
solution.append(bestCityIndex)
lastCityAdded = self.cities[bestCityIndex]
isCityAdded[bestCityIndex] = True
#solution.append(self.cities[firstCityIndex])
solutions.append(solution)
return solutions
def AllCitiesAdded(self, cities):
for x in cities:
if(x == False):
return False
return True
def InitTabWithValue(self, tab, tabLength, value):
for i in range(tabLength):
tab.append(value)