-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomAlgorithm.py
More file actions
30 lines (23 loc) · 1.03 KB
/
RandomAlgorithm.py
File metadata and controls
30 lines (23 loc) · 1.03 KB
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
from random import randrange
from Algorithm import Algorithm
class RandomAlgorithm(Algorithm):
def __init__(self, citiesList, numberOfGenerations, edgeWeightType):
super().__init__(citiesList, numberOfGenerations, edgeWeightType)
def start(self):
super().start()
for i in range(self.numberOfGenerations):
trail = self.runRandomMethod()
self.trailsLengths.append(self.getTrailLength(trail))
return self.resultsAnalyzer.analiseResult(self.trailsLengths)
def runRandomMethod(self):
numberOfCities = len(self.citiesList)
startCityIndex = randrange(numberOfCities)
visitedCitiesIndexes = [startCityIndex]
trail = [startCityIndex]
for i in range(numberOfCities-1):
nextCityIndex = randrange(numberOfCities)
while nextCityIndex in visitedCitiesIndexes:
nextCityIndex = randrange(numberOfCities)
trail.append(nextCityIndex)
visitedCitiesIndexes.append(nextCityIndex)
return trail