File: test_cmawm.py

package info (click to toggle)
python-cmaes 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 408 kB
  • sloc: python: 3,115; sh: 88; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import warnings

import numpy as np
from numpy.testing import assert_almost_equal
from unittest import TestCase
from cmaes import CMA, CMAwM


class TestCMAwM(TestCase):
    def test_no_discrete_spaces(self):
        mean = np.zeros(2)
        bounds = np.array([[-10, 10], [-10, 10]])
        steps = np.array([0, 0])
        sigma = 1.3
        seed = 1

        cma_optimizer = CMA(mean=mean, sigma=sigma, bounds=bounds, seed=seed)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=UserWarning)
            cmawm_optimizer = CMAwM(
                mean=mean, sigma=sigma, bounds=bounds, steps=steps, seed=seed
            )

        for i in range(100):
            solutions = []
            for _ in range(cma_optimizer.population_size):
                cma_x = cma_optimizer.ask()
                cmawm_x_encoded, cmawm_x_for_tell = cmawm_optimizer.ask()
                assert_almost_equal(cma_x, cmawm_x_encoded)
                assert_almost_equal(cma_x, cmawm_x_for_tell)

                objective = (cma_x[0] - 3) ** 2 + cma_x[1] ** 2
                solutions.append((cma_x, objective))
            cma_optimizer.tell(solutions)
            cmawm_optimizer.tell(solutions)