Welcome to the Prisoner's Dilemma Simulation, a Python-based project that delves into one of the most iconic problems in Game Theory. Through this simulation, you can explore how various strategies compete in multi-round games, gain insights into their effectiveness, and visualize the outcomes through engaging graphical representations. 🌟
The Prisoner's Dilemma represents a situation where individuals face a choice to either cooperate for mutual benefit or defect for personal gain at the expense of others. Despite its simplicity, this problem highlights the complexities of trust, competition, and collaboration in human interactions, economic systems, and evolutionary biology.
Opponent: C | Opponent: D | |
---|---|---|
Player: C | (3, 3) | (0, 5) |
Player: D | (5, 0) | (1, 1) |
- C = Cooperate 🤝
- D = Defect ❌
Key Takeaways:
- Mutual cooperation yields moderate rewards for both players.
- Defecting provides a higher payoff if the opponent cooperates but risks low rewards if both defect.
Game Theory is the study of strategic interactions between rational decision-makers. It provides mathematical frameworks to analyze situations where the outcome for each participant depends on the choices of others. Game theory helps in understanding and predicting behaviors in competitive and cooperative environments.
- Economics: Analyzing markets, auctions, and pricing strategies.
- Politics: Understanding alliances, voting systems, and conflict resolution.
- Biology: Exploring evolutionary strategies and species interactions.
- Artificial Intelligence: Designing algorithms for decision-making in multi-agent systems.
- Business: Developing competitive strategies, negotiation tactics, and collaboration frameworks.
- Everyday Life: From board games to complex societal issues, game theory helps explain interactions in a variety of contexts.
- Pre-implemented strategies:
- Always Cooperate 🤝
- Always Defect ❌
- Tit-for-Tat 🔄
- Grudger 😠
- Random 🎲
- Multi-round simulation to observe long-term dynamics.
- Graphical visualization of cumulative payoffs 📊.
- Detailed comparison of strategies.
This simulation demonstrates how cooperation can lead to better outcomes over time, yet selfishness (defection) often seems tempting in the short term.
Some strategies, like Tit-for-Tat or Grudger, balance cooperation and retaliation, showing how trust and punishment work together to enforce stability in repeated interactions.
Prisoners-Dilemma-Simulation/
├── README.md
├── LICENSE
├── requirements.txt
├── src/
│ ├── __init__.py
│ ├── strategies.py
│ ├── game.py
├── docs/
│ ├── strategy_explanations.md
│ ├── visuals/
│ ├── sample_graph.png
├── tests/
│ ├── __init__.py
│ ├── test_strategies.py
│ ├── test_game.py
└── venv/
└── main.py
- Python 3.8 or above.
pip
installed.
-
Clone the repository:
git clone https://github.com/mohd-faizy/Prisoners-Dilemma-Simulation.git cd Prisoners-Dilemma-Simulation
-
Set up a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the simulation:
Open the terminal in VSCode, navigate to the project folder, and execute:
python main.py
-
Run the program to initiate the simulation:
python src/main.py
-
Output:
- Round-by-round moves and payoffs.
- Graphical representation of cumulative payoffs.
- Comparative analysis of strategies.
The following demonstrates Tit-for-Tat versus Always Defect for 100 rounds.
Sample Output:
Round 1: Player 1 (Cooperate), Player 2 (Defect), Payoffs: (0, 5)
Round 2: Player 1 (Defect), Player 2 (Defect), Payoffs: (1, 1)
...
Final Cumulative Payoffs:
- Player 1: 50
- Player 2: 100
Strategy | Description |
---|---|
Always Cooperate | Always chooses "Cooperate" regardless of the opponent's moves. |
Always Defect | Always chooses "Defect" regardless of the opponent's moves. |
Tit-for-Tat | Starts with "Cooperate" and mimics the opponent's last move in subsequent rounds. |
Grudger | Cooperates until the opponent defects once, then defects forever. |
Random | Randomly chooses between "Cooperate" and "Defect". |
- The simulation generates graphs of cumulative payoffs for each strategy.
- Example graph from a 100-round game:
Contributions are welcome! Feel free to submit pull requests to improve strategies, optimize the code, or add features.
- Fork the repository.
- Create your feature branch:
git checkout -b feature/YourFeature
. - Commit your changes:
git commit -m 'Add some feature'
. - Push to the branch:
git push origin feature/YourFeature
. - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.