Beispiel #1
0
CSolutionInfo CVRPSolver::generateInitialSolution() {
    CSolutionInfo initialSolution;
    PGR_LOG("Inside gen ini sol");
    std::vector<int> vecOrders, vecVehicles;
    for (unsigned int i = 0; i < m_vOrderInfos.size(); i++) {
        vecOrders.push_back(m_vOrderInfos[i].getOrderId());
    }

    for (unsigned int i = 0; i < m_vVehicleInfos.size(); i++) {
        vecVehicles.push_back(m_vVehicleInfos[i].getId());
    }

    initialSolution.init(vecOrders, static_cast<int>(vecOrders.size()), vecVehicles);

    int iUnusedVehicles = static_cast<int>(initialSolution.getUnusedVehicleCount());
    int iUnservedOrders = static_cast<int>(initialSolution.getUnservedOrderCount());  // m_viUnservedOrderIndex.size();
    PGR_LOG("before while");
    while (iUnusedVehicles &&  iUnservedOrders) {
        CTourInfo curTour;

        int vehicleIndex = rand() % iUnusedVehicles--;
        int vehicleInd = m_mapVehicleIdToIndex[initialSolution.getUnusedVehicleAt(vehicleIndex)];
        curTour.setVehicleInfo(m_vVehicleInfos[vehicleInd]);  // m_viUnusedVehicleIndex[vehicleIndex]
        initialSolution.removeVehicle(vehicleIndex);

        curTour.setStartDepot(m_vDepotInfos[0].getDepotId());
        curTour.setEndDepot(m_vDepotInfos[0].getDepotId());

        // use a random seed to start to tour. (we can use better approach in future)

        bool insertAvailable = true;

        while (insertAvailable) {
            insertAvailable = false;
            std::pair<int, int> PotentialInsert;  //  first = insert_index, second = removed_order_index;
            std::pair<int, double> bestInsert = std::make_pair(-1, DOUBLE_MAX);  // first = order_insert_index, second = cost;

            for (int i = 0; i < iUnservedOrders; ++i) {
                int orderInd = m_mapOrderIdToIndex[initialSolution.getUnservedOrderAt(i)];
                COrderInfo curOrder = m_vOrderInfos[orderInd];
                std::pair<int, double> curInsert = getPotentialInsert(curTour, curOrder);

                if (curInsert.second < bestInsert.second) {
                    insertAvailable = true;
                    bestInsert = curInsert;
                    PotentialInsert = std::make_pair(curInsert.first, i);
                }
            }
            if (insertAvailable) {
                if (insertOrder(curTour, initialSolution.getUnservedOrderAt(PotentialInsert.second), PotentialInsert.first)) {
                    iUnservedOrders--;
                    initialSolution.removeOrder(PotentialInsert.second);
                }
            }
        }

        initialSolution.addTour(curTour);
    }

    return initialSolution;
}