コード例 #1
0
        public void Processor_Can_Generate_Initial_Population()
        {
            IGeneticProblem <int>           problem        = new TestProblem();
            IChildGenerator <int>           childGenerator = new TestChildGenerator();
            GeneticAlgorithmProcessor <int> processor      = new GeneticAlgorithmProcessor <int>(problem, childGenerator);
            int populationSize    = 100;
            var initialPopulation = processor.GetInitialPopulation(populationSize);

            Assert.AreEqual(populationSize, initialPopulation.Count());
        }
コード例 #2
0
        public void Processor_Can_Calculate_Population_Fitness()
        {
            IGeneticProblem <int>           problem        = new TestProblem();
            IChildGenerator <int>           childGenerator = new TestChildGenerator();
            GeneticAlgorithmProcessor <int> processor      = new GeneticAlgorithmProcessor <int>(problem, childGenerator);
            int populationSize    = 100;
            var initialPopulation = processor.GetInitialPopulation(populationSize);
            var populationFitness = processor.CalculatePopulationFitness(initialPopulation);

            Assert.IsTrue(populationFitness != 0, "Shouldn't happen, or at least, won't normally happen");
        }
コード例 #3
0
        public void Processor_Can_Improve_On_Each_Generation()
        {
            IGeneticProblem <int>           problem        = new TestProblem();
            IChildGenerator <int>           childGenerator = new TestChildGenerator();
            GeneticAlgorithmProcessor <int> processor      = new GeneticAlgorithmProcessor <int>(problem, childGenerator);
            const int populationSize    = 100;
            var       initialPopulation = processor.GetInitialPopulation(populationSize).ToArray();
            var       initialFitness    = processor.CalculatePopulationFitness(initialPopulation);

            var nextPopulation = processor.GetNextPopulation(initialPopulation);
            var nextFitness    = processor.CalculatePopulationFitness(nextPopulation);

            Assert.IsTrue(nextFitness < initialFitness, "Fitness moved from {0} to {1}", initialFitness, nextFitness);
        }
コード例 #4
0
        public void Processor_Can_Converge_Towards_A_Solution()
        {
            IGeneticProblem <int>           problem        = new TestProblem();
            IChildGenerator <int>           childGenerator = new TestChildGenerator();
            GeneticAlgorithmProcessor <int> processor      = new GeneticAlgorithmProcessor <int>(problem, childGenerator);

            const int populationSize = 10000;

            int[] population     = processor.GetInitialPopulation(populationSize).ToArray();
            var   initialFitness = processor.CalculatePopulationFitness(population);

            Debug.Print("Initial Fitness: {0}", initialFitness);

            for (int i = 0; i < 200; i++)
            {
                population = processor.GetNextPopulation(population).ToArray();
                var nextFitness = processor.CalculatePopulationFitness(population);
                Debug.Print("Iteration {0}: {1}", i, nextFitness);
                Assert.IsTrue(nextFitness < initialFitness);
            }
        }