Example #1
0
void
Vehicle_pickDeliver::erase(const Order &order) {
    invariant();
    pgassert(has_order(order));


    Vehicle::erase(order.pickup());
    Vehicle::erase(order.delivery());
    orders_in_vehicle.erase(orders_in_vehicle.find(order.id()));

    invariant();
    pgassert(!has_order(order));
}
Example #2
0
void
Vehicle_pickDeliver::push_front(const Order &order) {
    invariant();
    pgassert(!has_order(order));

    orders_in_vehicle.insert(order.id());
    m_path.insert(m_path.begin() + 1, order.delivery());
    m_path.insert(m_path.begin() + 1, order.pickup());
    evaluate(1);

    pgassert(has_order(order));
    pgassert(!has_cv());
    invariant();
}
Example #3
0
void
Vehicle_pickDeliver::push_back(const Order &order) {
    invariant();
    pgassert(!has_order(order));

    orders_in_vehicle.insert(order.id());
    m_path.insert(m_path.end() - 1, order.pickup());
    m_path.insert(m_path.end() - 1, order.delivery());
    evaluate(m_path.size() - 3);

    pgassert(has_order(order));
    pgassert(!has_cv());
    invariant();
}
Example #4
0
Order
Vehicle_pickDeliver::get_worse_order(
        std::set<size_t> orders) const {
    invariant();
    pgassert(!empty());

    // auto orders(of_this_subset);
    auto worse_order(problem->orders()[*orders.begin()]);
    auto delta_duration((std::numeric_limits<double>::max)());
    auto curr_duration(duration());
    while (!orders.empty()) {
        auto truck(*this);
        auto order(problem->orders()[*orders.begin()]);
        pgassert(truck.has_order(order));
        orders.erase(orders.begin());
        truck.erase(order);
        auto delta = truck.duration() - curr_duration;
        if (delta < delta_duration) {
            worse_order = order;
            delta_duration = delta;
        }
    }
    return worse_order;
}
Example #5
0
void
Vehicle_pickDeliver::insert(const Order &order) {
    invariant();
    pgassert(!has_order(order));

    auto pick_pos(position_limits(order.pickup()));
    auto deliver_pos(position_limits(order.delivery()));
#ifndef NDEBUG
    std::ostringstream err_log;
    err_log << "\n\tpickup limits (low, high) = ("
        << pick_pos.first << ", "
        << pick_pos.second << ") "
        << "\n\tdeliver limits (low, high) = ("
        << deliver_pos.first << ", "
        << deliver_pos.second << ") "
        << "\noriginal" << tau();
#endif

    if (pick_pos.second < pick_pos.first) {
        /* pickup generates twv evrywhere,
         *  so put the order as last */
        push_back(order);
        return;
    }

    if (deliver_pos.second < deliver_pos.first) {
        /* delivery generates twv evrywhere,
         *  so put the order as last */
        push_back(order);
        return;
    }
    /*
     * Because delivery positions were estimated without
     * the pickup:
     *   - increase the upper limit position estimation
     */
    ++deliver_pos.second;


    auto d_pos_backup(deliver_pos);
    auto best_pick_pos = m_path.size();
    auto best_deliver_pos = m_path.size() + 1;
    auto current_duration(duration());
    auto min_delta_duration = (std::numeric_limits<double>::max)();
    auto found(false);
    pgassertwm(!has_order(order), err_log.str());
    while (pick_pos.first <= pick_pos.second) {
#ifndef NDEBUG
        err_log << "\n\tpickup cycle limits (low, high) = ("
            << pick_pos.first << ", "
            << pick_pos.second << ") ";
#endif
        Vehicle::insert(pick_pos.first, order.pickup());
#ifndef NDEBUG
        err_log << "\npickup inserted: " << tau();
#endif

        while (deliver_pos.first <= deliver_pos.second) {
            Vehicle::insert(deliver_pos.first, order.delivery());
            orders_in_vehicle.insert(order.id());
            pgassertwm(has_order(order), err_log.str());
#ifndef NDEBUG
            err_log << "\ndelivery inserted: " << tau();
#endif
            if (is_feasable()) {
                pgassert(is_feasable());
                auto delta_duration = duration()-current_duration;
                if (delta_duration < min_delta_duration) {
#ifndef NDEBUG
                    err_log << "\nsuccess" << tau();
#endif
                    min_delta_duration = delta_duration;
                    best_pick_pos = pick_pos.first;
                    best_deliver_pos = deliver_pos.first;
                    found = true;
                }
            }
            Vehicle::erase(deliver_pos.first);
#ifndef NDEBUG
            err_log << "\ndelivery erased: " << tau();
#endif
            ++deliver_pos.first;
        }
        Vehicle::erase(pick_pos.first);
#ifndef NDEBUG
        err_log << "\npickup erased: " << tau();
#endif
        orders_in_vehicle.erase(order.id());
        pgassertwm(!has_order(order), err_log.str());

        deliver_pos = d_pos_backup;
#ifndef NDEBUG
        err_log << "\n\trestoring deliver limits (low, high) = ("
            << deliver_pos.first << ", "
            << deliver_pos.second << ") ";
#endif
        ++pick_pos.first;
    }
    pgassertwm(!has_order(order), err_log.str());
    if (!found) {
        /* order causes twv
         *  so put the order as last */
        push_back(order);
        return;
    }
    Vehicle::insert(best_pick_pos, order.pickup());
    Vehicle::insert(best_deliver_pos, order.delivery());

    orders_in_vehicle.insert(order.id());
    pgassertwm(is_feasable(), err_log.str());
    pgassertwm(has_order(order), err_log.str());
    pgassertwm(!has_cv(), err_log.str());
    invariant();
}
Example #6
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;
}