This research explores whether a Q-learning agent can discover the right strategy to play against different types of opponents in the iterated Prisoner’s Dilemma, using only the points it earns each round. The agent is trained separately against four fixed strategies: Always Cooperate, Always Defect, Tit-for-Tat, and Grudger. The results show that the agent does not simply learn to cooperate, instead, it learns the optimal response depending on who it is playing against. It converges to near-complete cooperation only against Tit-for-Tat, and to defection against the other three. The most interesting case is Grudger, where the agent consistently fails to cooperate not because of a flaw in learning, but because its state representation cannot track enough history to recognize what Grudger is doing. This shows that what the agent learns is directly shaped by what it is able to observe.

The Prisoner’s Dilemma

The Game

Setting Up the Problem

The Prisoner’s Dilemma is a two-player game where each player chooses to either cooperate or defect each round, and the points they receive depend on both players’ choices. In this experiment, cooperate is encoded as 0 and defect as 1. The payoff structure works like this:
- Both cooperate → each gets 3 points
- Both defect → each gets 1 point
- One defects, one cooperates → defector gets 5, cooperator gets 0
These values are not arbitrary — they need to satisfy two conditions from game theory for this to qualify as a proper Prisoner’s Dilemma.
Each outcome has a standard name. T (Temptation) = 5 is what you earn by betraying someone who trusted you. R (Reward) = 3 is what both players earn from mutual cooperation. P (Punishment) = 1 is what both players earn when both betray. S (Sucker) = 0 is what you earn when you cooperate but get betrayed.
The first condition is T > R > P > S, which just means these four values have to rank in this exact order: 5 > 3 > 1 > 0. This confirms that betrayal is always individually tempting (T is the highest payoff), but mutual punishment is still worse than mutual cooperation (P < R). If this condition were not met — say, if mutual defection gave more points than mutual cooperation — then the game would not actually create a dilemma. Everyone would just defect and be fine with it.
The second condition is 2R > T + S, which means 2(3) > 5 + 0, or 6 > 5. This one is specific to the repeated version of the game. It ensures that two players who cooperate every round (earning 3 + 3 = 6 together per round) do better collectively than two players who take turns exploiting each other (earning 5 + 0 = 5 together per round, alternating who gets the 5). Without this condition, players could just alternate who betrays whom and still come out ahead — which would make sustained cooperation pointless. This condition is what makes repeated cooperation genuinely worth pursuing.
Display the payoff matrix for all combinations of cooperation and defection:
Out[]=
P2: Cooperate
P2: Defect
P1: Cooperate
3, 3
0, 5
P1: Defect
5, 0
1, 1
Reading any single row, a player always does better by defecting: if P2 cooperates, defecting yields 5 instead of 3; if P2 defects, defecting yields 1 instead of 0. Defection is a dominant strategy in a single round.

Why Cooperation Is Hard

The reason this game is interesting is that individual logic and collective logic pull in opposite directions. If you look at the payoff matrix row by row, defecting always gives a better outcome for you personally, whether the opponent cooperates or not. If P2 cooperates, you get 5 by defecting instead of 3 by cooperating. If P2 defects, you get 1 by defecting instead of 0 by cooperating. So no matter what, defecting is individually the better choice.
But if both players follow that same logic, they both defect and each earns only 1 point , worse than the 3 they would have gotten from mutual cooperation. This gap is the core tension of the game. The outcome where both players defect is called the Nash equilibrium, neither player can do better by switching unilaterally. The outcome where both cooperate is called the Pareto optimum, both players are better off, and you cannot improve one player’s result without hurting the other. In a single round, the Nash equilibrium always wins because there is no reason to trust the other player. But in a repeated game, future rounds change things , if the opponent can retaliate next round, defecting now might cost more than it earns. The question this research explores is whether a learning agent can figure this out on its own, without being told any of the above.

A Tournament of Fixed Strategies

The Four Opponents

What Each Strategy Does

