示例#1
0
    def checkMaxGreenAndYellowPhaseRule(self, tl: TrafficLight,
                                        nextRule: Rule) -> bool:
        if "G" in traci.trafficlight.getPhaseName(tl.getName()):
            if tl.getTimeInCurrentPhase() >= self.maxGreenPhaseTime:
                if traci.trafficlight.getPhase(
                        tl.getName()) >= (len(tl.getPhases()) - 2):
                    traci.trafficlight.setPhase(tl.getName(), 0)
                    return True
                else:
                    traci.trafficlight.setPhase(
                        tl.getName(),
                        traci.trafficlight.getPhase(tl.getName()) + 1)
                    return True
            else:
                #traci.trafficlight.setPhase(tl.getName(), nextRule.getAction())
                tl.updateTimeInCurrentPhase(5)

        elif "Y" in traci.trafficlight.getPhaseName(tl.getName()):
            if tl.getTimeInCurrentPhase() >= self.maxYellowPhaseTime:
                if traci.trafficlight.getPhase(
                        tl.getName()) >= (len(tl.getPhases()) - 2):
                    traci.trafficlight.setPhase(tl.getName(), 0)
                    return True

                else:
                    traci.trafficlight.setPhase(
                        tl.getName(),
                        traci.trafficlight.getPhase(tl.getName()) + 1)
                    return True
            else:
                tl.updateTimeInCurrentPhase(5)
        else:
            return False
示例#2
0
 def checkMaxRedPhaseTimeRule(self, tl: TrafficLight) -> bool:
     if tl.maxRedPhaseTimeReached() is not False:
         traci.trafficlight.setPhase(tl.getName(),
                                     tl.maxRedPhaseTimeReached())
         return True
     else:
         return False
