| import pandas as pd |
| import matplotlib.pyplot as plt |
| from ortools.constraint_solver import routing_enums_pb2 |
| from ortools.constraint_solver import pywrapcp |
|
|
|
|
| def optimize_picking_route(df: pd.DataFrame): |
| df = df.copy() |
|
|
| |
| coords = df[["Aisle", "Rack"]].values.tolist() |
|
|
| |
| size = len(coords) |
| dist = [[abs(a1 - a2) + abs(r1 - r2) |
| for (a2, r2) in coords] for (a1, r1) in coords] |
|
|
| |
| manager = pywrapcp.RoutingIndexManager(size, 1, 0) |
| routing = pywrapcp.RoutingModel(manager) |
|
|
| def dist_callback(i, j): |
| return dist[manager.IndexToNode(i)][manager.IndexToNode(j)] |
|
|
| transit_idx = routing.RegisterTransitCallback(dist_callback) |
| routing.SetArcCostEvaluatorOfAllVehicles(transit_idx) |
|
|
| params = pywrapcp.DefaultRoutingSearchParameters() |
| params.first_solution_strategy = routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC |
|
|
| solution = routing.SolveWithParameters(params) |
|
|
| order = [] |
| idx = routing.Start(0) |
| while not routing.IsEnd(idx): |
| order.append(manager.IndexToNode(idx)) |
| idx = solution.Value(routing.NextVar(idx)) |
|
|
| ordered_df = df.iloc[order] |
|
|
| |
| plt.figure(figsize=(5, 5)) |
| plt.plot(ordered_df["Aisle"], ordered_df["Rack"], marker="o") |
| plt.title("Optimized Picking Route") |
| plt.xlabel("Aisle") |
| plt.ylabel("Rack") |
| img_path = "/tmp/route_plot.png" |
| plt.savefig(img_path) |
| plt.close() |
|
|
| return img_path, ordered_df |
|
|