def processTick(self, tick): """ process changes that happened in the tick to this car """ self.lastRerouteCounter += 1 # reroute every x ticks based on config value if self.lastRerouteCounter >= CustomRouter.reRouteEveryTicks and CustomRouter.reRouteEveryTicks > 0: self.lastRerouteCounter = 0 if self.smartCar: try: oldRoute = self.currentRouterResult.route currentEdgeID = traci.vehicle.getRoadID(self.id) nextNodeID = Network.getEdgeIDsToNode( currentEdgeID).getID() self.currentRouterResult = CustomRouter.route( nextNodeID, self.targetID, tick, self) traci.vehicle.setRoute(self.id, [currentEdgeID] + self.currentRouterResult.route) # print("OLD: " + str(oldRoute) + " - NEW: " + str(self.currentRouterResult.route)) except IndexError as e: # print(e) pass except traci.exceptions.TraCIException as e: # print(e) pass roadID = traci.vehicle.getSubscriptionResults(self.id)[80] if roadID != self.currentEdgeID and self.smartCar: if self.currentEdgeBeginTick is not None: CustomRouter.applyEdgeDurationToAverage( self.currentEdgeID, tick - self.currentEdgeBeginTick, tick) # CSVLogger.logEvent("edge", [tick, self.currentEdgeID, # tick - self.currentEdgeBeginTick, self.id]) # log to kafak # msg = dict() # msg["tick"] = tick # msg["edgeID"] = self.currentEdgeID, # msg["duration"] = tick - self.currentEdgeBeginTick # print("changed route to: " + roadID) self.currentEdgeBeginTick = tick self.currentEdgeID = roadID pass
def __createNewRoute(self, tick): """ creates a new route to a random target and uploads this route to SUMO """ # import here because python can not handle circular-dependencies if self.targetID is None: self.sourceID = random.choice(Network.nodes).getID() else: self.sourceID = self.targetID # We start where we stopped # random target self.targetID = random.choice(Network.nodes).getID() self.currentRouteID = self.id + "-" + str(self.rounds) self.currentRouterResult = CustomRouter.route(self.sourceID, self.targetID, tick, self) if len(self.currentRouterResult.route) > 0: traci.route.add(self.currentRouteID, self.currentRouterResult.route) # set color to red return self.currentRouteID else: # recursion aka. try again as this should work! return self.__createNewRoute(tick)