Prisoner’s Dilemma

This example models a population of agents repeatedly choosing between two strategies: cooperate or defect, as in the classic Prisoner’s Dilemma game.

It demonstrates how revision dynamics can drive the population toward defection, even when mutual cooperation would be socially optimal.

 1import numpy as np
 2import matplotlib.pyplot as plt
 3from popgames import (
 4    SinglePopulationGame,
 5    PayoffMechanism,
 6    PoissonRevisionProcess,
 7    Simulator,
 8)
 9from popgames.revision_protocol import Softmax
10
11T, R, P, S = 3, 2, 1, 0 # Prisoner's dilemma parameters
12                        # s.t. T > R > P > S
13                        # https://en.wikipedia.org/wiki/Prisoner%27s_dilemma
14def fitness_function(x):
15    return np.dot(
16        np.array([[R, S], [T, P]]),
17        x
18    )
19
20population_game = SinglePopulationGame(
21    num_strategies=2,
22    fitness_function=fitness_function,
23)
24
25payoff_mechanism = PayoffMechanism(
26    h_map=fitness_function,
27    n=2,
28)
29
30revision_process = PoissonRevisionProcess(
31    Poisson_clock_rate=1,
32    revision_protocol=Softmax(0.1),
33)
34
35sim = Simulator(
36    population_game=population_game,
37    payoff_mechanism=payoff_mechanism,
38    revision_processes=revision_process,
39    num_agents=1000
40)
41
42x0 = np.array([0.5, 0.5]).reshape(2, 1)
43sim.reset(x0=x0)
44out = sim.run(T_sim=30)
45
46plt.figure(figsize=(3, 3))
47plt.plot(out.t, out.x[0, :], label='Cooperators')
48plt.plot(out.t, out.x[1, :], label='Defectors')
49plt.xlabel('Time')
50plt.ylabel('Portion of agents choosing each strategy')
51plt.legend()
52plt.grid()
53plt.tight_layout()
54plt.show()
../_images/prisoners_dilemma.png