Prerequisites
The prerequisites for running the match graph example are listed below:- Graph server enabled
- Python API
- Match graph script
- D.C. roads CSV file
Python API Installation
Depending on the target operating system, a Python virtual environment may need to be installed first: The native Kinetica Python API is accessible through the following means:Python Virtual Environment
A Python virtual environment is necessary to install in an operating environment where Python is externally managed.-
Install a Python virtual environment:
-
Activate the Python virtual environment:
PyPI
-
Install the API:
-
Test the installation:
If Import Successful is displayed, the API has been installed as is ready for use.
Git
-
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.2: -
Change directory into the newly downloaded repository:
-
In the root directory of the unzipped repository, install the Kinetica API:
-
Test the installation (Python3 is necessary for running the API example):
Data File
The example script references the dc_roads.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. Thedc_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 two columns from the dc_shape dataset:
-
shape— a WKT linestring composed of points that make up various streets, roads, highways, alleyways, and footpaths throughout Washington, D.C. -
direction— an integer column that conveys information about the directionality of the road, with forward meaning the direction in which the way is drawn in OSM and backward being the opposite direction:0— a forward one-way road1— a two-way road2— a backward one-way road
shape column is also part of an inline
calculation for distance as weight during graph creation using
the ST_LENGTH and ST_NPOINTS
geospatial functions.
Script Detail
This example is going to demonstrate matching supply trucks to different demand points within Washington, D.C., relying on truck sizes, road weights, and proximity to determine the best path between supply trucks and demands.Constants
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 match operations will be createdThe 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. roads dataset is loaded -
TABLE_D/TABLE_S— the names for the tables into which the datasets in this file are loaded.TABLE_Dis a record of each demand’s (store) ID, WKT location, size of demand, and their local supplier’s ID;TABLE_Sis a record of each supplier’s ID, WKT location, truck ID, and truck size. -
GRAPH_DC— the D.C. roads graph -
TABLE_GRAPH_DC_S1/TABLE_GRAPH_DC_S2— the names for the tables into which the solutions are output
Constant Definitions
Table Setup
Before the supplies and demands datasets are generated, the D.C. roads dataset is loaded from a local CSV file. First, the D.C. roads table is created using the GPUdbTable interface:Create D.C. Roads Table
Populate D.C. Roads Table
Create Demands Table
There should not be multiple demand records for the same supplier,
e.g., two separate demands of 5 and 10 for Store ID 1 should be
combined for one demand of 15
3 is the highest priority in this
example and 0 is the lowest):
Populate Demands Table
Create Supplies Table
Populate Supplies Table
Graph Creation
One graph is used for the match graph example utilized in the script:dc_roads_graph, a graph based on the dc_roads dataset
(one of the CSV files mentioned in Prerequisites).
The dc_roads_graph graph is created with the following
characteristics:
- It is directed because the roads in the graph have directionality (one-way and two-way roads)
- It has no explicitly defined nodes because the example relies on implicit nodes attached to the defined edges
- The edges are identified by
WKTLINE, using WKT LINESTRINGs from theWKTcolumn of thedc_roadstable. The road segments’ directionality (DIRECTION) is derived from theonewaycolumn of thedc_roadstable. - The weights (
VALUESPECIFIED) are represented using the cost to travel the segment found in theweightcolumn of thedc_roadstable. The weights are matched to the edges using the sameWKTcolumn as edges (EDGE_WKTLINE) and the sameonewaycolumn as the edge direction (EDGE_DIRECTION). - It has no inherent restrictions for any of the nodes or edges in the graph
- It will be replaced with this instance of the graph if a graph of the same
name exists (
recreate)
Create D.C. Road Network Graph

Multiple Supply Demand with Priority
Both the supplies and demands match identifier combinations are provided to the /match/graph endpoint so the graph server can match the supply trucks to the demand locations and with respect to priority. Using the multiple supply demand match solver results in a solution table for each unique supplier ID; in this case, one table is created:dc_roads_graph_solved.
Match Graph to Supply & Demand with Priority
Match Graph to Supply & Demand with Priority Solution
The WKT route was left out of the displayed results for
readability.
Solution Analysis
For each truck from the supplies table that is involved in the solution, the solution table presents the store(s) visited (by ID), how much “supply” is dropped off at each store, the route taken to the store(s) and back to the supplier, and the cost for the entire route. Based on the results, you can see that the priority stores’ demands were met first: store IDs 46, 45, and 13. Truck ID 24 made a stop at Store 46 and dropped off its entire supply. Once a truck has exhausted its supply in the solution, it is no longer used. Truck ID 25 made a stop at Store ID 46 and 11: it stopped at Store 46 to fulfill the rest of the store’s demand and supplied Store 11 with the rest of its available supply. Trucks will drop off as much supply as they are able; if some supply is left on the truck after a drop-off, the truck will continue to the next most optimal drop-off location regardless of priority. Truck ID 23 made a stop at Store ID 45 to fulfill the store’s entire demand and Store ID 12 to drop off the rest of its available supply. Truck ID 21 made a stop at Store ID 13 to fulfill the store’s entire demand and Store ID 11 to drop off the rest of its available supply. Truck ID 22 made a stop at Store ID 12, 44, and 11 to either fulfill the store’s entire demand (44) or to complete the rest of the store’s demand (12, 11).Multiple Supply Demand with Priority and Max Trip Cost
Similarly to the example above, both the supplies and demands match identifier combinations are provided to the /match/graph endpoint so the graph server can match the supply trucks to the demand locations and with respect to priority and the max trip cost. Max trip cost affects this example by preventing trucks from supplying another store if the cost to travel to that store is greater than the provided cost. Using the multiple supply demand match solver results in a solution table for each unique supplier ID; in this case, one table is created:dc_roads_graph_solved_w_max_trip.
Match Graph to Supply & Demand with Priority & Max Trip Cost
Match Graph to Supply & Demand with Priority & Max Trip Cost Solution
The WKT route was left out of the displayed results for
readability.
Download & Run
Included below is a complete example containing all the above requests, the data files, and output. To run the complete sample, ensure that:- the match_graph_dc_multi_supply_demand.py script is in the current directory
- the dc_roads.csv file is in the current directory or use the
data_dirparameter to specify the local directory containing it
Run Example