def main():
    if __name__ == "__main__":
        debug("The main script is running.")
        options = get_options()
        env.options = options
        config.s_sumocfg_file = purr.mrf(env.options.map_dir, r'*.sumocfg')
        config.s_net_file = purr.mrf(env.options.map_dir, r'*.net.xml')
        debug("options.nogui=" + str(options.nogui))

        # This script has been called from the command line.
        # It will start sumo as a server, then connect and run.
        if options.nogui:
            s_sumo_binary = checkBinary('sumo')
        else:
            s_sumo_binary = checkBinary('sumo-gui')
        debug("s_sumo_binary=" + s_sumo_binary)

        # Have TraCI start sumo as a subprocess, then the python script
        # can connect and run
        debug("config.s_sumocfg_file=" + config.s_sumocfg_file)

        sumo_cmd = [s_sumo_binary, "-c", config.s_sumocfg_file]
        traci.start(sumo_cmd)

        run()
        global __F_START_TIME
        print("\n----- runtime takes %s seconds -----" %
              (time.time() - __F_START_TIME))
Esempio n. 2
0
def get_valid_edges_from_point(point,radius):
    print("Finding valid edges for point",point,"and radius",radius)
    x,y = point
    
    
    # Find the nodes that are within the radius
    nod_xml = purr.mrf(env.options.map_dir,r'*.nod.xml')
    nodes_all = purr.readXMLtag(nod_xml,'node')
    nodes_valid = []
    for node in nodes_all:        
        if abs(x - float(node['x'])) <= radius and abs(y - float(node['y'])) <= radius:
            nodes_valid.append(node)
        continue
        
    nodes_valid_ids = [node['id'] for node in nodes_valid]
    
    # Now that we know the nodes, we can pick out valid edges that the node begins at
    edg_xml = purr.mrf(env.options.map_dir,r'*.edg.xml')
    edges_all = purr.readXMLtag(edg_xml,'edge')
    edges_filtered = purr.batchfilterdicts(edges_all,'from',nodes_valid_ids,show_progress=True)
    edges_filtered = purr.flattenlist(edges_filtered)
    edges_valid = []
    for edge in edges_filtered:
        if not ':' in edge['id']:
            edges_valid.append(edge)
    
    print("Found %d edge candidates." % (len(edges_valid)))
    
    return edges_valid
Esempio n. 3
0
def main():
    # Modules
    dgpng = '../../modules/dgpng/dgpng.py'
    tracewrangler = '../../modules/nsf-traces/tracewrangler.py'

    # Map files
    map_dir = '../london-seg4/data/100'
    edg_xml = purr.mrf(map_dir, r'*.edg.xml')
    nod_xml = purr.mrf(map_dir, r'*.nod.xml')

    # Hot file
    hot = 'simulations/dgpng'

    # Working Dir
    temp = 'temp'
    if not os.path.exists(temp):
        os.mkdir(temp)

    # Output dir
    out = '%s/out' % (hot)
    if not os.path.exists(out):
        os.mkdir(out)

    # Retrieve everthing in the hotfile
    sims = purr.lsdir(hot)

    # For each simulation
    for sim in sims:
        if not '.json' in sim:
            continue
        path = '%s/%s' % (hot, sim)

        # Trace Wrangler
        cmd = 'python %s ' % (tracewrangler)
        cmd += '--trace.json=%s ' % (path)
        cmd += '--edg.xml=%s ' % (edg_xml)
        cmd += '--color.ssv=%s/%s.ssv ' % (temp, sim)
        cmd += '--node-color=(255,255,255,255) '
        cmd += '--edge-color=(0,0,0,255)'
        os.system(cmd)

        # dgpng
        cmd = 'python %s ' % (dgpng)
        cmd += '--edg.xml=%s ' % (edg_xml)
        cmd += '--nod.xml=%s ' % (nod_xml)
        cmd += '--png=%s/%s.png ' % (out, sim)
        cmd += '--background-color=(255,255,255,0) '
        cmd += '--node-color=(255,255,255,255) '
        cmd += '--edge-color=(0,0,0,50) '
        cmd += '--internal-node-color=(0,0,0,0) '
        cmd += '--edge-thickness=10 '
        cmd += '--padding=10 '
        cmd += '--color.ssv=%s/%s.ssv ' % (temp, sim)
        os.system(cmd)
        continue

    # Clean up
    purr.deldir(temp)
    return
