Exemple #1
0
def initialize_edges_for_spawns_and_sinks(traci):
    print("Initializeing edges/nodes.")
    initialize_edges_and_nodes()
    
    print("Finding spawn edge candidates.")
    if not os.path.exists('temp'):
        os.mkdir('temp')
    generate_spawns = True
    try:
        env.spawn_edge, last_spawn_point = purr.load('temp/spawns.bin')
        if last_spawn_point == env.point_spawn:
            generate_spawns = False
    except FileNotFoundError:
        pass
    if generate_spawns:
        env.spawn_edge = get_valid_edges_from_point(env.point_spawn,env.radius_spawn_sink)
        purr.save('temp/spawns.bin',[env.spawn_edge,env.point_spawn])
    add_radius_polygon(traci,env.point_spawn,env.radius_spawn_sink,(0,255,0))
    
    print("Finding sink edge candidates.")
    generate_sinks = True
    try:
        env.sink_edge,last_sink_point = purr.load('temp/sinks.bin')
        if last_sink_point == env.point_sink:
            generate_sinks = False
    except FileNotFoundError:
        pass
    if generate_sinks:
        env.sink_edge = get_valid_edges_from_point(env.point_sink,env.radius_spawn_sink)
        purr.save('temp/sinks.bin',[env.sink_edge,env.point_sink])
    add_radius_polygon(traci,env.point_sink,env.radius_spawn_sink,(255,0,0))
    return
    def run_sumo(self):
        purr.save(self.points_pybin,
                  (self.vPs_nodes, self.vPd_nodes, self.tP_nodes))
        for method in ("greedy", "smart"):
            cmd = "python runner.py "
            # ~ cmd += "--nogui "
            cmd += "--map-dir=%s " % (self.map_dir)
            cmd += "--veh.total=%d " % (self.N)
            cmd += "--veh.exists.max=%d " % (self.N)
            cmd += "--method=%s " % (method)
            cmd += "--points.file=%s " % (self.points_pybin)
            cmd += "--nash.R=%d " % (self.R)
            cmd += "--nash.tau=%f " % (self.tau)
            cmd += "--debug "
            if method == "greedy":
                cmd += "--out.dir=%s" % (self.out_dir_sumo_greedy)
            else:
                cmd += "--out.dir=%s" % (self.out_dir_sumo_smart)
            os.system(cmd)

            if method == "greedy":
                self.nA_sumo_greedy = purr.load(env.nA_greedy_file)
            else:
                self.nA_sumo_smart = purr.load(env.nA_smart_file)

            # ~ purr.pause()
            continue
    def dst2src_point_validation(self):
        print("Determining valid src and destination points.")
        while True:
            # Determine vehicle start/dest and target SUMO nodes
            self.vP, self.tP = ss2.getLocalizedPlacement(2 * self.N, self.M)
            vP_nodes = self.correlateNormalPoints2SumoNodes(
                self.vP, "Determining vehicle start/dest SUMO nodes ")
            self.vPs_nodes = []
            self.vPd_nodes = []
            for i, node in enumerate(vP_nodes):
                node['index'] = i
                if i % 2 == 0:
                    self.vPs_nodes.append(node)
                else:
                    self.vPd_nodes.append(node)
                continue
            del (vP_nodes)
            self.tP_nodes = self.correlateNormalPoints2SumoNodes(
                self.tP, "Correlating target SUMO nodes ")

            purr.save(self.points_pybin,
                      (self.vPs_nodes, self.vPd_nodes, self.tP_nodes))

            # Run SUMO to validate points
            cmd = "python runner.py "
            cmd += "--nogui "
            cmd += "--map-dir=%s " % (self.map_dir)
            cmd += "--veh.total=%d " % (self.N)
            cmd += "--veh.exists.max=%d " % (self.N)
            cmd += "--method=%s " % ("validate.pts")
            cmd += "--points.file=%s " % (self.points_pybin)

            os.system(cmd)

            if purr.load(env.status_file):
                break
            continue

        self.vPs_nodes, self.vPd_nodes, self.tP_nodes = purr.load(
            env.valid_points_file)

        zipped_vP = []
        for i, node in enumerate(self.vPs_nodes):
            zipped_vP.append(node['index'])
            zipped_vP.append(self.vPd_nodes[i]['index'])

        temp = [self.vP[index] for index in zipped_vP]
        self.vP = temp

        print("Complete")

        return
