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.

Uploading

python -m build
python -m twine upload dist/*

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.3.tar.gz (17.4 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.3-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spielmacher-0.0.3.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for spielmacher-0.0.3.tar.gz
Algorithm Hash digest
SHA256 bd60cb3088cb8bc57860a5c081edca3a4ae537fe7ba8d3207d9b4f48f4aca7e2
MD5 f671794b7347c5fd3ad5ba32c4341d85
BLAKE2b-256 439743ac0945d99528e8dd8bf414b2a07c38e033a7bb8d4c08e89d616b89334d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: spielmacher-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for spielmacher-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b954be39fbf2540b570fc7bba440b0f251acaf6b4d06449f12d51137443ca45f
MD5 14cdf11f11a756ed5750340219cf12b1
BLAKE2b-256 3a662f65f7bbc4c290c70c4fd1ff2f4a61a1280a9c2430fe6d61e90349085ea3

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