예제 #1
0
        static void Main(string[] args)
        {
            FillMileage();
            int[] cityList = ReadTour(args[0]).ToArray();
            if (cityList.Length != total)
            {
                TourData            tourInfo  = new TourData(ReadTour(args[0]).ToArray(), mileage);
                Task <TourResult>[] searchers = new Task <TourResult> [Environment.ProcessorCount / 2];
                TaskFactory         tf        = new TaskFactory();
                for (int i = 0; i < searchers.Length; ++i)
                {
                    searchers[i] = Task.Factory.StartNew <TourResult>(Search, ((ICloneable)tourInfo).Clone());
                }
                Task.WaitAll(searchers);
                TourResult[] results = new TourResult[searchers.Length];
                for (int i = 0; i < searchers.Length; ++i)
                {
                    results[i] = searchers[i].Result;
                }
                Array.Sort(results);
                Console.WriteLine(results[0].toString(cities));
            }
            else
            {
                AntColony ac = new AntColony(total, mileage);

                for (int currIteration = 0; currIteration < 10000; ++currIteration)
                {
                    if (ac.simulateAnts() == 0)
                    {
                        ac.updateTrail();
                        if (currIteration != 10000)
                        {
                            ac.Restart();
                        }
                    }
                }

                Console.WriteLine(ac.best);

                foreach (var result in ac.ResultTour())
                {
                    foreach (var city in cities)
                    {
                        if (result == city.Value)
                        {
                            Console.WriteLine(city.Key);
                        }
                    }
                }
            }
            Console.ReadLine();
        }
예제 #2
0
파일: Program.cs 프로젝트: ankel/AI-TSP
        static void Main(string[] args)
        {
            FillMileage();
            int[] cityList = ReadTour(args[0]).ToArray();
            if (cityList.Length != total)
            {
                TourData tourInfo = new TourData(ReadTour(args[0]).ToArray(), mileage);
                Task<TourResult>[] searchers = new Task<TourResult>[Environment.ProcessorCount / 2];
                TaskFactory tf = new TaskFactory();
                for (int i = 0; i < searchers.Length; ++i)
                {
                    searchers[i] = Task.Factory.StartNew<TourResult>(Search, ((ICloneable)tourInfo).Clone());
                }
                Task.WaitAll(searchers);
                TourResult[] results = new TourResult[searchers.Length];
                for (int i = 0; i < searchers.Length; ++i)
                {
                    results[i] = searchers[i].Result;
                }
                Array.Sort(results);
                Console.WriteLine(results[0].toString(cities));
            }
            else
            {

                AntColony ac = new AntColony(total, mileage);

                for (int currIteration = 0; currIteration < 10000; ++currIteration)
                {
                    if (ac.simulateAnts() == 0)
                    {
                        ac.updateTrail();
                        if (currIteration != 10000)
                        {
                            ac.Restart();
                        }
                    }
                }

                Console.WriteLine(ac.best);

                foreach (var result in ac.ResultTour())
                {
                    foreach (var city in cities)
                    {
                        if (result == city.Value)
                        {
                            Console.WriteLine(city.Key);
                        }
                    }
                }
            }
            Console.ReadLine();
        }