Skip to content

Commit

Permalink
Merge pull request #4521 from arnabanimesh/patch-3
Browse files Browse the repository at this point in the history
Don't add negative load during reloading at depots when calculat…
  • Loading branch information
lperron authored Jan 30, 2025
2 parents bfb987f + 81814e6 commit 15b93ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
15 changes: 9 additions & 6 deletions examples/notebook/routing/cvrp_reload.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -403,31 +403,34 @@
" continue\n",
" index = routing.Start(vehicle_id)\n",
" plan_output = f\"Route for vehicle {vehicle_id}:\\n\"\n",
" load_value = 0\n",
" distance = 0\n",
" while not routing.IsEnd(index):\n",
" load_var = capacity_dimension.CumulVar(index)\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 += distance_dimension.GetTransitValue(previous_index, index, vehicle_id)\n",
" load_var = capacity_dimension.CumulVar(index)\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_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",
" 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_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 += assignment.Min(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",
Expand Down
15 changes: 9 additions & 6 deletions ortools/routing/samples/cvrp_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,31 +336,34 @@ def print_solution(
continue
index = routing.Start(vehicle_id)
plan_output = f"Route for vehicle {vehicle_id}:\n"
load_value = 0
distance = 0
while not routing.IsEnd(index):
load_var = capacity_dimension.CumulVar(index)
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 += distance_dimension.GetTransitValue(previous_index, index, vehicle_id)
load_var = capacity_dimension.CumulVar(index)
# 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))
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_value}\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_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}")
Expand Down

0 comments on commit 15b93ce

Please sign in to comment.