Пример #1
0
/*
 *   .. to ... from ....
 */
bool
Optimize::swap_worse(Vehicle_pickDeliver &to, Vehicle_pickDeliver &from) {
#if 0
    pgassert(from.orders_in_vehicle().size() <= to.orders_in_vehicle().size());
#endif
    auto from_truck = from;
    auto to_truck = to;

    auto swapped = false;

#if 0
    auto best_from_order = from_truck.orders_in_vehicle().front();
    auto best_to_order = to_truck.orders_in_vehicle().front();
#endif
    for (auto from_orders = from_truck.orders_in_vehicle();
            !from_orders.empty();
            from_orders.pop_front()) {
        auto from_order = from_truck.orders()[from_orders.front()];
#if 0
        pgassert(from_truck.has_order(from_order));
        msg.log << "\n" << from_orders;
        msg.log << "\n from " << from_order.idx()
            << "," << from_order.pickup().original_id();
        pgassert(from_truck.has_order(from_order));
#endif
        auto curr_from_duration = from_truck.duration();
        pgassert(from_truck.has_order(from_order));

        for (auto to_orders = to_truck.orders_in_vehicle();
                !to_orders.empty();
                to_orders.pop_front()) {
            pgassert(from_truck.has_order(from_order));

            auto to_order = to.orders()[to_orders.front()];
#if 0
            msg.log << "\n" << to_orders;
            msg.log << "\n To " << to_order.idx();
#endif
            auto curr_to_duration = to_truck.duration();

            /*
             * delete from_order, and to order from their trucks
             */
#if 0
            pgassert(from_truck.has_order(from_order));
            msg.log << "\n" << from_truck.tau();
            msg.log << "\n" << from_order.idx();
            pgassert(from_truck.has_order(from_order));
#endif
            from_truck.erase(from_order);
            to_truck.erase(to_order);

            /*
             * insert them in the other truck
             */
            from_truck.insert(to_order);
            to_truck.insert(from_order);

            if (from_truck.is_feasable() && to_truck.is_feasable()) {
                /*
                 * Can swap but:
                 *   - only swap when the total duration is reduced
                 *   - or from_truck duration is reduced
                 */
#if 0
                msg.log << "\n Can swap";
                msg.log << "\n Curr_from_duration " << curr_from_duration;
                msg.log << " Curr_to_duration " << curr_to_duration;
                msg.log << "\n  new_from_duration "
                    << from_truck.duration();
                msg.log << "  new_to_duration " << to_truck.duration();
#endif
                auto estimated_delta =
                    - (curr_from_duration + curr_to_duration)
                    + (to_truck.duration() + from_truck.duration());

#if 1
                auto estimated_duration = duration() + estimated_delta;

                if (from_truck.duration() < curr_from_duration ||
                        estimated_delta < 0 ||
                        estimated_duration < best_solution.duration()) {
#endif
                    msg.log
                        << "\n Found Swap order "
                        << from_order.pickup().id()
                        << " from truck " << from_truck.idx()
                        << " with order " << to_order.pickup().id()
                        << " of truck " << to_truck.idx();

                    swapped = true;
#if 0
                    best_to_order = to_order.idx();
                    best_from_order = from_order.idx();
#endif
                    p_swaps.push(
                            Swap_info(
                                from,
                                to,
                                from_order.idx(),
                                to_order.idx(),
                                estimated_delta));
#if 1
                }
#endif
            }
            to_truck = to;
            from_truck = from;
        }
        from_truck = from;
    }

    return false && swapped;
}