-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgenTask.py
More file actions
153 lines (128 loc) · 6.02 KB
/
genTask.py
File metadata and controls
153 lines (128 loc) · 6.02 KB
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
# -*- coding: utf-8 -*-
import random
import json
import pathlib
import config
import numpy as np
"""
生成感知任务
感知任务包含以下选项:
temp1:经度、纬度、任务截止时间、所属类别(经过聚类预处理得到)
temp2:所需的传感器
temp3:任务的预算
temp4:任务所需的最少参与人数、任务所需的最多参与人数
"""
# 共计12种传感器类型,前10钟是常见的传感器,第11和第12是不常见的传感器
sensorID = list(range(1,config.sensor_num+1))
# 以一个小时作为一个任务分配周期
def genTask(file_name,flag=0):
# flag 0:txt 1:json
# 任务的经纬度从文件读取
# 使用出租车一次行驶的终点经纬度(即下客点)作为感知任务所在的位置,到达终点的时间作为感知任务的截止时间
Task = {}
if flag==0:
try:
with open( file_name, encoding='utf-8' ) as f:
lines = f.readlines( )
except FileNotFoundError:
print("文件不存在")
i=0
for line in lines:
temp1 = line.strip().split(',')
# 设置该任务需要的传感器数量
sensorNum = int(random.gauss(config.mu_sensor_num_of_task,config.sigma_sensor_num_of_task))
# 从传感器列表中随机选择sensorNum个类型的传感器
x=0
# 每个任务以0.2的概率拥有不通用的传感器11、12
spec_sensor = [11,12]
temp2 = []
if sensorNum>=2:
suiji1 = np.random.uniform( 0, 1, 2 )
for no in range(2):
if suiji1[no]<=0.2:
temp2.append(spec_sensor[no])
x = x+1
temp2 = temp2 + random.sample(sensorID[0:config.sensor_num-x],sensorNum-x)
# 完成任务所需的参与者人数,low and high
low = random.randint(config.left_need_people_low,config.right_need_people_low)
high = random.randint(config.left_need_people_high,config.right_need_people_high)
temp4 = [low,high]
# 任务的预算与完成任务所需的传感器数量成正比,但是设置了任务最低预算
price_per = random.randint(config.left_per_budget_range,config.right_per_budget_range) #不同的任务发布者对任务的预算估计不同
# 该任务的预算为
temp3 = price_per * sensorNum * low
# if temp3 < 10: temp3 = 10
task = [temp1,temp2,temp3,temp4]
Task[str(i)] = task
i = i + 1
elif flag==1:
try:
with open( file_name, encoding='utf-8' ) as f:
lines = json.load(f)
except FileNotFoundError:
print("文件不存在")
i=0
for line in lines:
temp1 = lines[str(line)]
# 设置该任务需要的传感器数量
sensorNum = int( random.gauss( config.mu_sensor_num_of_task, config.sigma_sensor_num_of_task ) )
# 从传感器列表中随机选择sensorNum个类型的传感器
x = 0
# 每个任务以0.2的概率拥有不通用的传感器11、12
spec_sensor = [11, 12]
temp2 = []
if sensorNum >= 2:
suiji1 = np.random.uniform( 0, 1, 2 )
for no in range( 2 ):
if suiji1[no] <= 0.2:
temp2.append( spec_sensor[no] )
x = x + 1
temp2 = temp2 + random.sample( sensorID[0:config.sensor_num - x], sensorNum - x )
# 完成任务所需的参与者人数,low and high
low = random.randint( config.left_need_people_low, config.right_need_people_low )
high = random.randint( config.left_need_people_high, config.right_need_people_high )
temp4 = [low, high]
# 任务的预算与完成任务所需的传感器数量成正比,但是设置了任务最低预算
price_per = random.randint( config.left_per_budget_range,
config.right_per_budget_range ) # 不同的任务发布者对任务的预算估计不同
# 该任务的预算为
temp3 = price_per * sensorNum * low
# if temp3 < 10: temp3 = 10
task = [temp1, temp2, temp3, temp4]
Task[str( i )] = task
i = i + 1
return Task
if __name__ == '__main__':
""" 生成某个小时内的任务"""
# 存储某小时任务位置的文件地址
for bai in range(65, 70):
for ge in range(1, 25):
if ge <= 9:
addr = 'd100data_end\\' + str(bai) + '0' + str(ge) + '.txt'
writename1 = 'ddd\\' + str(bai) + '0' + str(ge) + '.json'
else:
addr = 'd100data_end\\' + str(bai) + str(ge) + '.txt'
writename1 = 'ddd\\' + str(bai) + str(ge) + '.json'
# 如果构造的文件路径不存在,在继续下一个
filepathx = pathlib.Path(addr)
if not filepathx.exists():
continue
# addr = 'd100data_end\\6501.txt'
TASK = genTask(addr)
nums = len(TASK)
for i in range(nums):
print(TASK[str(i)])
# sim = [[0 for i in range(nums)] for j in range(nums)]
# for i in range(nums):
# ttemp = set(TASK[str(i)][1])
# for j in range(nums):
# if i==j:
# sim[i][j] = 0
# else:
# ttemp2 = set(TASK[str(j)][1])
# a = ttemp & ttemp2
# b = ttemp | ttemp2
# sim[i][j] = len(a) / len(b)
# print(sim)
with open( writename1, 'w', encoding='utf-8' ) as wf2:
json.dump( TASK, wf2 )