Thu, 26 Oct 2017 16:54:29 +0300
Remove assumptions and added some api stuff
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 |
52 | 5 | |
27 | 6 | replacements = { |
52 | 7 | "ylioppilaskylä-itä": "ylioppilaskylä", |
8 | "ylioppilaskylä-länsi": "ylioppilaskylä", | |
9 | "jokila": "lieto", | |
10 | "piispala": "lieto", | |
11 | 'saaro': 'ruissalo', | |
12 | 'silvola': 'vahto', | |
13 | 'hemmola': 'vahto', | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
14 | 'paasniittu': 'rusko', |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
15 | 'kahari': 'rusko', |
52 | 16 | 'kemiö': 'kemiönsaari', |
17 | 'kasnäs': 'kemiönsaari', | |
18 | 'pernon telakka': 'perno', | |
55
0f1d046b3f78
Suppeampi näkymä, pikatiet ja mootoritiet
Teemu Piippo <teemu@hecknology.net>
parents:
53
diff
changeset
|
19 | 'paimion sairaala': 'paimio', |
0f1d046b3f78
Suppeampi näkymä, pikatiet ja mootoritiet
Teemu Piippo <teemu@hecknology.net>
parents:
53
diff
changeset
|
20 | 'hanhijoki': 'paimio', |
27 | 21 | } |
22 | ||
52 | 23 | region_info = ConfigParser() |
24 | region_info.read('regions.ini') | |
25 | municipalities = set(region_info.sections()) | |
26 | municipalities_per_region = {} | |
27 | ||
28 | for municipality in municipalities: | |
29 | for region in region_info[municipality].keys(): | |
30 | municipalities_per_region[region] = municipality | |
31 | ||
32 | for place_from, place_to in replacements.items(): | |
33 | municipalities_per_region[place_to] = municipalities_per_region[place_from] | |
34 | ||
27 | 35 | def simplify_name(name): |
36 | name = replacements.get(name, name) | |
37 | return name | |
38 | ||
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
39 | def reduce_schedule(route, trip_length, whole = False, long = False): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
40 | length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
41 | if not route: |
5 | 42 | return '' |
43 | have_already = set() | |
44 | i = 0 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
45 | while i < len(route): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
46 | if route[i] in replacements: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
47 | route[i] = replacements[route[i]] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
48 | if route[i] in have_already: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
49 | del route[i] |
5 | 50 | else: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
51 | have_already.add(route[i]) |
5 | 52 | i += 1 |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
53 | priorities = { |
52 | 54 | 'ihala': 150, |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
55 | 'mylly': 200, |
68 | 56 | 'kuninkoja': 150, |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
57 | 'moikoinen': 100, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
58 | 'raisio': 150, |
52 | 59 | 'kukola': 50, |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
60 | 'kaistarniemi': 75, |
68 | 61 | 'papinsaari': 80, |
52 | 62 | 'oriniemi': 50, |
63 | 'pikisaari': 50, | |
64 | 'häppilä': 20, | |
65 | 'haarla': 80, | |
66 | 'ylioppilaskylä': 50, | |
67 | 'halinen': 70, | |
68 | 'kakskerta': 200, | |
69 | 'pansio': 50, | |
68 | 70 | 'räntämäki': 25, |
71 | 'verkahovi': 25, | |
52 | 72 | 'skanssi': 200, |
73 | 'vaala': 20, | |
74 | 'varissuo': 60, | |
75 | 'kohmo': 80, | |
76 | 'kupittaa': 70, | |
59 | 77 | 'kupittaa as': 120, |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
78 | 'kauppatori': inf, |
52 | 79 | 'lentoasema': 50, |
80 | 'runosmäki': 50, | |
81 | 'lieto': 100, | |
82 | 'lieto as': 200, | |
83 | 'ilmarinen': 50, | |
84 | 'vahto': 50, | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
85 | 'rusko': 200, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
86 | 'oriketo': 50, |
52 | 87 | 'harittu': 40, |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
88 | 'jäkärlä': 150, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
89 | 'moisio': 150, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
90 | 'paattinen': 25, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
91 | 'kaarina': 200, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
92 | 'naantali': 200, |
52 | 93 | 'uittamo': 50, |
94 | 'piikkiö': 100, | |
95 | 'paimio': 100, | |
96 | 'pargas': 100, | |
97 | 'nagu': 100, | |
60 | 98 | 'yli-maaria': 60, |
52 | 99 | 'saramäki': 50, |
100 | 'tarvasjoki': 100, | |
101 | 'marttila': 100, | |
102 | 'koski tl': 100, | |
103 | 'katariina': 50, | |
68 | 104 | 'länsikeskus': 30, |
52 | 105 | 'kaanaa': 100, |
106 | 'satava': 50, | |
107 | 'suikkila': 50, | |
108 | 'raunistula': 50, | |
109 | 'räntämäki': 50, | |
110 | 'pääskyvuori': 100, | |
111 | 'logomo': 100, | |
112 | 'rymättylä': 50, | |
113 | "sauvo": 500, | |
114 | "kemiönsaari": 500, | |
115 | 'suovuori': 300, | |
116 | 'eerikvalla': 50, | |
117 | 'petäsmäki': 50, | |
118 | 'tortinmäki': 50, | |
119 | 'aura': 100, | |
120 | 'friskala': -1, | |
121 | 'merimasku': 50, | |
53 | 122 | 'turkuhalli': 100, |
123 | 'messukeskus': 100, | |
68 | 124 | 'naantalin pikatie': 25, |
125 | 'helsingin valtatie': 25, | |
126 | 'lauste': 25, | |
127 | 'pompo': 25, | |
128 | 'loukinainen': 25, | |
129 | 'tuorla': 25, | |
130 | 'satakunnantie': 25, | |
131 | 'hepokulta': 25, | |
132 | 'nättinummi': 25, | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
133 | 'nummenmäki': 10, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
134 | 'kurala': 10, |
68 | 135 | 'itäharju': 25, |
136 | 'vasaramäki': 25, | |
137 | 'pohjola': 25, | |
138 | 'iso-heikkilä': 25, | |
139 | 'patterinhaka': 25, | |
140 | 'illoinen': 50, | |
141 | 'luolavuori': 25, | |
142 | 'mäntymäki': 25, | |
143 | 'kurjenmäki': 25, | |
144 | 'kuusisto': 25, | |
145 | 'rautatieasema': 25, | |
146 | 'majakkaranta': 25, | |
147 | 'itäranta': 25, | |
148 | 'martti': 25, | |
149 | 'vähä-heikkilä': 25, | |
150 | 'särkilahti': 25, | |
151 | 'urusvuori': 25, | |
152 | 'port arthur': 25, | |
5 | 153 | } |
154 | ||
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
155 | if 'kauppatori' not in route: |
52 | 156 | priorities['länsikeskus'] = priorities['skanssi'] = priorities['kauppatori'] |
5 | 157 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
158 | from_place = route[0] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
159 | destination = route[-1] |
5 | 160 | reitti_arvot = {} |
22 | 161 | f = lambda i: i**-0.3 |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
162 | factor = 1 / max(f(i + 1) for i in range(len(route))) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
163 | while priorities.get(route[-1], 0) < 0: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
164 | del route[-1] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
165 | destination = route[-1] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
166 | for i, stop in enumerate(route): |
5 | 167 | # 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
|
168 | i = f(i + 1) * factor |
5 | 169 | # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
170 | i *= priorities.get(stop, 1) |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
171 | reitti_arvot[stop] = i |
5 | 172 | # 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
|
173 | if from_place in reitti_arvot: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
174 | reitti_arvot[from_place] = 0 |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
175 | # varmista että destination tulee kylttiin |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
176 | reitti_arvot[destination] = 1e10 |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
177 | # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
178 | weights = sorted([ |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
179 | (stop, reitti_arvot[stop], i) \ |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
180 | for i, stop in enumerate(route) \ |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
181 | if reitti_arvot[stop] >= 1 |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
182 | ], key = lambda stop: -stop[1]) |
68 | 183 | if long: |
184 | weights = weights[:4] | |
185 | else: | |
186 | # enintään neljä tulee kylttiin | |
187 | weights = weights[:3] | |
188 | # jos kolmas kylttiarvo ei ole tarpeeksi merkittävä suhteessa reitin pituuteen niin otetaan se pois | |
189 | try: | |
190 | if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): | |
191 | del weights[2] | |
192 | except IndexError: | |
193 | pass | |
194 | try: | |
195 | if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): | |
196 | del weights[1] | |
197 | except IndexError: | |
198 | pass | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
199 | # lajitellaan painoarvot uudestaan reittijärjestykseen jotta sign tulee oikeinpäin |
52 | 200 | weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
201 | # muodostetaan sign.. |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
202 | sign = [paino[0] for paino in weights] |
52 | 203 | to_place = sign[-1] |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
204 | #nearby_municipalities = {'Turku', 'Raisio', 'Naantali', 'Kaarina', 'Lieto'} |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
205 | #if from_place != to_place: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
206 | # municipality_from = municipalities_per_region.get(from_place) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
207 | # municipality_to = municipalities_per_region.get(to_place) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
208 | # if municipality_from and municipality_to and municipality_from not in nearby_municipalities | {municipality_to} and municipality_to not in nearby_municipalities: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
209 | # def convert_region(region): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
210 | # municipality = municipalities_per_region.get(region, region) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
211 | # if municipality != municipality_from: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
212 | # return municipality |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
213 | # else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
214 | # return region |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
215 | # sign = [convert_region(region) for region in sign] |
52 | 216 | old_sign = sign.copy() |
217 | sign = [] | |
218 | for place in old_sign: | |
219 | if place not in sign: | |
220 | sign.append(place) | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
221 | if whole: |
52 | 222 | sign = [from_place] + sign |
223 | if not sign: | |
224 | sign = [destination] | |
225 | return sign |