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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716 | class MEESEmitter(Emitter):
"""
Emitter reproducing the MAP-Elites-ES algorithm from
"Scaling MAP-Elites to Deep Neuroevolution" by Colas et al:
https://dl.acm.org/doi/pdf/10.1145/3377930.3390217
One can choose between the three variants by setting use_explore and use_exploit:
ME-ES exploit-explore: use_exploit=True and use_explore=True
Alternates between num_optimizer_steps of fitness gradients and
num_optimizer_steps of novelty gradients, resample parent from the archive
every num_optimizer_steps steps.
ME-ES exploit: use_exploit=True and use_explore=False
Only uses fitness gradient, no novelty gradients, but resample parent from
the archive every num_optimizer_steps steps.
ME-ES explore: use_exploit=False and use_explore=True
Only uses novelty gradient, no fitness gradients, but resample parent from
the archive every num_optimizer_steps steps.
"""
def __init__(
self,
config: MEESConfig,
total_generations: int,
scoring_fn: Callable[
[Genotype, RNGKey], Tuple[Fitness, Descriptor, ExtraScores]
],
num_descriptors: int,
) -> None:
"""Initialise the MAP-Elites-ES emitter.
WARNING: total_generations is required to build the novelty archive.
Args:
config: algorithm config
scoring_fn: used to evaluate the samples for the gradient estimate.
total_generations: total number of generations for which the
emitter will run, allow to initialise the novelty archive.
num_descriptors: dimension of the descriptors, used to initialise
the empty novelty archive.
"""
self._config = config
self._scoring_fn = scoring_fn
self._total_generations = total_generations
self._num_descriptors = num_descriptors
# Initialise optimizer
if self._config.adam_optimizer:
self._optimizer = optax.adam(learning_rate=config.learning_rate)
else:
self._optimizer = optax.sgd(learning_rate=config.learning_rate)
@property
def batch_size(self) -> int:
"""
Returns:
the batch size emitted by the emitter.
"""
return 1
def init(
self,
key: RNGKey,
repertoire: MapElitesRepertoire,
genotypes: Genotype,
fitnesses: Fitness,
descriptors: Descriptor,
extra_scores: ExtraScores,
) -> MEESEmitterState:
"""Initializes the emitter state.
Args:
genotypes: The initial population.
key: A random key.
Returns:
The initial state of the MEESEmitter, a new random key.
"""
# Initialisation requires one initial genotype
if jax.tree.leaves(genotypes)[0].shape[0] > 1:
genotypes = jax.tree.map(
lambda x: x[0],
genotypes,
)
# Initialise optimizer
initial_optimizer_state = self._optimizer.init(genotypes)
# Create empty Novelty archive
if self._config.use_explore:
novelty_archive = NoveltyArchive.init(
self._total_generations, self._num_descriptors
)
else:
novelty_archive = NoveltyArchive.init(
self._config.novelty_nearest_neighbors, self._num_descriptors
)
# Create empty updated genotypes and fitness
last_updated_genotypes = jax.tree.map(
lambda x: jnp.zeros(shape=(self._config.last_updated_size,) + x.shape[1:]),
genotypes,
)
last_updated_fitnesses = -jnp.inf * jnp.ones(
shape=(self._config.last_updated_size, 1)
)
emitter_state = MEESEmitterState(
initial_optimizer_state=initial_optimizer_state,
optimizer_state=initial_optimizer_state,
offspring=genotypes,
generation_count=0,
novelty_archive=novelty_archive,
last_updated_genotypes=last_updated_genotypes,
last_updated_fitnesses=last_updated_fitnesses,
last_updated_position=0,
key=key,
)
return emitter_state
def emit( # type: ignore
self,
repertoire: MapElitesRepertoire,
emitter_state: MEESEmitterState,
key: RNGKey,
) -> Tuple[Genotype, ExtraScores]:
"""Return the offspring generated through gradient update.
Args:
repertoire: The MAP-Elites repertoire to sample from.
emitter_state: The current emitter state.
key: A JAX PRNG random key.
Returns:
The next gradient-based offspring and empty extra scores.
"""
return emitter_state.offspring, {}
def _sample_exploit(
self,
emitter_state: MEESEmitterState,
repertoire: MapElitesRepertoire,
key: RNGKey,
) -> Genotype:
"""Sample half of the time uniformly from the exploit_num_cell_sample
highest-performing cells of the repertoire and half of the time uniformly
from the exploit_num_cell_sample highest-performing cells among the
last updated cells.
Args:
emitter_state: current emitter_state
repertoire: the current repertoire
key: a jax PRNG random key
Returns:
samples: a genotype sampled in the repertoire
"""
def _sample(
key: RNGKey,
genotypes: Genotype,
fitnesses: Fitness,
) -> Genotype:
"""Sample uniformly from the 2 highest fitness cells."""
max_fitnesses, _ = jax.lax.top_k(
fitnesses.squeeze(axis=1), self._config.exploit_num_cell_sample
)
min_fitness = jnp.nanmin(
jnp.where(max_fitnesses > -jnp.inf, max_fitnesses, jnp.inf)
)
genotypes_empty = fitnesses.squeeze(axis=1) < min_fitness
p = (1.0 - genotypes_empty) / jnp.sum(1.0 - genotypes_empty)
samples = jax.tree.map(
lambda x: jax.random.choice(key, x, shape=(1,), p=p),
genotypes,
)
return samples
key, subkey = jax.random.split(key)
# Sample p uniformly
p = jax.random.uniform(subkey)
# Depending on the value of p, use one of the two sampling options
repertoire_sample = partial(
_sample, genotypes=repertoire.genotypes, fitnesses=repertoire.fitnesses
)
last_updated_sample = partial(
_sample,
genotypes=emitter_state.last_updated_genotypes,
fitnesses=emitter_state.last_updated_fitnesses,
)
samples = jax.lax.cond(
p < 0.5,
repertoire_sample,
last_updated_sample,
key,
)
return samples
def _sample_explore(
self,
emitter_state: MEESEmitterState,
repertoire: MapElitesRepertoire,
key: RNGKey,
) -> Genotype:
"""Sample uniformly from the explore_num_cell_sample most-novel genotypes.
Args:
emitter_state: current emitter state
repertoire: the current genotypes repertoire
key: a jax PRNG random key
Returns:
samples: a genotype sampled in the repertoire
"""
# Compute the novelty of all indivs in the archive
novelties = emitter_state.novelty_archive.novelty(
repertoire.descriptors, self._config.novelty_nearest_neighbors
)
novelties = jnp.where(
repertoire.fitnesses.squeeze(axis=1) > -jnp.inf, novelties, -jnp.inf
)
# Sample uniformly for the explore_num_cell_sample most novel cells
max_novelties, _ = jax.lax.top_k(
novelties, self._config.explore_num_cell_sample
)
min_novelty = jnp.nanmin(
jnp.where(max_novelties > -jnp.inf, max_novelties, jnp.inf)
)
repertoire_empty = novelties < min_novelty
p = (1.0 - repertoire_empty) / jnp.sum(1.0 - repertoire_empty)
samples = jax.tree.map(
lambda x: jax.random.choice(key, x, shape=(1,), p=p),
repertoire.genotypes,
)
return samples
def _es_emitter(
self,
parent: Genotype,
optimizer_state: optax.OptState,
key: RNGKey,
scores_fn: Callable[[Fitness, Descriptor], jax.Array],
) -> Tuple[Genotype, optax.OptState]:
"""Main es component, given a parent and a way to infer the score from
the fitnesses and descriptors of its es-samples, return its
approximated-gradient-generated offspring.
Args:
parent: the considered parent.
scores_fn: a function to infer the score of its es-samples from
their fitness and descriptors.
key
Returns:
The approximated-gradients-generated offspring and a new key.
"""
key, subkey = jax.random.split(key)
# Sampling mirror noise
total_sample_number = self._config.sample_number
if self._config.sample_mirror:
sample_number = total_sample_number // 2
half_sample_noise = jax.tree.map(
lambda x: jax.random.normal(
key=subkey,
shape=jnp.repeat(x, sample_number, axis=0).shape,
),
parent,
)
sample_noise = jax.tree.map(
lambda x: jnp.concatenate(
[jnp.expand_dims(x, axis=1), jnp.expand_dims(-x, axis=1)], axis=1
).reshape(jnp.repeat(x, 2, axis=0).shape),
half_sample_noise,
)
gradient_noise = half_sample_noise
# Sampling non-mirror noise
else:
sample_number = total_sample_number
sample_noise = jax.tree.map(
lambda x: jax.random.normal(
key=subkey,
shape=jnp.repeat(x, sample_number, axis=0).shape,
),
parent,
)
gradient_noise = sample_noise
# Applying noise
samples = jax.tree.map(
lambda x: jnp.repeat(x, total_sample_number, axis=0),
parent,
)
samples = jax.tree.map(
lambda mean, noise: mean + self._config.sample_sigma * noise,
samples,
sample_noise,
)
# Evaluating samples
fitnesses, descriptors, extra_scores = self._scoring_fn(samples, key)
# Computing rank, with or without normalisation
scores = scores_fn(fitnesses, descriptors)
if self._config.sample_rank_norm:
ranking_indices = jnp.argsort(scores, axis=0)
ranks = jnp.argsort(ranking_indices, axis=0)
ranks = (ranks / (total_sample_number - 1)) - 0.5
else:
ranks = scores
# Reshaping rank to match shape of genotype_noise
if self._config.sample_mirror:
ranks = jnp.reshape(ranks, (sample_number, 2))
ranks = jnp.apply_along_axis(lambda rank: rank[0] - rank[1], 1, ranks)
ranks = jax.tree.map(
lambda x: jnp.reshape(
jnp.repeat(ranks.ravel(), x[0].ravel().shape[0], axis=0), x.shape
),
gradient_noise,
)
# Computing the gradients
gradient = jax.tree.map(
lambda noise, rank: jnp.multiply(noise, rank),
gradient_noise,
ranks,
)
gradient = jax.tree.map(
lambda x: jnp.reshape(x, (sample_number, -1)),
gradient,
)
gradient = jax.tree.map(
lambda g, p: jnp.reshape(
-jnp.sum(g, axis=0) / (total_sample_number * self._config.sample_sigma),
p.shape,
),
gradient,
parent,
)
# Adding regularisation
gradient = jax.tree.map(
lambda g, p: g + self._config.l2_coefficient * p,
gradient,
parent,
)
# Applying gradients
(offspring_update, optimizer_state) = self._optimizer.update(
gradient, optimizer_state
)
offspring = optax.apply_updates(parent, offspring_update)
return offspring, optimizer_state
def _buffers_update(
self,
emitter_state: MEESEmitterState,
repertoire: MapElitesRepertoire,
genotypes: Genotype,
fitnesses: Fitness,
descriptors: Descriptor,
) -> MEESEmitterState:
"""Update the different buffers and archives in the emitter
state to generate the offspring for the next generation.
Args:
emitter_state: current emitter state
repertoire: the current genotypes repertoire
genotypes: the genotypes of the batch of emitted offspring.
fitnesses: the fitnesses of the batch of emitted offspring.
descriptors: the descriptors of the emitted offspring.
Returns:
The modified emitter state.
"""
# Updating novelty archive
novelty_archive = emitter_state.novelty_archive.update(descriptors)
# Check if genotype from previous iteration has been added to the grid
indice = get_cells_indices(descriptors, repertoire.centroids)
added_genotype = jnp.all(
jnp.asarray(
jax.tree.leaves(
jax.tree.map(
lambda new_gen, rep_gen: jnp.all(
jnp.equal(
jnp.ravel(new_gen), jnp.ravel(rep_gen.at[indice].get())
),
axis=0,
),
genotypes,
repertoire.genotypes,
),
)
),
axis=0,
)
# Update last_updated buffers
last_updated_position = jnp.where(
added_genotype,
emitter_state.last_updated_position,
self._config.last_updated_size + 1,
)
last_updated_fitnesses = emitter_state.last_updated_fitnesses
last_updated_fitnesses = last_updated_fitnesses.at[last_updated_position].set(
fitnesses[0]
)
last_updated_genotypes = jax.tree.map(
lambda last_gen, gen: last_gen.at[
jnp.expand_dims(last_updated_position, axis=0)
].set(gen),
emitter_state.last_updated_genotypes,
genotypes,
)
last_updated_position = (
emitter_state.last_updated_position + added_genotype
) % self._config.last_updated_size
# Return new emitter_state
return emitter_state.replace( # type: ignore
novelty_archive=novelty_archive,
last_updated_genotypes=last_updated_genotypes,
last_updated_fitnesses=last_updated_fitnesses,
last_updated_position=last_updated_position,
)
def state_update( # type: ignore
self,
emitter_state: MEESEmitterState,
repertoire: MapElitesRepertoire,
genotypes: Genotype,
fitnesses: Fitness,
descriptors: Descriptor,
extra_scores: ExtraScores,
) -> MEESEmitterState:
"""Generate the gradient offspring for the next emitter call. Also
update the novelty archive and generation count from current call.
Args:
emitter_state: current emitter state
repertoire: the current genotypes repertoire
genotypes: the genotypes of the batch of emitted offspring.
fitnesses: the fitnesses of the batch of emitted offspring.
descriptors: the descriptors of the emitted offspring.
extra_scores: a dictionary with other values outputted by the
scoring function.
Returns:
The modified emitter state.
"""
assert jax.tree.leaves(genotypes)[0].shape[0] == 1, (
"ERROR: MAP-Elites-ES generates 1 offspring per generation, "
+ "batch_size should be 1, the inputted batch has size:"
+ str(jax.tree.leaves(genotypes)[0].shape[0])
)
# Update all the buffers and archives of the emitter_state
emitter_state = self._buffers_update(
emitter_state, repertoire, genotypes, fitnesses, descriptors
)
# Use new or previous parents and exploitation or exploration
generation_count = emitter_state.generation_count
sample_new_parent = generation_count % self._config.num_optimizer_steps == 0
use_exploration = (
self._config.use_explore and not self._config.use_exploit
) or (
self._config.use_explore
and self._config.use_exploit
and ((generation_count // self._config.num_optimizer_steps) % 2 == 0)
)
# Select parent and optimizer_state
key, subkey = jax.random.split(emitter_state.key)
parent = jax.lax.cond(
sample_new_parent,
lambda emitter_state, repertoire, key: jax.lax.cond(
use_exploration,
self._sample_explore,
self._sample_exploit,
emitter_state,
repertoire,
key,
),
lambda emitter_state, repertoire, key: emitter_state.offspring,
emitter_state,
repertoire,
subkey,
)
optimizer_state = jax.lax.cond(
sample_new_parent,
lambda _unused: emitter_state.initial_optimizer_state,
lambda _unused: emitter_state.optimizer_state,
(),
)
# Define scores for es process
def exploration_exploitation_scores(
fitnesses: Fitness, descriptors: Descriptor
) -> jax.Array:
scores = jax.lax.cond(
use_exploration,
lambda fitnesses, descriptors: emitter_state.novelty_archive.novelty(
descriptors, self._config.novelty_nearest_neighbors
),
lambda fitnesses, descriptors: fitnesses,
fitnesses,
descriptors,
)
return scores
# Run es process
key, subkey = jax.random.split(key)
offspring, optimizer_state = self._es_emitter(
parent=parent,
optimizer_state=optimizer_state,
key=subkey,
scores_fn=exploration_exploitation_scores,
)
return emitter_state.replace( # type: ignore
optimizer_state=optimizer_state,
offspring=offspring,
generation_count=generation_count + 1,
key=key,
)
|