Skip to content

Commit

Permalink
Merge 4ac3b3c into ed7a94c
Browse files Browse the repository at this point in the history
  • Loading branch information
bcollazo committed Jul 26, 2021
2 parents ed7a94c + 4ac3b3c commit c3bf2cc
Show file tree
Hide file tree
Showing 186 changed files with 327 additions and 248 deletions.
2 changes: 0 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
data
ui
logs
experimental/logs
experimental/models
assets
venv
.git
13 changes: 10 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Lint and Tests

on:
push:
branches: [master]
branches: [master, staging]
pull_request:
branches: [master]
branches: [master, staging]

jobs:
build:
Expand All @@ -23,9 +23,16 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r dev-requirements.txt
pip install -e catanatron_core
pip install -e catanatron_gym
pip install -e catanatron_server
pip install -e catanatron_experimental
- name: Lint with black
run: |
black catanatron/ --check
black catanatron_core --check
black catanatron_gym --check
black catanatron_server --check
black catanatron_experimental --check
- name: Test with pytest
run: |
coverage run --source=catanatron -m pytest tests/
Expand Down
12 changes: 7 additions & 5 deletions Dockerfile.paperspace
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This is a Container we can use to run Experiments in Paperspace.
# Edit, build, and publish to DockerHub.
# `docker build . -f Dockerfile.paperspace -t bcollazo/paperspace-rl` to build.
# `docker run --rm -it bcollazo/paperspace-rl python3.8 experimental/play.py` to ensure it works.
# `docker run --rm -it bcollazo/paperspace-rl catanatron-play` to ensure it works.
# `docker push bcollazo/paperspace-rl` to publish.
# FROM paperspace/tensorflow:2.0.0-gpu-py3-jupyter-lab
FROM tensorflow/tensorflow:2.4.1-gpu-jupyter-py38
FROM tensorflow/tensorflow:2.4.1-gpu-jupyter

# Install Python3.8
RUN apt update && \
Expand All @@ -20,13 +20,15 @@ WORKDIR /app
COPY requirements.txt /app

RUN python3.8 -m pip install --upgrade pip
RUN python3.8 -m pip install -r requirements.txt
RUN python3.8 -m pip install -r dev-requirements.txt

# For some reason we can't include the -e . dependency of the
# catanatron package in the requirements.txt. So we install
# it separately, here:
COPY . .

RUN python3.8 -m pip install -e .
RUN python3.8 -m pip install -e catanatron_core
RUN python3.8 -m pip install -e catanatron_gym
RUN python3.8 -m pip install -e catanatron_server
RUN python3.8 -m pip install -e catanatron_experimental

EXPOSE 8888
8 changes: 4 additions & 4 deletions Dockerfile.web
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ ENV FLASK_ENV=development
ENV FLASK_APP=catanatron_server
ENV FLASK_RUN_HOST=0.0.0.0

# We can't leverage Docker caching too much since we
# depend on the catanatron package via "-e ." in requirements.txt
COPY . .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

COPY . .
RUN pip install -e catanatron_core
RUN pip install -e catanatron_server

EXPOSE 5000

Expand Down
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
clean: clean-build clean-pyc clean-test

clean-build:
rm -fr build/
rm -fr dist/
rm -fr .eggs/
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -f {} +

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +
# find . -name '*~' -exec rm -f {} +

clean-test:
rm -fr .tox/
rm -f .coverage
rm -fr htmlcov/

