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); } }
void CVRPSolver::attemptFeasibleNodeExchange(CSolutionInfo& solutionInfo) { ++m_iGeneratedSolutionCount; ++m_iStepsSinceLastSolution; CMoveInfo bestMove, curMove; int totalTour = solutionInfo.getTourCount(); for (int i = 0; i < totalTour; ++i) { CTourInfo curTour = solutionInfo.getTour(i); std::vector<int> vecOrderId = curTour.getOrderVector(); curMove.setInitialTour(curTour); int totalCustomer = curTour.getServedOrderCount(); std::pair<int, int> bestSwapIndex; double lowestCost = DOUBLE_MAX; for (int j = 0; j < totalCustomer; ++j) { for (int k = j + 1; k < totalCustomer; ++k) { COrderInfo firstCustomer = m_vOrderInfos[m_mapOrderIdToIndex[vecOrderId[j]]]; COrderInfo secondCustomer = m_vOrderInfos[m_mapOrderIdToIndex[vecOrderId[k]]]; if (curTour->isFeasibleReplace(j, pSecondCustomer) && pCurTour->isFeasibleReplace(k, pFirstCustomer)) { pCurTour->removeCustomer(j, false); pCurTour->addCustomer(pSecondCustomer, j); pCurTour->removeCustomer(k, false); pCurTour->addCustomer(pFirstCustomer, k); pCurMove->setModifiedTour(pCurTour); if (isTabuMove(pCurMove)) { pCurMove->getInitialTour(pCurTour); continue; } double curTourCost = pCurTour->getTourData()->calcCost(pCurTour->getAssignedVehicle()); if (curTourCost < lowestCost) { *pBestMove = *pCurMove; lowestCost = curTourCost; bestSwapIndex = std::make_pair(j, k); } pCurMove->getInitialTour(pCurTour); } } } if (lowestCost!= DOUBLE_MAX) { m_pCurrentSolution->replaceTourAt(i, pBestMove->getModifiedTourAt(0)); this->updateTabuCount(pBestMove); this->evaluateCurrentSolution(); } } delete pCurMove; delete pBestMove; }