From e6ad8ca1604f860f51b1cff14b3c0d750331861b Mon Sep 17 00:00:00 2001 From: Arnab Animesh Das Date: Sun, 26 Jan 2025 19:42:25 +0530 Subject: [PATCH 1/4] Don't subtract negative load during reloading at depots when calculating total load delivered by the truck in cvrp_reload --- examples/notebook/routing/cvrp_reload.ipynb | 24 +++++++++++++++------ ortools/routing/samples/cvrp_reload.py | 18 ++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/examples/notebook/routing/cvrp_reload.ipynb b/examples/notebook/routing/cvrp_reload.ipynb index 5be46a21d30..e6c34124e09 100644 --- a/examples/notebook/routing/cvrp_reload.ipynb +++ b/examples/notebook/routing/cvrp_reload.ipynb @@ -399,31 +399,37 @@ " for vehicle_id in range(data[\"num_vehicles\"]):\n", " index = routing.Start(vehicle_id)\n", " plan_output = f\"Route for vehicle {vehicle_id}:\\n\"\n", + " previous_index = None\n", + " load_var = 0\n", " distance = 0\n", " while not routing.IsEnd(index):\n", - " load_var = capacity_dimension.CumulVar(index)\n", + " if previous_index is not None:\n", + " # capacity dimension TransitVar is negative at reload stations during replenishment\n", + " # don't want to consider those values when calculating the total load of the route\n", + " # hence only considering the positive values\n", + " load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id))\n", " time_var = time_dimension.CumulVar(index)\n", " plan_output += (\n", " f\" {manager.IndexToNode(index)} \"\n", - " f\"Load({assignment.Min(load_var)}) \"\n", + " f\"Load({assignment.Min(capacity_dimension.CumulVar(index))}) \"\n", " f\"Time({assignment.Min(time_var)},{assignment.Max(time_var)}) ->\"\n", " )\n", " previous_index = index\n", " index = assignment.Value(routing.NextVar(index))\n", " distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id)\n", - " load_var = capacity_dimension.CumulVar(index)\n", + " load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id))\n", " time_var = time_dimension.CumulVar(index)\n", " plan_output += (\n", " f\" {manager.IndexToNode(index)} \"\n", - " f\"Load({assignment.Min(load_var)}) \"\n", + " f\"Load({assignment.Min(capacity_dimension.CumulVar(index))}) \"\n", " f\"Time({assignment.Min(time_var)},{assignment.Max(time_var)})\\n\"\n", " )\n", " plan_output += f\"Distance of the route: {distance}m\\n\"\n", - " plan_output += f\"Load of the route: {assignment.Min(load_var)}\\n\"\n", + " plan_output += f\"Load of the route: {load_var}\\n\"\n", " plan_output += f\"Time of the route: {assignment.Min(time_var)}min\\n\"\n", " print(plan_output)\n", " total_distance += distance\n", - " total_load += assignment.Min(load_var)\n", + " total_load += load_var\n", " total_time += assignment.Min(time_var)\n", " print(f\"Total Distance of all routes: {total_distance}m\")\n", " print(f\"Total Load of all routes: {total_load}\")\n", @@ -490,7 +496,11 @@ ] } ], - "metadata": {}, + "metadata": { + "language_info": { + "name": "python" + } + }, "nbformat": 4, "nbformat_minor": 5 } diff --git a/ortools/routing/samples/cvrp_reload.py b/ortools/routing/samples/cvrp_reload.py index cc7ae754592..bcbb77f8655 100755 --- a/ortools/routing/samples/cvrp_reload.py +++ b/ortools/routing/samples/cvrp_reload.py @@ -332,31 +332,37 @@ def print_solution( for vehicle_id in range(data["num_vehicles"]): index = routing.Start(vehicle_id) plan_output = f"Route for vehicle {vehicle_id}:\n" + previous_index = None + load_var = 0 distance = 0 while not routing.IsEnd(index): - load_var = capacity_dimension.CumulVar(index) + if previous_index is not None: + # capacity dimension TransitVar is negative at reload stations during replenishment + # don't want to consider those values when calculating the total load of the route + # hence only considering the positive values + load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) time_var = time_dimension.CumulVar(index) plan_output += ( f" {manager.IndexToNode(index)} " - f"Load({assignment.Min(load_var)}) " + f"Load({assignment.Min(capacity_dimension.CumulVar(index))}) " f"Time({assignment.Min(time_var)},{assignment.Max(time_var)}) ->" ) previous_index = index index = assignment.Value(routing.NextVar(index)) distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id) - load_var = capacity_dimension.CumulVar(index) + load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) time_var = time_dimension.CumulVar(index) plan_output += ( f" {manager.IndexToNode(index)} " - f"Load({assignment.Min(load_var)}) " + f"Load({assignment.Min(capacity_dimension.CumulVar(index))}) " f"Time({assignment.Min(time_var)},{assignment.Max(time_var)})\n" ) plan_output += f"Distance of the route: {distance}m\n" - plan_output += f"Load of the route: {assignment.Min(load_var)}\n" + plan_output += f"Load of the route: {load_var}\n" plan_output += f"Time of the route: {assignment.Min(time_var)}min\n" print(plan_output) total_distance += distance - total_load += assignment.Min(load_var) + total_load += load_var total_time += assignment.Min(time_var) print(f"Total Distance of all routes: {total_distance}m") print(f"Total Load of all routes: {total_load}") From 57d16ac04fb0b18ce586bdfd8886ffafd1721571 Mon Sep 17 00:00:00 2001 From: Arnab Animesh Das Date: Sun, 26 Jan 2025 19:56:13 +0530 Subject: [PATCH 2/4] Simplify code --- examples/notebook/routing/cvrp_reload.ipynb | 11 ++++------- ortools/routing/samples/cvrp_reload.py | 6 ++++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/notebook/routing/cvrp_reload.ipynb b/examples/notebook/routing/cvrp_reload.ipynb index e6c34124e09..af63968ea39 100644 --- a/examples/notebook/routing/cvrp_reload.ipynb +++ b/examples/notebook/routing/cvrp_reload.ipynb @@ -399,15 +399,9 @@ " for vehicle_id in range(data[\"num_vehicles\"]):\n", " index = routing.Start(vehicle_id)\n", " plan_output = f\"Route for vehicle {vehicle_id}:\\n\"\n", - " previous_index = None\n", " load_var = 0\n", " distance = 0\n", " while not routing.IsEnd(index):\n", - " if previous_index is not None:\n", - " # capacity dimension TransitVar is negative at reload stations during replenishment\n", - " # don't want to consider those values when calculating the total load of the route\n", - " # hence only considering the positive values\n", - " load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id))\n", " time_var = time_dimension.CumulVar(index)\n", " plan_output += (\n", " f\" {manager.IndexToNode(index)} \"\n", @@ -417,7 +411,10 @@ " previous_index = index\n", " index = assignment.Value(routing.NextVar(index))\n", " distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id)\n", - " load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id))\n", + " # capacity dimension TransitVar is negative at reload stations during replenishment\n", + " # don't want to consider those values when calculating the total load of the route\n", + " # hence only considering the positive values\n", + " load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id))\n", " time_var = time_dimension.CumulVar(index)\n", " plan_output += (\n", " f\" {manager.IndexToNode(index)} \"\n", diff --git a/ortools/routing/samples/cvrp_reload.py b/ortools/routing/samples/cvrp_reload.py index bcbb77f8655..5f25072885a 100755 --- a/ortools/routing/samples/cvrp_reload.py +++ b/ortools/routing/samples/cvrp_reload.py @@ -332,7 +332,6 @@ def print_solution( for vehicle_id in range(data["num_vehicles"]): index = routing.Start(vehicle_id) plan_output = f"Route for vehicle {vehicle_id}:\n" - previous_index = None load_var = 0 distance = 0 while not routing.IsEnd(index): @@ -350,7 +349,10 @@ def print_solution( previous_index = index index = assignment.Value(routing.NextVar(index)) distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id) - load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) + # capacity dimension TransitVar is negative at reload stations during replenishment + # don't want to consider those values when calculating the total load of the route + # hence only considering the positive values + load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) time_var = time_dimension.CumulVar(index) plan_output += ( f" {manager.IndexToNode(index)} " From aec831e9d6c62d7681a75ad6b70dc86d9b4c720f Mon Sep 17 00:00:00 2001 From: Arnab Animesh Das Date: Thu, 30 Jan 2025 15:59:46 +0530 Subject: [PATCH 3/4] Change load_var to load_value --- ortools/routing/samples/cvrp_reload.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ortools/routing/samples/cvrp_reload.py b/ortools/routing/samples/cvrp_reload.py index f89c91140fc..fb51127560e 100755 --- a/ortools/routing/samples/cvrp_reload.py +++ b/ortools/routing/samples/cvrp_reload.py @@ -334,14 +334,9 @@ def print_solution( for vehicle_id in range(data["num_vehicles"]): index = routing.Start(vehicle_id) plan_output = f"Route for vehicle {vehicle_id}:\n" - load_var = 0 + load_value = 0 distance = 0 while not routing.IsEnd(index): - if previous_index is not None: - # capacity dimension TransitVar is negative at reload stations during replenishment - # don't want to consider those values when calculating the total load of the route - # hence only considering the positive values - load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) time_var = time_dimension.CumulVar(index) plan_output += ( f" {manager.IndexToNode(index)} " @@ -354,7 +349,7 @@ def print_solution( # capacity dimension TransitVar is negative at reload stations during replenishment # don't want to consider those values when calculating the total load of the route # hence only considering the positive values - load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) + load_value += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) time_var = time_dimension.CumulVar(index) plan_output += ( f" {manager.IndexToNode(index)} " @@ -362,11 +357,11 @@ def print_solution( f"Time({assignment.Min(time_var)},{assignment.Max(time_var)})\n" ) plan_output += f"Distance of the route: {distance}m\n" - plan_output += f"Load of the route: {load_var}\n" + plan_output += f"Load of the route: {load_value}\n" plan_output += f"Time of the route: {assignment.Min(time_var)}min\n" print(plan_output) total_distance += distance - total_load += load_var + total_load += load_value total_time += assignment.Min(time_var) print(f"Total Distance of all routes: {total_distance}m") print(f"Total Load of all routes: {total_load}") From 81814e6a09c58943618fbf78ad8fde567c9e6857 Mon Sep 17 00:00:00 2001 From: Arnab Animesh Das Date: Thu, 30 Jan 2025 16:04:29 +0530 Subject: [PATCH 4/4] Update python notebook and minor formatting --- examples/notebook/routing/cvrp_reload.ipynb | 8 ++++---- ortools/routing/samples/cvrp_reload.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/notebook/routing/cvrp_reload.ipynb b/examples/notebook/routing/cvrp_reload.ipynb index 82bd9c5375b..413e72cfb7a 100644 --- a/examples/notebook/routing/cvrp_reload.ipynb +++ b/examples/notebook/routing/cvrp_reload.ipynb @@ -401,7 +401,7 @@ " for vehicle_id in range(data[\"num_vehicles\"]):\n", " index = routing.Start(vehicle_id)\n", " plan_output = f\"Route for vehicle {vehicle_id}:\\n\"\n", - " load_var = 0\n", + " load_value = 0\n", " distance = 0\n", " while not routing.IsEnd(index):\n", " time_var = time_dimension.CumulVar(index)\n", @@ -416,7 +416,7 @@ " # capacity dimension TransitVar is negative at reload stations during replenishment\n", " # don't want to consider those values when calculating the total load of the route\n", " # hence only considering the positive values\n", - " load_var += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id))\n", + " load_value += max(0, capacity_dimension.GetTransitValue(previous_index, index, vehicle_id))\n", " time_var = time_dimension.CumulVar(index)\n", " plan_output += (\n", " f\" {manager.IndexToNode(index)} \"\n", @@ -424,11 +424,11 @@ " f\"Time({assignment.Min(time_var)},{assignment.Max(time_var)})\\n\"\n", " )\n", " plan_output += f\"Distance of the route: {distance}m\\n\"\n", - " plan_output += f\"Load of the route: {load_var}\\n\"\n", + " plan_output += f\"Load of the route: {load_value}\\n\"\n", " plan_output += f\"Time of the route: {assignment.Min(time_var)}min\\n\"\n", " print(plan_output)\n", " total_distance += distance\n", - " total_load += load_var\n", + " total_load += load_value\n", " total_time += assignment.Min(time_var)\n", " print(f\"Total Distance of all routes: {total_distance}m\")\n", " print(f\"Total Load of all routes: {total_load}\")\n", diff --git a/ortools/routing/samples/cvrp_reload.py b/ortools/routing/samples/cvrp_reload.py index fb51127560e..f77076991e3 100755 --- a/ortools/routing/samples/cvrp_reload.py +++ b/ortools/routing/samples/cvrp_reload.py @@ -349,7 +349,7 @@ def print_solution( # capacity dimension TransitVar is negative at reload stations during replenishment # don't want to consider those values when calculating the total load of the route # hence only considering the positive values - load_value += max(0,capacity_dimension.GetTransitValue(previous_index,index,vehicle_id)) + load_value += max(0, capacity_dimension.GetTransitValue(previous_index, index, vehicle_id)) time_var = time_dimension.CumulVar(index) plan_output += ( f" {manager.IndexToNode(index)} "