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); } }
bool CVRPSolver::insertOrder(CTourInfo& tourInfo, int orderId, int pos) { if (pos < 0 || (unsigned int) pos > tourInfo.getOrderVector().size()) return false; int orderIndex = m_mapOrderIdToIndex[orderId]; if (!tourInfo.getVehicleInfo().loadUnit(m_vOrderInfos[orderIndex].getOrderUnit())) return false; tourInfo.insertOrder(orderId, pos); if (!updateTourCosts(tourInfo)) { tourInfo.removeOrder(pos); return false; } return true; }