Skip to content

Commit

Permalink
notebook to trim the transit demand for congested assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishKuls committed Apr 17, 2024
1 parent b23ad8a commit 5143896
Showing 1 changed file with 241 additions and 0 deletions.
241 changes: 241 additions & 0 deletions notebooks/trim-demand-for-congested-transit-assignment.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"show_input": true
},
"outputs": [],
"source": [
"import inro\n",
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# this notebook is run from inside EMME environment. Therefore it picks up the emmebank and scenario from the project that user opened. \n",
"modeller = inro.modeller.Modeller()\n",
"\n",
"emmebank = modeller.emmebank"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"scenario = emmebank.scenario(12)\n",
"scen_emmebank = scenario.emmebank"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"matrices_name_id_map = {\n",
" \"AM_WLK_TRN_WLK_DEMAND\": \"6\",\n",
" \"AM_PNR_TRN_WLK_DEMAND\" : \"7\",\n",
" \"AM_WLK_TRN_PNR_DEMAND\" : \"8\",\n",
" \"AM_KNR_TRN_WLK_DEMAND\" : \"9\",\n",
" \"AM_WLK_TRN_KNR_DEMAND\" : \"10\",\n",
" \"AM_WLK_TRN_WLK_TRIM\": \"155\",\n",
" \"AM_PNR_TRN_WLK_TRIM\" : \"177\",\n",
" \"AM_WLK_TRN_PNR_TRIM\" : \"199\",\n",
" \"AM_KNR_TRN_WLK_TRIM\" : \"221\",\n",
" \"AM_WLK_TRN_KNR_TRIM\" : \"243\",\n",
" \"AM_WLK_TRN_WLK_IVT\": \"146\",\n",
" \"AM_PNR_TRN_WLK_IVT\" : \"168\",\n",
" \"AM_WLK_TRN_PNR_IVT\" : \"190\",\n",
" \"AM_KNR_TRN_WLK_IVT\" : \"212\",\n",
" \"AM_WLK_TRN_KNR_IVT\" : \"224\",\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"matrix_calc = modeller.tool(\"inro.emme.matrix_calculation.matrix_calculator\")\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"output_file_name = \"D:/kulshresthaa/MTC/TM2.2.1.1/output_summaries/trimmed_demand_report_am.csv\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"report = []\n",
"period = \"AM\"\n",
"class_names = [\"WLK_TRN_WLK\", \"PNR_TRN_WLK\", \"WLK_TRN_PNR\", \"KNR_TRN_WLK\", \"WLK_TRN_KNR\"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trimming Demand for: WLK_TRN_WLK ... \n",
" Prev Demand = 281040.4\n",
" Current Demand = 276198.6\n",
" Trimmed Demand = 4841.8125\n",
"Trimming Demand for: PNR_TRN_WLK ... \n",
" Prev Demand = 53121.586\n",
" Current Demand = 52774.56\n",
" Trimmed Demand = 347.02734\n",
"Trimming Demand for: WLK_TRN_PNR ... \n",
" Prev Demand = 1037.2283\n",
" Current Demand = 1024.8384\n",
" Trimmed Demand = 12.389893\n",
"Trimming Demand for: KNR_TRN_WLK ... \n",
" Prev Demand = 37145.855\n",
" Current Demand = 36810.797\n",
" Trimmed Demand = 335.0586\n",
"Trimming Demand for: WLK_TRN_KNR ... \n",
" Prev Demand = 7373.4683\n",
" Current Demand = 7295.744\n",
" Trimmed Demand = 77.72412\n"
]
}
],
"source": [
"for name in class_names: \n",
" skim_mat_name = \"%s_%s_IVT\" % (period, name)\n",
" demand_mat_name = \"%s_%s_DEMAND\" % (period, name)\n",
" trim_mat_name = \"%s_%s_TRIM\" % (period, name)\n",
" \n",
" demand_id = matrices_name_id_map[demand_mat_name]\n",
" ivt_id = matrices_name_id_map[skim_mat_name]\n",
" trim_id = matrices_name_id_map[trim_mat_name]\n",
" \n",
" matrix = emmebank.matrix(f'mf{demand_id}')\n",
" prev_demand = matrix.get_numpy_data()\n",
" \n",
" print(\"Trimming Demand for\", name, \"... \")\n",
" print(\" Prev Demand =\", np.sum(prev_demand))\n",
" \n",
" spec_list = [\n",
" { # initialize TRIM skim\n",
" \"type\": \"MATRIX_CALCULATION\",\n",
" \"constraint\": None,\n",
" \"result\": f'mf{trim_id}',\n",
" \"expression\": '0',\n",
" },\n",
" \n",
" { # matrix used for trimming demands, set value to 1 if IVT is not infinite\n",
" \"type\": \"MATRIX_CALCULATION\",\n",
" \"constraint\": {\n",
" \"by_value\": {\n",
" \"od_values\": f'mf{ivt_id}',\n",
" \"interval_min\": 0,\n",
" \"interval_max\": 9999999,\n",
" \"condition\": \"INCLUDE\"}\n",
" },\n",
" \"result\": f'mf{trim_id}',\n",
" \"expression\": '1',\n",
" },\n",
" \n",
" { # update demand tables\n",
" \"type\": \"MATRIX_CALCULATION\",\n",
" \"constraint\": None,\n",
" \"result\": f'mf{demand_id}',\n",
" \"expression\": f'(mf{trim_id} * mf{demand_id})',\n",
" },\n",
" ]\n",
"\n",
" matrix_calc(spec_list, scenario=scenario)\n",
" \n",
" matrix = emmebank.matrix(f'mf{demand_id}')\n",
" current_demand = matrix.get_numpy_data()\n",
" \n",
" print(\" Current Demand =\", np.sum(current_demand))\n",
" \n",
" trimmed_demand = np.sum(prev_demand) - np.sum(current_demand)\n",
" \n",
" print(\" Trimmed Demand =\", trimmed_demand)\n",
" \n",
" report.append((name,trimmed_demand))\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('WLK_TRN_WLK', 4841.8125),\n",
" ('PNR_TRN_WLK', 347.02734),\n",
" ('WLK_TRN_PNR', 12.389893),\n",
" ('KNR_TRN_WLK', 335.0586),\n",
" ('WLK_TRN_KNR', 77.72412)]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"report"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"pd.DataFrame(report).to_csv(output_file_name, index=False, header=['class','trimmed_demand'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
},
"modeller": {
"desktop_app_port": "4242"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 5143896

Please sign in to comment.