Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrocamargo/459501b6390e4b41af735bc8566b547a to your computer and use it in GitHub Desktop.
Save pedrocamargo/459501b6390e4b41af735bc8566b547a to your computer and use it in GitHub Desktop.
Reproducing AequilibraE issue #124
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import pandas as pd\n",
"import numpy as np\n",
"import openmatrix as omx\n",
"from aequilibrae.matrix import AequilibraeMatrix\n",
"from aequilibrae.paths import Graph"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"data_folder = 'D:/src/TransportationNetworks/Anaheim'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Explicit logging"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from aequilibrae import logger\n",
"import logging\n",
"# We redirect the logging output to the terminal\n",
"stdout_handler = logging.StreamHandler(sys.stdout)\n",
"logger.addHandler(stdout_handler)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrix"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"matfile = os.path.join(data_folder, 'Anaheim_trips.tntp')\n",
"\n",
"# Creating the matrix\n",
"f = open(matfile, 'r')\n",
"all_rows = f.read()\n",
"blocks = all_rows.split('Origin')[1:]\n",
"matrix = {}\n",
"for k in range(len(blocks)):\n",
" orig = blocks[k].split('\\n')\n",
" dests = orig[1:]\n",
" orig=int(orig[0])\n",
"\n",
" d = [eval('{'+a.replace(';',',').replace(' ','') +'}') for a in dests]\n",
" destinations = {}\n",
" for i in d:\n",
" destinations = {**destinations, **i}\n",
" matrix[orig] = destinations\n",
"zones = max(matrix.keys())\n",
"mat = np.zeros((zones, zones))\n",
"for i in range(zones):\n",
" for j in range(zones):\n",
" mat[i, j] = matrix[i+1].get(j+1,0)\n",
"\n",
"# Saving the matrix to an OMX container\n",
"omxfile = matfile.replace('tntp', 'omx')\n",
"index = np.arange(zones) + 1\n",
"myfile = omx.open_file(omxfile,'w')\n",
"myfile['matrix'] = mat\n",
"myfile.create_mapping('taz', index)\n",
"myfile.close()\n",
"\n",
"# Or if you prefer an AequilibraE matrix\n",
"aemfile = matfile.replace('tntp', 'aem')\n",
"aem = AequilibraeMatrix()\n",
"kwargs = {'file_name': aemfile,\n",
" 'zones': zones,\n",
" 'matrix_names': ['matrix']}\n",
"\n",
"aem.create_empty(**kwargs)\n",
"aem.matrix['matrix'][:,:] = mat[:,:]\n",
"aem.index[:] = index[:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Graph"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"netfile = os.path.join(data_folder,'Anaheim_net.tntp')\n",
"net = pd.read_csv(netfile, skiprows=7, sep='\\t')\n",
"\n",
"g = Graph()\n",
"g.cost = net['free_flow_time'].values\n",
"g.capacity = net['capacity'].values\n",
"g.free_flow_time = net['free_flow_time'].values\n",
"\n",
"network = net[['init_node', 'term_node', 'free_flow_time', 'capacity', 'b', 'power']]\n",
"network.columns = ['a_node', 'b_node', 'free_flow_time', 'capacity', 'b', 'power']\n",
"network = network.assign(direction=1)\n",
"g.network = network.to_records(index=False)\n",
"g.network_ok = True\n",
"g.status = 'OK'\n",
"g.prepare_graph(index)\n",
"g.save_to_disk(os.path.join(data_folder, 'graph.aeg'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Assignment"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from aequilibrae.paths import TrafficAssignment, TrafficClass"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bfw Assignment STATS\n",
"Iteration, RelativeGap, stepsize\n",
"1,inf,1.0\n",
"2,0.018135363369113977,0.25610159320627784\n",
"3,0.005430047866020795,0.0\n",
"4,0.005430047866020795,0.0\n",
"5,0.005430047866020795,0.0\n",
"6,0.005430047866020795,0.0\n",
"7,0.005430047866020795,0.0\n",
"8,0.005430047866020795,0.0\n",
"9,0.005430047866020795,0.0\n",
"10,0.005430047866020795,0.0\n",
"11,0.005430047866020795,0.0\n",
"12,0.005430047866020795,0.0\n",
"13,0.005430047866020795,0.0\n",
"14,0.005430047866020795,0.0\n",
"15,0.005430047866020795,0.0\n",
"16,0.0054300478660209575,0.0\n",
"17,0.005430047866020795,0.0\n",
"18,0.005430047866020795,0.0\n",
"19,0.005430047866020795,0.0\n",
"20,0.005430047866020795,0.0\n",
"21,0.005430047866020795,0.0\n",
"22,0.005430047866020795,0.0\n",
"23,0.005430047866020795,0.0\n",
"24,0.005430047866020795,0.0\n",
"25,0.005430047866020795,0.0\n",
"26,0.005430047866020795,0.0\n",
"27,0.0054300478660209575,0.0\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-7-bb29f2922b19>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 27\u001b[0m \u001b[0massig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrgap_target\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0.00005\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 29\u001b[1;33m \u001b[0massig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# we then execute the assignment\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32mc:\\users\\pedro\\.virtualenvs\\basic_science-b7soidpa\\lib\\site-packages\\aequilibrae\\paths\\traffic_assignment.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 295\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m->\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 296\u001b[0m \u001b[1;34m\"\"\"Processes assignment\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 297\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massignment\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32mc:\\users\\pedro\\.virtualenvs\\basic_science-b7soidpa\\lib\\site-packages\\aequilibrae\\paths\\linear_approximation.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 294\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpyqt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 295\u001b[0m \u001b[0maon\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massignment\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msignal_handler\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 296\u001b[1;33m \u001b[0maon\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 297\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_aon_results\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtotal_flows\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 298\u001b[0m \u001b[0maon_flows\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_aon_results\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtotal_link_loads\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpce\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\users\\pedro\\.virtualenvs\\basic_science-b7soidpa\\lib\\site-packages\\aequilibrae\\paths\\all_or_nothing.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 75\u001b[0m \u001b[1;31m# self.func_assig_thread(orig, all_threads)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[0mpool\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 77\u001b[1;33m \u001b[0mpool\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 78\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mresults\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlink_loads\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maux_res\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtemp_link_loads\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 79\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Python\\Python37\\Lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mjoin\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 554\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_state\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mCLOSE\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTERMINATE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 555\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"In unknown state\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 556\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_worker_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 557\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_task_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 558\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_result_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Python\\Python37\\Lib\\threading.py\u001b[0m in \u001b[0;36mjoin\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m 1042\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1043\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1044\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_wait_for_tstate_lock\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1045\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1046\u001b[0m \u001b[1;31m# the behavior of a negative timeout isn't documented, but\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Python\\Python37\\Lib\\threading.py\u001b[0m in \u001b[0;36m_wait_for_tstate_lock\u001b[1;34m(self, block, timeout)\u001b[0m\n\u001b[0;32m 1058\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlock\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# already determined that the C code is done\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1059\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_is_stopped\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1060\u001b[1;33m \u001b[1;32melif\u001b[0m \u001b[0mlock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mblock\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1061\u001b[0m \u001b[0mlock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelease\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1062\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_stop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"demand = aem\n",
"# demand.load(os.path.join(data_folder, 'Anaheim_trips.omx'))\n",
"demand.computational_view(['matrix']) # We will only assign one user class stored as 'matrix' inside the OMX file\n",
"\n",
"assig = TrafficAssignment()\n",
"\n",
"# Creates the assignment class\n",
"assigclass = TrafficClass(g, demand)\n",
"\n",
"\n",
"# The first thing to do is to add at list of traffic classes to be assigned\n",
"assig.set_classes([assigclass])\n",
"\n",
"assig.set_vdf(\"BPR\") # This is not case-sensitive # Then we set the volume delay function\n",
"\n",
"assig.set_vdf_parameters({\"alpha\": \"b\", \"beta\": \"power\"}) # And its parameters\n",
"\n",
"assig.set_capacity_field(\"capacity\") # The capacity and free flow travel times as they exist in the graph\n",
"assig.set_time_field(\"free_flow_time\")\n",
"\n",
"# And the algorithm we want to use to assign\n",
"# ['all-or-nothing', 'msa', 'frank-wolfe', 'cfw', 'bfw']\n",
"assig.set_algorithm('bfw') #Biconjugate Frank-Wolfe\n",
"\n",
"# since I haven't checked the parameters file, let's make sure convergence criteria is good\n",
"assig.max_iter = 50\n",
"assig.rgap_target = 0.00005\n",
"\n",
"assig.execute() # we then execute the assignment"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment