The router engine uses decision diagrams to search for the best solution in the time alloted. Sometimes you may want to interact with state transitions to keep track of custom information. In particular, custom information may be used to calculate the value function to optimize. The router
engine provides the Update
option to:
- Create a personalized value function.
- Perform bookkeeping of custom data.
The router
engine relies on the vehicle and fleet engines. Given this distinction, the Update
option takes two arguments: VehicleUpdater
and FleetUpdater
interfaces, part of the router
engine. You can specify custom types to implement these interfaces and personalize the state transitions in the underlying vehicle
and fleet
engines. The user-defined custom types that implement the interfaces will be marshalled to be part of the output. To achieve efficient customizations, always try to update the components of the state that changed, as opposed to the complete state in both the vehicle and fleet engines.
To customize the value function that will be optimized by each engine, the integer return value from the Update
method in either interface must not be nil
. If the value is nil
, the default value is used and it corresponds to the configured measure.
The Update
option is often used together with the Sorter option.
Example
The router example is used as a base, where routes are created to visit seven landmarks in Kyoto using two vehicles. This time, custom data is updated and a personalized value function is used. A custom type is used to save the locations
, which map a stop's ID to its index in the corresponding route. The value function is modified to apply a score which is different for each vehicle.
Save the following information in an input.json
file (see input and output for more information on working with input files).
Code
The following program uses the CLI Runner to obtain a solution and requires access to the Nextmv code repository on GitHub. To request access, please contact support@nextmv.io.
To proceed with running the example, create a main.go
file and use the following code snippets. A custom type called vehicleData
is defined to keep track of the aforementioned locations
at the vehicle
engine level.
The vehicleData
type implements the VehicleUpdater
interface. Before updating the state, the Clone
function is applied. This function is used to prepare the custom type before being updated. This is useful for clearing data. Note that the Update
function returns a non-nil
integer which corresponds to the vehicle's objective value. You can notice that the value is obtained by applying the vehicle's score. The locations
attribute is updated for every state transition.
Similarly, a custom type called fleetData
is defined to keep track of the locations
at the fleet
engine level.
The fleetData
type implements the FleetUpdater
interface. Before updating the state, the Clone
function is applied. Note that the Update
function returns a non-nil
integer which corresponds to the sum of the vehicles' objective values. At each fleet
state, only the modified vehicles are processed to calculate the total value. In a comparable way, the global locations
type is updated based on the particular locations
at each vehicle. To achieve this, the vehicle
engine's state is cast to the Updater
interface. Calling on the Updater()
method returns the customized vehicle
updater, which in this example is the vehicleData
struct. There is no need to implement the Updater
interface as it is done in the underlying vehicle
engine. The locations
attribute is updated for every state transition.
Lastly, using the above definitions, you can define your main
function and execute the complete program.
To execute the example, specify the path to the input.json
file using command-line flags and use jq to extract the solution state (see runners for more information on building and running programs).
Solution
The solution should look similar to this one:
Please note that:
- An
updater
object is marshalled as part of the output holding the information for thelocations
indicating the route position for each stop. - Most of the stops are assigned to the vehicle with the lowest score.