Skip to main content

Python framework for running reproducible experiments using OpenTTD

Project description

OpenTTDLab logo

OpenTTDLab - Run reproducible experiments using OpenTTD

PyPI package Test suite Code coverage

OpenTTDLab is a Python framework for using OpenTTD to run reproducible experiments and extracting results from them, with as few manual steps as possible.

OpenTTDLab is based on Patric Stout's OpenTTD Savegame Reader.

[!CAUTION] OpenTTDLab currently does not work with OpenTTD 14.0 or later. The latest version of OpenTTD known to work is 13.4.


Contents


Features

  • Allows you to easily run OpenTTD in a headless mode (i.e. without a graphical interface) over a variety of configurations.
  • And allows you to do this from Python code - for example from a Jupyter Notebook.
  • As is typical from Python code, it is cross platform - allowing to share code snippets between macOS, Windows, and Linux, even though details like how to install and start OpenTTD are different on each platform.
  • Downloads (and caches) OpenTTD, OpenGFX, and AIs - no need to download these separately or through OpenTTD's built-in content browser.
  • Transparently parallelises runs of OpenTTD, by default up to the number of CPUs. (Although with fairly poor scaling properties.)
  • Results are extracted from OpenTTD savegames as plain Python dictionaries and lists - reasonably convenient for importing into tools such as pandas for analysis or visualisation.

Installation

OpenTTDLab is distributed via PyPI, and so can usually be installed using pip.

python -m pip install OpenTTDLab

When run on macOS, OpenTTDLab has a dependency that pip does not install: 7-zip. To install 7-zip, first install Homebrew, and then use Homebrew to install the p7zip package that contains 7-zip.

brew install p7zip

You do not need to separately download or install OpenTTD (or OpenGFX) in order to use OpenTTDLab. OpenTTDLab itself handles downloading them.

Running experiments

The core function of OpenTTD is the run_experiments function.

from openttdlab import run_experiments, bananas_ai

# Run experiments...
results = run_experiments(
    openttd_version='13.4',  # ... for a specific versions of OpenTTD
    opengfx_version='7.1',   # ... and a specific versions of OpenGFX
    experiments=(
       {
         # ... for random seeds
         'seed': seed,
         # ... running specific AIs. In this case a single AI, with no
         # parameters, fetching it from https://bananas.openttd.org/package/ai
         'ais': (
             bananas_ai('54524149', 'trAIns', ai_params=()),
         ),
         # ... each for a number of (in game) days
         'days': 365 * 4 + 1,
       }
       for seed in range(0, 10)
    ),
)

Plotting results

OpenTTD does not require any particular library for plotting results. However, pandas and Plotly Express are common options for plotting from Python. For example if you have a results object from run_experiments as in the above example, the following code

import pandas as pd
import plotly.express as px

df = pd.DataFrame(
    {
        'seed': row['seed'],
        'date': row['date'],
        'money': row['chunks']['PLYR']['0']['money'],
    }
    for row in results
)
df = df.pivot(index='date', columns='seed', values='money')
fig = px.line(df)
fig.show()

should output a plot much like this one.

A plot of money against time for 10 random seeds

Examples

A notebook of the above example and an example measuring the performance of OpenTTDLab are in the examples folder.

API

Running experiments

run_experiments(...)

The core function of OpenTTDLab is the run_experiments function, used to run an experiment and return results extracted from the savegame files that OpenTTD produces. It has the following parameters and defaults.

  • ais=()

    The list of AIs to run. See the Fetching AIs section for details on this parameter.

  • ais_libraries=()

    The list of AI libraries to have available to AI code. See the Fetching AI libraries section for details on this parameter.

  • seeds=(1,)

    An iterable of integers, where each is used to seed the random number generator in a run of OpenTTD.

  • days=365 * 4 + 1

    The number of in-game days that each run of OpenTTD should last.

  • base_openttd_config=''

    OpenTTD config to run each experiment under. This must be in the openttd.cfg format. This is added to by OpenTTDLab before being passed to OpenTTD.

  • final_screenshot_directory=None

    The directory to save a PNG screenshot of the entire map at the end of each run. Each is named in the format <seed>.png, where <seed> is the run's seed of the random number generator. If None, then no screenshots are saved.

    For technical reasons, a window will briefly appear while each screenshot is being saved. This can be avoided when running on Linux if xvfb-run is installed and available in the path.

  • max_workers=None

    The maximum number of workers to use to run OpenTTD in parallel. IfNone, then os.cpu_count() defined how many workers run.

  • openttd_version=None

    The version of OpenTTD to use. If None, the latest version available at openttd_base_url is used.

    [!CAUTION] OpenTTDLab currently does not work with OpenTTD 14.0 or later. The latest version of OpenTTD known to work is 13.4.

  • opengfx_version=None

    The version of OpenGFX to use. If None, the latest version available at opengfx_base_url is used.

  • openttd_base_url='https://cdn.openttd.org/openttd-releases/

    The base URL used to fetch the list of OpenTTD versions, and OpenTTD binaries.

  • opengfx_base_url='https://cdn.openttd.org/opengfx-releases/

    The URL used to fetch the list of OpenGFX versions, and OpenGFX binaries.

  • get_http_client=lambda: httpx.Client(transport=httpx.HTTPTransport(retries=3)

    The HTTP client used to make HTTP requests when fetching OpenTTD, OpenGFX, or AIs. Note that the bananas_ai function uses a raw TCP connection in addition to HTTP requests, and so not all outgoing connections use the client specified by this.

Fetching AIs

The ais parameter of run_experiments configures which AIs will run, how their code will be located, their names, and what parameters will be passed to each of them when they start. In more detail, the ais parameter must be an iterable of the return value of any of the the following 4 functions.

[!IMPORTANT] The ai_name argument passed to each of the following functions must exactly match the name of the corresponding AI as published. If it does not match, the AI will not be started.

[!IMPORTANT] The return value of each of the following is opaque: it should not be used in client code, other than by passing into run_experiments as part of the ais parameter.

bananas_ai(unique_id, ai_name, ai_params=())

Defines an AI by the unique_id and ai_name of an AI published through OpenTTD's content service at https://bananas.openttd.org/package/ai. This allows you to quickly run OpenTTDLab with a published AI. The ai_params parameter is an optional parameter of an iterable of (key, value) parameters passed to the AI on startup.

The unique_id is sometimes surfaced as the "Content Id", but it should not include its ai/ prefix.

local_folder(folder_path, ai_name, ai_params=()))

