Run a seeded NSLC search ======================== QDpy's NSLC example uses a child archive for selected candidates and a parent archive for evaluation history. Novelty is the average descriptor distance to the nearest neighbours in the parent. The child admits a sufficiently novel candidate or one whose fitness dominates its nearest neighbour. Local competition is the fraction of the requested neighbours it outperforms. This run uses the same normalised Rastrigin evaluator as :doc:`tutorial_map_elites`. It makes 24 sequential evaluations in steady-state mode. The evaluator returns one fitness objective to minimise and two descriptors in ``[0, 1]``. Use the search shortcut ----------------------- :func:`qdpy.simple.nslc` creates the novelty archive and its parent history. Choose a novelty threshold in the units of your descriptors. .. code-block:: python from qdpy.benchmarks import illumination_rastrigin_normalised from qdpy.simple import nslc, run search = nslc( dimension=3, budget=24, batch_size=6, fitness_domain=((0, 1),), features_domain=((0, 1), (0, 1)), k=3, threshold_novelty=0.15, ) result = run(search, illumination_rastrigin_normalised, seed=42, batch_mode=False) print("Evaluations", result.evaluations) print("Selected candidates", len(result.archive)) print("Candidates in parent history", len(result.archive.parents[0])) You can pass ``output_dir`` to record the run metadata. The novelty archive and its parent are available through the live optimiser. Use the logger in the next example to save both together. Build and log the search explicitly ----------------------------------- .. code-block:: python 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 Container, NoveltyArchive from qdpy.tools import open_pickle_file seed = 42 random.seed(seed) np.random.seed(seed) domains = dict( fitness_domain=((0.0, 1.0),), features_domain=((0.0, 1.0), (0.0, 1.0)), ) history = Container(name="history", **domains) archive = NoveltyArchive( parents=[history], k=3, threshold_novelty=0.15, disable_parents_pickling=False, **domains, ) search = RandomSearchMutPolyBounded( archive, budget=24, batch_size=6, dimension=3, optimisation_task="minimisation", name="nslc", ) evaluate = partial(illumination_rastrigin_normalised, nb_features=2) output = Path("results.nslc") output.mkdir(exist_ok=True) AlgorithmLogger(search, log_base_path=output, save_period=1, verbose=False, config={"seed": seed}) search.optimise(evaluate, batch_mode=False) results = open_pickle_file(output / "final.p") saved_archive = results["container"] print("Evaluations:", results["nb_evaluations"]) print("Selected candidates:", len(saved_archive)) print("Candidates in parent history:", len(saved_archive.parents[0])) print("Best fitness:", saved_archive.best_fitness.values) novelty, competition = saved_archive.novelty_local_competition( saved_archive.best, k=3, ignore_first=True, ) print("Best candidate novelty:", novelty) print("Best candidate local competition:", competition) ``disable_parents_pickling=False`` saves the parent history with the child. Use both when inspecting novelty or starting a new search from those candidates. Compare genomes, descriptors, fitness values, and archive membership when repeating the run. Evaluation durations vary between runs. Adaptive thresholds and indexed search -------------------------------------- The default NSLC archive uses a fixed novelty threshold. Use :class:`qdpy.containers.SelfAdaptiveNoveltyArchive` to estimate a threshold periodically from the diameter of a bounded sample of parent descriptors. The archive evaluates candidates against the same parent history and uses the same local replacement rule. .. code-block:: python from qdpy.containers import Container, SelfAdaptiveNoveltyArchive parent = Container() archive = SelfAdaptiveNoveltyArchive( parents=[parent], threshold_novelty=0.05, k=15, threshold_update_period=256, max_threshold_samples=2048, k_resolution=60000, seed=42, ) With this setup the first queries use the initial threshold. Each scheduled update measures the largest Euclidean distance in the sample, divides it by the square root of ``k_resolution``, and applies the configured threshold floor. Use ``indexed=True`` with either novelty archive to reuse a Euclidean tree. The index reads live descriptors on each query and uses parent order to break ties. Check its timing with your descriptor dimensions and archive size before enabling it for a long run.