Skip to main content

Lightweight Covariance Matrix Adaptation Evolution Strategy (CMA-ES) implementation for Python 3.

Project description

CMA-ES

Lightweight Covariance Matrix Adaptation Evolution Strategy (CMA-ES)[1] implementation.

visualize-six-hemp-camel

Himmelblau function.

visualize-himmelblau

Rosenbrock function.

visualize-rosenbrock

Quadratic function.

visualize-quadratic

These GIF animations are generated by visualizer.py.

Installation

Supported Python versions are 3.6 or later.

$ pip install cmaes

Usage

This library provides two interfaces that an Optuna's sampler interface and a low-level interface. I strongly recommend you to use this library via Optuna.

Optuna's sampler interface

Optuna [2] is an automatic hyperparameter optimization framework. Optuna officially implements a sampler based on pycma. It achieves almost the same performance. But this library is faster and simple.

import optuna
from cmaes.sampler import CMASampler

def objective(trial: optuna.Trial):
    x1 = trial.suggest_uniform("x1", -4, 4)
    x2 = trial.suggest_uniform("x2", -4, 4)
    return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2

def main():
    sampler = CMASampler()
    study = optuna.create_study(sampler=sampler)
    study.optimize(objective, n_trials=250)

if __name__ == "__main__":
    main()

Note that CMASampler doesn't support categorical distributions. Although pycma's sampler supports categorical distributions, it also has a problem (especially on high-cardinality categorical distribution). If your search space contains a categorical distribution, please use TPESampler.

Low-level interface

import numpy as np
from cmaes.cma import CMA

def quadratic(x1: float, x2: float):
    return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2

def main():
    cma_es = CMA(mean=np.zeros(2), sigma=1.3)

    best_value = float("inf")
    best_param = None

    for generation in range(50):
        solutions = []
        for _ in range(cma_es.population_size):
            z, x = cma_es.ask()
            evaluation = quadratic(x[0], x[1])

            if evaluation < best_value:
                best_value = evaluation
                best_param = x

            solutions.append((z, evaluation))

        cma_es.tell(solutions)
        print(f"#{generation}: {best_value} (x1={best_param[0]}, x2 = {best_param[1]})")

    print(f"RESULT: {best_value} (x1={best_param[0]}, x2 = {best_param[1]})")

if __name__ == "__main__":
    main()

Benchmark results

Rosenbrock function Six-Hemp Camel function
rosenbrock six-hemp-camel

This implementation (green) stands comparison with pycma (blue). See benchmark for details.

Links

Other libraries:

I respect all libraries involved in CMA-ES.

  • pycma : Most famous CMA-ES implementation by Nikolaus Hansen.
  • cma-es : A Tensorflow v2 implementation.

References:

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

cmaes-0.1.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cmaes-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file cmaes-0.1.0.tar.gz.

File metadata

  • Download URL: cmaes-0.1.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for cmaes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7ce16e919aed657712cad45c767290b5736448785da1efdc76e47946adcb3003
MD5 b157ad4b55acd36be48ca037c6422380
BLAKE2b-256 1fb90d27985942db0fc4c083c2da3edf41e56bc4d620a93963fd4b39349a1dda

See more details on using hashes here.

File details

Details for the file cmaes-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cmaes-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for cmaes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11a406fa908c652f93c49f1452b2bc79b2d7222b6a477ef76f95d8aed9bfb402
MD5 4d35dd1d7a18dc3a9874817e2378b405
BLAKE2b-256 0c5765718a2a43850ee38313fdfda687c9ee07591746856c05b8e9b8e7bdece0

See more details on using hashes here.

Supported by

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