示例#1
0
def update_nash():

    # Clean out the old object
    env.vehicles_active = []

    # Refill
    for i, vid in enumerate(env.vids_active):
        veh = dict()

        # Who am I?
        veh['id'] = vid

        # Where am I?
        veh['route index'] = env.traci.vehicle.getRouteIndex(veh['id'])

        # What is my route?
        veh['route id'] = env.traci.vehicle.getRouteID(veh['id'])

        # What is my sink node?
        veh['destination node'] = env.vehicles_dest[int(veh['id'][3:])]

        # How far along the edge am I (m)?
        veh['position on edge (m)'] = env.traci.vehicle.getLanePosition(
            veh['id'])

        # What edge am I on?
        eid = env.traci.vehicle.getRoadID(veh['id'])

        # Obtain the edge object. Here we can have two cases:
        #  1. veh is at an intersection
        #  2. veh is on an edg

        # Veh is within an intersection. Assume the start of the next edge with a position on edge (m) of zero.
        if ':' in eid:
            veh['route'] = env.traci.route.getEdges(veh['route id'])
            eid = veh['route'][veh['route index'] + 1]
            veh['current edge'] = purr.filterdicts(env.edges, 'id', eid)[0]
            veh['position on edge (m)'] = 0.000001
        else:
            veh['current edge'] = purr.filterdicts(env.edges, 'id', eid)[0]

        # How far am I along the edge (weight)
        veh['position on edge (s)'] = veh['position on edge (m)'] / float(
            veh['current edge']['speed'])

        # How much weight to the end of the edge?
        edge_candidates = env.nx.get_edge_data(veh['current edge']['from'],
                                               veh['current edge']['to'])
        for i in range(len(edge_candidates)):
            if veh['current edge']['id'] == edge_candidates[i]['id']:
                veh['weight remaining'] = edge_candidates[i]['weight'] - veh[
                    'position on edge (s)']
                break
            continue

        env.vehicles_active.append(veh)
        continue

    # ~ print('active vehicles',len(env.vehicles_active))
    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
def add():
    # Vehicle ID
    index = env.veh_id_counter
    env.veh_id_counter += 1
    vid = "veh%d" % (index)
    
    # Retrieve Route
    try:
        veh = env.vehicles[index]
    except IndexError:
        print(index,len(env.vehicles))
        purr.pause()
    
    # Create a route with the path eids
    rid = "route%d" % (env.route_id_counter)
    env.route_id_counter += 1
    env.traci.route.add(rid,veh['shortest path']['eids'])
    
    # Add a vehicle
    env.traci.vehicle.add(vid,rid)
    
    # Add the destination node to list of sink nodes so it may be found later
    dest_node = purr.filterdicts(env.nodes,'id',veh['shortest path']['nids'][-1])[0]
    env.vehicles_dest.append(dest_node)
    
    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
示例#5
0
def add():
    # Vehicle ID
    index = env.veh_id_counter
    env.veh_id_counter += 1
    vid = "veh%d" % (index)

    # Retrieve Route
    veh = env.vehicles[index]

    # Create a route with the path eids
    rid = "route%d" % (env.route_id_counter)
    env.route_id_counter += 1
    env.traci.route.add(rid, veh['shortest path']['eids'])

    # Add a vehicle
    env.traci.vehicle.add(vid, rid)

    # Add the destination node to list of sink nodes so it may be found later
    dest_node = purr.filterdicts(env.nodes, 'id',
                                 veh['shortest path']['nids'][-1])[0]
    env.vehicles_dest.append(dest_node)

    env.recalculate_nash = True
    env.update_vehicle_info = True

    return