Before training the learning agent, we first look at how four well-known fixed strategies perform against each other. These four were chosen because they represent a full range of behavior — from always trusting to never trusting — and are the most commonly studied strategies in repeated Prisoner’s Dilemma research [1].
Always Cooperate cooperates every round no matter what. If both players use this strategy, they both earn 3 points every round — the best collective outcome. But because it never retaliates, an opponent can defect every single round and earn 5 points each time while Always Cooperate earns 0. It has no way to protect itself.
Always Defect defects every round no matter what. Against an opponent who cooperates, it earns the maximum 5 points every round. Against an opponent who also defects, both players earn 1 point every round — neither can do better by switching, which is the Nash equilibrium. It is the safest strategy in the sense that no opponent can take advantage of it, but it also prevents any possibility of mutual cooperation.
Tit-for-Tat starts by cooperating, then copies whatever the opponent did last round. If you cooperated, it cooperates back. If you defected, it retaliates the very next round — but only for one round. The moment you cooperate again, it forgives and returns to cooperation. This makes it hard to exploit: any defection is immediately punished, but the punishment stops as soon as you stop defecting. It is the most famous strategy from Robert Axelrod’s tournaments and is known for being firm but fair [1].
Grudger also starts by cooperating, but if the opponent defects even once — at any point in the game — Grudger switches to permanent defection for all remaining rounds. Unlike Tit-for-Tat, it never forgives. One betrayal is enough to end all cooperation forever.

Simulating the Tournament

To see how these strategies compare, we simulate all 16 possible pairings for 100 rounds each and record the average score per round for the row player. In the match function, each strategy receives the list of what the opponent has done so far and uses that to decide its next action.
Simulate the tournament across all 16 strategy pairings and display the average scores:
In[]:=
alwaysCooperate[hist_] := 0​​alwaysDefect[hist_] := 1​​titForTat[hist_] := If[hist == {}, 0, Last[hist]]​​grudger[hist_] := If[MemberQ[hist, 1], 1, 0]​​​​runMatch[s1_, s2_, nRounds_] := Module[​​ {h1 = {}, h2 = {}, total = 0, a1, a2},​​ Do[​​ a1 = s1[h1]; a2 = s2[h2];​​ total += First[payoff[a1, a2]];​​ AppendTo[h1, a2]; AppendTo[h2, a1],​​ {nRounds}];​​ N[total/nRounds]​​]​​​​strategies = {alwaysCooperate, alwaysDefect, titForTat, grudger};​​names = {"Always C", "Always D", "Tit-for-Tat", "Grudger"};​​scores = Table[runMatch[strategies[[i]], strategies[[j]], 100], {i,4},{j,4}];​​​​Grid[​​ Prepend[​​ MapThread[Prepend, {scores, names}],​​ Prepend[names, ""]​​ ],​​ Frame -> All​​]
Out[]=
Always C
Always D
Tit-for-Tat
Grudger
Always C
3.
0.
3.
3.
Always D
5.
1.
1.04
1.04
Tit-for-Tat
3.
0.99
3.
3.
Grudger
3.
0.99
3.
3.
Always Defect earns 5.0 per round against Always Cooperate by exploiting it every round. But against Tit-for-Tat and Grudger, it only earns about 1.04, both strategies retaliate on round 2 and stay in mutual punishment for the rest of the game. Tit-for-Tat and Grudger both reach mutual cooperation (3.0 per round) against each other and against Always Cooperate. These results show that the best strategy depends entirely on who the opponent is, which is exactly what we want the learning agent to discover on its own.

The Q - Learning Agent

What Is Q - Learning?

Learning from Rewards

