Ejemplo n.º 1
0
        public override List <Tour> Solve(List <Tour> tours)
        {
            int         numberOfTours      = tours.Count;
            List <Tour> bestTours          = tours;
            double      totalCurrentEnergy = Common.Algos.WeightedReindeerWeariness.Calculate(bestTours);

            while (Temperature > 1)
            {
                var maxIndex      = numberOfTours - 1;
                int randomNumber1 = Random.Next(0, maxIndex);
                int randomNumber2 = Random.Next(0, maxIndex);

                Tour   first         = tours[randomNumber1];
                Tour   second        = tours[randomNumber2];
                double firstWRW      = Common.Algos.WeightedReindeerWeariness.Calculate(first);
                double secondWRW     = Common.Algos.WeightedReindeerWeariness.Calculate(second);
                double currentEnergy = firstWRW + secondWRW;

                List <Tour> changedTours = RouteImprovement.Swap(tours[randomNumber1], tours[randomNumber2]) as List <Tour>;

                first     = changedTours[0];
                second    = changedTours[1];
                firstWRW  = Common.Algos.WeightedReindeerWeariness.Calculate(first);
                secondWRW = Common.Algos.WeightedReindeerWeariness.Calculate(second);
                double neighbourEnergy = firstWRW + secondWRW;

                if (AcceptanceProbability(currentEnergy, neighbourEnergy) > Random.Next(0, 1))
                {
                    tours[randomNumber1] = changedTours[0];
                    tours[randomNumber2] = changedTours[1];
                }

                var currentNewEnergy = Common.Algos.WeightedReindeerWeariness.Calculate(tours);
                Console.WriteLine("{0}, {1}", totalCurrentEnergy, currentNewEnergy);
                if (totalCurrentEnergy > currentNewEnergy)
                {
                    bestTours          = tours;
                    totalCurrentEnergy = currentNewEnergy;
                }

                Temperature *= 1 - CoolingRate;
            }

            return(bestTours);
        }
        public override List <Tour> Solve(List <Tour> tours)
        {
            var    bestTours     = tours.Clone().ToList();
            double currentEnergy = Common.Algos.WeightedReindeerWeariness.Calculate(bestTours);

            while (Temperature > 1)
            {
                tours         = bestTours.Clone().ToList();
                currentEnergy = Common.Algos.WeightedReindeerWeariness.Calculate(bestTours);
                //TODO RouteImprovement with group of tours
                //List<Tour> changedTours = RouteImprovement.Swap(tours) as List<Tour>;

                Random rnd           = new Random();
                int    sdjdkflskdlfj = rnd.Next(0, tours.Count - 1);
                Tour   tour1         = tours[sdjdkflskdlfj];
                Tour   tour2         = new Tour();
                double distance      = Double.MaxValue;
                for (int i = 0; i < tours.Count; i++)
                {
                    if (i != sdjdkflskdlfj)
                    {
                        double newDistance = tour1.GetMiddlePointOfTour().DistanceTo(tours[i].GetMiddlePointOfTour());
                        if (newDistance < distance)
                        {
                            distance = newDistance;
                            tour2    = tours[i];
                        }
                    }
                }

                /*tours.Remove(tour1);
                *  tours.Remove(tour2);*/

                List <Tour> changedTours = new List <Tour>(tours);
                changedTours.Remove(tour1);
                changedTours.Remove(tour2);
                List <Tour> b = RouteImprovement.Swap(tour1, tour2) as List <Tour>;
                changedTours.AddRange(b);

                double neighbourEnergy = Common.Algos.WeightedReindeerWeariness.Calculate(changedTours);

                if (AcceptanceProbability(currentEnergy, neighbourEnergy) > Random.Next(0, 1))
                {
                    tours = changedTours;
                }

                var currentNewEnergy = Common.Algos.WeightedReindeerWeariness.Calculate(changedTours);
                Console.WriteLine("{0}, {1}", currentEnergy, currentNewEnergy);
                if (currentEnergy > currentNewEnergy)
                {
                    bestTours = changedTours.Clone().ToList();
                }

                //Writer writer = new Writer();
                //writer.WriteSolution(AreaPath, "", bestTours);

                Temperature *= 1 - CoolingRate;
            }

            return(bestTours);
        }