Example #1
0
        public static void SolveSalesmanProblem(string ProblemName, int N)
        {
            string Path  = "/Users/admin/Desktop/Travelling Salesman Problem/" + ProblemName + ".txt";
            var    stops = Enumerable.Range(1, N)
                           .Select(i => new Stop(new City(i, Path)))
                           .NearestNeighbors()
                           .ToList();

            foreach (var stop in stops)
            {
                Console.WriteLine(stop.City.CityName + " " + stop.City.X + " " + stop.City.Y);
            }
            //create next pointers between them
            stops.Connect(true);

            //wrap in a tour object
            Tour startingTour = new Tour(stops);

            //the actual algorithm
            while (true)
            {
                Console.WriteLine(startingTour);
                var newTour = startingTour.GenerateMutations()
                              .MinBy(tour => tour.Cost());
                if (newTour.Cost() < startingTour.Cost())
                {
                    startingTour = newTour;
                }
                else
                {
                    break;
                }
            }
        }
        static void Main(string[] args)
        {
            //create an initial tour out of nearest neighbors
            var stops = Enumerable.Range(1, 10)
                        .Select(i => new Stop(new City(i)))
                        .NearestNeighbors()
                        .ToList();

            //create next pointers between them
            stops.Connect(true);

            //wrap in a tour object
            Tour startingTour = new Tour(stops);

            //the actual algorithm
            while (true)
            {
                Console.WriteLine(startingTour);
                var newTour = startingTour.GenerateMutations()
                              .MinBy(tour => tour.Cost());
                if (newTour.Cost() < startingTour.Cost())
                {
                    startingTour = newTour;
                }
                else
                {
                    break;
                }
            }

            Console.ReadLine();
        }