Q-learning is a type of reinforcement learning, so it helps to understand what reinforcement learning is first. Reinforcement learning is a framework where an agent learns how to behave by interacting with an environment and receiving feedback in the form of rewards or penalties. The agent is not told what the correct action is — it has to figure that out on its own by trying different actions and observing what happens. Over time, it learns to take actions that lead to higher rewards. This is different from other types of machine learning where the algorithm is trained on labeled examples. In reinforcement learning, there are no examples — just experience.
The three core components of reinforcement learning are the agent, the environment, and the reward signal. The agent is the decision-maker. The environment is whatever the agent is interacting with — in this experiment, the environment is the Prisoner’s Dilemma game and the opponent. The reward signal is the feedback the agent receives after each action — here, it is simply the points earned from the payoff matrix each round. The agent’s goal is to maximize the total reward it accumulates over time, not just the reward from a single round.
Q-learning is one specific algorithm within reinforcement learning [2]. What makes it useful for this experiment is that it is model-free — the agent does not need to know anything about how the environment works in advance. It does not know the payoff matrix, it does not know the opponent’s strategy, and it does not know the rules of the game. It only knows what action it took and how many points it earned. The main structure it uses to store and organize what it has learned is called a Q-table — a grid where each row represents a situation the agent could be in (called a state) and each column represents a possible action. Each cell holds a number called a Q-value, which estimates how much total long-run reward the agent can expect from taking that action in that state. At the start of training, all Q-values are 0 because the agent knows nothing. As it plays more rounds and receives more rewards, the values gradually update and become more accurate. By the end of training, the Q-table essentially stores everything the agent has learned about how to play the game.

Defining the States

For the agent to use a Q-table, we need to define what a “state” means , in other words, how the agent describes the current situation it is in. In this experiment, the state is defined by what both players did in the previous round: (my last action, opponent’s last action). This gives four possible states, encoded as a single number using s = 2 × (my last action) + (opponent’s last action):
- State 0: both cooperated last round , (C, C)
- State 1: I cooperated, opponent defected , (C, D)
- State 2: I defected, opponent cooperated , (D, C)
- State 3: both defected last round , (D, D)
- State 4: the very first round, when there is no history yet
So the Q-table has 5 rows and 2 columns , 10 numbers total. This one-step memory is the standard minimal representation for playing against memory-one opponents in the iterated Prisoner’s Dilemma [1, 3].

The Bellman Update Rule

After each round, the agent updates the Q-value for whatever (state, action) pair it just experienced. Before looking at the exact formula, here is a pseudocode — a plain-language description of the logic the agent follows each round:
Initialize Q - table with all zeros (5 states * 2 actions)
Set learning rate α = 0.05, discount factor γ = 0.99, exploration rate ε = 0.
​
For each episode :
Reset action histories for both players
​
For each round :
Observe current state s (based on last actions of both players)
​
If random number < ε :
Choose a random action (exploration)
Else :
Choose action with highest Q - value in state s (exploitation)
​
Play the action, receive reward r from payoff matrix
Observe next state s'
​
Update Q - value :
Q (s, a) ← Q (s, a) + α · [ r + γ · max Q (s', a') − Q (s, a) ]
​
Decay ε (reduce exploration rate slightly)
The update step in the pseudocode is called the Bellman equation [2]:
Q(s, a) ← Q(s, a) + α · [ r + γ · max Q(s’, a’) − Q(s, a) ]
This formula looks complex but each piece has a clear meaning:
- Q(s, a) is the current Q-value , what the agent currently thinks about taking action a in state s.
- r is the reward just received this round (0, 1, 3, or 5 from the payoff matrix).
- γ · max Q(s’, a’) is the best Q-value available in the next state s’, multiplied by the discount factor γ. This is the estimated future value , the agent is not just thinking about this round, but about what comes next.
- r + γ · max Q(s’, a’) combines the immediate reward and the expected future reward into a new, updated estimate of how good this action was.
- [ r + γ · max Q(s’, a’) − Q(s, a) ] is the gap between the new estimate and the current one. If this is positive, the action turned out better than expected. If negative, it was worse.
- α = 0.05 is the learning rate, which controls how much the Q-value shifts each update. A small α means the agent adjusts slowly and does not overreact to a single round.
Over many rounds and many episodes, these small updates accumulate until the Q-values accurately reflect the true long-run value of each action in each state.

