-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithm.py
46 lines (35 loc) · 1.73 KB
/
Algorithm.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
import math
from ResultsAnalyzer import ResultsAnalyzer
class Algorithm:
def __init__(self, citiesList, numberOfGenerations, edgeWeightType):
self.citiesList = citiesList
self.numberOfGenerations = int(numberOfGenerations)
self.edgeWeightType = edgeWeightType
self.trailsLengths = []
self.resultsAnalyzer = ResultsAnalyzer()
def start(self):
pass
def getTrailLength(self, trail):
distance = 0.0
if self.edgeWeightType == 'GEO':
for i in range(len(trail) - 1):
lat1 = self.citiesList[trail[i]][0]
lon1 = self.citiesList[trail[i]][1]
lat2 = self.citiesList[trail[i + 1]][0]
lon2 = self.citiesList[trail[i + 1]][0]
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dlat = math.radians(lat2 - lat1)
dlon = math.radians(lon2 - lon1)
a = math.sin(dlat / 2) ** 2 + \
math.cos(phi1) * math.cos(phi2) * math.sin(dlon / 2) ** 2
distance += 2 * 6373 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
else:
numberOfCities = len(self.citiesList)
for i in range(numberOfCities - 1):
distance += math.sqrt(
abs(self.citiesList[trail[i]][0] - self.citiesList[trail[i + 1]][0]) ** 2 + abs(
self.citiesList[trail[i]][1] - self.citiesList[trail[i + 1]][1]) ** 2)
distance += math.sqrt(
abs(self.citiesList[trail[0]][0] - self.citiesList[trail[numberOfCities - 1]][0]) ** 2 + abs(
self.citiesList[trail[0]][1] - self.citiesList[trail[numberOfCities - 1]][1]) ** 2)
return distance