def main():
    global OPTIONS
    OPTIONS = get_options()

    # Nodes
    nod_xml = purr.mrf(OPTIONS.map_dir, r'*.nod.xml')
    nodes = purr.readXMLtag(nod_xml, 'node')
    print("nodes: ", len(nodes))

    # Edges
    edg_xml = purr.mrf(OPTIONS.map_dir, r'*.edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')
    print("edges: ", len(edges))

    # Load in Uber speed data
    original_edges = speedhist.load_edges()
    connections = speedhist.load_connections()

    with open(OPTIONS.output, 'w') as f:
        f.write("% from to weight id\n")

        # Each each is a connection
        n = 0
        total = len(edges)
        for edge in edges:
            # Edges with shape
            try:
                length = length_from_shape(edge['shape'])

            # Edges w/o shape
            except KeyError:
                node_from = purr.filterdicts(nodes, 'id', edge['from'])[0]
                node_to = purr.filterdicts(nodes, 'id', edge['to'])[0]
                length = length_from_nodes(node_from, node_to)

            # Determine Speed
            # ~ speed = float(edge['speed'])
            conn = purr.filterdicts(connections, 'ID', edge['id'])[0]
            child_edges = speedhist.get_edges(conn, original_edges)
            _sum = 0
            for ce in child_edges:
                _sum += ce['mean']
            speed = _sum / len(child_edges)

            f.write("%s %s %.3f %s\n" %
                    (edge['from'], edge['to'], length / speed, edge['id']))

            n += 1
            purr.update(n, total, msg="Calculating edge lengths ")
            continue
        pass
    print()

    return
Esempio n. 5
0
    def __init__(self, DIR):
        self.DIR = DIR
        self.edges = purr.readXMLtag(purr.mrf(DIR, r"*.edg.xml"), "edge")
        self.nodes = purr.readXMLtag(purr.mrf(DIR, r"*.nod.xml"), "node")

        x = [float(node['x']) for node in self.nodes]
        y = [float(node['y']) for node in self.nodes]
        self.centroid = (stats.fmean(x), stats.fmean(y))
        self.xMax = max(x)
        self.xMin = min(x)
        self.yMax = max(y)
        self.yMin = min(y)
        return
def main():
    map_dir = "../london-seg4/data/100"
    edges = purr.readXMLtag(purr.mrf(map_dir, r'*.edg.xml'), 'edge')
    connections = purr.readCSV(purr.mrf(map_dir, r'*.conn.csv'))
    stats = purr.readCSV('edges.stats.csv')
    purr.head(edges)
    purr.head(connections)
    purr.head(stats)

    # ~ return

    eids = [edge['id'] for edge in edges]
    connections = purr.flattenlist(
        purr.batchfilterdicts(connections,
                              'CONNECTION ID',
                              eids,
                              show_progress=True))
    print()

    total = len(connections) - 1
    for i, conn in enumerate(connections):
        try:
            speeds = purr.flattenlist(
                purr.batchfilterdicts(stats, 'ID', conn['EDGE IDS']))
        except TypeError:
            speeds = purr.flattenlist(
                purr.batchfilterdicts(stats, 'ID', [conn['EDGE IDS']]))
        mean = [float(speed['mean']) for speed in speeds]
        std = [float(speed['std']) for speed in speeds]
        p50 = [float(speed['p50']) for speed in speeds]
        p85 = [float(speed['p85']) for speed in speeds]
        edges[i]['mean'] = "%.3f" % (statistics.mean(mean))
        edges[i]['std'] = "%.3f" % (statistics.mean(std))
        edges[i]['p50'] = "%.3f" % (statistics.mean(p50))
        edges[i]['p85'] = "%.3f" % (statistics.mean(p85))
        purr.update(i, total, "Correlating speeds with edges ")
        continue

    with open("edg.xml", 'w') as f:
        f.write("<edges>")
        for edge in edges:
            f.write("\n\t<edge")
            for key, val in edge.items():
                f.write(' %s="%s"' % (key, val))
            f.write("/>")
            continue
        f.write("\n</edges>")

    print("COMPLETE!")

    return
def main():
    global OPTIONS
    OPTIONS = get_options()
    edg_xml = purr.mrf(OPTIONS.map_dir, r'*.edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')

    original_edges = speedhist.load_edges()
    connections = speedhist.load_connections()

    fn = 'hhh.edg.xml'

    with open(fn, 'w') as f:
        f.write('<edges>')

        n = 0
        total = len(edges)
        for edge in edges:
            conn = purr.filterdicts(connections, 'ID', edge['id'])[0]
            child_edges = speedhist.get_edges(conn, original_edges)
            _sum = 0
            for ce in child_edges:
                _sum += ce['mean']
            speed = _sum / len(child_edges)

            f.write(
                '\n\t<edge id="%s" from="%s" to="%s" priority="%s" numLanes="%s" speed="%.3f"/>'
                % (edge['id'], edge['from'], edge['to'], edge['priority'],
                   edge['numLanes'], speed))

            n += 1
            purr.update(n, total)
            continue

        f.write('\n</edges>')
    return
