public void Processor_Can_Generate_Next_Generation() { 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 nextGeneration = processor.GetNextPopulation(initialPopulation); Assert.AreEqual(populationSize, nextGeneration.Count()); }
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); }
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); } }
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); } }