build-catanatron: clean
python catanatron_core/setup.py sdist bdist_wheel
ls -l dist
twine check dist/*


build-catanatron-gym: clean
python catanatron_gym/setup.py sdist bdist_wheel
ls -l dist
twine check dist/*

upload:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*

upload-production:
twine upload dist/*
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Install with pip:
pip install catanatron
```

Make your own bot by implementing the following API (see examples in [catanatron/players](catanatron/players) and [experimental/machine_learning/players](experimental/machine_learning/players)):
Make your own bot by implementing the following API (see examples in [catanatron_core/catanatron/players](catanatron_core/catanatron/players) and [catanatron_experimental/catanatron_experimental/machine_learning/players](catanatron_experimental/catanatron_experimental/machine_learning/players)):

```python
from catanatron.game import Game
Expand Down Expand Up @@ -62,7 +62,7 @@ For watching these games in a UI see [watching games](#watching-games).

## Advanced Usage

Cloning the repo and using directly will allow you to access additional tools not included in the core package. In particular, a web UI for watching games and a `experimental/play.py` script that provides a blueprint to run many games, collect summary statistics (avg vps, avg game length, etc...),
Cloning the repo and using directly will allow you to access additional tools not included in the core package. In particular, a web UI for watching games and a `catanatron-play` CLI script that provides a blueprint to run many games, collect summary statistics (avg vps, avg game length, etc...),
save game for viewing in browser, and/or generate machine learning datasets.

Create a virtualenv with Python 3.8 and install requirements:
Expand All @@ -71,12 +71,16 @@ 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
pip install -e catanatron_core
pip install -e catanatron_server
pip install -e catanatron_gym
pip install -e catanatron_experimental
```

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

```
python experimental/play.py --num=100
catanatron-play --num=100
```

Currently, we can execute one game in ~76 milliseconds.
Expand Down Expand Up @@ -172,13 +176,13 @@ ptw --ignore=tests/integration_tests/ --nobeep
Generate data (GZIP CSVs of features and PGSQL rows) by running:

```
python experimental/play.py --num=100 --outpath=my-data-path/
catanatron-play --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`.
players in [reinforcement.py](catanatron_experimental/catanatron_experimental/machine_learning/players/reinforcement.py).

# Appendix

Expand Down Expand Up @@ -252,7 +256,7 @@ docker run -it --rm -v $(realpath ./notebooks):/tf/notebooks -p 8888:8888 tensor
### Testing Performance

```
python -m cProfile -o profile.pstats experimental/play.py --num=5
python -m cProfile -o profile.pstats catanatron_experimental/catanatron_experimental/play.py --num=5
snakeviz profile.pstats
```

Expand All @@ -273,13 +277,9 @@ In [3]: x.get_chunk(10)
catanatron Package

```
pip install twine
rm -rf build
rm -rf dist
python setup.py sdist bdist_wheel
twine check dist/*
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
twine upload dist/*
make build-catanatron
make upload
make upload-production
```

### Building Docs
Expand All @@ -292,7 +292,7 @@ sphinx-build -b html docs/source/ docs/build/html

# Contributing

I am new to Open Source Development, so open to suggestions on this section. The best contributions would be to make the core bot stronger by tinkering with the weights of each of the hand-crafted features in `experimental/machine_learning/players/minimax.py`, or coming up with new hand-crafted features! In particular, you can edit the `CONTENDER_WEIGHTS` and/or `contender_fn` function and run a command like: `python experimental/play.py --players=AB:2:False:C,AB:2 --num=100` to see if your changes improve the main bot.
I am new to Open Source Development, so open to suggestions on this section. The best contributions would be to make the core bot stronger by tinkering with the weights of each of the hand-crafted features in [minimax.py](catanatron_experimental/catanatron_experimental/machine_learning/players/minimax.py), or coming up with new hand-crafted features! In particular, you can edit the `CONTENDER_WEIGHTS` and/or `contender_fn` function and run a command like: `catanatron-play --players=AB:2:False:C,AB:2 --num=100` to see if your changes improve the main bot.

Here is also a list of ideas:

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 7 additions & 8 deletions setup.py → catanatron_core/setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import setuptools
import os

with open("README.md", "r") as fh:
readme_path = os.path.abspath(os.path.join(__file__, "..", "..", "README.md"))

with open(readme_path, "r") as fh:
long_description = fh.read()

setuptools.setup(
name="catanatron",
version="2.0.0",
version="3.0.0",
author="Bryan Collazo",
author_email="bcollazo2010@gmail.com",
description="Fast Settlers of Catan Python Implementation",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/bcollazo/catanatron",
packages=setuptools.find_packages(
exclude=[
"catanatron_server",
]
),
packages=setuptools.find_packages(exclude=["catanatron_server"]),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.8",
install_requires=["networkx", "gym"],
install_requires=["networkx"],
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from catanatron.game import Game

from catanatron.models.player import Color, Player, RandomPlayer
from experimental.alphatan.mcts import AlphaMCTS, game_end_value
from catanatron_experimental.alphatan.mcts import AlphaMCTS, game_end_value

from experimental.alphatan.simple_alpha_zero import (
from catanatron_experimental.alphatan.simple_alpha_zero import (
AlphaTan,
create_model,
load_replay_memory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import tensorflow as tf
import numpy as np

from experimental.machine_learning.players.reinforcement import ACTION_SPACE_SIZE
from experimental.dqn_player import to_action_space, from_action_space
from catanatron_experimental.machine_learning.players.reinforcement import (
ACTION_SPACE_SIZE,
)
from catanatron_experimental.dqn_player import to_action_space, from_action_space
from catanatron_gym.features import create_sample_vector

EPS = 1e-8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from experimental.machine_learning.players.reinforcement import ACTION_SPACE_SIZE
from catanatron_experimental.machine_learning.players.reinforcement import (
ACTION_SPACE_SIZE,
)
import time
from pathlib import Path

Expand All @@ -9,7 +11,7 @@

# Read datasets. Try to fit model
from simple_alpha_zero import create_model, load_replay_memory, pit
from experimental.machine_learning.board_tensor_features import (
from catanatron_experimental.machine_learning.board_tensor_features import (
HEIGHT,
WIDTH,
create_board_tensor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import uuid

from experimental.alphatan.simple_alpha_zero import (
from catanatron_experimental.alphatan.simple_alpha_zero import (
AlphaTan,
create_model,
load_replay_memory,
pit,
)
from catanatron.models.player import Color, Player, RandomPlayer
from experimental.play import play_batch
from catanatron_experimental.play import play_batch

save_in_db = False
model = create_model()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
import tensorflow as tf

from catanatron.game import Game
from experimental.alphatan.mcts import AlphaMCTS, game_end_value
from experimental.machine_learning.utils import ensure_dir
from catanatron_experimental.alphatan.mcts import AlphaMCTS, game_end_value
from catanatron_experimental.machine_learning.utils import ensure_dir
from catanatron.models.player import Color, Player, RandomPlayer
from experimental.play import play_batch
from catanatron_experimental.play import play_batch
from catanatron_gym.envs.catanatron_env import (
ACTIONS_ARRAY,
ACTION_SPACE_SIZE,
from_action_space,
to_action_space,
)
from experimental.dqn_player import epsilon_greedy_policy
from experimental.machine_learning.board_tensor_features import (
from catanatron_experimental.dqn_player import epsilon_greedy_policy
from catanatron_experimental.machine_learning.board_tensor_features import (
CHANNELS,
HEIGHT,
WIDTH,
Expand Down Expand Up @@ -108,7 +108,7 @@ def main():

# TODO: Save checkpoint
model.save_weights(f"data/checkpoints/{name}")
model.save(f"experimental/models/{name}")
model.save(f"data/models/{name}")


def save_replay_memory(data_directory, examples):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from bayes_opt import BayesianOptimization
from bayes_opt.logger import JSONLogger

from experimental.machine_learning.players.minimax import (
from catanatron_experimental.machine_learning.players.minimax import (
AlphaBetaPlayer,
DEFAULT_WEIGHTS,
ValueFunctionPlayer,
)
from catanatron.models.player import Color
from experimental.play import play_batch
from catanatron_experimental.play import play_batch


def black_box_function(a, b, c, d, e, f, g, h, i, j, k, l):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from experimental.play import simulate
from catanatron_experimental.play import simulate

simulate(50, "AB:2,F", None, False, False)

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import tensorflow as tf
import kerastuner as kt

from experimental.machine_learning.players.reinforcement import ACTION_SPACE_SIZE
from experimental.train import NUM_FEATURES
from catanatron_experimental.machine_learning.players.reinforcement import (
ACTION_SPACE_SIZE,
)
from catanatron_experimental.train import NUM_FEATURES


# ===== Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
create_sample_vector,
get_feature_ordering,
)
from experimental.machine_learning.board_tensor_features import (
from catanatron_experimental.machine_learning.board_tensor_features import (
CHANNELS,
HEIGHT,
WIDTH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import tensorflow as tf
import pandas as pd

from experimental.machine_learning.board_tensor_features import CHANNELS, HEIGHT, WIDTH
from catanatron_experimental.machine_learning.board_tensor_features import (
CHANNELS,
HEIGHT,
WIDTH,
)


def read_data(
Expand Down
File renamed without changes.
Loading

0 comments on commit c3bf2cc

Please sign in to comment.