Esempio n. 1
0
    def create_network(self, nodes_df, links_df):
        ### create graph
        links_df['capacity'] = links_df['lanes'] * 1900
        links_df['fft'] = np.where(links_df['lanes']<=0, 1e8, links_df['length']/links_df['maxmph']*2.2369)
        self.g = interface.from_dataframe(links_df, 'start_node_id', 'end_node_id', 'fft')

        ### Create link and node objects
        nodes = []
        links = []
        for row in nodes_df.itertuples():
            real_node = Node(getattr(row, 'node_id'), getattr(row, 'lon'), getattr(row, 'lat'), getattr(row, 'type'), getattr(row, 'node_osmid'), simulation=self)
            virtual_node = real_node.create_virtual_node()
            virtual_link = real_node.create_virtual_link()
            nodes.append(real_node)
            nodes.append(virtual_node)
            links.append(virtual_link)
        for row in links_df.itertuples():
            real_link = Link(getattr(row, 'link_id'), getattr(row, 'lanes'), getattr(row, 'length'), getattr(row, 'fft'), getattr(row, 'capacity'), getattr(row, 'type'), getattr(row, 'start_node_id'), getattr(row, 'end_node_id'), getattr(row, 'geometry'), simulation=self)
            links.append(real_link)

        ### dictionaries for quick look-up
        self.node2link_dict = {(link.start_nid, link.end_nid): link.lid for link in links}
        self.all_links = {link.lid: link for link in links}
        self.all_nodes = {node.nid: node for node in nodes}
        for link_id, link in self.all_links.items():
            self.all_nodes[link.start_nid].out_links.append(link_id)
            self.all_nodes[link.end_nid].in_links[link_id] = None
        for node_id, node in self.all_nodes.items():
            node.calculate_straight_ahead_links(node_id_dict=self.all_nodes, link_id_dict=self.all_links)
Esempio n. 2
0
    def dataframe_to_network(self,
                             network_file_edges=None,
                             network_file_nodes=None):

        # nodes
        nodes_df = pd.read_csv(network_file_nodes)
        nodes_df = gpd.GeoDataFrame(
            nodes_df,
            crs='epsg:4326',
            geometry=[
                Point(x, y) for (x, y) in zip(nodes_df.lon, nodes_df.lat)
            ]).to_crs('epsg:26910')
        nodes_df['x'] = nodes_df['geometry'].apply(lambda x: x.x)
        nodes_df['y'] = nodes_df['geometry'].apply(lambda x: x.y)
        # edges
        links_df = pd.read_csv(network_file_edges)
        links_df['fft'] = links_df['length'] / links_df['maxmph'] * 2.237
        links_df['capacity'] = 1900 * links_df['lanes']
        links_df = gpd.GeoDataFrame(
            links_df,
            crs='epsg:4326',
            geometry=links_df['geometry'].map(loads)).to_crs('epsg:26910')
        links_df = links_df[[
            'eid', 'nid_s', 'nid_e', 'lanes', 'capacity', 'maxmph', 'fft',
            'length', 'geometry'
        ]]
        # links_df0.to_csv(scratch_dir + simulation_outputs + '/modified_network_edges.csv', index=False)

        ### Convert to mtx
        self.g = interface.from_dataframe(links_df, 'nid_s', 'nid_e', 'fft')

        ### Create link and node objects
        for row in nodes_df.itertuples():
            real_node = Node(getattr(row, 'nid'), getattr(row, 'x'),
                             getattr(row, 'y'), 'real', getattr(row, 'osmid'))
            virtual_node = real_node.create_virtual_node()
            virtual_link = real_node.create_virtual_link()
            self.nodes[real_node.node_id] = real_node
            self.nodes_osmid_dict[real_node.osmid] = real_node.node_id
            self.nodes[virtual_node.node_id] = virtual_node
            self.links[virtual_link.link_id] = virtual_link
            self.node2link_dict[(virtual_link.start_nid,
                                 virtual_link.end_nid)] = virtual_link.link_id
        for row in links_df.itertuples():
            real_link = Link(getattr(row, 'eid'), getattr(row, 'lanes'),
                             getattr(row, 'length'), getattr(row, 'fft'),
                             getattr(row, 'capacity'), 'real',
                             getattr(row, 'nid_s'), getattr(row, 'nid_e'),
                             getattr(row, 'geometry'))
            self.links[real_link.link_id] = real_link
            self.node2link_dict[(real_link.start_nid,
                                 real_link.end_nid)] = real_link.link_id
