I have a distance matrix with first row and column set to zeros. I try to run the code provided on the VRP website (Or-Tools) and it provides me with the following solution. This is obviously not the correct solution since a vehicle cannot hit all the places with a total distance of 0m.
def create_data_model():
"""Stores the data for the problem."""
data = {}
data["distance_matrix"] = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 14.0, 201.0, 154.0, 154.0],
[0.0, 46.0, 0.0, 165.0, 118.0, 118.0],
[0.0, 70.0, 54.0, 0.0, 110.0, 110.0],
[0.0, 24.0, 26.0, 187.0, 0.0, 140.0],
[0.0, 28.0, 0.0, 199.0, 152.0, 0.0]]
data["num_vehicles"] = 3
data["depot"] = 0
return data
def print_solution(data, manager, routing, solution):
"""Prints solution on console."""
print(f"Objective: {solution.ObjectiveValue()}")
max_route_distance = 0
for vehicle_id in range(data["num_vehicles"]):
index = routing.Start(vehicle_id)
plan_output = f"Route for vehicle {vehicle_id}:n"
route_distance = 0
while not routing.IsEnd(index):
plan_output += f" {manager.IndexToNode(index)} -> "
previous_index = index
index = solution.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id
)
plan_output += f"{manager.IndexToNode(index)}n"
plan_output += f"Distance of the route: {route_distance}mn"
print(plan_output)
max_route_distance = max(route_distance, max_route_distance)
print(f"Maximum of the route distances: {max_route_distance}m")
def main():
"""Entry point of the program."""
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(
len(data["distance_matrix"]), data["num_vehicles"], data["depot"]
)
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
# Create and register a transit callback.
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data["distance_matrix"][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Add Distance constraint.
dimension_name = "Distance"
routing.AddDimension(
transit_callback_index,
0, # no slack
5000, # vehicle maximum travel distance
True, # start cumul to zero
dimension_name,
)
distance_dimension = routing.GetDimensionOrDie(dimension_name)
distance_dimension.SetGlobalSpanCostCoefficient(100)
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if solution:
print_solution(data, manager, routing, solution)
else:
print("No solution found !")
I tried changing the vehicle maximum travel distance and global span coefficient. I get the following solution.
Objective: 0
Route for vehicle 0:
0 -> 0
Distance of the route: 0m
Route for vehicle 1:
0 -> 0
Distance of the route: 0m
Route for vehicle 2:
0 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
Distance of the route: 0m
Maximum of the route distances: 0m
New contributor
Charles Tang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.