Algorithm for Fair Distribution of Orders Among Trips Based on Weight Constraints

“I’m developing a system to create trips for delivery orders, and I’m facing challenges in implementing a fair distribution algorithm to allocate orders among trips based on cubes (weight) constraints. Here’s an overview of my system and the specific problem I’m encountering:

System Overview:

  • The system aims to optimize the assignment of delivery orders to trips for efficient and to distribute fair numbers of orders based on no of stops and item cube.
  • Trips are created for a specific date and have constraints such as maximum cube capacity (selected_cube) and maximum number of stops (selected_stops).
  • Orders need to be distributed among trips in a fair and optimal manner, considering both cube constraints and no of stops.

Problem Statement:
I’m implementing a trip creation system in my application where I need to distribute orders among trips using a round-robin approach.

Example Scenario:
Let’s consider a scenario where there are 100 delivery orders with varying cubes, and we need to create trips for these orders. Each trip has a maximum weight capacity of 3000 cubes and can make up to 10 stops. How can we distribute these orders among trips to minimize the number of trips required while ensuring that no trip exceeds its weight capacity or stop limit? my current code is not populating a fair distribution of orders among trips when the total_selected_orders_cube is below 5000.

I have tried Round-robin and greedy approach unable to handle all the scenerios like small amount of selected orders.

Here’s a simplified version of my current implementation of round-robin:

    try {
    DB::beginTransaction();
    
    // Initialization code...

    $totalAssignedOrders = 0; // Track total assigned orders across all trips
    $tripIndices = []; // store trip indices for round-robin assignment
    $totalCreatedTrips = 0;
    
    for ($i = 0; $i < $requiredTrips && $totalAssignedOrders < $selectedOrdersCount; $i++){
        if (!isset($tripIndices[0]) || $tripIndices[0] >= $requiredTrips) {
            $tripIndices[0] = 0; // reset index if all trips are full
        }
    
        $trip = $this->tripService->createNewTrip($company_id, $schedule_date, 
        $defaultSelectedCubes, $defaultAssemblyTime);
    
        $unassignedOrders = []; // Store unassigned order IDs for this trip
    
        foreach ($request->input('orders') as $orderData) {
            $order = Orders::findOrFail($orderData['id']);
    
            // Check if the order already has a trip_id assigned
            if ($order->trip_id !== null) {
                continue;
            }
    
            $orderItems = $order->orderItems;
            $orderCube = $orderItems->sum('item_cube') ?? 0;
            $orderAssemblyTime = $orderItems->sum('assembly_time') ?? 0;
    
            if (
                $trip->assigned_cubes + $orderCube <= $defaultSelectedCubes &&
                $trip->assigned_assembly_time + $orderAssemblyTime <= $defaultAssemblyTime 
                &&
                $trip->no_of_stops < $defaultNoOfStops
            ) {
                $this->assignOrderToTrip($order, $trip, $schedule_date);
    
                $trip->assigned_cubes += $orderCube;
                $trip->assigned_assembly_time += $orderAssemblyTime;
                $trip->no_of_stops += 1;
                $trip->save();
    
                $totalAssignedOrders++; // Increment total assigned orders
            } else {
                $unassignedOrders[] = $order->id; // store only order ID for efficiency
            }
        }
    
        // Check if no orders were assigned to the newly created trip
        if (empty($unassignedOrders)) {
            continue; // Skip saving empty trips
        }
    
        $tripIndices[0]++; // increment trip index for round-robin
        $totalCreatedTrips++;
    }

    DB::commit();

    } catch (Exception $e) {
      // Exception handling code...
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật