Generational Adversarial MAP-Elites

In a game, the same strategy can have different fitness and behaviour against different opponents. qdpy.algorithms.game_search() keeps a separate archive for each opponent and alternates which side can propose new strategies. The example coevolves probability distributions over rock, paper, and scissors.

Run from the repository root with the base package.

python examples/game_rock_paper_scissors.py
# SPDX-License-Identifier: LGPL-3.0-or-later
## @file
# @brief Coevolve mixed strategies for rock-paper-scissors with GAME.

"""Use win and draw probabilities as opponent-specific behaviour descriptors."""

from itertools import product

import numpy as np

from qdpy.algorithms import MatchResult, game_search


def probabilities(genome):
    """Normalise three strategy weights, using uniform play for a zero vector."""
    weights = np.asarray(genome)
    total = weights.sum()
    return weights / total if total else np.full(3, 1 / 3)


def play(red, blue):
    """Return each side's expected payoff and its win and draw probabilities."""
    red = probabilities(red)
    blue = probabilities(blue)
    red_win = red @ np.roll(blue, 1)
    blue_win = blue @ np.roll(red, 1)
    draw = red @ blue
    payoff = red_win - blue_win
    return MatchResult((payoff, -payoff), ((red_win, draw), (blue_win, draw)))


def score_against_panel(round_result):
    """Measure coverage and worst payoff for the uniform elite mixture."""
    panel = np.eye(3)
    side = 0 if round_result.side == "red" else 1
    payoffs = np.empty((len(round_result.representatives), len(panel)))
    cells = set()
    for row, opponent_index in product(range(len(payoffs)), range(len(panel))):
        strategy = round_result.representatives[row].genome
        opponent = panel[opponent_index]
        match = play(strategy, opponent) if side == 0 else play(opponent, strategy)
        payoffs[row, opponent_index] = match.fitness[side]
        cell = tuple(min(7, int(value * 8)) for value in match.descriptors[side])
        cells.add(cell)
    return len(cells), float(payoffs.mean(axis=0).min())


if __name__ == "__main__":
    result = game_search(play, dimensions=(3, 3), rounds=2, matches_per_round=24,
                         task_count=3, cells_per_task=4, seed=42)
    print(f"Search matches: {result.search_attempts}")
    print(f"Bootstrap matches: {result.tournament_attempts}")
    for round_result in result.rounds:
        coverage, worst_payoff = score_against_panel(round_result)
        print(f"{round_result.side.title()}: {len(round_result.representatives)} strategies, "
              f"{coverage}/64 panel cells, worst panel payoff {worst_payoff:.3f}")

Implement play(red, blue) to receive two immutable real-valued genomes in Red and Blue order. Return a qdpy.algorithms.MatchResult with both fitness values and both descriptor vectors in that order. Both players maximise their scores. Here the expected payoffs sum to zero and each behaviour vector contains probabilities of winning and drawing. Keep each side’s descriptor length fixed across matches, including when it is the opponent.

GAME starts with Red against sampled Blue strategies. It uses Euclidean distance to maintain distinct behaviours per opponent. At each round boundary, it clusters the elites and selects the next opponents. Each old and new representative pair plays once to collect measurements for the archive in the next round.

Read result.rounds to inspect either side. Within each round, opponents[i] and elites_by_opponent[i] refer to the same opponent. Use a common panel to compare repertoires. The example tests the uniform mixture of selected strategies against the three pure strategies and reports the worst expected payoff.

The example spends 48 search matches and nine bootstrap matches. The runner reports these budgets separately and counts failures as attempts. With four opponents on each side, a tournament adds 16 matches.

The growing archive and tournament follow Anne et al.. QDpy takes descriptors directly from the evaluator. See Constrained search and GAME pilots for the random-proposal control.