Esempio n. 3
0
def network(network_file_edges=None,
            network_file_nodes=None,
            simulation_outputs=None,
            cf_files=[],
            scen_nm=''):
    # logger = logging.getLogger("butte_evac")

    links_df0 = pd.read_csv(work_dir + network_file_edges)
    ### contraflow
    if len(cf_files) > 0:
        ### read contraflow links
        cf_links = []
        for cf_file in cf_files:
            cf_link_df = pd.read_csv(work_dir + cf_file)
            cf_links.append(cf_link_df)
        cf_links_df = pd.concat(cf_links)
        ### along counterflow direction
        cf_links_id = cf_links_df.loc[cf_links_df['along'] == 1,
                                      'edge_id_igraph']
        links_df0['lanes'] = np.where(
            links_df0['edge_id_igraph'].isin(cf_links_id),
            links_df0['lanes'] * 2, links_df0['lanes'])
        ### opposite counterflow direction
        opcf_links_id = cf_links_df.loc[cf_links_df['along'] == 0,
                                        'edge_id_igraph']
        links_df0['lanes'] = np.where(
            links_df0['edge_id_igraph'].isin(opcf_links_id), 0,
            links_df0['lanes'])
        links_df0['maxmph'] = np.where(
            links_df0['edge_id_igraph'].isin(opcf_links_id), 0.000001,
            links_df0['maxmph'])

    links_df0['fft'] = links_df0['length'] / links_df0['maxmph'] * 2.237
    links_df0['capacity'] = 1900 * links_df0['lanes']
    links_df0 = links_df0[[
        'eid', 'nid_s', 'nid_e', 'lanes', 'capacity', 'maxmph', 'fft',
        'length', 'geometry'
    ]]
    links_df0.to_csv(scratch_dir + simulation_outputs +
                     '/modified_network_edges.csv',
                     index=False)
    # sys.exit(0)

    nodes_df0 = pd.read_csv(work_dir + network_file_nodes)

    ### Convert to mtx
    # wgh = links_df0['fft']
    # row = links_df0['nid_s']
    # col = links_df0['nid_e']
    # assert max(np.max(row)+1, np.max(col)+1) == nodes_df0.shape[0], 'nodes and links dimension do not match, row {}, col {}, nodes {}'.format(np.max(row), np.max(col), nodes_df0.shape[0])
    # g_coo = ssparse.coo_matrix((wgh, (row, col)), shape=(nodes_df0.shape[0], nodes_df0.shape[0]))
    # logging.info("({}, {}), {}".format(g_coo.shape[0], g_coo.shape[1], len(g_coo.data)))
    # sio.mmwrite(scratch_dir + simulation_outputs + '/network_sparse_{}.mtx'.format(scen_nm), g_coo)
    # g_coo = sio.mmread(absolute_path+'/outputs/network_sparse.mtx'.format(folder))
    # g = interface.readgraph(bytes(scratch_dir + simulation_outputs + '/network_sparse_{}.mtx'.format('tabu'), encoding='utf-8'))
    g = interface.from_dataframe(links_df0, 'nid_s', 'nid_e', 'fft')

    ### Create link and node objects
    nodes = []
    links = []
    for row in nodes_df0.itertuples():
        real_node = Node(getattr(row, 'nid'), getattr(row, 'lon'),
                         getattr(row, 'lat'), 'real', getattr(row, 'osmid'))
        virtual_node = real_node.create_virtual_node()
        virtual_link = real_node.create_virtual_link()
        nodes.append(real_node)
        nodes.append(virtual_node)
        links.append(virtual_link)
    for row in links_df0.itertuples():
        real_link = Link(getattr(row, 'eid'), getattr(row, 'lanes'),
                         getattr(row, 'length'), getattr(row, 'fft'),
                         getattr(row, 'capacity'), 'real',
                         getattr(row, 'nid_s'), getattr(row, 'nid_e'),
                         getattr(row, 'geometry'))
        links.append(real_link)

    return g, nodes, links
