コード例 #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
        static TourResult Search(object Data)
        {
            Debug.Assert(Data is TourData);
            TourData   data     = (TourData)Data;
            DateTime   deadline = DateTime.Now.AddSeconds(RUNTIME_MAX);
            TourResult result   = new TourResult(new int[data.CitiesInTour.Length], Int32.MaxValue);

            do
            {
                Shuffle(data.CitiesInTour);
                while (true)
                {
                    int swapScore = Score(data.CitiesInTour);
                    int swapI     = -1;
                    for (int i = 2; i < data.CitiesInTour.Length - 1; ++i)
                    {
                        swap(data.CitiesInTour, i - 1, i);
                        if (Score(data.CitiesInTour) < swapScore)
                        {
                            swapScore = Score(data.CitiesInTour);
                            swapI     = i;
                        }
                        swap(data.CitiesInTour, i - 1, i);
                    }

                    Debug.Assert(swapI > 1 && swapI < data.CitiesInTour.Length - 1 || swapI == -1, swapI.ToString());
                    if (swapI != -1)
                    {
                        swap(data.CitiesInTour, swapI, swapI - 1);
                    }
                    else        // local maximum reached
                    {
                        int n = Score(data.CitiesInTour);
                        if (n < result.Score)
                        {
                            Array.Copy(data.CitiesInTour, result.TourRoute, data.CitiesInTour.Length);
                            result.Score = n;
                        }
                        break;
                    }
                }
            } while (DateTime.Now <= deadline);
            return(result);
        }
コード例 #3
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();
        }