-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathccmaes2009.py
138 lines (120 loc) · 7.59 KB
/
ccmaes2009.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import numpy as np # engine for numerical computing
from pypop7.optimizers.es.es import ES # abstract class of all Evolution Strategies (ES) classes
class CCMAES2009(ES):
"""Cholesky-CMA-ES 2009 (CCMAES2009).
Parameters
----------
problem : `dict`
problem arguments with the following common settings (`keys`):
* 'fitness_function' - objective function to be **minimized** (`func`),
* 'ndim_problem' - number of dimensionality (`int`),
* 'upper_boundary' - upper boundary of search range (`array_like`),
* 'lower_boundary' - lower boundary of search range (`array_like`).
options : `dict`
optimizer options with the following common settings (`keys`):
* 'max_function_evaluations' - maximum of function evaluations (`int`, default: `np.inf`),
* 'max_runtime' - maximal runtime to be allowed (`float`, default: `np.inf`),
* 'seed_rng' - seed for random number generation needed to be *explicitly* set (`int`);
and with the following particular settings (`keys`):
* 'sigma' - initial global step-size, aka mutation strength (`float`),
* 'mean' - initial (starting) point, aka mean of Gaussian search distribution (`array_like`),
* if not given, it will draw a random sample from the uniform distribution whose search range is
bounded by `problem['lower_boundary']` and `problem['upper_boundary']`.
* 'n_individuals' - number of offspring, aka offspring population size (`int`, default:
`4 + int(3*np.log(problem['ndim_problem']))`),
* 'n_parents' - number of parents, aka parental population size (`int`, default:
`int(options['n_individuals']/2)`).
Examples
--------
Use the black-box optimizer `CCMAES2009` to minimize the well-known test function
`Rosenbrock <http://en.wikipedia.org/wiki/Rosenbrock_function>`_:
.. code-block:: python
:linenos:
>>> import numpy # engine for numerical computing
>>> from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized
>>> from pypop7.optimizers.es.ccmaes2009 import CCMAES2009
>>> problem = {'fitness_function': rosenbrock, # to define problem arguments
... 'ndim_problem': 2,
... 'lower_boundary': -5.0*numpy.ones((2,)),
... 'upper_boundary': 5.0*numpy.ones((2,))}
>>> options = {'max_function_evaluations': 5000, # to set optimizer options
... 'seed_rng': 2022,
... 'mean': 3.0*numpy.ones((2,)),
... 'sigma': 3.0} # global step-size may need to be fine-tuned for better performance
>>> ccmaes2009 = CCMAES2009(problem, options) # to initialize the optimizer class
>>> results = ccmaes2009.optimize() # to run the optimization/evolution process
>>> print(f"CCMAES2009: {results['n_function_evaluations']}, {results['best_so_far_y']}")
CCMAES2009: 5000, 2.1572e-16
For its correctness checking of Python coding, please refer to `this code-based repeatability report
</~https://github.com/Evolutionary-Intelligence/pypop/blob/main/pypop7/optimizers/es/_repeat_ccmaes2009.py>`_
for all details. For *pytest*-based automatic testing, please see `test_ccmaes2009.py
</~https://github.com/Evolutionary-Intelligence/pypop/blob/main/pypop7/optimizers/es/test_ccmaes2009.py>`_.
References
----------
Suttorp, T., Hansen, N. and Igel, C., 2009.
`Efficient covariance matrix update for variable metric evolution strategies.
<https://link.springer.com/article/10.1007/s10994-009-5102-1>`_
Machine Learning, 75(2), pp.167-197.
"""
def __init__(self, problem, options):
self.options = options
ES.__init__(self, problem, options)
self.c_s = None
self.d_s = None
self.c_c = options.get('c_c', 4.0/(self.ndim_problem + 4.0))
self.c_cov = options.get('c_cov', 2.0/np.power(self.ndim_problem + np.sqrt(2.0), 2.0))
def _set_c_s(self):
return np.sqrt(self._mu_eff)/(np.sqrt(self.ndim_problem) + np.sqrt(self._mu_eff))
def _set_d_s(self):
return 1.0 + 2.0*np.maximum(0.0, np.sqrt((self._mu_eff - 1.0)/(self.ndim_problem + 1.0)) - 1.0) + self.c_s
def initialize(self, is_restart=False):
mean = self._initialize_mean(is_restart) # mean of Gaussian search distribution
z = np.empty((self.n_individuals, self.ndim_problem)) # Gaussian noise for mutation
x = np.empty((self.n_individuals, self.ndim_problem)) # offspring population
a = np.diag(np.ones(self.ndim_problem,)) # Cholesky factors
a_i = np.diag(np.ones(self.ndim_problem,)) # inverse of Cholesky factors
p_s = np.zeros((self.ndim_problem,)) # evolution path for global step-size adaptation
p_c = np.zeros((self.ndim_problem,)) # evolution path for covariance matrix adaptation
y = np.empty((self.n_individuals,)) # fitness (no evaluation)
self.c_s = self.options.get('c_s', self._set_c_s())
self.d_s = self.options.get('d_s', self._set_d_s())
return mean, z, x, a, a_i, p_s, p_c, y
def iterate(self, z=None, x=None, mean=None, a=None, y=None, args=None):
for k in range(self.n_individuals):
if self._check_terminations():
return z, x, y
z[k] = self.rng_optimization.standard_normal((self.ndim_problem,))
x[k] = mean + self.sigma*np.dot(a, z[k])
y[k] = self._evaluate_fitness(x[k], args)
return z, x, y
def _update_distribution(self, z=None, x=None, a=None, a_i=None, p_s=None, p_c=None, y=None):
order = np.argsort(y)[:self.n_parents]
mean, z_w = np.dot(self._w, x[order]), np.dot(self._w, z[order])
p_c = (1.0-self.c_c)*p_c + np.sqrt(self.c_c*(2.0 - self.c_c)*self._mu_eff)*np.dot(a, z_w)
v = np.dot(a_i, p_c)
v_norm = np.dot(v, v) # (||v||)^2
s_v_norm = np.sqrt(1.0 + self.c_cov/(1.0 - self.c_cov)*v_norm)
a_i = (a_i - (1.0 - 1.0/s_v_norm)*np.dot(v[:, np.newaxis], np.dot(v[np.newaxis, :], a_i))/v_norm
)/np.sqrt(1.0 - self.c_cov)
a = np.sqrt(1.0 - self.c_cov)*(a + (s_v_norm - 1.0)*np.outer(p_c, v)/v_norm)
p_s = (1.0 - self.c_s)*p_s + np.sqrt(self.c_s*(2.0 - self.c_s)*self._mu_eff)*z_w
self.sigma *= np.exp(self.c_s/self.d_s*(np.linalg.norm(p_s)/self._e_chi - 1.0))
return mean, a, a_i, p_s, p_c
def restart_reinitialize(self, mean=None, z=None, x=None, a=None, a_i=None, p_s=None, p_c=None, y=None):
if self.is_restart and ES.restart_reinitialize(self, y):
mean, z, x, a, a_i, p_s, p_c, y = self.initialize(True)
return mean, z, x, a, a_i, p_s, p_c, y
def optimize(self, fitness_function=None, args=None): # for all generations (iterations)
fitness = ES.optimize(self, fitness_function)
mean, z, x, a, a_i, p_s, p_c, y = self.initialize()
while not self.termination_signal:
# sample and evaluate offspring population
z, x, y = self.iterate(z, x, mean, a, y, args)
if self._check_terminations():
break
mean, a, a_i, p_s, p_c = self._update_distribution(z, x, a, a_i, p_s, p_c, y)
self._print_verbose_info(fitness, y)
self._n_generations += 1
mean, z, x, a, a_i, p_s, p_c, y = self.restart_reinitialize(
mean, z, x, a, a_i, p_s, p_c, y)
return self._collect(fitness, y, mean)