# 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}")