Features

Custom output

A how-to guide for implementing custom output with vehicle routing.

This feature is only available for Platform. You can find a list of all available features here.

This how-to guide assumes you already completed the get started with vehicle routing tutorial.

You can change the default output that is generated by our vehicle routing engine. If you would like to calculate and track some KPI then we recommend you to use our statistics convention because it allows you to track this data running experiments on our platform. You can read more about this here.

But you can also generate your complete own output. For example, this may be useful if you want to integrate the engine into an existing system where several services depend on a certain output structure.

Example

First create a new struct which will repesent your output data:

type customOutput struct {
	Custom string  `json:"custom,omitempty"`
	Value  float64 `json:"value,omitempty"`
}
Copy

And then create a new function that takes a nextroute.Solution as an input and returns the output struct.

func toOutput(solution nextroute.Solution) customOutput {
	value := 0.0
	for _, t := range solution.Model().Objective().Terms() {
		value += solution.ObjectiveValue(t.Objective())
	}
	return customOutput{
		Custom: "hello world",
		Value:  value,
	}
}
Copy

Now we adapt the solver function to adhere to this new struct and change its return value to the new output struct:

func solver(
	ctx context.Context,
	input schema.Input,
	options options,
) (customOutput, error) {
Copy

Lastly, use the new output function to return you custom output:

last, err := solutions.Last()
if err != nil {
	return customOutput{}, err
}
out := toOutput(last)

return out, nil
Copy

Page last updated

Go to on-page nav menu