Skip to content

mohd-faizy/Game-Theory-Prisoners-Dilemma-Simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

author

Game Theory - Prisoner's Dilemma Simulation

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. ๐ŸŒŸ


Why the Prisoner's Dilemma Matters ๐Ÿค”

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.

The Payoff Matrix ๐Ÿงฎ

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.

What is Game Theory? ๐ŸŽฒ

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.

Applications of Game Theory ๐ŸŒ

  • 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.

Features ๐Ÿš€

  • 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.

Insights from the Simulation ๐Ÿ’ก

Cooperation vs. Competition โš”๏ธ

This simulation demonstrates how cooperation can lead to better outcomes over time, yet selfishness (defection) often seems tempting in the short term.

Emergence of Stable Strategies ๐Ÿ”’

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.


Directory Structure ๐Ÿ—‚๏ธ

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

Getting Started ๐Ÿ› ๏ธ

Prerequisites ๐Ÿ“‹

  • Python 3.8 or above.
  • pip installed.

Installation Steps ๐Ÿ“ฅ

  1. Clone the repository:

    git clone https://github.com/mohd-faizy/Prisoners-Dilemma-Simulation.git
    cd Prisoners-Dilemma-Simulation
  2. Set up a virtual environment (optional but recommended):

    python -m venv venv
    source venv/bin/activate   # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Run the simulation:

    Open the terminal in VSCode, navigate to the project folder, and execute:

    python main.py

Usage ๐Ÿ–ฅ๏ธ

  1. Run the program to initiate the simulation:

    python src/main.py
  2. Output:

    • Round-by-round moves and payoffs.
    • Graphical representation of cumulative payoffs.
    • Comparative analysis of strategies.

Example Simulation ๐ŸŽฏ

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

Strategies Implemented ๐Ÿค–

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".

Visualization ๐Ÿ“ˆ

  • The simulation generates graphs of cumulative payoffs for each strategy.
  • Example graph from a 100-round game:

Sample Graph


Contributing ๐Ÿค

Contributions are welcome! Feel free to submit pull requests to improve strategies, optimize the code, or add features.

  1. Fork the repository.
  2. Create your feature branch: git checkout -b feature/YourFeature.
  3. Commit your changes: git commit -m 'Add some feature'.
  4. Push to the branch: git push origin feature/YourFeature.
  5. Open a pull request.

License ๐Ÿ“œ

This project is licensed under the MIT License. See the LICENSE file for details.


$\color{skyblue}{\textbf{Connect with me:}}$


About

A Python-based simulation of the Prisoner's Dilemma to analyze strategies in Game Theory.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published