{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"hour=9,\n",
|
|
"minute=3 (더 정확하게는 minute <= 4)\n",
|
|
"\n",
|
|
"입력 시 make_rhists에서 에러가 나고 있음.\n",
|
|
"이때의 에러는, 특정 교차로에 대하여 history 데이터가 없기 때문으로 추정됨.\n",
|
|
"\n",
|
|
"특정 교차로에 대하여 history가 없더라도 에러가 나지 않도록 디버깅하려고 시도했으나, 일단 잘 되지 않았음. 나중으로 미룸 (2024. 8. 2. 09:27)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"import numpy as np\n",
|
|
"import os, sys, json, argparse, pickle\n",
|
|
"import sumolib, traci\n",
|
|
"from tqdm import tqdm\n",
|
|
"from datetime import datetime\n",
|
|
"path_root = os.path.dirname(os.path.dirname(os.path.abspath('.')))\n",
|
|
"path_scr = os.path.join(path_root, 'scripts')\n",
|
|
"sys.path.append(path_scr)\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-5. 필요한 보조 객체들이 모두 준비되었습니다.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"self = SignalGenerator(config_name='test_0731',\n",
|
|
" month=7,\n",
|
|
" day=31,\n",
|
|
" hour=9,\n",
|
|
" minute=3)\n",
|
|
"self.prepare_data()\n",
|
|
"self.make_rhistory()\n",
|
|
"# display(self.rhistory.head())\n",
|
|
"# self.make_rhists()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[436, 437, 438, 442, 443, 455, 456, 457, 458]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(self.inter_nos)\n",
|
|
"inter_no = 457"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1722384000\n"
|
|
]
|
|
},
|
|
{
|
|
"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>end_unix</th>\n",
|
|
" <th>inter_no</th>\n",
|
|
" <th>dura_A1</th>\n",
|
|
" <th>dura_A2</th>\n",
|
|
" <th>dura_A3</th>\n",
|
|
" <th>dura_A4</th>\n",
|
|
" <th>dura_A5</th>\n",
|
|
" <th>dura_A6</th>\n",
|
|
" <th>dura_A7</th>\n",
|
|
" <th>dura_A8</th>\n",
|
|
" <th>dura_B1</th>\n",
|
|
" <th>dura_B2</th>\n",
|
|
" <th>dura_B3</th>\n",
|
|
" <th>dura_B4</th>\n",
|
|
" <th>dura_B5</th>\n",
|
|
" <th>dura_B6</th>\n",
|
|
" <th>dura_B7</th>\n",
|
|
" <th>dura_B8</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>5</th>\n",
|
|
" <td>1722384411</td>\n",
|
|
" <td>457</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>11</th>\n",
|
|
" <td>1722384773</td>\n",
|
|
" <td>457</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>26</th>\n",
|
|
" <td>1722384591</td>\n",
|
|
" <td>457</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>33</th>\n",
|
|
" <td>1722384053</td>\n",
|
|
" <td>457</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>36</th>\n",
|
|
" <td>1722384232</td>\n",
|
|
" <td>457</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>43</th>\n",
|
|
" <td>1722384953</td>\n",
|
|
" <td>457</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" end_unix inter_no dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 \\\n",
|
|
"5 1722384411 457 45 20 40 40 35 \n",
|
|
"11 1722384773 457 45 20 40 40 35 \n",
|
|
"26 1722384591 457 45 20 40 40 35 \n",
|
|
"33 1722384053 457 45 20 40 35 40 \n",
|
|
"36 1722384232 457 45 20 40 40 35 \n",
|
|
"43 1722384953 457 45 20 40 40 35 \n",
|
|
"\n",
|
|
" dura_A6 dura_A7 dura_A8 dura_B1 dura_B2 dura_B3 dura_B4 dura_B5 \\\n",
|
|
"5 0 0 0 45 20 40 40 35 \n",
|
|
"11 0 0 0 45 20 40 40 35 \n",
|
|
"26 0 0 0 45 20 40 40 35 \n",
|
|
"33 0 0 0 45 20 40 35 40 \n",
|
|
"36 0 0 0 45 20 40 40 35 \n",
|
|
"43 0 0 0 45 20 40 40 35 \n",
|
|
"\n",
|
|
" dura_B6 dura_B7 dura_B8 \n",
|
|
"5 0 0 0 \n",
|
|
"11 0 0 0 \n",
|
|
"26 0 0 0 \n",
|
|
"33 0 0 0 \n",
|
|
"36 0 0 0 \n",
|
|
"43 0 0 0 "
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"print(self.present_time)\n",
|
|
"self.history[self.history.inter_no==457]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"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>start_unix</th>\n",
|
|
" <th>dura_A1</th>\n",
|
|
" <th>dura_A2</th>\n",
|
|
" <th>dura_A3</th>\n",
|
|
" <th>dura_A4</th>\n",
|
|
" <th>dura_A5</th>\n",
|
|
" <th>dura_A6</th>\n",
|
|
" <th>dura_A7</th>\n",
|
|
" <th>dura_A8</th>\n",
|
|
" <th>dura_B1</th>\n",
|
|
" <th>dura_B2</th>\n",
|
|
" <th>dura_B3</th>\n",
|
|
" <th>dura_B4</th>\n",
|
|
" <th>dura_B5</th>\n",
|
|
" <th>dura_B6</th>\n",
|
|
" <th>dura_B7</th>\n",
|
|
" <th>dura_B8</th>\n",
|
|
" <th>cycle</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>457</td>\n",
|
|
" <td>1722384400</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>180.0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>457</td>\n",
|
|
" <td>1722384400</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>180.0</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>0</th>\n",
|
|
" <td>457</td>\n",
|
|
" <td>1722384400</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>45</td>\n",
|
|
" <td>20</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>40</td>\n",
|
|
" <td>35</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>0</td>\n",
|
|
" <td>180.0</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" inter_no start_unix dura_A1 dura_A2 dura_A3 dura_A4 dura_A5 dura_A6 \\\n",
|
|
"0 457 1722384400 45 20 40 35 40 0 \n",
|
|
"0 457 1722384400 45 20 40 35 40 0 \n",
|
|
"0 457 1722384400 45 20 40 40 35 0 \n",
|
|
"\n",
|
|
" dura_A7 dura_A8 dura_B1 dura_B2 dura_B3 dura_B4 dura_B5 dura_B6 \\\n",
|
|
"0 0 0 45 20 40 35 40 0 \n",
|
|
"0 0 0 45 20 40 35 40 0 \n",
|
|
"0 0 0 45 20 40 40 35 0 \n",
|
|
"\n",
|
|
" dura_B7 dura_B8 cycle \n",
|
|
"0 0 0 180.0 \n",
|
|
"0 0 0 180.0 \n",
|
|
"0 0 0 180.0 "
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"self.rhistory[self.rhistory.inter_no==457]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"self.make_rhists()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rhistory = self.history[self.history.inter_no==inter_no]\n",
|
|
"rhistory = rhistory[(rhistory.end_unix <= self.present_time) & (rhistory.end_unix > self.present_time - self.subtractor)]\n",
|
|
"rhistory"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# rhistory에 모든 교차로번호가 존재하지 않으면 해당 교차로번호에 대한 신호이력을 추가함 (at 최근 프로그램 시작시각)\n",
|
|
"inter_no = inter_no\n",
|
|
"program_start, prow = self.load_prow(inter_no, self.present_time - self.subtractor)\n",
|
|
"cycle = prow.cycle.iloc[0]\n",
|
|
"row1 = prow.copy()\n",
|
|
"row2 = prow.copy()\n",
|
|
"# prow에서 필요한 부분을 rhistory에 추가\n",
|
|
"row1['end_unix'] = program_start\n",
|
|
"row2['end_unix'] = program_start + cycle\n",
|
|
"display(row1)\n",
|
|
"display(row2)\n",
|
|
"rhistory = pd.concat([rhistory, row1, row2])#.reset_index(drop=True)\n",
|
|
"rhistory"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# present_time + adder 의 시각에 한 주기의 신호 추가\n",
|
|
"program_start, prow = self.load_prow(inter_no, self.present_time)\n",
|
|
"cycle = prow.cycle.iloc[0]\n",
|
|
"row3 = prow.copy()\n",
|
|
"# # prow에서 필요한 부분을 rhistory에 추가\n",
|
|
"row3['end_unix'] = self.present_time + self.adder\n",
|
|
"rhistory = pd.concat([rhistory, row3])#.reset_index(drop=True)\n",
|
|
"rhistory"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"display(rhistory)\n",
|
|
"i = 0\n",
|
|
"row = rhistory.iloc[i]\n",
|
|
"inter_no = row.inter_no\n",
|
|
"end_unix = row.end_unix\n",
|
|
"elapsed_time = row[[f'dura_{alph}{j}' for alph in ['A', 'B'] for j in range(1,9)]].sum() // 2 # 현시시간 합\n",
|
|
"# 이전 유닉스 존재하지 않음 : 현시시간 합의 차\n",
|
|
"start_unix = end_unix - elapsed_time\n",
|
|
"pre_rows = self.history[:i] # previous rows\n",
|
|
"print(inter_no in pre_rows.inter_no.unique())\n",
|
|
"if inter_no in pre_rows.inter_no.unique(): # 이전 유닉스 존재\n",
|
|
" pre_unix = pre_rows[pre_rows.inter_no == inter_no]['end_unix'].iloc[-1] # previous unix time\n",
|
|
" # 이전 유닉스 존재, abs < 10 : 이전 유닉스\n",
|
|
" if abs(pre_unix - start_unix) < 10:\n",
|
|
" start_unix = pre_unix\n",
|
|
" # 이전 유닉스 존재, abs >=10 : 현시시간 합의 차\n",
|
|
" else:\n",
|
|
" pass\n",
|
|
"rhistory.loc[i, 'start_unix'] = start_unix\n",
|
|
"rhistory\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"self.inter_nos"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "siggen_env",
|
|
"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.12.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|