Run a seeded MAP-Elites search¶
In this example, each candidate is a vector of three values in [0, 1].
The evaluator minimises normalised Rastrigin fitness and uses the first two
values as behaviour descriptors. A four-by-four grid stores the best candidate
in each cell. Both the fitness and descriptor domains use the evaluator’s
normalised range.
Use the search shortcut¶
Install QDpy from this checkout and run this example. It evaluates 24 candidates sequentially in batches of six.
from qdpy.benchmarks import illumination_rastrigin_normalised
from qdpy.simple import map_elites, run
search = map_elites(
dimension=3, budget=24, batch_size=6, shape=(4, 4),
fitness_domain=((0, 1),), features_domain=((0, 1), (0, 1)),
)
result = run(
search, illumination_rastrigin_normalised, seed=42,
output_dir="results.map_elites.simple",
)
print("Evaluations", result.evaluations)
print("Occupied cells", result.archive.filled_bins)
print("Best fitness", result.best.fitness.values)
qdpy.simple.map_elites() makes the grid and returns the optimiser.
Use result.archive to inspect the same grid in memory. Passing
output_dir writes run.json and a numeric archive snapshot. Choose
a fresh directory for each saved run, or omit output_dir to run in memory.
The shortcut defaults to one candidate per batch so every proposal
can use the previous evaluation. Increase batch_size to make more
candidates available for parallel evaluation. See Control evaluation with ask and tell
for evaluator formats and scheduling options.
Use a CVT archive¶
CVT-MAP-Elites assigns each behaviour descriptor to its nearest
centroid. Choose the number of centroids independently of the number
of descriptor dimensions. Pass a CVTGrid directly to the optimiser.
Set NumPy’s seed before constructing the grid so that its fitted
centroids repeat. The seed passed to run() controls the search.
import numpy as np
from qdpy.algorithms import RandomSearchMutPolyBounded
from qdpy.benchmarks import illumination_rastrigin_normalised
from qdpy.containers import CVTGrid
from qdpy.simple import run
np.random.seed(42)
archive = CVTGrid(
shape=16, nb_sampled_points=512,
fitness_domain=((0., 1.),), features_domain=((0., 1.), (0., 1.)),
)
search = RandomSearchMutPolyBounded(
archive, budget=24, batch_size=6, dimension=3,
ind_domain=(0., 1.), optimisation_task="minimisation",
)
result = run(search, illumination_rastrigin_normalised, seed=42)
print(result.evaluations, archive.filled_bins, archive.cluster_centers.shape)
To reuse the same tessellation across runs, pass the saved centroid
array as centroids= when constructing another grid. See
Plot archive geometry and evolution for a plot of the cells.
Build and log the search explicitly¶
Use the grid and optimiser directly to control their construction.
This script saves the archive and logs in results.map_elites/.
import random
from functools import partial
from pathlib import Path
import numpy as np
from qdpy.algorithms import AlgorithmLogger, RandomSearchMutPolyBounded
from qdpy.benchmarks import illumination_rastrigin_normalised
from qdpy.containers import Grid
from qdpy.tools import open_pickle_file
seed = 42
random.seed(seed)
np.random.seed(seed)
grid = Grid(
shape=(4, 4),
max_items_per_bin=1,
fitness_domain=((0.0, 1.0),),
features_domain=((0.0, 1.0), (0.0, 1.0)),
)
search = RandomSearchMutPolyBounded(
grid, budget=24, batch_size=6, dimension=3,
optimisation_task="minimisation", name="map-elites",
)
evaluate = partial(illumination_rastrigin_normalised, nb_features=2)
output = Path("results.map_elites")
output.mkdir(exist_ok=True)
AlgorithmLogger(search, log_base_path=output, save_period=1,
verbose=False, config={"seed": seed})
search.optimise(evaluate)
results = open_pickle_file(output / "final.p")
saved_grid = results["container"]
print("Evaluations:", results["nb_evaluations"])
print("Occupied cells:", saved_grid.filled_bins)
print("Best fitness:", saved_grid.best_fitness.values)
print("Best cell:", saved_grid.best_index)
saved_grid.quality_array[..., 0] holds the fitness surface for the first
objective, with NaN in empty cells. The iteration result files let you inspect
the grid after each batch. To repeat the run, keep the seed, script, and
package versions and use a different output directory for each attempt.
See Run a seeded NSLC search for a search that uses neighbours to decide what to keep.