CMAESOptimizer<T, TInput, TOutput>
Models & Types · AiDotNet.Optimizers
Implements the Covariance Matrix Adaptation Evolution Strategy (CMA-ES) optimization algorithm.
Inheritance: OptimizerBase<T, TInput, TOutput> → CMAESOptimizer<T, TInput, TOutput>
Implements: IDerivativeFreeFunctionOptimizer<T>
For Beginners
CMA-ES is like an advanced search algorithm that tries to find the best solution by learning from previous attempts. It’s especially good at solving complex problems where the relationship between inputs and outputs isn’t straightforward.
How It Works
CMA-ES is a powerful optimization algorithm for non-linear, non-convex optimization problems. It is particularly effective for problems with up to about 100 dimensions and is known for its robustness and ability to handle complex fitness landscapes.
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Enums;
using AiDotNet.NeuralNetworks;
using AiDotNet.Optimizers;
using AiDotNet.Tensors.LinearAlgebra;
var rng = new Random(0);
var trainX = new Tensor<double>(new[] { 32, 8 });
var trainY = new Tensor<double>(new[] { 32, 2 });
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 8; j++) trainX[new[] { i, j }] = rng.NextDouble();
trainY[new[] { i, i % 2 }] = 1.0;
}
var model = new NeuralNetwork<double>(new NeuralNetworkArchitecture<double>(
inputFeatures: 8, numClasses: 2, complexity: NetworkComplexity.Simple));
var result = await new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
.ConfigureModel(model)
.ConfigureOptimizer(new CMAESOptimizer<double, Tensor<double>, Tensor<double>>(model))
.ConfigureDataLoader(DataLoaders.FromTensors(trainX, trainY))
.BuildAsync();
Console.WriteLine("Trained with CMAESOptimizer.");
Constructors
| Constructor | Summary |
|---|---|
CMAESOptimizer(IFullModel<T, TInput, TOutput> model, CMAESOptimizerOptions<T, TInput, TOutput> options, IEngine engine) |
Initializes a new instance of the CMAESOptimizer class. |
Methods
| Method | Summary |
|---|---|
CMAESGeneration<T> Ask(CMAESSearchState state, Random random) |
Samples the next generation of a search: lambda points m + sigma * y_k with y_k = A z_k ~ N(0, C), where A is the lower Cholesky factor of C and z_k is standard normal. |
CMAESOptimizer<T, TInput, TOutput> CreateForFunction(CMAESOptimizerOptions<T, TInput, TOutput> options) |
Creates a CMA-ES optimizer for minimizing a plain function, with no model attached. |
CMAESSearchState CreateSearchState(Vector<T> initialParameters) |
Starts an incremental (ask/tell) CMA-ES search centred on initialParameters. |
OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions() |
Gets the current options of the CMA-ES optimizer. |
OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData) |
Performs the main optimization process using the CMA-ES algorithm. |
Vector<T> Minimize(Vector<T> initialParameters, Func<Vector<T>, T> objective, int maxIterations, T tolerance) |
|
void Tell(CMAESSearchState state, CMAESGeneration<T> generation, IReadOnlyList<T> fitness) |
Updates a search from the fitness of the generation Ask sampled (lower is better): weighted recombination of the best mu draws moves the mean, the evolution paths are updated, C receives the rank-one and rank-mu updates, and sigma follows… |