public void RastriginTestMany() { double[] expected = new[] { 0.0, 0.0, 0.0, 0.0, 0.0 }; CmaEs target = new CmaEs( (x, o) => { if (x.Min() < -5.12 || x.Max() > 5.12) { return(100.0 + 10.0 * x.Length + x.Sum(xi => Math.Pow(xi, 2) - 10 * Math.Cos(2.0 * Math.PI * xi))); } else { return(10.0 * x.Length + x.Sum(xi => Math.Pow(xi, 2) - 10 * Math.Cos(2.0 * Math.PI * xi))); } }, initialParameters: Utilities.Randoms(expected.Length, null, null), stopEvalsMultiple: 1e4); Solution solution = target.Solve(); double[] actual = solution.BestParameters; for (int i = 0; i < expected.Length; i++) { Assert.IsTrue(Math.Abs(expected[i] - actual[i]) < SMALL_VALUE); } }
/// <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()); }
public void RosenbrockThirtyWithParamsTest() { double[] expected = new[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }; CmaEs target = new CmaEs((x, o) => { int dimension = x.Length; double res = 0; for (int i = 0; i < (dimension - 1); ++i) { res += Math.Pow(1 - x[i], 2) + 100.0 * Math.Pow(x[i + 1] - Math.Pow(x[i], 2), 2); } return(res); }, initialParameters: Utilities.Randoms(expected.Length, null, null), stopEvalsMultiple: 1e5); double[] actual = target.Solve().BestParameters; for (int i = 0; i < expected.Length; i++) { Assert.IsTrue(Math.Abs(expected[i] - actual[i]) < SMALL_VALUE); } }
/// <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); }
/// <inheritdoc /> public override void UseStatusDump(IGeneticEngineering evaluationModel) { // The CMA-ES runner must be reinitialized for every phase in order to use the correct genome as base. // Recreate the latest runner. var strategyStatus = this.DeserializeStrategyStatusFile(); var originalIncumbent = strategyStatus.OriginalIncumbent; if (originalIncumbent != null) { this._cmaEsRunner = this.CreateCmaEsRunner(evaluationBase: originalIncumbent); } base.UseStatusDump(evaluationModel); }
/// <summary> /// Initializes a new instance of the <see cref="LocalCovarianceMatrixAdaptationStrategy{TInstance, TResult}"/> /// class. /// </summary> /// <param name="configuration">Options used for this instance.</param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="genomeBuilder">Responsible for creation, modification and crossover of genomes. /// Needs to be compatible with the given parameter tree and configuration.</param> /// <param name="genomeSorter"> /// An <see cref="IActorRef" /> to a <see cref="GenomeSorter{TInstance,TResult}" />. /// </param> /// <param name="targetRunResultStorage"> /// An <see cref="IActorRef" /> to a <see cref="ResultStorageActor{TInstance,TResult}" /> /// which knows about all executed target algorithm runs and their results. /// </param> public LocalCovarianceMatrixAdaptationStrategy( AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, GenomeBuilder genomeBuilder, IActorRef genomeSorter, IActorRef targetRunResultStorage) : base(configuration, parameterTree, genomeSorter, targetRunResultStorage) { this._genomeBuilder = genomeBuilder ?? throw new ArgumentNullException(nameof(genomeBuilder)); // Create a dummy search point factory to enable CMA-ES dumps. this._searchPointFactory = vector => throw new InvalidOperationException("Called search point factory without initialization!"); this._cmaEsRunner = new CmaEs <PartialGenomeSearchPoint>(this.SearchPointSorter, this._searchPointFactory); }
/// <summary> /// Initializes a new instance of the /// <see cref="GlobalCovarianceMatrixAdaptationStrategy{TInstance, TResult}"/> class. /// </summary> /// <param name="configuration">Options used for this instance.</param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="genomeBuilder">Responsible for creation, modification and crossover of genomes. /// Needs to be compatible with the given parameter tree and configuration.</param> /// <param name="generationEvaluationActor"> /// An <see cref="IActorRef" /> to a <see cref="GenerationEvaluationActor{TTargetAlgorithm,TInstance,TResult}"/>. /// </param> /// <param name="targetRunResultStorage"> /// An <see cref="IActorRef" /> to a <see cref="ResultStorageActor{TInstance,TResult}" /> /// which knows about all executed target algorithm runs and their results. /// </param> public GlobalCovarianceMatrixAdaptationStrategy( AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, GenomeBuilder genomeBuilder, IActorRef generationEvaluationActor, IActorRef targetRunResultStorage) : base(configuration, parameterTree, generationEvaluationActor, targetRunResultStorage) { if (genomeBuilder == null) { throw new ArgumentNullException(nameof(genomeBuilder)); } ContinuizedGenomeSearchPoint.ObtainParameterBounds(parameterTree, out var lowerBounds, out var upperBounds); this._searchPointFactory = vector => new ContinuizedGenomeSearchPoint(vector, parameterTree, genomeBuilder, lowerBounds, upperBounds); this._cmaEsRunner = new CmaEs <ContinuizedGenomeSearchPoint>(this.SearchPointSorter, this._searchPointFactory); }
public void SchwefelOneGenerationTest() { double[] expected = new[] { 420.9687, 420.9687, 420.9687, 420.9687, 420.9687, }; CmaEs newCMA = new CmaEs((x, o) => { if ((x.Min() < -500.0) || (x.Max() > 500.0)) { return(10000.0 * x.Length + x.Sum(xi => - xi * Math.Sin(Math.Sqrt(Math.Abs(xi))))); } else { return(418.9829 * x.Length + x.Sum(xi => - xi * Math.Sin(Math.Sqrt(Math.Abs(xi))))); } }, initialParameters: Utilities.Randoms(expected.Length, null, null)); Solution lastSolution; int count = 0; while (true) { lastSolution = newCMA.Solve(isContinueFromLastSolution: true, isOneGenerationOnly: true); if (lastSolution.MinimizedFitness < SMALL_VALUE) { break; } count += lastSolution.LatestNumEvals; } Assert.IsTrue(count < 1e5); for (int i = 0; i < expected.Length; i++) { Assert.IsTrue(Math.Abs(expected[i] - lastSolution.BestParameters[i]) < 1.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()); }
public void SchwefelContinueFromLastTest() { int numTests = 10; double[] expected = new[] { 420.9687, 420.9687, 420.9687, 420.9687, 420.9687, }; CmaEs newCMA = new CmaEs((x, o) => { if (x.Min() < -500.0) { return(Math.Pow(Math.Abs(x.Min()), 4)); } else if (x.Max() > 500.0) { return(Math.Pow(Math.Abs(x.Max()), 4)); } else { return(418.9829 * x.Length + x.Sum(xi => - xi * Math.Sin(Math.Sqrt(Math.Abs(xi))))); } }, stopEvalsMultiple: 1e3, stopFitnessMinimization: 1e-5, initialParameters: Utilities.Randoms(expected.Length, null, null)); Stopwatch clock = new Stopwatch(); int count = 0; List <TimeSpan> times = new List <TimeSpan>(); for (int j = 0; j < numTests; j++) { clock.Restart(); var actual = newCMA.Solve(isContinueFromLastSolution: true); clock.Stop(); times.Add(clock.Elapsed); bool isAllOK = true; for (int i = 0; i < expected.Length; i++) { if (Math.Abs(expected[i] - actual.BestParameters[i]) > 1.0) { isAllOK = false; } } if (isAllOK) { count++; } } double percRight = count * 100.0 / numTests; double averageTime = times.Average(x => x.Seconds); Assert.IsTrue(percRight >= 90.0); Assert.IsTrue(averageTime < 2.0); }