cmaes


Namecmaes JSON
Version 0.11.1 PyPI version JSON
download
home_pageNone
SummaryLightweight Covariance Matrix Adaptation Evolution Strategy (CMA-ES) implementation for Python 3.
upload_time2024-08-13 06:46:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT License Copyright (c) 2020 CyberAgent, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # cmaes

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](./LICENSE) [![PyPI - Downloads](https://img.shields.io/pypi/dw/cmaes)](https://pypistats.org/packages/cmaes)

:whale: [**Paper is now available on arXiv!**](https://arxiv.org/abs/2402.01373)

*Simple* and *Practical* Python library for CMA-ES.
Please refer to the [paper](https://arxiv.org/abs/2402.01373) [Nomura and Shibata 2024] for detailed information, including the design philosophy and advanced examples.

![visualize-six-hump-camel](https://user-images.githubusercontent.com/5564044/73486622-db5cff00-43e8-11ea-98fb-8246dbacab6d.gif)

## Installation

Supported Python versions are 3.7 or later.

```
$ pip install cmaes
```

Or you can install via [conda-forge](https://anaconda.org/conda-forge/cmaes).

```
$ conda install -c conda-forge cmaes
```

## Usage

This library provides an "ask-and-tell" style interface. We employ the standard version of CMA-ES [Hansen 2016].

```python
import numpy as np
from cmaes import CMA

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

if __name__ == "__main__":
    optimizer = CMA(mean=np.zeros(2), sigma=1.3)

    for generation in range(50):
        solutions = []
        for _ in range(optimizer.population_size):
            x = optimizer.ask()
            value = quadratic(x[0], x[1])
            solutions.append((x, value))
            print(f"#{generation} {value} (x1={x[0]}, x2 = {x[1]})")
        optimizer.tell(solutions)
```

And you can use this library via [Optuna](https://github.com/optuna/optuna) [Akiba et al. 2019], an automatic hyperparameter optimization framework.
Optuna's built-in CMA-ES sampler which uses this library under the hood is available from [v1.3.0](https://github.com/optuna/optuna/releases/tag/v1.3.0) and stabled at [v2.0.0](https://github.com/optuna/optuna/releases/tag/v2.2.0).
See [the documentation](https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.CmaEsSampler.html) or [v2.0 release blog](https://medium.com/optuna/optuna-v2-3165e3f1fc2) for more details.

```python
import optuna

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 = optuna.samplers.CmaEsSampler()
    study = optuna.create_study(sampler=sampler)
    study.optimize(objective, n_trials=250)
```


## CMA-ES variants

#### Learning Rate Adaptation CMA-ES [Nomura et al. 2023]
The performance of the CMA-ES can deteriorate when faced with *difficult* problems such as multimodal or noisy ones, if its hyperparameter values are not properly configured.
The Learning Rate Adaptation CMA-ES (LRA-CMA) effectively addresses this issue by autonomously adjusting the learning rate.
Consequently, LRA-CMA eliminates the need for expensive hyperparameter tuning.

LRA-CMA can be used by simply adding `lr_adapt=True` to the initialization of `CMA()`.

<details>

<summary>Source code</summary>

```python
import numpy as np
from cmaes import CMA


def rastrigin(x):
    dim = len(x)
    return 10 * dim + sum(x**2 - 10 * np.cos(2 * np.pi * x))


if __name__ == "__main__":
    dim = 40
    optimizer = CMA(mean=3*np.ones(dim), sigma=2.0, lr_adapt=True)

    for generation in range(50000):
        solutions = []
        for _ in range(optimizer.population_size):
            x = optimizer.ask()
            value = rastrigin(x)
            if generation % 500 == 0:
                print(f"#{generation} {value}")
            solutions.append((x, value))
        optimizer.tell(solutions)

        if optimizer.should_stop():
            break
```

The full source code is available [here](./examples/lra_cma.py).

</details>



#### Warm Starting CMA-ES [Nomura et al. 2021]

Warm Starting CMA-ES (WS-CMA) is a method that transfers prior knowledge from similar tasks through the initialization of the CMA-ES.
This is useful especially when the evaluation budget is limited (e.g., hyperparameter optimization of machine learning algorithms).

![benchmark-lightgbm-toxic](https://github.com/c-bata/benchmark-warm-starting-cmaes/raw/main/result.png)

<details>
<summary>Source code</summary>

```python
import numpy as np
from cmaes import CMA, get_warm_start_mgd

def source_task(x1: float, x2: float) -> float:
    b = 0.4
    return (x1 - b) ** 2 + (x2 - b) ** 2

def target_task(x1: float, x2: float) -> float:
    b = 0.6
    return (x1 - b) ** 2 + (x2 - b) ** 2

if __name__ == "__main__":
    # Generate solutions from a source task
    source_solutions = []
    for _ in range(1000):
        x = np.random.random(2)
        value = source_task(x[0], x[1])
        source_solutions.append((x, value))

    # Estimate a promising distribution of the source task,
    # then generate parameters of the multivariate gaussian distribution.
    ws_mean, ws_sigma, ws_cov = get_warm_start_mgd(
        source_solutions, gamma=0.1, alpha=0.1
    )
    optimizer = CMA(mean=ws_mean, sigma=ws_sigma, cov=ws_cov)

    # Run WS-CMA-ES
    print(" g    f(x1,x2)     x1      x2  ")
    print("===  ==========  ======  ======")
    while True:
        solutions = []
        for _ in range(optimizer.population_size):
            x = optimizer.ask()
            value = target_task(x[0], x[1])
            solutions.append((x, value))
            print(
                f"{optimizer.generation:3d}  {value:10.5f}"
                f"  {x[0]:6.2f}  {x[1]:6.2f}"
            )
        optimizer.tell(solutions)

        if optimizer.should_stop():
            break
```

The full source code is available [here](./examples/ws_cma.py).

</details>


#### CMA-ES with Margin [Hamano et al. 2022]

CMA-ES with Margin (CMAwM) introduces a lower bound on the marginal probability for each discrete dimension, ensuring that samples avoid being fixed to a single point.
This method can be applied to mixed spaces consisting of continuous (such as float) and discrete elements (including integer and binary types).

|CMA|CMAwM|
|---|---|
|![CMA-ES](https://github.com/CyberAgentAILab/cmaes/assets/27720055/41d33c4b-b80b-42af-9f62-6d22f19dbae5)|![CMA-ESwM](https://github.com/CyberAgentAILab/cmaes/assets/27720055/9035deaa-6222-4720-a417-c31c765f3228)|

The above figures are taken from [EvoConJP/CMA-ES_with_Margin](https://github.com/EvoConJP/CMA-ES_with_Margin).

<details>
<summary>Source code</summary>

```python
import numpy as np
from cmaes import CMAwM


def ellipsoid_onemax(x, n_zdim):
    n = len(x)
    n_rdim = n - n_zdim
    r = 10
    if len(x) < 2:
        raise ValueError("dimension must be greater one")
    ellipsoid = sum([(1000 ** (i / (n_rdim - 1)) * x[i]) ** 2 for i in range(n_rdim)])
    onemax = n_zdim - (0.0 < x[(n - n_zdim) :]).sum()
    return ellipsoid + r * onemax


def main():
    binary_dim, continuous_dim = 10, 10
    dim = binary_dim + continuous_dim
    bounds = np.concatenate(
        [
            np.tile([-np.inf, np.inf], (continuous_dim, 1)),
            np.tile([0, 1], (binary_dim, 1)),
        ]
    )
    steps = np.concatenate([np.zeros(continuous_dim), np.ones(binary_dim)])
    optimizer = CMAwM(mean=np.zeros(dim), sigma=2.0, bounds=bounds, steps=steps)
    print(" evals    f(x)")
    print("======  ==========")

    evals = 0
    while True:
        solutions = []
        for _ in range(optimizer.population_size):
            x_for_eval, x_for_tell = optimizer.ask()
            value = ellipsoid_onemax(x_for_eval, binary_dim)
            evals += 1
            solutions.append((x_for_tell, value))
            if evals % 300 == 0:
                print(f"{evals:5d}  {value:10.5f}")
        optimizer.tell(solutions)

        if optimizer.should_stop():
            break


if __name__ == "__main__":
    main()
```

Source code is also available [here](./examples/cmaes_with_margin.py).

</details>


#### CatCMA [Hamano et al. 2024]
CatCMA is a method for mixed-category optimization problems, which is the problem of simultaneously optimizing continuous and categorical variables. CatCMA employs the joint probability distribution of multivariate Gaussian and categorical distributions as the search distribution.

![CatCMA](https://github.com/CyberAgentAILab/cmaes/assets/27720055/f91443b6-d71b-4849-bfc3-095864f7c58c)

<details>
<summary>Source code</summary>

```python
import numpy as np
from cmaes import CatCMA


def sphere_com(x, c):
    dim_co = len(x)
    dim_ca = len(c)
    if dim_co < 2:
        raise ValueError("dimension must be greater one")
    sphere = sum(x * x)
    com = dim_ca - sum(c[:, 0])
    return sphere + com


def rosenbrock_clo(x, c):
    dim_co = len(x)
    dim_ca = len(c)
    if dim_co < 2:
        raise ValueError("dimension must be greater one")
    rosenbrock = sum(100 * (x[:-1] ** 2 - x[1:]) ** 2 + (x[:-1] - 1) ** 2)
    clo = dim_ca - (c[:, 0].argmin() + c[:, 0].prod() * dim_ca)
    return rosenbrock + clo


def mc_proximity(x, c, cat_num):
    dim_co = len(x)
    dim_ca = len(c)
    if dim_co < 2:
        raise ValueError("dimension must be greater one")
    if dim_co != dim_ca:
        raise ValueError(
            "number of dimensions of continuous and categorical variables "
            "must be equal in mc_proximity"
        )

    c_index = np.argmax(c, axis=1) / cat_num
    return sum((x - c_index) ** 2) + sum(c_index)


if __name__ == "__main__":
    cont_dim = 5
    cat_dim = 5
    cat_num = np.array([3, 4, 5, 5, 5])
    # cat_num = 3 * np.ones(cat_dim, dtype=np.int64)
    optimizer = CatCMA(mean=3.0 * np.ones(cont_dim), sigma=1.0, cat_num=cat_num)

    for generation in range(200):
        solutions = []
        for _ in range(optimizer.population_size):
            x, c = optimizer.ask()
            value = mc_proximity(x, c, cat_num)
            if generation % 10 == 0:
                print(f"#{generation} {value}")
            solutions.append(((x, c), value))
        optimizer.tell(solutions)

        if optimizer.should_stop():
            break
```

The full source code is available [here](./examples/catcma.py).

</details>


#### Separable CMA-ES [Ros and Hansen 2008]

Sep-CMA-ES is an algorithm that limits the covariance matrix to a diagonal form.
This reduction in the number of parameters enhances scalability, making Sep-CMA-ES well-suited for high-dimensional optimization tasks.
Additionally, the learning rate for the covariance matrix is increased, leading to superior performance over the (full-covariance) CMA-ES on separable functions.

<details>
<summary>Source code</summary>

```python
import numpy as np
from cmaes import SepCMA

def ellipsoid(x):
    n = len(x)
    if len(x) < 2:
        raise ValueError("dimension must be greater one")
    return sum([(1000 ** (i / (n - 1)) * x[i]) ** 2 for i in range(n)])

if __name__ == "__main__":
    dim = 40
    optimizer = SepCMA(mean=3 * np.ones(dim), sigma=2.0)
    print(" evals    f(x)")
    print("======  ==========")

    evals = 0
    while True:
        solutions = []
        for _ in range(optimizer.population_size):
            x = optimizer.ask()
            value = ellipsoid(x)
            evals += 1
            solutions.append((x, value))
            if evals % 3000 == 0:
                print(f"{evals:5d}  {value:10.5f}")
        optimizer.tell(solutions)

        if optimizer.should_stop():
            break
```

Full source code is available [here](./examples/sep_cma.py).

</details>

#### IPOP-CMA-ES [Auger and Hansen 2005]

IPOP-CMA-ES is a method that involves restarting the CMA-ES with an incrementally increasing population size, as described below.

![visualize-ipop-cmaes-himmelblau](https://user-images.githubusercontent.com/5564044/88472274-f9e12480-cf4b-11ea-8aff-2a859eb51a15.gif)

<details>
<summary>Source code</summary>

```python
import math
import numpy as np
from cmaes import CMA

def ackley(x1, x2):
    # https://www.sfu.ca/~ssurjano/ackley.html
    return (
        -20 * math.exp(-0.2 * math.sqrt(0.5 * (x1 ** 2 + x2 ** 2)))
        - math.exp(0.5 * (math.cos(2 * math.pi * x1) + math.cos(2 * math.pi * x2)))
        + math.e + 20
    )

if __name__ == "__main__":
    bounds = np.array([[-32.768, 32.768], [-32.768, 32.768]])
    lower_bounds, upper_bounds = bounds[:, 0], bounds[:, 1]

    mean = lower_bounds + (np.random.rand(2) * (upper_bounds - lower_bounds))
    sigma = 32.768 * 2 / 5  # 1/5 of the domain width
    optimizer = CMA(mean=mean, sigma=sigma, bounds=bounds, seed=0)

    for generation in range(200):
        solutions = []
        for _ in range(optimizer.population_size):
            x = optimizer.ask()
            value = ackley(x[0], x[1])
            solutions.append((x, value))
            print(f"#{generation} {value} (x1={x[0]}, x2 = {x[1]})")
        optimizer.tell(solutions)

        if optimizer.should_stop():
            # popsize multiplied by 2 (or 3) before each restart.
            popsize = optimizer.population_size * 2
            mean = lower_bounds + (np.random.rand(2) * (upper_bounds - lower_bounds))
            optimizer = CMA(mean=mean, sigma=sigma, population_size=popsize)
            print(f"Restart CMA-ES with popsize={popsize}")
```

Full source code is available [here](./examples/ipop_cma.py).

</details>

## Citation
If you use our library in your work, please cite our paper:

Masahiro Nomura, Masashi Shibata.<br>
**cmaes : A Simple yet Practical Python Library for CMA-ES**<br>
[https://arxiv.org/abs/2402.01373](https://arxiv.org/abs/2402.01373)

Bibtex:
```
@article{nomura2024cmaes,
  title={cmaes : A Simple yet Practical Python Library for CMA-ES},
  author={Nomura, Masahiro and Shibata, Masashi},
  journal={arXiv preprint arXiv:2402.01373},
  year={2024}
}
```

## Contact
For any questions, feel free to raise an issue or contact me at nomura_masahiro@cyberagent.co.jp.

## Links

**Projects using cmaes:**

* [Optuna](https://github.com/optuna/optuna) : A hyperparameter optimization framework that supports CMA-ES using this library under the hood.
* [Kubeflow/Katib](https://www.kubeflow.org/docs/components/katib/katib-config/) : Kubernetes-based system for hyperparameter tuning and neural architecture search
* (If you are using `cmaes` in your project and would like it to be listed here, please submit a GitHub issue.)

**Other libraries:**

We have great respect for all libraries involved in CMA-ES.

* [pycma](https://github.com/CMA-ES/pycma) : Most renowned CMA-ES implementation, created and maintained by Nikolaus Hansen.
* [pymoo](https://github.com/msu-coinlab/pymoo) : A library for multi-objective optimization in Python.
* [evojax](https://github.com/google/evojax) : evojax offers a JAX-port of this library.
* [evosax](https://github.com/RobertTLange/evosax) : evosax provides a JAX-based implementation of CMA-ES and sep-CMA-ES, inspired by this library.

**References:**

* [Akiba et al. 2019] [T. Akiba, S. Sano, T. Yanase, T. Ohta, M. Koyama, Optuna: A Next-generation Hyperparameter Optimization Framework, KDD, 2019.](https://dl.acm.org/citation.cfm?id=3330701)
* [Auger and Hansen 2005] [A. Auger, N. Hansen, A Restart CMA Evolution Strategy with Increasing Population Size, CEC, 2005.](http://www.cmap.polytechnique.fr/~nikolaus.hansen/cec2005ipopcmaes.pdf)
* [Hamano et al. 2022] [R. Hamano, S. Saito, M. Nomura, S. Shirakawa, CMA-ES with Margin: Lower-Bounding Marginal Probability for Mixed-Integer Black-Box Optimization, GECCO, 2022.](https://arxiv.org/abs/2205.13482)
* [Hamano et al. 2024] [R. Hamano, S. Saito, M. Nomura, K. Uchida, S. Shirakawa, CatCMA : Stochastic Optimization for Mixed-Category Problems, GECCO, 2024.](https://arxiv.org/abs/2405.09962)
* [Hansen 2016] [N. Hansen, The CMA Evolution Strategy: A Tutorial. arXiv:1604.00772, 2016.](https://arxiv.org/abs/1604.00772)
* [Nomura et al. 2021] [M. Nomura, S. Watanabe, Y. Akimoto, Y. Ozaki, M. Onishi, Warm Starting CMA-ES for Hyperparameter Optimization, AAAI, 2021.](https://arxiv.org/abs/2012.06932)
* [Nomura et al. 2023] [M. Nomura, Y. Akimoto, I. Ono, CMA-ES with Learning
Rate Adaptation: Can CMA-ES with Default Population Size Solve Multimodal
and Noisy Problems?, GECCO, 2023.](https://arxiv.org/abs/2304.03473)
* [Nomura and Shibata 2024] [M. Nomura, M. Shibata, cmaes : A Simple yet Practical Python Library for CMA-ES, arXiv:2402.01373, 2024.](https://arxiv.org/abs/2402.01373)
* [Ros and Hansen 2008] [R. Ros, N. Hansen, A Simple Modification in CMA-ES Achieving Linear Time and Space Complexity, PPSN, 2008.](https://hal.inria.fr/inria-00287367/document)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "cmaes",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "Masahiro Nomura <nomura_masahiro@cyberagent.co.jp>",
    "keywords": null,
    "author": null,
    "author_email": "Masashi Shibata <m.shibata1020@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8f/29/86dc7d673dee33bb67b6d40d9809a6804658a2764ecd30bba201782c59d6/cmaes-0.11.1.tar.gz",
    "platform": null,
    "description": "# cmaes\n\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](./LICENSE) [![PyPI - Downloads](https://img.shields.io/pypi/dw/cmaes)](https://pypistats.org/packages/cmaes)\n\n:whale: [**Paper is now available on arXiv!**](https://arxiv.org/abs/2402.01373)\n\n*Simple* and *Practical* Python library for CMA-ES.\nPlease refer to the [paper](https://arxiv.org/abs/2402.01373) [Nomura and Shibata 2024] for detailed information, including the design philosophy and advanced examples.\n\n![visualize-six-hump-camel](https://user-images.githubusercontent.com/5564044/73486622-db5cff00-43e8-11ea-98fb-8246dbacab6d.gif)\n\n## Installation\n\nSupported Python versions are 3.7 or later.\n\n```\n$ pip install cmaes\n```\n\nOr you can install via [conda-forge](https://anaconda.org/conda-forge/cmaes).\n\n```\n$ conda install -c conda-forge cmaes\n```\n\n## Usage\n\nThis library provides an \"ask-and-tell\" style interface. We employ the standard version of CMA-ES [Hansen 2016].\n\n```python\nimport numpy as np\nfrom cmaes import CMA\n\ndef quadratic(x1, x2):\n    return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2\n\nif __name__ == \"__main__\":\n    optimizer = CMA(mean=np.zeros(2), sigma=1.3)\n\n    for generation in range(50):\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x = optimizer.ask()\n            value = quadratic(x[0], x[1])\n            solutions.append((x, value))\n            print(f\"#{generation} {value} (x1={x[0]}, x2 = {x[1]})\")\n        optimizer.tell(solutions)\n```\n\nAnd you can use this library via [Optuna](https://github.com/optuna/optuna) [Akiba et al. 2019], an automatic hyperparameter optimization framework.\nOptuna's built-in CMA-ES sampler which uses this library under the hood is available from [v1.3.0](https://github.com/optuna/optuna/releases/tag/v1.3.0) and stabled at [v2.0.0](https://github.com/optuna/optuna/releases/tag/v2.2.0).\nSee [the documentation](https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.CmaEsSampler.html) or [v2.0 release blog](https://medium.com/optuna/optuna-v2-3165e3f1fc2) for more details.\n\n```python\nimport optuna\n\ndef objective(trial: optuna.Trial):\n    x1 = trial.suggest_uniform(\"x1\", -4, 4)\n    x2 = trial.suggest_uniform(\"x2\", -4, 4)\n    return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2\n\nif __name__ == \"__main__\":\n    sampler = optuna.samplers.CmaEsSampler()\n    study = optuna.create_study(sampler=sampler)\n    study.optimize(objective, n_trials=250)\n```\n\n\n## CMA-ES variants\n\n#### Learning Rate Adaptation CMA-ES [Nomura et al. 2023]\nThe performance of the CMA-ES can deteriorate when faced with *difficult* problems such as multimodal or noisy ones, if its hyperparameter values are not properly configured.\nThe Learning Rate Adaptation CMA-ES (LRA-CMA) effectively addresses this issue by autonomously adjusting the learning rate.\nConsequently, LRA-CMA eliminates the need for expensive hyperparameter tuning.\n\nLRA-CMA can be used by simply adding `lr_adapt=True` to the initialization of `CMA()`.\n\n<details>\n\n<summary>Source code</summary>\n\n```python\nimport numpy as np\nfrom cmaes import CMA\n\n\ndef rastrigin(x):\n    dim = len(x)\n    return 10 * dim + sum(x**2 - 10 * np.cos(2 * np.pi * x))\n\n\nif __name__ == \"__main__\":\n    dim = 40\n    optimizer = CMA(mean=3*np.ones(dim), sigma=2.0, lr_adapt=True)\n\n    for generation in range(50000):\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x = optimizer.ask()\n            value = rastrigin(x)\n            if generation % 500 == 0:\n                print(f\"#{generation} {value}\")\n            solutions.append((x, value))\n        optimizer.tell(solutions)\n\n        if optimizer.should_stop():\n            break\n```\n\nThe full source code is available [here](./examples/lra_cma.py).\n\n</details>\n\n\n\n#### Warm Starting CMA-ES [Nomura et al. 2021]\n\nWarm Starting CMA-ES (WS-CMA) is a method that transfers prior knowledge from similar tasks through the initialization of the CMA-ES.\nThis is useful especially when the evaluation budget is limited (e.g., hyperparameter optimization of machine learning algorithms).\n\n![benchmark-lightgbm-toxic](https://github.com/c-bata/benchmark-warm-starting-cmaes/raw/main/result.png)\n\n<details>\n<summary>Source code</summary>\n\n```python\nimport numpy as np\nfrom cmaes import CMA, get_warm_start_mgd\n\ndef source_task(x1: float, x2: float) -> float:\n    b = 0.4\n    return (x1 - b) ** 2 + (x2 - b) ** 2\n\ndef target_task(x1: float, x2: float) -> float:\n    b = 0.6\n    return (x1 - b) ** 2 + (x2 - b) ** 2\n\nif __name__ == \"__main__\":\n    # Generate solutions from a source task\n    source_solutions = []\n    for _ in range(1000):\n        x = np.random.random(2)\n        value = source_task(x[0], x[1])\n        source_solutions.append((x, value))\n\n    # Estimate a promising distribution of the source task,\n    # then generate parameters of the multivariate gaussian distribution.\n    ws_mean, ws_sigma, ws_cov = get_warm_start_mgd(\n        source_solutions, gamma=0.1, alpha=0.1\n    )\n    optimizer = CMA(mean=ws_mean, sigma=ws_sigma, cov=ws_cov)\n\n    # Run WS-CMA-ES\n    print(\" g    f(x1,x2)     x1      x2  \")\n    print(\"===  ==========  ======  ======\")\n    while True:\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x = optimizer.ask()\n            value = target_task(x[0], x[1])\n            solutions.append((x, value))\n            print(\n                f\"{optimizer.generation:3d}  {value:10.5f}\"\n                f\"  {x[0]:6.2f}  {x[1]:6.2f}\"\n            )\n        optimizer.tell(solutions)\n\n        if optimizer.should_stop():\n            break\n```\n\nThe full source code is available [here](./examples/ws_cma.py).\n\n</details>\n\n\n#### CMA-ES with Margin [Hamano et al. 2022]\n\nCMA-ES with Margin (CMAwM) introduces a lower bound on the marginal probability for each discrete dimension, ensuring that samples avoid being fixed to a single point.\nThis method can be applied to mixed spaces consisting of continuous (such as float) and discrete elements (including integer and binary types).\n\n|CMA|CMAwM|\n|---|---|\n|![CMA-ES](https://github.com/CyberAgentAILab/cmaes/assets/27720055/41d33c4b-b80b-42af-9f62-6d22f19dbae5)|![CMA-ESwM](https://github.com/CyberAgentAILab/cmaes/assets/27720055/9035deaa-6222-4720-a417-c31c765f3228)|\n\nThe above figures are taken from [EvoConJP/CMA-ES_with_Margin](https://github.com/EvoConJP/CMA-ES_with_Margin).\n\n<details>\n<summary>Source code</summary>\n\n```python\nimport numpy as np\nfrom cmaes import CMAwM\n\n\ndef ellipsoid_onemax(x, n_zdim):\n    n = len(x)\n    n_rdim = n - n_zdim\n    r = 10\n    if len(x) < 2:\n        raise ValueError(\"dimension must be greater one\")\n    ellipsoid = sum([(1000 ** (i / (n_rdim - 1)) * x[i]) ** 2 for i in range(n_rdim)])\n    onemax = n_zdim - (0.0 < x[(n - n_zdim) :]).sum()\n    return ellipsoid + r * onemax\n\n\ndef main():\n    binary_dim, continuous_dim = 10, 10\n    dim = binary_dim + continuous_dim\n    bounds = np.concatenate(\n        [\n            np.tile([-np.inf, np.inf], (continuous_dim, 1)),\n            np.tile([0, 1], (binary_dim, 1)),\n        ]\n    )\n    steps = np.concatenate([np.zeros(continuous_dim), np.ones(binary_dim)])\n    optimizer = CMAwM(mean=np.zeros(dim), sigma=2.0, bounds=bounds, steps=steps)\n    print(\" evals    f(x)\")\n    print(\"======  ==========\")\n\n    evals = 0\n    while True:\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x_for_eval, x_for_tell = optimizer.ask()\n            value = ellipsoid_onemax(x_for_eval, binary_dim)\n            evals += 1\n            solutions.append((x_for_tell, value))\n            if evals % 300 == 0:\n                print(f\"{evals:5d}  {value:10.5f}\")\n        optimizer.tell(solutions)\n\n        if optimizer.should_stop():\n            break\n\n\nif __name__ == \"__main__\":\n    main()\n```\n\nSource code is also available [here](./examples/cmaes_with_margin.py).\n\n</details>\n\n\n#### CatCMA [Hamano et al. 2024]\nCatCMA is a method for mixed-category optimization problems, which is the problem of simultaneously optimizing continuous and categorical variables. CatCMA employs the joint probability distribution of multivariate Gaussian and categorical distributions as the search distribution.\n\n![CatCMA](https://github.com/CyberAgentAILab/cmaes/assets/27720055/f91443b6-d71b-4849-bfc3-095864f7c58c)\n\n<details>\n<summary>Source code</summary>\n\n```python\nimport numpy as np\nfrom cmaes import CatCMA\n\n\ndef sphere_com(x, c):\n    dim_co = len(x)\n    dim_ca = len(c)\n    if dim_co < 2:\n        raise ValueError(\"dimension must be greater one\")\n    sphere = sum(x * x)\n    com = dim_ca - sum(c[:, 0])\n    return sphere + com\n\n\ndef rosenbrock_clo(x, c):\n    dim_co = len(x)\n    dim_ca = len(c)\n    if dim_co < 2:\n        raise ValueError(\"dimension must be greater one\")\n    rosenbrock = sum(100 * (x[:-1] ** 2 - x[1:]) ** 2 + (x[:-1] - 1) ** 2)\n    clo = dim_ca - (c[:, 0].argmin() + c[:, 0].prod() * dim_ca)\n    return rosenbrock + clo\n\n\ndef mc_proximity(x, c, cat_num):\n    dim_co = len(x)\n    dim_ca = len(c)\n    if dim_co < 2:\n        raise ValueError(\"dimension must be greater one\")\n    if dim_co != dim_ca:\n        raise ValueError(\n            \"number of dimensions of continuous and categorical variables \"\n            \"must be equal in mc_proximity\"\n        )\n\n    c_index = np.argmax(c, axis=1) / cat_num\n    return sum((x - c_index) ** 2) + sum(c_index)\n\n\nif __name__ == \"__main__\":\n    cont_dim = 5\n    cat_dim = 5\n    cat_num = np.array([3, 4, 5, 5, 5])\n    # cat_num = 3 * np.ones(cat_dim, dtype=np.int64)\n    optimizer = CatCMA(mean=3.0 * np.ones(cont_dim), sigma=1.0, cat_num=cat_num)\n\n    for generation in range(200):\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x, c = optimizer.ask()\n            value = mc_proximity(x, c, cat_num)\n            if generation % 10 == 0:\n                print(f\"#{generation} {value}\")\n            solutions.append(((x, c), value))\n        optimizer.tell(solutions)\n\n        if optimizer.should_stop():\n            break\n```\n\nThe full source code is available [here](./examples/catcma.py).\n\n</details>\n\n\n#### Separable CMA-ES [Ros and Hansen 2008]\n\nSep-CMA-ES is an algorithm that limits the covariance matrix to a diagonal form.\nThis reduction in the number of parameters enhances scalability, making Sep-CMA-ES well-suited for high-dimensional optimization tasks.\nAdditionally, the learning rate for the covariance matrix is increased, leading to superior performance over the (full-covariance) CMA-ES on separable functions.\n\n<details>\n<summary>Source code</summary>\n\n```python\nimport numpy as np\nfrom cmaes import SepCMA\n\ndef ellipsoid(x):\n    n = len(x)\n    if len(x) < 2:\n        raise ValueError(\"dimension must be greater one\")\n    return sum([(1000 ** (i / (n - 1)) * x[i]) ** 2 for i in range(n)])\n\nif __name__ == \"__main__\":\n    dim = 40\n    optimizer = SepCMA(mean=3 * np.ones(dim), sigma=2.0)\n    print(\" evals    f(x)\")\n    print(\"======  ==========\")\n\n    evals = 0\n    while True:\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x = optimizer.ask()\n            value = ellipsoid(x)\n            evals += 1\n            solutions.append((x, value))\n            if evals % 3000 == 0:\n                print(f\"{evals:5d}  {value:10.5f}\")\n        optimizer.tell(solutions)\n\n        if optimizer.should_stop():\n            break\n```\n\nFull source code is available [here](./examples/sep_cma.py).\n\n</details>\n\n#### IPOP-CMA-ES [Auger and Hansen 2005]\n\nIPOP-CMA-ES is a method that involves restarting the CMA-ES with an incrementally increasing population size, as described below.\n\n![visualize-ipop-cmaes-himmelblau](https://user-images.githubusercontent.com/5564044/88472274-f9e12480-cf4b-11ea-8aff-2a859eb51a15.gif)\n\n<details>\n<summary>Source code</summary>\n\n```python\nimport math\nimport numpy as np\nfrom cmaes import CMA\n\ndef ackley(x1, x2):\n    # https://www.sfu.ca/~ssurjano/ackley.html\n    return (\n        -20 * math.exp(-0.2 * math.sqrt(0.5 * (x1 ** 2 + x2 ** 2)))\n        - math.exp(0.5 * (math.cos(2 * math.pi * x1) + math.cos(2 * math.pi * x2)))\n        + math.e + 20\n    )\n\nif __name__ == \"__main__\":\n    bounds = np.array([[-32.768, 32.768], [-32.768, 32.768]])\n    lower_bounds, upper_bounds = bounds[:, 0], bounds[:, 1]\n\n    mean = lower_bounds + (np.random.rand(2) * (upper_bounds - lower_bounds))\n    sigma = 32.768 * 2 / 5  # 1/5 of the domain width\n    optimizer = CMA(mean=mean, sigma=sigma, bounds=bounds, seed=0)\n\n    for generation in range(200):\n        solutions = []\n        for _ in range(optimizer.population_size):\n            x = optimizer.ask()\n            value = ackley(x[0], x[1])\n            solutions.append((x, value))\n            print(f\"#{generation} {value} (x1={x[0]}, x2 = {x[1]})\")\n        optimizer.tell(solutions)\n\n        if optimizer.should_stop():\n            # popsize multiplied by 2 (or 3) before each restart.\n            popsize = optimizer.population_size * 2\n            mean = lower_bounds + (np.random.rand(2) * (upper_bounds - lower_bounds))\n            optimizer = CMA(mean=mean, sigma=sigma, population_size=popsize)\n            print(f\"Restart CMA-ES with popsize={popsize}\")\n```\n\nFull source code is available [here](./examples/ipop_cma.py).\n\n</details>\n\n## Citation\nIf you use our library in your work, please cite our paper:\n\nMasahiro Nomura, Masashi Shibata.<br>\n**cmaes : A Simple yet Practical Python Library for CMA-ES**<br>\n[https://arxiv.org/abs/2402.01373](https://arxiv.org/abs/2402.01373)\n\nBibtex:\n```\n@article{nomura2024cmaes,\n  title={cmaes : A Simple yet Practical Python Library for CMA-ES},\n  author={Nomura, Masahiro and Shibata, Masashi},\n  journal={arXiv preprint arXiv:2402.01373},\n  year={2024}\n}\n```\n\n## Contact\nFor any questions, feel free to raise an issue or contact me at nomura_masahiro@cyberagent.co.jp.\n\n## Links\n\n**Projects using cmaes:**\n\n* [Optuna](https://github.com/optuna/optuna) : A hyperparameter optimization framework that supports CMA-ES using this library under the hood.\n* [Kubeflow/Katib](https://www.kubeflow.org/docs/components/katib/katib-config/) : Kubernetes-based system for hyperparameter tuning and neural architecture search\n* (If you are using `cmaes` in your project and would like it to be listed here, please submit a GitHub issue.)\n\n**Other libraries:**\n\nWe have great respect for all libraries involved in CMA-ES.\n\n* [pycma](https://github.com/CMA-ES/pycma) : Most renowned CMA-ES implementation, created and maintained by Nikolaus Hansen.\n* [pymoo](https://github.com/msu-coinlab/pymoo) : A library for multi-objective optimization in Python.\n* [evojax](https://github.com/google/evojax) : evojax offers a JAX-port of this library.\n* [evosax](https://github.com/RobertTLange/evosax) : evosax provides a JAX-based implementation of CMA-ES and sep-CMA-ES, inspired by this library.\n\n**References:**\n\n* [Akiba et al. 2019] [T. Akiba, S. Sano, T. Yanase, T. Ohta, M. Koyama, Optuna: A Next-generation Hyperparameter Optimization Framework, KDD, 2019.](https://dl.acm.org/citation.cfm?id=3330701)\n* [Auger and Hansen 2005] [A. Auger, N. Hansen, A Restart CMA Evolution Strategy with Increasing Population Size, CEC, 2005.](http://www.cmap.polytechnique.fr/~nikolaus.hansen/cec2005ipopcmaes.pdf)\n* [Hamano et al. 2022] [R. Hamano, S. Saito, M. Nomura, S. Shirakawa, CMA-ES with Margin: Lower-Bounding Marginal Probability for Mixed-Integer Black-Box Optimization, GECCO, 2022.](https://arxiv.org/abs/2205.13482)\n* [Hamano et al. 2024] [R. Hamano, S. Saito, M. Nomura, K. Uchida, S. Shirakawa, CatCMA : Stochastic Optimization for Mixed-Category Problems, GECCO, 2024.](https://arxiv.org/abs/2405.09962)\n* [Hansen 2016] [N. Hansen, The CMA Evolution Strategy: A Tutorial. arXiv:1604.00772, 2016.](https://arxiv.org/abs/1604.00772)\n* [Nomura et al. 2021] [M. Nomura, S. Watanabe, Y. Akimoto, Y. Ozaki, M. Onishi, Warm Starting CMA-ES for Hyperparameter Optimization, AAAI, 2021.](https://arxiv.org/abs/2012.06932)\n* [Nomura et al. 2023] [M. Nomura, Y. Akimoto, I. Ono, CMA-ES with Learning\nRate Adaptation: Can CMA-ES with Default Population Size Solve Multimodal\nand Noisy Problems?, GECCO, 2023.](https://arxiv.org/abs/2304.03473)\n* [Nomura and Shibata 2024] [M. Nomura, M. Shibata, cmaes : A Simple yet Practical Python Library for CMA-ES, arXiv:2402.01373, 2024.](https://arxiv.org/abs/2402.01373)\n* [Ros and Hansen 2008] [R. Ros, N. Hansen, A Simple Modification in CMA-ES Achieving Linear Time and Space Complexity, PPSN, 2008.](https://hal.inria.fr/inria-00287367/document)\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2020 CyberAgent, Inc.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Lightweight Covariance Matrix Adaptation Evolution Strategy (CMA-ES) implementation for Python 3.",
    "version": "0.11.1",
    "project_urls": {
        "Homepage": "https://github.com/CyberAgentAILab/cmaes"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06c9124823c5391ad69cf336fe482cd920fe485d96841ca43089c7316682fffc",
                "md5": "4675d67073759b183fa7209f5353d50d",
                "sha256": "1de77d2175045389680619c1e9b6d59d90e7888524d9e440e1704ba001de9fcc"
            },
            "downloads": -1,
            "filename": "cmaes-0.11.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4675d67073759b183fa7209f5353d50d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 35549,
            "upload_time": "2024-08-13T06:46:33",
            "upload_time_iso_8601": "2024-08-13T06:46:33.169788Z",
            "url": "https://files.pythonhosted.org/packages/06/c9/124823c5391ad69cf336fe482cd920fe485d96841ca43089c7316682fffc/cmaes-0.11.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8f2986dc7d673dee33bb67b6d40d9809a6804658a2764ecd30bba201782c59d6",
                "md5": "6a9c9f9cac19e51c4f9f7a1c1ba56fe4",
                "sha256": "cf71fa3679814723be771f2c9edd85f465b1bc1e409e1ad6d8a9e481efcd5160"
            },
            "downloads": -1,
            "filename": "cmaes-0.11.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6a9c9f9cac19e51c4f9f7a1c1ba56fe4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 31439,
            "upload_time": "2024-08-13T06:46:34",
            "upload_time_iso_8601": "2024-08-13T06:46:34.728425Z",
            "url": "https://files.pythonhosted.org/packages/8f/29/86dc7d673dee33bb67b6d40d9809a6804658a2764ecd30bba201782c59d6/cmaes-0.11.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-13 06:46:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "CyberAgentAILab",
    "github_project": "cmaes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "cmaes"
}
        
Elapsed time: 0.31531s