Skip to main content

A package for simulating the Prisoner's Dilemma game.

Project description

spielmacher

spielmacher is a Python package for simulating the Prisoner's Dilemma game. It supports both 1v1 and tournament-style gameplay, allowing users to create various player strategies and observe their interactions over multiple rounds.

Installation

pip install spielmacher

Usage

1v1 Gameplay

To simulate a 1v1 game, you need to create instances of Player subclasses and configure the game using GameConfig. Then, create a Game instance and run it.

from spielmacher.player import CooperativePlayer, EgoisticPlayer
from spielmacher.game import Game, GameConfig

# Create player instances
player1 = CooperativePlayer()
player2 = EgoisticPlayer()

# Configure the game
config = GameConfig(player1, player2)

# Create and run the game
game = Game(config)
game.run()
scores = game.get_scores()
print(f"Final Scores: {scores}")
game.plot_scores()

Tournament Gameplay

To simulate a tournament, create a list of Player instances and configure the tournament using the Tournament class. Then, run the tournament.

from spielmacher.player import CooperativePlayer, EgoisticPlayer, RandomPlayer
from spielmacher.tournament import Tournament

# Create player instances
players = [
    CooperativePlayer(),
    EgoisticPlayer(),
    RandomPlayer()
]

# Configure and run the tournament
tournament = Tournament(players, rounds_per_game=10)
tournament.run()

Configuration Options

GameConfig

The GameConfig class is used to configure a 1v1 game. It has the following options:

  • player1 (Player): The first player.
  • player2 (Player): The second player.
  • num_rounds (int, optional): The number of rounds to be played. Default is 10.
  • mistake_probability (float, optional): The probability of a mistake occurring in communication. Default is 0.0.
  • score_map (ScoreMap, optional): The rewards given after a round based on the players' decisions.

Example:

config = GameConfig(player1, player2, num_rounds=20, mistake_probability=0.05)

Tournament

The Tournament class is used to configure and run a tournament. It has the following options:

  • players (list[Player]): A list of player instances participating in the tournament.
  • rounds_per_game (int): The number of rounds to be played in each game.

Example:

tournament = Tournament(players, rounds_per_game=15)

Player Strategies

The package includes several predefined player strategies:

  • CooperativePlayer: Always cooperates.
  • EgoisticPlayer: Always defects.
  • RandomPlayer: Chooses moves randomly between cooperate and defect.
  • GrudgerPlayer: Cooperates until the opponent defects, then always defects.
  • DetectivePlayer: Cooperates until the opponent defects twice in a row, then always defects.
  • TitForTatPlayer: Cooperates at first, then plays the opponent's last move.
  • ForgivingTitForTatPlayer: Cooperates until the opponent defects, then has a 10% chance to forgive a defect.
  • SimpletonPlayer: Cooperates on the first round. If the opponent cooperates, repeats its last move. If the opponent defects, switches its last move.
  • EveryNthDefectorPlayer: Defects every nth turn.
  • HumanPlayer: Human player that selects moves manually.
  • SimulatedPlayer: Simulated player that selects moves based on a pre-trained model.

License

This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spielmacher-0.0.2.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spielmacher-0.0.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file spielmacher-0.0.2.tar.gz.

File metadata

  • Download URL: spielmacher-0.0.2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.1

File hashes

Hashes for spielmacher-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e3555249bd3d368ec746cfecbec248ff13c92c2630b2797fea08e80811dd2a29
MD5 95cb69aeec124deca3b91e66aaa3ccc9
BLAKE2b-256 1548c89c34d7be923697f532c930626c2e0ab9940841aacdf24378f975a3f0b0

See more details on using hashes here.

File details

Details for the file spielmacher-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: spielmacher-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.1

File hashes

Hashes for spielmacher-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 97501b09877b87e373794ca843cd9a3871ee9c936e0a7626d813fdba875c8d58
MD5 ae92bd01fe94cab1c4800950ed22a7a9
BLAKE2b-256 88285a18665cd6314028bbeb6368ff708872335a28afa4f4e139385c30af12be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page