Ejemplo n.º 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());
        }
Ejemplo n.º 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");
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
            }
        }