コード例 #1
0
 public void test_getFiniteAlphabet()
 {
     for (int i = 2; i <= 40; ++i)
     {
         ICollection <int> fab = NQueensGenAlgoUtil.getFiniteAlphabetForBoardOfSize(i);
         Assert.AreEqual(i, fab.Size());
     }
 }
コード例 #2
0
 public void test_generateRandomIndividual()
 {
     for (int i = 2; i <= 40; ++i)
     {
         Individual <int> individual = NQueensGenAlgoUtil.generateRandomIndividual(i);
         Assert.AreEqual(i, individual.length());
     }
 }
コード例 #3
0
        static void nQueensGeneticAlgorithmSearch()
        {
            System.Console.WriteLine("\nNQueensDemo GeneticAlgorithm  -->");
            try
            {
                IFitnessFunction <int>       fitnessFunction = NQueensGenAlgoUtil.getFitnessFunction();
                GoalTest <Individual <int> > goalTest        = NQueensGenAlgoUtil.getGoalTest();
                // Generate an initial population
                ISet <Individual <int> > population = CollectionFactory.CreateSet <Individual <int> >();
                for (int i = 0; i < 50; ++i)
                {
                    population.Add(NQueensGenAlgoUtil.generateRandomIndividual(boardSize));
                }

                GeneticAlgorithm <int> ga = new GeneticAlgorithm <int>(boardSize,
                                                                       NQueensGenAlgoUtil.getFiniteAlphabetForBoardOfSize(boardSize), 0.15);

                // Run for a set amount of time
                Individual <int> bestIndividual = ga.geneticAlgorithm(population, fitnessFunction, goalTest, 1000L);

                System.Console.WriteLine("Max Time (1 second) Best Individual=\n"
                                         + NQueensGenAlgoUtil.getBoardForIndividual(bestIndividual));
                System.Console.WriteLine("Board Size      = " + boardSize);
                //System.Console.WriteLine("# Board Layouts = " + (new BigDecimal(boardSize)).pow(boardSize)/*);*/
                System.Console.WriteLine("Fitness         = " + fitnessFunction.apply(bestIndividual));
                System.Console.WriteLine("Is Goal         = " + goalTest(bestIndividual));
                System.Console.WriteLine("Population Size = " + ga.getPopulationSize());
                System.Console.WriteLine("Iterations      = " + ga.getIterations());
                System.Console.WriteLine("Took            = " + ga.getTimeInMilliseconds() + "ms.");

                // Run till goal is achieved
                bestIndividual = ga.geneticAlgorithm(population, fitnessFunction, goalTest, 0L);

                System.Console.WriteLine("");
                System.Console
                .WriteLine("Goal Test Best Individual=\n" + NQueensGenAlgoUtil.getBoardForIndividual(bestIndividual));
                System.Console.WriteLine("Board Size      = " + boardSize);
                //System.Console.WriteLine("# Board Layouts = " + (new BigDecimal(boardSize)).pow(boardSize)/*);*/
                System.Console.WriteLine("Fitness         = " + fitnessFunction.apply(bestIndividual));
                System.Console.WriteLine("Is Goal         = " + goalTest(bestIndividual));
                System.Console.WriteLine("Population Size = " + ga.getPopulationSize());
                System.Console.WriteLine("Itertions       = " + ga.getIterations());
                System.Console.WriteLine("Took            = " + ga.getTimeInMilliseconds() + "ms.");
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #4
0
        public void test_getBoardForIndividual()
        {
            NQueensBoard board = NQueensGenAlgoUtil
                                 .getBoardForIndividual(new Individual <int>(CollectionFactory.CreateQueue <int>(new[] { 5, 6, 1, 3, 6, 4, 7, 7 })));

            Assert.AreEqual(" -  -  -  -  -  -  -  - \n"
                            + " -  -  Q  -  -  -  -  - \n" + " -  -  -  -  -  -  -  - \n"
                            + " -  -  -  Q  -  -  -  - \n" + " -  -  -  -  -  Q  -  - \n"
                            + " Q  -  -  -  -  -  -  - \n" + " -  Q  -  -  Q  -  -  - \n"
                            + " -  -  -  -  -  -  Q  Q \n", board.getBoardPic());

            Assert.AreEqual("--------\n" + "--Q-----\n" + "--------\n"
                            + "---Q----\n" + "-----Q--\n" + "Q-------\n" + "-Q--Q---\n"
                            + "------QQ\n", board.ToString());
        }
コード例 #5
0
 public void setUp()
 {
     fitnessFunction = NQueensGenAlgoUtil.getFitnessFunction();
     goalTest        = NQueensGenAlgoUtil.getGoalTest();
 }