示例#3
0
    def applyUserDefinedRuleAction(self, trafficLight: TrafficLight,
                                   currPhaseName: str, rule: Rule) -> None:
        # If max green phase time reached, switch phase to yellow in same direction
        if rule.getConditions()[0] == "maxGreenPhaseTimeReached":
            currPhase = traci.trafficlight.getPhaseName(trafficLight.getName())
            currPhase[5] = "Y"
            traci.trafficlight.setPhase(trafficLight.getName(), currPhase)

        # If max yellow phase time reached, switch to next phase in the schedule
        elif rule.getConditions()[0] == "maxYellowPhaseTimeReached":
            if traci.trafficlight.getPhase(trafficLight.getName()) >= (
                    len(trafficLight.getPhases()) - 2):
                traci.trafficlight.setPhase(trafficLight.getName(), 0)
            else:
                traci.trafficlight.setPhase(
                    trafficLight.getName(),
                    traci.trafficlight.getPhase(trafficLight.getName()) + 1)
    def getPredicateParameters(
        self, trafficLight: TrafficLight, predicate: str
    ) -> Union[float, int, str, bool, List[str, float, int], List[str]]:
        if "longestTimeWaitedToProceedStraight" == predicate:
            # Find max wait time for relevant intersection
            maxWaitTime: float = 0
            # Retrieve state of specified intersection
            state = self.getState(trafficLight)
            for lane in state:
                if lane in trafficLight.getLanes():
                    for veh in state[lane]:
                        if "_Stopped_S" in veh:
                            vehIDSplit = veh.split("_")
                            vehID = vehIDSplit[0]
                            if self.vehicleWaitingTimes[vehID] > maxWaitTime:
                                maxWaitTime = self.vehicleWaitingTimes[vehID]
            return maxWaitTime

        elif "longestTimeWaitedToTurnLeft" == predicate:
            # Find max wait time for relevant intersection
            maxWaitTime: float = 0
            # Retrieve state of specified intersection
            state = self.getState(trafficLight)
            for lane in state:
                if lane in trafficLight.getLanes():
                    for veh in state[lane]:
                        if "_Stopped_L" in veh:
                            vehIDSplit = veh.split("_")
                            vehID = vehIDSplit[0]
                            if self.vehicleWaitingTimes[vehID] > maxWaitTime:
                                maxWaitTime = self.vehicleWaitingTimes[vehID]
            return maxWaitTime

        elif "numCarsWaitingToProceedStraight" == predicate:
            carsWaiting: int = 0
            # Retrieve state of specified intersection
            state = self.getState(trafficLight)
            for lane in state:
                if lane in trafficLight.getLanes():
                    for veh in state[lane]:
                        if "_Stopped_S" in veh:
                            vehIDSplit = veh.split("_")
                            vehID = vehIDSplit[0]
                            if self.vehicleWaitingTimes[vehID] > 0:
                                carsWaiting += 1
            return carsWaiting

        elif "numCarsWaitingToTurnLeft" == predicate:
            carsWaiting: int = 0
            # Retrieve state of specified intersection
            state = self.getState(trafficLight)
            for lane in state:
                if lane in trafficLight.getLanes():
                    for veh in state[lane]:
                        if "_Stopped_L" in veh:
                            vehIDSplit = veh.split("_")
                            vehID = vehIDSplit[0]
                            if self.vehicleWaitingTimes[vehID] > 0:
                                carsWaiting += 1

            return carsWaiting

        elif "timeSpentInCurrentPhase" == predicate:
            return traci.trafficlight.getPhaseDuration(trafficLight.getName())

        elif ("verticalPhaseIs" in predicate
              or "horizontalPhaseIs" in predicate
              or "northSouthPhaseIs" in predicate
              or "southNorthPhaseIs" in predicate
              or "eastWestPhaseIs" in predicate
              or "westEastPhaseIs" in predicate):
            return traci.trafficlight.getPhaseName(
                trafficLight.getName()).split("_")

        elif "maxGreenPhaseTimeReached" == predicate:
            parameters: List[str, float, int] = []
            parameters.append(
                traci.trafficlight.getPhaseName(trafficLight.getName()))

            # Get phase (G or Y) from phase name
            getPhase = parameters[0].split("_")
            parameters[0] = getPhase[2]

            parameters.append(
                traci.trafficlight.getPhaseDuration(trafficLight.getName()) -
                (traci.trafficlight.getNextSwitch(trafficLight.getName()) -
                 traci.simulation.getTime()))
            parameters.append(self.maxGreenPhaseTime)

            return parameters

        elif "maxYellowPhaseTimeReached" == predicate:
            parameters = []
            parameters.append(
                traci.trafficlight.getPhaseName(
                    trafficLight.getName()))  # Get traffic light phase name

            # Get phase (G or Y) from phase name
            getPhase = parameters[0].split("_")
            parameters[0] = getPhase[2]

            parameters.append(
                traci.trafficlight.getPhaseDuration(trafficLight.getName()) -
                (traci.trafficlight.getNextSwitch(trafficLight.getName()) -
                 traci.simulation.getTime()))
            parameters.append(self.maxYellowPhaseTime)

            return parameters

        elif "EVDistanceToIntersection" == predicate:
            leadingEV = self.getLeadingEV(trafficLight)
            return leadingEV.getDistance() if leadingEV is not None else -1

        elif "EVTrafficDensity" == predicate:
            leadingEV = self.getLeadingEV(trafficLight)
            return leadingEV.getTrafficDensity(
            ) if leadingEV is not None else -1

        elif "leadingEVLane" == predicate:
            leadingEV = self.getLeadingEV(trafficLight)
            return leadingEV.getLane() if leadingEV is not None else None

        else:
            raise Exception("Undefined predicate:", predicate)
 def getTimeSinceLastEVThrough(self, trafficLight: TrafficLight) -> int:
     return self.timeSinceLastEVThrough[trafficLight.getName()]
 def getLeadingEV(self, trafficLight: TrafficLight) -> EmergencyVehicle:
     return self.leadingEV[trafficLight.getName()]
 def getLastEVs(self, trafficLight: TrafficLight) -> List[EmergencyVehicle]:
     return self.lastEVs[trafficLight.getName()]
 def getState(self, trafficLight: TrafficLight) -> Dict[str, List[str]]:
     return self.state[trafficLight.getName()]
