router engine is deprecated. To read about our current
vehicle routing engine documentation start reading here.
This how-to guide assumes you already completed the steps described in the 5-minute getting started experience. To test that the Nextmv CLI is correctly configured, you can optionally run the following command on your terminal. It will get some files that are necessary to work with the Nextmv Platform. You can see the expected output as well.
The Nextmv Software Development Kit (SDK) lets you automate any operational decision in a way that looks and feels like writing other code. It provides the guardrails to turn your data into automated decisions and test and deploy them into production environments.
This guide will walk you through our
cluster-tsp template. This templates is a variation of the routing template and in order to focus on the aspect of pre-clustering it only uses a small subset of the options shown in the
routing template. To get the
cluster-tsp template, simply run the following command.
You can check that all files are available in the newly created
routing folder. Running the
tree command, you should see the file structure.
README.mdgives a short introduction to the
cluster-tspproblem and shows you how to run the template.
go.sumdefine a Go module and are used to manage dependencies, including the Nextmv SDK.
input.jsondescribes the input data for a specific
cluster-tspproblem that is solved by the template.
licensecontains the Apache License 2.0 under which we distribute this template.
main.gocontains the actual code of the Nextmv routing app.
routing.code-workspacefile should be used to open the template in Visual Studio Code. It is pre-configured for you so that you can run and debug the template without any further steps.
Now you can run the template with the Nextmv CLI, reading from the
input.json file and writing to an
output.json file. The following command shows you how to specify solver limits as well. You should obtain an output similar to the one shown.
Note that transient fields like timestamps, duration, versions, etc. are represented with dummy values due to their dynamic nature. I.e., every time the input is run or the version is bumped, these fields will have a different value.
Since this template is based on the routing template, we will focus on the part where and how the clustering is done and only briefly mention the parts specific to routing. If you want to know more about the mechanics of the router, please read our routing template how-to guide.
The first part of the
main.go defines a package name, imports packages that are needed by the code below and a
main function which is the starting point for the app. In the
main function the
Run function from the Nextmv
run package is being called. This function executes a solver which is passed in the form of the
solver function further down in the file.
But before we look into the
solver function, we will examine the different structs that represent the needed data used by the app.
input struct lists the two required input fields,
Vehicles, as well as
ClusterCapacities that we will take into account in our cluster model.
Stops describes the list of locations to visit, and
Vehicles is an array of vehicle IDs. To learn about the additional options and how they're used, check out the how-to guide on router engine options.
solver function is where the model is defined. The function's signature adheres to the
run.Run function we saw earlier already.
routing template we directly created a router and used the
input data to add optional constraints. In this template we will first create a set of clusters which we will then use to solve multiple TSP problems per vehicle. To do this we use the Attribute option from the
router package and create a 1:1 relation between every stop and a vehicle. This happens in the
cluster() function where the returned values
stopAttributes represent this relation.
cluster() function takes the
input data and does four things:
- Build clusters by calling the
- Create a vehicle attribute for each vehicle:
- Loop over each cluster from step 1 create a stop attribute for each point in the cluster for the same vehicle:
- Return the attributes to be used by the
Now, lets look at at the clusterSolutions() function in detail where all the magic happens. First, we create some variables that we will need later on:
We need those variables to pass the following information to the k-means clustering model:
maximumPointsrepresents the number of points that can be assigned to a cluster at most. Since in this example will create as many clusters as we have vehicles, this is equivalent to the maximum number of stops for a route.
maximumValuesrepresents the max capacity for each vehicle.
valuesrepresent the value or weight of each stop per vehicle.
pointsis another representation of the stops from the input data that is expected by the k-means model.
weightsrepresents the weight or value a stop has. It is directly related to the variable values.
To gather all the information needed we loop over each
stop and each
vehicle in the input data:
Next we will create a new k-means model and pass in the points, the number of clusters/vehicles, the constraints MaximumPoints and MaximumSumValue.
For this model we then create a solver and solve options.
We can now finally solve the clustering model and return the solution:
Back in our solver function we can now use the variables
stopAttributes and pass them into the router engine:
Because of the
Attribute option, solving this routing model will result in solving multiple TSP problems. Each TSP is represented by the cluster we created and will add all stops of that cluster to the vehicle's route.
Finally, we return a
solver for our
router passing in options that were given at the very beginning by the calling function. This solver is then executed by the
run.Run function from the beginning.