신호생성 repo (24. 1. 5 ~).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

589 lines
18 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sumolib\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<sumolib.net.Net at 0x1bbcdd0edf0>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"net = sumolib.net.readNet('new_sungnam_network_internal_target_0721.net.xml')\n",
"net"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"node_id = '106234'\n",
"node_id_s = '106674'\n",
"node_id_n = '106681'\n",
"node_id_w = '106677'\n",
"node_id_e = '106510'\n",
"uturn_id_e = '109296'\n",
"uturn_id_w = '109297'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"513157\n",
"513153\n",
"513151\n",
"513155\n",
"\n",
"513158\n",
"513154\n",
"513152\n",
"513156\n",
"\n",
"519799\n",
"519798\n",
"519801\n",
"519800\n",
"\n"
]
}
],
"source": [
"def get_edge_id(head_node_id, tail_node_id):\n",
" headings = net.getNode(head_node_id).getOutgoing()\n",
" tailings = net.getNode(tail_node_id).getIncoming()\n",
" edge_id = list(set(headings) & set(tailings))[0].getID()\n",
" return edge_id\n",
"\n",
"iid_n = get_edge_id(node_id_n, node_id)\n",
"iid_s = get_edge_id(node_id_s, node_id)\n",
"iid_e = get_edge_id(uturn_id_e, node_id)\n",
"iid_w = get_edge_id(uturn_id_w, node_id)\n",
"\n",
"oid_n = get_edge_id(node_id, node_id_n)\n",
"oid_s = get_edge_id(node_id, node_id_s)\n",
"oid_e = get_edge_id(node_id, uturn_id_e)\n",
"oid_w = get_edge_id(node_id, uturn_id_w)\n",
"\n",
"uiid_e = get_edge_id(node_id_e, uturn_id_e)\n",
"uoid_e = get_edge_id(uturn_id_e, node_id_e)\n",
"uiid_w = get_edge_id(node_id_w, uturn_id_w)\n",
"uoid_w = get_edge_id(uturn_id_w, node_id_w)\n",
"\n",
"print(oid_n, oid_s, oid_e, oid_w, '', sep='\\n')\n",
"print(iid_n, iid_s, iid_e, iid_w, '', sep='\\n')\n",
"print(uiid_e, uoid_e, uiid_w, uoid_w, '', sep='\\n')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>parent_id</th>\n",
" <th>child_id</th>\n",
" <th>phase_no</th>\n",
" <th>ring_type</th>\n",
" <th>inc_edge_id</th>\n",
" <th>out_edge_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [parent_id, child_id, phase_no, ring_type, inc_edge_id, out_edge_id]\n",
"Index: []"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coord = pd.DataFrame({'parent_id':[], 'child_id':[], 'phase_no':[], 'ring_type':[], 'inc_edge_id':[], 'out_edge_id':[]})\n",
"coord.to_csv('coord.csv',index=0)\n",
"coord"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"inter_node = pd.DataFrame({\n",
" 'inter_no':[314]*3,\n",
" 'node_id':[node_id, uturn_id_e, uturn_id_w],\n",
" 'inter_type':['parent', 'child', 'child']\n",
"})\n",
"inter_node.to_csv('inter_node.csv', index=0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# nema : 생성하지 않음\n",
"# TC_IF_TOD_DAY_PLAN : 생성하지 않음\n",
"# TC_IF_TOD_HOLIDAY_PLAN : 생성하지 않음\n",
"# TC_IF_TOD_RED_YELLO : 생성하지 않음\n",
"# TC_IF_TOD_WEEK_PLAN : 생성하지 않음"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"inter_info_str = \\\n",
"f'''CRSRD_ID,CRSRD_NM,CRSRD_TYPE,CTRLER_TYPE,TRFLIG_TYPE,NODE_ID,LTTD,LGTD,PPC_TYPE,MAIN_PHASE,LOS_YN,USE_YN,FRST_REG_DT,LAST_MDFCN_DT,MAIN_CRSRD_ID,GRP_NO\n",
"314,신구대사거리,0,-1,-1,{node_id},37.44670852,127.1673035,-1,2,-1,-1,36:43.7,36:43.7,106234,1\n",
"'''\n",
"with open('inter_info.csv', 'w', encoding='utf-8') as file:\n",
" file.write(inter_info_str)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# TN_IF_SIGL_FLOW : 생성하지 않음"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"turn_type_str = \\\n",
"f'''node_id,inc_edge_id,out_edge_id,turn_type\n",
"{node_id},{iid_w},{oid_s},right\n",
"{node_id},{iid_w},{oid_e},straight\n",
"{node_id},{iid_w},{oid_n},left\n",
"{node_id},{iid_n},{oid_w},right\n",
"{node_id},{iid_n},{oid_s},straight\n",
"{node_id},{iid_n},{oid_e},left\n",
"{node_id},{iid_e},{oid_n},right\n",
"{node_id},{iid_e},{oid_w},straight\n",
"{node_id},{iid_e},{oid_s},left\n",
"{node_id},{iid_s},{oid_e},right\n",
"{node_id},{iid_s},{oid_n},straight\n",
"{node_id},{iid_s},{oid_w},left\n",
"'''\n",
"\n",
"with open('turn_type.csv', 'w') as file:\n",
" file.write(turn_type_str)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"u_condition_str = \\\n",
"f'''child_id,\tcondition\n",
"{uturn_id_e},좌회전시\n",
"{uturn_id_w},보행신호시\n",
"'''\n",
"\n",
"with open('u_condition.csv', 'w', encoding='utf-8') as file:\n",
" file.write(u_condition_str)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"uturn_str = \\\n",
"f'''parent_id,child_id,adj_inc_edge_id,adj_out_edge_id,inc_edge_id,out_edge_id\n",
"{node_id},{uturn_id_e},{iid_e},{oid_e},{uiid_e},{uoid_e}\n",
"{node_id},{uturn_id_w},{iid_w},{oid_w},{uiid_w},{uoid_w}\n",
"'''\n",
"with open('uturn.csv', 'w') as file:\n",
" file.write(uturn_str)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"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>CRSRD_ID</th>\n",
" <th>PLAN_NO</th>\n",
" <th>HOUR</th>\n",
" <th>MIN</th>\n",
" <th>CYCL</th>\n",
" <th>OFFSET</th>\n",
" <th>RINGA_PHASE1</th>\n",
" <th>RINGA_PHASE2</th>\n",
" <th>RINGA_PHASE3</th>\n",
" <th>RINGA_PHASE4</th>\n",
" <th>...</th>\n",
" <th>RINGA_PHASE8</th>\n",
" <th>RINGB_PHASE1</th>\n",
" <th>RINGB_PHASE2</th>\n",
" <th>RINGB_PHASE3</th>\n",
" <th>RINGB_PHASE4</th>\n",
" <th>RINGB_PHASE5</th>\n",
" <th>RINGB_PHASE6</th>\n",
" <th>RINGB_PHASE7</th>\n",
" <th>RINGB_PHASE8</th>\n",
" <th>LAST_MDFCN_DT</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>314</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>180</td>\n",
" <td>57</td>\n",
" <td>45</td>\n",
" <td>30</td>\n",
" <td>53</td>\n",
" <td>52</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>30</td>\n",
" <td>53</td>\n",
" <td>52</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36:43.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>314</td>\n",
" <td>1</td>\n",
" <td>7</td>\n",
" <td>0</td>\n",
" <td>180</td>\n",
" <td>40</td>\n",
" <td>45</td>\n",
" <td>30</td>\n",
" <td>55</td>\n",
" <td>50</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>45</td>\n",
" <td>30</td>\n",
" <td>55</td>\n",
" <td>50</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36:43.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>314</td>\n",
" <td>1</td>\n",
" <td>9</td>\n",
" <td>0</td>\n",
" <td>180</td>\n",
" <td>28</td>\n",
" <td>43</td>\n",
" <td>32</td>\n",
" <td>53</td>\n",
" <td>52</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>43</td>\n",
" <td>32</td>\n",
" <td>53</td>\n",
" <td>52</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36:43.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>314</td>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>30</td>\n",
" <td>180</td>\n",
" <td>18</td>\n",
" <td>43</td>\n",
" <td>32</td>\n",
" <td>55</td>\n",
" <td>50</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>43</td>\n",
" <td>32</td>\n",
" <td>55</td>\n",
" <td>50</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>36:43.7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>4 rows × 23 columns</p>\n",
"</div>"
],
"text/plain": [
" CRSRD_ID PLAN_NO HOUR MIN CYCL OFFSET RINGA_PHASE1 RINGA_PHASE2 \\\n",
"0 314 1 0 0 180 57 45 30 \n",
"1 314 1 7 0 180 40 45 30 \n",
"2 314 1 9 0 180 28 43 32 \n",
"3 314 1 18 30 180 18 43 32 \n",
"\n",
" RINGA_PHASE3 RINGA_PHASE4 ... RINGA_PHASE8 RINGB_PHASE1 RINGB_PHASE2 \\\n",
"0 53 52 ... 0 45 30 \n",
"1 55 50 ... 0 45 30 \n",
"2 53 52 ... 0 43 32 \n",
"3 55 50 ... 0 43 32 \n",
"\n",
" RINGB_PHASE3 RINGB_PHASE4 RINGB_PHASE5 RINGB_PHASE6 RINGB_PHASE7 \\\n",
"0 53 52 0 0 0 \n",
"1 55 50 0 0 0 \n",
"2 53 52 0 0 0 \n",
"3 55 50 0 0 0 \n",
"\n",
" RINGB_PHASE8 LAST_MDFCN_DT \n",
"0 0 36:43.7 \n",
"1 0 36:43.7 \n",
"2 0 36:43.7 \n",
"3 0 36:43.7 \n",
"\n",
"[4 rows x 23 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import datetime\n",
"dplan = pd.read_csv('TC_IF_TOD_DAY_PLAN.csv')\n",
"dplan"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import numpy as np\n",
"midnight = int(datetime(2024, 7, 22).timestamp())\n",
"next_day = int(datetime(2024, 7, 23).timestamp())\n",
"dplan['end_unix'] = dplan['HOUR'].astype(int) * 3600 + dplan['MIN'].astype(int) * 60 + midnight\n",
"history = []\n",
"current_time = midnight # 현재시각\n",
"for i in range(len(dplan)):\n",
" row = dplan.iloc[i]\n",
" cycle = row['CYCL']\n",
" offset = row['OFFSET']\n",
" unix_end = dplan.iloc[i+1]['end_unix'] if i < len(dplan) - 1 else next_day\n",
" r = 0\n",
" while current_time <= unix_end:\n",
" remainder = current_time % 10\n",
" if remainder != 0: # 현재시각의 일의자리가 0이 아니면 다음 현재시각의 일의자리가 0이 되도록 맞춰준다.\n",
" r = (5 - remainder) % 10 - 5 # 1>-1, 2>-2, 3>-3, 4>-4, 5>-5, 6>4, 7>3, 8>2, 9>1\n",
" else:\n",
" r = random.choices([0, -1, 1], weights=[10, 1, 1])[0] # 측정오차\n",
" # 0.001의 확률로 결측 발생\n",
" if random.random() < 0.001: # 주기의 배수만큼 결측\n",
" n = random.randint(0,100)\n",
" current_time += n * cycle + r\n",
" # 0.01의 확률로 이상치 발생\n",
" elif random.random() > 0.99: # 카이제곱분포값 * 주기만큼의 이상치 발생. 카이제곱분포값은 항상 양수이고 평균이 1\n",
" current_time += int(np.random.normal()**2 * cycle) + r\n",
" else:\n",
" current_time += cycle + r # 결측/이상 없음\n",
" new_row = row.copy()\n",
" new_row['PHASE_DT'] = current_time\n",
" new_row['CYCL'] = cycle\n",
" new_row['OFFSET'] = offset\n",
" history.append(new_row)\n",
"history = pd.concat(history, axis=1).transpose().reset_index(drop=True)\n",
"history = history[history.end_unix <= next_day]\n",
"# history = history.rename(columns={f'RING{alph}_PHASE{i}':f'RING{alph}_PHASE{i}' for alph in ['A', 'B'] for i in range(1, 9)})\n",
"history = history[['PHASE_DT', 'CRSRD_ID']\n",
" + [f'RING{alph}_PHASE{i}' for alph in ['A', 'B'] for i in range(1,9)]\n",
" + ['CYCL', 'OFFSET']]\n",
"history = history[history.PHASE_DT <= next_day]\n",
"history = history.rename(columns={'PHASE_DT':'end_unix'})\n",
"history.to_csv(f'history.csv')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"flows = pd.read_csv('TN_IF_SIGL_FLOW.csv')\n",
"for current_time in range(midnight, next_day, 5):\n",
" end_unixes = dplan['end_unix'].unique()\n",
" i = len(end_unixes[end_unixes < current_time]) - 1 # 프로그램 번호\n",
" cumdur = (current_time - midnight) % cycle\n",
" row = dplan.iloc[i]\n",
" cumsum = np.array(row[['RINGA_PHASE1', 'RINGA_PHASE2', 'RINGA_PHASE3', 'RINGA_PHASE4']].cumsum())\n",
" j = len(cumsum[cumsum < cumdur]) + 1 # 현시번호\n",
" move_A = flows[(flows.PHASE==j)&(flows.RING=='A')].iloc[0]['FLOW_NO']\n",
" move_B = flows[(flows.PHASE==j)&(flows.RING=='B')].iloc[0]['FLOW_NO']\n",
" move = pd.DataFrame({'inter_no':[314], 'phas_A':[j], 'phas_B':[j], 'move_A':[move_A], 'move_B':[move_B]})\n",
" move.to_csv(f'move/move_{current_time}.csv')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"flows = pd.read_csv('TN_IF_SIGL_FLOW.csv')\n",
"for PHASE_DT in range(midnight, next_day, 5):\n",
" current_time = datetime.fromtimestamp(PHASE_DT)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"sim_init_dt = datetime(2024, 7, 22, 8, 40)\n",
"sim_term_dt = datetime(2024, 7, 22, 9, 10)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1721605200 1721607000\n"
]
}
],
"source": [
"sim_init_unix = int(sim_init_dt.timestamp())\n",
"sim_term_unix = int(sim_term_dt.timestamp())\n",
"print(sim_init_unix, sim_term_unix)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "siggen",
"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
}