void TabuSearch(){ int debug; int number_of_iterations = 100; int TabuLength = n; get_first_path(); vector<vector<int> > tabuList(TabuLength, std::vector<int>(TabuLength)); vector <int> bestSol((int)current_path.size()); bestSol = current_path; int shortest_path = GetTotalDistance(current_path); for (int i = 0; i < number_of_iterations;i++) { current_path = getBestNeighbour(tabuList, current_path); int currCost = GetTotalDistance(current_path); if (currCost < shortest_path) { bestSol = current_path; shortest_path = currCost; } } printf("shortest_path = %d", shortest_path); getchar(); getchar(); }
Data MatrixGraph::tabuSearch(uint tabuListSize, uint iterations) { clock_t overallTime = clock(); initRand(); if (!iterations) iterations = 1 << 10; //iterations = 50; stringstream results; vector<uint> currentPath, bestPath; currentPath.reserve(vertexNumber), bestPath.reserve(vertexNumber); //TabuArray tabuList(vertexNumber, tabuListSize); TabuList tabuList(tabuListSize); uint currentCost = 0, bestCost = 0, diverseLimit = (iterations >> 4) + 1, noChange = 0; // 1 - Create an initial solution(could be created randomly), now call it the current solution. for (uint i = 0; i < vertexNumber; ++i) currentPath.push_back(i); currentCost = calculateCost(currentPath); bestPath = currentPath; bestCost = currentCost; for (uint i = 0; i < iterations; ++i) { // 2 - Find the best neighbor of the current solution by applying certain moves. currentCost = getBestNeighbour(tabuList, currentPath); // 3 - If the best neighbor is reached my performing a non - tabu move, accept as the new current solution. // else, find another neighbor(best non - tabu neighbour). if (currentCost < bestCost) { bestPath = currentPath; bestCost = currentCost; noChange = 0; } else { if (++noChange > diverseLimit) { random_shuffle(currentPath.begin(), currentPath.end()); currentCost = calculateCost(currentPath); tabuList.clear(); noChange = 0; } } } // 4 - If maximum number of iterations are reached(or any other stopping condition), go to 5, else go to 2. // 5 - Globally best solution is the best solution we found throughout the iterations. double duration = (clock() - overallTime) / (double)CLOCKS_PER_SEC; results << "Koszt drogi: " << bestCost << "\nCalkowity czas trwania: " << duration << " sekund\n"; return Data(bestPath, bestCost, results.str(), duration); }