Example #1
0
std::pair<int, double> CVRPSolver::getPotentialInsert(CTourInfo& curTour, COrderInfo& curOrder) {
    std::pair<int, double> bestInsert = std::make_pair(-1, DOUBLE_MAX);
    if (curOrder.getOrderUnit() > curTour.getRemainingCapacity()) {
        return bestInsert;
    }
    // check if ith position insert is fisible.
    std::vector<int> vecOrderId = curTour.getOrderVector();
    for (unsigned int i = 0; i <= vecOrderId.size(); ++i) {
        CostPack costToOrder, costFromOrder;

        if (!i) {
            costToOrder = getDepotToOrderCost(curTour.getStartDepot(), curOrder.getOrderId());
        } else {
            costToOrder = getOrderToOrderCost(vecOrderId[i-1], curOrder.getOrderId());
        }

        double dArrivalTime = costToOrder.traveltime + curTour.getStartTime(i);

        if (dArrivalTime > curOrder.getCloseTime()) {
            continue;
        }

        if (i == vecOrderId.size()) {
            costFromOrder = getOrderToDepotCost(curOrder.getOrderId(), curTour.getEndDepot());
        } else {
            costFromOrder = getOrderToOrderCost(curOrder.getOrderId(), vecOrderId[i]);
        }

        dArrivalTime += curOrder.getServiceTime() + costFromOrder.traveltime;

        if (i < vecOrderId.size() &&  dArrivalTime > m_vOrderInfos[m_mapOrderIdToIndex[vecOrderId[i]]].getCloseTime()) {
            continue;
        }

        CostPack totalCost = getCostForInsert(curTour, curOrder, i);

        if (totalCost.cost < bestInsert.second) {
            bestInsert = std::make_pair(i, totalCost.cost);
        }
    }
    return bestInsert;
}
Example #2
0
int find_vrp_solution(vrp_vehicles_t *vehicles, size_t vehicle_count,
                      vrp_orders_t *orders, size_t order_count,
                      vrp_cost_element_t *costmatrix, size_t cost_count,
                      int depot_id,
                      vrp_result_element_t **results, size_t *result_count, char **err_msg) {
    int res;

    std::string strError;
    try {
        PGR_LOG("Before load order");
        loadOrders(orders, static_cast<int>(order_count), depot_id);
        PGR_LOG("After load order");
        loadVehicles(vehicles, static_cast<int>(vehicle_count));
        PGR_LOG("After load vehicles");
        loadDistanceMatrix(costmatrix, static_cast<int>(cost_count), depot_id);
        PGR_LOG("After load distance matrix");
        res = solver.solveVRP(strError);
        PGR_LOG("After VRP Solve");
    }
    catch(std::exception& e) {
        *err_msg = (char *) e.what();
        return -1;
    }
    catch(...) {
        *err_msg = (char *) "Caught unknown exception!";
        return -1;
    }


    if (res < 0) {
        return res;
    } else {
        try {
        CSolutionInfo solution;
        CTourInfo ctour;
        // bool bOK =
                solver.getSolution(solution, strError);
        auto totalRoute = solution.getTourInfoVector().size();
        size_t totRows = 0;
        for (size_t i = 0; i < totalRoute; i++) {
            totRows += (solution.getTour(static_cast<int>(i)).getServedOrderCount() + 2);
        }
        *results = (vrp_result_element_t *) malloc(totRows * sizeof(vrp_result_element_t));
        *result_count = totRows;
        int cnt = 0;
        for (size_t i = 0; i < totalRoute; i++) {
            ctour = solution.getTour(static_cast<int>(i));
            std::vector<int> vecOrder = ctour.getOrderVector();
            auto totalOrder = vecOrder.size();

            // For start depot
            (*results)[cnt].order_id = ctour.getStartDepot();
            (*results)[cnt].order_pos = 0;
            (*results)[cnt].vehicle_id = ctour.getVehicleId();
            (*results)[cnt].arrival_time = -1;
            (*results)[cnt].depart_time = ctour.getStartTime(0);
            cnt++;

            // For each order
            for (size_t j = 0; j < totalOrder; j++) {
                (*results)[cnt].order_id = vecOrder[j];
                (*results)[cnt].order_pos = static_cast<int>(j) + 1;
                (*results)[cnt].vehicle_id = ctour.getVehicleId();
                (*results)[cnt].depart_time = ctour.getStartTime(static_cast<int>(j) + 1);
                (*results)[cnt].arrival_time = ctour.getStartTime(static_cast<int>(j) + 1) - solver.getServiceTime(vecOrder[j]);
                cnt++;
            }

            // For return depot
            (*results)[cnt].order_id = ctour.getEndDepot();
            (*results)[cnt].order_pos = static_cast<int>(totalOrder) + 1;
            (*results)[cnt].vehicle_id = ctour.getVehicleId();
            (*results)[cnt].arrival_time = ctour.getStartTime(static_cast<int>(totalOrder) + 1);
            (*results)[cnt].depart_time = -1;
            cnt++;
        }
        }
        catch(std::exception& e) {
        *err_msg = (char *) e.what();
        return -1;
        }
        catch(...) {
        *err_msg = (char *) "Caught unknown exception!";
        return -1;
        }
    }
    return EXIT_SUCCESS;
}