CMA-ES (pycma) for Numerical Optimization

4.4.4 · active · verified Sun Apr 12

CMA-ES (pycma) is a Python implementation of the Covariance Matrix Adaptation Evolution Strategy, a robust randomized derivative-free numerical optimization algorithm. It is designed for challenging non-convex, ill-conditioned, multi-modal, rugged, and noisy problems in continuous and mixed-integer search spaces. The library is currently at version 4.4.4 and maintains an active release cadence with regular improvements and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart optimizes the 10-dimensional Rosenbrock function using `cma.fmin2`, starting from an initial solution of all 0.1s and an initial step-size (sigma) of 0.5. It demonstrates how to define an objective function and call the primary optimization interface.

import cma
import numpy as np

def rosenbrock(x):
    """The Rosenbrock function for demonstration."""
    return sum(
        100.0 * (x[i + 1] - x[i]**2)**2 + (1 - x[i])**2
        for i in range(len(x) - 1)
    )

# Define initial solution and initial step-size (sigma)
# For a 10-dimensional problem, starting near the origin.
initial_solution = 10 * [0.1]  # [0.1, 0.1, ..., 0.1]
initial_sigma = 0.5

# Run the CMA-ES optimization using fmin2
# fmin2 returns (x_best, CMAEvolutionStrategy_instance)
x_best, es = cma.fmin2(
    rosenbrock,
    initial_solution,
    initial_sigma,
    options={'maxfevals': 10000, 'verb_log': 0} # Limit evaluations, suppress logging for quick run
)

print(f"Optimization finished after {es.result.evaluations} evaluations.")
print(f"Best solution found: {np.round(x_best, 4)}")
print(f"Objective value at best solution: {es.result.fbest}")

# Detailed results can be accessed via es.result or es.result_pretty()
# print(es.result_pretty())

view raw JSON →