SAC populations and search over frozen policy latents

From a checkout, install the optional RL extra with pip install '.[rl]' or uv sync --extra rl. The examples use a Gymnasium environment with two position coordinates and two continuous action coordinates. See examples/targeted_policy_qd.py for the environment, the held-out evaluation, and the command-line comparisons.

PBT-MAP-Elites with SAC

Each population slot has its own actor, critics, target critics and replay. At the start of a generation, low-ranked slots inherit the learned state of top-ranked agents. Some middle-ranked slots receive agents from the archive. New hyperparameters are sampled for the replacement slots and the inherited parameters begin with fresh Adam moments. The slot’s training history in replay stays with the slot. Evaluation episodes score the population and additional Iso+LineDD offspring. See Pierrot and Flajolet (ICLR 2023).

from qdpy.rl import PBTMAPElites, PolicyConfig, SACConfig
from examples.targeted_policy_qd import PositionControl, archive, descriptor

search = PBTMAPElites(
    PositionControl, archive(), descriptor=descriptor, population_size=4,
    elite_count=1, exploit_count=1, archive_replacements=1,
    training_steps_per_agent=24, offspring=2, max_episode_steps=12,
    policy=PolicyConfig((12,)), seed=11,
    sac=SACConfig(replay_capacity=512, batch_size=16, learning_starts=8,
                  critic_hidden_sizes=(16, 16)),
    hyperparameter_ranges={"actor_learning_rate": (1.5e-4, 6e-4)},
)
stats = search.train(408)
print(stats.training_steps, stats.evaluation_steps, stats.archive_replacements)
search.best_policy().save("best-sac.npz")

initialization_step_limit and generation_step_limit give the worst-case cost of a complete phase. Partial budgets can leave unused steps. population_configs gives the hyperparameters of the live agents. An empty range mapping gives fixed hyperparameters, and archive_replacements=0 disables archive injections for a matched comparison. Use SACPolicy.load to run a saved actor independently of the population.

The first implementation trains slots sequentially. Memory scales with the population’s replay buffers and the number of elite agents. Only exploratory training transitions enter replay.

BFM-QD with a frozen controller

BFM-QD stores a latent code in each cell. The same frozen controller acts on every code and in each case real rollouts provide the fitness and descriptor. Codes are projected onto the training sphere after mutation. A pretrained controller and its input preprocessing are supplied by the caller. See Bendib et al. (2026 preprint).

from qdpy.rl import BFMQD
from examples.targeted_policy_qd import frozen_controller, backward_features

search = BFMQD(
    PositionControl, archive(), descriptor=descriptor,
    controller=frozen_controller, latent_size=3, initial_policies=6,
    max_episode_steps=12, iso_sigma=.5, seed=11,
    backward_features=backward_features, backward_probability=.5,
    backward_step=.15,
)
stats = search.train(408)
print(stats.evaluation_steps, stats.backward_offspring)
policy = search.policy_for(search.archive.best)

The controller callable takes (observation, latent_code) and returns an action in the original Gymnasium space. The code is a read-only NumPy vector with default norm sqrt(latent_size). Use radius to match the pretrained model’s convention. A PyTorch model should be in eval mode and handle its own input and device conversions. Keep its checkpoint with the numeric archive so archived latent codes can be used later.

The example’s fixed analytic controller stands in for the pretrained model. The optional backward feature function maps a successor observation to the model’s latent features. The search projects the observed transition rewards onto these features with a least-squares solve, including when the feature covariance is singular. It moves some parents toward the normalized inferred code. The environment’s step reward provides the target for this projection. Supply a reward that matches the intended objective when using this operator.

python examples/targeted_policy_qd.py pbt pbt-no-injection bfm bfm-bi neural-me

See Targeted search and policy population benchmarks for the held-out measurements and interaction budgets.