Counterexamples, design symmetries, and learned policy variation ================================================================ The example checks schedules under single-job delays, searches networks with interchangeable internal nodes, and compares three ways to mutate a continuous feedback policy. See :doc:`verified_symmetry_manifold_benchmarks` for the workloads and per-seed results. .. code-block:: bash python -m pip install . python -I examples/verified_symmetry_manifold.py --task base python -m pip install ".[rl]" python -I examples/verified_symmetry_manifold.py --task control .. _counterexample-guided-qd: Counterexample-guided MAP-Elites -------------------------------- Separate nominal fitness and descriptors from the requirement a design must meet under every scenario. For instance, choose a buffer size and processing capacity, then check whether the station keeps up under each load spike in a declared set. .. code-block:: python from qdpy.algorithms import CounterexampleMAPElites from qdpy.containers import Grid archive = Grid( shape=(5,), features_domain=((0, 5),), fitness_domain=((0., 1.),), fitness_weights=(1.,), ) def measure(genome): capacity, buffer = genome return (1. / (capacity + buffer),), (buffer,) def verify(genome, load): capacity, buffer = genome return load <= capacity + buffer def propose(rng): return int(rng.integers(1, 6)), int(rng.integers(5)) def mutate(parent, rng): capacity, buffer = parent if rng.random() < .5: return int(rng.integers(1, 6)), buffer return capacity, int(rng.integers(5)) search = CounterexampleMAPElites( archive, (2, 3, 4, 5, 6), initial_scenarios=(2,), propose=propose, mutate=mutate, measure=measure, verify=verify, seed=4, ) stats = search.run(proposals=80, checks=160) if search.pending_genome is not None: stats = search.run(proposals=0, checks=5) print(search.counterexamples, stats.checks, len(archive)) Start with a few known cases. When a candidate fails on a new case, that case joins the screening set and runs before unseen cases on future candidates. The scalar fitness and descriptors from ``measure`` determine whether a candidate could improve the archive. Such candidates enter the archive after every scenario passes. The two budgets limit proposals and calls to ``verify`` separately. A candidate that runs out of checks stays pending. Returning ``None`` from ``verify`` also pauses that candidate. The next call retries the pending case and can spend its entire budget on that candidate. Certification applies to the declared finite scenario universe. This workflow adapts the proposal–verification loop from counterexample-guided synthesis. `SolarLezama2006 `__ .. _symmetry-aware-map-elites: Symmetry-aware MAP-Elites ------------------------- Use a canonicalization callback when several representations denote the same evaluated design. In this example, two machines have identical costs and capacities, so exchanging their labels preserves the total quality and the load imbalance. .. code-block:: python from qdpy.algorithms import SymmetryAwareMAPElites from qdpy.containers import Grid archive = Grid( shape=(5,), features_domain=((0, 5),), fitness_domain=((0., 1.),), fitness_weights=(1.,), ) def propose(rng): return tuple(map(int, rng.integers(5, size=2))) def mutate(genome, rng): child = list(genome) child[rng.integers(2)] = int(rng.integers(5)) return tuple(child) def evaluate(genome): first, second = genome return (min(1., (first + second) / 6.),), (abs(first - second),) search = SymmetryAwareMAPElites( archive, canonicalize=sorted, propose=propose, mutate=mutate, seed=4, ) stats = search.run(evaluate, proposals=60, max_evaluations=15) print(stats.proposals, stats.evaluations, stats.duplicates) Each proposal becomes a canonical tuple before evaluation. Its entries must be hashable. The callback must return the same tuple for all equivalent designs, and applying it twice must give the same result. Use a fixed, deterministic evaluator because the search caches successfully measured representatives across calls. To test a symmetry, evaluate a few designs before and after relabeling. Check fitness, descriptors, and feasibility. Preserve labels such as fixed terminals when they have physical meaning. The network example checks the full small design space against its canonical representatives. `McKay2014 `__ .. _policy-manifold-search: Policy Manifold Search ---------------------- PoMS trains an autoencoder on the policy vectors already in the archive. A parent with low reconstruction error is mutated in the latent space. The decoder Jacobian transforms the variation, and the decoder maps the child back into policy coordinates. A parent with high reconstruction error receives direct Gaussian parameter mutation. .. code-block:: python import numpy as np from qdpy.algorithms import PolicyManifoldSearch from qdpy.containers import Grid def rollout(genome): bias, feedback, phase_weight, damping = genome state = velocity = effort = 0. for phase in np.linspace(0., 2. * np.pi, 16): action = np.tanh(bias + feedback * state + phase_weight * np.sin(phase) + damping * velocity) velocity = .8 * velocity + .1 * action state += velocity effort += action ** 2 return (1. / (1. + effort),), (float(np.tanh(state)),) archive = Grid( shape=(9,), features_domain=((-1., 1.),), fitness_domain=((0., 1.),), fitness_weights=(1.,), ) search = PolicyManifoldSearch( archive, 96, dimension=4, ind_domain=(-1., 1.), batch_size=8, latent_size=2, hidden_size=12, min_elites=3, fit_interval=2, seed=4, ) search.optimise(rollout) print(search.operator_counts, search.model_fits, search.model_fit_seconds) The fitness and descriptor always come from evaluating the proposed policy. The archive uses scalar fitness to resolve a cell conflict. The reconstruction threshold is the mean error on the archive at the last fit. ``sigma`` is expressed as a fraction of the configured parameter range. PoMS imports PyTorch when you create the optimizer. `Rakicevic2021 `__