{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "import json\n", "import sumolib\n", "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def make_match1(path_move):\n", " '''\n", " 신호 DB에는 매 초마다 이동류정보가 업데이트 된다. 그리고 이 이동류정보를 매 5초마다 불러와서 사용하게 된다.\n", " '../Data/tables/move/'에는 5초마다의 이동류정보가 저장되어 있다.\n", "\n", " return : 통합된 이동류정보\n", " - 모든 inter_no(교차로번호)에 대한 A, B링 현시별 이동류정보\n", "\n", " match1을 만드는 데 시간이 소요되므로 한 번 만들어서 저장해두고 저장해둔 것을 쓴다.\n", " '''\n", " # [이동류번호] 불러오기 (약 1분의 소요시간)\n", " csv_moves = os.listdir(path_move)\n", " moves = [pd.read_csv(path_move + csv_move, index_col=0) for csv_move in tqdm(csv_moves)]\n", " match1 = pd.concat(moves).drop_duplicates().sort_values(by=['inter_no','phas_A','phas_B']).reset_index(drop=True)\n", " match1.to_csv('../Intermediates/match1.csv')\n", " return match1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def make_match2(match1):\n", " '''\n", " match1을 계층화함.\n", " - match1의 컬럼 : inter_no, phas_A, phas_B, move_A, move_B\n", " - match2의 컬럼 : inter_no, phase_no, ring_type, move_no\n", " '''\n", " # 계층화 (inter_no, phas_A, phas_B, move_A, move_B) -> ('inter_no', 'phase_no', 'ring_type', 'move_no')\n", " matchA = match1[['inter_no', 'phas_A', 'move_A']].copy()\n", " matchA.columns = ['inter_no', 'phase_no', 'move_no']\n", " matchA['ring_type'] = 'A'\n", " matchB = match1[['inter_no', 'phas_B', 'move_B']].copy()\n", " matchB.columns = ['inter_no', 'phase_no', 'move_no']\n", " matchB['ring_type'] = 'B'\n", " match2 = pd.concat([matchA, matchB]).drop_duplicates()\n", " match2 = match2[['inter_no', 'phase_no', 'ring_type', 'move_no']]\n", " match2 = match2.sort_values(by=list(match2.columns))\n", " return match2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def make_match3(match2, nema):\n", " '''\n", " 각 movement들에 방향(진입방향, 진출방향)을 매칭시켜 추가함.\n", " - match2의 컬럼 : inter_no, phase_no, ring_type, move_no\n", " - match3의 컬럼 : inter_no, phase_no, ring_type, move_no, inc_dir, out_dir\n", "\n", " nema : \n", " - 컬럼 : move_no, inc_dir, out_dir\n", " - 모든 종류의 이동류번호에 대하여 진입방향과 진출방향을 매칭시키는 테이블\n", " - 이동류번호 : 1 ~ 16, 17, 18, 21\n", " - 진입, 진출방향(8방위) : 동, 서, 남, 북, 북동, 북서, 남동, 남서\n", " '''\n", " # nema 정보 불러오기 및 병합\n", " match3 = pd.merge(match2, nema, how='left', on='move_no').drop_duplicates()\n", " return match3" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def make_match4(match3, angle):\n", " '''\n", " 방위각 정보를 매칭시켜 추가함.\n", " - match3의 컬럼 : inter_no, phase_no, ring_type, move_no, inc_dir, out_dir\n", " - match4의 컬럼 : inter_no, phase_no, ring_type, move_no, inc_dir, out_dir, inc_angle, out_angle\n", "\n", " angle_original : \n", " - 컬럼 : inter_no, angle_Aj, angle_Bj (j : 1 ~ 8)\n", " - 모든 종류의 이동류번호에 대하여 진입방향과 진출방향을 매칭시키는 테이블\n", " - 이동류번호 : 1 ~ 16, 17, 18, 21\n", " - 진입, 진출방향(8방위) : 동, 서, 남, 북, 북동, 북서, 남동, 남서\n", " '''\n", "\n", " # 계층화\n", " angles = []\n", " for i, row in angle.iterrows():\n", " angle_codes = row[[f'angle_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]]\n", " new = pd.DataFrame({'inter_no':[row.inter_no] * 16, 'phase_no':list(range(1, 9))*2, 'ring_type':['A'] * 8 + ['B'] * 8, 'angle_code':angle_codes.to_list()})\n", " angles.append(new)\n", " angles = pd.concat(angles)\n", " angles = angles.dropna().reset_index(drop=True)\n", "\n", " # 병합\n", " six_chars = angles.angle_code.apply(lambda x:len(x)==6)\n", " angles.loc[six_chars,'inc_angle'] = angles.angle_code.apply(lambda x:x[:3])\n", " angles.loc[six_chars,'out_angle'] = angles.angle_code.apply(lambda x:x[3:])\n", " angles = angles.drop('angle_code', axis=1)\n", " match4 = pd.merge(match3, angles, how='left', left_on=['inter_no', 'phase_no', 'ring_type'],\n", " right_on=['inter_no', 'phase_no', 'ring_type']).drop_duplicates()\n", " return match4" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def make_match5(match4, net, inter_node, inter_info):\n", " '''\n", " 진입엣지id, 진출엣지id, 노드id를 추가함 (주교차로).\n", " - match4의 컬럼 : inter_no, phase_no, ring_type, move_no, inc_dir, out_dir, inc_angle, out_angle\n", " - match5의 컬럼 : inter_no, phase_no, ring_type, move_no, inc_dir, out_dir, inc_angle, out_angle, inc_edge, out_edge, node_id\n", " \n", " 사용된 데이터 : \n", " (1) net\n", " - 성남시 정자동 부근의 샘플 네트워크\n", " (2) inter_node\n", " - 교차로번호와 노드id를 매칭시키는 테이블.\n", " - parent/child 정보도 포함되어 있음\n", " - 컬럼 : inter_no, node_id, inter_type\n", " (3) inter_info\n", " - 교차로 정보. 여기에서는 위도와 경도가 쓰임.\n", " - 컬럼 : inter_no, inter_name, inter_lat, inter_lon, group_no, main_phase_no\n", "\n", " 진입엣지id, 진출엣지id를 얻는 과정 :\n", " - match5 = match4.copy()의 각 열을 순회하면서 아래 과정을 반복함.\n", " * 진입에 대해서만 서술하겠지만 진출도 마찬가지로 설명될 수 있음\n", " - 해당 행의 교차로정보로부터 노드ID를 얻어내고, 해당 노드에 대한 모든 진출엣지id를 inc_edges에 저장.\n", " * inc_edge(진입엣지) : incoming edge, out_edge(진출엣지) : outgoing_edge\n", " - inc_edges의 모든 진입엣지에 대하여 진입방향(inc_dires, 2차원 단위벡터)을 얻어냄.\n", " - 해당 행의 진입각으로부터 그에 대응되는 진입각방향(단위벡터)를 얻어냄.\n", " - 주어진 진입각방향에 대하여 내적이 가장 작은 진입방향에 대한 진입엣지를 inc_edge_id로 지정함.\n", " '''\n", "\n", " # parent node만 가져옴.\n", " inter_node1 = inter_node[inter_node.inter_type == 'parent'].drop('inter_type', axis=1)\n", " inter_info1 = inter_info[['inter_no', 'inter_lat', 'inter_lon']]\n", " inter = pd.merge(inter_node1, inter_info1, how='left', left_on=['inter_no'],\n", " right_on=['inter_no']).drop_duplicates()\n", "\n", " inter2node = dict(zip(inter['inter_no'], inter['node_id']))\n", "\n", " match5 = match4.copy()\n", " # 진입진출ID 매칭\n", " for index, row in match5.iterrows():\n", " node_id = inter2node[row.inter_no]\n", " node = net.getNode(node_id)\n", " # 교차로의 모든 (from / to) edges\n", " inc_edges = [edge for edge in node.getIncoming() if edge.getFunction() == ''] # incoming edges\n", " out_edges = [edge for edge in node.getOutgoing() if edge.getFunction() == ''] # outgoing edges\n", " # 교차로의 모든 (from / to) directions\n", " inc_dirs = []\n", " for inc_edge in inc_edges:\n", " start = inc_edge.getShape()[-2]\n", " end = inc_edge.getShape()[-1]\n", " inc_dir = np.array(end) - np.array(start)\n", " inc_dir = inc_dir / (inc_dir ** 2).sum() ** 0.5\n", " inc_dirs.append(inc_dir)\n", " out_dirs = []\n", " for out_edge in out_edges:\n", " start = out_edge.getShape()[0]\n", " end = out_edge.getShape()[1]\n", " out_dir = np.array(end) - np.array(start)\n", " out_dir = out_dir / (out_dir ** 2).sum() ** 0.5\n", " out_dirs.append(out_dir)\n", " # 진입각, 진출각 불러오기\n", " if not pd.isna(row.inc_angle):\n", " inc_angle = int(row.inc_angle)\n", " out_angle = int(row.out_angle)\n", " # 방위각을 일반각으로 가공, 라디안 변환, 단위벡터로 변환\n", " inc_angle = (-90 - inc_angle) % 360\n", " inc_angle = inc_angle * np.pi / 180.\n", " inc_dir_true = np.array([np.cos(inc_angle), np.sin(inc_angle)])\n", " out_angle = (90 - out_angle) % 360\n", " out_angle = out_angle * np.pi / 180.\n", " out_dir_true = np.array([np.cos(out_angle), np.sin(out_angle)])\n", " # 매칭 엣지 반환\n", " inc_index = np.array([np.dot(inc_dir, inc_dir_true) for inc_dir in inc_dirs]).argmax()\n", " out_index = np.array([np.dot(out_dir, out_dir_true) for out_dir in out_dirs]).argmax()\n", " inc_edge_id = inc_edges[inc_index].getID()\n", " out_edge_id = out_edges[out_index].getID()\n", " match5.at[index, 'inc_edge'] = inc_edge_id\n", " match5.at[index, 'out_edge'] = out_edge_id\n", " match5['node_id'] = match5['inter_no'].map(inter2node)\n", " match5 = match5.sort_values(by=['inter_no','phase_no','ring_type']).reset_index(drop=True)\n", " return match5" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def make_match6(match5, inter_node, uturn, coord):\n", " '''\n", " 진입엣지id, 진출엣지id, 노드id를 추가함 (부교차로).\n", " - match6의 컬럼 : inter_no, phase_no, ring_type, move_no, inc_dir, out_dir, inc_angle, out_angle, inc_edge, out_edge, node_id\n", " \n", " 사용된 데이터 : \n", " (1) inter_node\n", " - 교차로번호와 노드id를 매칭시키는 테이블.\n", " - parent/child 정보도 포함되어 있음\n", " - 컬럼 : inter_no, node_id, inter_type\n", " (2) uturn (유턴정보)\n", " - 컬럼 : parent_id, child_id, direction, condition, inc_edge, out_edge\n", " - parent_id, child_id : 주교차로id, 유턴교차로id\n", " - direction : 주교차로에 대한 유턴노드의 상대적인 위치(방향)\n", " - condition : 좌회전시, 직진시, 직좌시, 보행신호시 중 하나\n", " - inc_edge, out_edge : 유턴에 대한 진입진출엣지\n", " (3) coord (연동교차로정보)\n", " - 컬럼 : parent_id, child_id, phase_no, ring_type, inc_edge, out_edge\n", " - parent_id, child_id : 주교차로id, 연동교차로id\n", " - 나머지 컬럼 : 각 (현시, 링)별 진입진출엣지\n", "\n", " 설명 :\n", " - match5는 주교차로에 대해서만 진입엣지id, 진출엣지id, 노드id를 추가했었음.\n", " 여기에서 uturn, coord를 사용해서 부교차로들(유턴교차로, 연동교차로)에 대해서도 해당 값들을 부여함.\n", " 유턴교차로 :\n", " - directions를 정북기준 시계방향의 8방위로 정함.\n", " - 이를 통해 진입방향이 주어진 경우에 좌회전, 직진, 보행 등에 대한 (진입방향, 진출방향)을 얻어낼 수 있음.\n", " - 예) 진입방향(direction)이 '북'일 때, \n", " - 직진 : (북, 남)\n", " * 남 : directions[(ind + 4) % len(directions)]\n", " - 좌회전 : (북, 동)\n", " * 동 : directions[(ind + 2) % len(directions)]\n", " - 보행 : (서, 동)\n", " * 서 : directions[(ind - 2) % len(directions)]\n", " - uturn의 각 행을 순회하면서 아래 과정을 반복함\n", " - match5에서 parent_id에 해당하는 행들을 가져옴(cmatch).\n", " - condition 별로 진입방향, 진출방향A, 진출방향B 정함.\n", " - 상술한 directions를 활용하여 정함.\n", " - (진입방향, 진출방향A, 진출방향B)을 고려하여 (현시, 링) 별로 진입엣지id, 진출엣지id를 정함.\n", " - ex) cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " - 순회하면서 만든 cmatch를 cmatchs라는 리스트에 저장함.\n", "\n", " 연동교차로 :\n", " - 연동교차로의 경우 coord에 (현시, 링)별 진입엣지ID, 진출엣지ID가 명시되어 있음.\n", " - 'inc_dir', 'out_dir', 'inc_angle','out_angle'와 같은 열들은 np.nan을 지정해놓음.\n", " - 이 열들은, 사실상 다음 스텝부터는 사용되지 않는 열들이기 때문에 np.nan으로 지정해놓아도 문제없음.\n", "\n", " match6 :\n", " - 이렇게 얻은 match5, cmatchs, coord를 모두 pd.concat하여 match6을 얻어냄.\n", " '''\n", "\n", " node2inter = dict(zip(inter_node['node_id'], inter_node['inter_no']))\n", "\n", " child_ids = inter_node[inter_node.inter_type=='child'].node_id.unique()\n", " ch2pa = {} # child to parent\n", " for child_id in child_ids:\n", " parent_no = inter_node[inter_node.node_id==child_id].inter_no.iloc[0]\n", " sub_inter_node = inter_node[inter_node.inter_no==parent_no]\n", " ch2pa[child_id] = sub_inter_node[sub_inter_node.inter_type=='parent'].iloc[0].node_id\n", " directions = ['북', '북동', '동', '남동', '남', '남서', '서', '북서'] # 정북기준 시계방향으로 8방향\n", "\n", " # 각 uturn node에 대하여 (inc_edge_id, out_edge_id) 부여\n", " cmatches = []\n", " for _, row in uturn.iterrows():\n", " child_id = row.child_id\n", " parent_id = row.parent_id\n", " direction = row.direction\n", " condition = row.condition\n", " inc_edge_id = row.inc_edge\n", " out_edge_id = row.out_edge\n", " # match5에서 parent_id에 해당하는 행들을 가져옴\n", " cmatch = match5.copy()[match5.node_id==parent_id] # match dataframe for a child node\n", " cmatch = cmatch.sort_values(by=['phase_no', 'ring_type']).reset_index(drop=True)\n", " cmatch['node_id'] = child_id\n", " cmatch[['inc_edge', 'out_edge']] = np.nan\n", "\n", " # condition 별로 inc_dire, out_dire_A, out_dire_B를 정함\n", " ind = directions.index(direction)\n", " if condition == \"좌회전시\":\n", " inc_dire = direction\n", " out_dire_A = out_dire_B = directions[(ind + 2) % len(directions)]\n", " elif condition == \"직진시\":\n", " inc_dire = direction\n", " out_dire_A = out_dire_B = directions[(ind + 4) % len(directions)]\n", " elif condition == \"보행신호시\":\n", " inc_dire = directions[(ind + 2) % len(directions)]\n", " out_dire_A = directions[(ind - 2) % len(directions)]\n", " out_dire_B = directions[(ind - 2) % len(directions)]\n", "\n", " # (inc_dire, out_dire_A, out_dire_B) 별로 inc_edge_id, out_edge_id를 정함\n", " if condition == '보행신호시':\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_B), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " # 이동류번호가 17(보행신호)이면서 유턴노드방향으로 가는 신호가 없으면 (inc_edge_id, out_edge_id)를 부여한다.\n", " cmatch.loc[(cmatch.move_no==17) & (cmatch.out_dir!=direction), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " else: # '직진시', '좌회전시'\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_B), ['inc_edge', 'out_edge']] = [inc_edge_id, out_edge_id]\n", " # 유턴신호의 이동류번호를 19로 부여한다.\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_A), 'move_no'] = 19\n", " cmatch.loc[(cmatch.inc_dir==inc_dire) & (cmatch.out_dir==out_dire_B), 'move_no'] = 19\n", " cmatches.append(cmatch)\n", "\n", " # 각 coordination node에 대하여 (inc_edge_id, out_edge_id) 부여\n", " coord['inter_no'] = coord['parent_id'].map(node2inter)\n", " coord = coord.rename(columns={'child_id':'node_id'})\n", " coord[['inc_dir', 'out_dir', 'inc_angle','out_angle']] = np.nan\n", " coord['move_no'] = 20\n", " coord = coord[['inter_no', 'phase_no', 'ring_type', 'move_no', 'inc_dir', 'out_dir', 'inc_angle','out_angle', 'inc_edge', 'out_edge', 'node_id']]\n", " \n", " # display(coord)\n", " cmatches = pd.concat(cmatches)\n", " match6 = pd.concat([match5, cmatches, coord]).drop_duplicates().sort_values(by=['inter_no', 'node_id', 'phase_no', 'ring_type'])\n", " match6.to_csv('../Intermediates/match6.csv')\n", " return match6" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def make_matching(match6, inter_node, nema):\n", " '''\n", " 이동류 매칭 : 각 교차로에 대하여, 가능한 모든 이동류 (1~18, 21)에 대한 진입·진출엣지ID를 지정한다.\n", " 모든 이동류에 대해 지정하므로, 시차제시 이전과 다른 이동류가 등장하더라도 항상 진입·진출 엣지 ID를 지정할 수 있다. \n", " - matching의 컬럼 : inter_no, move_no, inc_dir, out_dir, inc_edge, out_edge, node_id\n", " \n", " 설명 : \n", " - 필요한 리스트, 딕셔너리 등을 정의\n", " (1) 가능한 (진입방향, 진출방향) 목록 [리스트]\n", " (2) 각 교차로별 방향 목록 : pdires (possible directions) [딕셔너리]\n", " (3) 각 (교차로, 진입방향) 별 진입id 목록 : inc2id (incoming direction to incoming edge_id) [딕셔너리]\n", " (4) 각 (교차로, 진출방향) 별 진출id 목록 : out2id (outgoing direction to outgoing edge_id) [딕셔너리]\n", " (5) 각 교차로별 가능한 (진입방향, 진출방향) 목록 : pflow (possible flows) [딕셔너리]\n", " - matching은 빈 리스트로 지정.\n", " - 모든 노드id에 대하여 다음 과정을 반복\n", " - 해당 노드id에 대한 모든 가능한 (진입방향, 진출방향)에 대하여 다음 과정을 반복\n", " - (노드id, 진입방향)으로부터 진입엣지id를 얻어냄. 마찬가지로 진출엣지id도 얻어냄\n", " - 얻어낸 정보를 바탕으로 한 행(new_row)을 만들고 이것을 matching에 append\n", " '''\n", "\n", " match7 = match6.copy()\n", " match7 = match7[['inter_no', 'move_no', 'inc_dir', 'out_dir', 'inc_edge', 'out_edge', 'node_id']]\n", "\n", " parent_ids = sorted(inter_node[inter_node.inter_type=='parent'].node_id.unique())\n", " child_ids = sorted(inter_node[inter_node.inter_type=='child'].node_id.unique())\n", "\n", " # (1) 가능한 (진입방향, 진출방향) 목록 \n", " flows = nema.dropna().apply(lambda row: (row['inc_dir'], row['out_dir']), axis=1).tolist()\n", " # (2) 각 교차로별 방향 목록 : pdires (possible directions)\n", " pdires = {}\n", " for node_id in parent_ids:\n", " dires = match7[match7.node_id == node_id][['inc_dir','out_dir']].values.flatten()\n", " dires = {dire for dire in dires if type(dire)==str}\n", " pdires[node_id] = dires\n", " # (3) 각 (교차로, 진입방향) 별 진입id 목록 : inc2id (incoming direction to incoming edge_id)\n", " inc2id = {}\n", " for node_id in parent_ids:\n", " for inc_dir in pdires[node_id]:\n", " df = match7[(match7.node_id==node_id) & (match7.inc_dir==inc_dir)]\n", " inc2id[(node_id, inc_dir)] = df.inc_edge.iloc[0]\n", " # (4) 각 (교차로, 진출방향) 별 진출id 목록 : out2id (outgoing direction to outgoing edge_id)\n", " out2id = {}\n", " for node_id in parent_ids:\n", " for out_dir in pdires[node_id]:\n", " df = match7[(match7.node_id==node_id) & (match7.out_dir==out_dir)]\n", " out2id[(node_id, out_dir)] = df.out_edge.iloc[0]\n", " # (5) 각 교차로별 가능한 (진입방향, 진출방향) 목록 : pflow (possible flows)\n", " pflow = {}\n", " for node_id in parent_ids:\n", " pflow[node_id] = [flow for flow in flows if set(flow).issubset(pdires[node_id])]\n", " # (6) 가능한 이동류에 대하여 진입id, 진출id 배정 : matching\n", " node2inter = dict(zip(match7['node_id'], match7['inter_no']))\n", " dires_right = ['북', '서', '남', '동', '북'] # ex (북, 서), (서, 남) 등은 우회전 flow\n", " matching = []\n", " for node_id in parent_ids:\n", " inter_no = node2inter[node_id]\n", " # 좌회전과 직진(1 ~ 16)\n", " for (inc_dir, out_dir) in pflow[node_id]:\n", " move_no = nema[(nema.inc_dir==inc_dir) & (nema.out_dir==out_dir)].move_no.iloc[0]\n", " inc_edge = inc2id[(node_id, inc_dir)]\n", " out_edge = out2id[(node_id, out_dir)]\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'move_no':[move_no],\n", " 'inc_dir':[inc_dir], 'out_dir':[out_dir],\n", " 'inc_edge':[inc_edge], 'out_edge':[out_edge], 'node_id':[node_id]})\n", " matching.append(new_row)\n", " # 보행신호(17), 전적색(18)\n", " new_row = pd.DataFrame({'inter_no':[inter_no] * 2, 'move_no':[17, 18],\n", " 'inc_dir':[None]*2, 'out_dir':[None]*2,\n", " 'inc_edge':[None]*2, 'out_edge':[None]*2, 'node_id':[node_id]*2})\n", " matching.append(new_row)\n", " # 신호우회전(21)\n", " for d in range(len(dires_right)-1):\n", " inc_dir = dires_right[d]\n", " out_dir = dires_right[d+1]\n", " if {inc_dir, out_dir}.issubset(pdires[node_id]):\n", " inc_edge = inc2id[(node_id, inc_dir)]\n", " out_edge = out2id[(node_id, out_dir)]\n", " new_row = pd.DataFrame({'inter_no':[inter_no], 'move_no':[21],\n", " 'inc_dir':[inc_dir], 'out_dir':[out_dir],\n", " 'inc_edge':[inc_edge], 'out_edge':[out_edge], 'node_id':[node_id]})\n", " matching.append(new_row)\n", " matching.append(match7[match7.node_id.isin(child_ids)])\n", " matching = pd.concat(matching)\n", " matching = matching.dropna().sort_values(by=['inter_no', 'node_id', 'move_no']).reset_index(drop=True)\n", " matching['move_no'] = matching['move_no'].astype(int)\n", " matching.to_csv('../Intermediates/matching.csv')\n", " return matching" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def make_movements():\n", " movements_path = '../Intermediates/movement/'\n", " movements_list = [pd.read_csv(movements_path + file, index_col=0) for file in tqdm(os.listdir(movements_path))]\n", " movements = pd.concat(movements_list)\n", " movements = movements.drop(columns=['start_unix'])\n", " movements = movements.drop_duplicates()\n", " movements = movements.sort_values(by=['inter_no', 'phas_A', 'phas_B'])\n", " movements = movements.reset_index(drop=True)\n", " movements.to_csv('../Intermediates/movements.csv')\n", " return movements" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# node2num_cycles : A dictionary that maps a node_id to the number of cycles\n", "def get_node2num_cycles(plan, inter_node):\n", " node2inter = dict(zip(inter_node['node_id'], inter_node['inter_no']))\n", " node_ids = sorted(inter_node.node_id.unique())\n", "\n", " Aplan = plan.copy()[['inter_no'] + [f'dura_A{j}' for j in range(1,9)] + ['cycle']]\n", " grouped = Aplan.groupby('inter_no')\n", " df = grouped.agg({'cycle': 'min'}).reset_index()\n", " df = df.rename(columns={'cycle': 'min_cycle'})\n", " df['num_cycle'] = 300 // df['min_cycle'] + 2\n", " inter2num_cycles = dict(zip(df['inter_no'], df['num_cycle']))\n", " node2numcycles = {node_id : inter2num_cycles[node2inter[node_id]] for node_id in node_ids}\n", " with open('../Intermediates/node2numcycles.json', 'w') as file:\n", " json.dump(node2numcycles, file, indent=4)\n", " return node2numcycles" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def preprocess_daily():\n", " path_move = '../Data/tables/move/'\n", "\n", " inter_info = pd.read_csv('../Data/tables/inter_info.csv', index_col=0)\n", " angle = pd.read_csv('../Data/tables/angle.csv', index_col=0, dtype = {f'angle_{alph}{j}':'str' for alph in ['A', 'B'] for j in range(1,9)})\n", " plan = pd.read_csv('../Data/tables/plan.csv', index_col=0)\n", " inter_node = pd.read_csv('../Data/tables/inter_node.csv', index_col=0)\n", " uturn = pd.read_csv('../Data/tables/child_uturn.csv')\n", " coord = pd.read_csv('../Data/tables/child_coord.csv')\n", " nema = pd.read_csv('../Data/tables/nema.csv', encoding='cp949')\n", "\n", " net = sumolib.net.readNet('../Data/networks/sn.net.xml')\n", "\n", " match1 = make_match1(path_move)\n", " match2 = make_match2(match1)\n", " match3 = make_match3(match2, nema)\n", " match4 = make_match4(match3, angle)\n", " match5 = make_match5(match4, net, inter_node, inter_info)\n", " match6 = make_match6(match5, inter_node, uturn, coord)\n", " matching = make_matching(match6, inter_node, nema)\n", " movements = make_movements()\n", " node2num_cycles = get_node2num_cycles(plan, inter_node)\n", " return match6, matching, movements, node2num_cycles" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 0/17280 [00:00= 0) & (durations <= 100)).all(axis=1)\n", "sorted(plan[~valid_indices].inter_no.unique())\n", "# durations" ] } ], "metadata": { "kernelspec": { "display_name": "rts", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 2 }