Пример #1
0
void CVRPSolver::attemptVehicleExchange(CSolutionInfo& solutionInfo) {
    ++m_iGeneratedSolutionCount;
    ++m_iStepsSinceLastSolution;
    CMoveInfo curMove;
    CMoveInfo bestMove;

    int bestFreeCapacity = 0;
    std::pair<int, int> bestSwapIndex;
    int totalTour = static_cast<int>(solutionInfo.getTourCount());

    for (int i = 0; i < totalTour; ++i) {
        CTourInfo firstTour = solutionInfo.getTour(i);
        int firstTourLoad = firstTour.getVehicleInfo().getCurrentLoad();
        int firstVehicleCapacity = firstTour.getVehicleInfo().getCapacity();

        for (int j = i + 1; j < totalTour; ++j) {
            CTourInfo secondTour = solutionInfo.getTour(j);
            curMove.setInitialTour(firstTour, secondTour);

            int FirstTourRemainingCapacity = firstVehicleCapacity - secondTour.getVehicleInfo().getCurrentLoad();

            int SecondTourRemainingCapacity = secondTour.getVehicleInfo().getCapacity() - firstTourLoad;

            // int prevFreeCapacity = max(secondTour.getRemainingCapacity(), firstTour.getRemainingCapacity() );

            int curFreeCapacity = (std::max)(FirstTourRemainingCapacity, SecondTourRemainingCapacity);

            if ((FirstTourRemainingCapacity > 0) &&  (SecondTourRemainingCapacity > 0) &&
                    // curFreeCapacity > curFreeCapacity autological compare evaluates to false (error on MAC)
                    (curFreeCapacity > bestFreeCapacity)) {
                CVehicleInfo tempVehicle = m_vVehicleInfos[firstTour.getVehicleId()];
                firstTour.setVehicleInfo(m_vVehicleInfos[secondTour.getVehicleId()]);
                secondTour.setVehicleInfo(tempVehicle);

                curMove.setModifiedTour(firstTour, secondTour);

                if (!isTabuMove(curMove)) {
                    bestMove = curMove;
                    bestFreeCapacity = curFreeCapacity;
                    bestSwapIndex = std::make_pair(i, j);
                }

                curMove.getInitialTour(firstTour, secondTour);
            }
        }
    }
    if (bestFreeCapacity > 0) {
        CTourInfo tempTour;
        bestMove.getModifiedTourAt(0, tempTour);
        solutionInfo.replaceTourAt(bestSwapIndex.first, tempTour);
        bestMove.getModifiedTourAt(1, tempTour);
        solutionInfo.replaceTourAt(bestSwapIndex.second, tempTour);
        updateTabuCount(bestMove);
        updateFinalSolution(solutionInfo);
    }
}
Пример #2
0
bool CSolutionInfo::addTour(CTourInfo& tour) {
    m_vtourAll.push_back(tour);
    int vid = tour.getVehicleId();
    std::vector<int>::iterator it;
    it = std::find(m_vUnusedVehicles.begin(), m_vUnusedVehicles.end(), vid);
    if (it != m_vUnusedVehicles.end()) {
        m_vUnusedVehicles.erase(it);
    }
    m_iVehicleUsed++;
    m_dTotalDistance += tour.getDistance();
    m_dTotalTravelTime += tour.getTravelTime();
    m_dTotalCost += tour.getCost();

    std::vector<int> vecOrders = tour.getOrderVector();

    m_iOrdersServed += static_cast<int>(vecOrders.size());

    for (const auto &order : vecOrders) {
        int oid = order;
        it = std::find(m_vUnservedOrderId.begin(), m_vUnservedOrderId.end(), oid);
        if (it != m_vUnservedOrderId.end()) {
            m_vUnservedOrderId.erase(it);
        }
    }

    return true;
}
Пример #3
0
void CSolutionInfo::replaceTour(CTourInfo curTour) {
    unsigned int i;
    for (i = 0; i < m_vtourAll.size(); i++) {
        if (m_vtourAll[i].getVehicleId() == curTour.getVehicleId()) {
            m_vtourAll[i] = curTour;
            return;
        }
    }
    return;
}
Пример #4
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;
}