1 #!/usr/bin/env python3 |
1 #!/usr/bin/env python3 |
2 |
2 |
3 from configparser import ConfigParser |
|
4 from math import inf |
|
5 from misc import profile |
3 from misc import profile |
6 region_info = ConfigParser() |
4 regions = {} |
7 region_info.read('regions.ini') |
|
8 |
5 |
9 def simplify_name(name): |
6 def priority(region_name): |
10 return profile['replacements'].get(name, name) |
7 if region_name in regions: |
|
8 return float(regions[region_name]['priority']) |
|
9 else: |
|
10 return 0 |
11 |
11 |
12 def greatly_simplify_name(name): |
12 def simplify_name(region_name): |
13 return profile['more replacements'].get(name, simplify_name(name)) |
13 region = regions.get(region_name) |
|
14 if region: |
|
15 return region.get('short_name', region_name) |
|
16 else: |
|
17 return region_name |
14 |
18 |
15 def reduce_schedule(route, trip_length, whole = False, format = 'medium'): |
19 def reduce_schedule(route, trip_length, whole = False, format = 'medium'): |
16 priorities = profile['priorities'] |
|
17 length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
20 length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
18 have_already = set() |
21 have_already = set() |
19 i = 0 |
22 i = 0 |
20 if not route: |
23 if not route: |
21 return '' |
24 return '' |
22 while i < len(route): |
25 while i < len(route): |
23 if route[i] in profile['replacements']: |
26 region = regions.get(route[i]) |
24 route[i] = profile['replacements'][route[i]] |
27 if region and region.get('replacement'): |
25 if route[i] in have_already: |
28 route[i] = region['replacement'] |
|
29 if not route[i] or route[i] in have_already: |
26 del route[i] |
30 del route[i] |
27 else: |
31 else: |
28 have_already.add(route[i]) |
32 have_already.add(route[i]) |
29 i += 1 |
33 i += 1 |
30 from_place = route[0] |
34 from_place = route[0] |
31 destination = route[-1] |
35 destination = route[-1] |
32 reitti_arvot = {} |
36 route_weights = {} |
33 f = lambda i: i**-0.3 |
37 f = lambda i: i**-0.3 |
34 factor = 1 / max(f(i + 1) for i in range(len(route))) |
38 factor = 1 / max(f(i + 1) for i in range(len(route))) |
35 while float(priorities.get(route[-1], 0)) < 0: |
39 while priority(route[-1]) < 0: |
36 del route[-1] |
40 del route[-1] |
37 if not route: |
41 if not route: |
38 return '' |
42 return '' |
39 destination = route[-1] |
43 destination = route[-1] |
40 for i, stop in enumerate(route): |
44 for i, stop in enumerate(route): |
41 # muunna indeksi siten että myöhemmät alueet korostuvat |
45 # muunna indeksi siten että myöhemmät alueet korostuvat |
42 i = f(i + 1) * factor |
46 i = f(i + 1) * factor |
43 # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
47 # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
44 i *= float(priorities.get(stop, 1)) |
48 i *= priority(stop) |
45 reitti_arvot[stop] = i |
49 route_weights[stop] = i |
46 # nollaa lähtöpaikan arvo ettei se mitenkään tule kylttiin |
50 # nollaa lähtöpaikan arvo ettei se mitenkään tule kylttiin |
47 if from_place in reitti_arvot: |
51 if from_place in route_weights: |
48 reitti_arvot[from_place] = 0 |
52 route_weights[from_place] = 0 |
49 # varmista että destination tulee kylttiin |
53 # varmista että destination tulee kylttiin |
50 reitti_arvot[destination] = 1e10 |
54 route_weights[destination] = 1e10 |
51 # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
55 # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
52 weights = sorted([ |
56 weights = sorted([ |
53 (stop, reitti_arvot[stop], i) \ |
57 (stop, route_weights[stop], i) \ |
54 for i, stop in enumerate(route) \ |
58 for i, stop in enumerate(route) \ |
55 if reitti_arvot[stop] >= 1 |
59 if route_weights[stop] >= 1 |
56 ], key = lambda stop: -stop[1]) |
60 ], key = lambda stop: -stop[1]) |
57 if format == 'long': |
61 if format == 'long': |
58 weights = weights[:4] |
62 weights = weights[:4] |
59 elif format == 'short': |
63 elif format == 'short': |
60 weights = weights[:2] |
64 weights = weights[:2] |