-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.py
52 lines (35 loc) · 1.31 KB
/
Route.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
from dijkstra import Graph, DijkstraSPF
from dijkstrawrap import *
from Station import Station
class Route:
_counter = 0
def __init__(self, rm):
self.rm = rm
self.route_stops = []
self.spf = None
self._wtt = 0
Route._counter += 1
self.id = Route._counter
def has_station(self, station: Station) -> bool:
for s in self.route_stops:
if s != None and s.id == station.id:
return True
return False
def build_spf(self) -> None:
self.spf = DijkstraSPF(self.rm.graph, self.route_stops[0].id)
def get_wtt(self) -> float:
# Lesser wtt <=> lesser distance <=> better
# Doing some small value caching in here to avoid building a lot of graphs for re-calculating
# route's wtt
if self._wtt != 0:
return self._wtt
wtt = 0
for s in range(len(self.route_stops) - 1):
st_from = self.route_stops[s]
st_to = self.route_stops[s + 1]
wtt += get_path_info(self.rm.graph, self.rm.stations, st_from, st_to)["time"]
self._wtt = wtt
return wtt
def get_fitness(self) -> float:
# Fitness is the inverse of wtt <=> inverse of distance => greater fitness <=> better
return 1.0 / self.get_wtt()