QDpy
Loading...
Searching...
No Matches
gene_map_elites.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Combine GENE indirect encoding with the native neural MAP-Elites evaluator."""
6
7import numpy as np
8from torch import nn
9
10from qdpy.algorithms.geometric_encoding import GENEEncoding
11
12from .policy import _integer
13from .search import NeuralMAPElites
14
15
16
18 """Store a GENE genome per elite and decode one dense actor per proposal.
19
20 Use the native neural MAP-Elites rollout budget and Iso+LineDD
21 variation. Deterministic continuous policies and categorical
22 policies use the usual Torch actor after decoding. The returned
23 policy can be exported through its standard save method.
24
25 :param env_factory: Callable returning a single Gymnasium environment.
26 :param archive: Empty best-only scalar Grid or CVTGrid maximizing fitness.
27 :param coordinate_size: Coordinates evolved per neuron.
28 :param initial_std: Standard deviation for new geometric genomes.
29 :param kwargs: Descriptor, actor architecture, and evaluation settings
30 for :class:`NeuralMAPElites`.
31 """
32
33 def __init__(self, env_factory, archive, *, coordinate_size=3, initial_std=.4, **kwargs):
34 coordinate_size = _integer(coordinate_size, "coordinate_size")
35 if not 0. < initial_std < np.inf:
36 raise ValueError("initial_std must be finite and positive.")
37 self.initial_std = initial_std
38 super().__init__(env_factory, archive, **kwargs)
39 layers = [module for module in self._actor._network if isinstance(module, nn.Linear)]
40 self.encoding = GENEEncoding((layers[0].in_features, *(layer.out_features for layer in layers)),
41 coordinate_size)
42
43 def _genetic_parameters(self):
44 if not self._can_select():
45 return self._actor_rng.normal(0., self.initial_std, size=self.encoding.genome_size)
46 return super()._genetic_parameters()
47
48 def _prepare(self, cursor, budget):
49 self._genome = self._genetic_parameters()
50 self._actor._set_parameters(self.encoding.decode(self._genome))
51 return "gene"
52
53 def _offer(self, episodes, emitter, **kwargs):
54 kwargs.setdefault("parameters", self._genome)
55 return super()._offer(episodes, emitter, **kwargs)
56
57
61 def policy_for(self, individual, *, device=None, seed=0):
62 """Return an independent actor with the decoded parameters."""
63 return super().policy_for(self.encoding.decode(individual), device=device, seed=seed)
Encode layer weights with neuron coordinates and store biases directly.
Illuminate a policy archive by evolving coordinates for neurons.
policy_for(self, individual, *, device=None, seed=0)
Decode an archived geometric genome to a portable dense policy.
Illuminate a fixed QDpy archive by evaluating and mutating neural policies.
Definition search.py:24