public void GetIndexFromPopulation_Is_Weighted_Towards_Zero() { const int populationSize = 100; int[] count = new int[populationSize]; TestChildGenerator childGenerator = new TestChildGenerator(); for (int i = 0; i < 1000000; i++) { count[childGenerator.GetParentIndex(populationSize)]++; } for (int i = 0; i < 100; i++) { Debug.Print("{0} : {1}", i, count[i]); } int subsequentLargerThanPrevious = 0; int previous = count[0]; for (int i = 1; i < 100; i++) { if (count[i] > previous) { ++subsequentLargerThanPrevious; } else { previous = count[i]; } } Debug.Print("Out of Order: {0}", subsequentLargerThanPrevious); Assert.IsTrue(subsequentLargerThanPrevious < 15); }
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()); }
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"); }
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); } }