Skip to main content

Project Dilemma

Project Dilemma is a simulation tool for testing algorithms in the prisoner's dilemma. It provides a standard interface to define both algorithm and simulation classes so that they may be easily tested. Inspired by this Veritasium video.

Table of Contents

Installation

PyPi

  1. pip install project-dilemma

Manual

  1. Download git repo
  2. Change into repo root directory
  3. pip install .

Configuration

Config File Location

Project Dilemma will automatically try to load the configuration from the user's and system's configuration directories, usually set by $XDG_CONFIG_DIRS. For most Linux users, this will check ~/.config/project_dilemma and them somewhere in /etc.

This behaviour can be overridden by specifying the --config flag to the config file you want to use.

Config Format

Project Dilemma uses the TOML format for configuration files. This is a human-readable format that is easy to write. The schema has been provided below:

simulation_id = "name of simulation"
algorithms_directory = "/path/to/algorithms/"
nodes = [ { node_id = "node_1", algorithm = { file = "foo.py", object = "Foo" }, quantity = 11 },
          { node_id = "node_2", algorithm = { file = "bar/baz.py", object = "Baz" } } ]
simulation = { file = "foobar.py", object = "GenerationalFooBar" }
generational_simulation = { file = "foobar.py", object = "FooBar" }
simulation_arguments = { foo = "bar" }
simulation_data = "path/to/round.json"
simulation_data_output = "path/to/round.json"
simulation_results_output = "path/to/results.json"
simulations_directory = "/path/to/simulations/"
  • algorithms_directory
    • A path to the directory containing the algorithms files
  • generational_simulation
    • The simulation to run for each generation in a generational simulation as a Dynamic Import
  • nodes
    • An array of tables that specify:
      • node id
      • algorithm, as defined in the Dynamic Imports
      • quantity, if not specified then 1 node is assumed
        • Note: the node index will be appended to the node_id section
  • simulation
  • simulation_id
    • The name of the simulation
  • simulation_arguments
    • Arguments to pass into the simulation
  • simulation_data
    • Path to a JSON file containing previous simulation data
  • simulation_data_output
    • Path to write the simulation data as a JSON
  • simulation_results_output
    • Path to write the simulation results
  • simulations_directory
    • A path to the directory containing additional simulation files
    • Required for user provided simulations

Dynamic Imports

Because a lot of the objects, such as the algorithms and simulations, can or must be provided by the user, this data must be imported dynamically. In order to easily import these objects without importing every simulation and algorithm, the following format can be used to tell the program where to look for the imports:

{ file = "path/to/file", object = "ObjectToImport" }
  • file
    • A path to the file containing the object relative to the associated directory in the config
    • Required for algorithms and user provided simulations
  • object
    • The object to import
      • For builtin simulations, specify the simulation class name here

Algorithms

Algorithms can be defined very easily. Only four things must be done to subclass the Algorithm interface:

  1. Set class name
  2. Set algorithm_id
  3. Pass in the mutations to the interface's init (see template for example)
  4. Implement the decide function
  5. Set mutations (optional)

The decide function is what the simulation uses to run the algorithm. It accepts a project_dilemma.interfaces.base.Rounds object which can be used to get the results of prior rounds. The function should return True for cooperation, and False for defection.

If you want to add mutations, set the static mutation list after defining the class as to avoid circular imports.

A template has been provided in templates/algorithm_template.py for ease of use.

Simulations

Simulations a more complicated to configure as compared to algorithms. You only need to override the run_simulation and process_simulation methods, but these are incredibly important.

run_simulation returns a project_dilemma.interfaces.base.Simulations object that will be used by process_simulation to get the results.

For example, the provided standard simulations process the rounds data to calculate scores for each node A template can be found in templates/simulation_template.py.

Generational Simulations

Generational Simulations are deceptively simple. There is only one function to override: generational_hook. However, this means that all the generational processing must be done in this function.

A template has been provided in templates/generational_simulation_template.py.

License

Copyright 2023 Gabriele Ron

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This project utilizes the platformdirs project which is licensed under the MIT License. Copyright (c) 2010-202x The platformdirs developers

Metadata

Release files for project-dilemma 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for project-dilemma 1.1.0
File Size Uploaded
project_dilemma-1.1.0.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for project-dilemma 1.1.0
File Interpreter ABI Platform
project_dilemma-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.7 kB

Release files / project_dilemma-1.1.0.tar.gz

Download URL project_dilemma-1.1.0.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f464f91feb92f9d6a266c1da8cad1ba831dd6f7b84c48b72a1bf93560f2b294d
BLAKE2b-256 checksum
How to use checksums
ba098e012d685bc06ef954e824a9193d63dc1a765ff3a3b64180e018676d81c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / project_dilemma-1.1.0-py3-none-any.whl

Download URL project_dilemma-1.1.0-py3-none-any.whl
Size 27.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
023d7f483b77a6ccd72367af0d654ed3c98ba864183a1ee139bb2f1819bce309
BLAKE2b-256 checksum
How to use checksums
19ceff92b8bf22a93cd56a95b6b71d371355898f12df948bd1806bca53bef81a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page