static void TabuSearch(Instance instance, Process process, Random rnd, Stopwatch clock, string choice) { int typeSol = 0; List <int>[] listArray = Utility.BuildSLComplete(instance); PathStandard incumbentSol; PathStandard solHeuristic = Utility.NearestNeightbor(instance, rnd, listArray); incumbentSol = (PathStandard)solHeuristic.Clone(); Utility.PrintHeuristicSolution(instance, process, incumbentSol, incumbentSol.cost, typeSol); Tabu tabu = new Tabu("A", instance, 100); TabuSearch(instance, process, tabu, solHeuristic, incumbentSol, clock); solHeuristic = (PathStandard)incumbentSol.Clone(); TwoOpt(instance, solHeuristic); Utility.PrintHeuristicSolution(instance, process, solHeuristic, incumbentSol.cost, typeSol); Console.WriteLine("Best distance found within the timelit is: " + incumbentSol.cost); }
static void TabuSearch(Instance instance, Process process, Tabu tabu, PathStandard currentSol, PathStandard incumbentPath, Stopwatch clock) { int typeSol; int indexStart = 0; string nextBestMove = ""; string nextWorstMove = ""; double bestGain = double.MaxValue; double worstGain = double.MinValue; int a, b, c, d; double distAC, distBD, distTotABCD; do { for (int j = 0; j < instance.NNodes; j++, indexStart = b) { a = indexStart; b = currentSol.path[a]; c = currentSol.path[b]; d = currentSol.path[c]; for (int i = 0; i < instance.NNodes - 3; i++, c = d, d = currentSol.path[c]) { if (!tabu.IsTabu(a, c) && !tabu.IsTabu(b, d)) { distAC = Point.Distance(instance.Coord[a], instance.Coord[c], instance.EdgeType); distBD = Point.Distance(instance.Coord[b], instance.Coord[d], instance.EdgeType); distTotABCD = Point.Distance(instance.Coord[a], instance.Coord[b], instance.EdgeType) + Point.Distance(instance.Coord[c], instance.Coord[d], instance.EdgeType); if ((distAC + distBD) - distTotABCD < bestGain && (distAC + distBD) != distTotABCD) { nextBestMove = a + ";" + b + ";" + c + ";" + d; bestGain = (distAC + distBD) - distTotABCD; } if ((distAC + distBD) - distTotABCD > worstGain && (distAC + distBD) != distTotABCD) { nextWorstMove = a + ";" + b + ";" + c + ";" + d; worstGain = (distAC + distBD) - distTotABCD; } } } } string[] currentElements = nextBestMove.Split(';'); a = int.Parse(currentElements[0]); b = int.Parse(currentElements[1]); c = int.Parse(currentElements[2]); d = int.Parse(currentElements[3]); Utility.SwapRoute(c, b, currentSol); currentSol.path[a] = c; currentSol.path[b] = d; currentSol.cost += bestGain; if (incumbentPath.cost > currentSol.cost) { incumbentPath = (PathGenetic)currentSol.Clone(); } if (bestGain < 0) { typeSol = 0; } else { tabu.AddTabu(a, b, c, d); typeSol = 1; } Utility.PrintHeuristicSolution(instance, process, currentSol, incumbentPath.cost, typeSol); bestGain = double.MaxValue; worstGain = double.MinValue; } while (clock.ElapsedMilliseconds / 1000.0 < instance.TimeLimit); }