コード例 #1
0
ファイル: TSPTest.cs プロジェクト: ExCodeCowboy/AI.Core
        public void TestGenericAnnealer()
        {
            Debug.WriteLine("Loading cities data for SBC Traveling Salesman Problem analysis");
            CitiesData citiesData = new CitiesData(10);
            Debug.WriteLine(citiesData.ToString());
            Debug.WriteLine("Number of cities = " + citiesData.cities.Length);
            Debug.WriteLine("Number of possible paths = " +
              citiesData.NumberOfPossiblePaths().ToString("#,###"));
            Debug.WriteLine("Best possible solution (shortest path) length = " +
              citiesData.ShortestPathLength().ToString("F4"));


            var problemDef = new CitiesProblemDef(citiesData);

            Annealer<Char[]>.random = new Random(0);
            CitiesProblemDef._random = Annealer<Char[]>.random;

            var hive = new Annealer<Char[]>(problemDef.GenerateRandomMemoryMatrix,
                                 problemDef.GenerateNeighborMemoryMatrix,
                                 problemDef.MeasureOfQuality,
                                 1000000,0.999);

            Debug.WriteLine("\nInitial annealer");
            
            Char[] bestSolution = hive.Solve();

            Debug.WriteLine("\nFinal Solution");
            Debug.WriteLine(new string(bestSolution));
            Debug.WriteLine(hive);
            Debug.WriteLine("End annealer demo");

            Assert.AreEqual(citiesData.cities, bestSolution);
        }
コード例 #2
0
ファイル: TSPTest.cs プロジェクト: ExCodeCowboy/AI.Core
        public void TestGenericHive()
        {
            Debug.WriteLine("Loading cities data for SBC Traveling Salesman Problem analysis");
            CitiesData citiesData = new CitiesData(20);
            Debug.WriteLine(citiesData.ToString());
            Debug.WriteLine("Number of cities = " + citiesData.cities.Length);
            Debug.WriteLine("Number of possible paths = " +
              citiesData.NumberOfPossiblePaths().ToString("#,###"));
            Debug.WriteLine("Best possible solution (shortest path) length = " +
              citiesData.ShortestPathLength().ToString("F4"));


            var problemDef = new CitiesProblemDef(citiesData);

            int totalNumberBees = 100;
            int numberInactive = 20;
            int numberScout = 30;
            int maxNumberVisits = 100;
            int maxNumberCycles = 8000;

            GenericHive<Char[]>.random = new Random(0);
            CitiesProblemDef._random = GenericHive<Char[]>.random;

            var hive = new GenericHive<Char[]>(totalNumberBees,
                                 numberInactive,
                                 numberScout,
                                 maxNumberVisits,
                                 maxNumberCycles,
                                 problemDef.GenerateRandomMemoryMatrix,
                                 problemDef.GenerateNeighborMemoryMatrix,
                                 problemDef.MeasureOfQuality);

            Debug.WriteLine("\nInitial random hive");
            Debug.WriteLine(hive);
            Debug.WriteLine(new string(hive.BestSolution));

            bool doProgressBar = true;
            hive.Solve(doProgressBar);
            
            Debug.WriteLine("\nFinal hive");
            Debug.WriteLine(new string(hive.BestSolution));
            Debug.WriteLine(hive);
            Debug.WriteLine("End Simulated Bee Colony demo");

            Assert.AreEqual(citiesData.cities,hive.BestSolution);
        }