Defines an AI by the folder_path to a local folder that contains the AI code of an AI with name ai_name. The ai_params parameter is an optional parameter of an iterable of (key, value) parameters passed to the AI on startup.

local_file(path, ai_name, ai_params=())

Defines an AI by the local path to a .tar AI file that contains the AI code. The ai_params parameter is an optional parameter of an iterable of (key, value) parameters passed to the AI on startup.

remote_file(url, ai_name, ai_params=())

Fetches the AI by the URL of a tar.gz file that contains the AI code. For example, a specific GitHub tag of a repository that contains its code. The ai_params parameter is an optional parameter of an iterable of (key, value) parameters passed to the AI on startup.

Fetching AI libraries

The ai_libraries parameter of run_experiments ensures that AI libraries are available to the AIs running. In more detail, the ais_libraries parameter must be an iterable, where each item the the return value of the following function.

bananas_ai_library(unique_id, ai_library_name)

Fetches the AI library defined by unique_id and ai_name of a library published through OpenTTD's content service at https://bananas.openttd.org/package/ai-library.

The unique_id is sometimes surfaced as the "Content Id", but it should not include its ai-library/ prefix.

Compatibility

  • Linux (tested on Ubuntu 20.04), Windows (tested on Windows Server 2019), or macOS (tested on macOS 11)
  • Python >= 3.8.0 (tested on 3.8.0 and 3.12.0)

Licenses and attributions

TL;DR

OpenTTDLab is licensed under the GNU General Public License version 2.0.

In more detail

OpenTTDLab is based on Patric Stout's OpenTTD Savegame Reader, licensed under the GNU General Public License version 2.0.

The OpenTTDLab logo is a modified version of the OpenTTD logo, authored by the OpenTTD team. The OpenTTD logo is also licensed under the GNU General Public License version 2.0.

The .gitignore file is based on GitHub's Python .gitignore file. This was originally supplied under CC0 1.0 Universal. However, as part of OpenTTDLab it is licensed under GNU General Public License version 2.0.

trAIns is authored by Luis Henrique O. Rios, and licensed under the GNU General Public License version 2.0.

OpenTTD and OpenGFX are authored by the OpenTTD team. Both are licensed under the GNU General Public License version 2.0.

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

openttdlab-0.0.50.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

openttdlab-0.0.50-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file openttdlab-0.0.50.tar.gz.

File metadata

  • Download URL: openttdlab-0.0.50.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for openttdlab-0.0.50.tar.gz
Algorithm Hash digest
SHA256 2d84cdd90a518fa79bebb4cd94793c933bc0bcac4b4f871591c4fa065bf232f8
MD5 7dbfd6dae012064e0ed3bd3e91d512fd
BLAKE2b-256 5f66c17ea73d7aaca1e3fae9df117174a4d82b00b2a218556fabc7488fd1c04e

See more details on using hashes here.

Provenance

File details

Details for the file openttdlab-0.0.50-py3-none-any.whl.

File metadata

  • Download URL: openttdlab-0.0.50-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for openttdlab-0.0.50-py3-none-any.whl
Algorithm Hash digest
SHA256 9a7ee17b721fbf4a6f772bdc6a82e60e633895aa42bf1ef021f34d82d512141b
MD5 91831b7ec7730639f2797fa1d7909786
BLAKE2b-256 1402c665563b1ef71098a6de952793bbd206abbc7c1536ca9195910303908181

See more details on using hashes here.

Provenance

Supported by

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