# SPDX-License-Identifier: LGPL-3.0-or-later ## @file # @brief Audit a noisy search on fresh rollouts and snapshot the audited grid. """Repeat measurements during search and audit the elites independently.""" from pathlib import Path import random import numpy as np from qdpy.algorithms import UncertainMAPElites, reevaluate_archive from qdpy.containers import Grid from qdpy.results import export_grid, load_grid def evaluate(candidate, seed): """Use an independent random generator for each seeded rollout.""" rng = np.random.default_rng(seed) x, descriptor = candidate reward = 0.15 + 0.7 * (1.0 - abs(x - 0.7)) + rng.uniform(-0.04, 0.04) measured = 0.05 + 0.90 * descriptor + rng.uniform(-0.04, 0.04) return (reward,), (measured,) def main(): """Separate search and audit budgets, then report from the audited snapshot.""" random.seed(42) np.random.seed(42) archive = Grid(shape=8, max_items_per_bin=3, fitness_domain=((0.0, 1.0),), features_domain=((0.0, 1.0),)) search = UncertainMAPElites( archive, budget=64, dimension=2, policy="extract_me", sampling_size=16, initial_samples=2, optimisation_task="maximisation", seed=42, ) search.optimise(evaluate, seeded=True) audit = reevaluate_archive(archive, evaluate, samples=8, seed=43) path = Path("first-noisy/audited-archive.json") path.parent.mkdir() digest = export_grid(audit.archive, path) restored = load_grid(path, expected_sha256=digest) print(f"Search rollouts: {search.nb_evaluations}") print(f"Independent audit rollouts: {audit.rollouts}") print(f"Audited cells: {restored.filled_bins}/{restored.shape[0]}") print(f"Audited QD score: {restored.qd_score():.6f}") if __name__ == "__main__": main()