{"id":14959154,"url":"https://github.com/srom/cma-es","last_synced_at":"2025-05-02T12:31:33.937Z","repository":{"id":60721976,"uuid":"213148775","full_name":"srom/cma-es","owner":"srom","description":"Covariance Matrix Adaptation Evolution Strategy (CMA-ES)","archived":false,"fork":false,"pushed_at":"2021-12-03T14:30:17.000Z","size":2597,"stargazers_count":62,"open_issues_count":0,"forks_count":14,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-21T20:20:05.123Z","etag":null,"topics":["numerical-optimization","optimization","optimization-algorithms","python","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/srom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-06T10:31:35.000Z","updated_at":"2025-02-21T16:06:41.000Z","dependencies_parsed_at":"2022-10-03T20:32:05.958Z","dependency_job_id":null,"html_url":"https://github.com/srom/cma-es","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srom%2Fcma-es","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srom%2Fcma-es/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srom%2Fcma-es/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srom%2Fcma-es/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srom","download_url":"https://codeload.github.com/srom/cma-es/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252038171,"owners_count":21684636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["numerical-optimization","optimization","optimization-algorithms","python","tensorflow"],"created_at":"2024-09-24T13:18:57.053Z","updated_at":"2025-05-02T12:31:33.072Z","avatar_url":"https://github.com/srom.png","language":"Jupyter Notebook","readme":"Covariance Matrix Adaptation Evolution Strategy (CMA-ES)\n--------------------------------------------------------\n\nA TensorFlow 2 implementation.\n\n## What is CMA-ES?\n\n\u003e 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. It 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. \n\n[The CMA Evolution Strategy][1]\n\n## Installation\n\nThe package is [available on PyPI](https://pypi.org/project/cma-es/) and can be installed with pip:\n\n```sh\npip install cma-es\n```\n\nAlternatively, `cma-es` can also be installed from [conda-forge](https://anaconda.org/conda-forge/cma-es):\n\n```sh\nconda install -c conda-forge cma-es\n```\n\n## Example Usage\n\n### 1. Define the fitness function\n\nCMA attempts to __minimize__ a user-defined fitness function. \n\nFunction signature:\n\n```    \nArgs:\n  x: tf.Tensor of shape (M, N)\n\nReturns:\n  Fitness evaluations: tf.Tensor of shape (M,)\n```\n\nWhere `M` is the number of solutions to evaluate and `N` is the dimension of a single solution.\n\n```python\ndef fitness_fn(x):\n    \"\"\"\n    Six-Hump Camel Function\n    https://www.sfu.ca/~ssurjano/camel6.html\n    \"\"\"\n    return (\n        (4 - 2.1 * x[:,0]**2 + x[:,0]**4 / 3) * x[:,0]**2 +\n        x[:,0] * x[:,1] +\n        (-4 + 4 * x[:,1]**2) * x[:,1]**2\n    )\n```\n\n![Figure1: Six-Hump Camel Function](six_hump_camel_fn.png?raw=true)\n\n### 2. Configure CMA-ES\n\n```python\nfrom cma import CMA\n\ncma = CMA(\n    initial_solution=[1.5, -0.4],\n    initial_step_size=1.0,\n    fitness_function=fitness_fn,\n)\n```\n\nThe initial solution and initial step size (i.e. initial standard deviation of the search distribution) are problem specific.\n\nThe population size is automatically set by default, but it can be overidden by specifying the parameter `population_size`.\n\nFor bounded constraint optimization problems, the parameter `enforce_bounds` can be set, e.g. `enforce_bounds=[[-2, 2], [-1, 1]]` for a 2D function.\n\n### 3. Run the optimizer\n\nThe 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.\n\n```python\nbest_solution, best_fitness = cma.search()\n```\n\nThe notebook [`Example 1 - Six Hump Camel Function`][4] goes into more details, including ways to plot the optimization path such as in the figure below.\n\n![Figure 2: Optimization path](cma_trace.png?raw=true)\n\n## Logging\n\nA user-defined callback function can be specified to inspect variables during the search.\n\nIt is mainly intended for logging purpose, e.g:\n\n```python\nmax_epochs = 500\n\ndef logging_function(cma, logger):\n    if cma.generation % 10 == 0:\n        fitness = cma.best_fitness()\n        logger.info(f'Generation {cma.generation} - fitness {fitness}')\n\n    if cma.termination_criterion_met or cma.generation == max_epochs:\n        sol = cma.best_solution()\n        fitness = cma.best_fitness()\n        logger.info(f'Final solution at gen {cma.generation}: {sol} (fitness: {fitness})')\n\ncma = CMA(\n    initial_solution=[1.5, -0.4],\n    initial_step_size=1.0,\n    fitness_function=fitness_fn,\n    callback_function=logging_function,\n)\ncma.search(max_epochs)\n```\n\nCheck out an example logging progress to TensorBoard: [tensorboard_example.py][6]\n\n## Running on GPU\n\nBy virtue of using TensorFlow, CMA should make use of available GPUs without any code change. \n\nIf you run into issues, check the official [TF documentation](https://www.tensorflow.org/guide/gpu).\n\n## More examples\n\n- Jupyter notebooks with examples are available:\n  - [Example 1 - Six-Hump Camel Function][4]\n  - [Example 2 - Schwefel Function][5]\n  - [Example 3 - Logging to TensorBoard][6]\n- Unit tests provide a few more examples: `cma/core_test.py`\n\n## Resources\n\n- [CMA-ES at Wikipedia][3]\n- [The CMA Evolution Strategy][1]\n- [The CMA Evolution Strategy: A Tutorial][2]\n\n[1]: http://cma.gforge.inria.fr/\n[2]: https://arxiv.org/abs/1604.00772\n[3]: https://en.wikipedia.org/wiki/CMA-ES\n[4]: https://nbviewer.jupyter.org/github/srom/cma-es/blob/master/notebook/Example%201%20-%20Six%20Hump%20Camel%20Function.ipynb\n[5]: https://nbviewer.jupyter.org/github/srom/cma-es/blob/master/notebook/Example%202%20-%20Schwefel%20Function.ipynb\n[6]: https://github.com/srom/cma-es/blob/master/notebook/tensorboard_example.py\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrom%2Fcma-es","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrom%2Fcma-es","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrom%2Fcma-es/lists"}