Batched neural MAP-Elites¶
Pass evaluation_batch_size to qdpy.rl.NeuralMAPElites.train()
to evaluate several proposed policies together. QDpy creates independent
environments from the same factory. It steps the active environments
synchronously and uses one batch of PyTorch inference for their
different policies. The single-environment evaluation path remains the
default. Install the rl extra for either path.
import gymnasium as gym
from qdpy.containers import Grid
from qdpy.rl import NeuralMAPElites
archive = Grid(shape=8, fitness_domain=((0, 96),),
features_domain=((0, 1),))
search = NeuralMAPElites(
lambda: gym.make("CartPole-v1"),
archive,
descriptor=lambda episode: (episode.steps / 96,),
max_episode_steps=96,
seed=7,
)
stats = search.train(1024, evaluation_batch_size=4)
print(stats.env_steps, stats.evaluated_policies, archive.filled_bins)
The batch proposes candidates from the same archive state and applies
complete results in proposal order. Batch size can therefore change the
search trajectory, even with the same seed. Each lane keeps its own Gymnasium
episode and records the real final observation. Finished lanes wait
for the next evaluation repeat. The final round advances only as many
lanes as the remaining budget permits. Only candidates with all requested
episodes complete can enter the archive. An on_step callback receives
the step count already consumed for that batch as each complete candidate
is offered to the archive.
Reserve enough steps for complete policies¶
Pass budget_aware=True when the remaining budget is small relative
to the requested batch width. QDpy reserves the episode cap times the
number of episodes per policy and uses that bound to choose each batch’s
width. When fewer steps remain than the bound for one policy, QDpy tries
one policy at a time. A policy can still finish early and enter the
archive. A policy cut off by the search budget consumes steps and stays
out of the archive.
stats = search.train(512, evaluation_batch_size=16, budget_aware=True)
The 512-step comparison below uses a 96-step cap, three seeds, and three held-out episodes per elite. All 27 budget-aware trials spent exactly 512 search steps. Results are medians at a requested width of 16. The fixed-width values come from the held-out comparison below.
Task |
Fixed completed policies |
Budget-aware completed policies |
Fixed held-out QD score |
Budget-aware held-out QD score |
|---|---|---|---|---|
CartPole-v1 |
32 |
25 |
1.118 |
1.587 |
Pendulum-v1 |
0 |
5 |
0.000 |
1.837 |
Reacher-v5 |
0 |
10 |
0.000 |
3.760 |
On CartPole, many episodes finish before the cap, so reserving 96 steps per policy narrows batches more than early termination requires. Choose the option for the expected episode lengths and budget of your task.
python -I examples/audited_neural_map_elites.py --budgets 512 \
--batch-sizes 1 4 16 --budget-aware --output build/budget-aware.json
CPU throughput pilot¶
The pilot used Gymnasium 1.4.0, PyTorch 2.15.0, and one logical CPU on an Intel Xeon E5-2630 v4. Each run used 4,096 search steps, a 96-step episode cap, and two 32-unit hidden layers. The table uses medians over seeds 7, 13, and 23. Rates measure the entire training call.
Task |
Policies per batch |
Steps/s |
Speedup |
Completed policies |
|---|---|---|---|---|
CartPole-v1 |
1 |
3,180 |
1.00× |
186 |
CartPole-v1 |
4 |
4,511 |
1.42× |
130 |
CartPole-v1 |
16 |
8,530 |
2.68× |
117 |
Pendulum-v1 |
1 |
4,467 |
1.00× |
42 |
Pendulum-v1 |
4 |
8,477 |
1.90× |
40 |
Pendulum-v1 |
16 |
11,489 |
2.57× |
32 |
Every run spent exactly 4,096 steps. Batch size 16 improved step throughput on these tasks while completing fewer policy evaluations. Search trajectories and partial episodes both affect that count. The raw report includes archive occupancy, best return, and costs for each seed. A repeat reproduced the non-timing records.
python -m pip install ".[rl]"
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 \
python -I examples/batched_neural_map_elites.py \
--output build/batched-neural-map-elites.json
Download the pilot JSON.
See Discrete policy search with PPO and MAP-Elites and Continuous control with PPO, TD3, and PGA-MAP-Elites
for custom environment and policy export examples.
Held-out quality at matched budgets¶
The second pilot uses fresh searches at 512, 1,536, and 4,096 environment steps on CartPole, Pendulum, and Reacher. It uses the same three seeds and widths as above. Each native archive uses a 6 × 6 grid and scores one measured episode per candidate. The descriptors are clipped averages of the first two observation coordinates, scaled to the unit interval. Fitness uses the fraction of the horizon survived on CartPole, the mean reward scaled by 16.3 on Pendulum, and the exponential of the nonpositive mean reward on Reacher.
Every final elite sees the same three fresh reset seeds within a trial. Aggregate its fitness and descriptors before constructing the held-out archive. The audit has separate episode, step, and elapsed-time counts. Each point uses independent runs at the stated budget. The wall-time plots include both search and audit for that run. Search timing starts at the training call.
Task |
Batch width |
Median held-out QD score |
Median search and audit time (s) |
Seeds reaching target |
|---|---|---|---|---|
CartPole |
1 |
3.21 |
1.65 |
2 of 3 |
CartPole |
4 |
3.97 |
1.17 |
2 of 3 |
CartPole |
16 |
2.69 |
0.83 |
1 of 3 |
Pendulum |
1 |
1.91 |
1.24 |
0 of 3 |
Pendulum |
4 |
1.93 |
0.88 |
0 of 3 |
Pendulum |
16 |
2.45 |
0.74 |
0 of 3 |
Reacher |
1 |
10.86 |
1.67 |
3 of 3 |
Reacher |
4 |
10.01 |
1.33 |
3 of 3 |
Reacher |
16 |
11.13 |
1.36 |
3 of 3 |
The target QD score was 3 for CartPole and 4 for Pendulum and Reacher. On Reacher, the median total time at the first tested budget reaching the target was 0.83 s at width 1 and 0.57 s at width 16. On CartPole, width 4 achieved the highest median final QD score. At 512 steps, width 16 completed zero Pendulum and Reacher policies because the budget expired before any lane reached the end of its episode. These observations cover the tested tasks, budgets, and seeds.
The study used one logical CPU on the same Xeon as the throughput pilot, with Gymnasium 1.4.0, MuJoCo 4.0.1, and CPU PyTorch 2.15.0. Reacher is optional and uses the Gymnasium MuJoCo extra.
python -m pip install ".[rl]" "gymnasium[mujoco]"
MUJOCO_GL=disable OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 \
python -I examples/audited_neural_map_elites.py \
--tasks CartPole-v1 Pendulum-v1 Reacher-v5 \
--output build/audited-neural-map-elites.json --plot build/audited-neural-map-elites.png
Download the per-seed audit and search records.
See the official Reacher environment documentation.