-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteManager.py
58 lines (41 loc) · 1.83 KB
/
RouteManager.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
57
58
from Route import Route
from dijkstrawrap import *
import random
# TODO: Rename to factory
class RouteManager:
INSTANCE = None
def __init__(self, stations : list):
self.stations = stations
self.graph = build_dijkstra_graph(self.stations)
RouteManager.INSTANCE = self
def get_fixed_length_route(self) -> Route:
# Always gets a route of fixed length of #total stations, but definitely has a lot of duplicate
# stations en-route. On avg gives wtt=3000, but it is the best option for crossover methods
r = Route(self)
mixed_stops = list(range(1, len(self.stations) + 1))
random.shuffle(mixed_stops)
r.route_stops = [ self.stations[x - 1] for x in mixed_stops ]
r.build_spf()
return r
def get_variable_length_route(self) -> Route:
# Generates a route between random stations, but keeps track of sations inbetween, so that we attempt
# to avoid duplicated stations. On avg gives wtt=1000, but the length of the route varies - no idea
# how to work with that
r = Route(self)
unused_stations = set(list(range(1, len(self.stations) + 1)))
def _take_one_from_unused(remove=True) -> int:
elem = random.choice(tuple(unused_stations))
if remove:
unused_stations.remove(elem)
return elem
stops = [ self.stations[_take_one_from_unused() - 1] ]
while len(unused_stations) != 0:
st_from = stops[-1]
st_to = self.stations[_take_one_from_unused(False) - 1]
info = get_path_info(self.graph, self.stations, st_from, st_to)
for s in info["stations"]:
unused_stations.discard(s.id)
stops.append(s)
r.route_stops = stops
r.build_spf()
return r