Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmaEsElementsTest"/> class.
        /// </summary>
        public CmaEsElementsTest()
        {
            this._configuration    = new CmaEsConfiguration(20, Vector <double> .Build.Random(3), 0.1);
            this._distributionMean = Vector <double> .Build.DenseOfArray(new[] { 1.8, 4.9 });

            this._evolutionPath = Vector <double> .Build.DenseOfArray(new[] { 2.1, 3.2 });

            this._conjugateEvolutionPath = Vector <double> .Build.DenseOfArray(new[] { 1.7, 4.1 });

            this._covariances = Matrix <double> .Build
                                .DenseOfArray(
                new[, ]
            {
                {
                    3d,
                    1d,
                },
                {
                    1d,
                    3d,
                },
            });

            this._covariancesDecomposition = this._covariances.Evd(Symmetricity.Symmetric);
            this._diagonal     = this._covariancesDecomposition.D;
            this._eigenvectors = this._covariancesDecomposition.EigenVectors;
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        protected override void InitializeContinuousOptimizer(
            Population basePopulation,
            IncumbentGenomeWrapper <TResult> currentIncumbent)
        {
            if (basePopulation == null)
            {
                throw new ArgumentNullException(nameof(basePopulation));
            }

            if (currentIncumbent == null)
            {
                LoggingHelper.WriteLine(
                    VerbosityLevel.Warn,
                    "CMA-ES with focus on incumbent can only be executed if an incumbent exists, i.e. it is not possible to run it on its own.");
                throw new ArgumentNullException(nameof(currentIncumbent));
            }

            // We do not reuse anything from potential old configurations, because old information may be
            // outdated at the point a new phase is started.
            var initialMean = PartialGenomeSearchPoint.CreateFromGenome(
                currentIncumbent.IncumbentGenome,
                this.ParameterTree,
                this.StrategyConfiguration.MinimumDomainSize).Values;
            var cmaEsConfiguration = new CmaEsConfiguration(
                populationSize: basePopulation.CompetitiveCount,
                initialDistributionMean: initialMean,
                initialStepSize: this.StrategyConfiguration.InitialStepSize);

            this._cmaEsRunner = this.CreateCmaEsRunner(evaluationBase: currentIncumbent.IncumbentGenome);
            this._cmaEsRunner.Initialize(cmaEsConfiguration, this.CreateTerminationCriteria());
        }
Ejemplo n.º 3
0
        public void PropertiesGetInitializedCorrectly()
        {
            var config = new CmaEsConfiguration(
                this._populationSize,
                this._initialDistributionMean,
                this._initialStepSize);

            Assert.Equal(this._searchSpaceDimension, config.SearchSpaceDimension);
            Assert.Equal(this._populationSize, config.PopulationSize);
            Assert.True(3 == config.ParentNumber, "Parent number is not as expected (half of population).");
            Assert.Equal(
                this._initialDistributionMean,
                config.InitialDistributionMean);
            Assert.Equal(this._initialStepSize, config.InitialStepSize);

            var doubleComparisonExactness = 1e-04;

            Assert.Equal(
                this._populationSize,
                config.Weights.Count);
            TestUtils.Equals(config.Weights[0], 0.5856, doubleComparisonExactness);
            TestUtils.Equals(config.Weights[1], 0.2928, doubleComparisonExactness);
            TestUtils.Equals(config.Weights[2], 0.1215, doubleComparisonExactness);
            TestUtils.Equals(config.Weights[3], 0, doubleComparisonExactness);
            TestUtils.Equals(config.Weights[4], -0.4241, doubleComparisonExactness);
            TestUtils.Equals(config.Weights[5], -0.7706, doubleComparisonExactness);
            TestUtils.Equals(
                config.Weights[6],
                -1.0636,
                doubleComparisonExactness);

            TestUtils.Equals(
                config.VarianceEffectiveSelectionMass,
                2.2548,
                doubleComparisonExactness);

            TestUtils.Equals(
                config.CumulationLearningRate,
                0.5588,
                doubleComparisonExactness);
            TestUtils.Equals(
                config.RankOneUpdateLearningRate,
                0.0964,
                doubleComparisonExactness);
            TestUtils.Equals(
                config.RankMuUpdateLearningRate,
                0.0512,
                doubleComparisonExactness);

            TestUtils.Equals(
                config.StepSizeControlLearningRate,
                0.4149,
                doubleComparisonExactness);
            TestUtils.Equals(
                config.StepSizeControlDamping,
                1.4149,
                doubleComparisonExactness);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmaEsTest"/> class.
        /// </summary>
        public CmaEsTest()
        {
            this._runner              = new CmaEs <SearchPoint>(this._sorter, this._searchPointFactory);
            this._configuration       = new CmaEsConfiguration(7, Vector <double> .Build.Dense(3), 0.1);
            this._terminationCriteria = new List <ITerminationCriterion> {
                new MaxIterations(3)
            };

            Randomizer.Configure(0);
        }
Ejemplo n.º 5
0
        public void ExpectedConjugateEvolutionPathLengthIsComputedCorrectly()
        {
            var config = new CmaEsConfiguration(
                this._populationSize,
                this._initialDistributionMean,
                this._initialStepSize);

            TestUtils.Equals(
                config.ComputeExpectedConjugateEvolutionPathLength(),
                1.5957,
                1e-4);
        }
Ejemplo n.º 6
0
        public void UseStatusDumpWorksCorrectly()
        {
            // Create a status dump.
            this._runner.Initialize(this._configuration, this._terminationCriteria);
            this._runner.NextGeneration();
            this._runner.DumpStatus(this._statusFilePath);
            var originalStatusDump = StatusBase.ReadFromFile <CmaEsStatus>(this._statusFilePath);

            // Create a different runner, then use status dump.
            var otherConfiguration       = new CmaEsConfiguration(14, Vector <double> .Build.Dense(3), 0.2);
            var otherTerminationCriteria = new List <ITerminationCriterion> {
                new MaxIterations(89)
            };

            this._runner.Initialize(otherConfiguration, otherTerminationCriteria);
            this._runner.UseStatusDump(this._statusFilePath);

            // Check newly dumped status vs old one.
            this._runner.DumpStatus(this._statusFilePath);
            var newStatusDump = StatusBase.ReadFromFile <CmaEsStatus>(this._statusFilePath);

            // Compare dumps
            TestUtils.SetsAreEquivalent(
                originalStatusDump.TerminationCriteria.Select(criterion => criterion.GetType()).ToArray(),
                newStatusDump.TerminationCriteria.Select(criterion => criterion.GetType()).ToArray());
            Assert.Equal(
                originalStatusDump.Data.Configuration.CumulationLearningRate,
                newStatusDump.Data.Configuration.CumulationLearningRate);
            Assert.Equal(originalStatusDump.Data.Generation, newStatusDump.Data.Generation);
            Assert.Equal(
                originalStatusDump.Data.DistributionMean,
                newStatusDump.Data.DistributionMean);
            Assert.Equal(originalStatusDump.Data.StepSize, newStatusDump.Data.StepSize);

            Assert.Equal(
                originalStatusDump.Data.Covariances,
                newStatusDump.Data.Covariances);
            Assert.Equal(originalStatusDump.Data.CovariancesDiagonal, newStatusDump.Data.CovariancesDiagonal);
            Assert.Equal(
                originalStatusDump.Data.CovariancesEigenVectors,
                newStatusDump.Data.CovariancesEigenVectors);

            Assert.Equal(
                originalStatusDump.Data.EvolutionPath,
                newStatusDump.Data.EvolutionPath);
            Assert.Equal(
                originalStatusDump.Data.ConjugateEvolutionPath,
                newStatusDump.Data.ConjugateEvolutionPath);
        }
Ejemplo n.º 7
0
        public void DistributionMeanIsImmutable()
        {
            var config = new CmaEsConfiguration(
                this._populationSize,
                this._initialDistributionMean,
                this._initialStepSize);

            var returnedMean = config.InitialDistributionMean;

            returnedMean.Clear();
            Assert.NotEqual(returnedMean, this._initialDistributionMean);
            Assert.Equal(
                this._initialDistributionMean,
                config.InitialDistributionMean);

            this._initialDistributionMean.Clear();
            Assert.NotEqual(
                this._initialDistributionMean,
                config.InitialDistributionMean);
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        protected override void InitializeContinuousOptimizer(
            Population basePopulation,
            IncumbentGenomeWrapper <TResult> currentIncumbent)
        {
            if (basePopulation == null)
            {
                throw new ArgumentNullException(nameof(basePopulation));
            }

            // We do not reuse anything from potential old configurations, because old information may be
            // outdated at the point a new phase is started.
            var initialMean = currentIncumbent != null
                                  ? ContinuizedGenomeSearchPoint.CreateFromGenome(currentIncumbent.IncumbentGenome, this.ParameterTree).Values
                                  : this.ComputeMeanOfCompetitivePopulationPart(basePopulation);

            var cmaEsConfiguration = new CmaEsConfiguration(
                populationSize: basePopulation.CompetitiveCount,
                initialDistributionMean: initialMean,
                initialStepSize: this.StrategyConfiguration.InitialStepSize);

            this._cmaEsRunner = new CmaEs <ContinuizedGenomeSearchPoint>(this.SearchPointSorter, this._searchPointFactory);
            this._cmaEsRunner.Initialize(cmaEsConfiguration, this.CreateTerminationCriteria());
        }
Ejemplo n.º 9
0
        public override void ReadFromFileDeserializesCorrectly()
        {
            /* Create status. */
            this._terminationCriteria = new List <ITerminationCriterion> {
                new TolUpSigma()
            };
            var configuration = new CmaEsConfiguration(
                populationSize: 20,
                initialDistributionMean: Vector <double> .Build.Random(3),
                initialStepSize: 0.1);
            var covariances = 17 * Matrix <double> .Build.DenseIdentity(2);

            var covariancesDecomposition = covariances.Evd(Symmetricity.Symmetric);

            this._data = new CmaEsElements(
                configuration,
                generation: 24,
                stepSize: 0.14,
                distributionMean: Vector <double> .Build.Random(2),
                covariances: covariances,
                covariancesDecomposition: covariancesDecomposition,
                evolutionPath: Vector <double> .Build.Random(2),
                conjugateEvolutionPath: Vector <double> .Build.Random(2));
            var cmaEsStatus = new CmaEsStatus(this._terminationCriteria, this._data);

            /* Write and read it from file. */
            cmaEsStatus.WriteToFile(this.StatusFilePath);
            var deserializedStatus = StatusBase.ReadFromFile <CmaEsStatus>(this.StatusFilePath);

            /* Check it's still the same. */
            Assert.Equal(
                this._terminationCriteria.Count,
                deserializedStatus.TerminationCriteria.Count);
            Assert.Equal(
                this._terminationCriteria[0].GetType(),
                deserializedStatus.TerminationCriteria[0].GetType());
            Assert.Equal(
                configuration.PopulationSize,
                deserializedStatus.Data.Configuration.PopulationSize);
            Assert.Equal(
                configuration.InitialDistributionMean,
                deserializedStatus.Data.Configuration.InitialDistributionMean);
            Assert.Equal(
                configuration.InitialStepSize,
                deserializedStatus.Data.Configuration.InitialStepSize);
            Assert.Equal(
                this._data.Generation,
                deserializedStatus.Data.Generation);
            Assert.Equal(
                this._data.StepSize,
                deserializedStatus.Data.StepSize);
            Assert.Equal(
                this._data.DistributionMean,
                deserializedStatus.Data.DistributionMean);
            Assert.Equal(
                this._data.Covariances,
                deserializedStatus.Data.Covariances);
            Assert.Equal(
                this._data.CovariancesDiagonal,
                deserializedStatus.Data.CovariancesDiagonal);
            Assert.Equal(
                this._data.CovariancesEigenVectors,
                deserializedStatus.Data.CovariancesEigenVectors);
            Assert.Equal(
                this._data.EvolutionPath,
                deserializedStatus.Data.EvolutionPath);
            Assert.Equal(
                this._data.ConjugateEvolutionPath,
                deserializedStatus.Data.ConjugateEvolutionPath);
        }