public void CorrectNumberOfIndividualsGetReplaced() { int populationSize = 6; double replacementPercentage = 0.3; // Non competitive part of the population consists of 3 individuals. // 30 % of them correspond to one individual. int expectedNumberOfReplacedIndividuals = 1; // Build up non-competitive population. this._population = PopulationTest.CreatePopulation(populationSize, maxAge: 3, populationMutantRatio: replacementPercentage); var genome1 = new Genome(1); var genome2 = new Genome(2); var genome3 = new Genome(3); this._population.AddGenome(genome1, isCompetitive: false); this._population.AddGenome(genome2, isCompetitive: false); this._population.AddGenome(genome3, isCompetitive: false); // Check the population and replace individuals. var originalNonCompetitive = new List <Genome> { genome1, genome2, genome3 }; Assert.True(originalNonCompetitive.SequenceEqual(this._population.GetNonCompetitiveMates(), Genome.GenomeComparer)); this._population.ReplaceIndividualsWithMutants(this._genomeBuilder); // Check number of changed individuals. var difference = originalNonCompetitive.Except(this._population.GetNonCompetitiveMates()); Assert.True( expectedNumberOfReplacedIndividuals == difference.Count(), $"There should be {expectedNumberOfReplacedIndividuals} items changed between {string.Join(";", originalNonCompetitive.Select(g => g.ToString()))} and {string.Join(",", this._population.GetNonCompetitiveMates().Select(g => g.ToString()))}."); }
/// <summary> /// Initializes a new instance of the <see cref="PopulationTest"/> class. /// </summary> public PopulationTest() { Randomizer.Reset(); Randomizer.Configure(42); int populationSize = 6; this._population = PopulationTest.CreatePopulation(populationSize, this._maxAge, this._populationMutantRatio); }
public void AgeDistributionDoesNotChangeOnReplacement() { // Initialize a large population. int populationSize = 50; this._population = PopulationTest.CreatePopulation(populationSize, maxAge: 3, populationMutantRatio: this._populationMutantRatio); // Insert individuals with different ages into non competitive population. int numAgedZero = 6; int numAgedOne = 4; int numAgedTwo = 3; int numAgedThree = 12; for (int i = 0; i < numAgedZero; i++) { this._population.AddGenome(new Genome(), isCompetitive: false); } for (int i = 0; i < numAgedOne; i++) { this._population.AddGenome(new Genome(1), isCompetitive: false); } for (int i = 0; i < numAgedTwo; i++) { this._population.AddGenome(new Genome(2), isCompetitive: false); } for (int i = 0; i < numAgedThree; i++) { this._population.AddGenome(new Genome(3), isCompetitive: false); } // Check ages were inserted correctly. var nonCompetitive = this._population.GetNonCompetitiveMates(); Assert.Equal(numAgedZero, nonCompetitive.Where(genome => genome.Age == 0).Count()); Assert.Equal(numAgedOne, nonCompetitive.Where(genome => genome.Age == 1).Count()); Assert.Equal(numAgedTwo, nonCompetitive.Where(genome => genome.Age == 2).Count()); Assert.Equal(numAgedThree, nonCompetitive.Where(genome => genome.Age == 3).Count()); // Replace some individuals with new random individuals. this._population.ReplaceIndividualsWithMutants(this._genomeBuilder); nonCompetitive = this._population.GetNonCompetitiveMates(); // Check age distribution is still the same. Assert.Equal( numAgedZero, nonCompetitive.Where(genome => genome.Age == 0).Count()); Assert.Equal( numAgedOne, nonCompetitive.Where(genome => genome.Age == 1).Count()); Assert.Equal( numAgedTwo, nonCompetitive.Where(genome => genome.Age == 2).Count()); Assert.Equal( numAgedThree, nonCompetitive.Where(genome => genome.Age == 3).Count()); }