Esempio n. 8
0
def main():
    global OPTIONS
    OPTIONS = get_options()

    # Nodes
    nod_xml = purr.mrf(OPTIONS.map_dir, r'*.nod.xml')
    nodes = purr.readXMLtag(nod_xml, 'node')
    print("nodes: ", len(nodes))

    # Edges
    edg_xml = purr.mrf(OPTIONS.map_dir, r'*.edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')
    print("edges: ", len(edges))

    with open(OPTIONS.output, 'w') as f:
        f.write("% from to weight id\n")

        # Each each is a connection
        n = 0
        total = len(edges)
        for edge in edges:
            if edge['from'] == edge['to']:
                n += 1
                continue

            # Edges with shape
            try:
                length = length_from_shape(edge['shape'])

            # Edges w/o shape
            except KeyError:
                node_from = purr.filterdicts(nodes, 'id', edge['from'])[0]
                node_to = purr.filterdicts(nodes, 'id', edge['to'])[0]
                length = length_from_nodes(node_from, node_to)

            speed = float(edge['speed'])

            f.write("%s %s %.3f %s\n" %
                    (edge['from'], edge['to'], length / speed, edge['id']))

            n += 1
            purr.update(n, total, msg="Calculating edge lengths ")
            continue
        pass
    print()

    return
Esempio n. 9
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
Esempio n. 10
0
def initialize_nx():
    print("Loading networkx graph...", end='')
    map_nx = purr.mrf(env.options.map_dir, r'*.nx')
    env.nx = nx.read_edgelist(map_nx,
                              data=(
                                  ("weight", float),
                                  ("id", str),
                              ),
                              nodetype=str,
                              comments='%',
                              create_using=nx.MultiDiGraph())
    print("Complete!")
    return
Esempio n. 11
0
def main():
    map_dir = "../TAPASCologne-0.32.0"
    print("map dir=%s" % (map_dir))
    
    # Retrieve the rou.xml file
    trips_xml = purr.mrf(map_dir,r'*trips.xml')
    print("trips.xml=%s" % (trips_xml))
    
    # Load the trips into a dictionary type
    trips = purr.readXMLtag(trips_xml,'trip')
    print("First five trips.")
    purr.head(trips,5)
    
    # The data will be read into a dictionary as a string type
    # YOu can acess them like this.
    print('id=%s' % (trips[0]['id']))
    print('depart time=%.2f' % (float(trips[0]['depart'])))
    return
Esempio n. 12
0
def initialize(traci):
    os.system("cls")
    print("Initializing...")
    
    trips = purr.readXMLtag(purr.mrf(env.map_dir,r"*.trips.xml"),"trip")
    
    total = len(trips) - 1
    print('Number of trips:',len(trips))
    for i,trip in enumerate(trips):
        veh = vehicle.Vehicle("veh%d" % (i))
        try:
            veh.add(traci,trip['from'],trip['to'],trip['depart'])
        except:
            pass
        env.vehicles.append(veh)
        purr.update(i,total)

    print("Initialization complete!")
    return
    def __init__(self, map_dir, N, M, tau, R):
        self.map_dir = map_dir
        self.N = N
        self.M = M
        self.tau = tau
        self.R = R

        self.nodes = purr.readXMLtag(purr.mrf(self.map_dir, r'*.nod.xml'),
                                     'node')
        self.normalizeNodes(0.1)

        self.points_pybin = "temp/points.pybin"

        self.dst2src_point_validation()

        if not os.path.exists(self.out_dir_sumo):
            os.mkdir(self.out_dir_sumo)
        if not os.path.exists(self.out_dir_geom):
            os.mkdir(self.out_dir_geom)
        return
Esempio n. 14
0
def initialize_edges_and_nodes():
    edg_xml = purr.mrf(env.options.map_dir,r'*.edg.xml')
    env.edges = purr.readXMLtag(edg_xml,'edge')
    nod_xml = purr.mrf(env.options.map_dir,r'*.nod.xml')
    env.nodes = purr.readXMLtag(nod_xml,'node')
    return
