Ejemplo n.º 1
0
        public Solution Run()
        {
            var stopwatch           = Stopwatch.StartNew();
            var totalSecondsElapsed = 0.0;
            var currentSolution     = new HSolver(_dataset).Run();
            var temperature         = Config.GetSimulatedAnnealingParam().InitialTemperature;
            var alpha                      = Config.GetSimulatedAnnealingParam().Alpha;
            var iterationCount             = Config.GetSimulatedAnnealingParam().IterationCount;
            var diversifyForEachNIteration = Config.GetDiversificationParam().DiversifyForEachNIteration;
            var minCustomersToRemove       = Config.GetDiversificationParam().MinCustomersToRemove;
            var maxCustomersToRemove       = Config.GetDiversificationParam().MaxCustomersToRemove;

            _bestSolution = currentSolution;

            for (var i = 0; i <= iterationCount; i++)
            {
                totalSecondsElapsed += stopwatch.Elapsed.Milliseconds;

                if (i % diversifyForEachNIteration == 0)
                {
                    currentSolution = new Diversifier(currentSolution, minCustomersToRemove, maxCustomersToRemove).Diverisfy();
                }

                var solutionPool = new List <Solution>();
                solutionPool.AddRange(new TwoOptOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new ExchangeOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new RelocateOperator(currentSolution).GenerateFeasibleSolutions());
                var candidateSolution = Helpers.GetBestNeighbour(solutionPool);

                Console.WriteLine("Iteration: {0}, Time Elapsed: {1} sn, Temp: {2}, {3} candidate, Current Cost: {4}, Best Cost {5}",
                                  i,
                                  totalSecondsElapsed / 1_000.0,
                                  Math.Round(temperature, 3),
                                  solutionPool.Count,
                                  Math.Round(currentSolution.Cost, 3),
                                  Math.Round(_bestSolution.Cost, 3)
                                  );

                var currentCost   = currentSolution.Cost;
                var candidateCost = candidateSolution.Cost;
                //var rand = new Random().NextDouble();
                //var threshold = GetThreshold(currentCost, candidateCost, temperature);
                var acceptanceCondition = candidateCost < currentCost; //||
                //(candidateCost >= currentCost && rand <= threshold);

                if (acceptanceCondition)
                {
                    currentSolution = Helpers.Clone(candidateSolution);

                    if (currentSolution.Cost < _bestSolution.Cost)
                    {
                        _bestSolution = currentSolution;
                    }
                }

                temperature *= alpha;
            }

            stopwatch.Stop();

            return(_bestSolution);
        }
Ejemplo n.º 2
0
        public Solution Run()
        {
            var stopwatch                        = Stopwatch.StartNew();
            var totalSecondsElapsed              = 0.0;
            var currentSolution                  = new LocalSearch(_dataset).Run();
            var heuristicParams                  = Config.GetHeuristicsParam();
            var iterationCount                   = heuristicParams.IterationCount;
            var tabuListSize                     = heuristicParams.TabuSearchParam.TabuListSize;
            var numberOfNonImprovingIters        = heuristicParams.DiversificationParam.NumberOfNonImprovingIters;
            var minCustomersToRemove             = heuristicParams.DiversificationParam.MinCustomersToRemove;
            var maxCustomersToRemove             = heuristicParams.DiversificationParam.MaxCustomersToRemove;
            var numberOfNonImprovingItersCounter = 0;
            var tabuList = new Helpers.FixedSizedQueue <string>(tabuListSize);

            _bestSolution   = currentSolution;
            currentSolution = new Diversifier(Helpers.Clone(_bestSolution), minCustomersToRemove, maxCustomersToRemove).Diverisfy();
            tabuList.Enqueue(Helpers.GetStringFormOfSolution(currentSolution));

            for (var i = 0; i <= iterationCount; i++)
            {
                totalSecondsElapsed += stopwatch.Elapsed.Milliseconds;

                if (numberOfNonImprovingItersCounter == numberOfNonImprovingIters)
                {
                    currentSolution = new Diversifier(Helpers.Clone(_bestSolution), minCustomersToRemove, maxCustomersToRemove).Diverisfy();
                    numberOfNonImprovingItersCounter = 0;
                }

                var solutionPool = new List <Solution>();
                solutionPool.AddRange(new CrossOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new TwoOptOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new ExchangeOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new RelocateOperator(currentSolution).GenerateFeasibleSolutions());
                Solution candidateSolution = new Solution();
                solutionPool = solutionPool.OrderBy(s => s.Cost).ToList();

                for (var s = 0; s < solutionPool.Count; s++)
                {
                    if (!tabuList.Contains(Helpers.GetStringFormOfSolution(solutionPool[s])))
                    {
                        candidateSolution = solutionPool[s];
                        break;
                    }
                }

                Console.WriteLine("Iteration: {0}, Time Elapsed: {1} sn, {2} candidate, Current Cost: {3}, Best Cost {4}",
                                  i,
                                  totalSecondsElapsed / 1_000.0,
                                  solutionPool.Count,
                                  Math.Round(currentSolution.Cost, 3),
                                  Math.Round(_bestSolution.Cost, 3)
                                  );

                var currentCost         = currentSolution.Cost;
                var candidateCost       = candidateSolution.Cost;
                var acceptanceCondition = candidateCost < currentCost;

                if (acceptanceCondition)
                {
                    currentSolution = Helpers.Clone(candidateSolution);

                    if (currentSolution.Cost < _bestSolution.Cost)
                    {
                        _bestSolution = currentSolution;
                    }
                }
                else
                {
                    numberOfNonImprovingItersCounter++;
                }

                tabuList.Enqueue(Helpers.GetStringFormOfSolution(candidateSolution));
            }

            stopwatch.Stop();

            return(_bestSolution);
        }
