23 """Read one signed objective from the shared vector transition buffer."""
25 def __init__(self, shared, objective, direction):
28 def sample(self, count, rng, device):
29 observation, action, rewards, successor, terminated = self.
shared.sample(count, rng, device)
30 return observation, action, self.
direction * rewards[:, self.
objective], successor, terminated
32 def sample_observations(self, count, rng, device):
33 return self.
shared.sample_observations(count, rng, device)
47 """MOME-PGX from Janmohamed, Pierrot and Cully (GECCO 2023).
49 Use one transition buffer for all objectives and independent twin
50 critics and target actors for each objective. Sample cells uniformly
51 and then sample their members by finite crowding distance. ParetoGrid
52 truncation preserves boundary points when admitting measured policies.
54 :param env_factory: Factory for a single Gymnasium environment with Box spaces.
55 :param archive: Empty parentless ParetoGrid with crowding truncation.
56 :param reward_vector: Callable of (observation, action, successor, info)
57 returning one raw reward per archive objective.
58 :param descriptor: Callable of a completed Episode returning descriptor coordinates.
59 :param td3: Optional TD3Config for replay, optimizer, and target settings.
60 :param population_size: Even generation size, split equally between genetic
61 and gradient mutations. Gradient slots rotate among the objectives.
62 :param critic_steps: TD3 minibatch updates per objective and generation.
63 :param pg_steps: Adam steps per gradient-mutated policy.
64 :param pg_learning_rate: Adam step size for gradient mutation.
65 :param kwargs: Native neural MAP-Elites policy, evaluation, and seed arguments.
68 def __init__(self, env_factory, archive, *, reward_vector, descriptor, td3=None,
69 population_size=32, critic_steps=4, pg_steps=2, pg_learning_rate=.005, **kwargs):
70 if not callable(reward_vector):
71 raise TypeError(
"reward_vector must be callable.")
72 if "fitness" in kwargs:
73 raise TypeError(
"MOME-PGX uses measured vector returns.")
74 if not 0 < pg_learning_rate < np.inf:
75 raise ValueError(
"pg_learning_rate must be finite and positive.")
77 if not isinstance(self.
td3, TD3Config):
78 raise TypeError(
"td3 must be a TD3Config.")
79 self.
_population = _integer(population_size,
"population_size", 2)
81 raise ValueError(
"population_size must be even.")
83 self.
_pg_steps = _integer(pg_steps,
"pg_steps")
86 super().__init__(env_factory, archive, descriptor=descriptor, **kwargs)
92 for i, weight
in enumerate(archive.fitness_weights)
102 """Report policy counts in archive objective order."""
104 for name
in (
"actor_updates",
"critic_updates",
"optimizer_steps",
"policy_gradient_steps"):
105 counts[name] = sum(learner.counts[name]
for learner
in self.
_learners)
110 fronts = [front
for front
in self.
archive.solutions.values()
if front]
112 distances = _sampling_crowding([individual.fitness.values
for individual
in front])
113 total = distances.sum()
114 return front[self.
_selection_rng.choice(len(front), p=distances / total
if total
else None)]
116 def _propose_generation(self):
125 self.
_proposals.append((
"pg_offspring", objective, actor._parameters()))
133 or self.
_counts[
"evaluation_steps"] < self.
td3.learning_starts):
136 operator, objective, parameters = self.
_proposals.popleft()
137 if objective
is not None:
139 return operator, parameters
141 def _admit_candidate(self, candidate):
142 if candidate
is not None:
147 def _commit_measured(self):
150 self.
_counts[
"archive_updates"] += len(admitted)
155 """Merge the measured prefix if an interaction limit ends a generation early."""
156 super().
train(max_env_steps)