def buildTrafficLightsDictionary(mtraci): """ Returns a dictionary as {Key=edgeId, Value=traffic light ID which is located at the end of the edge} """ Logger.info("{}Building traffic lights dictionary...".format( constants.PRINT_PREFIX_TLL)) previousEdge = '' tllDict = dict() mtraci.acquire() tlls = traci.trafficlights.getIDList() mtraci.release() for tll in tlls: mtraci.acquire() controlledLanes = traci.trafficlights.getControlledLanes(tll) mtraci.release() for lane in controlledLanes: currentEdge = getEdgeFromLane(lane) if currentEdge != previousEdge: tllDict[currentEdge] = tll previousEdge = currentEdge Logger.info("{}Done".format(constants.PRINT_PREFIX_TLL)) return tllDict
def isRouteValidForTraCI(mtraci, route): """ Returns true if the given route is linked by SUMO lanes, false else """ for i in range(0, len(route) - 1): inEdge = route[i] outEdge = route[i + 1] inLaneIndex = 0 inLane = getFirstLaneFromEdge(inEdge) found = False while not found: try: mtraci.acquire() lanesOut = traci.lane.getLinks(inLane) mtraci.release() except: mtraci.release() return False laneOutIndex = 0 while laneOutIndex < len(lanesOut) and not found: outLane = lanesOut[laneOutIndex][0] if getEdgeFromLane(outLane) == outEdge: found = True else: laneOutIndex += 1 if not found: inLaneIndex += 1 inLane = inLane[:len(inLane) - 1] + str(inLaneIndex) return True
def getOutLane(mtraci, inLane, outEdge): """ Returns the (out) lane linked with the (in) lane which match with the (out) edge received If no lane match, the algorithm will use as inLane the next lane on the inEdge. If no lane match and if there is no other lane available on the inEdge, -1 will be returned """ found = False laneIndex = 0 laneOutIndex = 0 while not found: try: mtraci.acquire() lanesOut = traci.lane.getLinks(inLane) mtraci.release() except: mtraci.release() return -1 while laneOutIndex < len(lanesOut) and not found: outLane = lanesOut[laneOutIndex][0] if getEdgeFromLane(outLane) == outEdge: found = True else: laneOutIndex += 1 if not found: laneIndex += 1 laneOutIndex = 0 inLane = inLane[:len(inLane) - 1] + str(laneIndex) return outLane
def buildTrafficLightsDictionary(mtraci): """ Returns a dictionary as {Key=edgeId, Value=traffic light ID which is located at the end of the edge} """ Logger.info("{}Building traffic lights dictionary...".format(constants.PRINT_PREFIX_TLL)) previousEdge = '' tllDict = dict() mtraci.acquire() tlls = traci.trafficlights.getIDList() mtraci.release() for tll in tlls: mtraci.acquire() controlledLanes = traci.trafficlights.getControlledLanes(tll) mtraci.release() for lane in controlledLanes: currentEdge = getEdgeFromLane(lane) if currentEdge != previousEdge: tllDict[currentEdge] = tll previousEdge = currentEdge Logger.info("{}Done".format(constants.PRINT_PREFIX_TLL)) return tllDict
def updateTllForPriorityVehicles(mtraci, priorityVehicles, mPriorityVehicles, tllDict, yellowTllDict, managedTllDict): """ Determines for each priority vehicle which are the next traffic lights and the distance to these ones. Regarding to the vehicle current speed and the time elapsed during a SUMO simulation step, the traffic light hidden lane the vehicle will cross is set to green or the junction to a temporary orange state if the vehicle is close enough and if the concurrent access between priority vehicles allows it. Note: the following algorithm could be highly improved by exploring every edges the vehicle will use at the beginning, but then only explore the edges which are now in the vehicle scope because of the last step progression. Two list, one for the orange look up and one for the green, would be used for this purpose. Note 2: The main complexity of this algorithm resides in the lack of TraCI functionalities in order to get retrieve the state of a hidden lane linking two lanes. This could also be highly improved by using a dictionary built when starting the software. """ mPriorityVehicles.acquire() managedTlls = [] length = len(priorityVehicles) # Checking if the next traffic light has to be changed for each priority vehicleId in the simulation for prioVehIndex in range(0, length): vehicleId = priorityVehicles[prioVehIndex] # Getting route and position information for the current priority vehicleId mtraci.acquire() route = traci.vehicle.getRoute(vehicleId) currentLane = traci.vehicle.getLaneID(vehicleId) mtraci.release() if currentLane != '' and not isJunction(currentLane): # Getting speed information for space and time predictions mtraci.acquire() lanePosition = traci.vehicle.getLanePosition(vehicleId) laneLength = traci.lane.getLength(currentLane) mtraci.release() currentEdge = getEdgeFromLane(currentLane) currentEdgeIndex = route.index(currentEdge) remainingLength = laneLength - lanePosition edge = currentEdge lane = currentLane edgeIndex = currentEdgeIndex # Browsing the next edges the vehicleId will go to while remainingLength <= constants.YELLOW_LENGTH_ANTICIPATION and edgeIndex < len( route) - 1: # If the current edge (the vehicleId is not) ends with a traffic light if edge in tllDict: # If the car is close enough for the traffic light to become green if remainingLength <= constants.GREEN_LENGTH_ANTICIPATION: setState = constants.SET_GREEN # If the car is close enough for the traffic light to prepare (temporary state) becoming green elif not tllDict[edge] in yellowTllDict: setState = constants.SET_YELLOW else: managedTlls.append(tllDict[edge]) setState = constants.IGNORE # Calculating the next lane the vehicleId will go to outEdge = route[edgeIndex + 1] outLane = getOutLane(mtraci, lane, outEdge) # Calling for a traffic light change if outLane != -1 and setState != constants.IGNORE: tllId = tllDict[edge] managedTlls.append(tllId) if (tllId in managedTllDict and managedTllDict[tllId][1] > remainingLength ) or not tllId in managedTllDict: managedTllDict[tllId] = (vehicleId, remainingLength) changeState(mtraci, tllId, lane, outLane, setState, yellowTllDict) edgeIndex += 1 if edgeIndex < len(route): edge = route[edgeIndex] lane = getFirstLaneFromEdge(edge) mtraci.acquire() laneLength = traci.lane.getLength(lane) mtraci.release() remainingLength += laneLength # Removing the tlls which have been crossed from the managedTllDict for key in managedTllDict.keys(): if managedTllDict[key][ 0] == vehicleId and not key in managedTlls: del managedTllDict[key] managedTlls[:] = [] mPriorityVehicles.release()
def updateTllForPriorityVehicles(mtraci, priorityVehicles, mPriorityVehicles, tllDict, yellowTllDict, managedTllDict): """ Determines for each priority vehicle which are the next traffic lights and the distance to these ones. Regarding to the vehicle current speed and the time elapsed during a SUMO simulation step, the traffic light hidden lane the vehicle will cross is set to green or the junction to a temporary orange state if the vehicle is close enough and if the concurrent access between priority vehicles allows it. Note: the following algorithm could be highly improved by exploring every edges the vehicle will use at the beginning, but then only explore the edges which are now in the vehicle scope because of the last step progression. Two list, one for the orange look up and one for the green, would be used for this purpose. Note 2: The main complexity of this algorithm resides in the lack of TraCI functionalities in order to get retrieve the state of a hidden lane linking two lanes. This could also be highly improved by using a dictionary built when starting the software. """ mPriorityVehicles.acquire() managedTlls = []; length = len(priorityVehicles) # Checking if the next traffic light has to be changed for each priority vehicleId in the simulation for prioVehIndex in range(0, length): vehicleId = priorityVehicles[prioVehIndex] # Getting route and position information for the current priority vehicleId mtraci.acquire() route = traci.vehicle.getRoute(vehicleId) currentLane = traci.vehicle.getLaneID(vehicleId) mtraci.release() if currentLane != '' and not isJunction(currentLane): # Getting speed information for space and time predictions mtraci.acquire() lanePosition = traci.vehicle.getLanePosition(vehicleId) laneLength = traci.lane.getLength(currentLane) mtraci.release() currentEdge = getEdgeFromLane(currentLane) currentEdgeIndex = route.index(currentEdge) remainingLength = laneLength - lanePosition edge = currentEdge lane = currentLane edgeIndex = currentEdgeIndex # Browsing the next edges the vehicleId will go to while remainingLength <= constants.YELLOW_LENGTH_ANTICIPATION and edgeIndex < len(route) - 1: # If the current edge (the vehicleId is not) ends with a traffic light if edge in tllDict: # If the car is close enough for the traffic light to become green if remainingLength <= constants.GREEN_LENGTH_ANTICIPATION: setState = constants.SET_GREEN # If the car is close enough for the traffic light to prepare (temporary state) becoming green elif not tllDict[edge] in yellowTllDict: setState = constants.SET_YELLOW else: managedTlls.append(tllDict[edge]) setState = constants.IGNORE # Calculating the next lane the vehicleId will go to outEdge = route[edgeIndex + 1] outLane = getOutLane(mtraci, lane, outEdge) # Calling for a traffic light change if outLane != -1 and setState != constants.IGNORE: tllId = tllDict[edge] managedTlls.append(tllId) if (tllId in managedTllDict and managedTllDict[tllId][1] > remainingLength) or not tllId in managedTllDict: managedTllDict[tllId] = (vehicleId, remainingLength) changeState(mtraci, tllId, lane, outLane, setState, yellowTllDict) edgeIndex += 1 if edgeIndex < len(route): edge = route[edgeIndex] lane = getFirstLaneFromEdge(edge) mtraci.acquire() laneLength = traci.lane.getLength(lane) mtraci.release() remainingLength += laneLength # Removing the tlls which have been crossed from the managedTllDict for key in managedTllDict.keys(): if managedTllDict[key][0] == vehicleId and not key in managedTlls: del managedTllDict[key] managedTlls[:] = [] mPriorityVehicles.release()