-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_sort.py
165 lines (139 loc) · 5.65 KB
/
radix_sort.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from get_functions import history_point_data, getDistance, get_yymm, compute_weight
from center.meta import URL_MAP
def min_distance(history, point_data, U):
'''
MIN_DISTANCE
Type : Dictionary
Keys : International ID of each typhoon, 'i' in essay
Value: (dict) Dik, the min distance for ith typhoon to kth predicted point, for all j
{
"typhoon_international_id" : {
'point_k1': (float),
'point_k2': (float),
,...
}
}
'''
min_distance = {}
for i in point_data: # i refers to the ith historic typhoon
distance_ijk = {}
for k in U['points']:
min = 10 ** 10
for j in point_data[i]:
# calulate the minimum distance (lat_ij, long_ij, lat_k, long_k)
if getDistance(j[0], j[1], U['points'][k]['latitude'], U['points'][k]['longitude']) < min:
min = getDistance(j[0], j[1], U['points'][k]['latitude'], U['points'][k]['longitude'])
distance_ijk[k] = min
min_distance[i] = distance_ijk
print('MIN DISTANCE SUCCESS!')
return min_distance
def weight_of_all(history, point_data, U):
'''
WEIGHT_OF_ALL
Type : List
ith:
0: typhoon_international_id
1: <Route Score> sigma(k = 1, M) [1 + kw, k, 1] | for all Dik < radius
2: <Time Score> [year, month]
[
[ 'typhoon_international_id', sigma[1 + kw, k, 1], [year, month] ]
]
'''
temp = {} # key: typhoon name; Value: sigma(1 + k * w)
min_dist = min_distance(history, point_data, U)
num_predicted = len(U['points'].keys())
w = compute_weight(num_predicted) if U['parameter']['w'] == '' else U['parameter']['w']
### Part 1. Route Score
for i in point_data:
score = [0, 0, 0] # sigma[total, k, 1]
count = 1 # kth
# Whether the min distance(Dik) is less than radius
for k in U['points']:
if min_dist[i][k] < U['points'][k]['radius']:
score[2] += 1
score[1] += count
count += 1 # kth predicted
# Route score of ith typhoon
score[0] = score[2] + score[1] * w
temp[i] = score
### Part 2. Time Score
yymm_data = get_yymm(history)
weight_of_all = [ [i, temp[i], yymm_data[i]] for i in temp ]
print('WEIGHT OF ALL SUCCESS!')
print('TIME WEIGHT: ' + str(w))
return weight_of_all
def radix_sort(history, point_data, U):
'''
RADIX_SORT
Type : Dictionary
Keys : Priority of the approximate historic typhoons
Value: (dict) typhoon_international_id, name, points
{
"1":{
"id": typhoon_international_id,
"name": name of the typhoon,
"points":[
{
"latitude":lat_i1,
"longitude":lon_i1
},
{
"latitude":lat_i2,
"longitude":lon_i2
},
]
},
}
'''
import datetime
weight = weight_of_all(history, point_data, U)
### Part 1. Sort by the year (the closer, the more prior)
for i in range(len(weight) - 1, 0, -1):
for j in range(i):
if weight[j][2][0] < weight[j + 1][2][0]:
ret = weight[j]
weight[j] = weight[j + 1]
weight[j + 1] = ret
### Part 2. Sort by the month (the closer, the more prior)
month = U['parameter']['month']
month = datetime.datetime.now().month if month == '0' else int(month)
for i in range(len(weight) - 1, 0, -1):
for j in range(i):
if abs(weight[j][2][1] - month) > abs(weight[j + 1][2][1] - month):
ret = weight[j]
weight[j] = weight[j + 1]
weight[j + 1] = ret
### Part 3. Sort by the route score (the higher, the more prior), S(1 + kw) > S(k) > S(1), S = Sigma
for k in range(2, -1, -1):
for i in range(len(weight) - 1, 0, -1):
for j in range(i):
if weight[j][1][k] < weight[j + 1][1][k]:
ret = weight[j]
weight[j] = weight[j + 1]
weight[j + 1] = ret
### Part 4. output
final = {}
n = U['parameter']['n']
print() # For better layout of Showing total scores
for i in range(n):
typhoon_id = weight[i][0]
## name (zh first)
en = history[typhoon_id]['header']['name']
zh = URL_MAP[typhoon_id]['zh'] if typhoon_id in URL_MAP else en
year = URL_MAP[typhoon_id]['year'] if typhoon_id in URL_MAP else 'N/A'
link = URL_MAP[typhoon_id]['links']['cwb'] if typhoon_id in URL_MAP and URL_MAP[typhoon_id]['links'] != {} else 'N/A'
print(weight[i][1], end = ", ") ### Show the total score at local cmd
print("NAME: " + zh, end = ", ")
print("ID: " + typhoon_id, end = ", ")
print("Year: " + str(year), end = ", ")
print("link: " + link)
json_point_data = []
for j in point_data[typhoon_id]:
lat = j[0]
lon = j[1]
temp = {"latitude": lat, "longitude": lon}
json_point_data.append(temp)
final[i + 1] = { "id": typhoon_id, "name": zh, "year": year, "points": json_point_data, "links": link }
print() # For better layout of Showing total scores
print('RADIX SORT SUCCESS!')
return final