Skip to content

Iterated prisoner's dilemma on a lattice with strategies of cooperation rounds

Argun's evolutionary iterated prisoner's dilemma on a lattice.

  • Class: IteratedLatticeDilemmaSystem
  • Import: from skcomplex.systems.social.cooperation import IteratedLatticeDilemmaSystem
  • Field: social/cooperation
  • Topics: lattice, games, cooperation, iterated, mutation
  • References:
    • Argun et al. 2021
    • Axelrod and Hamilton 1981

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.social.cooperation import IteratedLatticeDilemmaSystem

trajectory = IteratedLatticeDilemmaSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
shape (30, 30)
rounds 7
reward 0.6
sucker 1.5
temptation 0.0
punishment 1.0
mutation 0.01
extremes False
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space lattice
Laws update
Randomness unknown
Domains integer
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class IteratedLatticeDilemmaSystem(BaseSystem):
    """Argun's evolutionary iterated prisoner's dilemma on a lattice.

    Its strategies are of `n` cooperation rounds. The payoffs are years in prison, `T < R < P < S`, so lower is better.
    A strategy `n` cooperates until round `n` or until the opponent defects, and defects afterwards, so `n = 0` always
    defects and `n = N` always cooperates. Against a strategy `m` the best reply is `n = m - 1`. Every step each player
    plays the `N`-round game with its four von Neumann neighbours, adopts the strategy of the lowest total among itself
    and its neighbours, random on ties, and mutates to a random strategy with the probability `mu` (Argun et al. 2021
    sections 13.2 and 13.3). With every strategy allowed and `S = 1.5`, cooperative strategies prevail for `R` below
    about 0.5, all strategies coexist and cycle between about 0.5 and 0.75, and defecting strategies dominate above, so
    the default `R = 0.6` lies in the middle of the cycling regime, where a finite lattice is not absorbed by defection.
    With only the two extreme strategies, `extremes=True`, cooperation dominates at `R = 0.82`, coexists with defection
    at `R = 0.84`, and vanishes at `R = 0.86` (Exercise 13.3).

    The system has the strategies `players.strategy`.

    Args:
        shape:
            The number of rows and columns of the periodic lattice.

        rounds:
            The number `N` of rounds of a game.

        reward:
            The punishment `R` of mutual cooperation.

        sucker:
            The punishment `S` of a betrayed cooperator.

        temptation:
            The punishment `T` of a defector who betrays.

        punishment:
            The punishment `P` of mutual defection.

        mutation:
            The probability `mu` of a mutation to a random strategy per player and step.

        extremes:
            Whether only the strategies 0, always defect, and `N`, always cooperate, are allowed.

        random_state:
            The seed or generator of the initial strategies.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.social.cooperation import IteratedLatticeDilemmaSystem
        >>> IteratedLatticeDilemmaSystem((10, 10)).prepare().system_.elements[0].strategy.size
        100
    """

    field = 'social/cooperation'
    topics = ('lattice', 'games', 'cooperation', 'iterated', 'mutation')
    references = ('Argun et al. 2021', 'Axelrod and Hamilton 1981')
    until = 100
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (30, 30),
        *,
        rounds: int = 7,
        reward: float = 0.6,
        sucker: float = 1.5,
        temptation: float = 0.0,
        punishment: float = 1.0,
        mutation: float = 0.01,
        extremes: bool = False,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.shape = shape
        self.rounds = rounds
        self.reward = reward
        self.sucker = sucker
        self.temptation = temptation
        self.punishment = punishment
        self.mutation = mutation
        self.extremes = extremes
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components built from the model parameters."""
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        if self.extremes:
            strategies = self.rounds * rng.integers(2, size=tuple(self.shape)).astype(float)
        else:
            strategies = rng.integers(self.rounds + 1, size=tuple(self.shape)).astype(float)
        players = IteratedPrisonerElement(
            'players',
            strategy=strategies,
            rounds=self.rounds,
            reward=self.reward,
            sucker=self.sucker,
            temptation=self.temptation,
            punishment=self.punishment,
            mutation=self.mutation,
            extremes=float(self.extremes),
            space=LatticeSpace(tuple(self.shape), metric='manhattan'),
        )
        self.system_ = System(
            self.label,
            elements=[players],
            interactions=[IteratedDilemmaInteraction('play', target='players')],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • IteratedPrisonerElement, from Element

Interactions:

  • IteratedDilemmaInteraction, from Interaction
class IteratedPrisonerElement(Element):
    """Player of the lattice iterated prisoner's dilemma.

    It cooperates for a number of rounds and then defects. The strategy `n` of a player is the number of rounds it
    intends to cooperate, until round `n` included or until its opponent defects, after which it defects for the rest of
    the game. The payoffs are years in prison, so lower is better. The play, the revision, and the mutation are an
    `IteratedDilemmaInteraction` of the lattice, which `IteratedLatticeDilemmaSystem` adds.

    Args:
        label:
            The label of the player or the lattice group.

        strategy:
            The number `n` of intended cooperation rounds, from 0, always defect, to `rounds`, always cooperate.

        rounds:
            The number `N` of rounds of a game.

        reward:
            The punishment `R` of mutual cooperation.

        sucker:
            The punishment `S` of a cooperator who is betrayed.

        temptation:
            The punishment `T` of a defector who betrays.

        punishment:
            The punishment `P` of mutual defection.

        mutation:
            The probability `mu` that a player mutates to a random strategy at the end of a step.

        extremes:
            1 to allow only the strategies `0` and `N`, and 0 to allow every strategy from 0 to `N`.

        space:
            The space with one member per site, or `None` for one member.

    Examples:
        >>> from skcomplex.systems.social.cooperation import IteratedPrisonerElement
        >>> IteratedPrisonerElement('player').rounds
        7
    """

    def __init__(
        self,
        label: str,
        *,
        strategy: Annotated[object, NN] = 0.0,
        rounds: Annotated[int, NN] = 7,
        reward: Annotated[object, RealSet.closed_open(0, oo)] = 0.72,
        sucker: Annotated[object, RealSet.closed_open(0, oo)] = 1.5,
        temptation: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
        punishment: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
        mutation: Annotated[object, RealSet.closed(0, 1)] = 0.01,
        extremes: Annotated[object, RealSet.closed(0, 1)] = 0.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.strategy = strategy
        self.rounds = rounds
        self.reward = reward
        self.sucker = sucker
        self.temptation = temptation
        self.punishment = punishment
        self.mutation = mutation
        self.extremes = extremes

class IteratedDilemmaInteraction(Interaction):
    """Play of the iterated prisoner's dilemma between neighbours.

    The play is followed by revision to the best strategy, and mutation. It is a stochastic local rule of the
    strategies over the cells within two von Neumann steps of a player. Every step each player plays the `N`-round
    prisoner's dilemma with its four von Neumann neighbours and sums its years in prison. A player of strategy `n`
    against one of strategy `m < n` gets `m R + S + (N - m - 1) P`, the other `m R + T + (N - m - 1) P`, and two players
    of strategy `n` get `n R + (N - n) P`. Every player then adopts the strategy of the lowest score among itself and
    its four neighbours, a random one of them on ties, and finally mutates to a uniformly drawn strategy with the
    probability `mu`.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the players on the lattice.

    Examples:
        >>> from skcomplex.systems.social.cooperation import IteratedDilemmaInteraction
        >>> IteratedDilemmaInteraction('play', target='players').target
        'players'
    """

    def __init__(self, label: str, *, target: Role) -> None:
        super().__init__(label)
        self.target = target

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the play, the revision, and the mutation of the strategies as the law.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        strategy = context.target.strategy
        self.laws_: list[BaseLaw] = [
            UpdateLaw(strategy, self._play, neighborhood=DIAMOND, label=self.label, stochastic=True),
        ]

    @staticmethod
    def get_years(
        own: Array,
        other: Array,
        *,
        rounds: float | Array,
        reward: float | Array,
        sucker: float | Array,
        temptation: float | Array,
        punishment: float | Array,
    ) -> Array:
        """Return the years in prison of a player of strategy `n` against strategy `m`.

        The game has `N` rounds.

        Args:
            own:
                The strategies `n` of the players.

            other:
                The strategies `m` of their opponents.

            rounds:
                The number `N` of rounds.

            reward:
                The punishment `R` of mutual cooperation.

            sucker:
                The punishment `S` of a betrayed cooperator.

            temptation:
                The punishment `T` of a defector who betrays.

            punishment:
                The punishment `P` of mutual defection.

        Returns:
            years:
                The years in prison of every player.

        Examples:
            >>> import numpy as np
            >>> from skcomplex.systems.social.cooperation import IteratedDilemmaInteraction
            >>> years = IteratedDilemmaInteraction.get_years(
            ...     np.arange(11.0), np.full(11, 6.0), rounds=10, reward=0.5, sucker=1.5, temptation=0.0, punishment=1.0
            ... )
            >>> int(np.argmin(years))
            5
        """
        first = np.minimum(own, other)
        betrayal = np.where(own < other, temptation, sucker)
        mixed = first * reward + betrayal + (rounds - first - 1) * punishment
        return np.where(own == other, own * reward + (rounds - own) * punishment, mixed)

    def _play(
        self,
        *values: Array,
        rounds: float | Array,
        reward: float | Array,
        sucker: float | Array,
        temptation: float | Array,
        punishment: float | Array,
        mutation: float | Array,
        extremes: float | Array,
        random: Array,
        **kwargs: object,
    ) -> Array:
        """Return the strategies after the play, the revision, and the mutation.

        The play is against the neighbours.
        """
        cells = dict(zip(DIAMOND.offsets, values, strict=True))
        payoffs = {'rounds': rounds, 'reward': reward, 'sucker': sucker, 'temptation': temptation}

        def score(center: Pair) -> Array:
            own = cells[center]
            return sum(
                (
                    IteratedDilemmaInteraction.get_years(
                        own,
                        cells[center[0] + dx, center[1] + dy],
                        punishment=punishment,
                        **payoffs,
                    )
                    for dx, dy in VON_NEUMANN
                ),
                start=np.zeros_like(own),
            )

        candidates = (ORIGIN, *VON_NEUMANN)
        scores = np.stack([score(offset) for offset in candidates])
        strategies = np.stack([cells[offset] for offset in candidates])
        tied = np.isclose(scores, scores.min(axis=0))
        mutation = np.broadcast_to(mutation, random.shape)
        choice = np.clip((random - mutation) / np.maximum(1 - mutation, 1e-12), 0.0, 1.0 - 1e-12)
        rank = (choice * tied.sum(axis=0)).astype(int)
        picked = np.argmax(np.cumsum(tied, axis=0) > rank, axis=0)
        revised = np.take_along_axis(strategies, picked[None], axis=0)[0]
        draw = random / np.maximum(mutation, 1e-12)
        mutant = np.where(
            np.broadcast_to(extremes, random.shape) > 0,
            np.where(draw < HALF, 0.0, rounds),
            np.minimum(np.floor(draw * (np.asarray(rounds) + 1)), rounds),
        )
        return np.where(random < mutation, mutant, revised)