Can someone explain me, on an example, how is the cost calculated if I set global span cost coefficient over the distance dimension?
You can then set a cost equal to the maximum of the total distances along each route.
https://developers.google.com/optimization/routing/vrp#distance_dimension
which is computed as:
cost_coefficient * (
max({CumulVar(end_1),...,CumulVar(end_k)}) -
min({CumulVar(start_1),...,CumulVar(start_k)})
)
https://developers.google.com/optimization/routing/vrp#dimension_details
note: usually your vehicles start at 0 (if you set start_cumul_to_zero to True)
e.g. in the VRP examples https://developers.google.com/optimization/routing/vrp#solution you have:
Route for vehicle 0:
0 -> 8 -> 6 -> 2 -> 5 -> 0
Distance of route: 1552m
Route for vehicle 1:
0 -> 7 -> 1 -> 4 -> 3 -> 0
Distance of route: 1552m
Route for vehicle 2:
0 -> 9 -> 10 -> 16 -> 14 -> 0
Distance of route: 1552m
Route for vehicle 3:
0 -> 12 -> 11 -> 15 -> 13 -> 0
Distance of route: 1552m
So the global span cost will be: 100 * 1552 = 155200 added to the objective.
here: 100 because the example set it...
# Try to minimize the max distance among vehicles.
distance_dimension.SetGlobalSpanCostCoefficient(100)
And if I have a time matrix and I add it as a new dimension and I set a span cost coefficient, how will influence the final cost?
the final cost will be the sum of all arc cost used + coef * (max(VehicleEndTimes) - min(VehicleStartTimes))
VehiclesEndTime being time_dimension.CumulVar(routing.End(vehicle_index)) for all vehicles...
Each dimension is just a "int" increasing/decreasing along the route of each vehicle with few constraints on it, so it can be distance, time(duration), capacity, whatever....
Most helpful comment
the final cost will be
the sum of all arc cost used + coef * (max(VehicleEndTimes) - min(VehicleStartTimes))VehiclesEndTime being
time_dimension.CumulVar(routing.End(vehicle_index))for all vehicles...Each dimension is just a "int" increasing/decreasing along the route of each vehicle with few constraints on it, so it can be distance, time(duration), capacity, whatever....