Exploration vs . Exploitation

There is a natural tension in learning: should the agent use what it already knows, or try something new and potentially discover something better? This is called the exploration-exploitation tradeoff.
The agent handles this with an ε-greedy policy. Each round, it generates a random number between 0 and 1. If the number is below ε, the agent takes a random action , this is exploration. If the number is above ε, the agent picks the action with the highest Q-value for the current state , this is exploitation.
ε starts at 0.3, meaning 30% of actions are random at the beginning of training. It decreases by a factor of 0.997 each episode, eventually settling at a minimum of 0.02. This means the agent explores a lot early on when it does not know anything, and gradually shifts toward using what it has learned as training progresses. The 0.02 floor means the agent keeps exploring a little even at the end of training, which is why the long-run cooperation rate never quite reaches 1.0.
The discount factor γ = 0.99 controls how much the agent values future rewards compared to immediate ones. A value close to 1 means the agent is very patient , it weighs future rounds almost as much as the current one. This is important because it makes the agent recognize that cooperating for 3 points now, while keeping the opponent cooperative for the next 198 rounds, is better than defecting for 5 points once and triggering retaliation. Setting γ = 0.99 also makes the game effectively infinite-horizon from the agent’s perspective, since it does not observe which round number it is on and cannot count down toward the end.

Training Against Tit-for-Tat

To check that the agent is working correctly before running the full experiment, we first train it against only Tit-for-Tat for 500 episodes of 200 rounds each, then look at the final Q-table. One important detail: the opponent function receives myHist , the agent’s own action history , because Tit-for-Tat mirrors what the agent did last round, not its own history.
Train the Q-learning agent against Tit-for-Tat and display the resulting Q-table:
In[]:=
qTable = ConstantArray[0., {5, 2}];​​alpha = 0.05; gamma = 0.99; eps = 0.3;​​​​Do[​​ myHist = {}; oppHist = {};​​ Do[​​ state = If[myHist == {}, 4, 2*Last[myHist] + Last[oppHist]];​​ action = If[RandomReal[] < eps, RandomInteger[1],​​ First@Ordering[qTable[[state+1]], -1] - 1];​​ oppAction = titForTat[myHist];​​ r = First[payoff[action, oppAction]];​​ AppendTo[myHist, action]; AppendTo[oppHist, oppAction];​​ nextState = 2*Last[myHist] + Last[oppHist];​​ qTable[[state+1, action+1]] +=​​ alpha*(r + gamma*Max[qTable[[nextState+1]]] - qTable[[state+1, action+1]]),​​ {200}];​​ eps = Max[0.02, eps*0.997],​​ {500}];​​​​Grid[​​ Prepend[​​ MapThread[Prepend,​​ {qTable,​​ {"(C,C) , state 0", "(C,D) , state 1",​​ "(D,C) , state 2", "(D,D) , state 3",​​ "Initial , state 4"}}],​​ {"State", "Q(Cooperate)", "Q(Defect)"}],​​ Frame -> All​​]

Reading the Q - Table

The Q-table shows what the agent learned after 500 episodes of playing against Tit-for-Tat.
In states (C,C) and (C,D) — the top two rows — cooperation and defection have very similar Q-values (both around 299–300). The difference is small, but cooperation is consistently slightly higher. This reflects the fact that against Tit-for-Tat, staying cooperative keeps the game in a high-reward cycle, while defecting risks triggering retaliation.
In state (D,C), the gap is a bit wider — cooperation is around 297 and defection around 294. When the agent defected last round but the opponent cooperated, the agent has learned it is better to return to cooperation than to keep defecting.
In state (D,D), the gap is the clearest — cooperation is around 297 while defection is around 268. This is the mutual punishment state, and the agent has learned that cooperating is by far the better way to escape it. If the agent defects again, it stays stuck in low-reward mutual defection. If it cooperates, Tit-for-Tat forgives and both players return to earning 3 per round.
The initial state (state 4) is the most revealing: cooperation has a Q-value of about 300, while defection is only about 224. This is the largest gap in the entire table. The agent strongly prefers to cooperate on the very first round, before any history exists. This makes sense — cooperating on round 1 establishes a cooperative dynamic with Tit-for-Tat from the start, while defecting first triggers immediate retaliation and forces the agent to recover from state (D,D). The agent has learned that starting cooperatively is the best long-run opening move. The cooperation rate staying at ≈ 0.97 rather than 1.0 is entirely explained by the ε = 0.02 exploration floor causing occasional random defections throughout training.

