I am solving the capacitated vehicle routing problem using OR Tools (Python). I just want to add a constraint, i.e., an upper limit on the length of the largest route. Can someone please let me know how I can do it in the code below.
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
def print_solution(data, manager, routing, solution):
“””Prints solution on console.”””
print(f”Objective: {solution.ObjectiveValue()}”)
total_distance = 0
total_load = 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
route_load = 0
while not routing.IsEnd(index):
node_index = manager.IndexToNode(index)
route_load += data[“demands”][node_index]
plan_output += f” {node_index} Load({route_load}) -> ”
previous_index = index
index = solution.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id
)
plan_output += f” {manager.IndexToNode(index)} Load({route_load})n”
plan_output += f”Distance of the route: {route_distance}mn”
plan_output += f”Load of the route: {route_load}n”
print(plan_output)
total_distance += route_distance
total_load += route_load
print(f”Total distance of all routes: {total_distance}m”)
print(f”Total load of all routes: {total_load}”)
def main():
“””Solve the CVRP problem.”””
# 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 Capacity constraint.
def demand_callback(from_index):
"""Returns the demand of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = manager.IndexToNode(from_index)
return data["demands"][from_node]
demand_callback_index = routing.RegisterUnaryTransitCallback(demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_callback_index,
0, # null capacity slack
data["vehicle_capacities"], # vehicle maximum capacities
True, # start cumul to zero
"Capacity",
)
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
)
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH
)
search_parameters.time_limit.FromSeconds(1)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if solution:
print_solution(data, manager, routing, solution)
if name == “main“:
main()
Santosh Kumar Mandal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.