Beispiel #1
0
        /// <summary>
        /// Checks that for each <see cref="Genome"/> in <see cref="Genomes"/>, two convertings using
        /// <see cref="GenomeTransformation"/> result in the <see cref="Genome"/> one started with.
        /// </summary>
        private void ValidateGenomeConversion()
        {
            var comparer = new Genome.GeneValueComparator();

            foreach (var genome in this.Genomes)
            {
                var convertedGenome = this.GenomeTransformation.ConvertGenomeToArray(genome);
                var restoredGenome  = this.GenomeTransformation.ConvertBack(convertedGenome);

                Assert.True(comparer.Equals(genome, restoredGenome));
            }
        }
        public void FinishPhaseReplacesCompetitiveGenomes()
        {
            var originalPopulation = this.CreatePopulation();
            var incumbent          = new IncumbentGenomeWrapper <IntegerResult>
            {
                IncumbentGeneration      = 0,
                IncumbentGenome          = originalPopulation.GetCompetitiveIndividuals().First(),
                IncumbentInstanceResults = new List <IntegerResult>().ToImmutableList(),
            };

            this.Strategy.Initialize(originalPopulation, incumbent, this.SingleTestInstance);
            this.Strategy.PerformIteration(0, this.SingleTestInstance);
            this.Strategy.DumpStatus();

            var status =
                StatusBase.ReadFromFile <CovarianceMatrixAdaptationStrategyStatus <ContinuizedGenomeSearchPoint, TestInstance> >(this.StatusFilePath);
            var searchPoints = status.MostRecentSorting;

            var updatedPopulation = this.Strategy.FinishPhase(originalPopulation);

            Assert.Equal(
                originalPopulation.GetCompetitiveIndividuals().Count,
                updatedPopulation.GetCompetitiveIndividuals().Count);

            var valueComparer = new Genome.GeneValueComparator();

            foreach (var point in searchPoints.Take(searchPoints.Count - 1))
            {
                var mappedGenome = point.Genome.CreateMutableGenome();
                Assert.True(
                    updatedPopulation.GetCompetitiveIndividuals()
                    .Any(genome => valueComparer.Equals(genome, mappedGenome)),
                    $"{mappedGenome} is not worst search point, but was not found in new competitives.");
            }

            Assert.Contains(
                incumbent.IncumbentGenome,
                updatedPopulation.GetCompetitiveIndividuals().ToArray());

            for (int age = 0; age < this.Configuration.MaxGenomeAge; age++)
            {
                Assert.True(
                    originalPopulation.GetCompetitiveIndividuals().Count(genome => genome.Age == age) ==
                    updatedPopulation.GetCompetitiveIndividuals().Count(genome => genome.Age == age),
                    $"Number of competitive genomes with age {age} changed.");
            }
        }
        public void FinishPhaseWorksWithoutOriginalIncumbent()
        {
            var originalPopulation = this.CreatePopulation();

            this.Strategy.Initialize(
                originalPopulation,
                currentIncumbent: null,
                instancesForEvaluation: this.SingleTestInstance);
            this.Strategy.PerformIteration(0, this.SingleTestInstance);
            this.Strategy.DumpStatus();

            var status =
                StatusBase.ReadFromFile <CovarianceMatrixAdaptationStrategyStatus <ContinuizedGenomeSearchPoint, TestInstance> >(this.StatusFilePath);
            var searchPoints = status.MostRecentSorting;

            var updatedPopulation = this.Strategy.FinishPhase(originalPopulation);

            Assert.Equal(
                originalPopulation.GetCompetitiveIndividuals().Count,
                updatedPopulation.GetCompetitiveIndividuals().Count);

            var valueComparer = new Genome.GeneValueComparator();

            foreach (var point in searchPoints)
            {
                var mappedGenome = point.Genome.CreateMutableGenome();
                Assert.True(
                    updatedPopulation.GetCompetitiveIndividuals()
                    .Any(genome => valueComparer.Equals(genome, mappedGenome)),
                    $"{mappedGenome} was not found in new competitives.");
            }

            for (int age = 0; age < this.Configuration.MaxGenomeAge; age++)
            {
                Assert.True(
                    originalPopulation.GetCompetitiveIndividuals().Count(genome => genome.Age == age) ==
                    updatedPopulation.GetCompetitiveIndividuals().Count(genome => genome.Age == age),
                    $"Number of competitive genomes with age {age} changed.");
            }
        }