Learning Curves

Tracking Behavior Over Training

How We Measure Learning

To see how the agent’s behavior changes over training, we track its cooperation rate per episode , the fraction of rounds in that episode where the agent chose to cooperate. A rate of 1.0 means it cooperated every round. A rate of 0.0 means it defected every round.
The agent is trained separately against each of the four opponents for 500 episodes of 200 rounds each. Each curve is smoothed with a 20-episode moving average to reduce noise from early exploration. If the cooperation rate rises over time, the agent learned that cooperating pays off against that opponent. If it drops to zero, the agent learned that defecting is better.
One thing to keep in mind before reading the graph: a cooperation rate of 0% is not always a bad sign. Against Always Defect, cooperating earns 0 while defecting earns 1 , defection is clearly the right choice. Against Always Cooperate, defecting earns 5 while cooperating only earns 3 , again, defection is better. If the agent ends up with 0% cooperation against these two, it means it correctly figured out the payoff structure, not that it failed.
Visualize how the agent’s cooperation rate changes over training for each opponent:

Interpreting the Results

Against Tit-for-Tat , Learning to Cooperate

Against Tit-for-Tat (blue), the cooperation rate starts low and gradually rises over the first 200 or so episodes, eventually stabilizing around 0.97. This is the result we expect from the game-theoretic analysis: cooperation is the correct long-run strategy against Tit-for-Tat.
The reason it takes time to converge is that early in training, ε is high and the agent takes many random actions. Sometimes it accidentally defects, Tit-for-Tat retaliates, and both players get stuck in a punishment cycle for several rounds. The agent earns low rewards during those cycles, and over time the Bellman updates encode this pattern , defecting against a retaliating opponent is a bad idea in the long run. After enough episodes, the Q-values settle on a clear message: cooperating earns more against this opponent.
The rate stops at around 0.97 rather than 1.0 because of the ε = 0.02 floor. Even in late training, 2% of actions are still random, so occasional accidental defections happen throughout. This is expected and does not indicate a problem with convergence.

Against Always Cooperate and Always Defect , Rational Defection

Against Always Defect and Always Cooperate (red and green), the cooperation rate drops quickly to near zero. This is the correct outcome.
Against Always Defect, the agent learns quickly that cooperating always earns 0 while defecting always earns 1. There is no incentive to cooperate because the opponent will defect regardless, and the Q-values reflect this within the first few dozen episodes.
Against Always Cooperate, the agent learns that defecting earns 5 per round while cooperating only earns 3, and the opponent never retaliates no matter what. So the agent defects every round. This might look like the agent is being unfair, but from the payoff structure’s point of view, defection is the rational best response. The fact that the agent figures this out is actually a sign that the learning algorithm is working correctly.

Against Grudger,When Memory Matters

