18 """Combine an evaluated greedy actor, genetic offspring, and gradient offspring.
20 Each post-warmup generation trains twin critics and a single greedy
21 actor from past rollout transitions. The first slot evaluates a copy
22 of the greedy actor. Genetic and gradient variation fill the other
23 slots using elites from the archive as it stood at the start of the
24 generation. A gradient offspring uses its own Adam optimizer and
25 uses Q1 to maximize predicted return on states sampled from replay.
26 Between training calls the search retains replay and queued proposals.
27 A partial evaluation contributes to replay but cannot enter the archive.
29 :param env_factory: Callable returning an environment with a finite
30 floating-point Box action space.
31 :param archive: Best-only Grid or CVTGrid as in :class:`NeuralMAPElites`.
32 :param td3: Replay and learner settings as a :class:`TD3Config`.
33 :param batch_size: Number of candidates per post-warmup generation.
34 :param genetic_offspring: Iso+LineDD slots per generation. Defaults to
35 half the batch rounded down. One slot is reserved for the greedy actor.
36 :param critic_steps: Number of TD3 critic minibatch updates per generation.
37 :param pg_steps: Number of Adam updates for each gradient offspring.
38 :param pg_learning_rate: Adam learning rate for gradient offspring.
39 :param kwargs: Evaluation, policy, and variation options for
40 :class:`NeuralMAPElites`. Gradients always use environment rewards.
43 def __init__(self, env_factory, archive, *, td3=None, batch_size=8, genetic_offspring=None,
44 critic_steps=16, pg_steps=4, pg_learning_rate=1e-3, **kwargs):
46 if not isinstance(self.
td3, TD3Config):
47 raise TypeError(
"td3 must be a TD3Config.")
48 self.
batch_size = _integer(batch_size,
"batch_size", 2)
50 else _integer(genetic_offspring,
"genetic_offspring", 0))
52 raise ValueError(
"genetic_offspring must leave room for the greedy actor.")
53 self.
critic_steps = _integer(critic_steps,
"critic_steps")
54 self.
pg_steps = _integer(pg_steps,
"pg_steps")
55 if not isfinite(pg_learning_rate)
or pg_learning_rate <= 0:
56 raise ValueError(
"pg_learning_rate must be finite and positive.")
58 super().__init__(env_factory, archive, **kwargs)
64 def _build_actor(self, env, config, seed, device):
65 return ContinuousPolicy(env.observation_space, env.action_space, config=config, seed=seed, device=device)
69 """Return cumulative costs, generation operator counts, and replay size."""
72 def _prepare(self, cursor, budget):
75 return super()._prepare(cursor, budget)
79 self.
_actor._set_parameters(parameters)
80 if emitter ==
"greedy":
81 self.
_counts[
"actor_injections"] += 1
83 self.
_counts[
"pg_offspring"] += 1
86 def _build_generation(self):
89 proposals = [(
"greedy", self.
_learner.actor._parameters())]
96 def _gradient_parameters(self):
99 return self.
_actor._parameters()