Skip to content

Commit

Permalink
Easier onboarding (#250)
Browse files Browse the repository at this point in the history
* Fix typo on function name (maintain_longest_road)

* Fix maintaining typo in documentation

* Fix maintaining typo

* Fix typo in function name (maintain_largest_army)

* Reduce tensorflow dependency in tests

* Lazy import tensorflow

* Require Node 16 on UI

* Try fixing Node 16 in the CI

* Set .nvmrc to use 16 (for netlify builds)

* Avoid calling db.init_app twice
  • Loading branch information
bcollazo committed Mar 19, 2023
1 parent 050384a commit d46f82c
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm ci
working-directory: ./ui
- run: npm run build
Expand Down
2 changes: 1 addition & 1 deletion catanatron_core/catanatron/models/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Board:
to Color (if there is a road there). Contains inverted
edges as well for ease of querying.
connected_components (Dict[Color, List[Set[NodeId]]]): Cache
datastructure to speed up mantaining longest road computation.
datastructure to speed up maintaining longest road computation.
To be queried by Color. Value is a list of node sets.
board_buildable_ids (Set[NodeId]): Cache of buildable node ids in board.
road_color (Color): Color of player with longest road.
Expand Down
10 changes: 5 additions & 5 deletions catanatron_core/catanatron/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
build_road,
build_settlement,
buy_dev_card,
mantain_longest_road,
maintain_longest_road,
play_dev_card,
player_can_afford_dev_card,
player_can_play_dev,
Expand Down Expand Up @@ -308,7 +308,7 @@ def apply_action(state: State, action: Action):
"""Main controller call. Follows redux-like pattern and
routes the given action to the appropiate state-changing calls.
Responsible for mantaining:
Responsible for maintaining:
.current_player_index, .current_turn_index,
.current_prompt (and similars), .playable_actions.
Expand Down Expand Up @@ -359,7 +359,7 @@ def apply_action(state: State, action: Action):
state.resource_freqdeck = freqdeck_add(
state.resource_freqdeck, SETTLEMENT_COST_FREQDECK
) # replenish bank
mantain_longest_road(state, previous_road_color, road_color, road_lengths)
maintain_longest_road(state, previous_road_color, road_color, road_lengths)

# state.current_player_index stays the same
# state.current_prompt stays as PLAY
Expand Down Expand Up @@ -396,7 +396,7 @@ def apply_action(state: State, action: Action):
result = state.board.build_road(action.color, edge)
previous_road_color, road_color, road_lengths = result
build_road(state, action.color, edge, True)
mantain_longest_road(state, previous_road_color, road_color, road_lengths)
maintain_longest_road(state, previous_road_color, road_color, road_lengths)

state.free_roads_available -= 1
if (
Expand All @@ -412,7 +412,7 @@ def apply_action(state: State, action: Action):
result = state.board.build_road(action.color, edge)
previous_road_color, road_color, road_lengths = result
build_road(state, action.color, edge, False)
mantain_longest_road(state, previous_road_color, road_color, road_lengths)
maintain_longest_road(state, previous_road_color, road_color, road_lengths)

# state.current_player_index stays the same
# state.current_prompt stays as PLAY
Expand Down
6 changes: 3 additions & 3 deletions catanatron_core/catanatron/state_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)


def mantain_longest_road(state, previous_road_color, road_color, road_lengths):
def maintain_longest_road(state, previous_road_color, road_color, road_lengths):
for color, length in road_lengths.items():
key = player_key(state, color)
state.player_state[f"{key}_LONGEST_ROAD_LENGTH"] = length
Expand All @@ -40,7 +40,7 @@ def mantain_longest_road(state, previous_road_color, road_color, road_lengths):
state.player_state[f"{loser_key}_ACTUAL_VICTORY_POINTS"] -= 2


def mantain_largets_army(state, color, previous_army_color, previous_army_size):
def maintain_largest_army(state, color, previous_army_color, previous_army_size):
candidate_size = get_played_dev_cards(state, color, "KNIGHT")
if candidate_size >= 3:
if previous_army_color is None:
Expand Down Expand Up @@ -317,7 +317,7 @@ def play_dev_card(state, color, dev_card):
state.player_state[f"{key}_HAS_PLAYED_DEVELOPMENT_CARD_IN_TURN"] = True
state.player_state[f"{key}_PLAYED_{dev_card}"] += 1
if dev_card == "KNIGHT":
mantain_largets_army(state, color, previous_army_color, previous_army_size) # type: ignore
maintain_largest_army(state, color, previous_army_color, previous_army_size) # type: ignore


def player_clean_turn(state, color):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
from collections import defaultdict

import tensorflow as tf
import numpy as np
import pandas as pd

Expand Down Expand Up @@ -181,6 +180,8 @@ def before(self, game):
)

def step(self, game, action):
import tensorflow as tf # lazy import tf so that catanatron simulator is usable without tf

self.data[action.color]["samples"].append(create_sample(game, action.color))
self.data[action.color]["actions"].append(
[to_action_space(action), to_action_type_space(action)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pathlib import Path

import pandas as pd
import tensorflow as tf

from catanatron_gym.features import (
create_sample_vector,
Expand Down Expand Up @@ -30,6 +29,8 @@ def __init__(self, output_path):
self.log_lines = []

def consume(self, game, mcts_labels):
import tensorflow as tf # lazy import tf so that catanatron simulator is usable without tf

for color in game.state.colors:
sample = create_sample_vector(game, color)
flattened_board_tensor = tf.reshape(
Expand Down
2 changes: 1 addition & 1 deletion catanatron_server/catanatron_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def create_app(test_config=None):
# ===== Initialize Database
from catanatron_server.models import db

db.init_app(app)
with app.app_context():
db.init_app(app)
db.create_all()

# ===== Initialize Routes
Expand Down
5 changes: 0 additions & 5 deletions tests/integration_tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ def app():
# create the app with common test config
app = create_app({"TESTING": True, "SQLALCHEMY_DATABASE_URI": "sqlite:///:memory:"})

# create the database and load test data
with app.app_context():
db.init_app(app)
db.create_all()

yield app


Expand Down
25 changes: 9 additions & 16 deletions tests/test_machine_learning.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import math
import random

import tensorflow as tf
import numpy as np

from tests.utils import advance_to_play_turn, build_initial_placements
Expand Down Expand Up @@ -363,8 +362,8 @@ def test_robber_plane_simple():
robber_channel = 13
tensor = create_board_tensor(game, players[0].color)

assert tf.math.reduce_sum(tensor[:, :, robber_channel]) == 6
assert tf.math.reduce_max(tensor[:, :, robber_channel]) == 1
assert np.sum(tensor[:, :, robber_channel]) == 6
assert np.max(tensor[:, :, robber_channel]) == 1


def test_resource_proba_planes():
Expand All @@ -389,31 +388,25 @@ def test_resource_proba_planes():
# Top left should be 0 for all resources. (water tile)
for resource_channel in range(4, 9):
top_left_tile = tensor[0:4, 0:2, resource_channel]
assert tf.math.reduce_all(tf.math.equal(top_left_tile, 0)).numpy()
assert np.all(np.equal(top_left_tile, 0))

# Assert ten sheep left edge looks good
sheep_channel = 10
ten_sheep_left_edge = tensor[4, 0:3, sheep_channel]
ten_proba = number_probability(10)
assert tf.math.reduce_sum(ten_sheep_left_edge) == ten_proba * 2 # 2 nodes
assert np.sum(ten_sheep_left_edge) == ten_proba * 2 # 2 nodes

# assert 5 wood top node has sheep too.
wood_channel = 8
five_proba = number_probability(5)
five_wood_top_node = tensor[4, 2]
assert tf.math.reduce_all(
tf.math.equal(five_wood_top_node[sheep_channel], ten_proba)
).numpy()
assert tf.math.reduce_all(
tf.math.equal(five_wood_top_node[wood_channel], five_proba)
).numpy()
assert np.all(np.equal(five_wood_top_node[sheep_channel], ten_proba))
assert np.all(np.equal(five_wood_top_node[wood_channel], five_proba))

# assert wood node adds up
total_proba = five_proba + number_probability(11) + number_probability(3)
middle_wood_node = tensor[4, 4]
assert tf.math.reduce_all(
tf.math.equal(middle_wood_node[wood_channel], total_proba)
).numpy()
assert np.all(np.equal(middle_wood_node[wood_channel], total_proba))

# assert brick tile has 6 non-zero node as expected
four_proba = number_probability(4)
Expand Down Expand Up @@ -443,10 +436,10 @@ def test_port_planes():
tensor = create_board_tensor(game, players[0].color)

# assert there are 18 port nodes (4 3:1 and 5 resource)
assert tf.math.reduce_sum(tensor[:, :, -6:]) == 2 * 9
assert np.sum(tensor[:, :, -6:]) == 2 * 9

# assert that 3:1 ports there are 4 * 2 nodes on.
assert tf.math.reduce_sum(tensor[:, :, -1]) == 2 * 4
assert np.sum(tensor[:, :, -1]) == 2 * 4


def test_robber_plane():
Expand Down
2 changes: 2 additions & 0 deletions ui/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# .npmrc
engine-strict=true
1 change: 1 addition & 0 deletions ui/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
4 changes: 4 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"react-zoom-pan-pinch": "^1.6.1",
"sass": "^1.54.5"
},
"engines" : {
"npm" : ">=8.0.0 <9.0.0",
"node" : ">=16.0.0 <17.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
Expand Down

0 comments on commit d46f82c

Please sign in to comment.