Skip to main content

Covariance Matrix Adaptation Evolution Strategy (CMA-ES), a TensorFlow v2 implementation

Project description

Covariance Matrix Adaptation Evolution Strategy (CMA-ES)

A Tensorflow v2 implementation.

What is CMA-ES

Quoting The CMA Evolution Strategy page:

The CMA-ES (Covariance Matrix Adaptation Evolution Strategy) is an evolutionary algorithm for difficult non-linear non-convex black-box optimisation problems in continuous domain. The CMA-ES is considered as state-of-the-art in evolutionary computation and has been adopted as one of the standard tools for continuous optimisation in many (probably hundreds of) research labs and industrial environments around the world.

The CMA-ES is typically applied to unconstrained or bounded constraint optimization problems, and search space dimensions between three and a hundred.

Example Usage

1. Define the fitness function

The CMA class expects fitness functions with the following signature:

Args:
  x: tf.Tensor of shape (M, N)

Returns:
  Fitness evaluations: tf.Tensor of shape (M,)

Where M is the number of solutions to evaluate and N is the dimension of a single solution.

def fitness_fn(x):
    """
    Six-Hump Camel Function
    https://www.sfu.ca/~ssurjano/camel6.html
    """
    return (
        (4 - 2.1 * x[:,0]**2 + x[:,0]**4 / 3) * x[:,0]**2 +
        x[:,0] * x[:,1] +
        (-4 + 4 * x[:,1]**2) * x[:,1]**2
    )

Six-Hump Camel Function

2. Configure CMA-ES

The initial solution and initial step size (i.e. initial standard deviation of the search distribution) are problem specific.

The population size is automatically set by default, but it can be overidden by setting the parameter population_size.

from cma import CMA

cma = CMA(
    initial_solution=[1.5, -0.4],
    initial_step_size=1.0,
    fitness_function=fitness_fn,
)

3. Run the optimizer

The search method runs until the maximum number of generation is reached or until one of the early termination criteria is met. By default, the maximum number of generations is 500.

cma.search()

Retrieve the best solution and its fitness value:

num_generations = cma.generation
best_solution = cma.best_solution()
best_fitness = cma.best_fitness()

The notebook notebook/Example 1 - Six Hump Camel Function.ipynb goes into more details, including ways to plot the evolution path of the optimization such as in the figure below.

Six-Hump Camel Function

More examples

  • Jupyter notebooks with examples are available in the notebook/ folder.
  • Unit tests also provide a few more examples: cma/core_test.py

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cma-es-1.0.0.tar.gz (7.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page