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.
Himmelblau function.
Rosenbrock function.
Quadratic function.
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 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
if __name__ == "__main__":
sampler = CMASampler()
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=250)
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 import CMA
def quadratic(x1, x2):
return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2
if __name__ == "__main__":
cma_es = CMA(mean=np.zeros(2), sigma=1.3)
for generation in range(50):
solutions = []
for _ in range(cma_es.population_size):
x = cma_es.ask()
value = quadratic(x[0], x[1])
solutions.append((x, value))
print(f"#{generation} {value} (x1={x[0]}, x2 = {x[1]})")
cma_es.tell(solutions)
Benchmark results
Algorithm's efficiency
Rosenbrock function | Six-Hemp Camel function |
---|---|
This implementation (green) stands comparison with pycma (blue). See benchmark for details.
Execution Speed
trials/params | pycma's sampler | this library |
---|---|---|
100 / 10 | 6.000s | 2.386s |
500 / 50 | 4m 52.008s | 2m 33.287s |
1000 / 100 | 37m 41.617s | 19m 54.699s |
This script was run on my laptop so the times should not be taken precisely. This library seems to be about 2-times faster than Optuna's pycma sampler (with Optuna v1.0.0 and pycma v2.7.0).
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:
- [1] N. Hansen, The CMA Evolution Strategy: A Tutorial. arXiv:1604.00772, 2016.
- [2] Takuya Akiba, Shotaro Sano, Toshihiko Yanase, Takeru Ohta, Masanori Koyama. 2019. Optuna: A Next-generation Hyperparameter Optimization Framework. In The 25th ACM SIGKDD Conference on Knowledge Discovery and Data Mining (KDD ’19), August 4–8, 2019.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.