Thu, 29 Mar 2018 23:55:36 +0300
updates
5 | 1 | #!/usr/bin/env python3 |
27 | 2 | |
52 | 3 | from configparser import ConfigParser |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
4 | from math import inf |
72 | 5 | from misc import profile |
52 | 6 | region_info = ConfigParser() |
7 | region_info.read('regions.ini') | |
8 | ||
27 | 9 | def simplify_name(name): |
87 | 10 | return profile['replacements'].get(name, name) |
27 | 11 | |
87 | 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'): | |
72 | 16 | priorities = profile['priorities'] |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
17 | length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
87 | 18 | have_already = set() |
19 | i = 0 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
20 | if not route: |
5 | 21 | return '' |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
22 | while i < len(route): |
72 | 23 | if route[i] in profile['replacements']: |
24 | route[i] = profile['replacements'][route[i]] | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
25 | if route[i] in have_already: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
26 | del route[i] |
5 | 27 | else: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
28 | have_already.add(route[i]) |
5 | 29 | i += 1 |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
30 | from_place = route[0] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
31 | destination = route[-1] |
5 | 32 | reitti_arvot = {} |
22 | 33 | f = lambda i: i**-0.3 |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
34 | factor = 1 / max(f(i + 1) for i in range(len(route))) |
72 | 35 | while float(priorities.get(route[-1], 0)) < 0: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
36 | del route[-1] |
87 | 37 | if not route: |
38 | return '' | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
39 | destination = route[-1] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
40 | for i, stop in enumerate(route): |
5 | 41 | # muunna indeksi siten että myöhemmät alueet korostuvat |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
42 | i = f(i + 1) * factor |
5 | 43 | # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
72 | 44 | i *= float(priorities.get(stop, 1)) |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
45 | reitti_arvot[stop] = i |
5 | 46 | # nollaa lähtöpaikan arvo ettei se mitenkään tule kylttiin |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
47 | if from_place in reitti_arvot: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
48 | reitti_arvot[from_place] = 0 |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
49 | # varmista että destination tulee kylttiin |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
50 | reitti_arvot[destination] = 1e10 |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
51 | # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
52 | weights = sorted([ |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
53 | (stop, reitti_arvot[stop], i) \ |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
54 | for i, stop in enumerate(route) \ |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
55 | if reitti_arvot[stop] >= 1 |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
56 | ], key = lambda stop: -stop[1]) |
87 | 57 | if format == 'long': |
68 | 58 | weights = weights[:4] |
87 | 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 | |
68 | 67 | else: |
68 | # enintään neljä tulee kylttiin | |
69 | weights = weights[:3] | |
87 | 70 | # if the third sign value is not significant enough, drop it |
68 | 71 | try: |
72 | if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): | |
73 | del weights[2] | |
74 | except IndexError: | |
75 | pass | |
87 | 76 | # repeat for the second sign value |
68 | 77 | try: |
78 | if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): | |
79 | del weights[1] | |
80 | except IndexError: | |
81 | pass | |
87 | 82 | # reorder to get the sign the right way around |
52 | 83 | weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
87 | 84 | # form the sign.. |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
85 | sign = [paino[0] for paino in weights] |
52 | 86 | old_sign = sign.copy() |
87 | sign = [] | |
88 | for place in old_sign: | |
89 | if place not in sign: | |
90 | sign.append(place) | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
91 | if whole: |
52 | 92 | sign = [from_place] + sign |
93 | if not sign: | |
94 | sign = [destination] | |
95 | return sign |