-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuaweiCode.py
320 lines (262 loc) · 11.3 KB
/
huaweiCode.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 10 13:50:35 2019
@author: wangyang
"""
#from quene import queue
#global carqueue
import const
const.Maxvalue = 10**10
#carqueue = queue()
#(道路id,道路长度,最高限速,车道数目,起始点id,终点id,是否双向)
class edgeInformation():
def __init__(self,weigh,roadId,lanseNo):
self.weigh = weigh
self.roadId = roadId
self.lanseNo = lanseNo
class Load():
def __init__(self,Id=None,length=None,maxspeed=None,
lanseNumber=None,startNode=None,endNode=None,Flag=0):
self.Id = Id
self.length = length
self.maxspeep = maxspeed
self.lanseNumber = lanseNumber
self.startNodeID = startNode
self.endNodeID = endNode
self.Flag = Flag
self.direction = self.__getdirection()
self.lansecatId = {}
def __getlansecatId(self):
for i in range(self.lanseNumber):
self.lansecatId[str(i)] = []
def __getdirection(self):
direction = []
for i in range(self.lanseNumber):
direction.append(None)
return direction
#(结点id,道路id,道路id,道路id,道路id)
class Node():
def __init__(self,idNode,upLoadID,rightLoadID,downLoadID,leftLoadID):
self.idNode = idNode
self.upLoadID = upLoad
self.rightLoadID = rightLoad
self.downLoadID = downLoad
self.leftLoadID = leftLoad
#(车辆id,始发地、目的地、最高速度、出发时间)
class cat():
def __init__(self,Id,startNode,endNode,maxspeed,departureTime):
self.Id = Id
self.startNodeID = startNode
self.endNodeID = endNode
self.maxspeed = maxspeed
self.departureTime = departureTime
self.bestWayList = [] #save RoadId
self.bestNodeList = [] #save NodeId
self.bestTurnDirection = [] # save direction
self.bestCostTime = None
self.weightMat = None #应该存储一个结构{minedge,Load}
self.getEdgeWeight = None
self.nowadayRoadID = None
self.nowadayRoadNeedTime = None
self.nextRoadID = None
self.indexNowadayRoadID = None
self.nowspeed = None
def updataEdgeWeight(self,catQueue,AdjMat):
self.weightMat = AdjMat.copy()
def bestWay(self,allLoadDict,allNodeDict):#Dijkstra
sSet = []
vSet = []
distance = {}
forward = {}
for key in allNodeDict:
vSet.append(key)
distance[key] = self.weightMat[self.startNodeID][key].weigh
forward[key] = self.startNodeID
sSet.append(self.startNodeID)
vSet.remove(self.startNodeID)
forwardNode = self.startNodeID
while(len(vSet) != 0):
minvalue = const.Maxvalue
index = None
for key in vSet:
if minvalue > distance[key]:
minvalue = distance[key]
index = key
#forward[key] = forwardNode
#forwardNode = key
sSet.append(index)
vSet.remove(index)
for key in vSet:
if distance[key] > distance[index] + self.weightMat[index][key].weigh:
distance[key] = distance[index] + self.weightMat[index][key].weigh
forward[key] = index
self.bestCostTime = distance[endNodeID]
self.__getBestNodeList(forward)
def __getBestNodeList(self,forward):
nodeId = self.endNodeID
while(nodeId != self.startNodeID):
self.bestNodeList.append(nodeId)
nodeId = forward[nodeId]
self.bestNodeList.append(nodeId)
self.bestNodeList.reverse()
def __getBestRoadList(self):
for i in range(0,len(self.bestNodeList) - 1):
self.bestWayList.append(self.weightMat[self.bestNodeList[i]][self.bestNodeList[i+1]].roadId)
def __getDirect(self,nodeId,loadId,nowLoadId):
if nowLoadId is None:
return 'straight'
direct = ['left','straight','right']
turnDict = {'up':{'right':'left','down':'straight','left':'right'},
'right':{'up':'right','down':'left','left':'straight'},
'down':{'up':'straight','right':'right','left':'left'},
'left':{'up':'left','down':'right','left':'right'}}
key1 = self.__getRoaddirect(nodeId,loadId)
key2 = self.__getRoaddirect(nodeId,nowLoadId)
return turnDict[key1][key2]
def __getRoaddirect(self,nodeId,loadId):
if nodeId.upLoadID == loadId:
return 'up'
elif nodeId.rightLoadID == loadId:
return 'right'
elif nodeId.downLoadID == loadId:
return 'down'
elif nodeId.leftLoadID == loadId:
return 'left'
else:
assert False ,'eeror, the road not have direct'
def __getbestTurnDirection(self):
nowRoadList = self.bestWayList.copy()
nowRoadList[1:len(nowRoadList)] = self.bestWayList[0:-1]
nowRoadList[0] = None
for loadId,nodeId,nowRoadId in zip(self.bestWayList,self.bestNodeList,nowRoadList):
self.bestTurnDirection.append(self.__getDirect(nodeId,loadId,nowRoadId))
#(建立邻接矩阵)
class creatGraph():
def __init__(self,nodePath=None,loadPath=None):
self.nodePath = nodePath
self.loadPath = loadPath
self.allLoadDict = {}
self.allNodeDict = {}
def __getAllNode(self):
assert self.allNodeDict is not None , 'the load path is None, plase input correct path'
with open(self.allNodeDict,'r') as ftp:
for line in ftp:
line = line.replace('(','').replace(')','')
NodeInformation = line.split(',')
self.allNodeDict[NodeInformation[0]] = Node(NodeInformation[0],
NodeInformation[1],NodeInformation[2],
NodeInformation[3],NodeInformation[4])
return
def __getAllLoad(self):
assert self.allLoadDict is not None
with open(self.allLoadDict,'r') as ftp:
for line in ftp:
line = line.replace('(','').replace(')','')
RoadInformation = line.split(',')
self.allLoadDict[RoadInformation[0]] = Load(RoadInformation[0],
RoadInformation[1],RoadInformation[2],
RoadInformation[3],RoadInformation[4],
RoadInformation[5],RoadInformation[6])
return
def __UnicomNode(self,Node,LoadID,NodeIdList,roadList):
if self.allLoadDict[LoadID].startNodeID == Node.Id:
NodeIdList.append(self.allLoadDict[LoadID].endNodeID)
roadList.append(LoadID)
elif self.allLoadDict[LoadID].endNodeID == Node.Id and self.allLoadDict[LoadID].Flag:
NodeIdList.append(self.allLoadDict[LoadID].startNodeID)
roadList.append(LoadID)
def __getUnicomNode(self,Node):
NodeIdList = []
roadList = []
NodeIdList = self.__UnicomNode(Node,Node.UpLoadID,NodeIdList,roadList)
NodeIdList = self.__UnicomNode(Node,Node.RightLoadID,NodeIdList,roadList)
NodeIdList = self.__UnicomNode(Node,Node.DownLoadID,NodeIdList,roadList)
NodeIdList = self.__UnicomNode(Node,Node.LeftLoadID,NodeIdList,roadList)
return NodeIdList,roadList
def __getAdjacencyMatrix(self):
AdjMax = {}
for key in self.allNodeDict:
AdjMax[key] = {}
for key2 in self.allNodeDict:
AdjMax[key][key2] = []
Node = self.allNodeDict[key]
NodeIdList,roadIdList = self.__getUnicomNode(Node)
for NodeID,roadId in zip(NodeIdList,roadIdList):
AdjMax[key][NodeID].append(roadId)
#更新每辆车的当前路段,和当前路段需要的时间
class disposeCarQueue():
def __init__(self,carqueue=None,allLoadDict=None,allNodeDict=None):
self.carqueue = carqueue
self.fristUpdataRoadCat = None
self.allLoadDict = {}
self.allNodeDict = {}
self.index = None
def copy(self):
newClass = disposeCarQueue()
newClass.allLoadDict = self.allLoadDict.copy()
newClass.allNodeDict = self.allNodeDict.copy()
newClass.carqueue = self.carqueue.copy()
newClass.fristUpdataRoadCat = self.fristUpdataRoadCat
newClass.index = self.index
return newClass
#更新每辆车的当前路段,和当前路段需要的时间
class disposeCarQueue():
def __init__(self,carqueue=None,allLoadDict=None,allNodeDict=None):
self.carqueue = carqueue
self.fristUpdataRoadCat = None
self.allLoadDict = {}
self.allNodeDict = {}
self.index = None
def copy(self):
newClass = disposeCarQueue()
newClass.allLoadDict = self.allLoadDict.copy()
newClass.allNodeDict = self.allNodeDict.copy()
newClass.carqueue = self.carqueue.copy()
newClass.fristUpdataRoadCat = self.fristUpdataRoadCat
newClass.index = self.index
return newClass
def __fristUpdataRoadCat(self):
MinNowRoadcostTime = const.Maxvalue
index = -1
for i in range(0,len(self.carqueue)):
if MinNowRoadcostTime >= self.carqueue[i].nowadayRoadNeedTime:
MinNowRoadcostTime = self.carqueue[i].nowadayRoadNeedTime
index = i
self.index = index
self.fristUpdataRoadCat = self.carqueue[index]
def addCat(self,cat):
self.carqueue.append(cat)
return
def __removeCat(self,fisrtCat):
if fisrtCat.nextRoadID is None:
del self.carqueue[self.index]
return True
return False
def updataQueue(self):
for i in range(0,len(self.carqueue)):
if self.index == i:
self.__updataFisrtCat(self.carqueue[i])
else:
self.__updataOrdinaryCar(self.carqueue[i])
def __updataOrdinaryCar(self,ordinaryCat):
ordinaryCat.nowadayRoadNeedTime -= self.carqueue[self.index].nowadayRoadNeedTime
def __updataFisrtCat(self,fisrtCat):
if self.__removeCat(fisrtCat):
fisrtCat.index += 1
fisrtCat.nowadayRoadID = fisrtCat.bestWaylist[fisrtCat.index]
self.__updataspeed(fisrtCat)
if len(fisrtCat.bestWaylist) > fisrtCat.index + 1:
fisrtCat.nextRoadID = fisrtCat.bestWaylist[fisrtCat.index + 1]
else:
fisrtCat.nextRoadID = None
def __updataspeed(self,Cat):
MaxTime = self.allLoadDict[Cat.nowadayRoadID].length/...
min([self.allLoadDict[Cat.nowadayRoadID].maxspeep,Cat.maxspeed])
for i in range(0,len(self.carqueue)):
if i != self.index:
if Cat.nowadayRoadID == self.carqueue[i].nowadayRoadID:
if MaxTime < self.carqueue[i].nowadayRoadNeedTime:
MaxTime = self.carqueue[i].nowadayRoadNeedTime
Cat.nowspeed = self.allLoadDict[Cat.nowadayRoadID].length/MaxTime
Cat.nowadayRoadNeedTime = MaxTime
def __replaceLoadCatList():