def main():
    map_dir = "../orlando-seg1/"

    # Load Nodes
    nod_xml = purr.mrf(map_dir, r'*.nod.xml')
    nodes = purr.readXMLtag(nod_xml, 'node')

    # Load Edges
    edg_xml = purr.mrf(map_dir, r'*edg.xml')
    edges = purr.readXMLtag(edg_xml, 'edge')

    # Correlate From/To Nodes with edges
    print('Finding Nodes From')
    nids_from = [edge['from'] for edge in edges]
    nodes_from = purr.flattenlist(
        purr.batchfilterdicts(nodes, 'id', nids_from, show_progress=True))

    print('\nFinding Nodes To')
    nids_to = [edge['to'] for edge in edges]
    nodes_to = purr.flattenlist(
        purr.batchfilterdicts(nodes, 'id', nids_to, show_progress=True))
    print()

    # Create the CSV for Nodes
    node_attr = [
        'id', 'x', 'y', 'z', 'type', 'tlType', 'tl', 'radius', 'keepClear',
        'rightOfWay'
    ]
    node_csv = 'node.csv'
    with open(node_csv, 'w') as csv:
        # Column Names
        csv.write(node_attr[0])
        for attr in node_attr[1:]:
            csv.write(',%s' % (attr))
        csv.write('\n')

        # Fill in rows
        n = 0
        total = len(nodes)
        for node in nodes:
            csv.write(node[node_attr[0]])
            for attr in node_attr[1:]:
                csv.write(',')
                try:
                    csv.write(node[attr])
                except KeyError:
                    pass
                continue
            csv.write('\n')
            n += 1
            purr.update(n, total, msg="Writing node.csv ")
            continue
        pass

    print()

    # Create the CSV for Edges
    edge_attr = [
        'id', 'from', 'to', 'type', 'numLanes', 'speed', 'priority',
        'spreadType', 'width', 'name', 'endOffset', 'sidewalkWidth'
    ]
    edge_allow = [
        'emergency', 'authority', 'passenger', 'hov', 'taxi', 'bus',
        'delivery', 'truck', 'trailer', 'motorcyle', 'moped'
    ]
    edge_csv = 'edge.csv'
    with open(edge_csv, 'w') as csv:
        # Column Names
        csv.write(edge_attr[0])
        for attr in edge_attr[1:]:
            csv.write(',%s' % (attr))
        csv.write(',length')
        for attr in edge_allow:
            csv.write(',%s' % (attr))
        csv.write('\n')

        # Fill in Rows
        n = 0
        total = len(edges)
        for i, edge in enumerate(edges):
            # id
            csv.write(edge[edge_attr[0]])

            # Attributes
            for attr in edge_attr[1:]:
                csv.write(',')
                try:
                    csv.write(edge[attr])
                except KeyError:
                    pass
                continue

            # Length
            try:
                csv.write(',%.3f' % (shape_len(edge['shape'])))
            except KeyError:
                x0 = float(nodes_from[i]['x'])
                y0 = float(nodes_from[i]['y'])
                x1 = float(nodes_to[i]['x'])
                y1 = float(nodes_to[i]['y'])
                csv.write(',%.3f' % (dist(x0, y0, x1, y1)))

            # Vehicle Types
            vtype_allowed = [0 for item in edge_allow]

            # Has neither tag. It is assumed that all vehicles are allowed.
            if (not purr.xml_has_atrribute(edge, 'allow')) and (
                    not purr.xml_has_atrribute(edge, 'disallow')):
                vtype_allowed = [1 for item in edge_allow]

            # Has the allow tag. Those specified are allowed
            elif (purr.xml_has_atrribute(edge, 'allow')):
                vtypes = edge['allow'].split(' ')
                for j, vt in enumerate(edge_allow):
                    if vt in vtypes:
                        edge_allow[j] = 1
                    continue

            # Lastly it has the dissalow tag, and what is not speficied is allowed
            elif (purr.xml_has_atrribute(edge, 'disallow')):
                vtype_allowed = [1 for item in edge_allow]
                vtypes = edge['allow'].split(' ')
                for j, vt in enumerate(edge_allow):
                    if vt in vtypes:
                        edge_allow[j] = 0
                    continue

            # ~ for j,vt in enumerate(edge_allow):
            # ~ print(edge_allow[j],vtype_allowed[j])

            for vt in vtype_allowed:
                csv.write(',%d' % (vt))
            csv.write('\n')

            n += 1
            purr.update(n, total, 'Writing edges.csv ')
            continue
        pass
        print('\nComplete.')
    return