public void RequestBadNumberOfChildren_ThrowsException(int childrenCount) { var childrenGenerator = new ChildrenGenerator(A.Fake <ICrossoverManager>(), new BasicMutationProbabilityManager(0), new RouletteWheelSelection()); childrenGenerator.GenerateChildren(GetPopulation(1), childrenCount, 0, null); }
public void SelectionStrategyReturnsNull_ThrowsException() { Assertions.AssertThrowAggretateExceptionOfType(() => { var selectionStrategy = A.Fake <ISelectionStrategy>(); A.CallTo(() => selectionStrategy.SelectChromosome()).Returns(null); var childrenGenerator = new ChildrenGenerator(A.Fake <ICrossoverManager>(), new BasicMutationProbabilityManager(0), selectionStrategy); childrenGenerator.GenerateChildren(GetPopulation(1), 1, 0, null); }, typeof(GeneticAlgorithmException)); }
public void BadMutationProbability_ThrowException(double probability) { var mutationManager = A.Fake <IMutationProbabilityManager>(); A.CallTo(() => mutationManager.MutationProbability(A <Population> ._, A <IEnvironment> ._, A <int> ._)) .Returns(probability); var childrenGenerator = new ChildrenGenerator(A.Fake <ICrossoverManager>(), mutationManager, new RouletteWheelSelection()); childrenGenerator.GenerateChildren(GetPopulation(1), 1, 1, null); }
public void RetusnRightNumberOfChromosomes(int childrenCount) { const int count = 1500; var crossoverManager = A.Fake <ICrossoverManager>(); var childrenGenerator = new ChildrenGenerator(crossoverManager, new BasicMutationProbabilityManager(0), new RouletteWheelSelection()); var children = childrenGenerator.GenerateChildren(GetPopulation(count), childrenCount, 0, null); Assert.AreEqual(childrenCount, children.Length, "Didn't get enough children"); foreach (var chromosome in children) { Assert.IsNotNull(chromosome, "No children should be null"); } }
public void TestMutationsHappen(double mutationProbability) { const int populationSize = 1500; int mutationCounter = 0; var population = GetPopulation(populationSize, () => Interlocked.Increment(ref mutationCounter)); var crossoverManager = A.Fake <ICrossoverManager>(); A.CallTo(() => crossoverManager.Crossover(A <IChromosome> ._, A <IChromosome> ._)) .ReturnsLazily((IChromosome c1, IChromosome c2) => c1); var childrenGenerator = new ChildrenGenerator(crossoverManager, new BasicMutationProbabilityManager(mutationProbability), new RouletteWheelSelection()); childrenGenerator.GenerateChildren(population, populationSize, 0, null); const double errorMargin = 0.05; Assert.IsTrue(mutationCounter < populationSize * mutationProbability + errorMargin * populationSize, $"Too few mutations ({mutationCounter})"); Assert.IsTrue(mutationCounter > populationSize * mutationProbability - errorMargin * populationSize, $"Too many mutations ({mutationCounter})"); }