-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
176 lines (164 loc) · 9.2 KB
/
main.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
#本程序作用:数学解题过程的直接操作,同时也是整个题目模型求解的程序入口
import DataOperate
import Convert
import os
import shutil
'''
import threading
class Thread_canReturn(threading.Thread): #继承threading.Thread类增添方法使得能获取多线程工作函数返回的值
def __init__(self,target,args):
threading.Thread.__init__(self)
self.Target = target
self.Args = args
def run(self):
self.result = self.Target(self.Args)
def get_result(self):
return self.result
#工作优化操作
def MultithreadingWork(FunctionWork,conditionMin,conditionMax,threadAmount): #计算机底层多线程规划任务,让数据集生成更快
if(len(conditionMax) != len(conditionMin)):#检查生成条件极值的对称性
return False
else:
parameterLen = len(conditionMin)
conditionStep = [] #根据线程数将生成条件分段
parameterID = 0
while(parameterID < parameterLen):
conditionStep.append(int((conditionMax[parameterID] - conditionMin[parameterID])/threadAmount)) #将数据集生成条件依据线程数进行分段,平均每段的生成条件区间长度
parameterID += 1
conditionData = [] #将数据集分段出多个子数据集的数据集
conditionParameter = [] #单位生成条件集
#数据集生成条件平均分段给不同线程进行同时工作运算生成数据集
threadIndex = 1 #线程索引
while(threadIndex <= threadAmount):
if(threadIndex == 1):
parameterID = 0
conditionParameter_ini0 = []
conditionParameter_ini1 = []
while (parameterID < parameterLen):
conditionParameter_ini0.append(conditionMin[parameterID])
conditionParameter_ini1.append(conditionMin[parameterID] + conditionStep[parameterID])
parameterID += 1
conditionParameter.append(conditionParameter_ini0)
conditionParameter.append(conditionParameter_ini1)
elif(threadIndex < threadAmount and threadIndex != 1):
parameterID = 0
while(parameterID < parameterLen):
conditionParameter[0][parameterID] = int(conditionParameter[0][parameterID]) + conditionStep[parameterID]
conditionParameter[1][parameterID] = int(conditionParameter[0][parameterID]) + conditionStep[parameterID]
parameterID += 1
elif(threadIndex == threadAmount):
parameterID = 0
while (parameterID < parameterLen):
conditionParameter[0][parameterID] = int(conditionParameter[0][parameterID]) + conditionStep[parameterID]
conditionParameter[1][parameterID] = conditionMax[parameterID]
parameterID += 1
conditionData.append(conditionParameter)
exec("threadWork{} = Thread_canReturn(target=FunctionWork,args=(conditionData[{}][0],conditionData[{}][1]))".format(threadIndex,threadIndex - 1,threadIndex - 1))
exec("threadWork{}.start()".format(threadIndex))
print("线程" + str(threadIndex) + "已开始工作")
threadIndex += 1
# 等待全部线程工作完毕
threadIndex = threadAmount #线程索引
while(threadIndex > 0):
exec("threadWork{}.join()".format(threadIndex))
print("线程" + str(threadIndex) + "已结束工作")
threadIndex -= 1
*
#轮询等待全部线程工作完毕
threadIndex = 1 # 线程索引
threadStatus_total = 0 #线程工作状态
while(threadStatus_total < threadAmount):
if(threadIndex > threadAmount):
threadIndex = 1
exec("threadStatus = threadWork{}.is_alive()".format(threadIndex))
print("线程" + str(threadIndex) + "工作状态:" + str(threadStatus))
if(threadStatus == False):
threadStatus_total += 1
print("线程" + str(threadIndex) + "已结束工作")
threadIndex += 1
*
#获取线程输出结果并对结果进行处理
threadIndex = 1
threadResult_total = [] #线程工作输出结果集
while(threadIndex <= threadAmount):
exec("threadResult = threadWork{}.get_result()".format(threadIndex))
threadResult_total.append(threadResult)
threadIndex += 1
threadIndex = 1 # 线程索引
threadResultTotal = [] #线程工作输出结果合并集
for threadResultEach in threadResult_total:
threadResultTotal += threadResultEach
print("正在合并线程" + str(threadIndex) + "输出结果")
threadIndex += 1
print("所有线程输出结果合并完成!")
return threadResultTotal
'''
#数学解题步骤
def Op_TotalBanCoordinate(): #生成不能建站的坐标集并输出为CSV数据集文件
existingStationList = Convert.CsvToList("./TopicData/附件2 现网站址坐标(筛选).csv")
totalBanCoordinate = DataOperate.TotalBanCoordinate(existingStationList)
Convert.ListToCsv("./Data","不能建站的坐标集.csv",['x','y'],totalBanCoordinate)
def Op_TotalCoordinate(): #生成题目环境全部坐标集并输出为CSV数据集文件
#MultithreadingWork(DataOperate.TotalCoordinate,(0,0),(20,20),10) 多线程任务规划算庞大数据集更快
totalCoordinate = DataOperate.TotalCoordinate()
Convert.ListToCsv("./Data","题目环境全部坐标集.csv",['x','y'],totalCoordinate)
def Op_AbleCoordinate(): #生成可以建站的坐标集并输出为CSV数据集文件
totalPosition = Convert.CsvToList("./Data/题目环境全部坐标集.csv")
banPosition = Convert.CsvToList("./Data/不能建站的坐标集.csv")
filterBanCoordinate = DataOperate.FilterCoordinate(totalPosition,banPosition) #过滤掉不能建站的坐标集
weakCoverCoordinate = Convert.CsvToList("./TopicData/附件1 弱覆盖栅格数据(筛选).csv")
ableCoordinate = DataOperate.AbleCoordinate(filterBanCoordinate,weakCoverCoordinate)
Convert.ListToCsv("./Data","可以建站的坐标集.csv",['x','y','traffic'],ableCoordinate)
def Op_MeetConditionCoordinate(): #生成满足题目条件的建站选址坐标集并输出为CSV数据集文件
ableCoordinate = Convert.CsvToList("./Data/可以建站的坐标集.csv")
weakCoverCoordinate = Convert.CsvToList("./TopicData/附件1 弱覆盖栅格数据(筛选).csv")
#算出全部弱覆盖区域的业务量总和
totalTraffic = 0
for weakPosition in weakCoverCoordinate:
totalTraffic = float(totalTraffic) + float(weakPosition[2])
#生成全部基站为宏基站且满足条件的建站选址坐标集 - 大方案一
indexDatum = 0
id = 0
while(indexDatum <= len(ableCoordinate)):
indexDatumNext = indexDatum + 1
while(indexDatumNext <= len(ableCoordinate)):
meetCondition = DataOperate.MeetConditionCoordinate_highCost(ableCoordinate,weakCoverCoordinate,totalTraffic,indexDatum,indexDatumNext)
if(meetCondition != False):
sumCost = meetCondition[0]
Convert.ListToCsv("./StationData/highCostPlan","ID" + id + "-COST-" + sumCost + ".csv",['x','y'],meetCondition[1])
id += 1
indexDatumNext += 1
indexDatum += 1
DataOperate.CostSort("./StationData/highCostPlan")
#生成全部基站为微基站且满足条件的建站选址坐标集 - 大方案二
indexDatum = 0
id = 0
while (indexDatum <= len(ableCoordinate)):
indexDatumNext = indexDatum + 1
while (indexDatumNext <= len(ableCoordinate)):
meetCondition = DataOperate.MeetConditionCoordinate_lowCost(ableCoordinate, weakCoverCoordinate,
totalTraffic, indexDatum, indexDatumNext)
if (meetCondition != False):
sumCost = meetCondition[0]
Convert.ListToCsv("./StationData/lowCostPlan","ID" + id + "-COST-" + sumCost + ".csv", ['x', 'y'], meetCondition[1])
id += 1
indexDatumNext += 1
indexDatum += 1
DataOperate.CostSort("./StationData/lowCostPlan")
#生成从全部基站为宏基站且满足条件的建站选址坐标集中逐步替换插入微基站的坐标集 - 大方案三
def Op_SortLeastCost(): #从每个方案排序选出最低成本的建站选址坐标集,并在不同大方案中排序选出最优方案
pureHighCost = DataOperate.CostSort("./StationData/highCostPlan")
for plan in pureHighCost:
shutil.copy("./StationData/highCostPlan/" + plan,"./StationData/FeaturedPlan")
pureLowCost = DataOperate.CostSort("./StationData/lowCostPlan")
for plan in pureLowCost:
shutil.copy("./StationData/lowCostPlan/" + plan,"./StationData/FeaturedPlan")
featured = DataOperate.CostSort("./StationData/FeaturedPlan")
for plan in featured:
shutil.copy("./StationData/lowCostPlan/" + plan,"./StationData/BestPlan")
if __name__ == '__main__':
Op_TotalBanCoordinate()
Op_TotalCoordinate()
Op_AbleCoordinate()
Op_MeetConditionCoordinate()
Op_SortLeastCost()