Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google ortools CVRP - different distance matrix by vehicle

In ortools, I know you can run the CVRP with different capacities per vehicle. However, can you pass a different distance matrix based upon the vehicle? For example, two cities may be 1000 miles apart, but it may be much faster to get there by airplane than by automobile, so in doing CVRP work I may wish to pass a time matrix, not an actual distance matrix. That time matrix would be different based upon the vehicle type.

like image 275
Ralph Avatar asked Dec 09 '25 17:12

Ralph


2 Answers

Should be close to this:

callback_indices = []
for vehicle_idx in range(data['n_vehicles']):
    def vehicle_callback(from_index, to_index, i=vehicle_idx):
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return data['vehicle_costs'][i] * data['time_matrices'][i][from_node][to_node]
    callback_index = routing.RegisterTransitCallback(vehicle_callback)
    callback_indices.append(callback_index)


routing.AddDimensionWithVehicleTransits(
    callback_indices,
    0,
    max,
    False,
    'DimensionName')
like image 166
Mizux Avatar answered Dec 11 '25 06:12

Mizux


You can pass a vector/list of evaluators.

Here is the C++ API/

like image 23
Laurent Perron Avatar answered Dec 11 '25 07:12

Laurent Perron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!