Backhaul Routing in Python
A backhaul routing graph solver example in Python
Note
This documentation is for a prior release of Kinetica. For the latest documentation, click here.
A backhaul routing graph solver example in Python
The following is a complete example, using the Python API, of solving a graph created with Seattle road network data for a backhaul routing problem via the /solve/graph endpoint. For more information on Network Graphs & Solvers, see Network Graphs & Solvers Concepts.
The prerequisites for running the backhaul routing 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 road_weights.csv
data file,
mentioned in the Prerequisites, in the current local directory, by default.
This directory can specified as a parameter when running the example script.
This example is going to demonstrate solving for routing a given set of remote assets, e.g., stores, to a number of fixed assets, e.g, distribution centers, located in a Seattle road network.
SCHEMA -- the name of the schema in which the tables supporting the graph creation and match 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_SRN -- the name of the table into which the Seattle road network dataset is loaded
GRAPH_S -- the Seattle road network graph
TABLE_GRAPH_S_BSOLVED -- the solved Seattle road network graph using the BACKHAUL_ROUTING solver type
|
|
One graph is used for the backhaul solve graph example utilized in the script: seattle_road_network_graph, a graph based on the road_weights dataset (the CSV file mentioned in Prerequisites).
The seattle_road_network_graph graph is created with the following characteristics:
|
|
First, the fixed assets and remote assets are set.
|
|
Next, the graph is solved with the solve results being exported to the response. The fixed assets are provided for the source nodes and the remote assets are provided for the destination nodes. All assets are snapped to corresponding locations on the Seattle road network graph.
|
|
The cost for each remote asset to travel to the nearest fixed asset is output:
|
|
The solution output to WMS:
Tip
To demonstrate how the remote assets are routed back to the fixed assets, the fixed assets can be plotted using a separate WMS call and mapped together with the solution. The picture below represents this; the fixed assets are * icons while remote assets can be found in lines leading away from the fixed assets.
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_seattle_backhaul.py
script is in the current
directoryroad_weights.csv
file is in the current directory or use
the data_dir parameter to specify the local directory containing itThen, run the following:
|
|