public static TabuAlgorithmResult GenerateResult(int size) { var init = new TabuAlgorithmResult() { Path = Enumerable.Range(0, size).ToList() }; init.Shuffle(); return(init); }
public void Invoke() { var init = TabuAlgorithmResult.GenerateResult(_graph.NumberOfCities); init.CalculateWeight(_graph); var result = TabuSearch(init); Result = new AlgorithmResult { Path = result.Path, Weight = result.Weight }; }
public TabuAlgorithmResult TabuSearch(TabuAlgorithmResult initialSolution) { bestSolution = initialSolution; TabuAlgorithmResult currentSolution = initialSolution; _tabuList = new Queue <Move>(); int critCounter = 0; int currentIteration = 0; while (!_stopCondition.mustStop(++currentIteration)) { //algorytm najblizszego sasiada TabuAlgorithmResult bestNeighborFound = FindBestNeighbor(currentSolution, _tabuList.ToList(), out var move); if (bestNeighborFound.Weight < bestSolution.Weight) { bestSolution = bestNeighborFound; critCounter = 0; } else { //dywersyfikacja critCounter++; if (critCounter == _maxCritCounter && _maxCritCounter != 0) { currentSolution = TabuAlgorithmResult.GenerateResult(_graph.NumberOfCities); critCounter = 0; } } //dodawanie ostatniego ruchu _tabuList.Enqueue(move); currentSolution = bestNeighborFound; //max tabu size if (_tabuList.Count > _maxTabuSize) { _tabuList.Dequeue(); } } return(bestSolution); }
public TabuAlgorithmResult FindBestNeighbor(TabuAlgorithmResult currentSolution, List <Move> tabuMoves, out Move move) { int bestCost = Int32.MaxValue; move = new Move(0, 0); TabuAlgorithmResult bestResult = new TabuAlgorithmResult(); for (int i = 0; i < currentSolution.Path.Count; i++) { for (int j = 1; j < currentSolution.Path.Count; j++) { var pathList = new List <int>(currentSolution.Path); if (j != i) { var currMove = new Move(i, j); TabuAlgorithmResult temp = new TabuAlgorithmResult(); temp.Path = Swap(i, j, pathList); int currCost = temp.CalculateWeight(_graph); if (!tabuMoves.Contains(currMove)) { if (currCost < bestCost || currCost < bestSolution.Weight) { bestCost = currCost; move = new Move(currMove); bestResult = new TabuAlgorithmResult(temp); } } } } } return(bestResult); }
public TabuAlgorithmResult(TabuAlgorithmResult alg) { Path = new List <int>(alg.Path); Weight = alg.Weight; }