Esempio n. 4
0
    def dataframe_to_network(self,
                             project_location=None,
                             network_file_edges=None,
                             network_file_nodes=None,
                             cf_files=None,
                             special_nodes=None,
                             scen_nm=None):

        # nodes
        print(abs_path)
        nodes_df = pd.read_csv(network_file_nodes)
        nodes_df = gpd.GeoDataFrame(
            nodes_df,
            crs='epsg:4326',
            geometry=[
                Point(x, y) for (x, y) in zip(nodes_df.lon, nodes_df.lat)
            ]).to_crs('epsg:26910')
        nodes_df['x'] = nodes_df['geometry'].apply(lambda x: x.x)
        nodes_df['y'] = nodes_df['geometry'].apply(lambda x: x.y)
        nodes_df = nodes_df[['nid', 'x', 'y', 'osmid']]
        # edges
        links_df = pd.read_csv(network_file_edges)
        if (cf_files is not None) and len(cf_files) > 0:
            for cf_file in cf_files:
                contraflow_links_df = pd.read_csv(cf_file)
                contraflow_links_df['new_lanes'] = contraflow_links_df['lanes']
                # print(contraflow_links_df[contraflow_links_df['new_lanes']==0])
                links_df = links_df.merge(
                    contraflow_links_df[['nid_s', 'nid_e', 'new_lanes']],
                    how='left',
                    on=['nid_s', 'nid_e'])
                links_df['lanes'] = np.where(np.isnan(links_df['new_lanes']),
                                             links_df['lanes'],
                                             links_df['new_lanes'])

        links_df['maxmph'] = np.where(links_df['lanes'] == 0, 0.00000001,
                                      links_df['maxmph'])
        links_df['fft'] = links_df['length'] / links_df['maxmph'] * 2.237
        # links_df = links_df.iloc[0:1]
        # set the fft of the longer one in double links to infinite
        # links_df = links_df.sort_values(by='fft', ascending=True)
        # links_df['duplicated'] = links_df[['nid_s', 'nid_e']].duplicated()
        # links_df['fft'] = np.where(links_df['duplicated'], 1e8, links_df['fft'])
        # links_df = links_df.sort_values(by='eid', ascending=True)
        links_df['capacity'] = 1900 * links_df['lanes']
        links_df['capacity'] = np.where(links_df['capacity'] == 0, 0.01,
                                        links_df['capacity'])
        # print(links_df['geometry'].iloc[0])
        links_df = gpd.GeoDataFrame(
            links_df,
            crs='epsg:4326',
            geometry=links_df['geometry'].map(loads)).to_crs('epsg:26910')
        # print(links_df.iloc[0])
        # sys.exit(0)
        links_df = links_df[[
            'eid', 'nid_s', 'nid_e', 'type', 'lanes', 'capacity', 'maxmph',
            'fft', 'length', 'geometry'
        ]]
        links_df.to_csv(
            project_location +
            '/simulation_outputs/network/modified_network_edges_{}.csv'.format(
                scen_nm),
            index=False)
        # sys.exit(0)

        ### link closure
        for closure_link in special_nodes['closure']:
            links_df.loc[links_df['eid'] == closure_link, 'fft'] = 1e8
            links_df.loc[links_df['eid'] == closure_link,
                         'maxmph'] = 0.00000001

        ### turn restrictions
        if len(special_nodes['prohibition'].keys()) == 0: pass
        else:
            new_nodes = []
            new_links = []
            new_node_id = nodes_df.shape[0]
            new_link_id = links_df.shape[0]
            for prohibit_node, prohibit_turns in special_nodes[
                    'prohibition'].items():
                # node to be removed
                prohibit_node = int(prohibit_node)
                # temporarily holding new nodes
                tmp_start_nodes = []
                tmp_end_nodes = []
                for row in links_df.loc[links_df['nid_s'] ==
                                        prohibit_node].itertuples():
                    links_df.loc[links_df['eid'] == getattr(row, 'eid'),
                                 'nid_s'] = new_node_id
                    tmp_point = getattr(row, 'geometry').interpolate(
                        0.1, normalized=True)
                    links_df.loc[links_df['eid'] == getattr(row, 'eid'),
                                 'geometry'] = list(
                                     split(getattr(row, 'geometry'),
                                           tmp_point.buffer(1)))[2]
                    [tmp_x, tmp_y] = list(tmp_point.coords)[0]
                    tmp_start_nodes.append([
                        new_node_id, tmp_x, tmp_y,
                        'n{}_l{}'.format(prohibit_node, getattr(row, 'eid')),
                        getattr(row, 'eid')
                    ])
                    new_node_id += 1
                for row in links_df.loc[links_df['nid_e'] ==
                                        prohibit_node].itertuples():
                    links_df.loc[links_df['eid'] == getattr(row, 'eid'),
                                 'nid_e'] = new_node_id
                    tmp_point = getattr(row, 'geometry').interpolate(
                        0.9, normalized=True)
                    links_df.loc[links_df['eid'] == getattr(row, 'eid'),
                                 'geometry'] = list(
                                     split(getattr(row, 'geometry'),
                                           tmp_point.buffer(1)))[0]
                    [tmp_x, tmp_y] = list(tmp_point.coords)[0]
                    tmp_end_nodes.append([
                        new_node_id, tmp_x, tmp_y,
                        'n{}_l{}'.format(prohibit_node, getattr(row, 'eid')),
                        getattr(row, 'eid')
                    ])
                    new_node_id += 1
                new_nodes += tmp_start_nodes
                new_nodes += tmp_end_nodes
                # prohibit turns
                # print(prohibit_turns)
                # print(tmp_start_nodes)
                # print(tmp_end_nodes)
                prohibit_turns = [(inlink, outlink)
                                  for [inlink, outlink] in prohibit_turns]
                for start_node in tmp_end_nodes:
                    for end_node in tmp_start_nodes:
                        if (start_node[-1],
                                end_node[-1]) not in prohibit_turns:
                            # print((start_node[-1], end_node[-1]), prohibit_turns)
                            new_links.append([
                                new_link_id, start_node[0], end_node[0],
                                start_node[1], start_node[2], end_node[1],
                                end_node[2]
                            ])
                            new_link_id += 1
            new_links_df = pd.DataFrame(new_links,
                                        columns=[
                                            'eid', 'nid_s', 'nid_e', 'start_x',
                                            'start_y', 'end_x', 'end_y'
                                        ])
            new_links_df['lanes'] = 100
            new_links_df['capacity'] = 100000
            new_links_df['maxmph'] = 100000
            new_links_df['fft'] = 0
            new_links_df['length'] = 0
            new_links_df['geometry'] = new_links_df.apply(lambda x: LineString(
                [[x['start_x'], x['start_y']], [x['end_x'], x['end_y']]]),
                                                          axis=1)
            new_links_df = new_links_df[[
                'eid', 'nid_s', 'nid_e', 'lanes', 'capacity', 'maxmph', 'fft',
                'length', 'geometry'
            ]]
            links_df = pd.concat([links_df, new_links_df])
            # new nodes
            new_nodes_df = pd.DataFrame(
                new_nodes, columns=['nid', 'x', 'y', 'osmid', 'link'])
            print(nodes_df.shape, new_nodes_df.shape)
            nodes_df = pd.concat(
                [nodes_df, new_nodes_df[['nid', 'x', 'y', 'osmid']]])
            print(nodes_df.shape)
            nodes_df.to_csv('nodes.csv', index=False)
            links_df.to_csv('links.csv', index=False)

        ### Convert to mtx
        self.g = interface.from_dataframe(links_df, 'nid_s', 'nid_e', 'fft')

        ### Create link and node objects
        for row in nodes_df.itertuples():
            real_node = Node(getattr(row, 'nid'), getattr(row, 'x'),
                             getattr(row, 'y'), 'real', getattr(row, 'osmid'))
            virtual_node = real_node.create_virtual_node()
            virtual_link = real_node.create_virtual_link()
            self.nodes[real_node.node_id] = real_node
            self.nodes_osmid_dict[real_node.osmid] = real_node.node_id
            self.nodes[virtual_node.node_id] = virtual_node
            self.links[virtual_link.link_id] = virtual_link
            self.node2link_dict[(virtual_link.start_nid,
                                 virtual_link.end_nid)] = virtual_link.link_id
        for row in links_df.itertuples():
            real_link = Link(getattr(row, 'eid'), getattr(row, 'lanes'),
                             getattr(row, 'length'), getattr(row, 'fft'),
                             getattr(row, 'capacity'), getattr(row, 'type'),
                             getattr(row, 'nid_s'), getattr(row, 'nid_e'),
                             getattr(row, 'geometry'))
            self.links[real_link.link_id] = real_link
            ### deal with duplicated links: keep the faster ones
            if (real_link.start_nid,
                    real_link.end_nid) in self.node2link_dict.keys():
                if real_link.fft < self.links[self.node2link_dict[(
                        real_link.start_nid, real_link.end_nid)]].fft:
                    self.node2link_dict[(
                        real_link.start_nid,
                        real_link.end_nid)] = real_link.link_id
                else:
                    pass
            else:
                self.node2link_dict[(real_link.start_nid,
                                     real_link.end_nid)] = real_link.link_id
