Example #1
0
        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
            };
        }
Example #2
0
        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);
        }