Skip to main content

Fast Settlers of Catan Python Implementation

Project description

catanatron

Coverage Status Gradient Netlify Status

Settlers of Catan Python implementation and Machine-Learning player.

Usage

Create a virtualenv with Python 3.8 and install requirements:

python3.8 -m venv venv
source ./venv/bin/activate
pip install -r dev-requirements.txt

Run games with the play.py script. It provides extra options you can explore with --help:

python experimental/play.py --num=100

Make your own bot

You can create your own AI bots/players by implementing the following API:

from catanatron.game import Game
from catanatron.models.actions import Action
from catanatron.models.players import Player

class MyPlayer(Player):
    def decide(self, game: Game, playable_actions: Iterable[Action]):
        """Should return one of the playable_actions.

        Args:
            game (Game): complete game state. read-only.
            playable_actions (Iterable[Action]): options to choose from
        Return:
            action (Action): Chosen element of playable_actions
        """
        raise NotImplementedError

See examples in players.py. You can then modify play.py to import your bot and test it against others.

Library API

You can use catanatron as a stand-alone module.

from catanatron.game import Game
from catanatron.models.player import RandomPlayer, Color

players = [
    RandomPlayer(Color.RED),
    RandomPlayer(Color.BLUE),
    RandomPlayer(Color.WHITE),
    RandomPlayer(Color.ORANGE),
]
game = Game(players)
game.play()

You can then inspect the game state any way you want (e.g. game.state.actions, game.state.board, game.state.players[0].buildings, etc...).

Watching Games

We provide a complete docker-compose.yml with everything needed to watch games (watching random bots is very enteraining!). Ensure you have Docker Compose installed, and run:

docker-compose up

You can also use a handy open_game function to create a link for a particular game:

from catanatron_server.utils import open_game
open_game(game)  # prints a link to open in browser

For open_game to work correctly, you must run docker-compose up in another tab. The docker-compose contains the web-server infrastructure needed to render the game in a browser.

Architecture

For debugging and entertainment purposes, we wanted to provide a UI with which to inspect games.

We decided to use the browser as a rendering engine (as opposed to the terminal or a desktop GUI) because of HTML/CSS's ubiquitousness and the ability to use modern animation libraries in the future (https://www.framer.com/motion/ or https://www.react-spring.io/).

To achieve this, we separated the code into three components:

  • catanatron: A pure python implementation of the game logic. Uses networkx for fast graph operations. Is pip-installable (see setup.py) and can be used as a Python package.

  • catanatron_server: Contains a Flask web server in order to serve game states from a database to a Web UI. The idea of using a database, is to ease watching games from different processes (you can play a game in a standalone Python script and save it for viewing).

  • React Web UI: A web UI to render games. The ui folder.

Experimental Folder

The experimental folder contains unorganized code with many failed attempts at finding the best possible bot.

Developing for Catanatron

To develop for Catanatron core logic you can use the following test suite:

coverage run --source=catanatron -m pytest tests/ && coverage report

Or you can run the suite in watch-mode with:

ptw --ignore=tests/integration_tests/ --nobeep

Machine Learning

Generate data (GZIP CSVs of features and PGSQL rows) by running:

python experimental/play.py --num=100 --outpath=my-data-path/

You can then use this data to build a machine learning model, and then implement a Player subclass that implements the corresponding "predict" step of your model. There are some examples of these type of players in experimental/machine_learning/players/reinforcement.py.

Appendix

Running Components Individually

As an alternative to running the project with Docker, you can run the following 3 components: a React UI, a Flask Web Server, and a PostgreSQL database in three separate Terminal tabs.

React UI

Make sure you have yarn installed (https://classic.yarnpkg.com/en/docs/install/).

cd ui/
yarn install
yarn start

This can also be run via Docker independetly like (after building):

docker build -t bcollazo/catanatron-react-ui:latest ui/
docker run -it -p 3000:3000 bcollazo/catanatron-react-ui

Flask Web Server

Ensure you are inside a virtual environment with all dependencies installed and use flask run.

python3.8 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt
flask run

This can also be run via Docker independetly like (after building):

docker build -t bcollazo/catanatron-server:latest .
docker run -it -p 5000:5000 bcollazo/catanatron-server

PostgreSQL Database

Make sure you have docker-compose installed (https://docs.docker.com/compose/install/).

docker-compose up

Or run any other database deployment (locally or in the cloud).

Other Useful Commands

TensorBoard

For watching training progress, use keras.callbacks.TensorBoard and open TensorBoard:

tensorboard --logdir logs

Docker GPU TensorFlow

docker run -it tensorflow/tensorflow:latest-gpu-jupyter bash
docker run -it --rm -v $(realpath ./notebooks):/tf/notebooks -p 8888:8888 tensorflow/tensorflow:latest-gpu-jupyter

Testing Performance

python -m cProfile -o profile.pstats experimental/play.py --num=5
snakeviz profile.pstats
pytest --benchmark-compare=0001 --benchmark-compare-fail=mean:10% --benchmark-columns=min,max,mean,stddev

Head Large Datasets with Pandas

In [1]: import pandas as pd
In [2]: x = pd.read_csv("data/mcts-playouts-labeling-2/labels.csv.gzip", compression="gzip", iterator=True)
In [3]: x.get_chunk(10)

Publishing to PyPi

pip install twine
python setup.py sdist bdist_wheel
twine check dist/*
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
twine upload dist/*

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

catanatron-0.0.2.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

catanatron-0.0.2-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: catanatron-0.0.2.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for catanatron-0.0.2.tar.gz
Algorithm Hash digest
SHA256 860c9ff69b9c14cba874b773afdf487670d7ab8a5c571dd3fba43dfb6ab082e0
MD5 9ae20593ab0c7cf6af9a7c34141f2756
BLAKE2b-256 f38ea8ca7cc3557cd5744bf171c34701b5fcaa3fb33ae17e1b7f62670bcea485

See more details on using hashes here.

File details

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

File metadata

  • Download URL: catanatron-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for catanatron-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c21ad0b2581907693ab3e7a5b17f279f70babbd4ba11a5330ed1cbac66e63b1
MD5 32565ef9c9bc3464536d97d3745824a2
BLAKE2b-256 40f704f756f29bca9b053d4c221d9b85af701e860f99e55a03d7525a6f1a24d6

See more details on using hashes here.

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