Esempio n. 5
0
def query_path(vphh=None,
               visitor_cnts=None,
               player_origin=None,
               player_destin=None,
               start_time=None,
               end_time=None,
               read_path=None):

    if read_path == None:
        read_path = abs_path

    ### get graph
    links_df = pd.read_csv(
        read_path +
        '/simulation_outputs/network/modified_network_edges_vphh{}_visitor{}.csv'
        .format(vphh, visitor_cnts))
    network_g = interface.from_dataframe(links_df, 'nid_s', 'nid_e', 'fft')

    network_links = json.load(
        open(read_path + '/simulation_outputs/network/network_links.json'))
    node2link_dict = json.load(
        open(read_path + '/simulation_outputs/network/node2link_dict.json'))

    link_speed_dict = json.load(
        open(
            read_path +
            '/simulation_outputs/link_weights/link_speed_vphh{}_visitor{}.json'
            .format(vphh, visitor_cnts)))
    link_length_dict = {
        getattr(l, 'eid'): getattr(l, 'length')
        for l in links_df.itertuples()
    }

    current_link = 'n{}_vl'.format(player_origin)
    current_link_distance = 0
    # current_link_angle = 0

    # all nodes that player passed
    player_nodes = [player_origin]
    player_nodes_time_traffic = []

    for t_p in range(start_time, end_time):
        if t_p > start_time:
            current_link_distance += link_speed_dict[str(t_p)][current_link]

        if (t_p == start_time) or (current_link_distance >=
                                   network_links[str(current_link)]['length']):

            if network_links[str(current_link)]['end_nid'] == player_destin:
                print('Reach destination at {} seconds'.format(t_p))
                break

            # extra distance to next link
            current_link_distance = current_link_distance - network_links[str(
                current_link)]['length']

            # reroute at intersection
            links_df['current_travel_time'] = link_speed_dict[str(t_p)]
            network_g = interface.from_dataframe(links_df, 'nid_s', 'nid_e',
                                                 'current_travel_time')
            player_route_by_nodes = get_route(
                network_g, network_links[str(current_link)]['end_nid'],
                player_destin)
            # print(player_route_by_nodes)

            ### move agent to to chosen link
            next_node = player_route_by_nodes[network_links[str(current_link)]
                                              ['end_nid']]
            current_link = node2link_dict[str(
                network_links[str(current_link)]['end_nid'])][str(next_node)]
            player_nodes.append(network_links[str(current_link)]['end_nid'])
            player_nodes_time_traffic.append(
                (network_links[str(current_link)]['start_nid'], t_p,
                 link_speed_dict[str(t_p)][current_link],
                 link_length_dict[current_link]))
            # print('new link {} at {}\n'.format(current_link, t_p))

    print(
        'vehicle is on link {} at {} seconds. The end node ID of the current link is {}'
        .format(current_link, end_time, next_node))
    print('Player path nodes {}'.format(player_nodes))
    print('Player path nodes/time/speed_of_next_link/length_of_next_link {}'.
          format(player_nodes_time_traffic))
    # traffic: light: > 9 m/s, medium: 3-9 m/s, heavy: < 3 m/s

    return player_nodes, player_nodes_time_traffic