def start(processID, parallelMode,useGUI): random.seed(Config.random_seed) """ main entry point into the application """ Config.parallelMode = parallelMode Config.sumoUseGUI = useGUI info('#####################################', Fore.CYAN) info('# Starting TRAPP v0.1 #', Fore.CYAN) info('#####################################', Fore.CYAN) # Check if sumo is installed and available SUMODependency.checkDeps() info('# SUMO-Dependency check OK!', Fore.GREEN) # Load the sumo map we are using into Python Network.loadNetwork() info(Fore.GREEN + "# Map loading OK! " + Fore.RESET) info(Fore.CYAN + "# Nodes: " + str(Network.nodesCount()) + " / Edges: " + str(Network.edgesCount()) + Fore.RESET) # After the network is loaded, we init the router CustomRouter.init() # Start sumo in the background SUMOConnector.start() info("\n# SUMO-Application started OK!", Fore.GREEN) # Start the simulation Simulation.start() # Simulation ended, so we shutdown info(Fore.RED + '# Shutdown' + Fore.RESET) traci.close() sys.stdout.flush() return None
def create_epos_output_files(self, sourceID, targetID, tick, agent_ind): router_res_length = CustomRouter.route_by_min_length( sourceID, targetID) if len(router_res_length.route) > 0: self.create_output_files( history_prefs[self.id]["min_length"], router_res_length.route, self.find_occupancy_for_route(router_res_length.meta), agent_ind) router_res_speeds = CustomRouter.route_by_max_speed(sourceID, targetID) if len(router_res_speeds.route) > 0: self.create_output_files( history_prefs[self.id]["max_speed"], router_res_speeds.route, self.find_occupancy_for_route(router_res_speeds.meta), agent_ind) router_res_length_and_speeds = CustomRouter.minimalRoute( sourceID, targetID) if len(router_res_length_and_speeds.route) > 0: self.create_output_files( history_prefs[self.id]["balanced"], router_res_length_and_speeds.route, self.find_occupancy_for_route( router_res_length_and_speeds.meta), agent_ind)
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 start(processID, parallelMode, useGUI): """ main entry point into the application """ Config.processID = processID Config.parallelMode = parallelMode Config.sumoUseGUI = useGUI info('#####################################', Fore.CYAN) info('# Starting CrowdNav v0.2 #', Fore.CYAN) info('#####################################', Fore.CYAN) info('# Configuration:', Fore.YELLOW) info('# Kafka-Host -> ' + Config.kafkaHost, Fore.YELLOW) info('# Kafka-Topic1 -> ' + Config.kafkaTopicTrips, Fore.YELLOW) info('# Kafka-Topic2 -> ' + Config.kafkaTopicPerformance, Fore.YELLOW) # init sending updates to kafka and getting commands from there if Config.kafkaUpdates or Config.mqttUpdates: RTXForword.connect() RTXConnector.connect() # Check if sumo is installed and available SUMODependency.checkDeps() info('# SUMO-Dependency check OK!', Fore.GREEN) # Load the sumo map we are using into Python Network.loadNetwork() info(Fore.GREEN + "# Map loading OK! " + Fore.RESET) info(Fore.CYAN + "# Nodes: " + str(Network.nodesCount()) + " / Edges: " + str(Network.edgesCount()) + Fore.RESET) # After the network is loaded, we init the router CustomRouter.init() # Start sumo in the background SUMOConnector.start() info("\n# SUMO-Application started OK!", Fore.GREEN) # Start the simulation Simulation.start() # Simulation ended, so we shutdown info(Fore.RED + '# Shutdown' + Fore.RESET) traci.close() sys.stdout.flush() return None
def setArrived(self, tick): """ car arrived at its target, so we add some statistic data """ # import here because python can not handle circular-dependencies from app.entitiy.CarRegistry import CarRegistry # add a round to the car self.rounds += 1 self.lastRerouteCounter = 0 if tick > Config.initialWaitTicks and self.smartCar: # as we ignore the first 1000 ticks for this # add a route to the global registry CarRegistry.totalTrips += 1 # add the duration for this route to the global tripAverage durationForTrip = (tick - self.currentRouteBeginTick) CarRegistry.totalTripAverage = addToAverage( CarRegistry.totalTrips, # 100 for faster updates CarRegistry.totalTripAverage, durationForTrip) # CSVLogger.logEvent("arrived", [tick, self.sourceID, self.targetID, # durationForTip, self.id,self.currentRouterResult.isVictim]) # log the overrhead values minimalCosts = CustomRouter.minimalRoute(self.sourceID, self.targetID, None, None).totalCost tripOverhead = durationForTrip / minimalCosts / 1.1 # 1.6 is to correct acceleration and deceleration # when the distance is very short, we have no overhead if durationForTrip < 10: tripOverhead = 1 # in rare cases a trip does take very long - as most outliers are <30, we cap the overhead to 30 here if tripOverhead > 30: print("-> capped overhead to 30 - " + str(minimalCosts) + " - " + str(durationForTrip) + " - " + str(tripOverhead)) tripOverhead = 30 CarRegistry.totalTripOverheadAverage = addToAverage( CarRegistry.totalTrips, CarRegistry.totalTripOverheadAverage, tripOverhead) CSVLogger.logEvent("overhead", [ tick, self.sourceID, self.targetID, durationForTrip, minimalCosts, tripOverhead, self.id, self.currentRouterResult.isVictim ]) # log to kafka msg = dict() msg["tick"] = tick msg["overhead"] = tripOverhead msg["complaint"] = self.generate_complaint(tripOverhead) RTXForword.publish(msg, Config.kafkaTopicTrips) # if car is still enabled, restart it in the simulation if self.disabled is False: self.addToSimulation(tick)
def __createNewRoute(self, tick): """ creates a new route to a random target and uploads this route to SUMO """ if self.targetID is None: self.sourceID = Network.get_random_node_id_of_passenger_edge( random) else: self.sourceID = self.targetID # We start where we stopped self.targetID = Network.get_random_node_id_of_passenger_edge(random) self.currentRouteID = self.id + "-" + str(self.rounds) try: if self.driver_preference == "min_length": self.currentRouterResult = CustomRouter.route_by_min_length( self.sourceID, self.targetID) elif self.driver_preference == "max_speed": self.currentRouterResult = CustomRouter.route_by_max_speed( self.sourceID, self.targetID) else: self.currentRouterResult = CustomRouter.minimalRoute( self.sourceID, self.targetID) if len(self.currentRouterResult.route) > 0: traci.route.add(self.currentRouteID, self.currentRouterResult.route) return self.currentRouteID else: if Config.debug: print "vehicle " + str( self.id) + " could not be added, retrying" return self.__createNewRoute(tick) except: if Config.debug: print "vehicle " + str( self.id) + " could not be added [exception], retrying" return self.__createNewRoute(tick)
def setArrived(self, tick): """ car arrived at its target, so we add some statistic data """ # import here because python can not handle circular-dependencies from app.entity.CarRegistry import CarRegistry self.lastRerouteCounter = 0 if self.smartCar: # as we ignore the first 1000 ticks for this # add a route to the global registry CarRegistry.totalTrips += 1 # add the duration for this route to the global tripAverage durationForTrip = (tick - self.currentRouteBeginTick) CarRegistry.totalTripAverage = addToAverage( CarRegistry.totalTrips, # 100 for faster updates CarRegistry.totalTripAverage, durationForTrip) minimalCosts = CustomRouter.minimalRoute(self.sourceID, self.targetID).totalCost tripOverhead = durationForTrip / minimalCosts / 1.1 # 1.6 is to correct acceleration and deceleration # when the distance is very short, we have no overhead if durationForTrip < 10: tripOverhead = 1 # in rare cases a trip does take very long - as most outliers are <30, we cap the overhead to 30 here if tripOverhead > 30: print("-> capped overhead to 30 - " + str(minimalCosts) + " - " + str(durationForTrip) + " - " + str(tripOverhead)) tripOverhead = 30 CarRegistry.totalTripOverheadAverage = addToAverage( CarRegistry.totalTrips, CarRegistry.totalTripOverheadAverage, tripOverhead) CSVLogger.logEvent("overheads", [ tick, self.sourceID, self.targetID, self.rounds, durationForTrip, minimalCosts, tripOverhead, self.id, self.driver_preference ]) # if car is still enabled, restart it in the simulation if self.disabled is False: # add a round to the car self.rounds += 1 self.addToSimulation(tick, False)
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)