public void DefineCompetitivePopulationWorksWithPositiveReplacementRate()
        {
            var informationFlow = new LocalDifferentialEvolutionInformationFlow(
                this._deConfiguration,
                this._parameterTree,
                this._genomeBuilder);

            // Create a competitive population part s.t. two genomes should be replaced using the default replacement
            // rate of 0.25.
            var originalCompetitives = this.CreateRandomGenomes(number: 6).ToList();

            // Define some search points.
            var searchPoints = this.CreateRandomSearchPoints();
            var incumbent    = this._genomeBuilder.CreateRandomGenome(age: 2);

            // Define new competitive population.
            var updatedCompetitives = informationFlow
                                      .DefineCompetitivePopulation(originalCompetitives, incumbent, searchPoints)
                                      .ToList();

            // Check if best points were added to population.
            Assert.True(
                updatedCompetitives.Contains(searchPoints[0].Genome.CreateMutableGenome(), new Genome.GeneValueComparator()),
                "Updated population should contain best search point, but does not.");
            Assert.True(
                updatedCompetitives.Contains(searchPoints[1].Genome.CreateMutableGenome(), new Genome.GeneValueComparator()),
                "Updated population should contain second best search point, but does not.");
            Assert.False(
                updatedCompetitives.Contains(searchPoints[2].Genome.CreateMutableGenome(), new Genome.GeneValueComparator()),
                "Updated population should not contain worst search point.");

            // Then check ages.
            for (int age = 0; age < 3; age++)
            {
                Assert.True(
                    originalCompetitives.Count(individual => individual.Age == age) ==
                    updatedCompetitives.Count(individual => individual.Age == age),
                    $"Different number of genomes with age {age}.");
            }

            Assert.False(
                updatedCompetitives.Any(individual => individual.Age < 0 || individual.Age > 3),
                "There exists a genome with age not in age range!");
        }
        public void DefineCompetitivePopulationThrowsForIncumbentChangeOnlyWithoutIncumbent()
        {
            // Create configuration with a replacement rate of 0.
            this._completeConfiguration = LocalDifferentialEvolutionInformationFlowTest.CreateConfiguration(replacementRate: 0);
            this._deConfiguration       =
                this._completeConfiguration.ExtractDetailedConfiguration <DifferentialEvolutionStrategyConfiguration>(
                    DifferentialEvolutionStrategyArgumentParser.Identifier);
            var informationFlow = new LocalDifferentialEvolutionInformationFlow(
                this._deConfiguration,
                this._parameterTree,
                this._genomeBuilder);

            // Call defineCompetitivePopulation without an incumbent.
            Assert.Throws <ArgumentNullException>(
                () => informationFlow
                .DefineCompetitivePopulation(
                    originalCompetitives: this.CreateRandomGenomes(number: 6).ToList(),
                    originalIncumbent: null,
                    mostRecentSorting: this.CreateRandomSearchPoints())
                .ToList());
        }
        public void DefineCompetitivePopulationWorksForIncumbentChangeOnly()
        {
            // Create configuration with a replacement rate of 0.
            this._completeConfiguration = LocalDifferentialEvolutionInformationFlowTest.CreateConfiguration(replacementRate: 0);
            this._deConfiguration       =
                this._completeConfiguration.ExtractDetailedConfiguration <DifferentialEvolutionStrategyConfiguration>(
                    DifferentialEvolutionStrategyArgumentParser.Identifier);
            var informationFlow = new LocalDifferentialEvolutionInformationFlow(
                this._deConfiguration,
                this._parameterTree,
                this._genomeBuilder);

            // Create a competitive population which contains the same genome twice.
            var originalCompetitives = this.CreateRandomGenomes(number: 6).ToList();
            var incumbent            = originalCompetitives[3];

            originalCompetitives.Add(new Genome(incumbent));
            Assert.Equal(
                2,
                originalCompetitives.Count(individual => new Genome.GeneValueComparator().Equals(individual, incumbent)));

            // Call define competitive population with some search points.
            var searchPoints        = this.CreateRandomSearchPoints();
            var updatedCompetitives = informationFlow.DefineCompetitivePopulation(
                originalCompetitives,
                incumbent,
                searchPoints);

            // Best point should now replace incumbent in competitive population.
            var expectedCompetitives = originalCompetitives
                                       .Select(genome => genome.ToString())
                                       .ToList();

            expectedCompetitives.Remove(incumbent.ToString());
            expectedCompetitives.Add(new Genome(searchPoints[0].Genome.CreateMutableGenome(), incumbent.Age).ToString());
            Assert.Equal(
                expectedCompetitives.OrderBy(x => x).ToArray(),
                updatedCompetitives.Select(genome => genome.ToString()).OrderBy(x => x).ToArray());
        }