You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

带载重、托盘数约束的订单运输多车辆分配实现方法

全量订单运输分配实现方案

你现有fill_one_car()仅实现了单台车的单次装载逻辑,缺少待分配订单池维护、已分配订单剔除、循环终止判断环节,按以下方式改造即可得到完整的多车分配结果。

核心逻辑说明

  • 提前筛选超大订单:单个订单重量/托盘数超过单车上限的订单,每单单独占用1台车,不进入混装逻辑
  • 改造单车载货函数:入参为剩余待分配订单列表,返回「当前车辆装载订单」「剩余待分配订单」两个结果,避免重复分配已装走的订单
  • 循环调用载货函数:直到待分配订单池为空,所有车辆的装载列表汇总即为最终结果

完整实现代码

import pandas as pd

# 车辆约束参数
max_weight = 20000
max_pallets = 44

# ----------------------
# 此处替换为你的实际DataFrame读取逻辑
df = pd.DataFrame({
    'order #': [31785053, 31785071, 31785044, 31827117, 31827228],
    'pallets': [1.0, 1.0, 6.0, 7.0, 1.0],
    'weight': [174.0, 45.0, 8300.0, 9684.0, 1404.0]
})
# ----------------------

# 预处理:拆分超大订单和可混装普通订单
big_orders = []
normal_orders = []
for _, row in df.iterrows():
    oid = row['order #']
    p_cnt = row['pallets']
    w_val = row['weight']
    if p_cnt > max_pallets or w_val > max_weight:
        big_orders.append([oid])
    else:
        normal_orders.append((oid, p_cnt, w_val))

# 改造后的单车载货函数,兼容原有判断逻辑
def fill_one_car(remaining_orders):
    current_car = []
    total_pallets = 0
    total_weight = 0
    left_orders = []
    for oid, p, w in remaining_orders:
        if (total_pallets + p <= max_pallets) and (total_weight + w <= max_weight):
            current_car.append(oid)
            total_pallets += p
            total_weight += w
        else:
            left_orders.append((oid, p, w))
    return current_car, left_orders

# 循环执行全部分配
final_allocation = []
# 先加入所有超大订单的专属车辆
final_allocation.extend(big_orders)

remaining_pool = normal_orders.copy()
while remaining_pool:
    car_orders, remaining_pool = fill_one_car(remaining_pool)
    # 死循环防护:如果当前车没装到任何订单,说明剩余订单存在异常,终止循环
    if not car_orders:
        break
    final_allocation.append(car_orders)

# 输出结果
print(final_allocation)

结果说明

  • 用提供的示例数据运行,所有订单总托盘数16、总重量19607,均未超过车辆约束,输出为:
    [[31785053, 31785071, 31785044, 31827117, 31827228]]
    
  • 当订单总量超过单车上限时,代码会自动将剩余订单拆分到后续车辆,最终输出需要的嵌套列表格式,每个子列表对应一台车的装载订单。

可选优化

如果需要提升车辆装载率、减少总用车数,可以在预处理普通订单后增加一行排序代码,采用**首次适配递减(FFD)**装箱策略,相比默认顺序装载通常能提升10%~15%的空间利用率:

# 普通订单按重量降序、托盘数降序排序后再分配
normal_orders.sort(key=lambda x: (-x[2], -x[1]))

内容的提问来源于stack exchange,提问作者eeealesha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 16:36:30