Shortest Path with Turn Penalties & Restrictions
Modeling turn penalties and restrictions for graph solving
Modeling turn penalties and restrictions for graph solving
The following is a complete example, using the Python API, of solving a graph created with a Washington, D.C. HERE dataset for a shortest path problem with turn penalties via the /solve/graph endpoint. For more information on Network Graphs & Solvers, see Network Graphs & Solvers Concepts. For more information on turn penalties and restrictions, see Using Turn-based Weights & Restrictions.
The prerequisites for running this solve graph example are listed below:
The native Kinetica Python API is accessible through the following means:
The Python package manager, pip, is required to install the API from PyPI.
Install the API:
|
|
Test the installation:
|
|
If Import Successful is displayed, the API has been installed as is ready for use.
In the desired directory, run the following, but be sure to replace <kinetica-version> with the name of the installed Kinetica version, e.g., v7.1:
|
|
Change directory into the newly downloaded repository:
|
|
In the root directory of the unzipped repository, install the Kinetica API:
|
|
Test the installation (Python 2.7 (or greater) is necessary for running the API example):
|
|
The example script references the dc_shape.csv
data file,
mentioned in the Prerequisites, in the current local directory, by default.
This directory can specified as a parameter when running the script.
The dc_shape dataset is a HERE dataset and is analogous to most road network datasets you could find in that it includes columns for the type of road, the average speed, the direction of the road, a WKT linestring for its geographic location, a unique ID integer for the road, and more. The graph used in the example is created with four columns from the dc_shape dataset:
This example is going to demonstrate solving for the shortest path with varying turn-based penalties and restrictions between source points and destination points located within a road network in Washington, D.C.
Several constants are defined at the beginning of the script:
SCHEMA -- the name of the schema in which the tables supporting the graph creation and solve operations will be created
Important
The schema is created during the table setup portion of the script because the schema must exist prior to creating the tables that will later support the graph creation and match operations.
TABLE_DC -- the name of the table into which the D.C. road network dataset is loaded
GRAPH_DC -- the D.C. road network graph
SOLUTION_GRAPH_DC_1 / SOLUTION_GRAPH_DC_1 / SOLUTION_GRAPH_DC_3 -- the D.C. road network graph shortest path solution tables
|
|
Before the graph can be created, the D.C. shape dataset is loaded from a local CSV file. First, the D.C. shape table is created using the GPUdbTable interface:
|
|
Then, the CSV file is uploaded into KiFS and loaded into the table:
|
|
One graph is used for the shortest path solve graph example utilized in the script: GRAPH_DC, a graph based on the dc_shape dataset (the CSV file mentioned in Prerequisites).
The GRAPH_DC graph is created with the following characteristics:
|
|
The first example illustrates a simple shortest path solve from a single source node to a single destination node. First, the source node and destination node are defined.
Note
The source and destination node are the same for each example.
|
|
Next, the GRAPH_DC graph is solved with the solve results being exported to the response:
|
|
The cost for the source node to visit the destination node is represented as time in seconds:
|
|
The solution output to WMS:
The second example illustrates a shortest path solve from a single source node to a single destination node, but this time traveling through an intersection of two-way roads will incur an additional cost. The GRAPH_DC graph is solved with the solve results being exported to the response and an intersection penalty of 20 seconds:
|
|
The cost for the source node to visit the destination node is represented as time in seconds:
|
|
The solution output to WMS, noting the immediate U-turn to return to Q Street from Connecticut Avenue to avoid the intersection penalty:
The third example illustrates a shortest path solve from a single source node to a single destination node, but this time a single turn from Q Street on to 15th Street is restricted. The GRAPH_DC graph is solved with turns from road segment ID 18352169 (Q Street) to road segment ID 18352166 (15th Street) being restricted and the solve results being exported to the response:
|
|
The cost for the source node to visit the destination node is represented as time in seconds:
|
|
The solution output to WMS, noting the detour on to 16th Street and Church Street to avoid the restricted turn:
Included below is a complete example containing all the above requests, the data files, and output.
To run the complete sample, ensure that:
solve_graph_dc_shortest_path_turn.py
script is in the
current directorydc_shape.csv
file is in the current directory or use
the data_dir parameter to specify the local directory containing itThen, run the following:
|
|