{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sys\n",
    "sys.path.append('../../Scripts')\n",
    "from preprocess_daily import DailyPreprocessor\n",
    "from generate_signals import SignalGenerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 데이터를 로드합니다.\n",
      "1-1. 네트워크가 로드되었습니다.\n",
      "1-2. 테이블들이 로드되었습니다.\n",
      "1-3. 네트워크의 모든 clean state requirement들을 체크했습니다.\n",
      "1-4. 테이블들의 무결성 검사를 완료했습니다.\n",
      "1-5. 주요 객체 (리스트, 딕셔너리)들을 저장했습니다.\n"
     ]
    }
   ],
   "source": [
    "self = DailyPreprocessor()\n",
    "self.load_data() # 1. 데이터 불러오기\n",
    "self.make_match1() # 2-1-1\n",
    "self.make_match2() # 2-1-2\n",
    "self.make_match3() # 2-1-3\n",
    "self.make_match4() # 2-1-4\n",
    "self.make_match5() # 2-1-5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>node_id</th>\n",
       "      <th>inc_edge_id</th>\n",
       "      <th>out_edge_id</th>\n",
       "      <th>turn_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>i8</td>\n",
       "      <td>-571500569_01</td>\n",
       "      <td>571500583_02</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>i8</td>\n",
       "      <td>-571500569_01</td>\n",
       "      <td>571500618_01</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500618_02</td>\n",
       "      <td>571500583_02</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500618_02</td>\n",
       "      <td>571500617_01</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500617_02</td>\n",
       "      <td>571500618_01</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500618_02</td>\n",
       "      <td>571500617_01</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500617_02</td>\n",
       "      <td>571500618_01</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500617_02</td>\n",
       "      <td>571500569_01</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500583_01</td>\n",
       "      <td>571500617_01</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>i8</td>\n",
       "      <td>571500583_01</td>\n",
       "      <td>571500569_01</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   node_id    inc_edge_id   out_edge_id turn_type\n",
       "23      i8  -571500569_01  571500583_02  straight\n",
       "24      i8  -571500569_01  571500618_01      left\n",
       "25      i8   571500618_02  571500583_02      left\n",
       "26      i8   571500618_02  571500617_01  straight\n",
       "27      i8   571500617_02  571500618_01  straight\n",
       "28      i8   571500618_02  571500617_01  straight\n",
       "29      i8   571500617_02  571500618_01  straight\n",
       "30      i8   571500617_02  571500569_01      left\n",
       "31      i8   571500583_01  571500617_01      left\n",
       "32      i8   571500583_01  571500569_01  straight"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "self.turn_type[self.turn_type.node_id=='i8']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "self.match5 = self.match4.copy()\n",
    "# 진입진출ID 매칭\n",
    "for index, row in self.match5.iterrows():\n",
    "    node_id = self.inter2node[row.inter_no]\n",
    "    node = self.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) unit vector\n",
    "    inc_vecs = []\n",
    "    for inc_edge in inc_edges:\n",
    "        start = inc_edge.getShape()[-1]\n",
    "        end = inc_edge.getShape()[-2]\n",
    "        inc_vec = np.array(end) - np.array(start)\n",
    "        inc_vec = inc_vec / (inc_vec ** 2).sum() ** 0.5\n",
    "        inc_vecs.append(inc_vec)\n",
    "    out_vecs = []\n",
    "    for out_edge in out_edges:\n",
    "        start = out_edge.getShape()[0]\n",
    "        end = out_edge.getShape()[1]\n",
    "        out_vec = np.array(end) - np.array(start)\n",
    "        out_vec = out_vec / (out_vec ** 2).sum() ** 0.5\n",
    "        out_vecs.append(out_vec)\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_vec_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_vec_true = np.array([np.cos(out_angle), np.sin(out_angle)])\n",
    "        # 매칭 엣지 반환\n",
    "        inc_index = np.array([np.dot(inc_vec, inc_vec_true) for inc_vec in inc_vecs]).argmax()\n",
    "        out_index = np.array([np.dot(out_vec, out_vec_true) for out_vec in out_vecs]).argmax()\n",
    "        inc_edge_id = inc_edges[inc_index].getID()\n",
    "        out_edge_id = out_edges[out_index].getID()\n",
    "        self.match5.at[index, 'inc_edge_id'] = inc_edge_id\n",
    "        self.match5.at[index, 'out_edge_id'] = out_edge_id\n",
    "self.match5['node_id'] = self.match5['inter_no'].map(self.inter2node)\n",
    "self.match5 = self.match5.sort_values(by=['inter_no','phase_no','ring_type']).reset_index(drop=True)\n",
    "\n",
    "# n2io2turn : dictionary that maps node_id to io2turn\n",
    "self.n2io2turn = dict()\n",
    "for node_id in self.parent_ids:\n",
    "    turn = self.turn_type[self.turn_type.node_id==node_id]\n",
    "    io = list(zip(turn.inc_edge_id, turn.out_edge_id))\n",
    "    # io2turn : dictionary that maps (inc_edge_id, out_edge_id) to turn_type\n",
    "    io2turn = dict(zip(io, turn.turn_type))\n",
    "    self.n2io2turn[node_id] = io2turn\n",
    "\n",
    "# turn_type 지정\n",
    "for i, row in self.match5.iterrows():\n",
    "    node_id = row.node_id\n",
    "    inc_edge_id = row.inc_edge_id\n",
    "    out_edge_id = row.out_edge_id\n",
    "    if not (pd.isna(inc_edge_id) and pd.isna(out_edge_id)):\n",
    "        turn_type = self.n2io2turn[node_id][(inc_edge_id, out_edge_id)]\n",
    "        self.match5.at[i, 'turn_type'] = turn_type\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 데이터를 로드합니다.\n",
      "1-1. 네트워크가 로드되었습니다.\n",
      "1-2. 테이블들이 로드되었습니다.\n",
      "1-3. 네트워크의 모든 clean state requirement들을 체크했습니다.\n",
      "1-4. 테이블들의 무결성 검사를 완료했습니다.\n",
      "1-5. 주요 객체 (리스트, 딕셔너리)들을 저장했습니다.\n",
      "2-1. 매칭 테이블들을 생성했습니다.\n",
      "2-2. 초기화 신호가 지정되었습니다. (우회전 : g)\n"
     ]
    }
   ],
   "source": [
    "self = DailyPreprocessor() \n",
    "self.load_data() # 1\n",
    "self.get_matches() # 2\n",
    "self.initialize_state() # 2-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>inter_no</th>\n",
       "      <th>phase_no</th>\n",
       "      <th>ring_type</th>\n",
       "      <th>move_no</th>\n",
       "      <th>inc_dire</th>\n",
       "      <th>out_dire</th>\n",
       "      <th>inc_angle</th>\n",
       "      <th>out_angle</th>\n",
       "      <th>inc_edge_id</th>\n",
       "      <th>out_edge_id</th>\n",
       "      <th>node_id</th>\n",
       "      <th>turn_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>175</td>\n",
       "      <td>1</td>\n",
       "      <td>A</td>\n",
       "      <td>8</td>\n",
       "      <td>남</td>\n",
       "      <td>북</td>\n",
       "      <td>179</td>\n",
       "      <td>000</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571500487_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175</td>\n",
       "      <td>1</td>\n",
       "      <td>B</td>\n",
       "      <td>4</td>\n",
       "      <td>북</td>\n",
       "      <td>남</td>\n",
       "      <td>001</td>\n",
       "      <td>180</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571542797_02</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>2</td>\n",
       "      <td>A</td>\n",
       "      <td>7</td>\n",
       "      <td>북</td>\n",
       "      <td>동</td>\n",
       "      <td>001</td>\n",
       "      <td>090</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571545870_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>175</td>\n",
       "      <td>2</td>\n",
       "      <td>B</td>\n",
       "      <td>3</td>\n",
       "      <td>남</td>\n",
       "      <td>서</td>\n",
       "      <td>179</td>\n",
       "      <td>270</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>175</td>\n",
       "      <td>3</td>\n",
       "      <td>A</td>\n",
       "      <td>6</td>\n",
       "      <td>동</td>\n",
       "      <td>서</td>\n",
       "      <td>090</td>\n",
       "      <td>270</td>\n",
       "      <td>571545870_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>175</td>\n",
       "      <td>3</td>\n",
       "      <td>B</td>\n",
       "      <td>1</td>\n",
       "      <td>동</td>\n",
       "      <td>남</td>\n",
       "      <td>090</td>\n",
       "      <td>180</td>\n",
       "      <td>571545870_02</td>\n",
       "      <td>571542797_02</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>175</td>\n",
       "      <td>4</td>\n",
       "      <td>A</td>\n",
       "      <td>5</td>\n",
       "      <td>서</td>\n",
       "      <td>북</td>\n",
       "      <td>270</td>\n",
       "      <td>000</td>\n",
       "      <td>571510153_02</td>\n",
       "      <td>571500487_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>175</td>\n",
       "      <td>4</td>\n",
       "      <td>B</td>\n",
       "      <td>2</td>\n",
       "      <td>서</td>\n",
       "      <td>동</td>\n",
       "      <td>270</td>\n",
       "      <td>090</td>\n",
       "      <td>571510153_02</td>\n",
       "      <td>571545870_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   inter_no  phase_no ring_type  move_no inc_dire out_dire inc_angle  \\\n",
       "0       175         1         A        8        남        북       179   \n",
       "1       175         1         B        4        북        남       001   \n",
       "2       175         2         A        7        북        동       001   \n",
       "3       175         2         B        3        남        서       179   \n",
       "4       175         3         A        6        동        서       090   \n",
       "5       175         3         B        1        동        남       090   \n",
       "6       175         4         A        5        서        북       270   \n",
       "7       175         4         B        2        서        동       270   \n",
       "\n",
       "  out_angle    inc_edge_id   out_edge_id node_id turn_type  \n",
       "0       000  -571542797_02  571500487_01      i0  straight  \n",
       "1       180  -571500487_01  571542797_02      i0  straight  \n",
       "2       090  -571500487_01  571545870_01      i0      left  \n",
       "3       270  -571542797_02  571510153_01      i0      left  \n",
       "4       270   571545870_02  571510153_01      i0  straight  \n",
       "5       180   571545870_02  571542797_02      i0      left  \n",
       "6       000   571510153_02  571500487_01      i0      left  \n",
       "7       090   571510153_02  571545870_01      i0  straight  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m5 = self.match5[(self.match5.node_id=='i0')]\n",
    "display(m5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>inter_no</th>\n",
       "      <th>phase_no</th>\n",
       "      <th>ring_type</th>\n",
       "      <th>move_no</th>\n",
       "      <th>inc_dire</th>\n",
       "      <th>out_dire</th>\n",
       "      <th>inc_angle</th>\n",
       "      <th>out_angle</th>\n",
       "      <th>inc_edge_id</th>\n",
       "      <th>out_edge_id</th>\n",
       "      <th>node_id</th>\n",
       "      <th>turn_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>175</td>\n",
       "      <td>1</td>\n",
       "      <td>A</td>\n",
       "      <td>8</td>\n",
       "      <td>남</td>\n",
       "      <td>북</td>\n",
       "      <td>179</td>\n",
       "      <td>000</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571500487_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175</td>\n",
       "      <td>1</td>\n",
       "      <td>B</td>\n",
       "      <td>4</td>\n",
       "      <td>북</td>\n",
       "      <td>남</td>\n",
       "      <td>001</td>\n",
       "      <td>180</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571542797_02</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>2</td>\n",
       "      <td>A</td>\n",
       "      <td>7</td>\n",
       "      <td>북</td>\n",
       "      <td>동</td>\n",
       "      <td>001</td>\n",
       "      <td>090</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571545870_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>175</td>\n",
       "      <td>2</td>\n",
       "      <td>B</td>\n",
       "      <td>3</td>\n",
       "      <td>남</td>\n",
       "      <td>서</td>\n",
       "      <td>179</td>\n",
       "      <td>270</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>175</td>\n",
       "      <td>3</td>\n",
       "      <td>A</td>\n",
       "      <td>6</td>\n",
       "      <td>동</td>\n",
       "      <td>서</td>\n",
       "      <td>090</td>\n",
       "      <td>270</td>\n",
       "      <td>571545870_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   inter_no  phase_no ring_type  move_no inc_dire out_dire inc_angle  \\\n",
       "0       175         1         A        8        남        북       179   \n",
       "1       175         1         B        4        북        남       001   \n",
       "2       175         2         A        7        북        동       001   \n",
       "3       175         2         B        3        남        서       179   \n",
       "4       175         3         A        6        동        서       090   \n",
       "\n",
       "  out_angle    inc_edge_id   out_edge_id node_id turn_type  \n",
       "0       000  -571542797_02  571500487_01      i0  straight  \n",
       "1       180  -571500487_01  571542797_02      i0  straight  \n",
       "2       090  -571500487_01  571545870_01      i0      left  \n",
       "3       270  -571542797_02  571510153_01      i0      left  \n",
       "4       270   571545870_02  571510153_01      i0  straight  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>inter_no</th>\n",
       "      <th>phase_no</th>\n",
       "      <th>ring_type</th>\n",
       "      <th>move_no</th>\n",
       "      <th>inc_dire</th>\n",
       "      <th>out_dire</th>\n",
       "      <th>inc_angle</th>\n",
       "      <th>out_angle</th>\n",
       "      <th>inc_edge_id</th>\n",
       "      <th>out_edge_id</th>\n",
       "      <th>node_id</th>\n",
       "      <th>turn_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>175</td>\n",
       "      <td>1</td>\n",
       "      <td>A</td>\n",
       "      <td>8</td>\n",
       "      <td>남</td>\n",
       "      <td>북</td>\n",
       "      <td>179</td>\n",
       "      <td>000</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571500487_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175</td>\n",
       "      <td>1</td>\n",
       "      <td>B</td>\n",
       "      <td>4</td>\n",
       "      <td>북</td>\n",
       "      <td>남</td>\n",
       "      <td>001</td>\n",
       "      <td>180</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571542797_02</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>2</td>\n",
       "      <td>A</td>\n",
       "      <td>7</td>\n",
       "      <td>북</td>\n",
       "      <td>동</td>\n",
       "      <td>001</td>\n",
       "      <td>090</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571545870_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>175</td>\n",
       "      <td>2</td>\n",
       "      <td>B</td>\n",
       "      <td>3</td>\n",
       "      <td>남</td>\n",
       "      <td>서</td>\n",
       "      <td>179</td>\n",
       "      <td>270</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>175</td>\n",
       "      <td>3</td>\n",
       "      <td>A</td>\n",
       "      <td>6</td>\n",
       "      <td>동</td>\n",
       "      <td>서</td>\n",
       "      <td>090</td>\n",
       "      <td>270</td>\n",
       "      <td>571545870_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>i0</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   inter_no  phase_no ring_type  move_no inc_dire out_dire inc_angle  \\\n",
       "0       175         1         A        8        남        북       179   \n",
       "1       175         1         B        4        북        남       001   \n",
       "2       175         2         A        7        북        동       001   \n",
       "3       175         2         B        3        남        서       179   \n",
       "4       175         3         A        6        동        서       090   \n",
       "\n",
       "  out_angle    inc_edge_id   out_edge_id node_id turn_type  \n",
       "0       000  -571542797_02  571500487_01      i0  straight  \n",
       "1       180  -571500487_01  571542797_02      i0  straight  \n",
       "2       090  -571500487_01  571545870_01      i0      left  \n",
       "3       270  -571542797_02  571510153_01      i0      left  \n",
       "4       270   571545870_02  571510153_01      i0  straight  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>inter_no</th>\n",
       "      <th>node_id</th>\n",
       "      <th>move_no</th>\n",
       "      <th>inc_dire</th>\n",
       "      <th>out_dire</th>\n",
       "      <th>inc_edge_id</th>\n",
       "      <th>out_edge_id</th>\n",
       "      <th>turn_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>175</td>\n",
       "      <td>i0</td>\n",
       "      <td>1</td>\n",
       "      <td>동</td>\n",
       "      <td>남</td>\n",
       "      <td>571545870_02</td>\n",
       "      <td>571542797_02</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>175</td>\n",
       "      <td>i0</td>\n",
       "      <td>2</td>\n",
       "      <td>서</td>\n",
       "      <td>동</td>\n",
       "      <td>571510153_02</td>\n",
       "      <td>571545870_01</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>i0</td>\n",
       "      <td>3</td>\n",
       "      <td>남</td>\n",
       "      <td>서</td>\n",
       "      <td>-571542797_02</td>\n",
       "      <td>571510153_01</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>175</td>\n",
       "      <td>i0</td>\n",
       "      <td>4</td>\n",
       "      <td>북</td>\n",
       "      <td>남</td>\n",
       "      <td>-571500487_01</td>\n",
       "      <td>571542797_02</td>\n",
       "      <td>straight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>175</td>\n",
       "      <td>i0</td>\n",
       "      <td>5</td>\n",
       "      <td>서</td>\n",
       "      <td>북</td>\n",
       "      <td>571510153_02</td>\n",
       "      <td>571500487_01</td>\n",
       "      <td>left</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   inter_no node_id  move_no inc_dire out_dire    inc_edge_id   out_edge_id  \\\n",
       "0       175      i0        1        동        남   571545870_02  571542797_02   \n",
       "1       175      i0        2        서        동   571510153_02  571545870_01   \n",
       "2       175      i0        3        남        서  -571542797_02  571510153_01   \n",
       "3       175      i0        4        북        남  -571500487_01  571542797_02   \n",
       "4       175      i0        5        서        북   571510153_02  571500487_01   \n",
       "\n",
       "  turn_type  \n",
       "0      left  \n",
       "1  straight  \n",
       "2      left  \n",
       "3  straight  \n",
       "4      left  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(self.match5.head())\n",
    "display(self.match6.head())\n",
    "display(self.matching.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 데이터를 로드합니다.\n",
      "1-1. 네트워크가 로드되었습니다.\n",
      "1-2. 테이블들이 로드되었습니다.\n",
      "1-3. 네트워크의 모든 clean state requirement들을 체크했습니다.\n",
      "1-4. 테이블들의 무결성 검사를 완료했습니다.\n",
      "1-5. 주요 객체 (리스트, 딕셔너리)들을 저장했습니다.\n",
      "2-1. 매칭 테이블들을 생성했습니다.\n",
      "2-2. 초기화 신호가 지정되었습니다. (우회전 : g)\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "self = DailyPreprocessor()\n",
    "self.load_data() # 1. 데이터 불러오기\n",
    "self.get_matches() # 2-1 매칭테이블 생성\n",
    "self.initialize_state() # 2-2 신호 초기화"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "self.p2UPLindices = dict() # parent id to unprotected left index\n",
    "for parent_id in self.parent_ids:\n",
    "    init_state = self.node2init[parent_id]\n",
    "    # 우회전 이동류 인덱스\n",
    "    indices_right = [i for i in range(len(init_state)) if init_state[i]=='g']\n",
    "    # from-to가 지정된 이동류 인덱스\n",
    "    indices_assigned = []\n",
    "    m5 = self.match5[(self.match5.node_id==parent_id)].dropna(subset=['inc_edge_id', 'out_edge_id'])\n",
    "    for row in m5.itertuples():\n",
    "        inc_edge = self.net.getEdge(row.inc_edge_id)\n",
    "        out_edge = self.net.getEdge(row.out_edge_id)\n",
    "        conns = inc_edge.getConnections(out_edge)\n",
    "        indices = [conn for conn in conns if conn.getTLLinkIndex()>=0]\n",
    "        indices = [conn for conn in conns if conn.getJunctionIndex()>=0]\n",
    "        indices = [conn.getTLLinkIndex() for conn in conns]\n",
    "        indices_assigned.extend(indices)\n",
    "    # 좌회전 이동류 인덱스\n",
    "    indices_left = []\n",
    "    for row in self.turn_type[self.turn_type.turn_type=='left'].itertuples():\n",
    "        inc_edge = self.net.getEdge(row.inc_edge_id)\n",
    "        out_edge = self.net.getEdge(row.out_edge_id)\n",
    "        conns = inc_edge.getConnections(out_edge)\n",
    "        indices = [conn for conn in conns if conn.getTLLinkIndex()>=0]\n",
    "        indices = [conn for conn in conns if conn.getJunctionIndex()>=0]\n",
    "        indices = [conn.getTLLinkIndex() for conn in conns]\n",
    "        indices_left.extend(indices)\n",
    "    # 비보호좌회전 인덱스 (unprotected left index)\n",
    "    UPLindices = list((set(range(len(init_state))) - set(indices_right) - set(indices_assigned)).intersection(indices_left))\n",
    "    self.p2UPLindices[parent_id] = UPLindices\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "self.p2UPLindices = dict() # parent id to unprotected left index\n",
    "for parent_id in self.parent_ids:\n",
    "    init_state = self.node2init[parent_id]\n",
    "    # 우회전 이동류 인덱스\n",
    "    indices_right = [i for i in range(len(init_state)) if init_state[i]=='g']\n",
    "    # from-to가 지정된 이동류 인덱스\n",
    "    indices_assigned = []\n",
    "    m5 = self.match5[(self.match5.node_id==parent_id)].dropna(subset=['inc_edge_id', 'out_edge_id'])\n",
    "    for row in m5.itertuples():\n",
    "        inc_edge = self.net.getEdge(row.inc_edge_id)\n",
    "        out_edge = self.net.getEdge(row.out_edge_id)\n",
    "        conns = inc_edge.getConnections(out_edge)\n",
    "        indices = [conn for conn in conns if conn.getTLLinkIndex()>=0]\n",
    "        indices = [conn for conn in conns if conn.getJunctionIndex()>=0]\n",
    "        indices = [conn.getTLLinkIndex() for conn in conns]\n",
    "        indices_assigned.extend(indices)\n",
    "    # 좌회전 이동류 인덱스\n",
    "    indices_left = []\n",
    "    for row in self.turn_type[self.turn_type.turn_type=='left'].itertuples():\n",
    "        inc_edge = self.net.getEdge(row.inc_edge_id)\n",
    "        out_edge = self.net.getEdge(row.out_edge_id)\n",
    "        conns = inc_edge.getConnections(out_edge)\n",
    "        indices = [conn for conn in conns if conn.getTLLinkIndex()>=0]\n",
    "        indices = [conn for conn in conns if conn.getJunctionIndex()>=0]\n",
    "        indices = [conn.getTLLinkIndex() for conn in conns]\n",
    "        indices_left.extend(indices)\n",
    "    # 비보호좌회전 인덱스 (unprotected left index)\n",
    "    UPLindices = list((set(range(len(init_state))) - set(indices_right) - set(indices_assigned)).intersection(indices_left))\n",
    "    self.p2UPLindices[parent_id] = dict()\n",
    "    for UPLindex in UPLindices:\n",
    "        node = self.net.getNode(parent_id)\n",
    "        conns = node.getConnections()\n",
    "        conns = [conn for conn in conns if conn.getTLLinkIndex() == UPLindex]\n",
    "        inc_edge_ids = [conn.getFrom().getID() for conn in conns]\n",
    "        self.p2UPLindices[parent_id][UPLindex] = inc_edge_ids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: ['571542073_01']}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "self.p2UPLindices[self.parent_ids[5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "571542073_01\n"
     ]
    }
   ],
   "source": [
    "for UPLindex in UPLindices:\n",
    "    node = self.net.getNode(parent_id)\n",
    "    conns = node.getConnections()\n",
    "    conns = [conn for conn in conns if conn.getTLLinkIndex() == UPLindex]\n",
    "    inc_edge_ids = [conn.getFrom().getID() for conn in conns]\n",
    "\n",
    "\n",
    "parent_id = self.parent_ids[5]\n",
    "UPLindices = self.p2UPLindices[parent_id]\n",
    "UPLindex = UPLindices[0]\n",
    "print(UPLindex)\n",
    "\n",
    "node = self.net.getNode(parent_id)\n",
    "conns = node.getConnections()\n",
    "conns = [conn for conn in conns if conn.getTLLinkIndex() == UPLindex]\n",
    "conn = conns[0]\n",
    "inc_edge = conn.getFrom().getID()\n",
    "print(inc_edge)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "sts",
   "language": "python",
   "name": "sts"
  },
  "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
}
