예제 #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);
            }
        }
예제 #5
0
        public void Can_Run_Robots_Through_Genetic_Processor()
        {
            RobotGeneticProblem robotGeneticProblem = new RobotGeneticProblem();
            // IChildGenerator<Robot> childGenerator = new GenerateRobotsByPairing();
            IChildGenerator <Robot>           childGenerator = new GenerateRobotsByMutation();
            GeneticAlgorithmProcessor <Robot> processor      = new GeneticAlgorithmProcessor <Robot>(robotGeneticProblem, childGenerator);

            const int populationSize = 300;
            var       population     = processor.GetInitialPopulation(populationSize).ToArray();
            var       initialFitness = processor.CalculatePopulationFitness(population);

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

            for (int i = 0; i < 500; i++)
            {
                population = processor.GetNextPopulation(population).ToArray();
                var nextFitness = processor.CalculatePopulationFitness(population);
                var maxFitness  = population.Max(x => robotGeneticProblem.GetFitness(x));

                Debug.Print("Iteration {0}: avg = {1}, max = {2}", i, nextFitness, maxFitness);
            }
        }