Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion catanatron/catanatron/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def production_features(game: Game, p0_color: Color):

@functools.lru_cache(maxsize=1000)
def get_node_production(catan_map, node_id, resource):
tiles = catan_map.adjacent_tiles[node_id]
tiles = catan_map.node_to_tiles[node_id]
return sum([number_probability(t.number) for t in tiles if t.resource == resource])


Expand Down
5 changes: 4 additions & 1 deletion catanatron/catanatron/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def default(self, obj):
{"coordinate": coordinate, "tile": self.default(tile)}
for coordinate, tile in obj.state.board.map.tiles.items()
],
"adjacent_tiles": obj.state.board.map.adjacent_tiles,
"map": {
"node_to_tiles": obj.state.board.map.node_to_tiles,
"node_production": obj.state.board.map.node_production,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need node_production here? (cc: @bcollazo)

},
"nodes": nodes,
"edges": list(edges.values()),
"actions": [self.default(a) for a in obj.state.actions],
Expand Down
31 changes: 15 additions & 16 deletions catanatron/catanatron/models/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __init__(
land_tiles: Dict[Coordinate, LandTile] = dict(),
port_nodes: Dict[Union[FastResource, None], Set[int]] = dict(),
land_nodes: FrozenSet[NodeId] = frozenset(),
adjacent_tiles: Dict[int, List[LandTile]] = dict(),
node_to_tiles: Dict[int, List[LandTile]] = dict(),
node_production: Dict[NodeId, Counter] = dict(),
tiles_by_id: Dict[int, LandTile] = dict(),
ports_by_id: Dict[int, Port] = dict(),
Expand All @@ -210,7 +210,7 @@ def __init__(
self.land_tiles = land_tiles
self.port_nodes = port_nodes
self.land_nodes = land_nodes
self.adjacent_tiles = adjacent_tiles
self.node_to_tiles = node_to_tiles
self.node_production = node_production
self.tiles_by_id = tiles_by_id
self.ports_by_id = ports_by_id
Expand All @@ -236,9 +236,8 @@ def from_tiles(tiles: Dict[Coordinate, Tile]):
land_nodes_list = map(lambda t: set(t.nodes.values()), self.land_tiles.values())
self.land_nodes = frozenset().union(*land_nodes_list)

# TODO: Rename to self.node_to_tiles
self.adjacent_tiles = init_adjacent_tiles(self.land_tiles)
self.node_production = init_node_production(self.adjacent_tiles)
self.node_to_tiles = init_node_to_tiles(self.land_tiles)
self.node_production = init_node_production(self.node_to_tiles)
self.tiles_by_id = {
t.id: t for t in self.tiles.values() if isinstance(t, LandTile)
}
Expand Down Expand Up @@ -270,30 +269,30 @@ def init_port_nodes_cache(
return port_nodes


def init_adjacent_tiles(
def init_node_to_tiles(
land_tiles: Dict[Coordinate, LandTile],
) -> Dict[int, List[LandTile]]:
adjacent_tiles = defaultdict(list) # node_id => tile[3]

node_to_tiles = defaultdict(list) # node_id => tile[3]
for tile in land_tiles.values():
for node_id in tile.nodes.values():
adjacent_tiles[node_id].append(tile)
return adjacent_tiles
node_to_tiles[node_id].append(tile)
return node_to_tiles


def init_node_production(
adjacent_tiles: Dict[int, List[LandTile]],
node_to_tiles: Dict[int, List[LandTile]],
) -> Dict[NodeId, Counter]:
"""Returns node_id => Counter({WHEAT: 0.123, ...})"""
node_production = dict()
for node_id in adjacent_tiles.keys():
node_production[node_id] = get_node_counter_production(adjacent_tiles, node_id)
node_production = {}
for node_id in node_to_tiles.keys():
node_production[node_id] = get_node_counter_production(node_to_tiles, node_id)
return node_production


def get_node_counter_production(
adjacent_tiles: Dict[int, List[LandTile]], node_id: NodeId
node_to_tiles: Dict[int, List[LandTile]], node_id: NodeId
):
tiles = adjacent_tiles[node_id]
tiles = node_to_tiles[node_id]
production = defaultdict(float)
for tile in tiles:
if tile.resource is not None:
Expand Down
10 changes: 6 additions & 4 deletions catanatron/catanatron/players/tree_search_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ def list_prunned_actions(game):

# Prune Initial Settlements at 1-tile places
if ActionType.BUILD_SETTLEMENT in types and game.state.is_initial_build_phase:
actions = filter(
lambda a: len(game.state.board.map.adjacent_tiles[a.value]) != 1, actions
actions = list(
filter(
lambda a: len(game.state.board.map.node_to_tiles[a.value]) != 1, actions
)
)

# Prune Trading if can hold for resources. Only for rare resources.
Expand Down Expand Up @@ -160,9 +162,9 @@ def prune_robber_actions(current_color, game, actions):
enemy_color = next(filter(lambda c: c != current_color, game.state.colors))
enemy_owned_tiles = set()
for node_id in get_player_buildings(game.state, enemy_color, SETTLEMENT):
enemy_owned_tiles.update(game.state.board.map.adjacent_tiles[node_id])
enemy_owned_tiles.update(game.state.board.map.node_to_tiles[node_id])
for node_id in get_player_buildings(game.state, enemy_color, CITY):
enemy_owned_tiles.update(game.state.board.map.adjacent_tiles[node_id])
enemy_owned_tiles.update(game.state.board.map.node_to_tiles[node_id])

robber_moves = set(
filter(
Expand Down
2 changes: 1 addition & 1 deletion catanatron/catanatron/players/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def fn(game, p0_color):
owned_nodes = buildings[SETTLEMENT] + buildings[CITY]
owned_tiles = set()
for n in owned_nodes:
owned_tiles.update(game.state.board.map.adjacent_tiles[n])
owned_tiles.update(game.state.board.map.node_to_tiles[n])
num_tiles = len(owned_tiles)

# TODO: Simplify to linear(?)
Expand Down
2 changes: 1 addition & 1 deletion catanatron/catanatron/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def apply_action(state: State, action: Action):
is_second_house = len(buildings) == 2
if is_second_house:
key = player_key(state, action.color)
for tile in state.board.map.adjacent_tiles[node_id]:
for tile in state.board.map.node_to_tiles[node_id]:
if tile.resource != None:
freqdeck_draw(state.resource_freqdeck, 1, tile.resource) # type: ignore
state.player_state[f"{key}_{tile.resource}_IN_HAND"] += 1
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_mini_map_can_be_created():
assert len(mini.tiles_by_id) == 7
assert len(mini.ports_by_id) == 0
assert len(mini.port_nodes) == 0
assert len(mini.adjacent_tiles) == 24
assert len(mini.node_to_tiles) == 24
assert len(mini.node_production) == 24

resources = [i.resource for i in mini.land_tiles.values()]
Expand Down