示例#9
0
    def checkAssignGreenPhaseToSingleWaitingPhaseRule(
            self, tl: TrafficLight) -> bool:
        lanesWithWaitingVehicles = []
        if tl.getName() == "four-arm":
            state = self.getState(tl)
            # print(state)
            for x in state:
                if state[x] != [] and "2four-arm" in x:
                    lanesWithWaitingVehicles.append(x)

            possibleLanes0 = ["WB2four-arm_LTL_0", "incoming2four-arm_LTL_0"]
            possibleLanes2 = ["WB2four-arm_LTL_1", "incoming2four-arm_LTL_1"]
            possibleLanes4 = ["NWB2four-arm_LTL_0", "bend2four-arm_LTL_0"]
            possibleLanes6 = ["NWB2four-arm_LTL_1", "bend2four-arm_LTL_1"]
            posLanesWaiting = []
            if set(lanesWithWaitingVehicles).issubset(set(possibleLanes0)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes0:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                # print("posLanesWaiting is", posLanesWaiting,
                #       "and lanesWithWaitingVeh is", lanesWithWaitingVehicles)
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 0)
                    return True

            elif set(lanesWithWaitingVehicles).issubset(set(possibleLanes2)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes2:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 2)
                    return True

            elif set(lanesWithWaitingVehicles).issubset(set(possibleLanes4)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes4:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 4)
                    return True

            elif set(lanesWithWaitingVehicles).issubset(set(possibleLanes6)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes6:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 6)
                    return True

        elif tl.getName() == "incoming":
            state = self.getState(tl)
            for x in state:
                if state[x] != [] and "2incoming" in x:
                    lanesWithWaitingVehicles.append(x)

            possibleLanes0 = [
                "four-arm2incoming_0", "four-arm2incoming_1", "EB2incoming_0",
                "EB2incoming_1"
            ]
            possibleLanes2 = [
                "T-intersection2incoming_LTL_0",
                "T-intersection2incoming_LTL_1"
            ]
            possibleLanes4 = ["NEB2incoming_LTL_0", "NEB2incoming_LTL_1"]
            posLanesWaiting = []

            if set(lanesWithWaitingVehicles).issubset(set(possibleLanes0)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes0:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 0)
                    return True

            elif set(lanesWithWaitingVehicles).issubset(set(possibleLanes2)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes2:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 2)
                    return True

            elif set(lanesWithWaitingVehicles).issubset(set(possibleLanes4)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes4:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 4)
                    return True

        else:
            state = self.getState(tl)
            for x in state:
                if state[x] != [] and "2T" in x:
                    lanesWithWaitingVehicles.append(x)

            possibleLanes0 = [
                "SEB2T-intersection_0", "SEB2T-intersection_1",
                "bend2T-intersection_LTL_0"
            ]
            possibleLanes2 = ["bend2T-intersection_LTL_1"]
            posLanesWaiting = []

            if set(lanesWithWaitingVehicles).issubset(set(possibleLanes0)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes0:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 0)
                    return True

            elif set(lanesWithWaitingVehicles).issubset(set(possibleLanes2)):
                for i in range(len(lanesWithWaitingVehicles) + 1):
                    if i == len(lanesWithWaitingVehicles):
                        for i in range(len(lanesWithWaitingVehicles)):
                            if lanesWithWaitingVehicles[i] in possibleLanes2:
                                posLanesWaiting.append(
                                    lanesWithWaitingVehicles[i])
                if len(posLanesWaiting
                       ) > 0 and posLanesWaiting == lanesWithWaitingVehicles:
                    traci.trafficlight.setPhase(tl.getName(), 2)
                    return True

        return False  # If not returned true by now, return false