0
# Samplers and Adaptation
1
2
Advanced sampling methods and step-size adaptation techniques that extend CMA-ES functionality for specialized optimization scenarios.
3
4
## Sampling Methods
5
6
### Core Sampler Classes
7
8
```python { .api }
9
# Access sampling functionality
10
from cma.sampler import GaussFullSampler, GaussStandardSampler
11
from cma.restricted_gaussian_sampler import RestrictedGaussianSampler
12
13
class GaussFullSampler:
14
"""
15
Full Gaussian sampler with complete covariance matrix.
16
17
Standard CMA-ES sampler that maintains and updates full covariance matrix.
18
"""
19
20
def __init__(self, N, **kwargs):
21
"""
22
Initialize Gaussian sampler.
23
24
Parameters:
25
-----------
26
N : int
27
Problem dimension.
28
"""
29
pass
30
31
def sample(self, number, update=None):
32
"""
33
Sample candidate solutions.
34
35
Parameters:
36
-----------
37
number : int
38
Number of samples to generate.
39
update : bool, optional
40
Whether to update internal state.
41
42
Returns:
43
--------
44
list[array]
45
List of sampled solutions.
46
"""
47
pass
48
49
class GaussStandardSampler:
50
"""
51
Standard Gaussian sampler for basic sampling needs.
52
"""
53
54
def __init__(self, N, **kwargs):
55
"""Initialize standard Gaussian sampler."""
56
pass
57
58
class RestrictedGaussianSampler:
59
"""
60
Gaussian sampler with restrictions on eigenvalue range.
61
62
Prevents numerical issues by restricting condition number of
63
covariance matrix within reasonable bounds.
64
"""
65
66
def __init__(self, dimension, **kwargs):
67
"""
68
Initialize restricted sampler.
69
70
Parameters:
71
-----------
72
dimension : int
73
Problem dimension.
74
"""
75
pass
76
```
77
78
## Step-Size Adaptation
79
80
### Sigma Adaptation Classes
81
82
```python { .api }
83
# Step-size adaptation methods
84
from cma.sigma_adaptation import CMAAdaptSigmaCSA, CMAAdaptSigmaTPA
85
86
class CMAAdaptSigmaCSA:
87
"""
88
Cumulative Step-size Adaptation (CSA) for CMA-ES.
89
90
Classic step-size adaptation method that adjusts global step-size
91
based on evolution path length and direction.
92
"""
93
94
def __init__(self, dimension, **kwargs):
95
"""
96
Initialize CSA adaptation.
97
98
Parameters:
99
-----------
100
dimension : int
101
Problem dimension.
102
"""
103
pass
104
105
def update(self, es, **kwargs):
106
"""
107
Update step-size based on evolution path.
108
109
Parameters:
110
-----------
111
es : CMAEvolutionStrategy
112
Evolution strategy instance.
113
"""
114
pass
115
116
class CMAAdaptSigmaTPA:
117
"""
118
Two-Point Adaptation (TPA) for step-size control.
119
120
Alternative adaptation method using two reference points.
121
"""
122
123
def __init__(self, dimension, **kwargs):
124
"""Initialize TPA adaptation."""
125
pass
126
127
def update(self, es, **kwargs):
128
"""Update step-size using TPA method."""
129
pass
130
```
131
132
## Recombination Weights
133
134
```python { .api }
135
# Recombination weight computation
136
from cma.recombination_weights import RecombinationWeights
137
138
class RecombinationWeights:
139
"""
140
Compute and manage recombination weights for parent selection.
141
142
Determines how much each parent contributes to the next generation
143
mean and covariance matrix updates.
144
"""
145
146
def __init__(self, number_of_weights, **kwargs):
147
"""
148
Initialize recombination weights.
149
150
Parameters:
151
-----------
152
number_of_weights : int
153
Number of parent solutions (mu).
154
"""
155
pass
156
157
@property
158
def weights(self):
159
"""array: Current recombination weights."""
160
pass
161
162
@property
163
def mueff(self):
164
"""float: Effective parent population size."""
165
pass
166
167
@property
168
def diversity_emphasis(self):
169
"""float: Emphasis on population diversity."""
170
pass
171
```
172
173
## Usage Examples
174
175
### Custom Sampling Configuration
176
177
```python { .api }
178
import cma
179
from cma.sampler import GaussFullSampler
180
181
# Configure custom sampler
182
options = {
183
'CMA_sampler': GaussFullSampler,
184
'CMA_sampler_options': {'eigenmethod': 1}
185
}
186
187
es = cma.CMAEvolutionStrategy([0, 0, 0], 0.5, options)
188
189
# Optimization with custom sampler
190
while not es.stop():
191
X = es.ask()
192
es.tell(X, [cma.ff.sphere(x) for x in X])
193
```
194
195
### Step-Size Adaptation Methods
196
197
```python { .api }
198
import cma
199
from cma.sigma_adaptation import CMAAdaptSigmaCSA
200
201
# Configure step-size adaptation
202
options = {
203
'AdaptSigma': CMAAdaptSigmaCSA,
204
'CSA_dampfac': 1.0, # Damping factor
205
'CSA_damp_mueff_exponent': 0.5
206
}
207
208
es = cma.CMAEvolutionStrategy([1, 2, 3], 0.3, options)
209
210
# Monitor step-size evolution
211
while not es.stop():
212
X = es.ask()
213
es.tell(X, [cma.ff.rosen(x) for x in X])
214
215
if es.countiter % 10 == 0:
216
print(f"Iteration {es.countiter}: sigma = {es.sigma:.6f}")
217
```
218
219
### Recombination Weight Customization
220
221
```python { .api }
222
import cma
223
from cma.recombination_weights import RecombinationWeights
224
225
# Custom recombination weights
226
def create_custom_weights(mu):
227
"""Create custom recombination weights."""
228
weights = RecombinationWeights(mu)
229
# Modify weights for specific requirements
230
return weights
231
232
options = {
233
'CMA_recombination_weights': create_custom_weights,
234
'popsize': 20
235
}
236
237
es = cma.CMAEvolutionStrategy([0] * 5, 0.8, options)
238
```
239
240
## Advanced Sampling Strategies
241
242
### Mirrored Sampling
243
244
```python { .api }
245
# Enable mirrored sampling for improved efficiency
246
options = {
247
'CMA_mirrors': 0.5, # Fraction of mirrored solutions
248
'CMA_mirrormethod': 2 # Mirroring method
249
}
250
251
es = cma.CMAEvolutionStrategy([1, 1, 1], 0.5, options)
252
253
while not es.stop():
254
X = es.ask()
255
# Note: Some solutions in X are mirrored versions
256
es.tell(X, [objective_function(x) for x in X])
257
```
258
259
### Elitist Strategy
260
261
```python { .api }
262
# Enable elitist selection
263
options = {
264
'CMA_elitist': True, # Use elitist variant
265
'CMA_elitist_percentage': 0.1 # Percentage of elite solutions
266
}
267
268
es = cma.CMAEvolutionStrategy([0, 0, 0], 0.5, options)
269
```