Ejemplo n.º 3
0
        public Solution Run()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Restart();
            var totalSecondsElapsed              = 0.0;
            var currentSolution                  = new LocalSearch(_dataset).Run();
            var heuristicParams                  = Config.GetHeuristicsParam();
            var iterationCount                   = heuristicParams.IterationCount;
            var numberOfNonImprovingIters        = heuristicParams.DiversificationParam.NumberOfNonImprovingIters;
            var minCustomersToRemove             = heuristicParams.DiversificationParam.MinCustomersToRemove;
            var maxCustomersToRemove             = heuristicParams.DiversificationParam.MaxCustomersToRemove;
            var numberOfNonImprovingItersCounter = 0;

            _bestSolution   = currentSolution;
            currentSolution = new Diversifier(Helpers.Clone(_bestSolution), minCustomersToRemove, maxCustomersToRemove).Diverisfy();

            totalSecondsElapsed += TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
            Console.WriteLine("Local search completed in {0} seconds!", Math.Round(totalSecondsElapsed, 3));

            for (var i = 0; i <= iterationCount; i++)
            {
                stopwatch.Restart();

                if (numberOfNonImprovingItersCounter == numberOfNonImprovingIters)
                {
                    currentSolution = new Diversifier(Helpers.Clone(_bestSolution), minCustomersToRemove, maxCustomersToRemove).Diverisfy();
                    numberOfNonImprovingItersCounter = 0;
                }

                var solutionPool = new List <Solution>();
                solutionPool.AddRange(new CrossOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new TwoOptOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new ExchangeOperator(currentSolution).GenerateFeasibleSolutions());
                solutionPool.AddRange(new RelocateOperator(currentSolution).GenerateFeasibleSolutions());
                var candidateSolution   = Helpers.GetBestNeighbour(solutionPool);
                var currentCost         = currentSolution.Cost;
                var candidateCost       = candidateSolution.Cost;
                var acceptanceCondition = candidateCost < currentCost;

                if (acceptanceCondition)
                {
                    currentSolution = Helpers.Clone(candidateSolution);

                    if (currentSolution.Cost < _bestSolution.Cost)
                    {
                        _bestSolution = currentSolution;
                    }
                }
                else
                {
                    numberOfNonImprovingItersCounter++;
                }

                totalSecondsElapsed += TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;

                Console.WriteLine("Iteration: {0}, Time Elapsed: {1} sn, {2} candidate, Current Cost: {3}, Best Cost {4}",
                                  i,
                                  Math.Round(totalSecondsElapsed, 3),
                                  solutionPool.Count,
                                  Math.Round(currentSolution.Cost, 3),
                                  Math.Round(_bestSolution.Cost, 3)
                                  );
            }

            stopwatch.Stop();

            return(_bestSolution);
        }