forked from CyberAgentAILab/cmaes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lra_cma.py
27 lines (21 loc) · 725 Bytes
/
lra_cma.py
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
import numpy as np
from cmaes import CMA
def rastrigin(x):
dim = len(x)
if dim < 2:
raise ValueError("dimension must be greater one")
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, seed=10, 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