def load_complete_spm():
    complete_spm_pybin = "temp/complete-spm.pybin"
    try:
        env.spm_complete = purr.load(complete_spm_pybin)
    except FileNotFoundError:
        nodes = list(env.nx.nodes)
        weights = []
        n = 0
        total = len(nodes) * len(nodes)
        for irow, rn in enumerate(nodes):
            row = []
            for icol, cn in enumerate(nodes):
                weight = float('inf')
                if not cn == rn:
                    weight = None
                row.append(weight)
                n += 1
                purr.update(n, total, "Generating Complete SPM...")
                continue
            weights.append(row)
            continue
        spm = {'colnames': nodes, 'rownames': nodes, 'weights': weights}
        env.spm_complete = spm
        purr.save(complete_spm_pybin, env.spm_complete)
    print("Complete!")
    return
Exemple #5
0
def initialize_shortest_path_weights():
    _file = purr.mrf(env.options.map_dir,r'*.weight.pybin')
    env.shortest_path_weights = purr.load(_file)
    nodes = []   
    for i,nid in enumerate(list(env.nx.nodes)):
        nodes.append({"id":nid,"spw index":i,"sort id":purr.ascii2int(nid)})
    env.nid2spwid = purr.sortdicts(nodes,'sort id')
    env.nid2spwid_sids = [item['sort id'] for item in env.nid2spwid]
    return
Exemple #6
0
def get_options():
    opt_parser = optparse.OptionParser()
    # GUI
    opt_parser.add_option("--nogui",
                          action="store_true",
                          default=False,
                          help="run the commandline version of sumo")
    #
    opt_parser.add_option('--seed',
                          type='int',
                          dest='seed',
                          default=8635839050,
                          help='Random Seed')
    opt_parser.add_option('--map-dir',
                          type='string',
                          dest='map_dir',
                          default=None,
                          help='Directory of SUMO map files')
    #
    opt_parser.add_option('--veh.total',
                          type='int',
                          dest='veh_total',
                          default=1000,
                          help='Total amount of vehicles.')
    opt_parser.add_option(
        '--veh.exists.max',
        type='int',
        dest='veh_exists_max',
        default=50,
        help='The amount of vehicles that exist in the simulation at any time.'
    )
    #
    #
    opt_parser.add_option('-o',
                          '--out.dir',
                          type='string',
                          dest='out_dir',
                          default='out',
                          help='Output directory.')
    #
    opt_parser.add_option('-m',
                          '--method',
                          type='string',
                          dest='method',
                          default='baseline',
                          help='Target selection algorithm.')
    #
    opt_parser.add_option('-R',
                          '--nash.R',
                          type='float',
                          dest='nash_R',
                          default=None,
                          help='R value for nash algorithm.')
    opt_parser.add_option('--nash.tau',
                          type='float',
                          dest='nash_tau',
                          default=None,
                          help='tau value for nash algorithm.')
    #
    opt_parser.add_option('--points.file',
                          type='string',
                          dest='points',
                          default=None,
                          help="Pickle file with vPs, vPd, and tP.")
    #
    opt_parser.add_option('--debug',
                          action='store_true',
                          dest='debug',
                          default=False,
                          help="Debug operations.")
    #
    options, args = opt_parser.parse_args()

    # Validate Options
    if options.map_dir == None:
        print('Please specify a map directory with --map-dir')
        sys.exit(1)
    elif not options.method.lower() in ['greedy', 'smart', 'validate.pts']:
        print('Unknown method. Please specify a valid method with --method.')
        sys.exit(1)
    elif options.method.lower() == 'nash':
        if options.nash_R == None:
            print("Please specify an R value with --nash.R")
            sys.exit(1)
        elif options.nash_tau == None:
            print("Please specify a taur value with --nash.tau")
            sys.exit(1)
    elif options.points == None:
        print("Please specify the points file that has vPs, vPd, and tP.")
        sys.exit(1)

    # Set variables
    env.veh_total = options.veh_total
    env.veh_exists_max = options.veh_exists_max
    env.out_dir = options.out_dir
    options.method = options.method.lower()
    env.method = options.method
    env.R = options.nash_R
    env.tau = options.nash_tau
    env.seed = options.seed
    env.debug = options.debug
    env.vPs, env.vPd, env.tP = purr.load(options.points)

    # Splash
    if env.method == 'greedy':
        splash_greedy()
    elif env.method == 'smart':
        splash_smart()
    elif env.method == 'validate.pts':
        splash_validate_pts()

    return options