/// <summary>
        /// Checks adding a 0.2-standard deviation vector in any single coordinate does
        /// not change the distribution mean.
        /// </summary>
        /// <param name="data">Internal <see cref="CmaEs{TSearchPoint}"/> data.</param>
        /// <returns>Whether no change occurred in distribution mean.</returns>
        public bool IsMet(CmaEsElements data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (!data.IsCompletelySpecified())
            {
                throw new ArgumentOutOfRangeException(
                          nameof(data),
                          "Data must be completely specified for this termination criterion.");
            }

            for (int i = 0; i < data.Configuration.SearchSpaceDimension; i++)
            {
                var shiftedMean = data.DistributionMean[i] + ((0.2 * data.StepSize) * data.Covariances[i, i]);
                if (data.DistributionMean[i].Equals(shiftedMean))
                {
                    // Might be true due to numerical issues. Then we should stop.
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public void PropertiesAreSetCorrectly()
        {
            var data = new CmaEsElements(
                this._configuration,
                this._generation,
                this._distributionMean,
                this._stepSize,
                this._covariances,
                this._covariancesDecomposition,
                this._evolutionPath,
                this._conjugateEvolutionPath);

            Assert.Equal(this._configuration, data.Configuration);
            Assert.Equal(this._generation, data.Generation);
            Assert.Equal(this._distributionMean, data.DistributionMean);
            Assert.Equal(this._stepSize, data.StepSize);
            Assert.Equal(this._covariances, data.Covariances);
            Assert.Equal(this._diagonal, data.CovariancesDiagonal);
            Assert.Equal(
                this._eigenvectors,
                data.CovariancesEigenVectors);
            Assert.Equal(this._evolutionPath, data.EvolutionPath);
            Assert.Equal(
                this._conjugateEvolutionPath,
                data.ConjugateEvolutionPath);
        }
        /// <summary>
        /// Checks whether the current generation should be the last one.
        /// </summary>
        /// <param name="data">Internal <see cref="CmaEs{TSearchPoint}"/> data.</param>
        /// <returns>Whether the generation was the <see cref="_maximum"/>th one.</returns>
        public bool IsMet(CmaEsElements data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            return(data.Generation >= this._maximum);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Called before every test case.
 /// </summary>
 protected override void InitializeDefault()
 {
     this._data = new CmaEsElements(
         configuration: new CmaEsConfiguration(20, Vector <double> .Build.Random(3), 0.1),
         generation: 1,
         stepSize: 0.1,
         distributionMean: Vector <double> .Build.Random(2),
         covariances: Matrix <double> .Build.DenseIdentity(2),
         covariancesDecomposition: Matrix <double> .Build.DenseIdentity(2).Evd(),
         evolutionPath: Vector <double> .Build.Random(2),
         conjugateEvolutionPath: Vector <double> .Build.Random(2));
     this._terminationCriteria = new List <ITerminationCriterion> {
         new MaxIterations(57)
     };
 }
Exemplo n.º 5
0
        /// <summary>
        /// Checks whether the condition number of the covariance matrix exceeds <see cref="MaxCondition"/>.
        /// </summary>
        /// <param name="data">Internal <see cref="CmaEs{TSearchPoint}"/> data.</param>
        /// <returns>Whether the condition number exceeds <see cref="MaxCondition"/>.</returns>
        public bool IsMet(CmaEsElements data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (data.Covariances == null)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(data),
                          "Data must have the covariance matrix set for this termination criterion.");
            }

            return(data.Covariances.ConditionNumber() > MaxCondition);
        }
Exemplo n.º 6
0
        public void IsCompletelySpecifiedReturnsTrueForEverythingSpecified()
        {
            var data = new CmaEsElements(
                this._configuration,
                this._generation,
                this._distributionMean,
                this._stepSize,
                this._covariances,
                this._covariancesDecomposition,
                this._evolutionPath,
                this._conjugateEvolutionPath);

            Assert.True(
                data.IsCompletelySpecified(),
                "Data is completely specified.");
        }
Exemplo n.º 7
0
        public void IsCompletelySpecifiedReturnsFalseForMissingConjugateEvolutionPath()
        {
            var noConjugateEvolutionPath = new CmaEsElements(
                this._configuration,
                this._generation,
                this._distributionMean,
                this._stepSize,
                this._covariances,
                this._covariancesDecomposition,
                this._evolutionPath,
                conjugateEvolutionPath: null);

            Assert.False(
                noConjugateEvolutionPath.IsCompletelySpecified(),
                "Data without a conjugate evolution path is not completely specified.");
        }
Exemplo n.º 8
0
        public void IsCompletelySpecifiedReturnsFalseForMissingDistributionMean()
        {
            var noDistributionMean = new CmaEsElements(
                this._configuration,
                this._generation,
                distributionMean: null,
                stepSize: this._stepSize,
                covariances: this._covariances,
                covariancesDecomposition: this._covariancesDecomposition,
                evolutionPath: this._evolutionPath,
                conjugateEvolutionPath: this._conjugateEvolutionPath);

            Assert.False(
                noDistributionMean.IsCompletelySpecified(),
                "Data without a distribution mean is not completely specified.");
        }
Exemplo n.º 9
0
        public void IsMetThrowsForMissingConfiguration()
        {
            var covariances = Matrix <double> .Build.DenseOfDiagonalArray(new[] { 1d, 1d, 1d });

            var data = new CmaEsElements(
                configuration: null,
                generation: 5,
                distributionMean: Vector <double> .Build.Dense(3),
                stepSize: 0.2,
                covariances: covariances,
                covariancesDecomposition: covariances.Evd(),
                evolutionPath: Vector <double> .Build.Dense(3),
                conjugateEvolutionPath: Vector <double> .Build.Dense(3));

            var terminationCriterion = new NoEffectAxis();

            Assert.Throws <ArgumentOutOfRangeException>(() => terminationCriterion.IsMet(data));
        }
        /// <summary>
        /// Checks if the factor between current and initial step size is greater than a <see cref="MaxFactor"/> times
        /// the square root of the covariance matrix's largest eigenvalue.
        /// </summary>
        /// <param name="data">Internal <see cref="CmaEs{TSearchPoint}"/> data.</param>
        /// <returns>Whether the factor is greater.</returns>
        public bool IsMet(CmaEsElements data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (!data.IsCompletelySpecified())
            {
                throw new ArgumentOutOfRangeException(
                          nameof(data),
                          "Data must be completely specified for this termination criterion.");
            }

            var largestEigenvalue = data.CovariancesDiagonal.Diagonal().Maximum();

            return(data.StepSize / data.Configuration.InitialStepSize > MaxFactor *Math.Sqrt(largestEigenvalue));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Checks adding a 0.1-standard deviation vector in a principal axis direction of the covariance matrix does
        /// not change the distribution mean.
        /// <para>Axis is chosen dependent on generation.</para>
        /// </summary>
        /// <param name="data">Internal <see cref="CmaEs{TSearchPoint}"/> data.</param>
        /// <returns>Whether no change occurred in distribution mean.</returns>
        public bool IsMet(CmaEsElements data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (!data.IsCompletelySpecified())
            {
                throw new ArgumentOutOfRangeException(
                          nameof(data),
                          "Data must be completely specified for this termination criterion.");
            }

            int index = data.Generation % data.Configuration.SearchSpaceDimension;
            var principalAxisDirection =
                Math.Sqrt(data.CovariancesDiagonal[index, index]) * data.CovariancesEigenVectors.Column(index);
            var shiftedMean = data.DistributionMean + ((0.1 * data.StepSize) * principalAxisDirection);

            // Might be true due to numerical issues.
            return(data.DistributionMean.Equals(shiftedMean));
        }
Exemplo n.º 12
0
        public void PropertiesAreSetCorrectlyForMinimumSpecification()
        {
            var data = new CmaEsElements(
                configuration: null,
                generation: this._generation,
                distributionMean: null,
                stepSize: this._stepSize,
                covariances: null,
                covariancesDecomposition: null,
                evolutionPath: null,
                conjugateEvolutionPath: null);

            Assert.Null(data.Configuration);
            Assert.Equal(this._generation, data.Generation);
            Assert.Null(data.DistributionMean);
            Assert.Equal(this._stepSize, data.StepSize);
            Assert.Null(data.Covariances);
            Assert.Null(data.CovariancesDiagonal);
            Assert.Null(data.CovariancesEigenVectors);
            Assert.Null(data.EvolutionPath);
            Assert.Null(data.ConjugateEvolutionPath);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
        public void ObjectIsImmutable()
        {
            var data = new CmaEsElements(
                this._configuration,
                this._generation,
                this._distributionMean,
                this._stepSize,
                this._covariances,
                this._covariancesDecomposition,
                this._evolutionPath,
                this._conjugateEvolutionPath);

            // Play around with getters
            var returnedMean = data.DistributionMean;

            returnedMean.Clear();
            Assert.NotEqual(returnedMean, this._distributionMean);
            Assert.Equal(this._distributionMean, data.DistributionMean);

            var returnedCovariances = data.Covariances;

            returnedCovariances.Clear();
            Assert.NotEqual(returnedCovariances, this._covariances);
            Assert.Equal(this._covariances, data.Covariances);

            var returnedDiagonal = data.CovariancesDiagonal;

            returnedDiagonal.Clear();
            Assert.NotEqual(returnedDiagonal, this._diagonal);
            Assert.Equal(this._diagonal, data.CovariancesDiagonal);

            var returnedEigenVectors = data.CovariancesEigenVectors;

            returnedEigenVectors.Clear();
            Assert.NotEqual(returnedEigenVectors, this._eigenvectors);
            Assert.Equal(
                this._eigenvectors,
                data.CovariancesEigenVectors);

            var returnedEvolutionPath = data.EvolutionPath;

            returnedEvolutionPath.Clear();
            Assert.NotEqual(
                returnedEvolutionPath,
                this._evolutionPath);
            Assert.Equal(this._evolutionPath, data.EvolutionPath);

            var returnedConjugateEvolutionPath = data.ConjugateEvolutionPath;

            returnedConjugateEvolutionPath.Clear();
            Assert.NotEqual(
                returnedConjugateEvolutionPath,
                this._conjugateEvolutionPath);
            Assert.Equal(
                this._conjugateEvolutionPath,
                data.ConjugateEvolutionPath);

            // Then change values the object was initialized with.
            this._distributionMean.Clear();
            Assert.NotEqual(
                this._distributionMean,
                data.DistributionMean);

            this._covariances.Clear();
            Assert.NotEqual(this._covariances, data.Covariances);

            this._covariancesDecomposition.D.Clear();
            Assert.NotEqual(
                this._diagonal,
                data.CovariancesDiagonal);
            this._covariancesDecomposition.EigenVectors.Clear();
            Assert.NotEqual(
                this._eigenvectors,
                data.CovariancesEigenVectors);

            this._evolutionPath.Clear();
            Assert.NotEqual(this._evolutionPath, data.EvolutionPath);

            this._conjugateEvolutionPath.Clear();
            Assert.NotEqual(
                this._conjugateEvolutionPath,
                data.ConjugateEvolutionPath);
        }