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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362 | class AURORA:
"""Core elements of the AURORA algorithm.
Args:
scoring_function: a function that takes a batch of genotypes and compute
their fitnesses and descriptors
emitter: an emitter is used to suggest offsprings given a MAPELites
repertoire. It has two compulsory functions. A function that takes
emits a new population, and a function that update the internal state
of the emitter.
metrics_function: a function that takes a repertoire and computes
any useful metric to track its evolution
"""
def __init__(
self,
scoring_function: Optional[
Callable[
[Genotype, RNGKey],
Tuple[Fitness, Descriptor, PyTree],
]
],
emitter: Emitter,
metrics_function: Callable[[MapElitesRepertoire], Metrics],
encoder_function: Callable[[Observation, AuroraExtraInfo], Descriptor],
training_function: Callable[
[UnstructuredRepertoire, Params, int, Observation, RNGKey],
AuroraExtraInfo,
],
observations_key: str = "observations",
) -> None:
"""
Args:
scoring_function: a function that takes a batch of genotypes and compute
their fitnesses and descriptors
emitter: an emitter is used to suggest offsprings given a MAPELites
repertoire.
metrics_function: a function that takes a repertoire and computes
any useful metric to track its evolution
encoder_function: a function that takes a batch of observations and
returns a batch of descriptors
training_function: a function that takes a repertoire, a model
parameters, an iteration number and a key, and returns an updated
AuroraExtraInfo
observations_key: the key to use for the observations in the extra_scores
of the repertoire
"""
self._scoring_function = scoring_function
self._emitter = emitter
self._metrics_function = metrics_function
self._encoder_fn = encoder_function
self._train_fn = training_function
self.observations_key = observations_key
def train(
self,
repertoire: UnstructuredRepertoire,
model_params: Params,
iteration: int,
key: RNGKey,
) -> Tuple[UnstructuredRepertoire, AuroraExtraInfo]:
observations = repertoire.extra_scores[self.observations_key]
key, subkey = jax.random.split(key)
aurora_extra_info = self._train_fn(
repertoire,
model_params,
iteration,
observations,
subkey,
)
# re-addition of all the new behavioural descriptors with the new ae
new_descriptors = self._encoder_fn(observations, aurora_extra_info)
return (
repertoire.init(
genotypes=repertoire.genotypes,
fitnesses=repertoire.fitnesses,
extra_scores=repertoire.extra_scores,
keys_extra_scores=repertoire.keys_extra_scores,
descriptors=new_descriptors,
l_value=repertoire.l_value,
max_size=repertoire.max_size,
),
aurora_extra_info,
)
def container_size_control(
self,
repertoire: UnstructuredRepertoire,
target_size: int,
previous_error: jax.Array,
) -> Tuple[UnstructuredRepertoire, jax.Array]:
# update the l value
num_indivs = jnp.sum(repertoire.fitnesses != -jnp.inf)
# CVC Implementation to keep a constant number of individuals in the archive
current_error = num_indivs - target_size
change_rate = current_error - previous_error
prop_gain = 1 * 10e-6
l_value = (
repertoire.l_value + (prop_gain * current_error) + (prop_gain * change_rate)
)
repertoire = repertoire.init(
genotypes=repertoire.genotypes,
fitnesses=repertoire.fitnesses,
extra_scores=repertoire.extra_scores,
keys_extra_scores=repertoire.keys_extra_scores,
descriptors=repertoire.descriptors,
l_value=l_value,
max_size=repertoire.max_size,
)
return repertoire, current_error
def init(
self,
genotypes: Genotype,
aurora_extra_info: AuroraExtraInfo,
l_value: jax.Array,
max_size: int,
key: RNGKey,
) -> Tuple[
UnstructuredRepertoire, Optional[EmitterState], Metrics, AuroraExtraInfo
]:
"""Initialize an unstructured repertoire with an initial population of
genotypes. Also performs the first training of the AURORA encoder.
Args:
genotypes: initial genotypes, pytree in which leaves
have shape (batch_size, num_features)
aurora_extra_info: information to perform AURORA encodings,
such as the encoder parameters
l_value: threshold distance for the unstructured repertoire
max_size: maximum size of the repertoire
key: a random key used for stochastic operations.
Returns:
an initialized unstructured repertoire, with the initial state of
the emitter, and the updated information to perform AURORA encodings
"""
key, subkey = jax.random.split(key)
fitnesses, descriptors, extra_scores = self._scoring_function(
genotypes,
subkey,
) # type: ignore
return self.init_ask_tell(
genotypes=genotypes,
fitnesses=fitnesses,
descriptors=descriptors,
aurora_extra_info=aurora_extra_info,
l_value=l_value,
max_size=max_size,
key=key,
extra_scores=extra_scores,
)
def init_ask_tell(
self,
genotypes: Genotype,
fitnesses: Fitness,
descriptors: Descriptor,
aurora_extra_info: AuroraExtraInfo,
l_value: jax.Array,
max_size: int,
key: RNGKey,
extra_scores: Optional[ExtraScores] = None,
) -> Tuple[
UnstructuredRepertoire, Optional[EmitterState], Metrics, AuroraExtraInfo
]:
if extra_scores is None:
extra_scores = {}
observations = extra_scores[self.observations_key]
descriptors = self._encoder_fn(observations, aurora_extra_info)
repertoire = UnstructuredRepertoire.init(
genotypes=genotypes,
fitnesses=fitnesses,
descriptors=descriptors,
extra_scores=extra_scores,
keys_extra_scores=(self.observations_key,),
l_value=l_value,
max_size=max_size,
)
# get initial state of the emitter
key, subkey = jax.random.split(key)
emitter_state = self._emitter.init(
key=subkey,
repertoire=repertoire,
genotypes=genotypes,
fitnesses=fitnesses,
descriptors=descriptors,
extra_scores=extra_scores,
)
repertoire, updated_aurora_extra_info = self.train(
repertoire, aurora_extra_info.model_params, iteration=0, key=key
)
# calculate the initial metrics
metrics = self._metrics_function(repertoire)
return repertoire, emitter_state, metrics, updated_aurora_extra_info
def update(
self,
repertoire: MapElitesRepertoire,
emitter_state: Optional[EmitterState],
key: RNGKey,
aurora_extra_info: AuroraExtraInfo,
) -> Tuple[MapElitesRepertoire, Optional[EmitterState], Metrics]:
"""Main step of the AURORA algorithm.
Performs one iteration of the AURORA algorithm.
1. A batch of genotypes is sampled in the archive and the genotypes are copied.
2. The copies are mutated and crossed-over
3. The obtained offsprings are scored and then added to the archive.
Args:
repertoire: unstructured repertoire
emitter_state: state of the emitter
key: a jax PRNG random key
aurora_extra_info: extra info for computing encodings
Results:
the updated MAP-Elites repertoire
the updated (if needed) emitter state
metrics about the updated repertoire
a new key
"""
if self._scoring_function is None:
raise ValueError("Scoring function is not set.")
# generate offsprings with the emitter
key, subkey = jax.random.split(key)
genotypes, extra_info = self.ask(repertoire, emitter_state, subkey)
# scores the offsprings
key, subkey = jax.random.split(key)
fitnesses, descriptors, extra_scores = self._scoring_function(
genotypes,
subkey,
)
repertoire, emitter_state, metrics = self.tell(
genotypes=genotypes,
fitnesses=fitnesses,
descriptors=descriptors,
repertoire=repertoire,
emitter_state=emitter_state,
aurora_extra_info=aurora_extra_info,
extra_scores=extra_scores,
extra_info=extra_info,
)
return repertoire, emitter_state, metrics
def ask(
self,
repertoire: MapElitesRepertoire,
emitter_state: Optional[EmitterState],
key: RNGKey,
) -> Tuple[Genotype, ExtraScores]:
"""
Ask the emitter to generate a new batch of genotypes.
Args:
repertoire: the MAP-Elites repertoire
emitter_state: state of the emitter
key: a jax PRNG random key
"""
key, subkey = jax.random.split(key)
genotypes, extra_info = self._emitter.emit(repertoire, emitter_state, subkey)
return genotypes, extra_info
def tell(
self,
genotypes: Genotype,
fitnesses: Fitness,
descriptors: Descriptor,
repertoire: MapElitesRepertoire,
emitter_state: Optional[EmitterState],
aurora_extra_info: AuroraExtraInfo,
extra_scores: Optional[ExtraScores] = None,
extra_info: Optional[ExtraScores] = None,
) -> Tuple[MapElitesRepertoire, Optional[EmitterState], Metrics]:
"""
Add new genotypes to the repertoire and update the emitter state.
Args:
genotypes: new genotypes to add to the repertoire
fitnesses: fitnesses of the new genotypes
descriptors: descriptors of the new genotypes
extra_scores: extra scores of the new genotypes
repertoire: the MAP-Elites repertoire
emitter_state: state of the emitter
"""
if extra_scores is None:
extra_scores = {}
if extra_info is None:
extra_info = {}
observations = extra_scores[self.observations_key]
descriptors = self._encoder_fn(observations, aurora_extra_info)
# add genotypes and observations in the repertoire
repertoire = repertoire.add(
genotypes,
descriptors,
fitnesses,
extra_scores,
)
# update emitter state after scoring is made
emitter_state = self._emitter.state_update(
emitter_state=emitter_state,
repertoire=repertoire,
genotypes=genotypes,
fitnesses=fitnesses,
descriptors=descriptors,
extra_scores={**extra_scores, **extra_info},
)
# update the metrics
metrics = self._metrics_function(repertoire)
return repertoire, emitter_state, metrics
|