Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MatchChunk dataclass #1456

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 22 additions & 2 deletions axelrod/match_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
from dataclasses import dataclass
from typing import Any, Dict, Iterator, Tuple

from axelrod.random_ import BulkRandomGenerator


@dataclass
class MatchChunk(object):
index_pair: Tuple[int]
match_params: Dict[str, Any]
repetitions: int
seed: BulkRandomGenerator

def as_tuple(self) -> Tuple:
"""Kept for legacy reasons"""
return (self.index_pair, self.match_params, self.repetitions, self.seed)


class MatchGenerator(object):
def __init__(
self,
Expand Down Expand Up @@ -62,7 +77,7 @@ def __init__(
def __len__(self):
return self.size

def build_match_chunks(self):
def build_match_chunks(self) -> Iterator[MatchChunk]:
"""
A generator that returns player index pairs and match parameters for a
round robin tournament.
Expand All @@ -80,7 +95,12 @@ def build_match_chunks(self):
for index_pair in edges:
match_params = self.build_single_match_params()
r = next(self.random_generator)
yield (index_pair, match_params, self.repetitions, r)
yield MatchChunk(
index_pair=index_pair,
match_params=match_params,
repetitions=self.repetitions,
seed=r,
)

def build_single_match_params(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions axelrod/tests/unit/test_match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_build_match_chunks(self, repetitions):
game=test_game,
repetitions=repetitions,
)
chunks = list(rr.build_match_chunks())
chunks = [chunk.as_tuple() for chunk in rr.build_match_chunks()]
match_definitions = [
tuple(list(index_pair) + [repetitions])
for (index_pair, match_params, repetitions, _) in chunks
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_spatial_build_match_chunks(self, repetitions):
edges=cycle,
repetitions=repetitions,
)
chunks = list(rr.build_match_chunks())
chunks = [chunk.as_tuple() for chunk in rr.build_match_chunks()]
match_definitions = [
tuple(list(index_pair) + [repetitions])
for (index_pair, match_params, repetitions, _) in chunks
Expand Down
6 changes: 4 additions & 2 deletions axelrod/tests/unit/test_tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
strategy_lists,
tournaments,
)
from axelrod.tournament import _close_objects
from axelrod.tournament import MatchChunk, _close_objects

C, D = axl.Action.C, axl.Action.D

Expand Down Expand Up @@ -663,7 +663,9 @@ def make_chunk_generator():
for player2_index in range(player1_index, len(self.players)):
index_pair = (player1_index, player2_index)
match_params = {"turns": turns, "game": self.game}
yield (index_pair, match_params, self.test_repetitions, 0)
yield MatchChunk(
index_pair, match_params, self.test_repetitions, 0
)

chunk_generator = make_chunk_generator()
interactions = {}
Expand Down
17 changes: 8 additions & 9 deletions axelrod/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from .game import Game
from .match import Match
from .match_generator import MatchGenerator
from .match_generator import MatchChunk, MatchGenerator
from .result_set import ResultSet

C, D = Action.C, Action.D
Expand Down Expand Up @@ -427,7 +427,7 @@ def _worker(
done_queue.put("STOP")
return True

def _play_matches(self, chunk, build_results=True):
def _play_matches(self, chunk: Match, build_results: bool = True):
"""
Play matches in a given chunk.

Expand All @@ -446,22 +446,21 @@ def _play_matches(self, chunk, build_results=True):
(0, 1) -> [(C, D), (D, C),...]
"""
interactions = defaultdict(list)
index_pair, match_params, repetitions, seed = chunk
p1_index, p2_index = index_pair
p1_index, p2_index = chunk.index_pair
player1 = self.players[p1_index].clone()
player2 = self.players[p2_index].clone()
match_params["players"] = (player1, player2)
match_params["seed"] = seed
match = Match(**match_params)
for _ in range(repetitions):
chunk.match_params["players"] = (player1, player2)
chunk.match_params["seed"] = chunk.seed
match = Match(**chunk.match_params)
for _ in range(chunk.repetitions):
match.play()

if build_results:
results = self._calculate_results(match.result)
else:
results = None

interactions[index_pair].append([match.result, results])
interactions[chunk.index_pair].append([match.result, results])
return interactions

def _calculate_results(self, interactions):
Expand Down
Loading