semvec/pypi-packages huggingface dataset:
gcma 0.1.53
CMA-ES wrapper

## gcma: CMA-ES wrapper

- [Sources](https://gitlab.inria.fr/gdesrues1/gcma)
- [Documentation](http://gcma.rtfd.io/)
- Paper: [The CMA Evolution Strategy: A Comparing Review, Hansen, 2006](https://www.researchgate.net/publication/227050324_The_CMA_Evolution_Strategy_A_Comparing_Review)
- Original implementation: [pycma](https://github.com/CMA-ES/pycma)

```bash
pip install gcma
```

### Define the problem

We try to find the best set of parameters such that the function f equals 0.
```math
f:(x_1, x_2, x_3) \mapsto (x_1-1)^2 + (x_2+2)^2 + x_3^2
```


```python
pa = [
    Param("x1", initial_value=2, bounds=(-10, 10)),
    Param("x2", initial_value=-7, bounds=(-10, 20)),
    Param("x3", initial_value=-7, bounds=(-15, 20)),
]
fe = [
    Feature("y1", ref=0),
]


class MyCMA(CMA):
    def energy(self, params: List[TParams], **kwargs) -> Tuple[np.ndarray, np.ndarray]:
        errs = np.zeros((self.popsize, self.n_features))
        vals = np.zeros_like(errs)
        y1 = self.features["y1"]

        for i, p in enumerate(params):
            x1, x2, x3 = p["x1"].value, p["x2"].value, p["x3"].value
            y1.value = np.sqrt(np.square(x1 - 1) + np.square(x2 + 2) + np.square(x3))
            errs[i, :] = y1.error2
            vals[i, :] = y1.value

        return errs, vals
```

### Start CMA-ES generations

We will start the genetic algorithm with mean multivariate distribution given by the initial values
of the parameters and a standard deviation `sigma0 = 2`. At each generation, the covariance matrix
of the `popsize` points in the space parameters will be updated according to the ranks of the `CMA_mu`
best f-values.

```python
opts = Options(
    popsize=10,
    maxiter=100,
    verb_disp=None,
    tolfunrel=1e-3,
)

out_dir = tf.Tree.new(__file__, "cmaes_out")
with MyCMA(pa, fe, opts, output_dir=out_dir, sigma0=2) as cma:
    cma.run()
    
    log.info(f"Best Result:\n{cma.best}")
    log.debug(f"Stop criteria: {cma.es.stop()}")
    
    cma.plotter.plot_non_normalized_params()
    cma.plotter.plot_errors()
    
    cma.plotter.plot_dist()
```


### Results
See [the video](https://gitlab.inria.fr/gdesrues1/gcma/-/blob/master/usage/src/generations.mp4).

![Parameters History](usage/src/params.png)
![Features Error](usage/src/fvalue.png)