-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscawg_util.py
240 lines (203 loc) · 7.4 KB
/
scawg_util.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import subprocess
from os import listdir, makedirs, path
from shutil import rmtree, move
from math import log
__author__ = 'Jian Xun'
__initial__ = False
class Worker:
def __init__(self):
self.id = None
self.longitude = None
self.latitude = None
self.capacity = 1
self.activeness = 1
self.velocity = 0.25
self.min_lat = None
self.min_lon = None
self.max_lat = None
self.max_lon = None
self.reliability = 1
self.velocity = None
self.min_direction = None
self.max_direction = None
def from_general(self, params):
"""
Args:
params(list): a list of params parsed from SCAWG output file
"""
self.id = params[0]
self.latitude = params[1]
self.longitude = params[2]
self.capacity = params[3]
self.activeness = params[4]
# only use one parameter to generate a square region
self.min_lat = params[5][0]
self.min_lon = params[5][1]
self.max_lat = params[5][2]
self.max_lon = params[5][3]
self.reliability = params[6]
self.velocity = params[7]
class Task:
def __init__(self):
self.id = None
self.longitude = None
self.latitude = None
self.arrival_time = None
self.expire_time = None
self.require_answer_count = 1
self.assigned = 0
self.entropy = 0.5
self.confidence = 0.5
def from_general(self, params):
self.latitude = params[0]
self.longitude = params[1]
self.arrival_time = params[2]
self.expire_time = params[3]
self.require_answer_count = params[4]
self.confidence = params[5]
self.entropy = params[6]
def run_jar(params):
print 'run params', str(params)
subprocess.call(['java', '-jar', './SCDataGenerator.jar'] + params)
def parse_line(line):
"""
parse the line to several parts, a part can be either a string or a list.
Args:
line(str):
Returns: a list of strings or lists or both
"""
result = []
cur = 0
while cur < len(line):
if line[cur] == '[':
last = line.index(']', cur)
result.append(parse_line(line[cur + 1: last]))
cur = last + 2
else:
try:
last = line.index(';', cur)
result.append(line[cur: last])
cur = last + 1
except ValueError:
result.append(line[cur:])
cur = len(line)
for i in xrange(len(result)):
if isinstance(result[i], str) and '.' in result[i]:
result[i] = float(result[i])
if isinstance(result[i], str) and '.' not in result[i]:
result[i] = int(result[i])
return result
def generate_instance(options):
run_jar(options)
def generate_general_task_and_worker(variable_name, dist, options):
run_jar(['general'] + options)
source_dirct = 'res/output/'
dirct = dist
prefix_task = dist+'_tasks'
prefix_worker = dist+'_workers'
for option in options:
if option.startswith('instance='):
instance = int(option[9:])
if 'real' in options:
source_dirct = 'dataset/real'
dirct = 'real'
prefix_task = 'tasks'
prefix_worker = 'workers'
# save data to the specific directory
sub_dir = param_to_dir_name(variable_name, options)
parent_dir = path.join('dataset', dirct, 'task')
if not path.exists(path.join(parent_dir, sub_dir)):
makedirs(path.join(parent_dir, sub_dir))
for i in xrange(instance):
file_name = prefix_task + str(i) + '.txt'
move(path.join(source_dirct, 'task/tasks' + str(i) + '.txt'), path.join(parent_dir, sub_dir, file_name))
parent_dir = path.join('dataset', dirct, 'worker')
if not path.exists(path.join(parent_dir, sub_dir)):
makedirs(path.join(parent_dir, sub_dir))
for i in xrange(instance):
file_name = prefix_worker + str(i) + '.txt'
move(path.join(source_dirct, 'worker/workers' + str(i)+'.txt'), path.join(parent_dir, sub_dir, file_name))
def read_task_and_worker(variable_name, dist, options):
dirct = dist
prefix_task = dist + '_tasks'
prefix_worker = dist + '_workers'
for option in options:
if option.startswith('instance='):
instance = int(option[9:])
if 'real' in options:
dirct = 'real'
prefix_task = 'tasks'
prefix_worker = 'workers'
sub_dir = param_to_dir_name(variable_name, options)
tasks = []
for i in xrange(instance):
temp = []
f = open(path.join('dataset', dirct, 'task', sub_dir, prefix_task + str(i) + '.txt'), 'r')
for line in f:
task = Task()
task.from_general(parse_line(line))
temp.append(task)
f.close()
tasks.append(temp)
workers = []
for i in xrange(instance):
temp = []
f = open(path.join('dataset', dirct, 'worker', sub_dir, prefix_worker + str(i) + '.txt'), 'r')
for line in f:
worker = Worker()
worker.from_general(parse_line(line))
temp.append(worker)
f.close()
workers.append(temp)
compute_entropy(tasks, workers)
return tasks, workers
def compute_entropy(tasks, workers):
for t_ins in xrange(len(tasks)):
for task in tasks[t_ins]:
# calculate entropy for this task
total_num = 0.0
distinct = {}
for w_ins in xrange(t_ins + 1):
for worker in workers[w_ins]:
dis_lon = worker.longitude - task.longitude if worker.longitude > task.longitude\
else task.longitude - worker.longitude
dis_lat = worker.latitude - task.latitude if worker.latitude > task.latitude\
else task.latitude - worker.latitude
if dis_lon <= 0.1 and dis_lat < 0.1:
w_id = str(worker.id)
if w_id in distinct:
distinct[w_id] += 1
else:
distinct[w_id] = 1
total_num += 1
entropy = 0.0
for w_id in distinct:
pl = distinct[w_id] / total_num
entropy -= pl * log(pl)
task.entropy = entropy
# print 'entropy is', entropy
def param_to_dir_name(variable_name, options):
for option in options:
if variable_name in option:
return variable_name + '/' + option.split('=')[1].replace(' ', '')
raise Exception('cannot find variable ' + variable_name + ' in options ' + str(options))
def clear_dir():
files = listdir('./dataset')
if 'uni' in files:
rmtree('./dataset/uni')
if 'real' in files:
_files = listdir('./dataset/real')
if 'task' in _files:
rmtree('./dataset/real/task')
if 'worker' in _files:
rmtree('./dataset/real/worker')
files = listdir('./res')
if 'dataset' in files:
rmtree('./res/dataset')
if __name__ == '__main__':
line = '598;74.16063640794154;45.10879073935417;19;0.7866654507704679;[74.16063640794154;45.10879073935417;' \
'0.0;0.0]0.3106544569620695'
print parse_line(line)
worker = Worker()
worker.from_general(parse_line(line))
print worker.min_lon, worker.min_lat, worker.max_lon, worker.max_lat