Esempio n. 1
0
 def handleTransportRequest(self, routing):
     logging.info("Traci version %s %s" % traci.getVersion())
     guid = routing["id"]
     vtype = routing["tags"]["unit"]
     startEdge, startPos, startLane = s = traci.simulation.convertRoad(
         routing["route"][0]["longitude"], routing["route"][0]["latitude"],
         True)
     endEdge, endPos, endLane = e = traci.simulation.convertRoad(
         routing["route"][1]["longitude"], routing["route"][1]["latitude"],
         True)
     logging.info("routing from %s to %s" % (s, e))
     if vtype not in traci.vehicletype.getIDList():
         # TODO adapt the emergency vehicle look if it is a police / fire brigade
         traci.vehicletype.copy("emergency", vtype)
     routeID = "%s.%s" % (guid, self._requestIdx)
     traci.route.add(routeID, (startEdge, endEdge))
     self._requestIdx += 1
     if guid in self._inserted:
         logging.info("removing %s" % guid)
         traci.vehicle.remove(guid)
     traci.vehicle.add(
         guid,
         routeID,
         vtype,
         departPos=str(startPos),
         arrivalPos=str(endPos)
     )  #, departLane=str(startLane), arrivalLane=str(endLane))
     traci.vehicle.subscribe(guid, [
         tc.VAR_TYPE, tc.VAR_POSITION3D, tc.VAR_ANGLE, tc.VAR_SLOPE,
         tc.VAR_SPEED
     ])
     self._inserted[guid] = [
         vtype, routing["route"][1]["longitude"],
         routing["route"][1]["latitude"], 0.
     ]
def run():
    """execute the TraCI control loop"""
    traci.init(PORT)
    print("Server version: " + str(traci.getVersion()))
    traci.vehicle.subscribe("x", [0])  # sub to list of vehicles in sim
    for i in range(0, 10000):
        traci.simulationStep()
        car_list = traci.vehicle.getSubscriptionResults("x")[0]  # get list
        for car in car_list:
            current_road = traci.vehicle.getRoadID(car)  # get road
            if (current_road == new_route[0]) and (car not in affected_cars):
                print("route change for " + str(car))
                traci.vehicle.setColor(car, (255, 0, 0, 0))
                traci.vehicle.setRoute(car, new_route)
                affected_cars.append(car)
    traci.close()
Esempio n. 3
0
    def __init__(self):
        """
        Constructor. Instantiates methods' implementation depending on SUMO
        version. SUMO must be already started when instantiating the class
        """
        self.plexe = None
        api, version = traci.getVersion()
        if version in self.versions:
            self.version = self.versions[version]
        else:
            self.version = self.latest
            for k, v in self.versions.items():
                if version.startswith(k):
                    self.version = self.versions[k]
                    break

        files = listdir(join(dirname(__file__), "plexe_imp"))
        plexe_f, plexe_fn, plexe_d = imp.find_module("plexe_imp",
                                                     [dirname(__file__)])
        plexe_imp = imp.load_module("plexe_imp", plexe_f, plexe_fn,
                                    plexe_d)
        default_impl = None
        for f in files:
            name, ext = splitext(f)
            if ext == ".py":
                mod_f, mod_fn, mod_d = imp.find_module(name, plexe_imp.__path__)
                try:
                    mod = imp.load_module("plexe.plexe_imp." + name, mod_f,
                                          mod_fn, mod_d)
                except ImportError:
                    continue
                try:
                    instance = mod.PlexeImp()
                except AttributeError:
                    continue
                versions = instance.register()
                for v in versions:
                    if version.startswith(v):
                        self.plexe = instance
                        break
                if 'default' in versions:
                    default_impl = instance
        if self.plexe is None:
            self.plexe = default_impl
        if self.plexe is None:
            print("No Plexe API implementation found for %s" % version)
            raise Exception()
Esempio n. 4
0
def run():
    """execute the TraCI control loop"""
    traci.init(PORT)
    print("Server version: " + str(traci.getVersion()))
    print("Server timestep: " + str(traci.simulation.getDeltaT()))
    print(str(traci.simulation.getNetBoundary()))
    traci.simulation.subscribe([SIMTIME, DEPARTEDVHCLST, ARRIVEDVHCLST])
    traci.vehicle.subscribe("x",[0, 1])
    for i in range(0, 12100):
        
        traci.simulationStep()
        simsubs = traci.simulation.getSubscriptionResults()
        vehsubs = traci.vehicle.getSubscriptionResults("x")

        print("Current SIM time: " + str(simsubs[SIMTIME]))
        
        dep = simsubs[DEPARTEDVHCLST]
        arr = simsubs[ARRIVEDVHCLST]
        carsinsim = vehsubs[0]
        print 'dep', dep
        print 'arr', arr

##        for car in carsinsim:
##            #traci.vehicle.setSpeed(car, 5.0)
##            road = traci.vehicle.getRoadID(car)
##            if road == "26:2" and car not in affected:
##                #traci.vehicle.slowDown(car, 0.0, 10000)
##                #traci.vehicle.setSpeed(car, 4.0)
##                traci.vehicle.changeLane(car, 1, 40000)
##                affected.append(car)
##            elif road != "26:2" and car in affected:
##                traci.vehicle.changeLane(car, 1, -1)
##                affected.remove(car)
        
        
        time.sleep(0.1)
    traci.close()
Esempio n. 5
0
    logger.info(options)

    trip_file = instance_folder + '/' + instance_opts + '.xml'

    statistics_values = StatisticsValues(options.scenario_folder,
                                         instance_folder, trip_file,
                                         options.algorithm, options.ev)

    if not statistics_values.skip_because_json_file(
            options.override, options.skip, algorithm.instance_name()):
        traci.start([
            sumoBinary, '-v', 'true', '-c',
            options.scenario_folder + '/osm-' + options.prefix + '.sumocfg',
            '--duration-log.statistics', '--tripinfo-output', trip_file,
            '--start', '--time-to-teleport', '300', '--seed',
            str(options.seedsumo), '--ignore-junction-blocker', '50'
        ])

        logger.info('SUMO Version: {}'.format(traci.getVersion()))

        run(options, algorithm, statistics_values)

        if os.path.isfile(trip_file):
            os.remove(trip_file)
            logger.info('File {} was deleted'.format(trip_file))

        if os.path.isfile(logfile):
            logger.info('Deleting {}...'.format(logfile))
            os.remove(logfile)