5 from misc import profile |
5 from misc import profile |
6 region_info = ConfigParser() |
6 region_info = ConfigParser() |
7 region_info.read('regions.ini') |
7 region_info.read('regions.ini') |
8 |
8 |
9 def simplify_name(name): |
9 def simplify_name(name): |
10 name = profile['replacements'].get(name, name) |
10 return profile['replacements'].get(name, name) |
11 return name |
|
12 |
11 |
13 def reduce_schedule(route, trip_length, whole = False, long = False): |
12 def greatly_simplify_name(name): |
|
13 return profile['more replacements'].get(name, simplify_name(name)) |
|
14 |
|
15 def reduce_schedule(route, trip_length, whole = False, format = 'medium'): |
14 priorities = profile['priorities'] |
16 priorities = profile['priorities'] |
15 length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
17 length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
|
18 have_already = set() |
|
19 i = 0 |
16 if not route: |
20 if not route: |
17 return '' |
21 return '' |
18 have_already = set() |
|
19 i = 0 |
|
20 while i < len(route): |
22 while i < len(route): |
21 if route[i] in profile['replacements']: |
23 if route[i] in profile['replacements']: |
22 route[i] = profile['replacements'][route[i]] |
24 route[i] = profile['replacements'][route[i]] |
23 if route[i] in have_already: |
25 if route[i] in have_already: |
24 del route[i] |
26 del route[i] |
30 reitti_arvot = {} |
32 reitti_arvot = {} |
31 f = lambda i: i**-0.3 |
33 f = lambda i: i**-0.3 |
32 factor = 1 / max(f(i + 1) for i in range(len(route))) |
34 factor = 1 / max(f(i + 1) for i in range(len(route))) |
33 while float(priorities.get(route[-1], 0)) < 0: |
35 while float(priorities.get(route[-1], 0)) < 0: |
34 del route[-1] |
36 del route[-1] |
|
37 if not route: |
|
38 return '' |
35 destination = route[-1] |
39 destination = route[-1] |
36 for i, stop in enumerate(route): |
40 for i, stop in enumerate(route): |
37 # muunna indeksi siten että myöhemmät alueet korostuvat |
41 # muunna indeksi siten että myöhemmät alueet korostuvat |
38 i = f(i + 1) * factor |
42 i = f(i + 1) * factor |
39 # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
43 # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
48 weights = sorted([ |
52 weights = sorted([ |
49 (stop, reitti_arvot[stop], i) \ |
53 (stop, reitti_arvot[stop], i) \ |
50 for i, stop in enumerate(route) \ |
54 for i, stop in enumerate(route) \ |
51 if reitti_arvot[stop] >= 1 |
55 if reitti_arvot[stop] >= 1 |
52 ], key = lambda stop: -stop[1]) |
56 ], key = lambda stop: -stop[1]) |
53 if long: |
57 if format == 'long': |
54 weights = weights[:4] |
58 weights = weights[:4] |
|
59 elif format == 'short': |
|
60 weights = weights[:2] |
|
61 # repeat for the second sign value |
|
62 try: |
|
63 if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): |
|
64 del weights[1] |
|
65 except IndexError: |
|
66 pass |
55 else: |
67 else: |
56 # enintään neljä tulee kylttiin |
68 # enintään neljä tulee kylttiin |
57 weights = weights[:3] |
69 weights = weights[:3] |
58 # jos kolmas kylttiarvo ei ole tarpeeksi merkittävä suhteessa reitin pituuteen niin otetaan se pois |
70 # if the third sign value is not significant enough, drop it |
59 try: |
71 try: |
60 if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): |
72 if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): |
61 del weights[2] |
73 del weights[2] |
62 except IndexError: |
74 except IndexError: |
63 pass |
75 pass |
|
76 # repeat for the second sign value |
64 try: |
77 try: |
65 if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): |
78 if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): |
66 del weights[1] |
79 del weights[1] |
67 except IndexError: |
80 except IndexError: |
68 pass |
81 pass |
69 # lajitellaan painoarvot uudestaan reittijärjestykseen jotta sign tulee oikeinpäin |
82 # reorder to get the sign the right way around |
70 weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
83 weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
71 # muodostetaan sign.. |
84 # form the sign.. |
72 sign = [paino[0] for paino in weights] |
85 sign = [paino[0] for paino in weights] |
73 to_place = sign[-1] |
|
74 old_sign = sign.copy() |
86 old_sign = sign.copy() |
75 sign = [] |
87 sign = [] |
76 for place in old_sign: |
88 for place in old_sign: |
77 if place not in sign: |
89 if place not in sign: |
78 sign.append(place) |
90 sign.append(place) |