Against Grudger (orange), the cooperation rate also falls to near zero , and this is the most interesting result in the experiment, for a different reason than the other three.
The best strategy against Grudger is actually to never defect, even once. If the agent cooperates every single round, Grudger cooperates back every round, and the agent earns 3 points per round for the entire episode , the same result as against Tit-for-Tat. The problem is that this requires perfect cooperation throughout all 200 rounds of every episode, which the agent cannot guarantee.
In early episodes, ε is high and the agent takes random actions. At some point, it will randomly defect. The moment that happens, Grudger switches to permanent defection and never cooperates again for the rest of that episode. From that point on, the game is stuck in state (D, D) , both players defecting every round.
Here is the key issue: from the agent’s point of view, a Grudger that has switched to permanent defection looks completely identical to Always Defect. The agent only sees the current state, what both players did last round, and has no way of knowing whether the opponent has always been defecting, or was cooperative until it got triggered. This is called partial observability: the agent’s state representation cannot capture enough history to tell these two situations apart.
So the agent does what makes sense given what it can see: it treats Grudger like Always Defect and defects every round. This is not a learning failure , it is actually the correct response given the agent’s limited view of the game. But it shows that the one-step state representation has a real limitation. It works well against opponents whose behavior only depends on the last round (Always Cooperate, Always Defect, Tit-for-Tat), but it fails against Grudger, whose behavior depends on the entire history of the game [3].

Discussion and Conclusion

What the Agent Actually Learned

Across all four opponents, the agent’s behavior matched what game theory would predict as the optimal response. It cooperated against Tit-for-Tat, defected against Always Cooperate and Always Defect, and defected against Grudger, in each case for the right reason given the opponent’s behavior. The agent was not given any of this knowledge in advance. It discovered the right response to each opponent from experience alone.
The main finding is that cooperation is not something the agent learns universally, it learns the optimal strategy per opponent, and cooperation only happens to be that strategy against one of the four. A better way to describe the result is: the agent learns when to cooperate and when not to, depending on who it is playing against.

Limitations

There are two limitations worth acknowledging. First, every curve in this experiment comes from a single training run. Since Q-learning with ε-greedy exploration involves randomness, two runs with identical settings can produce different results, especially in how quickly the cooperation rate rises against Tit-for-Tat. To make stronger claims about convergence, results should ideally be averaged over 20–30 independent runs and shown with confidence bands [2].
Second, all four opponents in this experiment use fixed strategies that do not change over time. This makes the environment stable and predictable for the agent. In a real multi-agent setting, both players would be learning simultaneously, and each would have to adapt to a moving target [3]. That is a much harder and more realistic version of the problem, and a natural direction to extend this work.

Conclusion

This research shows that a Q-learning agent with minimal state information just 10 learned values can figure out the right way to play against each of four different opponents in the iterated Prisoner’s Dilemma, using only the rewards it earns. It cooperates when cooperation is the best long-run response, and defects when it is not. The Grudger result adds an important nuance: the agent’s ability to learn the right strategy is bounded by what it can observe. When an opponent’s behavior depends on history that the agent cannot track, the agent will misread the situation and respond to something simpler than what is actually happening. That limitation is not a flaw to fix it is part of what makes this experiment worth studying. What the agent learns tells us as much about the game as it does about the algorithm.

References

◼
  • [1] R. Axelrod, The Evolution of Cooperation. New York: Basic Books, 1984.
  • ◼
  • [2] C. J. C. H. Watkins and P. Dayan, “Q-learning,” Machine Learning, vol. 8, no. 3–4, pp. 279–292, 1992.
  • ◼
  • [3] T. W. Sandholm and R. H. Crites, “Multiagent reinforcement learning in the iterated prisoner’s dilemma,” Biosystems, vol. 37, no. 1–2, pp. 147–166, 1996.
  • ◼
  • [4] M. A. Nowak, “Five rules for the evolution of cooperation,” Science, vol. 314, no. 5805, pp. 1560–1563, 2006.
  • AI Use Disclosure

    AI was used as a coding assistant throughout the development of this project. Specifically, AI assisted with debugging Wolfram Language syntax, identifying logical errors in the Q-learning implementation (including the history variable assignment), and suggesting hyperparameter choices. All code was reviewed, tested, and understood by the author. The research direction, strategy selection, and interpretation of results reflect the author’s own thinking.

    CITE THIS NOTEBOOK

    A Q-learning agent discovers the optimal strategy against each opponent in the Prisoner's Dilemma​
    by Hyeri Ahn​
    Wolfram Community, STAFF PICKS, June 4, 2026
    ​https://community.wolfram.com/groups/-/m/t/3727339