示例#6
0
def add_nash(traci):
    # Vehicle ID
    vid = "veh%d" % (env.veh_id_counter)
    env.veh_id_counter += 1

    # Selected a path. It must be have a conenction between spawn and sink
    ntrys = 0
    shortest_path = {
        'weight': None,
        'nids': None,
        'eids': None
    }
    while True:
        # Pick a random spawn and sink edge
        spawn_edge = random.choice(env.spawn_edge)
        sink_edge = random.choice(env.sink_edge)

        # Determine the shortest path
        try:
            shortest_path['nids'] = nxops.dijkstra(env.nx, spawn_edge['from'],
                                                   sink_edge['to'])
            break
        except nx.NetworkXNoPath:
            ntrys += 1
            print(
                "%s: Cannont create a path between %s and %s... Trying again (%d)."
                % (vid, spawn_edge['from'], sink_edge['to'], ntrys))
        continue
    shortest_path['weight'], shortest_path['eids'] = nxops.path_info(
        env.nx, shortest_path['nids'])

    # Create a route with the path eids
    rid = "route%d" % (env.route_id_counter)
    env.route_id_counter += 1
    traci.route.add(rid, shortest_path['eids'])

    # Add a vehicle
    traci.vehicle.add(vid, rid)

    # Add the destination node to list of sink nodes so it may be found later
    dest_node = purr.filterdicts(env.nodes, 'id', shortest_path['nids'][-1])[0]
    env.vehicles_dest.append(dest_node)

    # The nash equilibrium will have to be recalculated since another contestant is being introduced.
    env.recalculate_nash = True

    # Create the vehicle dictionary
    veh = {
        "id": vid,
        "source": spawn_edge['from'],
        "destination": sink_edge['to'],
        "shortest path length": shortest_path['weight'],
        "diversion path length": None,
        "sample time": None,
        "target nid": None,
        "target eid": None
    }
    env.vehicles.append(veh)

    return
示例#7
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
def update():
    env.update_vehicle_info = False
    
    # Clean out the old object 
    env.vehicles_active = []
    
    # Refill 
    total = len(env.vehicles)
    for i,v in enumerate(env.vehicles):
        veh = dict()
            
        # Who am I?
        veh['id'] = v['id']
        
        # Where am I?
        veh['route index'] = env.traci.vehicle.getRouteIndex(veh['id'])
        
        # What is my route?
        veh['route id'] = env.traci.vehicle.getRouteID(veh['id'])
        
        # What is my sink node?
        veh['destination node'] = env.vehicles_dest[int(veh['id'][3:])]
        
        if veh['route index'] < 0:
            veh['route index'] = 0
            veh['position on edge (m)'] = 0.000001
            eid = env.traci.route.getEdges(veh['route id'])[0]
        else:
        
            # How far along the edge am I (m)?
            veh['position on edge (m)'] = env.traci.vehicle.getLanePosition(veh['id'])
            
            # What edge am I on?
            eid = env.traci.vehicle.getRoadID(veh['id'])
        
        # Obtain the edge object. Here we can have two cases:
        #  1. veh is at an intersection
        #  2. veh is on an edg
        
        # Veh is within an intersection. Assume the start of the next edge with a position on edge (m) of zero.
        if ':' in eid:
            veh['route'] = env.traci.route.getEdges(veh['route id'])
            eid = veh['route'][veh['route index']+1]
            veh['current edge'] = purr.filterdicts(env.edges,'id',eid)[0]
            veh['position on edge (m)'] = 0.000001
        else:
            veh['current edge'] = purr.filterdicts(env.edges,'id',eid)[0]
            
        # How far am I along the edge (weight)
        veh['position on edge (s)'] = veh['position on edge (m)'] / float(veh['current edge']['speed'])
            
        # How much weight to the end of the edge?
        edge_candidates = env.nx.get_edge_data(veh['current edge']['from'],veh['current edge']['to'])
        for j in range(len(edge_candidates)):
            if veh['current edge']['id'] == edge_candidates[j]['id']:
                veh['weight remaining'] = edge_candidates[j]['weight'] - veh['position on edge (s)']
                break
            continue
            
        # What is the weight of Veh --> Dest
        veh['veh2dest weight'] = veh['weight remaining'] + nxops.path_info(env.nx,nx.dijkstra_path(env.nx,veh['current edge']['to'],veh['destination node']['id']))[0]

        env.vehicles_active.append(veh)
        
        purr.update(i+1,total,"Updating vehicle location data ")
        # ~ env.recalculate_nash = True
        continue
    print()
    return