From 945af1bbd79f15e82f89c65b4f2797ebf51643a5 Mon Sep 17 00:00:00 2001 From: "T.J. Gaffney" Date: Fri, 10 Apr 2020 00:45:27 -0700 Subject: [PATCH 01/10] Add check to config.yml --- .github/workflows/config.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index e309d38b2..b69295dd5 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -58,6 +58,9 @@ jobs: run: | python -m pip install pylint python -m pylint --disable=all --enable=unused-import axelrod/strategies/_strategies.py + - name: Check format + run: | + python -m black -l 80 . --check - name: Check that installs run: | python setup.py install From 4187b39fe742d5403d65cc073e4bba52e99a5e60 Mon Sep 17 00:00:00 2001 From: "T.J. Gaffney" Date: Fri, 10 Apr 2020 01:59:56 -0700 Subject: [PATCH 02/10] Install black before trying to use it. --- .github/workflows/config.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index b69295dd5..41fb9db4f 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -33,6 +33,12 @@ jobs: python -m pip install docutils==0.12 python -m pip install mock cd docs; make clean; make html; cd ..; + - name: Install testing dependencies + run: | + python -m pip install coverage + python -m pip install hypothesis==3.2 + python -m pip install mypy + python -m pip install black - name: Run tests run: | python -m pip install coverage From 4e95db1222f9676f33d8aac0b1d33bc7fa11fe2a Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 11:18:52 +0100 Subject: [PATCH 03/10] Move black installation This is just to avoid installing in case the CI fails beforehand. --- .github/workflows/config.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index 41fb9db4f..0f9a5eea1 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -37,8 +37,6 @@ jobs: run: | python -m pip install coverage python -m pip install hypothesis==3.2 - python -m pip install mypy - python -m pip install black - name: Run tests run: | python -m pip install coverage @@ -66,6 +64,7 @@ jobs: python -m pylint --disable=all --enable=unused-import axelrod/strategies/_strategies.py - name: Check format run: | + python -m pip install black python -m black -l 80 . --check - name: Check that installs run: | From c50a6e3427e2731167907e44610ace3c6d7c7c20 Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 11:20:59 +0100 Subject: [PATCH 04/10] Run black --- .prepare-commit-msg.py | 4 +- axelrod/evolvable_player.py | 3 +- axelrod/fingerprint.py | 40 +- axelrod/game.py | 11 +- axelrod/graph.py | 5 +- axelrod/history.py | 8 +- axelrod/interaction_utils.py | 3 +- axelrod/match.py | 5 +- axelrod/match_generator.py | 2 +- axelrod/moran.py | 46 ++- axelrod/player.py | 24 +- axelrod/plot.py | 8 +- axelrod/random_.py | 12 +- axelrod/result_set.py | 71 +++- axelrod/strategies/__init__.py | 4 +- axelrod/strategies/_filters.py | 18 +- axelrod/strategies/adaptor.py | 20 +- axelrod/strategies/ann.py | 89 ++-- axelrod/strategies/axelrod_first.py | 71 +++- axelrod/strategies/axelrod_second.py | 99 ++++- axelrod/strategies/backstabber.py | 4 +- axelrod/strategies/bush_mosteller.py | 21 +- axelrod/strategies/calculator.py | 5 +- axelrod/strategies/cycler.py | 30 +- axelrod/strategies/dbs.py | 32 +- axelrod/strategies/defector.py | 5 +- axelrod/strategies/finite_state_machines.py | 95 +++-- axelrod/strategies/gambler.py | 10 +- axelrod/strategies/gobymajority.py | 4 +- axelrod/strategies/grudger.py | 2 +- axelrod/strategies/hmm.py | 148 +++++-- axelrod/strategies/human.py | 12 +- axelrod/strategies/hunter.py | 14 +- axelrod/strategies/lookerup.py | 109 +++-- axelrod/strategies/memoryone.py | 8 +- axelrod/strategies/memorytwo.py | 25 +- axelrod/strategies/meta.py | 16 +- axelrod/strategies/prober.py | 2 +- axelrod/strategies/punisher.py | 8 +- axelrod/strategies/qlearner.py | 20 +- axelrod/strategies/shortmem.py | 2 +- axelrod/strategies/titfortat.py | 9 +- axelrod/strategies/zero_determinant.py | 20 +- axelrod/strategy_transformers.py | 31 +- axelrod/tests/integration/test_filtering.py | 16 +- axelrod/tests/integration/test_matches.py | 8 +- axelrod/tests/integration/test_names.py | 2 +- axelrod/tests/integration/test_tournament.py | 26 +- axelrod/tests/property.py | 85 ++-- axelrod/tests/strategies/test_adaptive.py | 14 +- axelrod/tests/strategies/test_ann.py | 5 +- axelrod/tests/strategies/test_apavlov.py | 105 ++++- .../tests/strategies/test_axelrod_first.py | 159 +++++-- .../tests/strategies/test_axelrod_second.py | 391 +++++++++++++----- axelrod/tests/strategies/test_backstabber.py | 10 +- .../strategies/test_better_and_better.py | 12 +- axelrod/tests/strategies/test_calculator.py | 54 ++- axelrod/tests/strategies/test_cooperator.py | 10 +- axelrod/tests/strategies/test_cycler.py | 16 +- axelrod/tests/strategies/test_darwin.py | 12 +- axelrod/tests/strategies/test_dbs.py | 24 +- axelrod/tests/strategies/test_doubler.py | 4 +- .../tests/strategies/test_evolvable_player.py | 36 +- .../strategies/test_finite_state_machines.py | 107 +++-- axelrod/tests/strategies/test_forgiver.py | 20 +- axelrod/tests/strategies/test_gambler.py | 44 +- axelrod/tests/strategies/test_geller.py | 22 +- axelrod/tests/strategies/test_gobymajority.py | 38 +- axelrod/tests/strategies/test_grudger.py | 39 +- axelrod/tests/strategies/test_grumpy.py | 20 +- axelrod/tests/strategies/test_hmm.py | 22 +- axelrod/tests/strategies/test_human.py | 4 +- axelrod/tests/strategies/test_inverse.py | 18 +- axelrod/tests/strategies/test_lookerup.py | 55 ++- axelrod/tests/strategies/test_memoryone.py | 81 +++- axelrod/tests/strategies/test_memorytwo.py | 31 +- axelrod/tests/strategies/test_meta.py | 70 +++- axelrod/tests/strategies/test_negation.py | 4 +- axelrod/tests/strategies/test_player.py | 58 ++- axelrod/tests/strategies/test_prober.py | 31 +- axelrod/tests/strategies/test_qlearner.py | 32 +- axelrod/tests/strategies/test_rand.py | 8 +- axelrod/tests/strategies/test_retaliate.py | 12 +- .../tests/strategies/test_revised_downing.py | 1 + axelrod/tests/strategies/test_selfsteem.py | 31 +- axelrod/tests/strategies/test_shortmem.py | 22 +- axelrod/tests/strategies/test_stalker.py | 13 +- axelrod/tests/strategies/test_titfortat.py | 152 +++++-- .../tests/strategies/test_worse_and_worse.py | 3 +- .../tests/strategies/test_zero_determinant.py | 83 +++- axelrod/tests/unit/test_classification.py | 47 ++- ...est_compute_finite_state_machine_memory.py | 2 +- .../tests/unit/test_deterministic_cache.py | 4 +- axelrod/tests/unit/test_filters.py | 38 +- axelrod/tests/unit/test_fingerprint.py | 101 +++-- axelrod/tests/unit/test_graph.py | 127 ++++-- axelrod/tests/unit/test_history.py | 12 +- axelrod/tests/unit/test_interaction_utils.py | 81 +++- axelrod/tests/unit/test_match.py | 26 +- axelrod/tests/unit/test_match_generator.py | 28 +- axelrod/tests/unit/test_moran.py | 100 +++-- axelrod/tests/unit/test_pickling.py | 65 ++- axelrod/tests/unit/test_plot.py | 51 ++- axelrod/tests/unit/test_property.py | 16 +- axelrod/tests/unit/test_resultset.py | 213 ++++++++-- .../tests/unit/test_strategy_transformers.py | 116 ++++-- axelrod/tests/unit/test_strategy_utils.py | 15 +- axelrod/tests/unit/test_tournament.py | 111 +++-- axelrod/tournament.py | 49 ++- docs/conf.py | 12 +- doctests.py | 3 +- 111 files changed, 3264 insertions(+), 1138 deletions(-) diff --git a/.prepare-commit-msg.py b/.prepare-commit-msg.py index 780f8594b..dac608fd9 100755 --- a/.prepare-commit-msg.py +++ b/.prepare-commit-msg.py @@ -41,7 +41,9 @@ if branch.startswith(issue_prefix): issue_number = re.match("%s(.*)" % issue_prefix, branch).group(1) - print("prepare-commit-msg: Prepending [#%s] to commit message" % issue_number) + print( + "prepare-commit-msg: Prepending [#%s] to commit message" % issue_number + ) with open(commit_msg_filepath, "r+") as f: content = f.read() diff --git a/axelrod/evolvable_player.py b/axelrod/evolvable_player.py index 7dd4073b3..ce32bf0e5 100644 --- a/axelrod/evolvable_player.py +++ b/axelrod/evolvable_player.py @@ -7,6 +7,7 @@ class InsufficientParametersError(Exception): """Error indicating that insufficient parameters were specified to initialize an Evolvable Player.""" + def __init__(self, *args): super().__init__(*args) @@ -49,7 +50,7 @@ def create_new(self, **kwargs): def serialize_parameters(self): """Serialize parameters.""" pickled = dumps(self.init_kwargs) # bytes - s = base64.b64encode(pickled).decode('utf8') # string + s = base64.b64encode(pickled).decode("utf8") # string return s @classmethod diff --git a/axelrod/fingerprint.py b/axelrod/fingerprint.py index 264856a15..8ef34d60b 100644 --- a/axelrod/fingerprint.py +++ b/axelrod/fingerprint.py @@ -42,7 +42,7 @@ def _create_points(step: float, progress_bar: bool = True) -> List[Point]: num = int((1 / step) // 1) + 1 if progress_bar: - p_bar = tqdm.tqdm(total=num**2, desc="Generating points") + p_bar = tqdm.tqdm(total=num ** 2, desc="Generating points") points = [] for x in np.linspace(0, 1, num): @@ -88,8 +88,8 @@ def _create_jossann(point: Point, probe: Any) -> Player: if x + y >= 1: joss_ann = DualTransformer()( - JossAnnTransformer((1 - x, 1 - y))( - probe_class))(**init_kwargs) + JossAnnTransformer((1 - x, 1 - y))(probe_class) + )(**init_kwargs) else: joss_ann = JossAnnTransformer((x, y))(probe_class)(**init_kwargs) return joss_ann @@ -177,7 +177,10 @@ def _generate_data(interactions: dict, points: list, edges: list) -> dict: """ edge_scores = [ np.mean( - [compute_final_score_per_turn(scores)[0] for scores in interactions[edge]] + [ + compute_final_score_per_turn(scores)[0] + for scores in interactions[edge] + ] ) for edge in edges ] @@ -215,7 +218,9 @@ def _reshape_data(data: dict, points: list, size: int) -> np.ndarray: class AshlockFingerprint(object): def __init__( - self, strategy: Union[type, Player], probe: Union[type, Player] = axl.TitForTat + self, + strategy: Union[type, Player], + probe: Union[type, Player] = axl.TitForTat, ) -> None: """ Parameters @@ -277,7 +282,7 @@ def fingerprint( processes: int = None, filename: str = None, progress_bar: bool = True, - seed: int = None + seed: int = None, ) -> dict: """Build and play the spatial tournament. @@ -323,8 +328,11 @@ def fingerprint( self.step = step self.spatial_tournament = axl.Tournament( - tourn_players, turns=turns, repetitions=repetitions, edges=edges, - seed=seed + tourn_players, + turns=turns, + repetitions=repetitions, + edges=edges, + seed=seed, ) self.spatial_tournament.play( build_results=False, @@ -432,7 +440,7 @@ def fingerprint( processes: int = None, filename: str = None, progress_bar: bool = True, - seed: int = None + seed: int = None, ) -> np.array: """Creates a spatial tournament to run the necessary matches to obtain fingerprint data. @@ -479,7 +487,7 @@ def fingerprint( turns=turns, noise=noise, repetitions=repetitions, - seed=seed + seed=seed, ) tournament.play( filename=filename, @@ -516,7 +524,9 @@ def analyse_cooperation_ratio(filename): opponent in each turn. The ith row corresponds to the ith opponent and the jth column the jth turn. """ - did_c = np.vectorize(lambda actions: [int(action == "C") for action in actions]) + did_c = np.vectorize( + lambda actions: [int(action == "C") for action in actions] + ) cooperation_rates = {} df = dd.read_csv(filename) @@ -525,7 +535,10 @@ def analyse_cooperation_ratio(filename): df = df[df["Player index"] == 0][["Opponent index", "Actions"]] for _, row in df.iterrows(): - opponent_index, player_history = row["Opponent index"], row["Actions"] + opponent_index, player_history = ( + row["Opponent index"], + row["Actions"], + ) if opponent_index in cooperation_rates: cooperation_rates[opponent_index].append(did_c(player_history)) else: @@ -590,7 +603,8 @@ def plot( if display_names: plt.yticks( - range(len(self.opponents)), [str(player) for player in self.opponents] + range(len(self.opponents)), + [str(player) for player in self.opponents], ) else: plt.yticks([0, len(self.opponents) - 1], [0, 1]) diff --git a/axelrod/game.py b/axelrod/game.py index 1c3278275..af30aca29 100644 --- a/axelrod/game.py +++ b/axelrod/game.py @@ -16,7 +16,9 @@ class Game(object): The numerical score attribute to all combinations of action pairs. """ - def __init__(self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1) -> None: + def __init__( + self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1 + ) -> None: """Create a new game object. Parameters @@ -30,7 +32,12 @@ def __init__(self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1) -> No p: int or float Score obtained by both player for mutual defection. """ - self.scores = {(C, C): (r, r), (D, D): (p, p), (C, D): (s, t), (D, C): (t, s)} + self.scores = { + (C, C): (r, r), + (D, D): (p, p), + (C, D): (s, t), + (D, C): (t, s), + } def RPST(self) -> Tuple[Score, Score, Score, Score]: """Returns game matrix values in Press and Dyson notation.""" diff --git a/axelrod/graph.py b/axelrod/graph.py index 9f41bde4d..6b91677b0 100644 --- a/axelrod/graph.py +++ b/axelrod/graph.py @@ -155,8 +155,9 @@ def attached_complete_graphs(length, loops=True, directed=False): for cluster in range(2): for i in range(length): for j in range(i + 1, length): - edges.append(("{}:{}".format(cluster, i), - "{}:{}".format(cluster, j))) + edges.append( + ("{}:{}".format(cluster, i), "{}:{}".format(cluster, j)) + ) # Attach at one node edges.append(("0:0", "1:0")) graph = Graph(directed=directed, edges=edges) diff --git a/axelrod/history.py b/axelrod/history.py index 9c0ff2ef1..aea7b1f64 100644 --- a/axelrod/history.py +++ b/axelrod/history.py @@ -82,7 +82,9 @@ def __eq__(self, other): if isinstance(other, list): return self._plays == other elif isinstance(other, History): - return self._plays == other._plays and self._coplays == other._coplays + return ( + self._plays == other._plays and self._coplays == other._coplays + ) raise TypeError("Cannot compare types.") def __getitem__(self, key): @@ -121,7 +123,9 @@ def __init__(self, memory_depth, plays=None, coplays=None): def flip_plays(self): """Creates a flipped plays history for use with DualTransformer.""" flipped_plays = [action.flip() for action in self._plays] - return self.__class__(self.memory_depth, plays=flipped_plays, coplays=self._coplays) + return self.__class__( + self.memory_depth, plays=flipped_plays, coplays=self._coplays + ) def append(self, play, coplay): """Appends a new (play, coplay) pair an updates metadata for diff --git a/axelrod/interaction_utils.py b/axelrod/interaction_utils.py index 82159f94c..220053ee3 100644 --- a/axelrod/interaction_utils.py +++ b/axelrod/interaction_utils.py @@ -32,7 +32,8 @@ def compute_final_score(interactions, game=None): return None final_score = tuple( - sum([score[player_index] for score in scores]) for player_index in [0, 1] + sum([score[player_index] for score in scores]) + for player_index in [0, 1] ) return final_score diff --git a/axelrod/match.py b/axelrod/match.py index 907e85f44..19c83abd9 100644 --- a/axelrod/match.py +++ b/axelrod/match.py @@ -29,7 +29,7 @@ def __init__( noise=0, match_attributes=None, reset=True, - seed=None + seed=None, ): """ Parameters @@ -193,7 +193,8 @@ def play(self): result = [] for _ in range(turns): plays = self.simultaneous_play( - self.players[0], self.players[1], self.noise) + self.players[0], self.players[1], self.noise + ) result.append(plays) if self._cache_update_required: diff --git a/axelrod/match_generator.py b/axelrod/match_generator.py index c0ad188bd..37038e7b5 100644 --- a/axelrod/match_generator.py +++ b/axelrod/match_generator.py @@ -12,7 +12,7 @@ def __init__( prob_end=None, edges=None, match_attributes=None, - seed=None + seed=None, ): """ A class to generate matches. This is used by the Tournament class which diff --git a/axelrod/moran.py b/axelrod/moran.py index 3c9876c40..af235e1d9 100644 --- a/axelrod/moran.py +++ b/axelrod/moran.py @@ -28,7 +28,7 @@ def __init__( fitness_transformation: Callable = None, mutation_method="transition", stop_on_fixation=True, - seed=None + seed=None, ) -> None: """ An agent based Moran process class. In each round, each player plays a @@ -93,7 +93,9 @@ def __init__( if m in ["atomic", "transition"]: self.mutation_method = m else: - raise ValueError("Invalid mutation method {}".format(mutation_method)) + raise ValueError( + "Invalid mutation method {}".format(mutation_method) + ) assert (mutation_rate >= 0) and (mutation_rate <= 1) assert (noise >= 0) and (noise <= 1) mode = mode.lower() @@ -127,7 +129,9 @@ def __init__( d[str(p)] = p mutation_targets = dict() for key in sorted(keys): - mutation_targets[key] = [v for (k, v) in sorted(d.items()) if k != key] + mutation_targets[key] = [ + v for (k, v) in sorted(d.items()) if k != key + ] self.mutation_targets = mutation_targets if interaction_graph is None: @@ -146,14 +150,18 @@ def __init__( self.fitness_transformation = fitness_transformation # Map players to graph vertices self.locations = sorted(interaction_graph.vertices) - self.index = dict(zip(sorted(interaction_graph.vertices), range(len(players)))) + self.index = dict( + zip(sorted(interaction_graph.vertices), range(len(players))) + ) self.fixated = self.fixation_check() def set_players(self) -> None: """Copy the initial players into the first population, setting seeds as needed.""" self.players = [] for player in self.initial_players: - if (self.mutation_method == "atomic") and issubclass(player.__class__, EvolvablePlayer): + if (self.mutation_method == "atomic") and issubclass( + player.__class__, EvolvablePlayer + ): # For reproducibility, we generate random seeds for evolvable players. seed = next(self._bulk_random) new_player = player.create_new(seed=seed) @@ -163,8 +171,9 @@ def set_players(self) -> None: self.players.append(player) self.populations = [self.population_distribution()] - def fitness_proportionate_selection(self, - scores: List, fitness_transformation: Callable = None) -> int: + def fitness_proportionate_selection( + self, scores: List, fitness_transformation: Callable = None + ) -> int: """Randomly selects an individual proportionally to score. Parameters @@ -200,7 +209,9 @@ def mutate(self, index: int) -> Player: if self.mutation_method == "atomic": if not issubclass(self.players[index].__class__, EvolvablePlayer): - raise TypeError("Player is not evolvable. Use a subclass of EvolvablePlayer.") + raise TypeError( + "Player is not evolvable. Use a subclass of EvolvablePlayer." + ) return self.players[index].mutate() # Assuming mutation_method == "transition" @@ -237,7 +248,9 @@ def death(self, index: int = None) -> int: # Select locally # index is not None in this case vertex = self._random.choice( - sorted(self.reproduction_graph.out_vertices(self.locations[index])) + sorted( + self.reproduction_graph.out_vertices(self.locations[index]) + ) ) i = self.index[vertex] return i @@ -370,7 +383,7 @@ def score_all(self) -> List: noise=self.noise, game=self.game, deterministic_cache=self.deterministic_cache, - seed=next(self._bulk_random) + seed=next(self._bulk_random), ) match.play() match_scores = match.final_score_per_turn() @@ -484,8 +497,11 @@ class ApproximateMoranProcess(MoranProcess): """ def __init__( - self, players: List[Player], cached_outcomes: dict, mutation_rate: float = 0, - seed: Optional[int] = None + self, + players: List[Player], + cached_outcomes: dict, + mutation_rate: float = 0, + seed: Optional[int] = None, ) -> None: """ Parameters @@ -503,7 +519,7 @@ def __init__( noise=0, deterministic_cache=None, mutation_rate=mutation_rate, - seed=seed + seed=seed, ) self.cached_outcomes = cached_outcomes @@ -529,7 +545,9 @@ def score_all(self) -> List: scores = [0] * N for i in range(N): for j in range(i + 1, N): - player_names = tuple([str(self.players[i]), str(self.players[j])]) + player_names = tuple( + [str(self.players[i]), str(self.players[j])] + ) cached_score = self._get_scores_from_cache(player_names) scores[i] += cached_score[0] scores[j] += cached_score[1] diff --git a/axelrod/player.py b/axelrod/player.py index 6b7370be2..87c08ddb8 100644 --- a/axelrod/player.py +++ b/axelrod/player.py @@ -59,6 +59,7 @@ def _post_init(self): See here to learn more: https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ """ + def __call__(cls, *args, **kwargs): # This calls cls.__new__ and cls.__init__ obj = type.__call__(cls, *args, **kwargs) @@ -130,7 +131,9 @@ def __eq__(self, other): if self.__repr__() != other.__repr__(): return False - for attribute in set(list(self.__dict__.keys()) + list(other.__dict__.keys())): + for attribute in set( + list(self.__dict__.keys()) + list(other.__dict__.keys()) + ): value = getattr(self, attribute, None) other_value = getattr(other, attribute, None) @@ -148,14 +151,20 @@ def __eq__(self, other): ): # Split the original generator so it is not touched generator, original_value = itertools.tee(value) - other_generator, original_other_value = itertools.tee(other_value) + other_generator, original_other_value = itertools.tee( + other_value + ) if isinstance(value, types.GeneratorType): setattr(self, attribute, (ele for ele in original_value)) - setattr(other, attribute, (ele for ele in original_other_value)) + setattr( + other, attribute, (ele for ele in original_other_value) + ) else: setattr(self, attribute, itertools.cycle(original_value)) - setattr(other, attribute, itertools.cycle(original_other_value)) + setattr( + other, attribute, itertools.cycle(original_other_value) + ) for _ in range(200): try: @@ -190,7 +199,8 @@ def set_seed(self, seed): if seed is None: warnings.warn( "Initializing player with seed from Axelrod module random number generator. " - "Results may not be seed reproducible.") + "Results may not be seed reproducible." + ) self._seed = _module_random.random_seed_int() else: self._seed = seed @@ -201,7 +211,9 @@ def __repr__(self): Appends the `__init__` parameters to the strategy's name.""" name = self.name prefix = ": " - gen = (value for value in self.init_kwargs.values() if value is not None) + gen = ( + value for value in self.init_kwargs.values() if value is not None + ) for value in gen: try: if issubclass(value, Player): diff --git a/axelrod/plot.py b/axelrod/plot.py index f56cc9f4f..feacae984 100644 --- a/axelrod/plot.py +++ b/axelrod/plot.py @@ -97,7 +97,9 @@ def _winplot_dataset(self): # Sort wins by median wins = self.result_set.wins medians = map(median, wins) - medians = sorted([(m, i) for (i, m) in enumerate(medians)], reverse=True) + medians = sorted( + [(m, i) for (i, m) in enumerate(medians)], reverse=True + ) # Reorder and grab names wins = [wins[x[-1]] for x in medians] ranked_names = [str(self.players[x[-1]]) for x in medians] @@ -324,7 +326,9 @@ def save_all_plots( pbar = tqdm.tqdm(total=total, desc="Obtaining plots") for method, name in plots: - f = getattr(self, method)(title="{} - {}".format(title_prefix, name)) + f = getattr(self, method)( + title="{} - {}".format(title_prefix, name) + ) path = pathlib.Path("{}_{}.{}".format(prefix, method, filetype)) f.savefig(axl_filename(path)) plt.close(f) diff --git a/axelrod/random_.py b/axelrod/random_.py index 23949c93e..73289ff8f 100644 --- a/axelrod/random_.py +++ b/axelrod/random_.py @@ -11,6 +11,7 @@ class RandomGenerator(object): """Container around a random number generator. Enables reproducibility of player behavior, matches, and tournaments.""" + def __init__(self, seed: Optional[int] = None): # _random is the internal object that generators random values self._random = RandomState() @@ -28,7 +29,7 @@ def randint(self, *args, **kwargs): return self._random.randint(*args, **kwargs) def random_seed_int(self) -> int: - return self.randint(low=0, high=2**32-1, dtype="uint64") + return self.randint(low=0, high=2 ** 32 - 1, dtype="uint64") def choice(self, *args, **kwargs): return self._random.choice(*args, **kwargs) @@ -118,7 +119,8 @@ class BulkRandomGenerator(object): """Bulk generator of random integers for tournament seeding and reproducibility. Bulk generation of random values is more efficient. Use this class like a generator.""" - def __init__(self, seed=None, batch_size:int = 1000): + + def __init__(self, seed=None, batch_size: int = 1000): self._random_generator = RandomState() self._random_generator.seed(seed) self._ints = None @@ -130,10 +132,8 @@ def _fill_ints(self): # Generate more random values. Store as a list since generators # cannot be pickled. self._ints = self._random_generator.randint( - low=0, - high=2**32 - 1, - size=self._batch_size, - dtype="uint64") + low=0, high=2 ** 32 - 1, size=self._batch_size, dtype="uint64" + ) self._index = 0 def __next__(self): diff --git a/axelrod/result_set.py b/axelrod/result_set.py index 65353cc6d..ee6a515de 100644 --- a/axelrod/result_set.py +++ b/axelrod/result_set.py @@ -117,9 +117,15 @@ def _reshape_out( alternative=0, ) - self.wins = self._reshape_two_dim_list(sum_per_player_repetition_df["Win"]) - self.scores = self._reshape_two_dim_list(sum_per_player_repetition_df["Score"]) - self.normalised_scores = self._reshape_two_dim_list(normalised_scores_series) + self.wins = self._reshape_two_dim_list( + sum_per_player_repetition_df["Win"] + ) + self.scores = self._reshape_two_dim_list( + sum_per_player_repetition_df["Score"] + ) + self.normalised_scores = self._reshape_two_dim_list( + normalised_scores_series + ) self.cooperation = self._build_cooperation( sum_per_player_opponent_df["Cooperation count"] @@ -132,7 +138,9 @@ def _reshape_out( self.state_distribution = self._build_state_distribution( sum_per_player_opponent_df[columns] ) - self.normalised_state_distribution = self._build_normalised_state_distribution() + self.normalised_state_distribution = ( + self._build_normalised_state_distribution() + ) columns = [ "CC to C count", @@ -166,7 +174,9 @@ def _reshape_out( self.ranked_names = self._build_ranked_names() self.payoff_matrix = self._build_summary_matrix(self.payoffs) - self.payoff_stddevs = self._build_summary_matrix(self.payoffs, func=np.std) + self.payoff_stddevs = self._build_summary_matrix( + self.payoffs, func=np.std + ) self.payoff_diffs_means = self._build_payoff_diffs_means() self.cooperating_rating = self._build_cooperating_rating() @@ -266,7 +276,9 @@ def _build_good_partner_matrix(self, good_partner_series): # interactions. row.append(0) else: - row.append(good_partner_dict.get((player_index, opponent_index), 0)) + row.append( + good_partner_dict.get((player_index, opponent_index), 0) + ) good_partner_matrix.append(row) return good_partner_matrix @@ -334,13 +346,17 @@ def _build_normalised_state_distribution(self): for counter in player: total = sum(counter.values()) counters.append( - Counter({key: value / total for key, value in counter.items()}) + Counter( + {key: value / total for key, value in counter.items()} + ) ) normalised_state_distribution.append(counters) return normalised_state_distribution @update_progress_bar - def _build_state_to_action_distribution(self, state_to_action_distribution_series): + def _build_state_to_action_distribution( + self, state_to_action_distribution_series + ): state_to_action_key_map = { "CC to C count": ((C, C), C), "CC to D count": ((C, C), D), @@ -396,8 +412,12 @@ def _build_normalised_state_to_action_distribution(self): return normalised_state_to_action_distribution @update_progress_bar - def _build_initial_cooperation_count(self, initial_cooperation_count_series): - initial_cooperation_count_dict = initial_cooperation_count_series.to_dict() + def _build_initial_cooperation_count( + self, initial_cooperation_count_series + ): + initial_cooperation_count_dict = ( + initial_cooperation_count_series.to_dict() + ) initial_cooperation_count = [ initial_cooperation_count_dict.get(player_index, 0) for player_index in range(self.num_players) @@ -427,7 +447,8 @@ def _build_initial_cooperation_rate(self, interactions_series): warnings.simplefilter("ignore") initial_cooperation_rate = list( np.nan_to_num( - np.array(self.initial_cooperation_count) / interactions_array + np.array(self.initial_cooperation_count) + / interactions_array ) ) return initial_cooperation_rate @@ -453,7 +474,9 @@ def _build_eigenmoses_rating(self): The eigenmoses rating as defined in: http://www.scottaaronson.com/morality.pdf """ - eigenvector, eigenvalue = eigen.principal_eigenvector(self.vengeful_cooperation) + eigenvector, eigenvalue = eigen.principal_eigenvector( + self.vengeful_cooperation + ) return eigenvector.tolist() @@ -577,7 +600,9 @@ def _build_tasks(self, df): ] sum_per_player_opponent_task = df.groupby(groups)[columns].sum() - ignore_self_interactions_task = df["Player index"] != df["Opponent index"] + ignore_self_interactions_task = ( + df["Player index"] != df["Opponent index"] + ) adf = df[ignore_self_interactions_task] groups = ["Player index", "Repetition"] @@ -591,7 +616,9 @@ def _build_tasks(self, df): groups = ["Player index"] column = "Initial cooperation" initial_cooperation_count_task = adf.groupby(groups)[column].sum() - interactions_count_task = adf.groupby("Player index")["Player index"].count() + interactions_count_task = adf.groupby("Player index")[ + "Player index" + ].count() return ( mean_per_reps_player_opponent_task, @@ -642,8 +669,12 @@ def list_equal_with_nans(v1: List[float], v2: List[float]) -> bool: self.cooperating_rating == other.cooperating_rating, self.good_partner_matrix == other.good_partner_matrix, self.good_partner_rating == other.good_partner_rating, - list_equal_with_nans(self.eigenmoses_rating, other.eigenmoses_rating), - list_equal_with_nans(self.eigenjesus_rating, other.eigenjesus_rating), + list_equal_with_nans( + self.eigenmoses_rating, other.eigenmoses_rating + ), + list_equal_with_nans( + self.eigenjesus_rating, other.eigenjesus_rating + ), ] ) @@ -713,7 +744,9 @@ def summarise(self): rates = [] for state in states: counts = [ - counter[(state, C)] for counter in player if counter[(state, C)] > 0 + counter[(state, C)] + for counter in player + if counter[(state, C)] > 0 ] if len(counts) > 0: @@ -736,7 +769,9 @@ def summarise(self): summary_data = [] for rank, i in enumerate(self.ranking): - data = list(summary_measures[i]) + state_prob[i] + state_to_C_prob[i] + data = ( + list(summary_measures[i]) + state_prob[i] + state_to_C_prob[i] + ) summary_data.append(self.player(rank, *data)) return summary_data diff --git a/axelrod/strategies/__init__.py b/axelrod/strategies/__init__.py index a7635596e..537ee89bc 100644 --- a/axelrod/strategies/__init__.py +++ b/axelrod/strategies/__init__.py @@ -90,7 +90,9 @@ short_run_time_strategies = [ s for s in strategies if not Classifiers["long_run_time"](s()) ] -cheating_strategies = [s for s in all_strategies if not Classifiers.obey_axelrod(s())] +cheating_strategies = [ + s for s in all_strategies if not Classifiers.obey_axelrod(s()) +] ordinary_strategies = strategies # This is a legacy and will be removed diff --git a/axelrod/strategies/_filters.py b/axelrod/strategies/_filters.py index c9c199bf7..2dc7a88b6 100644 --- a/axelrod/strategies/_filters.py +++ b/axelrod/strategies/_filters.py @@ -155,15 +155,24 @@ class ExampleStrategy(Player): ), "manipulates_state": FilterFunction( function=passes_operator_filter, - kwargs={"classifier_key": "manipulates_state", "operator": operator.eq}, + kwargs={ + "classifier_key": "manipulates_state", + "operator": operator.eq, + }, ), "manipulates_source": FilterFunction( function=passes_operator_filter, - kwargs={"classifier_key": "manipulates_source", "operator": operator.eq}, + kwargs={ + "classifier_key": "manipulates_source", + "operator": operator.eq, + }, ), "inspects_source": FilterFunction( function=passes_operator_filter, - kwargs={"classifier_key": "inspects_source", "operator": operator.eq}, + kwargs={ + "classifier_key": "inspects_source", + "operator": operator.eq, + }, ), "memory_depth": FilterFunction( function=passes_operator_filter, @@ -178,7 +187,8 @@ class ExampleStrategy(Player): kwargs={"classifier_key": "memory_depth", "operator": operator.le}, ), "makes_use_of": FilterFunction( - function=passes_in_list_filter, kwargs={"classifier_key": "makes_use_of"} + function=passes_in_list_filter, + kwargs={"classifier_key": "makes_use_of"}, ), } diff --git a/axelrod/strategies/adaptor.py b/axelrod/strategies/adaptor.py index f8c550f61..5b4026c8c 100644 --- a/axelrod/strategies/adaptor.py +++ b/axelrod/strategies/adaptor.py @@ -36,12 +36,13 @@ class AbstractAdaptor(Player): "manipulates_state": False, } - def __init__(self, delta: Dict[Tuple[Action, Action], float], - perr: float = 0.01) -> None: + def __init__( + self, delta: Dict[Tuple[Action, Action], float], perr: float = 0.01 + ) -> None: super().__init__() self.perr = perr self.delta = delta - self.s = 0. + self.s = 0.0 def strategy(self, opponent: Player) -> Action: if self.history: @@ -51,7 +52,8 @@ def strategy(self, opponent: Player) -> Action: # Compute probability of Cooperation p = self.perr + (1.0 - 2 * self.perr) * ( - heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1)) + heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1) + ) # Draw action action = self._random.random_choice(p) return action @@ -71,10 +73,10 @@ class AdaptorBrief(AbstractAdaptor): def __init__(self) -> None: delta = { - (C, C): 0., # R + (C, C): 0.0, # R (C, D): -1.001505, # S - (D, C): 0.992107, # T - (D, D): -0.638734 # P + (D, C): 0.992107, # T + (D, D): -0.638734, # P } super().__init__(delta=delta) @@ -93,9 +95,9 @@ class AdaptorLong(AbstractAdaptor): def __init__(self) -> None: delta = { - (C, C): 0., # R + (C, C): 0.0, # R (C, D): 1.888159, # S (D, C): 1.858883, # T - (D, D): -0.995703 # P + (D, D): -0.995703, # P } super().__init__(delta=delta) diff --git a/axelrod/strategies/ann.py b/axelrod/strategies/ann.py index 74bd1ab7e..0079a8f1e 100644 --- a/axelrod/strategies/ann.py +++ b/axelrod/strategies/ann.py @@ -113,7 +113,10 @@ def compute_features(player: Player, opponent: Player) -> List[int]: def activate( - bias: List[float], hidden: List[float], output: List[float], inputs: List[int] + bias: List[float], + hidden: List[float], + output: List[float], + inputs: List[int], ) -> float: """ Compute the output of the neural network: @@ -193,8 +196,7 @@ class ANN(Player): } def __init__( - self, num_features: int, num_hidden: int, - weights: List[float] = None + self, num_features: int, num_hidden: int, weights: List[float] = None ) -> None: Player.__init__(self) self.num_features = num_features @@ -224,42 +226,68 @@ def strategy(self, opponent: Player) -> Action: class EvolvableANN(ANN, EvolvablePlayer): """Evolvable version of ANN.""" + name = "EvolvableANN" def __init__( - self, num_features: int, num_hidden: int, + self, + num_features: int, + num_hidden: int, weights: List[float] = None, mutation_probability: float = None, mutation_distance: int = 5, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) - num_features, num_hidden, weights, mutation_probability = self._normalize_parameters( - num_features, num_hidden, weights, mutation_probability) - ANN.__init__(self, - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + ( + num_features, + num_hidden, + weights, + mutation_probability, + ) = self._normalize_parameters( + num_features, num_hidden, weights, mutation_probability + ) + ANN.__init__( + self, + num_features=num_features, + num_hidden=num_hidden, + weights=weights, + ) self.mutation_probability = mutation_probability self.mutation_distance = mutation_distance self.overwrite_init_kwargs( num_features=num_features, num_hidden=num_hidden, weights=weights, - mutation_probability=mutation_probability) + mutation_probability=mutation_probability, + ) - def _normalize_parameters(self, num_features=None, num_hidden=None, weights=None, mutation_probability=None): + def _normalize_parameters( + self, + num_features=None, + num_hidden=None, + weights=None, + mutation_probability=None, + ): if not (num_features and num_hidden): - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableANN") + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableANN" + ) size = num_weights(num_features, num_hidden) if not weights: weights = [self._random.uniform(-1, 1) for _ in range(size)] if mutation_probability is None: - mutation_probability = 10. / size + mutation_probability = 10.0 / size return num_features, num_hidden, weights, mutation_probability - def mutate_weights(self, weights, num_features, num_hidden, mutation_probability, - mutation_distance): + def mutate_weights( + self, + weights, + num_features, + num_hidden, + mutation_probability, + mutation_distance, + ): size = num_weights(num_features, num_hidden) randoms = self._random.random(size) for i, r in enumerate(randoms): @@ -270,13 +298,19 @@ def mutate_weights(self, weights, num_features, num_hidden, mutation_probability def mutate(self): weights = self.mutate_weights( - self.weights, self.num_features, self.num_hidden, - self.mutation_probability, self.mutation_distance) + self.weights, + self.num_features, + self.num_hidden, + self.mutation_probability, + self.mutation_distance, + ) return self.create_new(weights=weights) def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") + raise TypeError( + "Crossover must be between the same player classes." + ) weights = crossover_lists(self.weights, other.weights, self._random) return self.create_new(weights=weights) @@ -299,9 +333,8 @@ class EvolvedANN(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN"] super().__init__( - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + num_features=num_features, num_hidden=num_hidden, weights=weights + ) class EvolvedANN5(ANN): @@ -322,9 +355,8 @@ class EvolvedANN5(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN 5"] super().__init__( - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + num_features=num_features, num_hidden=num_hidden, weights=weights + ) class EvolvedANNNoise05(ANN): @@ -345,6 +377,5 @@ class EvolvedANNNoise05(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN 5 Noise 05"] super().__init__( - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + num_features=num_features, num_hidden=num_hidden, weights=weights + ) diff --git a/axelrod/strategies/axelrod_first.py b/axelrod/strategies/axelrod_first.py index 0cc16d4e6..65a6a7776 100644 --- a/axelrod/strategies/axelrod_first.py +++ b/axelrod/strategies/axelrod_first.py @@ -66,7 +66,7 @@ def strategy(self, opponent: Player) -> Action: opponent ever plays D.""" if len(self.history) < self._rounds_to_cooperate: return C - if opponent.defections > 0: # Implement Grudger + if opponent.defections > 0: #  Implement Grudger return D return C @@ -262,12 +262,14 @@ def strategy(self, opponent: Player) -> Action: # Adding 1 to cooperations for assumption that first opponent move # being a response to a cooperation. See docstring for more # information. - alpha = (self.number_opponent_cooperations_in_response_to_C / - (self.cooperations + 1)) + alpha = self.number_opponent_cooperations_in_response_to_C / ( + self.cooperations + 1 + ) # Adding 2 to defections on the assumption that the first two # moves are defections, which may not be true in a noisy match - beta = (self.number_opponent_cooperations_in_response_to_D / - max(self.defections, 2)) + beta = self.number_opponent_cooperations_in_response_to_D / max( + self.defections, 2 + ) R, P, S, T = self.match_attributes["game"].RPST() expected_value_of_cooperating = alpha * R + (1 - alpha) * S @@ -430,25 +432,34 @@ def strategy(self, opponent: Player) -> Action: # Check if opponent plays randomly, if so, defect for the rest of the game p_value = chisquare([opponent.cooperations, opponent.defections]).pvalue - self.opponent_is_random = (p_value >= self.alpha) or self.opponent_is_random + self.opponent_is_random = ( + p_value >= self.alpha + ) or self.opponent_is_random if self.opponent_is_random: return D - if all( - opponent.history[i] == self.history[i - 1] - for i in range(1, len(self.history)) - ) or opponent.history == self.history: + if ( + all( + opponent.history[i] == self.history[i - 1] + for i in range(1, len(self.history)) + ) + or opponent.history == self.history + ): # Check if opponent plays Tit for Tat or a clone of itself. if opponent.history[-1] == D: return D return C if self.next_random_defection_turn is None: - self.next_random_defection_turn = self._random.randint(5, 15) + len(self.history) + self.next_random_defection_turn = self._random.randint(5, 15) + len( + self.history + ) if len(self.history) == self.next_random_defection_turn: # resample the next defection turn - self.next_random_defection_turn = self._random.randint(5, 15) + len(self.history) + self.next_random_defection_turn = self._random.randint(5, 15) + len( + self.history + ) return D return C @@ -580,7 +591,27 @@ class FirstByNydegger(Player): } def __init__(self) -> None: - self.As = [1, 6, 7, 17, 22, 23, 26, 29, 30, 31, 33, 38, 39, 45, 49, 54, 55, 58, 61] + self.As = [ + 1, + 6, + 7, + 17, + 22, + 23, + 26, + 29, + 30, + 31, + 33, + 38, + 39, + 45, + 49, + 54, + 55, + 58, + 61, + ] self.score_map = {(C, C): 0, (C, D): 2, (D, C): 1, (D, D): 3} super().__init__() @@ -610,7 +641,9 @@ def strategy(self, opponent: Player) -> Action: else: # TFT return D if opponent.history[-1] == D else C - A = self.score_history(self.history[-3:], opponent.history[-3:], self.score_map) + A = self.score_history( + self.history[-3:], opponent.history[-3:], self.score_map + ) if A in self.As: return D return C @@ -849,7 +882,9 @@ def strategy(self, opponent: Player) -> Action: return opponent.history[-1] if round_number % 15 == 0: - p_value = chisquare([opponent.cooperations, opponent.defections]).pvalue + p_value = chisquare( + [opponent.cooperations, opponent.defections] + ).pvalue self.opponent_is_random = p_value >= self.alpha if self.opponent_is_random: @@ -982,8 +1017,10 @@ def strategy(self, opponent: Player) -> Action: std_deviation = (N ** (1 / 2)) / 2 lower = N / 2 - 3 * std_deviation upper = N / 2 + 3 * std_deviation - if (self.remembered_number_of_opponent_defectioons <= lower or - self.remembered_number_of_opponent_defectioons >= upper): + if ( + self.remembered_number_of_opponent_defectioons <= lower + or self.remembered_number_of_opponent_defectioons >= upper + ): # Opponent deserves a fresh start self.last_fresh_start = current_round self._fresh_start() diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index 3ab54f40a..3d4f4b6c8 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -327,8 +327,12 @@ def __init__(self): self.opponent_consecutive_defections = 0 # equal to S variable self.one_turn_after_good_defection_ratio = 5 # equal to AD variable self.two_turns_after_good_defection_ratio = 0 # equal to NO variable - self.one_turn_after_good_defection_ratio_count = 1 # equal to AK variable - self.two_turns_after_good_defection_ratio_count = 1 # equal to NK variable + self.one_turn_after_good_defection_ratio_count = ( + 1 # equal to AK variable + ) + self.two_turns_after_good_defection_ratio_count = ( + 1 # equal to NK variable + ) # All above variables correspond to those in original Fotran Code self.dict = {C: 0, D: 1} @@ -353,7 +357,12 @@ def update_state(self, opponent): ) + (3 - (3 * self.dict[opponent.history[-1]])) + (2 * self.dict[self.history[-1]]) - - ((self.dict[opponent.history[-1]] * self.dict[self.history[-1]])) + - ( + ( + self.dict[opponent.history[-1]] + * self.dict[self.history[-1]] + ) + ) ) / (self.two_turns_after_good_defection_ratio_count + 1) self.two_turns_after_good_defection_ratio_count += 1 elif self.num_turns_after_good_defection == 1: @@ -365,7 +374,10 @@ def update_state(self, opponent): ) + (3 - (3 * self.dict[opponent.history[-1]])) + (2 * self.dict[self.history[-1]]) - - (self.dict[opponent.history[-1]] * self.dict[self.history[-1]]) + - ( + self.dict[opponent.history[-1]] + * self.dict[self.history[-1]] + ) ) / (self.one_turn_after_good_defection_ratio_count + 1) self.one_turn_after_good_defection_ratio_count += 1 @@ -402,7 +414,10 @@ def strategy(self, opponent: Player) -> Action: return D if (current_score[0] / ((len(self.history)) + 1)) >= 1.75: probability = ( - (0.25 + ((opponent.cooperations + 1) / ((len(self.history)) + 1))) + ( + 0.25 + + ((opponent.cooperations + 1) / ((len(self.history)) + 1)) + ) - (self.opponent_consecutive_defections * 0.25) + ((current_score[0] - current_score[1]) / 100) + (4 / ((len(self.history)) + 1)) @@ -516,7 +531,12 @@ class SecondByKluepfel(Player): def __init__(self): super().__init__() - self.cd_counts, self.dd_counts, self.dc_counts, self.cc_counts = 0, 0, 0, 0 + self.cd_counts, self.dd_counts, self.dc_counts, self.cc_counts = ( + 0, + 0, + 0, + 0, + ) def strategy(self, opponent: Player) -> Action: # First update the response matrix. @@ -534,7 +554,9 @@ def strategy(self, opponent: Player) -> Action: # Check for randomness if len(self.history) > 26: - if self.cd_counts >= (self.cd_counts + self.dd_counts) / 2 - 0.75 * np.sqrt( + if self.cd_counts >= ( + self.cd_counts + self.dd_counts + ) / 2 - 0.75 * np.sqrt( self.cd_counts + self.dd_counts ) and self.dc_counts >= ( self.dc_counts + self.cc_counts @@ -969,7 +991,10 @@ def strategy(self, opponent: Player) -> Action: if self.forgive_flag: self.forgive_flag = False self.defect_padding = 0 - if self.grudge < len(self.history) + 1 and opponent.history[-1] == D: + if ( + self.grudge < len(self.history) + 1 + and opponent.history[-1] == D + ): # Then override self.grudge += 20 return self.try_return(C) @@ -1095,7 +1120,9 @@ def __init__(self): self.mode = "Normal" self.recorded_defects = 0 # Count opponent defects after turn 1 self.exit_defect_meter = 0 # When >= 11, then exit defect mode. - self.coops_in_first_36 = None # On turn 37, count cooperations in first 36 + self.coops_in_first_36 = ( + None # On turn 37, count cooperations in first 36 + ) self.was_defective = False # Previously in Defect mode self.prob = 0.25 # After turn 37, probability that we'll defect @@ -1105,7 +1132,9 @@ def __init__(self): self.more_coop = 0 # This schedules cooperation for future turns # Initial last_generous_n_turns_ago to 3 because this counts up and # triggers a strategy change at 2. - self.last_generous_n_turns_ago = 3 # How many tuns ago was a "generous" move + self.last_generous_n_turns_ago = ( + 3 # How many tuns ago was a "generous" move + ) self.burned = False self.defect_streak = 0 @@ -1114,7 +1143,9 @@ def __init__(self): 0, ] # Counters that get (almost) alternatively incremented. self.parity_bit = 0 # Which parity_streak to increment - self.parity_limit = 5 # When a parity streak hits this limit, alter strategy. + self.parity_limit = ( + 5 # When a parity streak hits this limit, alter strategy. + ) self.parity_hits = 0 # Counts how many times a parity_limit was hit. # After hitting parity_hits 8 times, lower parity_limit to 3. @@ -1153,7 +1184,9 @@ def calculate_chi_squared(self, turn): denom = turn - 2 expected_matrix = ( - np.outer(self.move_history.sum(axis=1), self.move_history.sum(axis=0)) + np.outer( + self.move_history.sum(axis=1), self.move_history.sum(axis=0) + ) / denom ) @@ -1162,7 +1195,9 @@ def calculate_chi_squared(self, turn): for j in range(2): expect = expected_matrix[i, j] if expect > 1.0: - chi_squared += (expect - self.move_history[i, j]) ** 2 / expect + chi_squared += ( + expect - self.move_history[i, j] + ) ** 2 / expect return chi_squared @@ -1184,7 +1219,10 @@ def detect_random(self, turn): if self.move_history[0, 0] / denom >= 0.8: return False - if self.recorded_defects / denom < 0.25 or self.recorded_defects / denom > 0.75: + if ( + self.recorded_defects / denom < 0.25 + or self.recorded_defects / denom > 0.75 + ): return False if self.calculate_chi_squared(turn) > 3: @@ -1276,7 +1314,11 @@ def strategy(self, opponent: Player) -> Action: # Only enter Fair-weather mode if the opponent Cooperated the first 37 # turns then Defected on the 38th. - if turn == 38 and opponent.history[-1] == D and opponent.cooperations == 36: + if ( + turn == 38 + and opponent.history[-1] == D + and opponent.cooperations == 36 + ): self.mode = "Fair-weather" return self.try_return(to_return=C, lower_flags=False) @@ -1297,7 +1339,9 @@ def strategy(self, opponent: Player) -> Action: self.parity_streak[ self.parity_bit ] = 0 # Reset `parity_streak` when we hit the limit. - self.parity_hits += 1 # Keep track of how many times we hit the limit. + self.parity_hits += ( + 1 # Keep track of how many times we hit the limit. + ) if self.parity_hits >= 8: # After 8 times, lower the limit. self.parity_limit = 3 return self.try_return( @@ -1521,7 +1565,9 @@ def strategy(self, opponent: Player) -> Action: recent_history[-go_back] = opponent.history[-go_back] return self._random.random_choice( - self.prob_coop[(recent_history[-3], recent_history[-2], recent_history[-1])] + self.prob_coop[ + (recent_history[-3], recent_history[-2], recent_history[-1]) + ] ) @@ -1704,7 +1750,10 @@ def strategy(self, opponent: Player) -> Action: else: self.def_after_ab_count += 1 self.streak_needed = ( - np.floor(20.0 * self.def_after_ab_count / self.coop_after_ab_count) + 1 + np.floor( + 20.0 * self.def_after_ab_count / self.coop_after_ab_count + ) + + 1 ) self.current_streak = 0 return C @@ -1804,7 +1853,9 @@ def strategy(self, opponent: Player) -> Action: # Update history if turn >= 3: - self.count_them_us_them[(them_three_ago, us_two_ago, them_two_ago)] += 1 + self.count_them_us_them[ + (them_three_ago, us_two_ago, them_two_ago) + ] += 1 if ( self.count_them_us_them[(them_two_ago, us_last, C)] @@ -1866,7 +1917,9 @@ def __init__(self) -> None: (10, D, 7, C), ) - super().__init__(transitions=transitions, initial_state=0, initial_action=C) + super().__init__( + transitions=transitions, initial_state=0, initial_action=C + ) class SecondByMikkelson(FSMPlayer): @@ -2100,6 +2153,8 @@ def strategy(self, opponent: Player) -> Action: # Calculate the probability that the opponent cooperated last turn given # what we know two turns ago. - prob_coop = self.opp_c_after_x[us_two_turns_ago] / self.total_num_of_x[ - us_two_turns_ago] + prob_coop = ( + self.opp_c_after_x[us_two_turns_ago] + / self.total_num_of_x[us_two_turns_ago] + ) return self._random.random_choice(prob_coop) diff --git a/axelrod/strategies/backstabber.py b/axelrod/strategies/backstabber.py index 0d9d821d9..3150d8974 100644 --- a/axelrod/strategies/backstabber.py +++ b/axelrod/strategies/backstabber.py @@ -97,7 +97,9 @@ def _opponent_triggers_alt_strategy(opponent: Player) -> bool: return before_alt_strategy < current_round <= last_round_of_alt_strategy -def _opponent_defected_in_first_n_rounds(opponent: Player, first_n_rounds: int) -> bool: +def _opponent_defected_in_first_n_rounds( + opponent: Player, first_n_rounds: int +) -> bool: """ If opponent defected in the first N rounds, return True. Else return False. """ diff --git a/axelrod/strategies/bush_mosteller.py b/axelrod/strategies/bush_mosteller.py index 3fed4127b..154d02571 100644 --- a/axelrod/strategies/bush_mosteller.py +++ b/axelrod/strategies/bush_mosteller.py @@ -62,7 +62,10 @@ def __init__( self._c_prob, self._d_prob = c_prob, d_prob self._init_c_prob, self._init_d_prob = c_prob, d_prob self._aspiration_level = abs( - (max(self.match_attributes["game"].RPST()) / aspiration_level_divider) + ( + max(self.match_attributes["game"].RPST()) + / aspiration_level_divider + ) ) self._stimulus = 0.0 @@ -104,7 +107,9 @@ def stimulus_update(self, opponent: Player): ) elif self._stimulus < 0: - self._c_prob += self._learning_rate * self._stimulus * self._c_prob + self._c_prob += ( + self._learning_rate * self._stimulus * self._c_prob + ) # Updates probability following previous choice D if self.history[-1] == D: @@ -114,15 +119,21 @@ def stimulus_update(self, opponent: Player): ) elif self._stimulus < 0: - self._d_prob += self._learning_rate * self._stimulus * self._d_prob + self._d_prob += ( + self._learning_rate * self._stimulus * self._d_prob + ) def strategy(self, opponent: Player) -> Action: # First turn if len(self.history) == 0: - return self._random.random_choice(self._c_prob / (self._c_prob + self._d_prob)) + return self._random.random_choice( + self._c_prob / (self._c_prob + self._d_prob) + ) # Updating stimulus depending on his own latest choice self.stimulus_update(opponent) - return self._random.random_choice(self._c_prob / (self._c_prob + self._d_prob)) + return self._random.random_choice( + self._c_prob / (self._c_prob + self._d_prob) + ) diff --git a/axelrod/strategies/calculator.py b/axelrod/strategies/calculator.py index 05cdba01e..259bccb91 100644 --- a/axelrod/strategies/calculator.py +++ b/axelrod/strategies/calculator.py @@ -39,8 +39,9 @@ def set_seed(self, seed: int = None): def strategy(self, opponent: Player) -> Action: turn = len(self.history) if turn > 0: - self.joss_instance.history.append(self.history[-1], - opponent.history[-1]) + self.joss_instance.history.append( + self.history[-1], opponent.history[-1] + ) if turn == 20: self.cycle = detect_cycle(opponent.history) return self.extended_strategy(opponent) diff --git a/axelrod/strategies/cycler.py b/axelrod/strategies/cycler.py index bff8c83d4..ead3e7127 100644 --- a/axelrod/strategies/cycler.py +++ b/axelrod/strategies/cycler.py @@ -111,23 +111,25 @@ def __init__( cycle_length: int = None, mutation_probability: float = 0.2, mutation_potency: int = 1, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) cycle, cycle_length = self._normalize_parameters(cycle, cycle_length) Cycler.__init__(self, cycle=cycle) # Overwrite init_kwargs in the case that we generated a new cycle from cycle_length - self.overwrite_init_kwargs( - cycle=cycle, - cycle_length=cycle_length) + self.overwrite_init_kwargs(cycle=cycle, cycle_length=cycle_length) self.mutation_probability = mutation_probability self.mutation_potency = mutation_potency - def _normalize_parameters(self, cycle=None, cycle_length=None) -> Tuple[str, int]: + def _normalize_parameters( + self, cycle=None, cycle_length=None + ) -> Tuple[str, int]: """Compute other parameters from those that may be missing, to ensure proper cloning.""" if not cycle: if not cycle_length: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableCycler") + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableCycler" + ) cycle = self._generate_random_cycle(cycle_length) cycle_length = len(cycle) return cycle, cycle_length @@ -136,7 +138,9 @@ def _generate_random_cycle(self, cycle_length: int) -> str: """ Generate a sequence of random moves """ - return actions_to_str(self._random.choice(actions) for _ in range(cycle_length)) + return actions_to_str( + self._random.choice(actions) for _ in range(cycle_length) + ) def mutate(self) -> EvolvablePlayer: """ @@ -145,8 +149,12 @@ def mutate(self) -> EvolvablePlayer: if self._random.random() <= self.mutation_probability: mutated_sequence = list(str_to_actions(self.cycle)) for _ in range(self.mutation_potency): - index_to_change = self._random.randint(0, len(mutated_sequence) - 1) - mutated_sequence[index_to_change] = mutated_sequence[index_to_change].flip() + index_to_change = self._random.randint( + 0, len(mutated_sequence) - 1 + ) + mutated_sequence[index_to_change] = mutated_sequence[ + index_to_change + ].flip() cycle = actions_to_str(mutated_sequence) else: cycle = self.cycle @@ -158,7 +166,9 @@ def crossover(self, other) -> EvolvablePlayer: Creates and returns a new Player instance with a single crossover point. """ if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") + raise TypeError( + "Crossover must be between the same player classes." + ) cycle_list = crossover_lists(self.cycle, other.cycle, self._random) cycle = "".join(cycle_list) cycle, _ = self._normalize_parameters(cycle) diff --git a/axelrod/strategies/dbs.py b/axelrod/strategies/dbs.py index 8e7916718..0d4f6742d 100644 --- a/axelrod/strategies/dbs.py +++ b/axelrod/strategies/dbs.py @@ -266,7 +266,9 @@ def strategy(self, opponent: Player) -> Action: if r_minus_in_Rd: self.v += 1 # If the number of violations is superior to a threshold, clean Rd. - if (self.v > self.reject_threshold) or (r_plus_in_Rc and r_minus_in_Rd): + if (self.v > self.reject_threshold) or ( + r_plus_in_Rc and r_minus_in_Rd + ): self.Rd.clear() self.v = 0 @@ -274,7 +276,9 @@ def strategy(self, opponent: Player) -> Action: Rp = {} all_cond = [(C, C), (C, D), (D, C), (D, D)] for outcome in all_cond: - if (outcome not in self.Rc.keys()) and (outcome not in self.Rd.keys()): + if (outcome not in self.Rc.keys()) and ( + outcome not in self.Rd.keys() + ): # Compute opponent's C answer probability. Rp[outcome] = self.compute_prob_rule(outcome, self.alpha) @@ -327,8 +331,12 @@ def get_siblings(self): siblings which are DeterministicNodes, their depth is equal to current node depth's + 1. """ - opponent_c_choice = DeterministicNode(self.own_action, C, self.depth + 1) - opponent_d_choice = DeterministicNode(self.own_action, D, self.depth + 1) + opponent_c_choice = DeterministicNode( + self.own_action, C, self.depth + 1 + ) + opponent_d_choice = DeterministicNode( + self.own_action, D, self.depth + 1 + ) return opponent_c_choice, opponent_d_choice def is_stochastic(self): @@ -354,8 +362,12 @@ def get_siblings(self, policy): of the same depth as the current node. Their probabilities pC are defined by the policy argument. """ - c_choice = StochasticNode(C, policy[(self.action1, self.action2)], self.depth) - d_choice = StochasticNode(D, policy[(self.action1, self.action2)], self.depth) + c_choice = StochasticNode( + C, policy[(self.action1, self.action2)], self.depth + ) + d_choice = StochasticNode( + D, policy[(self.action1, self.action2)], self.depth + ) return c_choice, d_choice def is_stochastic(self): @@ -401,7 +413,9 @@ def minimax_tree_search(begin_node, policy, max_depth): # The stochastic node value is the expected value of siblings. node_value = begin_node.pC * minimax_tree_search( siblings[0], policy, max_depth - ) + (1 - begin_node.pC) * minimax_tree_search(siblings[1], policy, max_depth) + ) + (1 - begin_node.pC) * minimax_tree_search( + siblings[1], policy, max_depth + ) return node_value else: # Deterministic node if begin_node.depth == max_depth: @@ -433,7 +447,9 @@ def move_gen(outcome, policy, depth_search_tree=5): using tree-search procedure. """ current_node = DeterministicNode(outcome[0], outcome[1], depth=0) - values_of_choices = minimax_tree_search(current_node, policy, depth_search_tree) + values_of_choices = minimax_tree_search( + current_node, policy, depth_search_tree + ) # Returns the Action which correspond to the best choice in terms of # expected value. In case value(C) == value(D), returns C. actions_tuple = (C, D) diff --git a/axelrod/strategies/defector.py b/axelrod/strategies/defector.py index 7b4831443..514e5bed8 100644 --- a/axelrod/strategies/defector.py +++ b/axelrod/strategies/defector.py @@ -54,6 +54,9 @@ def strategy(self, opponent: Player) -> Action: Defect if opponent has cooperated at least once in the past and has defected for the last 3 turns in a row. """ - if opponent.history.cooperations > 0 and opponent.history[-3:] == [D] * 3: + if ( + opponent.history.cooperations > 0 + and opponent.history[-3:] == [D] * 3 + ): return C return D diff --git a/axelrod/strategies/finite_state_machines.py b/axelrod/strategies/finite_state_machines.py index 29fa1a291..ec49ad2f4 100644 --- a/axelrod/strategies/finite_state_machines.py +++ b/axelrod/strategies/finite_state_machines.py @@ -70,7 +70,9 @@ def state_transitions(self) -> dict: return self._state_transitions.copy() def transitions(self) -> list: - return [[x[0], x[1], y[0], y[1]] for x, y in self._state_transitions.items()] + return [ + [x[0], x[1], y[0], y[1]] for x, y in self._state_transitions.items() + ] def move(self, opponent_action: Action) -> Action: """Computes the response move and changes state.""" @@ -112,7 +114,7 @@ def __init__( self, transitions: Tuple[Transition, ...] = ((1, C, 1, C), (1, D, 1, D)), initial_state: int = 1, - initial_action: Action = C + initial_action: Action = C, ) -> None: Player.__init__(self) self.initial_state = initial_state @@ -147,39 +149,62 @@ def __init__( initial_action: Action = None, num_states: int = None, mutation_probability: float = 0.1, - seed: int = None + seed: int = None, ) -> None: """If transitions, initial_state, and initial_action are None then generate random parameters using num_states.""" EvolvablePlayer.__init__(self, seed=seed) - transitions, initial_state, initial_action, num_states = self._normalize_parameters( - transitions, initial_state, initial_action, num_states) + ( + transitions, + initial_state, + initial_action, + num_states, + ) = self._normalize_parameters( + transitions, initial_state, initial_action, num_states + ) FSMPlayer.__init__( self, transitions=transitions, initial_state=initial_state, - initial_action=initial_action) + initial_action=initial_action, + ) self.mutation_probability = mutation_probability self.overwrite_init_kwargs( transitions=transitions, initial_state=initial_state, initial_action=initial_action, - num_states=self.num_states) + num_states=self.num_states, + ) @classmethod - def normalize_transitions(cls, transitions: Sequence[Sequence]) -> Tuple[Tuple[Any, ...], ...]: + def normalize_transitions( + cls, transitions: Sequence[Sequence] + ) -> Tuple[Tuple[Any, ...], ...]: """Translate a list of lists to a tuple of tuples.""" normalized = [] for t in transitions: normalized.append(tuple(t)) return tuple(normalized) - def _normalize_parameters(self, transitions: Tuple = None, initial_state: int = None, initial_action: Action = None, - num_states: int = None) -> Tuple[Tuple, int, Action, int]: - if not ((transitions is not None) and (initial_state is not None) and (initial_action is not None)): + def _normalize_parameters( + self, + transitions: Tuple = None, + initial_state: int = None, + initial_action: Action = None, + num_states: int = None, + ) -> Tuple[Tuple, int, Action, int]: + if not ( + (transitions is not None) + and (initial_state is not None) + and (initial_action is not None) + ): if not num_states: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableFSMPlayer") - transitions, initial_state, initial_action = self.random_params(num_states) + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableFSMPlayer" + ) + transitions, initial_state, initial_action = self.random_params( + num_states + ) transitions = self.normalize_transitions(transitions) num_states = len(transitions) // 2 return transitions, initial_state, initial_action, num_states @@ -188,7 +213,9 @@ def _normalize_parameters(self, transitions: Tuple = None, initial_state: int = def num_states(self) -> int: return self.fsm.num_states() - def random_params(self, num_states: int) -> Tuple[Tuple[Transition, ...], int, Action]: + def random_params( + self, num_states: int + ) -> Tuple[Tuple[Transition, ...], int, Action]: rows = [] for j in range(num_states): for action in actions: @@ -225,10 +252,14 @@ def mutate(self): if self._random.random() < self.mutation_probability / 10: initial_action = self.initial_action.flip() initial_state = self.initial_state - if self._random.random() < self.mutation_probability / (10 * self.num_states): + if self._random.random() < self.mutation_probability / ( + 10 * self.num_states + ): initial_state = self._random.randint(0, self.num_states) try: - transitions = self.mutate_rows(self.fsm.transitions(), self.mutation_probability) + transitions = self.mutate_rows( + self.fsm.transitions(), self.mutation_probability + ) self.fsm = SimpleFSM(transitions, self.initial_state) except ValueError: # If the FSM is malformed, try again. @@ -239,7 +270,9 @@ def mutate(self): initial_action=initial_action, ) - def crossover_rows(self, rows1: List[List], rows2: List[List]) -> List[List]: + def crossover_rows( + self, rows1: List[List], rows2: List[List] + ) -> List[List]: num_states = len(rows1) // 2 cross_point = 2 * self._random.randint(0, num_states) new_rows = copy_lists(rows1[:cross_point]) @@ -248,8 +281,12 @@ def crossover_rows(self, rows1: List[List], rows2: List[List]) -> List[List]: def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") - transitions = self.crossover_rows(self.fsm.transitions(), other.fsm.transitions()) + raise TypeError( + "Crossover must be between the same player classes." + ) + transitions = self.crossover_rows( + self.fsm.transitions(), other.fsm.transitions() + ) transitions = self.normalize_transitions(transitions) return self.create_new(transitions=transitions) @@ -267,22 +304,28 @@ def receive_vector(self, vector): Finally, a probability to determine the player's first move. """ num_states = self.fsm.num_states() - state_scale = vector[:num_states * 2] + state_scale = vector[: num_states * 2] next_states = [int(s * (num_states - 1)) for s in state_scale] - actions = vector[num_states * 2: -1] + actions = vector[num_states * 2 : -1] self.initial_action = C if round(vector[-1]) == 0 else D self.initial_state = 1 transitions = [] - for i, (initial_state, action) in enumerate(itertools.product(range(num_states), [C, D])): + for i, (initial_state, action) in enumerate( + itertools.product(range(num_states), [C, D]) + ): next_action = C if round(actions[i]) == 0 else D - transitions.append([initial_state, action, next_states[i], next_action]) + transitions.append( + [initial_state, action, next_states[i], next_action] + ) transitions = self.normalize_transitions(transitions) self.fsm = SimpleFSM(transitions, self.initial_state) - self.overwrite_init_kwargs(transitions=transitions, - initial_state=self.initial_state, - initial_action=self.initial_action) + self.overwrite_init_kwargs( + transitions=transitions, + initial_state=self.initial_state, + initial_action=self.initial_action, + ) def create_vector_bounds(self): """Creates the bounds for the decision variables.""" diff --git a/axelrod/strategies/gambler.py b/axelrod/strategies/gambler.py index 3e50a76b9..69bf9221b 100644 --- a/axelrod/strategies/gambler.py +++ b/axelrod/strategies/gambler.py @@ -59,7 +59,7 @@ def __init__( pattern: Any = None, # pattern is str or tuple of Actions. parameters: Plays = None, mutation_probability: float = None, - seed: int = None + seed: int = None, ) -> None: EvolvableLookerUp.__init__( self, @@ -68,7 +68,7 @@ def __init__( pattern=pattern, parameters=parameters, mutation_probability=mutation_probability, - seed=seed + seed=seed, ) self.pattern = list(self.pattern) Gambler.__init__( @@ -76,7 +76,7 @@ def __init__( lookup_dict=self.lookup_dict, initial_actions=self.initial_actions, pattern=self.pattern, - parameters=self.parameters + parameters=self.parameters, ) self.overwrite_init_kwargs( lookup_dict=self.lookup_dict, @@ -105,7 +105,9 @@ def receive_vector(self, vector): """Receives a vector and updates the player's pattern. Ignores extra parameters.""" self.pattern = vector self_depth, op_depth, op_openings_depth = self.parameters - self._lookup = LookupTable.from_pattern(self.pattern, self_depth, op_depth, op_openings_depth) + self._lookup = LookupTable.from_pattern( + self.pattern, self_depth, op_depth, op_openings_depth + ) def create_vector_bounds(self): """Creates the bounds for the decision variables. Ignores extra parameters.""" diff --git a/axelrod/strategies/gobymajority.py b/axelrod/strategies/gobymajority.py index 7617b571f..9a7692c5c 100644 --- a/axelrod/strategies/gobymajority.py +++ b/axelrod/strategies/gobymajority.py @@ -60,7 +60,9 @@ def __init__( self.memory = self.classifier["memory_depth"] else: self.memory = 0 - self.name = "Go By Majority" + (self.memory > 0) * (": %i" % self.memory) + self.name = "Go By Majority" + (self.memory > 0) * ( + ": %i" % self.memory + ) if self.soft: self.name = "Soft " + self.name else: diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index a646851af..41d1479c1 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -24,7 +24,7 @@ class Grudger(Player): name = "Grudger" classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "long_run_time": False, "inspects_source": False, diff --git a/axelrod/strategies/hmm.py b/axelrod/strategies/hmm.py index ad64ddb55..8eccef194 100644 --- a/axelrod/strategies/hmm.py +++ b/axelrod/strategies/hmm.py @@ -59,7 +59,11 @@ class SimpleHMM(object): """ def __init__( - self, transitions_C, transitions_D, emission_probabilities, initial_state + self, + transitions_C, + transitions_D, + emission_probabilities, + initial_state, ) -> None: """ Params @@ -133,13 +137,17 @@ def move(self, opponent_action: Action) -> Action: next_state = self._cache_C[self.state] except KeyError: num_states = len(self.emission_probabilities) - next_state = self._random.choice(num_states, 1, p=self.transitions_C[self.state])[0] + next_state = self._random.choice( + num_states, 1, p=self.transitions_C[self.state] + )[0] else: try: next_state = self._cache_D[self.state] except KeyError: num_states = len(self.emission_probabilities) - next_state = self._random.choice(num_states, 1, p=self.transitions_D[self.state])[0] + next_state = self._random.choice( + num_states, 1, p=self.transitions_D[self.state] + )[0] self.state = next_state # Choose action to emit. @@ -178,7 +186,7 @@ def __init__( transitions_D=None, emission_probabilities=None, initial_state=0, - initial_action=C + initial_action=C, ) -> None: Player.__init__(self) if not transitions_C: @@ -189,7 +197,10 @@ def __init__( self.initial_state = initial_state self.initial_action = initial_action self.hmm = SimpleHMM( - copy_lists(transitions_C), copy_lists(transitions_D), list(emission_probabilities), initial_state + copy_lists(transitions_C), + copy_lists(transitions_D), + list(emission_probabilities), + initial_state, ) assert self.hmm.is_well_formed() self.state = self.hmm.state @@ -230,6 +241,7 @@ def set_seed(self, seed=None): class EvolvableHMMPlayer(HMMPlayer, EvolvablePlayer): """Evolvable version of HMMPlayer.""" + name = "EvolvableHMMPlayer" def __init__( @@ -241,18 +253,35 @@ def __init__( initial_action=C, num_states=None, mutation_probability=None, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) - transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability = self._normalize_parameters( - transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability) + ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + num_states, + mutation_probability, + ) = self._normalize_parameters( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + num_states, + mutation_probability, + ) self.mutation_probability = mutation_probability - HMMPlayer.__init__(self, - transitions_C=transitions_C, - transitions_D=transitions_D, - emission_probabilities=emission_probabilities, - initial_state=initial_state, - initial_action=initial_action) + HMMPlayer.__init__( + self, + transitions_C=transitions_C, + transitions_D=transitions_D, + emission_probabilities=emission_probabilities, + initial_state=initial_state, + initial_action=initial_action, + ) self.hmm._random = self._random self.overwrite_init_kwargs( transitions_C=transitions_C, @@ -261,16 +290,35 @@ def __init__( initial_state=initial_state, initial_action=initial_action, num_states=num_states, - mutation_probability=mutation_probability + mutation_probability=mutation_probability, ) - def _normalize_parameters(self, transitions_C=None, transitions_D=None, emission_probabilities=None, - initial_state=None, initial_action=None, num_states=None, mutation_probability=None): - if not ((transitions_C and transitions_D and emission_probabilities) and (initial_state is not None) and (initial_action is not None)): + def _normalize_parameters( + self, + transitions_C=None, + transitions_D=None, + emission_probabilities=None, + initial_state=None, + initial_action=None, + num_states=None, + mutation_probability=None, + ): + if not ( + (transitions_C and transitions_D and emission_probabilities) + and (initial_state is not None) + and (initial_action is not None) + ): if not num_states: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableHMMPlayer") - transitions_C, transitions_D, emission_probabilities, initial_state, initial_action = self.random_params( - num_states) + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableHMMPlayer" + ) + ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + ) = self.random_params(num_states) # Normalize types of various matrices for m in [transitions_C, transitions_D]: for i in range(len(m)): @@ -281,7 +329,15 @@ def _normalize_parameters(self, transitions_C=None, transitions_D=None, emission mutation_probability = 10 / (num_states ** 2) else: mutation_probability = mutation_probability - return transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability + return ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + num_states, + mutation_probability, + ) def random_params(self, num_states): transitions_C = [] @@ -293,7 +349,13 @@ def random_params(self, num_states): emission_probabilities.append(self._random.random()) initial_state = self._random.randint(0, num_states) initial_action = C - return transitions_C, transitions_D, emission_probabilities, initial_state, initial_action + return ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + ) @property def num_states(self): @@ -307,16 +369,23 @@ def mutate_rows(self, rows, mutation_probability): def mutate(self): transitions_C = self.mutate_rows( - self.hmm.transitions_C, self.mutation_probability) + self.hmm.transitions_C, self.mutation_probability + ) transitions_D = self.mutate_rows( - self.hmm.transitions_D, self.mutation_probability) + self.hmm.transitions_D, self.mutation_probability + ) emission_probabilities = mutate_row( - self.hmm.emission_probabilities, self.mutation_probability, self._random) + self.hmm.emission_probabilities, + self.mutation_probability, + self._random, + ) initial_action = self.initial_action if self._random.random() < self.mutation_probability / 10: initial_action = self.initial_action.flip() initial_state = self.initial_state - if self._random.random() < self.mutation_probability / (10 * self.num_states): + if self._random.random() < self.mutation_probability / ( + 10 * self.num_states + ): initial_state = self._random.randint(0, self.num_states) return self.create_new( transitions_C=transitions_C, @@ -328,15 +397,24 @@ def mutate(self): def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") - transitions_C = crossover_lists(self.hmm.transitions_C, other.hmm.transitions_C, self._random) - transitions_D = crossover_lists(self.hmm.transitions_D, other.hmm.transitions_D, self._random) + raise TypeError( + "Crossover must be between the same player classes." + ) + transitions_C = crossover_lists( + self.hmm.transitions_C, other.hmm.transitions_C, self._random + ) + transitions_D = crossover_lists( + self.hmm.transitions_D, other.hmm.transitions_D, self._random + ) emission_probabilities = crossover_lists( - self.hmm.emission_probabilities, other.hmm.emission_probabilities, self._random) + self.hmm.emission_probabilities, + other.hmm.emission_probabilities, + self._random, + ) return self.create_new( transitions_C=transitions_C, transitions_D=transitions_D, - emission_probabilities=emission_probabilities + emission_probabilities=emission_probabilities, ) def receive_vector(self, vector): @@ -353,12 +431,12 @@ class with self.num_states. entry is the initial_action. """ - assert(len(vector) == 2 * self.num_states ** 2 + self.num_states + 1) + assert len(vector) == 2 * self.num_states ** 2 + self.num_states + 1 def deserialize(vector): matrix = [] for i in range(self.num_states): - row = vector[self.num_states * i: self.num_states * (i + 1)] + row = vector[self.num_states * i : self.num_states * (i + 1)] row = normalize_vector(row) matrix.append(row) return matrix @@ -371,7 +449,7 @@ def deserialize(vector): deserialize(vector[0:break_tc]), deserialize(vector[break_tc:break_td]), normalize_vector(vector[break_td:break_ep]), - initial_state + initial_state, ) self.initial_action = C if round(vector[-1]) == 0 else D self.initial_state = initial_state diff --git a/axelrod/strategies/human.py b/axelrod/strategies/human.py index b3a0e9564..20d921782 100644 --- a/axelrod/strategies/human.py +++ b/axelrod/strategies/human.py @@ -36,7 +36,9 @@ def validate(self, document) -> None: text = document.text if text and text.upper() not in ["C", "D"]: - raise ValidationError(message="Action must be C or D", cursor_position=0) + raise ValidationError( + message="Action must be C or D", cursor_position=0 + ) class Human(Player): @@ -83,10 +85,14 @@ def _history_toolbar(self): Described at http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-a-bottom-toolbar """ my_history = [self.symbols[action] for action in self.history] - opponent_history = [self.symbols[action] for action in self.history.coplays] + opponent_history = [ + self.symbols[action] for action in self.history.coplays + ] history = list(zip(my_history, opponent_history)) if self.history: - content = "History ({}, opponent): {}".format(self.human_name, history) + content = "History ({}, opponent): {}".format( + self.human_name, history + ) else: content = "" return content diff --git a/axelrod/strategies/hunter.py b/axelrod/strategies/hunter.py index 51c6968d7..e53ead1f3 100644 --- a/axelrod/strategies/hunter.py +++ b/axelrod/strategies/hunter.py @@ -26,7 +26,10 @@ class DefectorHunter(Player): } def strategy(self, opponent: Player) -> Action: - if len(self.history) >= 4 and len(opponent.history) == opponent.defections: + if ( + len(self.history) >= 4 + and len(opponent.history) == opponent.defections + ): return D return C @@ -50,7 +53,10 @@ class CooperatorHunter(Player): } def strategy(self, opponent: Player) -> Action: - if len(self.history) >= 4 and len(opponent.history) == opponent.cooperations: + if ( + len(self.history) >= 4 + and len(opponent.history) == opponent.cooperations + ): return D return C @@ -244,6 +250,8 @@ def strategy(self, opponent: Player) -> Action: probabilities.append(self.countCC / self.cooperations) if self.defections > 5: probabilities.append(self.countDD / self.defections) - if probabilities and all([abs(p - 0.5) < 0.25 for p in probabilities]): + if probabilities and all( + [abs(p - 0.5) < 0.25 for p in probabilities] + ): return D return C diff --git a/axelrod/strategies/lookerup.py b/axelrod/strategies/lookerup.py index 1f57dc685..8bbed1e60 100644 --- a/axelrod/strategies/lookerup.py +++ b/axelrod/strategies/lookerup.py @@ -101,7 +101,11 @@ def _raise_error_for_bad_lookup_dict(self): @classmethod def from_pattern( - cls, pattern: tuple, player_depth: int, op_depth: int, op_openings_depth: int + cls, + pattern: tuple, + player_depth: int, + op_depth: int, + op_openings_depth: int, ): keys = create_lookup_table_keys( player_depth=player_depth, @@ -151,7 +155,9 @@ def display( """ def sorter(plays): - return tuple(actions_to_str(getattr(plays, field) for field in sort_by)) + return tuple( + actions_to_str(getattr(plays, field) for field in sort_by) + ) col_width = 11 sorted_keys = sorted(self._dict, key=sorter) @@ -318,7 +324,7 @@ def __init__( lookup_dict: dict = None, initial_actions: tuple = None, pattern: Any = None, # pattern is str or tuple of Action's. - parameters: Plays = None + parameters: Plays = None, ) -> None: Player.__init__(self) @@ -404,11 +410,21 @@ def __init__( pattern: Any = None, # pattern is str or tuple of Action's. parameters: Plays = None, mutation_probability: float = None, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) - lookup_dict, initial_actions, pattern, parameters, mutation_probability = self._normalize_parameters( - lookup_dict, initial_actions, pattern, parameters, mutation_probability + ( + lookup_dict, + initial_actions, + pattern, + parameters, + mutation_probability, + ) = self._normalize_parameters( + lookup_dict, + initial_actions, + pattern, + parameters, + mutation_probability, ) LookerUp.__init__( self, @@ -426,30 +442,50 @@ def __init__( mutation_probability=mutation_probability, ) - def _normalize_parameters(self, lookup_dict=None, initial_actions=None, pattern=None, parameters=None, - mutation_probability=None): + def _normalize_parameters( + self, + lookup_dict=None, + initial_actions=None, + pattern=None, + parameters=None, + mutation_probability=None, + ): if lookup_dict and initial_actions: # Compute the associated pattern and parameters # Map the table keys to namedTuple Plays - lookup_table = self._get_lookup_table(lookup_dict, pattern, parameters) + lookup_table = self._get_lookup_table( + lookup_dict, pattern, parameters + ) lookup_dict = lookup_table.dictionary - parameters = (lookup_table.player_depth, lookup_table.op_depth, lookup_table.op_openings_depth) + parameters = ( + lookup_table.player_depth, + lookup_table.op_depth, + lookup_table.op_openings_depth, + ) pattern = tuple(v for k, v in sorted(lookup_dict.items())) elif pattern and parameters and initial_actions: # Compute the associated lookup table plays, op_plays, op_start_plays = parameters - lookup_table = self._get_lookup_table(lookup_dict, pattern, parameters) + lookup_table = self._get_lookup_table( + lookup_dict, pattern, parameters + ) lookup_dict = lookup_table.dictionary elif parameters: # Generate a random pattern and (maybe) initial actions plays, op_plays, op_start_plays = parameters - pattern, lookup_table = self.random_params(plays, op_plays, op_start_plays) + pattern, lookup_table = self.random_params( + plays, op_plays, op_start_plays + ) lookup_dict = lookup_table.dictionary if not initial_actions: num_actions = max([plays, op_plays, op_start_plays]) - initial_actions = tuple([self._random.choice((C, D)) for _ in range(num_actions)]) + initial_actions = tuple( + [self._random.choice((C, D)) for _ in range(num_actions)] + ) else: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableLookerUp") + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableLookerUp" + ) # Normalize pattern if isinstance(pattern, str): pattern = str_to_actions(pattern) @@ -457,8 +493,14 @@ def _normalize_parameters(self, lookup_dict=None, initial_actions=None, pattern= if mutation_probability is None: plays, op_plays, op_start_plays = parameters keys = create_lookup_table_keys(plays, op_plays, op_start_plays) - mutation_probability = 2. / len(keys) - return lookup_dict, initial_actions, pattern, parameters, mutation_probability + mutation_probability = 2.0 / len(keys) + return ( + lookup_dict, + initial_actions, + pattern, + parameters, + mutation_probability, + ) def random_value(self): return self._random.choice(actions) @@ -483,7 +525,9 @@ def mutate_table(self, table, mutation_probability): return table def mutate(self): - lookup_dict = self.mutate_table(self.lookup_dict, self.mutation_probability) + lookup_dict = self.mutate_table( + self.lookup_dict, self.mutation_probability + ) # Add in starting moves initial_actions = list(self.initial_actions) for i in range(len(initial_actions)): @@ -491,14 +535,17 @@ def mutate(self): if r < self.mutation_probability: initial_actions[i] = initial_actions[i].flip() return self.create_new( - lookup_dict=lookup_dict, - initial_actions=tuple(initial_actions), + lookup_dict=lookup_dict, initial_actions=tuple(initial_actions), ) def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") - lookup_dict = crossover_dictionaries(self.lookup_dict, other.lookup_dict, self._random) + raise TypeError( + "Crossover must be between the same player classes." + ) + lookup_dict = crossover_dictionaries( + self.lookup_dict, other.lookup_dict, self._random + ) return self.create_new(lookup_dict=lookup_dict) @@ -515,7 +562,9 @@ class EvolvedLookerUp1_1_1(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=1, op_openings=1) - super().__init__(parameters=params, pattern="CDDDDCDD", initial_actions=(C,)) + super().__init__( + parameters=params, pattern="CDDDDCDD", initial_actions=(C,) + ) class EvolvedLookerUp2_2_2(LookerUp): @@ -531,8 +580,12 @@ class EvolvedLookerUp2_2_2(LookerUp): def __init__(self) -> None: params = Plays(self_plays=2, op_plays=2, op_openings=2) - pattern = "CDDCDCDDCDDDCDDDDDCDCDCCCDDCCDCDDDCCCCCDDDCDDDDDDDDDCCDDCDDDCCCD" - super().__init__(parameters=params, pattern=pattern, initial_actions=(C, C)) + pattern = ( + "CDDCDCDDCDDDCDDDDDCDCDCCCDDCCDCDDDCCCCCDDDCDDDDDDDDDCCDDCDDDCCCD" + ) + super().__init__( + parameters=params, pattern=pattern, initial_actions=(C, C) + ) class Winner12(LookerUp): @@ -549,7 +602,9 @@ class Winner12(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=2, op_openings=0) pattern = "CDCDDCDD" - super().__init__(parameters=params, pattern=pattern, initial_actions=(C, C)) + super().__init__( + parameters=params, pattern=pattern, initial_actions=(C, C) + ) class Winner21(LookerUp): @@ -566,7 +621,9 @@ class Winner21(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=2, op_openings=0) pattern = "CDCDCDDD" - super().__init__(parameters=params, pattern=pattern, initial_actions=(D, C)) + super().__init__( + parameters=params, pattern=pattern, initial_actions=(D, C) + ) def get_last_n_plays(player: Player, depth: int) -> tuple: diff --git a/axelrod/strategies/memoryone.py b/axelrod/strategies/memoryone.py index a8ad1c571..7773cd63f 100644 --- a/axelrod/strategies/memoryone.py +++ b/axelrod/strategies/memoryone.py @@ -66,7 +66,9 @@ class MemoryOnePlayer(Player): } def __init__( - self, four_vector: Tuple[float, float, float, float] = None, initial: Action = C + self, + four_vector: Tuple[float, float, float, float] = None, + initial: Action = C, ) -> None: """ Parameters @@ -109,7 +111,9 @@ def set_four_vector(self, four_vector: Tuple[float, float, float, float]): "An element in the probability vector, {}, is not " "between 0 and 1.".format(str(four_vector)) ) - self._four_vector = dict(zip([(C, C), (C, D), (D, C), (D, D)], four_vector)) + self._four_vector = dict( + zip([(C, C), (C, D), (D, C), (D, D)], four_vector) + ) def _post_init(self): # Adjust classifiers diff --git a/axelrod/strategies/memorytwo.py b/axelrod/strategies/memorytwo.py index 9573c308f..43b5e539d 100644 --- a/axelrod/strategies/memorytwo.py +++ b/axelrod/strategies/memorytwo.py @@ -53,7 +53,9 @@ class MemoryTwoPlayer(Player): } def __init__( - self, sixteen_vector: Optional[Tuple[float, ...]] = None, initial: Optional[Tuple[Action, Action]] = None + self, + sixteen_vector: Optional[Tuple[float, ...]] = None, + initial: Optional[Tuple[Action, Action]] = None, ) -> None: """ Parameters @@ -69,7 +71,9 @@ def __init__( self._initial = initial self.set_initial_sixteen_vector(sixteen_vector) - def set_initial_sixteen_vector(self, sixteen_vector: Optional[Tuple[float, ...]]): + def set_initial_sixteen_vector( + self, sixteen_vector: Optional[Tuple[float, ...]] + ): if sixteen_vector is None: sixteen_vector = tuple([1] * 16) warnings.warn("Memory two player is set to default, Cooperator.") @@ -84,7 +88,8 @@ def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]): ) states = [ - (hist[:2], hist[2:]) for hist in list(itertools.product((C, D), repeat=4)) + (hist[:2], hist[2:]) + for hist in list(itertools.product((C, D), repeat=4)) ] self._sixteen_vector = dict( @@ -92,7 +97,9 @@ def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]): ) # type: Dict[tuple, float] @staticmethod - def compute_memory_depth(sixteen_vector: Dict[Tuple[Action, Action], float]) -> int: + def compute_memory_depth( + sixteen_vector: Dict[Tuple[Action, Action], float] + ) -> int: values = set(list(sixteen_vector.values())) # Memory-depth 0 @@ -119,7 +126,9 @@ def compute_memory_depth(sixteen_vector: Dict[Tuple[Action, Action], float]) -> def _post_init(self): values = set(self._sixteen_vector.values()) self.classifier["stochastic"] = any(0 < x < 1 for x in values) - self.classifier["memory_depth"] = self.compute_memory_depth(self._sixteen_vector) + self.classifier["memory_depth"] = self.compute_memory_depth( + self._sixteen_vector + ) def strategy(self, opponent: Player) -> Action: turn = len(self.history) @@ -259,7 +268,11 @@ class MEM2(Player): def __init__(self) -> None: super().__init__() - self.players = {"TFT": TitForTat(), "TFTT": TitFor2Tats(), "ALLD": Defector()} + self.players = { + "TFT": TitForTat(), + "TFTT": TitFor2Tats(), + "ALLD": Defector(), + } self.play_as = "TFT" self.shift_counter = 3 self.alld_counter = 0 diff --git a/axelrod/strategies/meta.py b/axelrod/strategies/meta.py index 5f15b340e..b27907529 100644 --- a/axelrod/strategies/meta.py +++ b/axelrod/strategies/meta.py @@ -104,7 +104,9 @@ def update_histories(self, coplay): # As a sanity check, look for at least one reclassifier, otherwise # this try-except clause could hide a bug. if len(self._reclassifiers) == 0: - raise TypeError("MetaClass update_histories issue, expected a reclassifier.") + raise TypeError( + "MetaClass update_histories issue, expected a reclassifier." + ) # Otherwise just update with C always, so at least the histories have the # expected length. for player in self.team: @@ -234,7 +236,9 @@ def _post_init(self): # If the team has repeated identical members, then it reduces to a singular team # and it may not actually be stochastic. if team and len(set(team)) == 1: - self.classifier["stochastic"] = Classifiers["stochastic"](self.team[0]) + self.classifier["stochastic"] = Classifiers["stochastic"]( + self.team[0] + ) self.singular = True def meta_strategy(self, results, opponent): @@ -551,7 +555,9 @@ def _post_init(self): if distribution and len(set(distribution)) > 1: self.classifier["stochastic"] = True if len(self.team) == 1: - self.classifier["stochastic"] = Classifiers["stochastic"](self.team[0]) + self.classifier["stochastic"] = Classifiers["stochastic"]( + self.team[0] + ) # Overwrite strategy to avoid use of _random. This will ignore self.meta_strategy. self.index = 0 self.strategy = self.index_strategy @@ -564,7 +570,9 @@ def _post_init(self): if 1 in distribution: self.index = list(distribution).index(1) # It's potentially deterministic. - self.classifier["stochastic"] = Classifiers["stochastic"](self.team[self.index]) + self.classifier["stochastic"] = Classifiers["stochastic"]( + self.team[self.index] + ) # Overwrite strategy to avoid use of _random. This will ignore self.meta_strategy. self.strategy = self.index_strategy diff --git a/axelrod/strategies/prober.py b/axelrod/strategies/prober.py index 0f5293c61..dcfb828a1 100644 --- a/axelrod/strategies/prober.py +++ b/axelrod/strategies/prober.py @@ -80,7 +80,7 @@ def strategy(self, opponent: Player) -> Action: return self.initial_actions[hist_size] if D not in opponent.history[:init_size]: return D - return opponent.history[-1] # TFT + return opponent.history[-1] # TFT class Prober(Player): diff --git a/axelrod/strategies/punisher.py b/axelrod/strategies/punisher.py index 262901675..f976bf370 100644 --- a/axelrod/strategies/punisher.py +++ b/axelrod/strategies/punisher.py @@ -51,7 +51,9 @@ def strategy(self, opponent: Player) -> Action: return D elif D in opponent.history[-1:]: - self.mem_length = (opponent.defections * 20) // len(opponent.history) + self.mem_length = (opponent.defections * 20) // len( + opponent.history + ) self.grudged = True return D @@ -101,7 +103,9 @@ def strategy(self, opponent: Player) -> Action: self.grudge_memory += 1 return D elif D in opponent.history[-1:]: - self.mem_length = (opponent.cooperations * 20) // len(opponent.history) + self.mem_length = (opponent.cooperations * 20) // len( + opponent.history + ) if self.mem_length == 0: self.mem_length += 1 self.grudged = True diff --git a/axelrod/strategies/qlearner.py b/axelrod/strategies/qlearner.py index ef2819e01..1a81ae227 100644 --- a/axelrod/strategies/qlearner.py +++ b/axelrod/strategies/qlearner.py @@ -65,7 +65,9 @@ def strategy(self, opponent: Player) -> Action: if state not in self.Qs: self.Qs[state] = OrderedDict(zip([C, D], [0, 0])) self.Vs[state] = 0 - self.perform_q_learning(self.prev_state, state, self.prev_action, reward) + self.perform_q_learning( + self.prev_state, state, self.prev_action, reward + ) action = self.select_action(state) self.prev_state = state self.prev_action = action @@ -90,16 +92,22 @@ def find_state(self, opponent: Player) -> str: action_str = actions_to_str(opponent.history[-self.memory_length :]) return action_str + prob - def perform_q_learning(self, prev_state: str, state: str, action: Action, reward): + def perform_q_learning( + self, prev_state: str, state: str, action: Action, reward + ): """ Performs the qlearning algorithm """ - self.Qs[prev_state][action] = (1.0 - self.learning_rate) * self.Qs[prev_state][ - action - ] + self.learning_rate * (reward + self.discount_rate * self.Vs[state]) + self.Qs[prev_state][action] = (1.0 - self.learning_rate) * self.Qs[ + prev_state + ][action] + self.learning_rate * ( + reward + self.discount_rate * self.Vs[state] + ) self.Vs[prev_state] = max(self.Qs[prev_state].values()) - def find_reward(self, opponent: Player) -> Dict[Action, Dict[Action, Score]]: + def find_reward( + self, opponent: Player + ) -> Dict[Action, Dict[Action, Score]]: """ Finds the reward gained on the last iteration """ diff --git a/axelrod/strategies/shortmem.py b/axelrod/strategies/shortmem.py index f110f56b8..e1cd7a6fb 100644 --- a/axelrod/strategies/shortmem.py +++ b/axelrod/strategies/shortmem.py @@ -22,7 +22,7 @@ class ShortMem(Player): name = "ShortMem" classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "long_run_time": False, "inspects_source": False, diff --git a/axelrod/strategies/titfortat.py b/axelrod/strategies/titfortat.py index 31c20b639..d3ab2d68d 100644 --- a/axelrod/strategies/titfortat.py +++ b/axelrod/strategies/titfortat.py @@ -1,6 +1,9 @@ from axelrod.action import Action, actions_to_str from axelrod.player import Player -from axelrod.strategy_transformers import FinalTransformer, TrackHistoryTransformer +from axelrod.strategy_transformers import ( + FinalTransformer, + TrackHistoryTransformer, +) C, D = Action.C, Action.D @@ -128,7 +131,9 @@ def strategy(self, opponent): return C if D in opponent.history[-2:]: # Probability of cooperating regardless - return self._random.random_choice(opponent.cooperations / len(opponent.history)) + return self._random.random_choice( + opponent.cooperations / len(opponent.history) + ) else: return C diff --git a/axelrod/strategies/zero_determinant.py b/axelrod/strategies/zero_determinant.py index 7b849eadb..89852ae20 100644 --- a/axelrod/strategies/zero_determinant.py +++ b/axelrod/strategies/zero_determinant.py @@ -151,7 +151,9 @@ class ZDExtort2v2(LRPlayer): name = "ZD-Extort-2 v2" - def __init__(self, phi: float = 1 / 8, s: float = 0.5, l: float = 1) -> None: + def __init__( + self, phi: float = 1 / 8, s: float = 0.5, l: float = 1 + ) -> None: super().__init__(phi, s, l) @@ -168,7 +170,9 @@ class ZDExtort3(LRPlayer): name = "ZD-Extort3" - def __init__(self, phi: float = 3 / 26, s: float = 1 / 3, l: float = 1) -> None: + def __init__( + self, phi: float = 3 / 26, s: float = 1 / 3, l: float = 1 + ) -> None: super().__init__(phi, s, l) @@ -185,7 +189,9 @@ class ZDExtort4(LRPlayer): name = "ZD-Extort-4" - def __init__(self, phi: float = 4 / 17, s: float = 0.25, l: float = 1) -> None: + def __init__( + self, phi: float = 4 / 17, s: float = 0.25, l: float = 1 + ) -> None: super().__init__(phi, s, l) @@ -200,7 +206,9 @@ class ZDGen2(LRPlayer): name = "ZD-GEN-2" - def __init__(self, phi: float = 1 / 8, s: float = 0.5, l: float = 3) -> None: + def __init__( + self, phi: float = 1 / 8, s: float = 0.5, l: float = 3 + ) -> None: super().__init__(phi, s, l) @@ -251,5 +259,7 @@ class ZDSet2(LRPlayer): name = "ZD-SET-2" - def __init__(self, phi: float = 1 / 4, s: float = 0.0, l: float = 2) -> None: + def __init__( + self, phi: float = 1 / 4, s: float = 0.0, l: float = 2 + ) -> None: super().__init__(phi, s, l) diff --git a/axelrod/strategy_transformers.py b/axelrod/strategy_transformers.py index a7297382d..70773ff60 100644 --- a/axelrod/strategy_transformers.py +++ b/axelrod/strategy_transformers.py @@ -107,20 +107,30 @@ def __call__(self, PlayerClass): reclassifiers = PlayerClass._reclassifiers.copy() if reclassifier is not None: - reclassifiers.append((makes_use_of_reclassifier, (PlayerClass, strategy_wrapper), {})) + reclassifiers.append( + ( + makes_use_of_reclassifier, + (PlayerClass, strategy_wrapper), + {}, + ) + ) # This one is second on the assumption that the wrapper reclassifier knows best. reclassifiers.append((reclassifier, args, kwargs)) # First handle the case where the strategy method is static. if is_strategy_static(PlayerClass): + def inner_strategy(self, opponent): return PlayerClass.strategy(opponent) + else: + def inner_strategy(self, opponent): return PlayerClass.strategy(self, opponent) # For the dual wrapper, we flip the history before and after the transform. if strategy_wrapper == dual_wrapper: + def dual_inner_strategy(self, opponent): """The dual wrapper requires flipping the history. It may be more efficient to use a custom History class that tracks a flipped history and swaps labels.""" @@ -128,16 +138,19 @@ def dual_inner_strategy(self, opponent): proposed_action = inner_strategy(self, opponent) self._history = self.history.flip_plays() return proposed_action + outer_strategy = dual_inner_strategy # For the JossAnn transformer, we want to avoid calling the wrapped strategy, # in the cases where it is unnecessary, to avoid affecting stochasticity. elif strategy_wrapper == joss_ann_wrapper: + def joss_ann_inner_strategy(self, opponent): if not self.classifier["stochastic"]: proposed_action = C else: proposed_action = inner_strategy(self, opponent) return proposed_action + outer_strategy = joss_ann_inner_strategy else: outer_strategy = inner_strategy @@ -727,13 +740,15 @@ def __call__(self, player, opponent, action, retaliations): def retailiation_reclassifier(original_classifier, retaliations): if retaliations > 0: original_classifier["memory_depth"] = max( - retaliations, original_classifier["memory_depth"]) + retaliations, original_classifier["memory_depth"] + ) return original_classifier RetaliationTransformer = StrategyTransformerFactory( - RetaliationWrapper(), name_prefix="Retaliating", - reclassifier=retailiation_reclassifier + RetaliationWrapper(), + name_prefix="Retaliating", + reclassifier=retailiation_reclassifier, ) @@ -751,11 +766,13 @@ def __call__(self, player, opponent, action): def rua_reclassifier(original_classifier): original_classifier["memory_depth"] = max( - 1, original_classifier["memory_depth"]) + 1, original_classifier["memory_depth"] + ) return original_classifier RetaliateUntilApologyTransformer = StrategyTransformerFactory( - RetaliationUntilApologyWrapper(), name_prefix="RUA", - reclassifier=rua_reclassifier + RetaliationUntilApologyWrapper(), + name_prefix="RUA", + reclassifier=rua_reclassifier, ) diff --git a/axelrod/tests/integration/test_filtering.py b/axelrod/tests/integration/test_filtering.py index 43226caf6..4f3b57453 100644 --- a/axelrod/tests/integration/test_filtering.py +++ b/axelrod/tests/integration/test_filtering.py @@ -36,7 +36,9 @@ def test_boolean_filtering(self, strategies): for classifier in classifiers: comprehension = set(filter(axl.Classifiers[classifier], strategies)) filterset = {classifier: True} - filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) + filtered = set( + axl.filtered_strategies(filterset, strategies=strategies) + ) self.assertEqual(comprehension, filtered) @given( @@ -90,7 +92,9 @@ def test_memory_depth_filtering( ] ) filterset = {"memory_depth": memory_depth} - filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) + filtered = set( + axl.filtered_strategies(filterset, strategies=strategies) + ) self.assertEqual(comprehension, filtered) @given(strategies=strategy_lists(min_size=20, max_size=20)) @@ -106,12 +110,16 @@ def test_makes_use_of_filtering(self, strategies): [ s for s in strategies - if set(classifier).issubset(set(axl.Classifiers["makes_use_of"](s))) + if set(classifier).issubset( + set(axl.Classifiers["makes_use_of"](s)) + ) ] ) filterset = {"makes_use_of": classifier} - filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) + filtered = set( + axl.filtered_strategies(filterset, strategies=strategies) + ) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) diff --git a/axelrod/tests/integration/test_matches.py b/axelrod/tests/integration/test_matches.py index d46ded024..46f4e318f 100644 --- a/axelrod/tests/integration/test_matches.py +++ b/axelrod/tests/integration/test_matches.py @@ -9,10 +9,14 @@ C, D = axl.Action.C, axl.Action.D deterministic_strategies = [ - s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s()) + s + for s in axl.short_run_time_strategies + if not axl.Classifiers["stochastic"](s()) ] stochastic_strategies = [ - s for s in axl.short_run_time_strategies if axl.Classifiers["stochastic"](s()) + s + for s in axl.short_run_time_strategies + if axl.Classifiers["stochastic"](s()) ] diff --git a/axelrod/tests/integration/test_names.py b/axelrod/tests/integration/test_names.py index 56061b959..ad4ff85af 100644 --- a/axelrod/tests/integration/test_names.py +++ b/axelrod/tests/integration/test_names.py @@ -5,7 +5,7 @@ class TestNames(unittest.TestCase): def test_all_strategies_have_names(self): - names = [s.name for s in axl.all_strategies if s.name != 'Player'] + names = [s.name for s in axl.all_strategies if s.name != "Player"] self.assertEqual(len(names), len(axl.all_strategies)) def test_all_names_are_unique(self): diff --git a/axelrod/tests/integration/test_tournament.py b/axelrod/tests/integration/test_tournament.py index d2d26cf56..8568e165d 100644 --- a/axelrod/tests/integration/test_tournament.py +++ b/axelrod/tests/integration/test_tournament.py @@ -51,7 +51,9 @@ def test_big_tournaments(self, tournament): path = pathlib.Path("test_outputs/test_tournament.csv") filename = axl_filename(path) self.assertIsNone( - tournament.play(progress_bar=False, filename=filename, build_results=False) + tournament.play( + progress_bar=False, filename=filename, build_results=False + ) ) def test_serial_play(self): @@ -94,9 +96,13 @@ def test_repeat_tournament_deterministic(self): turns=2, repetitions=2, ) - path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_)) + path = pathlib.Path( + "test_outputs/stochastic_tournament_{}.csv".format(_) + ) files.append(axl_filename(path)) - tournament.play(progress_bar=False, filename=files[-1], build_results=False) + tournament.play( + progress_bar=False, filename=files[-1], build_results=False + ) self.assertTrue(filecmp.cmp(files[0], files[1])) def test_repeat_tournament_stochastic(self): @@ -116,11 +122,15 @@ def test_repeat_tournament_stochastic(self): game=self.game, turns=2, repetitions=2, - seed=17 + seed=17, + ) + path = pathlib.Path( + "test_outputs/stochastic_tournament_{}.csv".format(_) ) - path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_)) files.append(axl_filename(path)) - tournament.play(progress_bar=False, filename=files[-1], build_results=False) + tournament.play( + progress_bar=False, filename=files[-1], build_results=False + ) self.assertTrue(filecmp.cmp(files[0], files[1])) @@ -162,7 +172,9 @@ def test_matches_have_different_length(self): p2 = axl.Cooperator() p3 = axl.Cooperator() players = [p1, p2, p3] - tournament = axl.Tournament(players, prob_end=0.5, repetitions=2, seed=3) + tournament = axl.Tournament( + players, prob_end=0.5, repetitions=2, seed=3 + ) results = tournament.play(progress_bar=False) # Check that match length are different across the repetitions self.assertNotEqual(results.match_lengths[0], results.match_lengths[1]) diff --git a/axelrod/tests/property.py b/axelrod/tests/property.py index c3dc009cc..d5d1919d9 100644 --- a/axelrod/tests/property.py +++ b/axelrod/tests/property.py @@ -4,12 +4,21 @@ import itertools import axelrod as axl -from hypothesis.strategies import composite, floats, integers, lists, sampled_from +from hypothesis.strategies import ( + composite, + floats, + integers, + lists, + sampled_from, +) @composite def strategy_lists( - draw, strategies=axl.short_run_time_strategies, min_size=1, max_size=len(axl.strategies) + draw, + strategies=axl.short_run_time_strategies, + min_size=1, + max_size=len(axl.strategies), ): """ A hypothesis decorator to return a list of strategies @@ -106,14 +115,20 @@ def tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] turns = draw(integers(min_value=min_turns, max_value=max_turns)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = axl.Tournament(players, turns=turns, repetitions=repetitions, noise=noise) + tournament = axl.Tournament( + players, turns=turns, repetitions=repetitions, noise=noise + ) return tournament @@ -129,7 +144,7 @@ def prob_end_tournaments( max_noise=1, min_repetitions=1, max_repetitions=20, - seed=None + seed=None, ): """ A hypothesis decorator to return a tournament, @@ -158,15 +173,23 @@ def prob_end_tournaments( Random seed """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] prob_end = draw(floats(min_value=min_prob_end, max_value=max_prob_end)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=repetitions, noise=noise, seed=seed + players, + prob_end=prob_end, + repetitions=repetitions, + noise=noise, + seed=seed, ) return tournament @@ -209,29 +232,30 @@ def spatial_tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] player_indices = list(range(len(players))) all_potential_edges = list(itertools.combinations(player_indices, 2)) all_potential_edges.extend([(i, i) for i in player_indices]) # Loops - edges = draw( - lists( - sampled_from(all_potential_edges), - unique=True, - ) - ) + edges = draw(lists(sampled_from(all_potential_edges), unique=True,)) # Ensure all players/nodes are connected: node_indices = sorted(set([node for edge in edges for node in edge])) - missing_nodes = [index for index in player_indices if index not in node_indices] + missing_nodes = [ + index for index in player_indices if index not in node_indices + ] for index in missing_nodes: opponent = draw(sampled_from(player_indices)) edges.append((index, opponent)) turns = draw(integers(min_value=min_turns, max_value=max_turns)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( @@ -278,33 +302,38 @@ def prob_end_spatial_tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] player_indices = list(range(len(players))) all_potential_edges = list(itertools.combinations(player_indices, 2)) all_potential_edges.extend([(i, i) for i in player_indices]) # Loops - edges = draw( - lists( - sampled_from(all_potential_edges), - unique=True, - ) - ) + edges = draw(lists(sampled_from(all_potential_edges), unique=True,)) # Ensure all players/nodes are connected: node_indices = sorted(set([node for edge in edges for node in edge])) - missing_nodes = [index for index in player_indices if index not in node_indices] + missing_nodes = [ + index for index in player_indices if index not in node_indices + ] for index in missing_nodes: opponent = draw(sampled_from(player_indices)) edges.append((index, opponent)) prob_end = draw(floats(min_value=min_prob_end, max_value=max_prob_end)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=repetitions, noise=noise, edges=edges + players, + prob_end=prob_end, + repetitions=repetitions, + noise=noise, + edges=edges, ) return tournament diff --git a/axelrod/tests/strategies/test_adaptive.py b/axelrod/tests/strategies/test_adaptive.py index 20befc067..330195ca8 100644 --- a/axelrod/tests/strategies/test_adaptive.py +++ b/axelrod/tests/strategies/test_adaptive.py @@ -54,7 +54,9 @@ def test_scoring_with_default_game(self): opponent = axl.Cooperator() attrs = {"scores": {C: 3, D: 0}} expected_actions = list(zip([C, C], [C, C])) - self.versus_test(opponent, expected_actions, turns=2, attrs=attrs, seed=9) + self.versus_test( + opponent, expected_actions, turns=2, attrs=attrs, seed=9 + ) def test_scoring_with_alternate_game(self): """Tests that the alternate game is used in scoring.""" @@ -63,5 +65,11 @@ def test_scoring_with_alternate_game(self): expected_actions = list(zip([C, C, C], [C, D, C])) attrs = {"scores": {C: 7, D: 0}} match_attributes = {"game": axl.Game(-3, 10, 10, 10)} - self.versus_test(opponent, expected_actions, turns=3, attrs=attrs, seed=9, - match_attributes=match_attributes) + self.versus_test( + opponent, + expected_actions, + turns=3, + attrs=attrs, + seed=9, + match_attributes=match_attributes, + ) diff --git a/axelrod/tests/strategies/test_ann.py b/axelrod/tests/strategies/test_ann.py index 985462e34..c2a8290fe 100644 --- a/axelrod/tests/strategies/test_ann.py +++ b/axelrod/tests/strategies/test_ann.py @@ -18,6 +18,7 @@ class TestSplitWeights(unittest.TestCase): def test_split_weights(self): with self.assertRaises(ValueError): split_weights([0] * 20, 12, 10) + # Doesn't Raise split_weights([0] * 70, 5, 10) split_weights([0] * 12, 10, 1) @@ -130,7 +131,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): init_parameters = { "num_features": nn_weights["Evolved ANN 5"][0], "num_hidden": nn_weights["Evolved ANN 5"][1], - "weights": nn_weights["Evolved ANN 5"][2] + "weights": nn_weights["Evolved ANN 5"][2], } @@ -139,7 +140,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): axl.EvolvableANN, num_features=num_features, num_hidden=num_hidden, - weights=weights + weights=weights, ) diff --git a/axelrod/tests/strategies/test_apavlov.py b/axelrod/tests/strategies/test_apavlov.py index 8d3288a93..3b37f1278 100644 --- a/axelrod/tests/strategies/test_apavlov.py +++ b/axelrod/tests/strategies/test_apavlov.py @@ -34,7 +34,9 @@ def test_strategy_versus_mock_player(self): opponent = axl.MockPlayer(actions=[C] * 6 + [D]) actions = [(C, C)] * 6 + [(C, D), (D, C)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Cooperative"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Cooperative"}, ) def test_strategy_versus_defector(self): @@ -70,30 +72,47 @@ def test_strategy_PavlovD(self): opponent = axl.Cycler(cycle="DDC") actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "PavlovD"}, ) def test_strategy_PavlovD2(self): """Tests that PavolvD is identified by DDCDDC and that the response is D then C""" opponent = axl.MockPlayer(actions=[D, D, C, D, D, C, D]) - actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D), (C, D)] + actions = [ + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, C), + (D, D), + (C, D), + ] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "PavlovD"}, ) def test_strategy_random(self): opponent = axl.MockPlayer(actions=[C, C, C, D, D, D]) actions = [(C, C), (C, C), (C, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) def test_strategy_random2(self): opponent = axl.MockPlayer(actions=[D, D, D, C, C, C]) actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (D, D)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) @@ -143,42 +162,100 @@ def test_strategy_defector3(self): def test_strategy_defector4(self): opponent = axl.MockPlayer(actions=[C, D, D, C, D, D, D]) - actions = [(C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, D), (D, C)] + actions = [ + (C, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"} ) def test_strategy_stft(self): opponent = axl.MockPlayer(actions=[C, D, D, C, C, D, D]) - actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (C, D), (D, C)] + actions = [ + (C, C), + (C, D), + (D, D), + (D, C), + (C, C), + (C, D), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_stft2(self): opponent = axl.MockPlayer(actions=[C, D, C, D, C, D, D]) - actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, C)] + actions = [ + (C, C), + (C, D), + (D, C), + (C, D), + (D, C), + (C, D), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_stft3(self): opponent = axl.MockPlayer(actions=[D, D, D, C, C, C, C]) - actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (C, C), (C, D)] + actions = [ + (C, D), + (D, D), + (D, D), + (D, C), + (C, C), + (C, C), + (C, C), + (C, D), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_random(self): opponent = axl.MockPlayer(actions=[C, C, C, C, D, D]) - actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, D), (D, C), (D, C)] + actions = [ + (C, C), + (C, C), + (C, C), + (C, C), + (C, D), + (D, D), + (D, C), + (D, C), + ] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) def test_strategy_random2(self): opponent = axl.MockPlayer(actions=[D, D, C, C, C, C]) - actions = [(C, D), (D, D), (D, C), (C, C), (C, C), (C, C), (D, D), (D, D)] + actions = [ + (C, D), + (D, D), + (D, C), + (C, C), + (C, C), + (C, C), + (D, D), + (D, D), + ] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) diff --git a/axelrod/tests/strategies/test_axelrod_first.py b/axelrod/tests/strategies/test_axelrod_first.py index 40f09e80c..dfb1bd0b9 100644 --- a/axelrod/tests/strategies/test_axelrod_first.py +++ b/axelrod/tests/strategies/test_axelrod_first.py @@ -107,7 +107,9 @@ class TestFirstByFeld(TestPlayer): def test_cooperation_probability(self): # Test cooperation probabilities - p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100) + p1 = self.player( + start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100 + ) self.assertEqual(1.0, p1._cooperation_probability()) p2 = axl.Cooperator() match = axl.Match((p1, p2), turns=50) @@ -118,7 +120,9 @@ def test_cooperation_probability(self): self.assertEqual(0.8, p1._cooperation_probability()) # Test cooperation probabilities, second set of params - p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200) + p1 = self.player( + start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200 + ) self.assertEqual(1.0, p1._cooperation_probability()) match = axl.Match((p1, p2), turns=100) match.play() @@ -131,10 +135,14 @@ def test_decay(self): # Test beyond 200 rounds for opponent in [axl.Cooperator(), axl.Defector()]: player = self.player() - self.assertEqual(player._cooperation_probability(), player._start_coop_prob) + self.assertEqual( + player._cooperation_probability(), player._start_coop_prob + ) match = axl.Match((player, opponent), turns=201) match.play() - self.assertEqual(player._cooperation_probability(), player._end_coop_prob) + self.assertEqual( + player._cooperation_probability(), player._end_coop_prob + ) def test_stochastic_behavior(self): actions = [(C, C)] * 13 + [(D, C)] @@ -176,19 +184,28 @@ def test_strategy(self): actions += [(D, C)] # 51 turns actions += [(C, D), (D, C)] * 2 + [(C, D)] # 56 turns self.versus_test( - axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=0 + axl.Alternator(), + expected_actions=actions, + attrs=expected_attrs, + seed=0, ) # Against defector actions = [(C, D)] + [(D, D)] * 55 # 56 turns self.versus_test( - axl.Defector(), expected_actions=actions, attrs=expected_attrs, seed=0 + axl.Defector(), + expected_actions=actions, + attrs=expected_attrs, + seed=0, ) # Against cooperator actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=0 + axl.Cooperator(), + expected_actions=actions, + attrs=expected_attrs, + seed=0, ) # Test recognition of random player @@ -198,12 +215,21 @@ def test_strategy(self): } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 # 56 turns self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=1 + axl.Cooperator(), + expected_actions=actions, + attrs=expected_attrs, + seed=1, ) - expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 63} + expected_attrs = { + "opponent_is_random": False, + "next_random_defection_turn": 63, + } actions += [(C, C)] # 57 turns self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=8 + axl.Cooperator(), + expected_actions=actions, + attrs=expected_attrs, + seed=8, ) expected_attrs = { @@ -215,11 +241,17 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 57 turns actions += [(D, D)] self.versus_test( - axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=3 + axl.Alternator(), + expected_actions=actions, + attrs=expected_attrs, + seed=3, ) actions += [(D, C), (D, D)] * 5 self.versus_test( - axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=4 + axl.Alternator(), + expected_actions=actions, + attrs=expected_attrs, + seed=4, ) # Test versus TfT @@ -231,20 +263,35 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 56 turns actions += [(C, D), (D, C)] * 50 self.versus_test( - axl.TitForTat(), expected_actions=actions, seed=0, attrs=expected_attrs + axl.TitForTat(), + expected_actions=actions, + seed=0, + attrs=expected_attrs, ) # Test random defections - expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 76} + expected_attrs = { + "opponent_is_random": False, + "next_random_defection_turn": 76, + } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 15 + [(D, C)] + [(C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs + axl.Cooperator(), + expected_actions=actions, + seed=0, + attrs=expected_attrs, ) - expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 79} + expected_attrs = { + "opponent_is_random": False, + "next_random_defection_turn": 79, + } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 14 + [(D, C)] + [(C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, seed=5, attrs=expected_attrs + axl.Cooperator(), + expected_actions=actions, + seed=5, + attrs=expected_attrs, ) @@ -270,11 +317,29 @@ def test_strategy(self): self.versus_test(axl.Alternator(), expected_actions=actions, seed=0) opponent = axl.MockPlayer(actions=[D] * 8) - actions = [(C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D)] + actions = [ + (C, D), + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + ] self.versus_test(opponent, expected_actions=actions, seed=1) opponent = axl.MockPlayer(actions=[D] * 8) - actions = [(C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D), (C, D)] + actions = [ + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + (C, D), + ] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -349,14 +414,41 @@ def test_strategy(self): actions = [(C, C)] * 9 self.versus_test(axl.Cooperator(), expected_actions=actions) - actions = [(C, D), (D, D), (D, D), (C, D), (C, D), (C, D), (C, D), (C, D)] + actions = [ + (C, D), + (D, D), + (D, D), + (C, D), + (C, D), + (C, D), + (C, D), + (C, D), + ] self.versus_test(axl.Defector(), expected_actions=actions) - actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C), (C, D)] + actions = [ + (C, C), + (C, D), + (D, C), + (C, D), + (D, C), + (C, D), + (D, C), + (C, D), + ] self.versus_test(axl.Alternator(), expected_actions=actions) opponent = axl.MockPlayer(actions=[D, C]) - actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (D, D), (D, C)] + actions = [ + (C, D), + (D, C), + (D, D), + (D, C), + (D, D), + (D, C), + (D, D), + (D, C), + ] self.versus_test(opponent, expected_actions=actions) @@ -437,7 +529,9 @@ def test_strategy(self): # Test beyond 10 rounds opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 5 + [C, D] * 5) - actions = [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)] + actions = ( + [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)] + ) self.versus_test(opponent, expected_actions=actions, seed=20) @@ -494,7 +588,9 @@ def test_strategy(self): opponent = axl.Cooperator() actions = [(C, C)] * 17 + [(D, C)] * 2 self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_is_random": False} + opponent, + expected_actions=actions, + attrs={"opponent_is_random": False}, ) actions = actions[:-2] + [(C, C)] * 2 @@ -510,7 +606,9 @@ def test_strategy(self): opponent = axl.Defector() actions = [(C, D)] * 4 + [(D, D)] * 15 self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_is_random": False} + opponent, + expected_actions=actions, + attrs={"opponent_is_random": False}, ) # SteinAndRapoport vs Alternator @@ -525,7 +623,9 @@ def test_strategy(self): actions += [(D, D), (D, C), (D, D), (D, C)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_is_random": True} + opponent, + expected_actions=actions, + attrs={"opponent_is_random": True}, ) # The test is carried out again every 15 rounds. @@ -570,8 +670,11 @@ def test_strategy(self): # Cooperator Test does noot defect if game length is unknown opponent = axl.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] - self.versus_test(opponent, expected_actions=actions, - match_attributes={"length": float("inf")}) + self.versus_test( + opponent, + expected_actions=actions, + match_attributes={"length": float("inf")}, + ) # Defector Test opponent = axl.Defector() diff --git a/axelrod/tests/strategies/test_axelrod_second.py b/axelrod/tests/strategies/test_axelrod_second.py index 26fa8078e..5172eb89a 100644 --- a/axelrod/tests/strategies/test_axelrod_second.py +++ b/axelrod/tests/strategies/test_axelrod_second.py @@ -112,7 +112,9 @@ def test_strategy(self): # Now play TfT opponent = axl.MockPlayer(actions=[C, D, C, D, D, C]) actions = [(D, C), (C, D), (C, C), (C, D), (D, D), (D, C), (C, C)] - self.versus_test(opponent, expected_actions=actions, attrs={"is_TFT": True}) + self.versus_test( + opponent, expected_actions=actions, attrs={"is_TFT": True} + ) class TestGladstein(TestPlayer): @@ -151,11 +153,15 @@ def test_strategy(self): # Ratio is 1/3 when MockPlayer defected for the first time. opponent = axl.MockPlayer(actions=[C, C, C, D, D]) actions = [(D, C), (C, C), (C, C), (D, D), (C, D)] - self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) + self.versus_test( + opponent, expected_actions=actions, attrs={"patsy": False} + ) opponent = axl.AntiTitForTat() actions = [(D, C), (C, C), (C, D), (C, D), (D, D)] - self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) + self.versus_test( + opponent, expected_actions=actions, attrs={"patsy": False} + ) class TestTranquilizer(TestPlayer): @@ -184,7 +190,16 @@ def test_initialised_variables(self): def test_strategy(self): opponent = axl.Bully() - actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, D), (D, C), (C, C)] + actions = [ + (C, D), + (D, D), + (D, C), + (C, C), + (C, D), + (D, D), + (D, C), + (C, C), + ] expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, @@ -192,7 +207,9 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) # Tests whether TitForTat is played given score is below 1.75 @@ -205,10 +222,14 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) opponent = axl.MockPlayer([C] * 2 + [D] * 8 + [C] * 4) - actions = [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 + actions = ( + [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 + ) expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, @@ -216,7 +237,9 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) def test_strategy2(self): # If score is between 1.75 and 2.25, may cooperate or defect @@ -356,7 +379,9 @@ def test_strategy10(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) class TestGrofman(TestPlayer): @@ -603,7 +628,9 @@ def test_strategy(self): # Tries to cooperate every third time until detecting defective actions = ( - [(C, D), (D, D), (D, D), (D, D)] * 6 + [(C, D), (D, D)] + [(D, D)] * 100 + [(C, D), (D, D), (D, D), (D, D)] * 6 + + [(C, D), (D, D)] + + [(D, D)] * 100 ) self.versus_test(axl.Defector(), expected_actions=actions) @@ -656,7 +683,9 @@ def test_stochastic_behavior(self): """Test random responses on turns 1 through 17.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match((axl.SecondByCave(), axl.Alternator()), turns=30, seed=1) + match = axl.Match( + (axl.SecondByCave(), axl.Alternator()), turns=30, seed=1 + ) match.play() player_history = [round[0] for round in match.result] self.assertTrue(C in player_history[1:17]) @@ -667,7 +696,9 @@ def test_serial_defection_against_defector(self): to respond D->D.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match((axl.SecondByCave(), axl.Defector()), turns=30, seed=1) + match = axl.Match( + (axl.SecondByCave(), axl.Defector()), turns=30, seed=1 + ) result = match.play() self.assertEqual(result[0], (C, D)) self.assertEqual(result[18:], [(D, D)] * 12) @@ -679,7 +710,9 @@ def test_serial_defection_against_mostly_defector(self): # makes finding seeds reproducibly difficult. opponent_actions = [D] * 17 + [C, C, C, C] almost_defector = axl.MockPlayer(actions=opponent_actions) - match = axl.Match((axl.SecondByCave(), almost_defector), turns=21, seed=1) + match = axl.Match( + (axl.SecondByCave(), almost_defector), turns=21, seed=1 + ) result = match.play() self.assertEqual(result[0], (C, D)) self.assertEqual(result[-3], (C, C)) @@ -690,10 +723,12 @@ def test_versus_alternator(self): random and defect.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match((axl.SecondByCave(), axl.Alternator()), turns=100, seed=1) + match = axl.Match( + (axl.SecondByCave(), axl.Alternator()), turns=100, seed=1 + ) result = match.play() self.assertEqual(result[0], (C, C)) - self.assertEqual(result[37: 40], [(C, D), (D, C), (D, D)]) + self.assertEqual(result[37:40], [(C, D), (D, C), (D, D)]) self.assertEqual(result[40:], [(D, C), (D, D)] * 30) @@ -923,7 +958,10 @@ def test_exit_fair_weather(self): # Immediately exit Fair-weather actions += [(D, C), (C, C), (D, C), (C, C)] self.versus_test( - Defect37_big, expected_actions=actions, seed=10, attrs={"mode": "Normal"} + Defect37_big, + expected_actions=actions, + seed=10, + attrs={"mode": "Normal"}, ) def test_exit_fair_weather2(self): @@ -934,7 +972,10 @@ def test_exit_fair_weather2(self): opponent_actions = [C] * 36 + [D] + [C] * 100 + [D] + [C] * 4 Defect37_big = axl.MockPlayer(actions=opponent_actions) self.versus_test( - Defect37_big, expected_actions=actions, seed=1, attrs={"mode": "Normal"} + Defect37_big, + expected_actions=actions, + seed=1, + attrs={"mode": "Normal"}, ) def test_non_fair_weather(self): @@ -1015,8 +1056,10 @@ def test_parity_limit_shortening(self): # Now hit the limit sooner actions += [(C, D), (D, C), (C, D), (C, C)] * 5 self.versus_test( - AsyncAlternator, expected_actions=actions, attrs={"parity_limit": 3}, - seed=10 + AsyncAlternator, + expected_actions=actions, + attrs={"parity_limit": 3}, + seed=10, ) def test_detect_streak(self): @@ -1036,14 +1079,15 @@ def test_detect_streak(self): axl.Defector(), expected_actions=actions, attrs={"recorded_defects": 119}, - seed=10 + seed=10, ) def test_detect_random(self): """Tests that detect_random() is triggered on a Random opponent and that the strategy defects thereafter.""" - match = axl.Match((axl.SecondByHarrington(), axl.Random()), seed=10, - turns=31) + match = axl.Match( + (axl.SecondByHarrington(), axl.Random()), seed=10, turns=31 + ) match.play() player = match.players[0] # Check that detect_random(30) is True. @@ -1149,14 +1193,18 @@ def test_strategy(self): actions = [(C, D)] + [(D, D)] * 8 self.versus_test( - axl.Defector(), expected_actions=actions, attrs={"score_to_beat_inc": 5} + axl.Defector(), + expected_actions=actions, + attrs={"score_to_beat_inc": 5}, ) actions = [(C, D)] + [(D, D)] * 8 # On tenth turn, try a fresh start actions += [(C, D), (C, D)] + [(D, D)] * 2 self.versus_test( - axl.Defector(), expected_actions=actions, attrs={"last_fresh_start": 11} + axl.Defector(), + expected_actions=actions, + attrs={"last_fresh_start": 11}, ) actions = [(C, C), (C, D)] @@ -1333,9 +1381,28 @@ def test_defects_on_turn_10_against_defector(self): self.versus_test(axl.Defector(), expected_actions=actions) def test_defection_logic_triggered(self): - actions = [(C, D), (C, D), (C, C), (C, D), (C, D), (C, C), (C, D), (C, D), - (C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (C, D), - (D, D), (D, C), (C, D), (D, D)] + actions = [ + (C, D), + (C, D), + (C, C), + (C, D), + (C, D), + (C, C), + (C, D), + (C, D), + (C, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + ] self.versus_test(axl.CyclerDDC(), expected_actions=actions) def test_defection_logic_not_triggered(self): @@ -1409,7 +1476,9 @@ class TestRichardHufford(TestPlayer): def test_strategy(self): actions = [(C, C)] * 19 + [(D, C), (C, C), (C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 14} + axl.Cooperator(), + expected_actions=actions, + attrs={"streak_needed": 14}, ) actions = [(C, C)] * 19 + [(D, C), (C, C)] @@ -1418,14 +1487,18 @@ def test_strategy(self): ] # This is the first Cooperation that gets counted on the new streak actions += [(C, C)] * 13 + [(D, C), (C, C), (C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 11} + axl.Cooperator(), + expected_actions=actions, + attrs={"streak_needed": 11}, ) opponent_actions = [C] * 20 + [D] BoredCooperator = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 19 + [(D, C), (C, D), (C, C)] self.versus_test( - BoredCooperator, expected_actions=actions, attrs={"streak_needed": 31} + BoredCooperator, + expected_actions=actions, + attrs={"streak_needed": 31}, ) actions = [(C, D)] # "Disagreement" @@ -1434,7 +1507,9 @@ def test_strategy(self): actions += [(C, D)] # TFT. Disagreement actions += [(D, C)] # Three of last four are disagreements. actions += [(C, C)] # TFT. Disagreement - actions += [(D, D)] # Three of last four are disagreements. Disagreement + actions += [ + (D, D) + ] # Three of last four are disagreements. Disagreement actions += [(D, D)] # Three of last four are disagreements. actions += [(D, D)] # Now there are 5/9 disagreements, so Defect. self.versus_test( @@ -1470,23 +1545,39 @@ def test_strategy(self): # It's actually impossible to Defect on the third move. actions += [(D, D)] # (D, C, *) gets updated, then checked. actions += [(C, D)] # (D, C, *) gets updated, but (D, D, *) checked. - actions += [(D, D)] * 30 # (D, D, *) gets updated and checked from here on. + actions += [ + (D, D) + ] * 30 # (D, D, *) gets updated and checked from here on. self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D)] - actions += [(C, C)] # Increment (C, C, C). Check (C, C, *). Cooperate. + actions += [ + (C, C) + ] # Increment (C, C, C). Check (C, C, *). Cooperate. # Reminder that first C is default value and last C is opponent's first move. - actions += [(C, D)] # Increment (C, C, D). Check (D, C, *) = 0. Cooperate. - actions += [(C, C)] # Increment (D, C, C). Check (C, C, *) = 0. Cooperate. + actions += [ + (C, D) + ] # Increment (C, C, D). Check (D, C, *) = 0. Cooperate. + actions += [ + (C, C) + ] # Increment (D, C, C). Check (C, C, *) = 0. Cooperate. # There is one Defection and one Cooperation in this scenario, # but the Cooperation was due to a default value only. We can see where this is going. - actions += [(C, D)] # Increment (C, C, D). Check (D, C, *) = 1. Cooperate. - actions += [(D, C)] # Increment (D, C, C). Check (C, C, *) = -1. Defect. + actions += [ + (C, D) + ] # Increment (C, C, D). Check (D, C, *) = 1. Cooperate. + actions += [ + (D, C) + ] # Increment (D, C, C). Check (C, C, *) = -1. Defect. actions += [ (C, D) ] # Increment (C, C, D). Check (D, D, *) = 0 (New). Cooperate. - actions += [(D, C)] # Increment (D, D, C). Check (C, C, *) < 0. Defect. - actions += [(C, D)] # Increment (C, C, D). Check (D, D, *) > 0. Cooperate. + actions += [ + (D, C) + ] # Increment (D, D, C). Check (C, C, *) < 0. Defect. + actions += [ + (C, D) + ] # Increment (C, C, D). Check (D, D, *) > 0. Cooperate. actions += [(D, C), (C, D)] * 15 # This pattern continues for a while. actions += [ (D, C), @@ -1670,7 +1761,9 @@ def test_strategy(self): opponent_actions = [C] * 100 + [D] * 10 Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 100 + [(C, D)] * 4 - self.versus_test(Change_of_Heart, expected_actions=actions, attrs={"credit": 2}) + self.versus_test( + Change_of_Heart, expected_actions=actions, attrs={"credit": 2} + ) Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions += [(C, D)] * 2 self.versus_test( @@ -1678,6 +1771,7 @@ def test_strategy(self): ) # Still Cooperate, because Defect rate is low + class TestRowsam(TestPlayer): name = "Second by Rowsam" player = axl.SecondByRowsam @@ -1699,12 +1793,18 @@ def test_strategy(self): # Against a Defector should eventually enter Defect mode actions = [(C, D)] * 5 actions += [(D, D), (C, D), (D, D)] # Do a Coop-Def cycle - self.versus_test(axl.Defector(), expected_actions=actions, attrs={ - "distrust_points": 5}) + self.versus_test( + axl.Defector(), + expected_actions=actions, + attrs={"distrust_points": 5}, + ) actions += [(C, D)] * 3 # Continue for now actions += [(D, D)] * 100 # Now Defect mode - self.versus_test(axl.Defector(), expected_actions=actions, attrs={ - "distrust_points": 10, "mode": "Defect"}) + self.versus_test( + axl.Defector(), + expected_actions=actions, + attrs={"distrust_points": 10, "mode": "Defect"}, + ) # Test specific score scenarios # 5 Defects @@ -1712,8 +1812,11 @@ def test_strategy(self): custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 5 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 5, "current_score": 0}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 5, "current_score": 0}, + ) # 3 Defects opponent_actions = [D] * 3 + [C] * 100 @@ -1721,8 +1824,11 @@ def test_strategy(self): actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 3, "current_score": 6}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 3, "current_score": 6}, + ) # 2 Defects opponent_actions = [D] * 2 + [C] * 100 @@ -1730,8 +1836,11 @@ def test_strategy(self): actions = [(C, D)] * 2 actions += [(C, C)] * 3 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 2, "current_score": 9}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 2, "current_score": 9}, + ) # 1 Defect opponent_actions = [D] * 1 + [C] * 100 @@ -1739,8 +1848,11 @@ def test_strategy(self): actions = [(C, D)] * 1 actions += [(C, C)] * 4 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 1, "current_score": 12}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 1, "current_score": 12}, + ) # Test that some distrust_points wear off. opponent_actions = [D] * 3 + [C] * 100 @@ -1748,27 +1860,42 @@ def test_strategy(self): actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 3, "current_score": 6}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 3, "current_score": 6}, + ) custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 3 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 4, "current_score": 28}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 4, "current_score": 28}, + ) custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 4 # No defect or cycle this time. - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 3, "current_score": 50}) # One point wears off. + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 3, "current_score": 50}, + ) # One point wears off. custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 2}) # Second point wears off + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 2}, + ) # Second point wears off custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 2}) # But no more + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 2}, + ) # But no more class TestAppold(TestPlayer): @@ -1787,64 +1914,93 @@ class TestAppold(TestPlayer): def test_cooperate_against_cooperating_opponent(self): """Strategy should cooperate 100% of the time with a fully cooperating opponent.""" actions = [(C, C)] * 100 - self.versus_test(axl.Cooperator(), expected_actions=actions, - attrs={ - "first_opp_def": False, - "total_num_of_x": {C: 99, D: 1}, - "opp_c_after_x": {C: 99, D: 1} - }) + self.versus_test( + axl.Cooperator(), + expected_actions=actions, + attrs={ + "first_opp_def": False, + "total_num_of_x": {C: 99, D: 1}, + "opp_c_after_x": {C: 99, D: 1}, + }, + ) def test_cooperate_on_first_four_turns(self): """Strategy will cooperate on the first four turns regardless of opponent.""" # Hypothesis opportunity: choose random opponent player_expected_actions = [C, C, C, C] coplayer_expected_actions = [D, D, D, D] - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(axl.Defector(), turns=4, expected_actions=expected_actions, - attrs={ - "first_opp_def": False, - "total_num_of_x": {C: 3, D: 1}, - "opp_c_after_x": {C: 0, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + axl.Defector(), + turns=4, + expected_actions=expected_actions, + attrs={ + "first_opp_def": False, + "total_num_of_x": {C: 3, D: 1}, + "opp_c_after_x": {C: 0, D: 1}, + }, + ) def test_fifth_move_cooperate(self): """Strategy will cooperate on a fifth move defection and set first_opp_def.""" player_expected_actions = [C, C, C, C, C, C] coplayer_expected_actions = [C, C, C, C, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(coplayer, turns=6, expected_actions=expected_actions, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 5, D: 1}, - "opp_c_after_x": {C: 4, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + coplayer, + turns=6, + expected_actions=expected_actions, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 5, D: 1}, + "opp_c_after_x": {C: 4, D: 1}, + }, + ) def test_sixth_move_cooperate(self): """Strategy will cooperate on a sixth move defection if it is the first.""" player_expected_actions = [C, C, C, C, C, C, C] coplayer_expected_actions = [C, C, C, C, C, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(coplayer, turns=7, expected_actions=expected_actions, seed=1, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 6, D: 1}, - "opp_c_after_x": {C: 5, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + coplayer, + turns=7, + expected_actions=expected_actions, + seed=1, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 6, D: 1}, + "opp_c_after_x": {C: 5, D: 1}, + }, + ) def test_sixth_move_defect(self): """Strategy will defect on a sixth move defection if it is not the first.""" player_expected_actions = [C, C, C, C, C, C, D] coplayer_expected_actions = [C, C, C, C, D, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(coplayer, turns=7, expected_actions=expected_actions, seed=10, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 6, D: 1}, - "opp_c_after_x": {C: 4, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + coplayer, + turns=7, + expected_actions=expected_actions, + seed=10, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 6, D: 1}, + "opp_c_after_x": {C: 4, D: 1}, + }, + ) def test_later_single_defection_forgiveness(self): # An opponent who defects after a long time, then tries cooperating @@ -1856,8 +2012,12 @@ def test_later_single_defection_forgiveness(self): actions += [(C, D)] # But we forgive it (and record it). actions += [(C, C)] * 10 - self.versus_test(MostlyCooperates, expected_actions=actions, seed=1, - attrs={"first_opp_def": True}) + self.versus_test( + MostlyCooperates, + expected_actions=actions, + seed=1, + attrs={"first_opp_def": True}, + ) def test_stochastic_behavior(self): opponent = axl.Defector() @@ -1871,19 +2031,24 @@ def test_stochastic_behavior(self): # Then defect most of the time, depending on the random number. We # don't defect 100% of the time, because of the way that initialize # opp_c_after_x. - actions += [(D, D), - (C, D), - (D, D), - (D, D), # C can never be two moves after a C. - (D, D), - (D, D), - (D, D), - (D, D), - (D, D), - (D, D), - (C, D), - (C, D), - (D, D), + actions += [ + (D, D), + (C, D), + (D, D), + (D, D), # C can never be two moves after a C. + (D, D), + (D, D), + (D, D), + (D, D), + (D, D), + (D, D), + (C, D), + (C, D), + (D, D), ] - self.versus_test(opponent, expected_actions=actions, seed=1018, - attrs={"first_opp_def": True}) + self.versus_test( + opponent, + expected_actions=actions, + seed=1018, + attrs={"first_opp_def": True}, + ) diff --git a/axelrod/tests/strategies/test_backstabber.py b/axelrod/tests/strategies/test_backstabber.py index 1f580c9cf..c4a988aec 100644 --- a/axelrod/tests/strategies/test_backstabber.py +++ b/axelrod/tests/strategies/test_backstabber.py @@ -117,9 +117,13 @@ def test_when_alt_strategy_is_triggered(self): def test_starting_defect_keeps_alt_strategy_from_triggering(self): opponent_actions_suffix = [C, D, C, D, D] + 3 * [C] - expected_actions_suffix = [(C, C), (C, D), (C, C), (C, D), (C, D)] + 3 * [ - (D, C) - ] + expected_actions_suffix = [ + (C, C), + (C, D), + (C, C), + (C, D), + (C, D), + ] + 3 * [(D, C)] defects_on_first = [D] + [C] * 6 defects_on_first_actions = [(C, D)] + [(C, C)] * 6 diff --git a/axelrod/tests/strategies/test_better_and_better.py b/axelrod/tests/strategies/test_better_and_better.py index 2499eb4a3..6b351fe7e 100644 --- a/axelrod/tests/strategies/test_better_and_better.py +++ b/axelrod/tests/strategies/test_better_and_better.py @@ -25,21 +25,15 @@ def test_strategy(self): """Tests that the strategy gives expected behaviour.""" expected_actions = [(D, D)] * 90 + [(C, D)] self.versus_test( - axl.Defector(), - expected_actions=expected_actions, - seed=6, + axl.Defector(), expected_actions=expected_actions, seed=6, ) expected_actions = [(D, C)] * 10 self.versus_test( - axl.Cooperator(), - expected_actions=expected_actions, - seed=8, + axl.Cooperator(), expected_actions=expected_actions, seed=8, ) expected_actions = [(D, D)] * 41 + [(C, D)] self.versus_test( - axl.Defector(), - expected_actions=expected_actions, - seed=13, + axl.Defector(), expected_actions=expected_actions, seed=13, ) expected_indices = [18, 39, 49, 67, 77, 116, 139, 142, 149] m = axl.Match((self.player(), axl.Defector()), turns=150, seed=111) diff --git a/axelrod/tests/strategies/test_calculator.py b/axelrod/tests/strategies/test_calculator.py index 0d5eedfb3..e99114600 100644 --- a/axelrod/tests/strategies/test_calculator.py +++ b/axelrod/tests/strategies/test_calculator.py @@ -25,7 +25,9 @@ class TestCalculator(TestPlayer): def test_twenty_rounds_joss_for_cyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed + ) match.play() self.versus_test( axl.Alternator(), expected_actions=match.result, seed=seed @@ -34,17 +36,24 @@ def test_twenty_rounds_joss_for_cyclers(self): def test_twenty_rounds_joss_then_defects_for_cyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed + ) match.play() expected_actions = match.result + [(D, C), (D, D), (D, C), (D, D)] self.versus_test( - axl.Alternator(), expected_actions=expected_actions, seed=seed, turns=24, + axl.Alternator(), + expected_actions=expected_actions, + seed=seed, + turns=24, ) def test_twenty_rounds_joss_for_noncyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed + ) match.play() self.versus_test( axl.AntiCycler(), expected_actions=match.result, seed=seed @@ -53,16 +62,29 @@ def test_twenty_rounds_joss_for_noncyclers(self): def test_twenty_rounds_joss_then_tft_for_noncyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed + ) match.play() - expected_actions = match.result + [(C, C), (C, C), (C, D), (D, C), (C, C)] + expected_actions = match.result + [ + (C, C), + (C, C), + (C, D), + (D, C), + (C, C), + ] self.versus_test( - axl.AntiCycler(), expected_actions=expected_actions, seed=seed, turns=24, + axl.AntiCycler(), + expected_actions=expected_actions, + seed=seed, + turns=24, ) def test_edge_case_calculator_sees_cycles_of_size_ten(self): ten_length_cycle = [C, D, C, C, D, C, C, C, D, C] - self.assertEqual(detect_cycle((ten_length_cycle * 2)), tuple(ten_length_cycle)) + self.assertEqual( + detect_cycle((ten_length_cycle * 2)), tuple(ten_length_cycle) + ) ten_cycle_twenty_rounds = get_joss_strategy_actions( ten_length_cycle * 2, indices_to_flip=[16] @@ -85,7 +107,9 @@ def test_edge_case_calculator_ignores_cycles_gt_len_ten(self): ) opponent_actions = twenty_rounds_of_eleven_len_cycle[:-1] + [D] + [C, D] - self.assertEqual(detect_cycle(opponent_actions), tuple(eleven_length_cycle)) + self.assertEqual( + detect_cycle(opponent_actions), tuple(eleven_length_cycle) + ) uses_tit_for_tat_after_twenty_rounds = twenty_rounds + [(D, C), (C, D)] self.versus_test( @@ -105,13 +129,19 @@ def test_get_joss_strategy_actions(self): self.assertEqual(get_joss_strategy_actions(opponent, []), without_flip) self.assertEqual( - get_joss_strategy_actions(opponent, flip_never_occurs_at_index_zero), + get_joss_strategy_actions( + opponent, flip_never_occurs_at_index_zero + ), without_flip, ) - self.assertEqual(get_joss_strategy_actions(opponent, flip_indices), with_flip) + self.assertEqual( + get_joss_strategy_actions(opponent, flip_indices), with_flip + ) -def get_joss_strategy_actions(opponent_moves: list, indices_to_flip: list) -> list: +def get_joss_strategy_actions( + opponent_moves: list, indices_to_flip: list +) -> list: """ Takes a list of opponent moves and returns a tuple list of [(Joss moves, opponent moves)]. "indices_to_flip" are the indices where Joss differs from it's expected TitForTat. diff --git a/axelrod/tests/strategies/test_cooperator.py b/axelrod/tests/strategies/test_cooperator.py index aca2290ae..7dd4e7d3b 100644 --- a/axelrod/tests/strategies/test_cooperator.py +++ b/axelrod/tests/strategies/test_cooperator.py @@ -41,7 +41,9 @@ class TestTrickyCooperator(TestPlayer): def test_strategy(self): # Test if it tries to trick opponent. - self.versus_test(axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)]) + self.versus_test( + axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)] + ) opponent_actions = [C, C, C, C, D, D] expected_actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (C, D)] @@ -68,7 +70,7 @@ def test_cooperates_in_first_three_rounds(self): self.versus_test(axl.Alternator(), expected_actions=against_alternator) def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_depth_ten( - self + self, ): against_cooperator = [(C, C)] * 3 + [(D, C)] * 20 self.versus_test(axl.Cooperator(), expected_actions=against_cooperator) @@ -76,4 +78,6 @@ def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_d def test_defects_when_opponent_has_no_defections_to_history_depth_ten(self): opponent_actions = [D] + [C] * 10 + [D, C] expected_actions = [(C, D)] + [(C, C)] * 10 + [(D, D), (C, C)] - self.versus_test(axl.MockPlayer(actions=opponent_actions), expected_actions) + self.versus_test( + axl.MockPlayer(actions=opponent_actions), expected_actions + ) diff --git a/axelrod/tests/strategies/test_cycler.py b/axelrod/tests/strategies/test_cycler.py index 675d4b4d5..a12b14bc3 100644 --- a/axelrod/tests/strategies/test_cycler.py +++ b/axelrod/tests/strategies/test_cycler.py @@ -98,7 +98,9 @@ def test_memory_depth_is_len_cycle_minus_one(self): def test_cycler_works_as_expected(self): expected = [(C, D), (D, D), (D, D), (C, D)] * 2 self.versus_test( - axl.Defector(), expected_actions=expected, init_kwargs={"cycle": "CDDC"} + axl.Defector(), + expected_actions=expected, + init_kwargs={"cycle": "CDDC"}, ) def test_cycle_raises_value_error_on_bad_cycle_str(self): @@ -172,11 +174,17 @@ def test_normalized_parameters(self): ) cycle = "C" * random.randint(0, 20) + "D" * random.randint(0, 20) - self.assertEqual(self.player_class(cycle=cycle, seed=1)._normalize_parameters(cycle=cycle), - (cycle, len(cycle))) + self.assertEqual( + self.player_class(cycle=cycle, seed=1)._normalize_parameters( + cycle=cycle + ), + (cycle, len(cycle)), + ) cycle_length = random.randint(1, 20) - random_cycle, cycle_length2 = self.player_class(cycle=cycle, seed=1)._normalize_parameters(cycle_length=cycle_length) + random_cycle, cycle_length2 = self.player_class( + cycle=cycle, seed=1 + )._normalize_parameters(cycle_length=cycle_length) self.assertEqual(len(random_cycle), cycle_length) self.assertEqual(cycle_length, cycle_length2) diff --git a/axelrod/tests/strategies/test_darwin.py b/axelrod/tests/strategies/test_darwin.py index 50c0c4f2b..146489f8a 100644 --- a/axelrod/tests/strategies/test_darwin.py +++ b/axelrod/tests/strategies/test_darwin.py @@ -76,7 +76,9 @@ def test_against_geller_and_mindreader(self): def test_reset_history_and_attributes(self): # Overwrite this method because Darwin does not reset - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 + ) p1 = self.player() self.assertEqual(p1.genome, [D, C, C, C, D]) @@ -89,7 +91,9 @@ def test_all_darwin_instances_share_one_genome(self): p2 = self.player() self.assertIs(p1.genome, p2.genome) - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 + ) self.assertEqual(p2.genome, [D, C, C, C, D]) self.assertIs(p1.genome, p2.genome) @@ -97,7 +101,9 @@ def test_all_darwin_instances_share_one_genome(self): self.assertIs(p3.genome, p2.genome) def test_reset_genome(self): - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 + ) self.player.reset_genome() self.assertEqual(self.player().genome, [C]) diff --git a/axelrod/tests/strategies/test_dbs.py b/axelrod/tests/strategies/test_dbs.py index 93d61e41a..a3afb81a4 100644 --- a/axelrod/tests/strategies/test_dbs.py +++ b/axelrod/tests/strategies/test_dbs.py @@ -67,7 +67,9 @@ def test_move_gen_cooperator(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.cooperator_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.cooperator_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_defector(self): @@ -89,7 +91,9 @@ def test_move_gen_defector(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.defector_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.defector_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_titForTat(self): @@ -125,7 +129,9 @@ def test_move_gen_titForTat(self): """ expected_output = [C, C, C, C] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.titForTat_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.titForTat_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_alternator(self): @@ -147,7 +153,9 @@ def test_move_gen_alternator(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.random_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_random(self): @@ -169,7 +177,9 @@ def test_move_gen_random(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.random_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_grudger(self): @@ -192,7 +202,9 @@ def test_move_gen_grudger(self): """ expected_output = [C, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.grudger_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.grudger_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) diff --git a/axelrod/tests/strategies/test_doubler.py b/axelrod/tests/strategies/test_doubler.py index e3d436302..a02f42695 100644 --- a/axelrod/tests/strategies/test_doubler.py +++ b/axelrod/tests/strategies/test_doubler.py @@ -22,7 +22,7 @@ class TestDoubler(TestPlayer): } def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperations( - self + self, ): opponent_plays = [C] * 7 + [D] * 4 + [C] actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, C)] @@ -31,7 +31,7 @@ def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperat ) def test_defects_if_opponent_last_play_D_and_defections_equal_two_times_cooperations( - self + self, ): opponent_plays = [C] * 8 + [D] * 4 + [C] actions = [(C, C)] * 8 + [(C, D)] * 4 + [(D, C)] diff --git a/axelrod/tests/strategies/test_evolvable_player.py b/axelrod/tests/strategies/test_evolvable_player.py index 6ed840b0b..ea0fb77fc 100644 --- a/axelrod/tests/strategies/test_evolvable_player.py +++ b/axelrod/tests/strategies/test_evolvable_player.py @@ -3,7 +3,11 @@ import axelrod as axl from axelrod.action import Action -from axelrod.evolvable_player import copy_lists, crossover_dictionaries, crossover_lists +from axelrod.evolvable_player import ( + copy_lists, + crossover_dictionaries, + crossover_lists, +) from .test_player import TestPlayer @@ -17,8 +21,7 @@ class PartialedClass(cls): seed = kwargs["seed"] except KeyError: kwargs["seed"] = 1 - __init__ = functools.partialmethod( - cls.__init__, **kwargs) + __init__ = functools.partialmethod(cls.__init__, **kwargs) return PartialedClass @@ -45,7 +48,9 @@ def mutate(self): def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") + raise TypeError( + "Crossover must be between the same player classes." + ) value = self.value + other.value return EvolvableTestOpponent(value) @@ -125,7 +130,11 @@ def test_crossover(self): players.append(player) player1, player2 = players crossed = player1.crossover(player2) - if player1 != crossed and player2 != crossed and crossed == crossed.clone(): + if ( + player1 != crossed + and player2 != crossed + and crossed == crossed.clone() + ): return # Should never get here unless a change breaks the test, so don't include in coverage. self.assertFalse(True) # pragma: no cover @@ -140,7 +149,9 @@ def test_serialization(self): """Serializing and deserializing should return the original player.""" player = self.player(seed=1) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters(serialized) + deserialized_player = player.__class__.deserialize_parameters( + serialized + ) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -149,7 +160,7 @@ def test_serialization_csv(self): player = self.player(seed=1) serialized = player.serialize_parameters() s = "0, 1, {}, 3".format(serialized) - s2 = s.split(',')[2] + s2 = s.split(",")[2] deserialized_player = player.__class__.deserialize_parameters(s2) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -178,7 +189,9 @@ def test_behavior(self): self.behavior_test(player, parent_player) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters(serialized) + deserialized_player = player.__class__.deserialize_parameters( + serialized + ) self.behavior_test(deserialized_player, parent_player) def test_seed_propagation(self): @@ -202,7 +215,6 @@ def test_seed_preservation(self): class TestUtilityFunctions(unittest.TestCase): - def test_copy_lists(self): l1 = [list(range(10)), list(range(20))] l2 = copy_lists(l1) @@ -221,12 +233,12 @@ def test_crossover_lists(self): self.assertEqual(crossed, list1[:1] + list2[1:]) def test_crossover_dictionaries(self): - dict1 = {'1': 1, '2': 2, '3': 3} - dict2 = {'1': 'a', '2': 'b', '3': 'c'} + dict1 = {"1": 1, "2": 2, "3": 3} + dict2 = {"1": "a", "2": "b", "3": "c"} rng = axl.RandomGenerator(seed=1) crossed = crossover_dictionaries(dict1, dict2, rng) - self.assertEqual(crossed, {'1': 1, '2': 'b', '3': 'c'}) + self.assertEqual(crossed, {"1": 1, "2": "b", "3": "c"}) rng = axl.RandomGenerator(seed=2) crossed = crossover_dictionaries(dict1, dict2, rng) diff --git a/axelrod/tests/strategies/test_finite_state_machines.py b/axelrod/tests/strategies/test_finite_state_machines.py index f3525b0e1..f7eab3029 100644 --- a/axelrod/tests/strategies/test_finite_state_machines.py +++ b/axelrod/tests/strategies/test_finite_state_machines.py @@ -2,7 +2,9 @@ import unittest import axelrod as axl -from axelrod.compute_finite_state_machine_memory import get_memory_from_transitions +from axelrod.compute_finite_state_machine_memory import ( + get_memory_from_transitions, +) from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.finite_state_machines import ( EvolvableFSMPlayer, @@ -46,8 +48,15 @@ def test__eq__false_by_state(self): self.assertFalse(new_two_state.__eq__(self.two_state)) def test__eq__false_by_transition(self): - different_transitions = ((1, C, 0, D), (1, D, 0, D), (0, C, 1, D), (0, D, 1, C)) - new_two_state = SimpleFSM(transitions=different_transitions, initial_state=1) + different_transitions = ( + (1, C, 0, D), + (1, D, 0, D), + (0, C, 1, D), + (0, D, 1, C), + ) + new_two_state = SimpleFSM( + transitions=different_transitions, initial_state=1 + ) self.assertFalse(new_two_state.__eq__(self.two_state)) @@ -91,7 +100,9 @@ def test_state_setter_raises_error_for_bad_input(self): with self.assertRaises(ValueError) as cm: self.two_state.state = 5 error_msg = cm.exception.args[0] - self.assertEqual(error_msg, "state: 5 does not have values for both C and D") + self.assertEqual( + error_msg, "state: 5 does not have values for both C and D" + ) class TestSampleFSMPlayer(TestPlayer): @@ -157,7 +168,12 @@ def test_wsls(self): """Tests that the player defined by the table for TFT is in fact WSLS (also known as Pavlov.""" wsls_init_kwargs = { - "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), + "transitions": ( + (1, C, 1, C), + (1, D, 2, D), + (2, C, 2, D), + (2, D, 1, C), + ), "initial_state": 1, "initial_action": C, } @@ -272,7 +288,9 @@ def test_strategy(self): opponent = axl.MockPlayer([D, D, C, C, D]) actions = [(C, D), (C, D), (C, C), (D, C), (C, D)] self.versus_test( - opponent, expected_actions=actions, init_kwargs={"transitions": transitions} + opponent, + expected_actions=actions, + init_kwargs={"transitions": transitions}, ) def test_memory(self): @@ -280,7 +298,10 @@ def test_memory(self): Test the memory depth using implemented algorithm """ transitions = self.player().fsm._state_transitions - self.assertEqual(get_memory_from_transitions(transitions), self.expected_classifier["memory_depth"]) + self.assertEqual( + get_memory_from_transitions(transitions), + self.expected_classifier["memory_depth"], + ) class TestFortress3(TestFSMPlayer): @@ -311,7 +332,15 @@ def test_strategy(self): state_and_actions = [(1, C), (1, D), (2, C), (1, C)] self.transitions_test(state_and_actions) - state_and_actions = [(1, D), (2, D), (3, C), (3, C), (3, C), (3, D), (1, C)] * 2 + state_and_actions = [ + (1, D), + (2, D), + (3, C), + (3, C), + (3, C), + (3, D), + (1, C), + ] * 2 self.transitions_test(state_and_actions) @unittest.expectedFailure @@ -415,11 +444,15 @@ def test_strategy(self): ] + [(7, D), (7, C), (8, C), (8, D), (6, D)] * 3 self.transitions_test(state_and_actions) - state_and_actions = [(0, D), (1, C), (2, D), (3, C), (5, D), (3, C), (5, C)] + [ - (7, C), - (8, D), - (6, C), - ] * 5 + state_and_actions = [ + (0, D), + (1, C), + (2, D), + (3, C), + (5, D), + (3, C), + (5, C), + ] + [(7, C), (8, D), (6, C),] * 5 self.transitions_test(state_and_actions) state_and_actions = ( @@ -520,7 +553,9 @@ class TestRipoff(TestFSMPlayer): """ def test_strategy(self): - state_and_actions = [(1, C), (2, C)] * 3 + [(1, D)] + [(3, C), (3, D)] * 5 + state_and_actions = ( + [(1, C), (2, C)] * 3 + [(1, D)] + [(3, C), (3, D)] * 5 + ) self.transitions_test(state_and_actions) state_and_actions = [(1, C), (2, D)] + [(3, D)] * 5 @@ -621,7 +656,11 @@ class TestSolutionB1(TestFSMPlayer): def test_strategy(self): state_and_actions = ( - [(1, D)] * 3 + [(1, C)] + [(2, C)] * 3 + [(2, D)] + [(3, C), (3, D)] * 3 + [(1, D)] * 3 + + [(1, C)] + + [(2, C)] * 3 + + [(2, D)] + + [(3, C), (3, D)] * 3 ) self.transitions_test(state_and_actions) @@ -802,7 +841,9 @@ class TestEvolvedFSM16(TestFSMPlayer): def test_strategy(self): # finished: 0, - state_and_actions = [(0, C)] * 3 + [(0, D)] + [(12, D), (11, D), (5, C)] * 3 + state_and_actions = ( + [(0, C)] * 3 + [(0, D)] + [(12, D), (11, D), (5, C)] * 3 + ) self.transitions_test(state_and_actions) # finished: 0, 5, 10 @@ -849,7 +890,13 @@ def test_strategy(self): self.transitions_test(state_and_actions) # finished: 0, 1, 2, 3, 5, 10, 11, 12, 13, 14, 15 - state_and_actions = to_state_seven + [(7, D), (1, D), (6, C), (5, D), (10, C)] + state_and_actions = to_state_seven + [ + (7, D), + (1, D), + (6, C), + (5, D), + (10, C), + ] self.transitions_test(state_and_actions) # finished: 0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15 @@ -979,7 +1026,12 @@ def test_strategy(self): # finished 0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 13, 15 to_state_eleven = [(0, D), (3, C), (10, D), (1, D), (15, D)] - state_and_actions = to_state_eleven + [(11, C), (14, C), (3, C), (10, D)] + state_and_actions = to_state_eleven + [ + (11, C), + (14, C), + (3, C), + (10, D), + ] self.transitions_test(state_and_actions) # finished 0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 15 @@ -1058,7 +1110,12 @@ def test_normalized_parameters(self): self.assertRaises( InsufficientParametersError, self.player_class, - transitions=[[0, C, 1, D], [0, D, 0, D], [1, C, 1, C], [1, D, 1, D]], + transitions=[ + [0, C, 1, D], + [0, D, 0, D], + [1, C, 1, C], + [1, D, 1, D], + ], seed=1, # To prevent exception from unset seed. ) @@ -1083,7 +1140,9 @@ def test_vector_to_instance(self): self.assertIsInstance(player, axl.EvolvableFSMPlayer) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters(serialized) + deserialized_player = player.__class__.deserialize_parameters( + serialized + ) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -1098,7 +1157,8 @@ def test_mutate(self): """Test to trigger random lines in mutate""" for seed in [18, 22]: player = axl.EvolvableFSMPlayer( - num_states=4, mutation_probability=0.5, seed=seed) + num_states=4, mutation_probability=0.5, seed=seed + ) player.mutate() @@ -1126,7 +1186,7 @@ class TestEvolvableFSMPlayer4(TestEvolvablePlayer): init_parameters = { "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), "initial_state": 1, - "initial_action": C + "initial_action": C, } @@ -1135,7 +1195,8 @@ class TestEvolvableFSMPlayer4(TestEvolvablePlayer): EvolvableFSMPlayer, transitions=((1, C, 1, C), (1, D, 1, D)), initial_state=1, - initial_action=C) + initial_action=C, +) class EvolvableFSMAsFSM(TestFSMPlayer): diff --git a/axelrod/tests/strategies/test_forgiver.py b/axelrod/tests/strategies/test_forgiver.py index e83b86d76..43754f5b0 100644 --- a/axelrod/tests/strategies/test_forgiver.py +++ b/axelrod/tests/strategies/test_forgiver.py @@ -25,15 +25,19 @@ def test_strategy(self): # If opponent has defected more than 10 percent of the time, defect. self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 10) - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10 + ) def test_cooperates_if_opponent_defections_is_ten_pct_and_defects_if_opponent_defections_gt_ten_pct( - self + self, ): final_action_lowers_defections_to_ten_percent = [D] + [C] * 9 expected = [(C, D)] + [(D, C)] * 9 self.versus_test( - axl.MockPlayer(actions=final_action_lowers_defections_to_ten_percent), + axl.MockPlayer( + actions=final_action_lowers_defections_to_ten_percent + ), expected_actions=expected * 5, ) @@ -62,7 +66,9 @@ class TestForgivingTitForTat(TestPlayer): def test_strategy(self): self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5 + ) self.versus_test( axl.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5 ) @@ -77,7 +83,11 @@ def test_never_defects_if_opponent_defections_le_ten_percent(self): def test_plays_tit_for_tat_while_defections_gt_ten_percent(self): before_tft = (18 * [C] + [D]) * 3 + [D, D, D] - only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [(C, D), (C, D), (C, D)] + only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [ + (C, D), + (C, D), + (C, D), + ] self.versus_test( axl.MockPlayer(actions=before_tft), expected_actions=only_cooperates ) diff --git a/axelrod/tests/strategies/test_gambler.py b/axelrod/tests/strategies/test_gambler.py index cc58083e3..e9ab3b140 100755 --- a/axelrod/tests/strategies/test_gambler.py +++ b/axelrod/tests/strategies/test_gambler.py @@ -415,9 +415,7 @@ def test_vs_DCDDC2(self): (D, D), # different than above test ] self.versus_test( - axl.MockPlayer(opponent_actions), - expected_actions=expected, - seed=5, + axl.MockPlayer(opponent_actions), expected_actions=expected, seed=5, ) def test_vs_DCDDC3(self): @@ -520,14 +518,17 @@ def test_vs_alternator2(self): class TestEvolvableGambler(unittest.TestCase): - def test_receive_vector(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1) + parameters=(plays, op_plays, op_start_plays), seed=1 + ) - self.assertRaises(AttributeError, axl.EvolvableGambler.__getattribute__, - *[player, 'vector']) + self.assertRaises( + AttributeError, + axl.EvolvableGambler.__getattribute__, + *[player, "vector"] + ) vector = [random.random() for _ in range(8)] player.receive_vector(vector) @@ -536,19 +537,24 @@ def test_receive_vector(self): def test_vector_to_instance(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1) + parameters=(plays, op_plays, op_start_plays), seed=1 + ) vector = [random.random() for _ in range(8)] player.receive_vector(vector) - keys = create_lookup_table_keys(player_depth=plays, op_depth=op_plays, - op_openings_depth=op_start_plays) + keys = create_lookup_table_keys( + player_depth=plays, + op_depth=op_plays, + op_openings_depth=op_start_plays, + ) action_dict = dict(zip(keys, vector)) self.assertEqual(player._lookup.dictionary, action_dict) def test_create_vector_bounds(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1) + parameters=(plays, op_plays, op_start_plays), seed=1 + ) lb, ub = player.create_vector_bounds() self.assertIsInstance(lb, list) self.assertIsInstance(ub, list) @@ -566,8 +572,7 @@ class TestEvolvableGambler2(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (1, 1, 1), - "initial_actions": (C,)} + init_parameters = {"parameters": (1, 1, 1), "initial_actions": (C,)} class TestEvolvableGambler3(TestEvolvablePlayer): @@ -575,8 +580,7 @@ class TestEvolvableGambler3(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (3, 2, 1), - "initial_actions": (C, C, C,)} + init_parameters = {"parameters": (3, 2, 1), "initial_actions": (C, C, C,)} class TestEvolvableGambler4(TestEvolvablePlayer): @@ -584,9 +588,11 @@ class TestEvolvableGambler4(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (2, 2, 2), - "pattern": [random.random() for _ in range(64)], - "initial_actions": (C, C,)} + init_parameters = { + "parameters": (2, 2, 2), + "pattern": [random.random() for _ in range(64)], + "initial_actions": (C, C,), + } # Substitute EvolvableHMMPlayer as a regular HMMPlayer. @@ -594,7 +600,7 @@ class TestEvolvableGambler4(TestEvolvablePlayer): axl.EvolvableGambler, pattern=tables[("PSO Gambler 2_2_2", 2, 2, 2)], parameters=(2, 2, 2), - initial_actions=(C, C,) + initial_actions=(C, C,), ) diff --git a/axelrod/tests/strategies/test_geller.py b/axelrod/tests/strategies/test_geller.py index 38491fe08..0cb60d7c0 100644 --- a/axelrod/tests/strategies/test_geller.py +++ b/axelrod/tests/strategies/test_geller.py @@ -43,7 +43,9 @@ def test_strategy(self): """Should cooperate against cooperators and defect against defectors.""" self.versus_test(axl.Defector(), expected_actions=[(D, D)] * 5) self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test(axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5) + self.versus_test( + axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5 + ) def test_strategy_against_lookerup_players(self): """ @@ -64,7 +66,9 @@ def test_returns_foil_inspection_strategy_of_opponent(self): seed=2, ) - self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)], seed=3) + self.versus_test( + axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)], seed=3 + ) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)], seed=1 @@ -91,10 +95,13 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axl.GellerDefector(), expected_actions=[(D, C), (D, C), (D, C), (D, C)] + axl.GellerDefector(), + expected_actions=[(D, C), (D, C), (D, C), (D, C)], ) - self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test( + axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)] + ) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] @@ -122,10 +129,13 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axl.GellerDefector(), expected_actions=[(D, D), (D, D), (D, D), (D, D)] + axl.GellerDefector(), + expected_actions=[(D, D), (D, D), (D, D), (D, D)], ) - self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test( + axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)] + ) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] diff --git a/axelrod/tests/strategies/test_gobymajority.py b/axelrod/tests/strategies/test_gobymajority.py index 6cd553880..88c5c555a 100644 --- a/axelrod/tests/strategies/test_gobymajority.py +++ b/axelrod/tests/strategies/test_gobymajority.py @@ -38,7 +38,9 @@ def test_memory_depth_infinite_soft_is_false(self): + [(C, C)] ) opponent = axl.MockPlayer(actions=opponent_actions) - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_memory_depth_even_soft_is_false(self): memory_depth = 4 @@ -46,7 +48,9 @@ def test_memory_depth_even_soft_is_false(self): if self.default_soft: init_kwargs["soft"] = False - opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer( + actions=[C] * memory_depth + [D] * memory_depth + ) actions = ( [(D, C)] + [(C, C)] * 3 @@ -55,7 +59,9 @@ def test_memory_depth_even_soft_is_false(self): + [(D, C)] * 3 + [(C, C)] ) - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_memory_depth_odd(self): memory_depth = 5 @@ -64,7 +70,9 @@ def test_memory_depth_odd(self): first_action = [(C, C)] else: first_action = [(D, C)] - opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer( + actions=[C] * memory_depth + [D] * memory_depth + ) actions = ( first_action + [(C, C)] * 4 @@ -73,7 +81,9 @@ def test_memory_depth_odd(self): + [(D, C)] * 3 + [(C, C)] * 2 ) - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_default_values(self): player = self.player() @@ -90,7 +100,11 @@ class TestGoByMajority(TestHardGoByMajority): def test_memory_depth_infinite_soft_is_true(self): opponent_actions = [C] * 50 + [D] * 100 + [C] * 52 actions = ( - [(C, C)] * 50 + [(C, D)] * 51 + [(D, D)] * 49 + [(D, C)] * 50 + [(C, C)] * 2 + [(C, C)] * 50 + + [(C, D)] * 51 + + [(D, D)] * 49 + + [(D, C)] * 50 + + [(C, C)] * 2 ) opponent = axl.MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions) @@ -100,8 +114,12 @@ def test_memory_depth_even_soft_is_true(self): init_kwargs = {"memory_depth": memory_depth} opponent = axl.MockPlayer([C] * memory_depth + [D] * memory_depth) - actions = [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + actions = ( + [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 + ) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_name(self): player = self.player(soft=True) @@ -161,7 +179,9 @@ def test_strategy(self): else: cooperations = int(memory_depth * 1.5) - 1 defections = len(opponent_actions) - cooperations - 1 - player_actions = first_player_action + [C] * cooperations + [D] * defections + player_actions = ( + first_player_action + [C] * cooperations + [D] * defections + ) actions = list(zip(player_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_grudger.py b/axelrod/tests/strategies/test_grudger.py index 79194b5d8..3ec1d6ac4 100644 --- a/axelrod/tests/strategies/test_grudger.py +++ b/axelrod/tests/strategies/test_grudger.py @@ -12,7 +12,7 @@ class TestGrudger(TestPlayer): name = "Grudger" player = axl.Grudger expected_classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "makes_use_of": set(), "long_run_time": False, @@ -258,11 +258,31 @@ def test_strategy(self): """Test strategy with multiple initial parameters""" # Testing default parameters of n=1, d=4, c=2 (same as Soft Grudger) - actions = [(C, D), (D, D), (D, C), (D, C), (D, D), (C, D), (C, C), (C, C)] - self.versus_test(axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions) + actions = [ + (C, D), + (D, D), + (D, C), + (D, C), + (D, D), + (C, D), + (C, C), + (C, C), + ] + self.versus_test( + axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions + ) # Testing n=2, d=4, c=2 - actions = [(C, D), (C, D), (D, C), (D, C), (D, D), (D, D), (C, C), (C, C)] + actions = [ + (C, D), + (C, D), + (D, C), + (D, C), + (D, D), + (D, D), + (C, C), + (C, C), + ] self.versus_test( axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions, @@ -270,7 +290,16 @@ def test_strategy(self): ) # Testing n=1, d=1, c=1 - actions = [(C, D), (D, D), (C, C), (C, C), (C, D), (D, D), (C, C), (C, C)] + actions = [ + (C, D), + (D, D), + (C, C), + (C, C), + (C, D), + (D, D), + (C, C), + (C, C), + ] self.versus_test( axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions, diff --git a/axelrod/tests/strategies/test_grumpy.py b/axelrod/tests/strategies/test_grumpy.py index 1fba6bbdd..fa0e5b327 100644 --- a/axelrod/tests/strategies/test_grumpy.py +++ b/axelrod/tests/strategies/test_grumpy.py @@ -46,32 +46,42 @@ def test_starting_state(self): actions = ([(C, D)] * 11 + [(D, C)] * 22 + [(C, D)] * 11) * 3 init_kwargs = {"starting_state": "Nice"} - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) opponent = axl.MockPlayer(actions=opponent_actions) grumpy_starting = [(D, D)] * 11 + [(D, C)] * 22 + [(C, D)] * 11 actions = grumpy_starting + actions init_kwargs = {"starting_state": "Grumpy"} - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_thresholds(self): init_kwargs = {"grumpy_threshold": 3, "nice_threshold": -2} opponent_actions = [D] * 4 + [C] * 7 + [D] * 3 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 4 + [(D, C)] * 7 + [(C, D)] * 3) * 3 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) init_kwargs = {"grumpy_threshold": 0, "nice_threshold": -2} opponent_actions = [D] * 1 + [C] * 4 + [D] * 3 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 1 + [(D, C)] * 4 + [(C, D)] * 3) * 3 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) init_kwargs = {"grumpy_threshold": 3, "nice_threshold": 0} opponent_actions = [D] * 4 + [C] * 5 + [D] * 1 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 4 + [(D, C)] * 5 + [(C, D)] * 1) * 3 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_reset_state_with_non_default_init(self): player = axl.Grumpy(starting_state="Grumpy") diff --git a/axelrod/tests/strategies/test_hmm.py b/axelrod/tests/strategies/test_hmm.py index e60d4e3dd..beffae2c5 100644 --- a/axelrod/tests/strategies/test_hmm.py +++ b/axelrod/tests/strategies/test_hmm.py @@ -193,17 +193,23 @@ class TestEvolvedHMM5(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) class TestEvolvedHMM5vsCooperator(TestMatch): def test_rounds(self): - self.versus_test(axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5, seed=3) + self.versus_test( + axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5, seed=3 + ) class TestEvolvedHMM5vsDefector(TestMatch): def test_rounds(self): - self.versus_test(axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D], seed=5) + self.versus_test( + axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D], seed=5 + ) class TestEvolvableHMMPlayer(unittest.TestCase): @@ -218,9 +224,7 @@ def test_normalized_parameters(self): initial_action = C self.assertRaises( - InsufficientParametersError, - self.player_class, - seed=1 + InsufficientParametersError, self.player_class, seed=1 ) self.assertRaises( InsufficientParametersError, @@ -229,7 +233,7 @@ def test_normalized_parameters(self): transitions_D=transitions_D, emission_probabilities=emission_probabilities, initial_state=None, - seed=1 + seed=1, ) self.assertRaises( InsufficientParametersError, @@ -238,14 +242,14 @@ def test_normalized_parameters(self): transitions_D=transitions_D, emission_probabilities=emission_probabilities, initial_action=None, - seed=1 + seed=1, ) self.assertRaises( InsufficientParametersError, self.player_class, initial_state=initial_state, initial_action=initial_action, - seed=1 + seed=1, ) def test_vector_to_instance(self): diff --git a/axelrod/tests/strategies/test_human.py b/axelrod/tests/strategies/test_human.py index e0e2ca811..a372b25ca 100644 --- a/axelrod/tests/strategies/test_human.py +++ b/axelrod/tests/strategies/test_human.py @@ -27,7 +27,9 @@ def test_validator(self): ActionValidator().validate(test_document) test_document = TestDocument("E") - self.assertRaises(ValidationError, ActionValidator().validate, test_document) + self.assertRaises( + ValidationError, ActionValidator().validate, test_document + ) class TestHumanClass(TestPlayer): diff --git a/axelrod/tests/strategies/test_inverse.py b/axelrod/tests/strategies/test_inverse.py index 95ac95033..3d89e11dd 100644 --- a/axelrod/tests/strategies/test_inverse.py +++ b/axelrod/tests/strategies/test_inverse.py @@ -28,10 +28,22 @@ def test_strategy(self): # Tests that if opponent has played all D then player chooses D, # regardless of the random seed. - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9, seed=None) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9, seed=None + ) - expected_actions = [(C, D), (D, C), (D, C), (C, D), (D, C), (C, C), - (D, C), (C, C), (C, D), (D, D)] + expected_actions = [ + (C, D), + (D, C), + (D, C), + (C, D), + (D, C), + (C, C), + (D, C), + (C, C), + (C, D), + (D, D), + ] self.versus_test( axl.MockPlayer(actions=[a[1] for a in expected_actions]), expected_actions=expected_actions, diff --git a/axelrod/tests/strategies/test_lookerup.py b/axelrod/tests/strategies/test_lookerup.py index c48474388..b3fdf6c9c 100755 --- a/axelrod/tests/strategies/test_lookerup.py +++ b/axelrod/tests/strategies/test_lookerup.py @@ -20,6 +20,7 @@ C, D = axl.Action.C, axl.Action.D random = axl.RandomGenerator() + class TestLookupTable(unittest.TestCase): lookup_dict = { ((C, C), (C,), ()): C, @@ -69,7 +70,9 @@ def test_from_pattern(self): table = LookupTable.from_pattern( pattern, player_depth=2, op_depth=1, op_openings_depth=0 ) - self.assertEqual(table.dictionary, make_keys_into_plays(self.lookup_dict)) + self.assertEqual( + table.dictionary, make_keys_into_plays(self.lookup_dict) + ) def test_from_pattern_raises_error_pattern_len_ne_dict_size(self): too_big = (C,) * 17 @@ -151,7 +154,9 @@ def test_plays_equals_tuple(self): self.assertEqual(Plays(1, 2, 3), (1, 2, 3)) def test_plays_assign_values(self): - self.assertEqual(Plays(op_plays=2, self_plays=1, op_openings=3), Plays(1, 2, 3)) + self.assertEqual( + Plays(op_plays=2, self_plays=1, op_openings=3), Plays(1, 2, 3) + ) def test_make_keys_into_plays(self): old = {((C, D), (C,), ()): 1, ((D, D), (D,), ()): 2} @@ -307,7 +312,9 @@ def test_set_memory_depth(self): self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_3), 3) mem_depth_inf = axl.LookerUp(pattern="CC", parameters=Plays(0, 0, 1)) - self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_inf), float("inf")) + self.assertEqual( + axl.Classifiers["memory_depth"](mem_depth_inf), float("inf") + ) def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D)] @@ -375,10 +382,13 @@ def test_opponent_starting_moves_table(self): def test_lookup_table_display(self): player = axl.LookerUp( - pattern="CCCC", parameters=Plays(self_plays=2, op_plays=0, op_openings=0) + pattern="CCCC", + parameters=Plays(self_plays=2, op_plays=0, op_openings=0), ) self.assertEqual( - player.lookup_table_display(("self_plays", "op_plays", "op_openings")), + player.lookup_table_display( + ("self_plays", "op_plays", "op_openings") + ), ( "self_plays | op_plays |op_openings\n" + " C, C , , : C,\n" @@ -420,12 +430,16 @@ def test_new_data(self): def test_vs_initial_defector(self): opponent = [D, C, C, D, D, C] expected = [(C, D), (D, C), (C, C), (D, D), (D, D), (D, C)] - self.versus_test(axl.MockPlayer(actions=opponent), expected_actions=expected) + self.versus_test( + axl.MockPlayer(actions=opponent), expected_actions=expected + ) def test_vs_initial_cooperator(self): opponent = [C, D, D, C, C, D] expected = [(C, C), (C, D), (D, D), (D, C), (D, C), (D, D)] - self.versus_test(axl.MockPlayer(actions=opponent), expected_actions=expected) + self.versus_test( + axl.MockPlayer(actions=opponent), expected_actions=expected + ) class TestEvolvedLookerUp2_2_2(TestPlayer): @@ -521,7 +535,14 @@ def test_vs_initial_defector(self): def test_vs_initial_d_c(self): opponent_actions = [D, C] + [C, D] * 3 - expected = [(C, D), (C, C)] + [(D, C), (C, D), (C, C), (D, D), (C, C), (C, D)] + expected = [(C, D), (C, C)] + [ + (D, C), + (C, D), + (C, C), + (D, D), + (C, C), + (C, D), + ] self.versus_test( axl.MockPlayer(actions=opponent_actions), expected_actions=expected ) @@ -604,7 +625,9 @@ def test_strategy(self): vs_alternator = [(D, C), (C, D)] + [(D, C), (D, D)] * 5 self.versus_test(axl.Alternator(), expected_actions=vs_alternator) - self.versus_test(axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10) + self.versus_test( + axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10 + ) self.versus_test( axl.Defector(), expected_actions=([(D, D), (C, D)] + [(D, D)] * 10) @@ -616,7 +639,11 @@ def test_convert_key(self): opponent_starting_plays = "" player_last_plays = "CC" opponent_last_plays = "D" - old_key = (opponent_starting_plays, player_last_plays, opponent_last_plays) + old_key = ( + opponent_starting_plays, + player_last_plays, + opponent_last_plays, + ) new_key = Plays(self_plays=(C, C), op_plays=(D,), op_openings=()) @@ -665,22 +692,20 @@ def test_normalized_parameters(self): pattern = ("".join([random.choice(("C", "D")) for _ in range(8)]),) self.assertRaises( - InsufficientParametersError, - self.player_class, - seed=1 + InsufficientParametersError, self.player_class, seed=1 ) self.assertRaises( InsufficientParametersError, self.player_class, pattern=pattern, initial_actions=initial_actions, - seed=1 + seed=1, ) self.assertRaises( InsufficientParametersError, self.player_class, lookup_dict=lookup_dict, - seed=1 + seed=1, ) diff --git a/axelrod/tests/strategies/test_memoryone.py b/axelrod/tests/strategies/test_memoryone.py index 01e720618..f1e6513e9 100644 --- a/axelrod/tests/strategies/test_memoryone.py +++ b/axelrod/tests/strategies/test_memoryone.py @@ -69,11 +69,15 @@ class TestGTFT(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) def test_strategy2(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) def test_four_vector(self): (R, P, S, T) = axl.Game().RPST() @@ -111,11 +115,15 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, D), (D, D), (D, D), (C, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=10 + ) def test_strategy3(self): actions = [(C, D), (D, D), (C, D), (D, D), (D, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=1 + ) class TestStochasticCooperator(TestPlayer): @@ -143,19 +151,27 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (C, C), (D, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=113) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=113 + ) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) def test_strategy3(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=5) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=5 + ) def test_strategy4(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=12) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=12 + ) class TestStochasticWSLS(TestPlayer): @@ -174,19 +190,27 @@ class TestStochasticWSLS(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (D, C), (D, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=50) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=50 + ) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=2 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (D, C), (D, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=23) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=23 + ) def test_four_vector(self): player = self.player() @@ -204,7 +228,8 @@ class TestMemoryOnePlayer(unittest.TestCase): def test_default_if_four_vector_not_set(self): player = MemoryOnePlayer() self.assertEqual( - player._four_vector, {(C, C): 1.0, (C, D): 0.0, (D, C): 0.0, (D, D): 1.0} + player._four_vector, + {(C, C): 1.0, (C, D): 0.0, (D, C): 0.0, (D, D): 1.0}, ) def test_exception_if_four_vector_not_set(self): @@ -246,11 +271,15 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) def test_strategy2(self): actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=5 + ) class TestALLCorALLD(TestPlayer): @@ -269,11 +298,15 @@ class TestALLCorALLD(TestPlayer): def test_strategy(self): actions = [(D, C)] * 10 - self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=0) + self.versus_test( + opponent=axl.Cooperator(), expected_actions=actions, seed=0 + ) def test_strategy2(self): actions = [(C, C)] * 10 - self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Cooperator(), expected_actions=actions, seed=1 + ) class TestGenericReactiveStrategy(unittest.TestCase): @@ -287,13 +320,16 @@ class TestGenericReactiveStrategy(unittest.TestCase): def test_four_vector(self): self.assertEqual( - self.p1._four_vector, {(C, D): 0.0, (D, C): 0.0, (C, C): 0.0, (D, D): 0.0} + self.p1._four_vector, + {(C, D): 0.0, (D, C): 0.0, (C, C): 0.0, (D, D): 0.0}, ) self.assertEqual( - self.p2._four_vector, {(C, D): 0.0, (D, C): 1.0, (C, C): 1.0, (D, D): 0.0} + self.p2._four_vector, + {(C, D): 0.0, (D, C): 1.0, (C, C): 1.0, (D, D): 0.0}, ) self.assertEqual( - self.p3._four_vector, {(C, D): 0.5, (D, C): 1.0, (C, C): 1.0, (D, D): 0.5} + self.p3._four_vector, + {(C, D): 0.5, (D, C): 1.0, (C, C): 1.0, (D, D): 0.5}, ) def test_subclass(self): @@ -304,6 +340,7 @@ def test_subclass(self): class TestMemoryOneAlternator(TestAlternator): """Alternator is equivalent to MemoryOnePlayer((0, 0, 1, 1), C)""" + name = "Generic Memory One Player: (0, 0, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 1, 1)) expected_classifier = { @@ -319,6 +356,7 @@ class TestMemoryOneAlternator(TestAlternator): class TestMemoryOneCooperator(TestCooperator): """Cooperator is equivalent to MemoryOnePlayer((1, 1, 1, 1), C)""" + name = "Generic Memory One Player: (1, 1, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 1, 1, 1)) expected_classifier = { @@ -334,6 +372,7 @@ class TestMemoryOneCooperator(TestCooperator): class TestMemoryOneDefector(TestDefector): """Defector is equivalent to MemoryOnePlayer((0, 0, 0, 0), D)""" + name = "Generic Memory One Player: (0, 0, 0, 0), D" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0), initial=D) expected_classifier = { @@ -349,6 +388,7 @@ class TestMemoryOneDefector(TestDefector): class TestMemoryOneTitForTat(TestTitForTat): """TitForTat is equivalent to MemoryOnePlayer((1, 0, 1, 0), C)""" + name = "Generic Memory One Player: (1, 0, 1, 0), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) expected_classifier = { @@ -364,6 +404,7 @@ class TestMemoryOneTitForTat(TestTitForTat): class TestMemoryOneWSLS(TestWinStayLoseShift): """WinStayLoseShift is equivalent to MemoryOnePlayer((1, 0, 0, 1), C)""" + name = "Generic Memory One Player: (1, 0, 0, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 0, 1)) expected_classifier = { diff --git a/axelrod/tests/strategies/test_memorytwo.py b/axelrod/tests/strategies/test_memorytwo.py index 339aef728..ee5265eac 100644 --- a/axelrod/tests/strategies/test_memorytwo.py +++ b/axelrod/tests/strategies/test_memorytwo.py @@ -80,9 +80,7 @@ def test_exception_if_probability_vector_outside_valid_values(self): class TestMemoryStochastic(TestPlayer): - name = ( - "Generic Memory Two Player" - ) + name = "Generic Memory Two Player" player = axl.MemoryTwoPlayer expected_classifier = { "memory_depth": 0, # Memory-two Sixteen-Vector @@ -156,7 +154,9 @@ def test_strategy(self): # tests states 3, 5 and 12 actions = [(C, D), (C, C), (D, C), (D, D), (D, D), (C, D)] - self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions) + self.versus_test( + opponent=axl.SuspiciousTitForTat(), expected_actions=actions + ) # tests state 1 actions = [(C, C), (C, C), (C, C), (C, C)] @@ -189,12 +189,15 @@ def test_strategy_mutually_cooperative(self): # tests states 1, 4 and 8 actions = [(C, D), (C, D), (D, D), (C, C), (C, C), (C, D)] self.versus_test( - opponent=axl.Cycler(["D", "D", "D", "C", "C"]), expected_actions=actions + opponent=axl.Cycler(["D", "D", "D", "C", "C"]), + expected_actions=actions, ) # tests states 3, 5 actions = [(C, D), (C, C), (D, C), (D, D), (C, D)] - self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions) + self.versus_test( + opponent=axl.SuspiciousTitForTat(), expected_actions=actions + ) class TestMEM2(TestPlayer): @@ -256,8 +259,11 @@ def test_strategy(self): class TestMemoryTwoCooperator(TestCooperator): """Cooperator is equivalent to MemoryTwoPlayer((1, 1, ..., 1), C)""" + name = "Generic Memory Two Player: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], (C, C)" - player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=[1] * 16, initial=(C, C)) + player = lambda x: axl.MemoryTwoPlayer( + sixteen_vector=[1] * 16, initial=(C, C) + ) expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -271,8 +277,11 @@ class TestMemoryTwoCooperator(TestCooperator): class TestMemoryTwoDefector(TestDefector): """Defector is equivalent to MemoryTwoPlayer((0, 0, ..., 0), D)""" + name = "Generic Memory Two Player: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], (D, D)" - player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=[0] * 16, initial=(D, D)) + player = lambda x: axl.MemoryTwoPlayer( + sixteen_vector=[0] * 16, initial=(D, D) + ) expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -292,8 +301,12 @@ def four_vector_to_sixteen_vector(four_vector): class TestMemoryTwoAlternator(TestAlternator): """Alternator is equivalent to MemoryTwoPlayer(0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1), C).""" + name = "Generic Memory Two Player: [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1], (C, D)" - player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=four_vector_to_sixteen_vector((0, 0, 1, 1)), initial=(C, D)) + player = lambda x: axl.MemoryTwoPlayer( + sixteen_vector=four_vector_to_sixteen_vector((0, 0, 1, 1)), + initial=(C, D), + ) expected_classifier = { "memory_depth": 1, "stochastic": False, diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 6ec22db6b..3cb2c8241 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -41,7 +41,9 @@ def classifier_test(self, expected_class_classifier=None): for t in player.team: try: - classifier["makes_use_of"].update(axl.Classifiers["make_use_of"](t)) + classifier["makes_use_of"].update( + axl.Classifiers["make_use_of"](t) + ) except KeyError: pass @@ -250,7 +252,6 @@ class TestNiceMetaWinnerEnsemble(TestMetaPlayer): "manipulates_state": False, } - def test_strategy(self): actions = [(C, C)] * 8 self.versus_test( @@ -370,7 +371,9 @@ def test_strategy(self): opponent = axl.MockPlayer([C] * 100 + [D]) actions = [(C, C)] * 100 + [(C, D), (D, C)] self.versus_test( - opponent=opponent, expected_actions=actions, init_kwargs={"team": team} + opponent=opponent, + expected_actions=actions, + init_kwargs={"team": team}, ) @@ -389,7 +392,9 @@ class TestMetaMajorityMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaMajorityFiniteMemory(TestMetaPlayer): @@ -407,7 +412,9 @@ class TestMetaMajorityFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) class TestMetaMajorityLongMemory(TestMetaPlayer): @@ -425,11 +432,15 @@ class TestMetaMajorityLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=0) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=0 + ) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaWinnerMemoryOne(TestMetaPlayer): @@ -447,7 +458,9 @@ class TestMetaWinnerMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaWinnerFiniteMemory(TestMetaPlayer): @@ -465,7 +478,9 @@ class TestMetaWinnerFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaWinnerLongMemory(TestMetaPlayer): @@ -483,7 +498,9 @@ class TestMetaWinnerLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=4) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=4 + ) class TestMetaWinnerDeterministic(TestMetaPlayer): @@ -519,7 +536,9 @@ class TestMetaWinnerStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaMixer(TestMetaPlayer): @@ -595,7 +614,12 @@ def test_strategy(self): ) team.append(axl.Defector) - distribution = [0.2, 0.5, 0.3, 0] # If add a defector but does not occur + distribution = [ + 0.2, + 0.5, + 0.3, + 0, + ] # If add a defector but does not occur self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, @@ -639,7 +663,9 @@ def classifier_test(self, expected_class_classifier=None): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=11) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=11 + ) class TestNMWEStochastic(TestMetaPlayer): @@ -657,7 +683,9 @@ class TestNMWEStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=16) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=16 + ) class TestNMWEFiniteMemory(TestMetaPlayer): @@ -675,7 +703,9 @@ class TestNMWEFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=7) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=7 + ) class TestNMWELongMemory(TestMetaPlayer): @@ -693,7 +723,9 @@ class TestNMWELongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=3) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=3 + ) class TestNMWEMemoryOne(TestMetaPlayer): @@ -711,7 +743,9 @@ class TestNMWEMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) class TestMemoryDecay(TestPlayer): @@ -820,5 +854,5 @@ def test_memory_alter_delete(self): opponent, expected_actions=actions, init_kwargs={"start_strategy": axl.Cooperator}, - seed=11 + seed=11, ) diff --git a/axelrod/tests/strategies/test_negation.py b/axelrod/tests/strategies/test_negation.py index 8c7542aaa..f74211b46 100644 --- a/axelrod/tests/strategies/test_negation.py +++ b/axelrod/tests/strategies/test_negation.py @@ -36,4 +36,6 @@ def test_strategy(self): opponent=axl.Cooperator(), expected_actions=actions, seed=1 ) actions = [(D, D), (C, D), (C, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=2 + ) diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index ec9fe051c..76c06e945 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -458,14 +458,22 @@ def test_reset_clone(self): clone = player.clone() self.assertEqual(player, clone) - @given(seed=integers(min_value=1, max_value=20000000), - turns=integers(min_value=5, max_value=10), - noise=integers(min_value=0, max_value=10)) + @given( + seed=integers(min_value=1, max_value=20000000), + turns=integers(min_value=5, max_value=10), + noise=integers(min_value=0, max_value=10), + ) @settings(max_examples=1, deadline=None) def test_clone_reproducible_play(self, seed, turns, noise): # Test that the cloned player produces identical play player = self.player() - if player.name in ["Darwin", "Human", "Mind Bender", "Mind Controller", "Mind Warper"]: + if player.name in [ + "Darwin", + "Human", + "Mind Bender", + "Mind Controller", + "Mind Warper", + ]: # Known exceptions return @@ -479,8 +487,15 @@ def test_clone_reproducible_play(self, seed, turns, noise): player_clone = player.clone() op = op.clone() op_clone = op.clone() - m1 = axl.Match((player, op), turns=turns, seed=seed, noise=noise/100.) - m2 = axl.Match((player_clone, op_clone), turns=turns, seed=seed, noise=noise/100.) + m1 = axl.Match( + (player, op), turns=turns, seed=seed, noise=noise / 100.0 + ) + m2 = axl.Match( + (player_clone, op_clone), + turns=turns, + seed=seed, + noise=noise / 100.0, + ) m1.play() m2.play() self.assertEqual(m1.result, m2.result) @@ -577,7 +592,7 @@ def versus_test( noise=noise, seed=seed, attrs=attrs, - match_attributes=match_attributes + match_attributes=match_attributes, ) def classifier_test(self, expected_class_classifier=None): @@ -610,13 +625,14 @@ def classifier_test(self, expected_class_classifier=None): axl.Classifiers[key](player), self.expected_classifier[key], msg="%s - Behaviour: %s != Expected Behaviour: %s" - % ( - key, - axl.Classifiers[key](player), - self.expected_classifier[key], - ), + % ( + key, + axl.Classifiers[key](player), + self.expected_classifier[key], + ), ) + class TestMatch(unittest.TestCase): """Test class for heads up play between two given players. Plays an axelrod match between the two players.""" @@ -631,7 +647,7 @@ def versus_test( noise=None, seed=None, match_attributes=None, - attrs=None + attrs=None, ): """Tests a sequence of outcomes for two given players.""" if len(expected_actions1) != len(expected_actions2): @@ -640,14 +656,22 @@ def versus_test( turns = len(expected_actions1) match = axl.Match( - (player1, player2), turns=turns, noise=noise, seed=seed, - match_attributes=match_attributes) + (player1, player2), + turns=turns, + noise=noise, + seed=seed, + match_attributes=match_attributes, + ) match.play() # Test expected sequence of plays from the match is as expected. - for i, (play, expected_play) in enumerate(zip(player1.history, expected_actions1)): + for i, (play, expected_play) in enumerate( + zip(player1.history, expected_actions1) + ): self.assertEqual((i, play), (i, expected_play)) - for i, (play, expected_play) in enumerate(zip(player2.history, expected_actions2)): + for i, (play, expected_play) in enumerate( + zip(player2.history, expected_actions2) + ): self.assertEqual((i, play), (i, expected_play)) # Test final player attributes are as expected diff --git a/axelrod/tests/strategies/test_prober.py b/axelrod/tests/strategies/test_prober.py index 229b3fe1e..7f9e3dd48 100644 --- a/axelrod/tests/strategies/test_prober.py +++ b/axelrod/tests/strategies/test_prober.py @@ -194,7 +194,28 @@ class TestProber4(TestPlayer): "manipulates_source": False, "manipulates_state": False, } - initial_sequence = [C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D] + initial_sequence = [ + C, + C, + D, + C, + D, + D, + D, + C, + C, + D, + C, + D, + C, + C, + D, + C, + D, + D, + C, + D, + ] def test_strategy(self): # Starts by playing CCDCDDDCCDCDCCDCDDCD. @@ -214,7 +235,9 @@ def test_strategy(self): for history in provocative_histories: opponent = axl.MockPlayer(history + [C] * 5) actions = list(zip(self.initial_sequence, history)) + [(D, C)] * 5 - self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) + self.versus_test( + opponent=opponent, expected_actions=actions, attrs=attrs + ) # Otherwise cooperates for 5 rounds and plays TfT afterwards unprovocative_histories = [ @@ -230,7 +253,9 @@ def test_strategy(self): opponent = axl.MockPlayer(history + [D] * 5 + [C, C]) actions = list(zip(self.initial_sequence, history)) + [(C, D)] * 5 actions += [(D, C), (C, C)] - self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) + self.versus_test( + opponent=opponent, expected_actions=actions, attrs=attrs + ) class TestHardProber(TestPlayer): diff --git a/axelrod/tests/strategies/test_qlearner.py b/axelrod/tests/strategies/test_qlearner.py index 948c8c02e..c44c64c77 100644 --- a/axelrod/tests/strategies/test_qlearner.py +++ b/axelrod/tests/strategies/test_qlearner.py @@ -41,7 +41,13 @@ def test_strategy(self): "CC2.0": {C: 2.7, D: 0}, "CCC3.0": {C: 0, D: 0}, }, - "Vs": {"": 0.9, "0.0": 2.7, "C1.0": 4.5, "CC2.0": 2.7, "CCC3.0": 0}, + "Vs": { + "": 0.9, + "0.0": 2.7, + "C1.0": 4.5, + "CC2.0": 2.7, + "CCC3.0": 0, + }, "prev_state": "CCC3.0", }, ) @@ -75,7 +81,13 @@ def test_strategy(self): "CC2.0": {C: 2.7, D: 0}, "CCC3.0": {C: 0, D: 0}, }, - "Vs": {"": 0.9, "0.0": 2.7, "C1.0": 4.5, "CC2.0": 2.7, "CCC3.0": 0}, + "Vs": { + "": 0.9, + "0.0": 2.7, + "C1.0": 4.5, + "CC2.0": 2.7, + "CCC3.0": 0, + }, "prev_state": "CCC3.0", }, ) @@ -109,7 +121,13 @@ def test_strategy(self): "DD0.0": {C: 0, D: 0}, "DDD0.0": {C: 0, D: 0}, }, - "Vs": {"": 0.1, "0.0": 0.0, "D0.0": 0.1, "DD0.0": 0.0, "DDD0.0": 0}, + "Vs": { + "": 0.1, + "0.0": 0.0, + "D0.0": 0.1, + "DD0.0": 0.0, + "DDD0.0": 0, + }, "prev_state": "DDD0.0", }, ) @@ -143,7 +161,13 @@ def test_strategy(self): "DD0.0": {C: 0, D: 0}, "DDD0.0": {C: 0, D: 0}, }, - "Vs": {"": 0.1, "0.0": 0.0, "D0.0": 0.1, "DD0.0": 0.0, "DDD0.0": 0}, + "Vs": { + "": 0.1, + "0.0": 0.0, + "D0.0": 0.1, + "DD0.0": 0.0, + "DDD0.0": 0, + }, "prev_state": "DDD0.0", }, ) diff --git a/axelrod/tests/strategies/test_rand.py b/axelrod/tests/strategies/test_rand.py index c9341ab87..03754a169 100644 --- a/axelrod/tests/strategies/test_rand.py +++ b/axelrod/tests/strategies/test_rand.py @@ -23,10 +23,14 @@ class TestRandom(TestPlayer): def test_deterministic(self): actions = [(D, C), (D, C), (D, C)] - self.versus_test(axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0}) + self.versus_test( + axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0} + ) actions = [(C, C), (C, C), (C, C)] - self.versus_test(axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1}) + self.versus_test( + axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1} + ) def test_stochastic_behavior1(self): """Test that strategy is randomly picked (not affected by history).""" diff --git a/axelrod/tests/strategies/test_retaliate.py b/axelrod/tests/strategies/test_retaliate.py index cd8660733..e3918f1ff 100644 --- a/axelrod/tests/strategies/test_retaliate.py +++ b/axelrod/tests/strategies/test_retaliate.py @@ -93,14 +93,18 @@ def test_strategy(self): opponent = axl.Cooperator() actions = [(C, C)] * 5 self.versus_test( - opponent=opponent, expected_actions=actions, attrs={"retaliating": False} + opponent=opponent, + expected_actions=actions, + attrs={"retaliating": False}, ) # Retaliate after a (C, D) round. opponent = axl.MockPlayer([C, C, C, D, C]) actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)] self.versus_test( - opponent=opponent, expected_actions=actions, attrs={"retaliating": True} + opponent=opponent, + expected_actions=actions, + attrs={"retaliating": True}, ) opponent = axl.Alternator() @@ -108,7 +112,9 @@ def test_strategy(self): # Count retaliations actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] self.versus_test( - opponent=opponent, expected_actions=actions, attrs={"retaliation_count": 3} + opponent=opponent, + expected_actions=actions, + attrs={"retaliation_count": 3}, ) opponent = axl.Alternator() diff --git a/axelrod/tests/strategies/test_revised_downing.py b/axelrod/tests/strategies/test_revised_downing.py index fa6897a85..8006e03ef 100644 --- a/axelrod/tests/strategies/test_revised_downing.py +++ b/axelrod/tests/strategies/test_revised_downing.py @@ -4,6 +4,7 @@ C, D = axl.Action.C, axl.Action.D + class TestRevisedDowning(TestPlayer): name = "Revised Downing" diff --git a/axelrod/tests/strategies/test_selfsteem.py b/axelrod/tests/strategies/test_selfsteem.py index d45730247..76b6e5db2 100644 --- a/axelrod/tests/strategies/test_selfsteem.py +++ b/axelrod/tests/strategies/test_selfsteem.py @@ -23,20 +23,33 @@ class TestSelfSteem(TestPlayer): def test_strategy1(self): # Check for f > 0.95, defect actions = ( - [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] + [(C, C)] * 4 + [(D, C)] + [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] + + [(C, C)] * 4 + + [(D, C)] ) self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) def test_strategy2(self): # Check for f < -0.95, cooperate - actions = [(D, C), (C, C), (D, C), (D, C), (C, C), (D, C), (C, C), (C, C)] + actions = [ + (D, C), + (C, C), + (D, C), + (D, C), + (C, C), + (D, C), + (C, C), + (C, C), + ] self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, seed=10 ) def test_strategy3(self): actions = [(D, D)] + [(D, D)] * 5 + [(D, D), (C, D), (C, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=10 + ) def test_strategy4(self): # Check for -0.3 < f < 0.3, random @@ -57,7 +70,9 @@ def test_strategy5(self): + [(D, D)] * 8 + [(C, D), (C, D), (D, D), (D, D), (D, D)] ) - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=32) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=32 + ) def test_strategy6(self): # Check for 0.95 > abs(f) > 0.3, follows TitForTat @@ -66,7 +81,9 @@ def test_strategy6(self): + [(C, D), (D, D), (C, D), (C, D), (D, D), (C, D)] + [(D, D)] * 5 ) - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=17) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=17 + ) def test_strategy7(self): actions = [ @@ -81,4 +98,6 @@ def test_strategy7(self): (C, C), (C, C), ] - self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.Cooperator(), expected_actions=actions, seed=10 + ) diff --git a/axelrod/tests/strategies/test_shortmem.py b/axelrod/tests/strategies/test_shortmem.py index 48dcd0138..525aea843 100644 --- a/axelrod/tests/strategies/test_shortmem.py +++ b/axelrod/tests/strategies/test_shortmem.py @@ -12,7 +12,7 @@ class TestShortMem(TestPlayer): name = "ShortMem" player = axl.ShortMem expected_classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "makes_use_of": set(), "inspects_source": False, @@ -44,14 +44,26 @@ def test_strategy(self): ) # If neither of the above conditions are met, apply TitForTat - actions = [(C, D)] * 5 + [(C, C)] * 6 + [(C, D), (D, D), (D, D), (D, C), (C, C)] + actions = ( + [(C, D)] * 5 + + [(C, C)] * 6 + + [(C, D), (D, D), (D, D), (D, C), (C, C)] + ) self.versus_test( - opponent=axl.MockPlayer(actions=[D] * 5 + [C] * 6 + [D, D, D, C, C]), + opponent=axl.MockPlayer( + actions=[D] * 5 + [C] * 6 + [D, D, D, C, C] + ), expected_actions=actions, ) - actions = [(C, C)] * 5 + [(C, D)] * 6 + [(D, C), (C, C), (C, C), (C, D), (D, D)] + actions = ( + [(C, C)] * 5 + + [(C, D)] * 6 + + [(D, C), (C, C), (C, C), (C, D), (D, D)] + ) self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 5 + [D] * 6 + [C, C, C, D, D]), + opponent=axl.MockPlayer( + actions=[C] * 5 + [D] * 6 + [C, C, C, D, D] + ), expected_actions=actions, ) diff --git a/axelrod/tests/strategies/test_stalker.py b/axelrod/tests/strategies/test_stalker.py index 59b6d1370..21e90dfde 100644 --- a/axelrod/tests/strategies/test_stalker.py +++ b/axelrod/tests/strategies/test_stalker.py @@ -28,12 +28,14 @@ def test_strategy(self): # wish_score < current_average_score < very_good_score actions = [(C, C)] * 7 + [(C, D), (C, D), (C, C), (C, C), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 2), expected_actions=actions + opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 2), + expected_actions=actions, ) actions = [(C, C)] * 7 + [(C, D), (C, C), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D]), expected_actions=actions + opponent=axl.MockPlayer(actions=[C] * 7 + [D]), + expected_actions=actions, ) # current_average_score > 2 @@ -43,7 +45,8 @@ def test_strategy(self): # 1 < current_average_score < 2 actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, D)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 5), expected_actions=actions + opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 5), + expected_actions=actions, ) def test_strategy2(self): @@ -75,7 +78,9 @@ def test_strategy4(self): # defect in last round actions = [(C, C)] * 199 + [(D, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, match_attributes={"length": 200} + axl.Cooperator(), + expected_actions=actions, + match_attributes={"length": 200}, ) # Force a defection before the end of the actual match which ensures diff --git a/axelrod/tests/strategies/test_titfortat.py b/axelrod/tests/strategies/test_titfortat.py index fb984ea4b..a13c06568 100644 --- a/axelrod/tests/strategies/test_titfortat.py +++ b/axelrod/tests/strategies/test_titfortat.py @@ -386,7 +386,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, C), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, @@ -394,7 +403,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, D), + (D, D), + (D, D), + ] self.versus_test( opponent, expected_actions=actions, @@ -471,7 +489,9 @@ def test_specific_set_of_results(self): to a memory one player that start by defecting and only cooperates if both players cooperated in the previous round. """ - mistrust_with_bug = axl.MemoryOnePlayer(initial=D, four_vector=(1, 0, 0, 0),) + mistrust_with_bug = axl.MemoryOnePlayer( + initial=D, four_vector=(1, 0, 0, 0), + ) players = [ self.player(), axl.TitForTat(), @@ -614,7 +634,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, C), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, @@ -627,7 +656,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, D), + (D, D), + (D, D), + ] self.versus_test( opponent, expected_actions=actions, @@ -669,7 +707,9 @@ def test_output_from_literature(self): ] turns = 1000 - tournament = axl.Tournament(players, turns=turns, repetitions=1, seed=75) + tournament = axl.Tournament( + players, turns=turns, repetitions=1, seed=75 + ) results = tournament.play(progress_bar=False) scores = [ round(average_score_per_turn * 1000, 1) @@ -714,8 +754,10 @@ def test_init(self): self.assertEqual(player._recorded_history, []) @given( - strategies=strategy_lists(strategies=deterministic_strategies, max_size=1), - turns=integers(min_value=1, max_value=20) + strategies=strategy_lists( + strategies=deterministic_strategies, max_size=1 + ), + turns=integers(min_value=1, max_value=20), ) @settings(deadline=None) def test_is_tit_for_tat_with_no_noise(self, strategies, turns): @@ -727,33 +769,59 @@ def test_is_tit_for_tat_with_no_noise(self, strategies, turns): self.assertEqual(m1.play(), m2.play()) def test_strategy_with_noise1(self): - self.versus_test(axl.Defector(), [(C, D)], turns=1, seed=9, - attrs={"_recorded_history": [C]}) + self.versus_test( + axl.Defector(), + [(C, D)], + turns=1, + seed=9, + attrs={"_recorded_history": [C]}, + ) def test_strategy_with_noise2(self): - self.versus_test(axl.Defector(), [(D, C)], turns=1, noise=0.5, seed=11, - attrs={"_recorded_history": [C]}) + self.versus_test( + axl.Defector(), + [(D, C)], + turns=1, + noise=0.5, + seed=11, + attrs={"_recorded_history": [C]}, + ) def test_strategy_with_noise3(self): # After noise: is contrite actions = list(zip([D, C], [C, D])) - self.versus_test(axl.Defector(), actions, turns=2, noise=0.5, seed=49, - attrs={"_recorded_history": [C, C], - "contrite": True}) + self.versus_test( + axl.Defector(), + actions, + turns=2, + noise=0.5, + seed=49, + attrs={"_recorded_history": [C, C], "contrite": True}, + ) def test_strategy_with_noise4(self): # Cooperates and no longer contrite actions = list(zip([D, C, C], [C, D, D])) - self.versus_test(axl.Defector(), actions, turns=3, noise=0.5, seed=49, - attrs={"_recorded_history": [C, C, C], - "contrite": False}) + self.versus_test( + axl.Defector(), + actions, + turns=3, + noise=0.5, + seed=49, + attrs={"_recorded_history": [C, C, C], "contrite": False}, + ) def test_strategy_with_noise5(self): # Defects and no longer contrite actions = list(zip([D, C, C, D], [C, D, D, D])) - self.versus_test(axl.Defector(), actions, turns=4, noise=0.5, seed=158, - attrs={"_recorded_history": [C, C, C, D], - "contrite": False}) + self.versus_test( + axl.Defector(), + actions, + turns=4, + noise=0.5, + seed=158, + attrs={"_recorded_history": [C, C, C, D], "contrite": False}, + ) class TestAdaptiveTitForTat(TestPlayer): @@ -900,7 +968,9 @@ class TestEugineNier(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (D, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False} + axl.Cooperator(), + expected_actions=actions, + attrs={"is_defector": False}, ) actions = [(C, C), (C, C), (C, C), (C, C)] @@ -914,7 +984,9 @@ def test_strategy(self): # Plays TfT and defects in last round actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)] self.versus_test( - axl.Alternator(), expected_actions=actions, attrs={"is_defector": False} + axl.Alternator(), + expected_actions=actions, + attrs={"is_defector": False}, ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] @@ -927,7 +999,16 @@ def test_strategy(self): # Becomes defector after 5 defections opponent = axl.MockPlayer(actions=[D, C, D, D, D, D, C, C]) - actions = [(C, D), (D, C), (C, D), (D, D), (D, D), (D, D), (D, C), (D, C)] + actions = [ + (C, D), + (D, C), + (C, D), + (D, D), + (D, D), + (D, D), + (D, C), + (D, C), + ] self.versus_test(opponent, expected_actions=actions) @@ -955,13 +1036,17 @@ def test_strategy(self): init_kwargs = {"N": 1, "M": 2} opponent = axl.MockPlayer(actions=[D, D, D, C, C]) actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) # TwoTitsForTat test_strategy init_kwargs = {"N": 2, "M": 1} opponent = axl.MockPlayer(actions=[D, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) actions = [(C, C), (C, C)] self.versus_test( opponent=axl.Cooperator(), @@ -970,7 +1055,9 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (D, D)] self.versus_test( - opponent=axl.Defector(), expected_actions=actions, init_kwargs=init_kwargs, + opponent=axl.Defector(), + expected_actions=actions, + init_kwargs=init_kwargs, ) # Cooperator test_strategy @@ -1017,6 +1104,7 @@ def test_varying_memory_depth(self): class Test1TitsFor1TatsIsTFT(TestTitForTat): """Tests that for N = 1 = M, all the TFT tests are passed.""" + name = "N Tit(s) For M Tat(s): 1, 1" player = lambda x: axl.NTitsForMTats(1, 1) expected_classifier = { @@ -1032,6 +1120,7 @@ class Test1TitsFor1TatsIsTFT(TestTitForTat): class Test1TitsFor2TatsIsTF2T(TestTitFor2Tats): """Tests that for N = 1, M = 2, all the TF2T tests are passed.""" + name = "N Tit(s) For M Tat(s): 1, 2" player = lambda x: axl.NTitsForMTats(1, 2) expected_classifier = { @@ -1047,6 +1136,7 @@ class Test1TitsFor2TatsIsTF2T(TestTitFor2Tats): class Test2TitsFor1TatsIs2TFT(TestTwoTitsForTat): """Tests that for N = 2, M = 1, all the 2TFT tests are passed.""" + name = "N Tit(s) For M Tat(s): 2, 1" player = lambda x: axl.NTitsForMTats(2, 1) expected_classifier = { @@ -1165,7 +1255,9 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (D, D)] - self.versus_test(axl.Defector(), expected_actions=actions, init_kwargs={"p": 0}) + self.versus_test( + axl.Defector(), expected_actions=actions, init_kwargs={"p": 0} + ) actions = [(C, C), (C, C), (D, C), (C, C)] self.versus_test( @@ -1173,7 +1265,9 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (C, D), (D, D)] - self.versus_test(axl.Defector(), expected_actions=actions, init_kwargs={"p": 1}) + self.versus_test( + axl.Defector(), expected_actions=actions, init_kwargs={"p": 1} + ) actions = [(C, C), (C, C), (D, C), (C, C), (C, C), (C, C)] self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) diff --git a/axelrod/tests/strategies/test_worse_and_worse.py b/axelrod/tests/strategies/test_worse_and_worse.py index 1fd2d5ef8..fa87429ef 100644 --- a/axelrod/tests/strategies/test_worse_and_worse.py +++ b/axelrod/tests/strategies/test_worse_and_worse.py @@ -101,7 +101,8 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, C), (C, C), (C, D), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C, C, D, C]), expected_actions=actions + opponent=axl.MockPlayer(actions=[C, C, D, C]), + expected_actions=actions, ) def test_strategy3(self): diff --git a/axelrod/tests/strategies/test_zero_determinant.py b/axelrod/tests/strategies/test_zero_determinant.py index 75b98fb5f..2d9bbac5c 100644 --- a/axelrod/tests/strategies/test_zero_determinant.py +++ b/axelrod/tests/strategies/test_zero_determinant.py @@ -32,7 +32,12 @@ class TestZDExtortion(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 0.64, (C, D): 0.18, (D, C): 0.28, (D, D): 0} + expected_dictionary = { + (C, C): 0.64, + (C, D): 0.18, + (D, C): 0.28, + (D, D): 0, + } test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -43,7 +48,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=1 + ) class TestZDExtort2(TestPlayer): @@ -61,7 +68,12 @@ class TestZDExtort2(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 8 / 9, (C, D): 0.5, (D, C): 1 / 3, (D, D): 0.0} + expected_dictionary = { + (C, C): 8 / 9, + (C, D): 0.5, + (D, C): 1 / 3, + (D, D): 0.0, + } test_four_vector(self, expected_dictionary) def test_receive_match_attributes(self): @@ -83,11 +95,15 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (D, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=10 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=7) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=7 + ) class TestZDExtort2v2(TestPlayer): @@ -121,7 +137,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=2 + ) class TestZDExtort3(TestPlayer): @@ -154,7 +172,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=2 + ) class TestZDExtort4(TestPlayer): @@ -172,7 +192,12 @@ class TestZDExtort4(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 11 / 17, (C, D): 0, (D, C): 8 / 17, (D, D): 0.0} + expected_dictionary = { + (C, C): 11 / 17, + (C, D): 0, + (D, C): 8 / 17, + (D, D): 0.0, + } test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -183,7 +208,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=10 + ) class TestZDGen2(TestPlayer): @@ -201,7 +228,12 @@ class TestZDGen2(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 1, (C, D): 9 / 16, (D, C): 1 / 2, (D, D): 1 / 8} + expected_dictionary = { + (C, C): 1, + (C, D): 9 / 16, + (D, C): 1 / 2, + (D, D): 1 / 8, + } test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -218,11 +250,15 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=10 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=3) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=3 + ) class TestZDGTFT2(TestPlayer): @@ -239,7 +275,12 @@ class TestZDGTFT2(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 1.0, (C, D): 1 / 8, (D, C): 1.0, (D, D): 0.25} + expected_dictionary = { + (C, C): 1.0, + (C, D): 1 / 8, + (D, C): 1.0, + (D, D): 0.25, + } test_four_vector(self, expected_dictionary) def test_receive_match_attributes(self): @@ -261,11 +302,15 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=4) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=4 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=23) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=23 + ) class TestZDMischief(TestPlayer): @@ -294,7 +339,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=4) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=4 + ) class TestZDSet2(TestPlayer): @@ -328,4 +375,6 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=12) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=12 + ) diff --git a/axelrod/tests/unit/test_classification.py b/axelrod/tests/unit/test_classification.py index c5d90df5f..c44d23516 100644 --- a/axelrod/tests/unit/test_classification.py +++ b/axelrod/tests/unit/test_classification.py @@ -50,7 +50,9 @@ def tearDown(self) -> None: def test_classifier_build(self): dirname = os.path.dirname(__file__) - test_path = os.path.join(dirname, "../../../test_outputs/classifier_test.yaml") + test_path = os.path.join( + dirname, "../../../test_outputs/classifier_test.yaml" + ) # Just returns the name of the player. For testing. name_classifier = Classifier[Text]("name", lambda player: player.name) @@ -66,7 +68,10 @@ def test_classifier_build(self): self.assertDictEqual( all_player_dicts, - {"Cooperator": {"name": "Cooperator"}, "Defector": {"name": "Defector"}}, + { + "Cooperator": {"name": "Cooperator"}, + "Defector": {"name": "Defector"}, + }, ) def test_singletonity_of_classifiers_class(self): @@ -91,7 +96,9 @@ def test_key_error_on_uknown_classifier(self): Classifiers["invalid_key"](axl.TitForTat) def test_will_lookup_key_in_dict(self): - self.assertEqual(Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1) + self.assertEqual( + Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1 + ) def test_will_lookup_key_for_classes_that_cant_init(self): with self.assertRaises(Exception) as exptn: @@ -114,7 +121,9 @@ def test_known_classifiers(self): for s in axl.all_strategies: s = s() - self.assertTrue(None not in [Classifiers[key](s) for key in known_keys]) + self.assertTrue( + None not in [Classifiers[key](s) for key in known_keys] + ) def test_multiple_instances(self): """Certain instances of classes of strategies will have different @@ -185,13 +194,19 @@ def test_obey_axelrod(self): ] for strategy in known_cheaters: - self.assertFalse(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) + self.assertFalse( + axl.Classifiers.obey_axelrod(strategy()), msg=strategy + ) for strategy in known_basic: - self.assertTrue(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) + self.assertTrue( + axl.Classifiers.obey_axelrod(strategy()), msg=strategy + ) for strategy in known_ordinary: - self.assertTrue(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) + self.assertTrue( + axl.Classifiers.obey_axelrod(strategy()), msg=strategy + ) def test_is_basic(self): """A test that verifies if the is_basic function works correctly""" @@ -294,7 +309,9 @@ def test_inclusion_of_strategy_lists(self): axl.basic_strategies, axl.long_run_time_strategies, ]: - self.assertTrue(str_reps(strategy_list).issubset(str_reps(strategies_set))) + self.assertTrue( + str_reps(strategy_list).issubset(str_reps(strategies_set)) + ) def test_long_run_strategies(self): long_run_time_strategies = [ @@ -319,7 +336,8 @@ def test_long_run_strategies(self): ] self.assertEqual( - str_reps(long_run_time_strategies), str_reps(axl.long_run_time_strategies) + str_reps(long_run_time_strategies), + str_reps(axl.long_run_time_strategies), ) self.assertTrue( all(map(Classifiers["long_run_time"], axl.long_run_time_strategies)) @@ -331,10 +349,13 @@ def test_short_run_strategies(self): ] self.assertEqual( - str_reps(short_run_time_strategies), str_reps(axl.short_run_time_strategies) + str_reps(short_run_time_strategies), + str_reps(axl.short_run_time_strategies), ) self.assertFalse( - any(map(Classifiers["long_run_time"], axl.short_run_time_strategies)) + any( + map(Classifiers["long_run_time"], axl.short_run_time_strategies) + ) ) def test_meta_inclusion(self): @@ -353,4 +374,6 @@ def test_demo_strategies(self): axl.Grudger, axl.Random, ] - self.assertTrue(str_reps(demo_strategies), str_reps(axl.demo_strategies)) + self.assertTrue( + str_reps(demo_strategies), str_reps(axl.demo_strategies) + ) diff --git a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py index 2b8ac5408..8d46c2ced 100644 --- a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py +++ b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py @@ -98,7 +98,7 @@ def test_three_state_tft(self): (1, C, 2, C), (1, D, 0, D), (2, C, 0, C), - (2, D, 2, D) + (2, D, 2, D), ) trans_dict = self.transitions_to_dict(transitions) diff --git a/axelrod/tests/unit/test_deterministic_cache.py b/axelrod/tests/unit/test_deterministic_cache.py index e2c1d1ae3..7d3929a2d 100644 --- a/axelrod/tests/unit/test_deterministic_cache.py +++ b/axelrod/tests/unit/test_deterministic_cache.py @@ -18,7 +18,9 @@ def setUpClass(cls): cls.test_save_file = axl_filename(save_path) load_path = pathlib.Path("test_outputs/test_cache_load.txt") cls.test_load_file = axl_filename(load_path) - test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]} + test_data_to_pickle = { + ("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)] + } cls.test_pickle = pickle.dumps(test_data_to_pickle) with open(cls.test_load_file, "wb") as f: diff --git a/axelrod/tests/unit/test_filters.py b/axelrod/tests/unit/test_filters.py index 88133a6b0..70e11ed6e 100644 --- a/axelrod/tests/unit/test_filters.py +++ b/axelrod/tests/unit/test_filters.py @@ -18,10 +18,14 @@ class TestStrategy(Player): def test_equality_filter(self): self.assertTrue( - passes_operator_filter(self.TestStrategy, "stochastic", True, operator.eq) + passes_operator_filter( + self.TestStrategy, "stochastic", True, operator.eq + ) ) self.assertFalse( - passes_operator_filter(self.TestStrategy, "stochastic", False, operator.eq) + passes_operator_filter( + self.TestStrategy, "stochastic", False, operator.eq + ) ) self.assertTrue( passes_operator_filter( @@ -70,7 +74,9 @@ def test_list_filter(self): passes_in_list_filter(self.TestStrategy, "makes_use_of", ["length"]) ) self.assertTrue( - passes_in_list_filter(self.TestStrategy, "makes_use_of", ["game", "length"]) + passes_in_list_filter( + self.TestStrategy, "makes_use_of", ["game", "length"] + ) ) self.assertFalse( passes_in_list_filter(self.TestStrategy, "makes_use_of", "test") @@ -119,11 +125,21 @@ def test_passes_filterset(self, smaller, larger): "min_memory_depth": smaller, } - self.assertTrue(passes_filterset(self.TestStrategy, full_passing_filterset_1)) - self.assertTrue(passes_filterset(self.TestStrategy, full_passing_filterset_2)) - self.assertTrue(passes_filterset(self.TestStrategy, sparse_passing_filterset)) - self.assertFalse(passes_filterset(self.TestStrategy, full_failing_filterset)) - self.assertFalse(passes_filterset(self.TestStrategy, sparse_failing_filterset)) + self.assertTrue( + passes_filterset(self.TestStrategy, full_passing_filterset_1) + ) + self.assertTrue( + passes_filterset(self.TestStrategy, full_passing_filterset_2) + ) + self.assertTrue( + passes_filterset(self.TestStrategy, sparse_passing_filterset) + ) + self.assertFalse( + passes_filterset(self.TestStrategy, full_failing_filterset) + ) + self.assertFalse( + passes_filterset(self.TestStrategy, sparse_failing_filterset) + ) def test_filtered_strategies(self): class StochasticTestStrategy(Player): @@ -134,7 +150,11 @@ class StochasticTestStrategy(Player): } class MemoryDepth2TestStrategy(Player): - classifier = {"stochastic": False, "memory_depth": 2, "makes_use_of": []} + classifier = { + "stochastic": False, + "memory_depth": 2, + "makes_use_of": [], + } class UsesLengthTestStrategy(Player): classifier = { diff --git a/axelrod/tests/unit/test_fingerprint.py b/axelrod/tests/unit/test_fingerprint.py index 9c727f91e..8f9e25b43 100644 --- a/axelrod/tests/unit/test_fingerprint.py +++ b/axelrod/tests/unit/test_fingerprint.py @@ -90,7 +90,9 @@ def test_fingerprint_player(self): self.assertEqual(af.points, self.points_when_using_half_step) self.assertEqual(af.spatial_tournament.turns, 5) self.assertEqual(af.spatial_tournament.repetitions, 3) - self.assertEqual(af.spatial_tournament.edges, self.edges_when_using_half_step) + self.assertEqual( + af.spatial_tournament.edges, self.edges_when_using_half_step + ) # The first player is the fingerprinted one, the rest are probes. self.assertIsInstance(af.spatial_tournament.players[0], axl.Cooperator) @@ -129,7 +131,17 @@ def test_fingerprint_interactions_cooperator(self): # The keys are edges between players, values are repetitions. self.assertCountEqual( af.interactions.keys(), - [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9)], + [ + (0, 1), + (0, 2), + (0, 3), + (0, 4), + (0, 5), + (0, 6), + (0, 7), + (0, 8), + (0, 9), + ], ) self.assertEqual(len(af.interactions.values()), 9) @@ -187,7 +199,9 @@ def test_fingerprint_interactions_titfortat(self): def test_progress_bar_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) - data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=True) + data = af.fingerprint( + turns=10, repetitions=2, step=0.5, progress_bar=True + ) self.assertEqual(sorted(data.keys()), self.points_when_using_half_step) @patch("axelrod.fingerprint.mkstemp", RecordedMksTemp.mkstemp) @@ -216,7 +230,11 @@ def test_fingerprint_with_filename(self): filename = axl_filename(path) af = AshlockFingerprint(axl.TitForTat) af.fingerprint( - turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename + turns=1, + repetitions=1, + step=0.5, + progress_bar=False, + filename=filename, ) with open(filename, "r") as out: data = out.read() @@ -224,7 +242,9 @@ def test_fingerprint_with_filename(self): def test_serial_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) - data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=False) + data = af.fingerprint( + turns=10, repetitions=2, step=0.5, progress_bar=False + ) edge_keys = sorted(list(af.interactions.keys())) coord_keys = sorted(list(data.keys())) self.assertEqual(af.step, 0.5) @@ -244,9 +264,13 @@ def test_parallel_fingerprint(self): def test_plot_data(self): af = AshlockFingerprint(axl.Cooperator()) - af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False, seed=0) + af.fingerprint( + turns=5, repetitions=3, step=0.5, progress_bar=False, seed=0 + ) - reshaped_data = np.array([[0.0, 0.0, 0.0], [1., 2., 1.6], [3.0, 3.0, 3.0]]) + reshaped_data = np.array( + [[0.0, 0.0, 0.0], [1.0, 2.0, 1.6], [3.0, 3.0, 3.0]] + ) plotted_data = af.plot().gca().images[0].get_array() np.testing.assert_allclose(plotted_data, reshaped_data) @@ -292,12 +316,13 @@ def test_wsls_fingerprint(self): Point(x=1.0, y=0.25): 4.86, Point(x=1.0, y=0.5): 4.36, Point(x=1.0, y=0.75): 4.05, - Point(x=1.0, y=1.0): 1.3 + Point(x=1.0, y=1.0): 1.3, } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) - data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, - seed=0) + data = af.fingerprint( + turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 + ) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -328,12 +353,13 @@ def test_tft_fingerprint(self): Point(x=1.0, y=0.25): 2.68, Point(x=1.0, y=0.5): 2.51, Point(x=1.0, y=0.75): 2.41, - Point(x=1.0, y=1.0): 2.18 + Point(x=1.0, y=1.0): 2.18, } af = axl.AshlockFingerprint(axl.TitForTat(), axl.TitForTat) - data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, - seed=0) + data = af.fingerprint( + turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 + ) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -364,12 +390,13 @@ def test_majority_fingerprint(self): Point(x=1.0, y=0.25): 2.12, Point(x=1.0, y=0.5): 1.8599999999999999, Point(x=1.0, y=0.75): 2.0300000000000002, - Point(x=1.0, y=1.0): 2.26 + Point(x=1.0, y=1.0): 2.26, } af = axl.AshlockFingerprint(axl.GoByMajority, axl.TitForTat) - data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, - seed=0) + data = af.fingerprint( + turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 + ) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -383,22 +410,30 @@ def test_pair_fingerprints(self, strategy_pair): """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=1) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=1 + ) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=2) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=2 + ) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe()) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=3) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=3 + ) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 + ) self.assertIsInstance(data, dict) @given(strategy_pair=strategy_lists(min_size=2, max_size=2)) @@ -410,11 +445,15 @@ def test_fingerprint_reproducibility(self, strategy_pair): """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 + ) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data2 = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) + data2 = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 + ) self.assertEqual(data, data2) @@ -425,13 +464,16 @@ def test_init(self): fingerprint = axl.TransitiveFingerprint(strategy=player) self.assertEqual(fingerprint.strategy, player) self.assertEqual( - fingerprint.opponents, [axl.Random(p) for p in np.linspace(0, 1, 50)] + fingerprint.opponents, + [axl.Random(p) for p in np.linspace(0, 1, 50)], ) def test_init_with_opponents(self): player = axl.TitForTat() opponents = [s() for s in axl.demo_strategies] - fingerprint = axl.TransitiveFingerprint(strategy=player, opponents=opponents) + fingerprint = axl.TransitiveFingerprint( + strategy=player, opponents=opponents + ) self.assertEqual(fingerprint.strategy, player) self.assertEqual(fingerprint.opponents, opponents) @@ -443,7 +485,8 @@ def test_init_with_not_default_number(self): ) self.assertEqual(fingerprint.strategy, player) self.assertEqual( - fingerprint.opponents, [axl.Random(p) for p in np.linspace(0, 1, 10)] + fingerprint.opponents, + [axl.Random(p) for p in np.linspace(0, 1, 10)], ) def test_fingerprint_with_filename(self): @@ -451,7 +494,9 @@ def test_fingerprint_with_filename(self): filename = axl_filename(path) strategy = axl.TitForTat() tf = TransitiveFingerprint(strategy) - tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename) + tf.fingerprint( + turns=1, repetitions=1, progress_bar=False, filename=filename + ) with open(filename, "r") as out: data = out.read() self.assertEqual(len(data.split("\n")), 102) @@ -461,9 +506,7 @@ def test_serial_fingerprint(self): tf = TransitiveFingerprint(strategy) path = pathlib.Path("test_outputs/test_fingerprint.csv") tf.fingerprint( - repetitions=1, - progress_bar=False, - filename=axl_filename(path), + repetitions=1, progress_bar=False, filename=axl_filename(path), ) self.assertEqual(tf.data.shape, (50, 50)) diff --git a/axelrod/tests/unit/test_graph.py b/axelrod/tests/unit/test_graph.py index 41f7d6d62..2ca72af31 100644 --- a/axelrod/tests/unit/test_graph.py +++ b/axelrod/tests/unit/test_graph.py @@ -39,8 +39,12 @@ def test_undirected_graph_with_vertices_and_unweighted_edges(self): self.assertEqual(str(g), "") self.assertEqual(g._edges, [(1, 2), (2, 1), (2, 3), (3, 2)]) - self.assert_out_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) - self.assert_in_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) + self.assert_out_mapping( + g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}} + ) + self.assert_in_mapping( + g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}} + ) def test_undirected_graph_with_vertices_and_weighted_edges(self): g = axl.graph.Graph(edges=[[1, 2, 10], [2, 3, 5]]) @@ -91,7 +95,11 @@ def test_add_loops_with_existing_loop_and_using_strings(self): g.add_loops() self.assertEqual( list(sorted(g._edges)), - list(sorted([("a", "b"), ("b", "a"), ("c", "c"), ("a", "a"), ("b", "b")])), + list( + sorted( + [("a", "b"), ("b", "a"), ("c", "c"), ("a", "a"), ("b", "b")] + ) + ), ) @@ -147,9 +155,19 @@ def test_length_4_undirected(self): edges = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2), (3, 0), (0, 3)] self.assertEqual(g.vertices, [0, 1, 2, 3]) self.assertEqual(g.edges, edges) - for vertex, neighbors in [(0, (1, 3)), (1, (0, 2)), (2, (1, 3)), (3, (0, 2))]: + for vertex, neighbors in [ + (0, (1, 3)), + (1, (0, 2)), + (2, (1, 3)), + (3, (0, 2)), + ]: self.assertEqual(set(g.out_vertices(vertex)), set(neighbors)) - for vertex, neighbors in [(0, (1, 3)), (1, (0, 2)), (2, (1, 3)), (3, (0, 2))]: + for vertex, neighbors in [ + (0, (1, 3)), + (1, (0, 2)), + (2, (1, 3)), + (3, (0, 2)), + ]: self.assertEqual(set(g.in_vertices(vertex)), set(neighbors)) @@ -210,7 +228,17 @@ def test_size_2_with_loops(self): def test_size_3_with_loops(self): g = axl.graph.complete_graph(3, loops=True) self.assertEqual(g.vertices, [0, 1, 2]) - edges = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1), (0, 0), (1, 1), (2, 2)] + edges = [ + (0, 1), + (1, 0), + (0, 2), + (2, 0), + (1, 2), + (2, 1), + (0, 0), + (1, 1), + (2, 2), + ] self.assertEqual(g.edges, edges) self.assertEqual(g.directed, False) @@ -246,59 +274,70 @@ def test_size_4_with_loops(self): class TestAttachedComplete(unittest.TestCase): def test_size_2(self): g = axl.graph.attached_complete_graphs(2, loops=False) - self.assertEqual(g.vertices, ['0:0', '0:1', '1:0', '1:1']) + self.assertEqual(g.vertices, ["0:0", "0:1", "1:0", "1:1"]) self.assertEqual( g.edges, - [('0:0', '0:1'), ('0:1', '0:0'), ('1:0', '1:1'), ('1:1', '1:0'), ('0:0', '1:0'), ('1:0', '0:0')] + [ + ("0:0", "0:1"), + ("0:1", "0:0"), + ("1:0", "1:1"), + ("1:1", "1:0"), + ("0:0", "1:0"), + ("1:0", "0:0"), + ], ) self.assertEqual(g.directed, False) def test_size_3(self): g = axl.graph.attached_complete_graphs(3, loops=False) - self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) + self.assertEqual(g.vertices, ["0:0", "0:1", "0:2", "1:0", "1:1", "1:2"]) self.assertEqual( g.edges, - [('0:0', '0:1'), - ('0:1', '0:0'), - ('0:0', '0:2'), - ('0:2', '0:0'), - ('0:1', '0:2'), - ('0:2', '0:1'), - ('1:0', '1:1'), - ('1:1', '1:0'), - ('1:0', '1:2'), - ('1:2', '1:0'), - ('1:1', '1:2'), - ('1:2', '1:1'), - ('0:0', '1:0'), - ('1:0', '0:0')] + [ + ("0:0", "0:1"), + ("0:1", "0:0"), + ("0:0", "0:2"), + ("0:2", "0:0"), + ("0:1", "0:2"), + ("0:2", "0:1"), + ("1:0", "1:1"), + ("1:1", "1:0"), + ("1:0", "1:2"), + ("1:2", "1:0"), + ("1:1", "1:2"), + ("1:2", "1:1"), + ("0:0", "1:0"), + ("1:0", "0:0"), + ], ) self.assertEqual(g.directed, False) def test_size_3_with_loops(self): g = axl.graph.attached_complete_graphs(3, loops=True) - self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) + self.assertEqual(g.vertices, ["0:0", "0:1", "0:2", "1:0", "1:1", "1:2"]) self.assertEqual( g.edges, - [('0:0', '0:1'), - ('0:1', '0:0'), - ('0:0', '0:2'), - ('0:2', '0:0'), - ('0:1', '0:2'), - ('0:2', '0:1'), - ('1:0', '1:1'), - ('1:1', '1:0'), - ('1:0', '1:2'), - ('1:2', '1:0'), - ('1:1', '1:2'), - ('1:2', '1:1'), - ('0:0', '1:0'), - ('1:0', '0:0'), - ('0:0', '0:0'), - ('0:1', '0:1'), - ('0:2', '0:2'), - ('1:0', '1:0'), - ('1:1', '1:1'), - ('1:2', '1:2')] + [ + ("0:0", "0:1"), + ("0:1", "0:0"), + ("0:0", "0:2"), + ("0:2", "0:0"), + ("0:1", "0:2"), + ("0:2", "0:1"), + ("1:0", "1:1"), + ("1:1", "1:0"), + ("1:0", "1:2"), + ("1:2", "1:0"), + ("1:1", "1:2"), + ("1:2", "1:1"), + ("0:0", "1:0"), + ("1:0", "0:0"), + ("0:0", "0:0"), + ("0:1", "0:1"), + ("0:2", "0:2"), + ("1:0", "1:0"), + ("1:1", "1:1"), + ("1:2", "1:2"), + ], ) self.assertEqual(g.directed, False) diff --git a/axelrod/tests/unit/test_history.py b/axelrod/tests/unit/test_history.py index e7b105c31..11cd2445c 100644 --- a/axelrod/tests/unit/test_history.py +++ b/axelrod/tests/unit/test_history.py @@ -83,8 +83,7 @@ def test_flip_plays(self): self.assertEqual(flipped_history, [D, C, D, C, D]) self.assertEqual(flipped_history.cooperations, 2) self.assertEqual(flipped_history.defections, 3) - self.assertEqual(flipped_history.state_distribution, - new_distribution) + self.assertEqual(flipped_history.state_distribution, new_distribution) # Flip operation is idempotent flipped_flipped_history = flipped_history.flip_plays() @@ -94,7 +93,6 @@ def test_flip_plays(self): class TestLimitedHistory(unittest.TestCase): - def test_memory_depth(self): h = LimitedHistory(memory_depth=3) h.append(C, C) @@ -105,8 +103,9 @@ def test_memory_depth(self): self.assertEqual(len(h), 3) self.assertEqual(h.cooperations, 2) self.assertEqual(h.defections, 1) - self.assertEqual(h.state_distribution, - Counter({(C, C): 1, (D, D): 1, (C, D): 1})) + self.assertEqual( + h.state_distribution, Counter({(C, C): 1, (D, D): 1, (C, D): 1}) + ) h.append(D, C) self.assertEqual(len(h), 3) self.assertEqual(h._plays, [D, C, D]) @@ -115,4 +114,5 @@ def test_memory_depth(self): self.assertEqual(h.defections, 2) self.assertEqual( h.state_distribution, - Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0})) + Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0}), + ) diff --git a/axelrod/tests/unit/test_interaction_utils.py b/axelrod/tests/unit/test_interaction_utils.py index 49706fe01..d56e62cce 100644 --- a/axelrod/tests/unit/test_interaction_utils.py +++ b/axelrod/tests/unit/test_interaction_utils.py @@ -49,39 +49,65 @@ def test_compute_scores(self): def test_compute_final_score(self): for inter, final_score in zip(self.interactions, self.final_scores): - self.assertEqual(final_score, axl.interaction_utils.compute_final_score(inter)) + self.assertEqual( + final_score, axl.interaction_utils.compute_final_score(inter) + ) def test_compute_final_score_per_turn(self): for inter, final_score_per_round in zip( self.interactions, self.final_score_per_turn ): self.assertEqual( - final_score_per_round, axl.interaction_utils.compute_final_score_per_turn(inter) + final_score_per_round, + axl.interaction_utils.compute_final_score_per_turn(inter), ) def test_compute_winner_index(self): for inter, winner in zip(self.interactions, self.winners): - self.assertEqual(winner, axl.interaction_utils.compute_winner_index(inter)) + self.assertEqual( + winner, axl.interaction_utils.compute_winner_index(inter) + ) def test_compute_cooperations(self): for inter, coop in zip(self.interactions, self.cooperations): - self.assertEqual(coop, axl.interaction_utils.compute_cooperations(inter)) + self.assertEqual( + coop, axl.interaction_utils.compute_cooperations(inter) + ) def test_compute_normalised_cooperations(self): for inter, coop in zip(self.interactions, self.normalised_cooperations): - self.assertEqual(coop, axl.interaction_utils.compute_normalised_cooperation(inter)) + self.assertEqual( + coop, + axl.interaction_utils.compute_normalised_cooperation(inter), + ) def test_compute_state_distribution(self): for inter, dist in zip(self.interactions, self.state_distribution): - self.assertEqual(dist, axl.interaction_utils.compute_state_distribution(inter)) + self.assertEqual( + dist, axl.interaction_utils.compute_state_distribution(inter) + ) def test_compute_normalised_state_distribution(self): - for inter, dist in zip(self.interactions, self.normalised_state_distribution): - self.assertEqual(dist, axl.interaction_utils.compute_normalised_state_distribution(inter)) + for inter, dist in zip( + self.interactions, self.normalised_state_distribution + ): + self.assertEqual( + dist, + axl.interaction_utils.compute_normalised_state_distribution( + inter + ), + ) def test_compute_state_to_action_distribution(self): - for inter, dist in zip(self.interactions, self.state_to_action_distribution): - self.assertEqual(dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) + for inter, dist in zip( + self.interactions, self.state_to_action_distribution + ): + self.assertEqual( + dist, + axl.interaction_utils.compute_state_to_action_distribution( + inter + ), + ) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ Counter( @@ -93,17 +119,25 @@ def test_compute_state_to_action_distribution(self): ((D, D), C): 1, } ), - Counter({((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1}), + Counter( + {((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1} + ), ] - self.assertEqual(expected_dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) + self.assertEqual( + expected_dist, + axl.interaction_utils.compute_state_to_action_distribution(inter), + ) def test_compute_normalised_state_to_action_distribution(self): for inter, dist in zip( self.interactions, self.normalised_state_to_action_distribution ): self.assertEqual( - dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) + dist, + axl.interaction_utils.compute_normalised_state_to_action_distribution( + inter + ), ) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ @@ -116,15 +150,22 @@ def test_compute_normalised_state_to_action_distribution(self): ((D, D), C): 1, } ), - Counter({((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1}), + Counter( + {((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1} + ), ] self.assertEqual( - expected_dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) + expected_dist, + axl.interaction_utils.compute_normalised_state_to_action_distribution( + inter + ), ) def test_compute_sparklines(self): for inter, spark in zip(self.interactions, self.sparklines): - self.assertEqual(spark, axl.interaction_utils.compute_sparklines(inter)) + self.assertEqual( + spark, axl.interaction_utils.compute_sparklines(inter) + ) def test_read_interactions_from_file(self): tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=False) @@ -137,10 +178,14 @@ def test_read_interactions_from_file(self): (0, 1): [[(C, D), (C, D)] for _ in range(3)], (1, 1): [[(D, D), (D, D)] for _ in range(3)], } - interactions = axl.interaction_utils.read_interactions_from_file(tmp_file.name, progress_bar=False) + interactions = axl.interaction_utils.read_interactions_from_file( + tmp_file.name, progress_bar=False + ) self.assertEqual(expected_interactions, interactions) def test_string_to_interactions(self): string = "CDCDDD" interactions = [(C, D), (C, D), (D, D)] - self.assertEqual(axl.interaction_utils.string_to_interactions(string), interactions) + self.assertEqual( + axl.interaction_utils.string_to_interactions(string), interactions + ) diff --git a/axelrod/tests/unit/test_match.py b/axelrod/tests/unit/test_match.py index 84040eaa5..6012ce32f 100644 --- a/axelrod/tests/unit/test_match.py +++ b/axelrod/tests/unit/test_match.py @@ -38,7 +38,9 @@ def test_init_with_prob_end(self, prob_end, game): self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), game.RPST()) - self.assertEqual(match.players[0].match_attributes["length"], float("inf")) + self.assertEqual( + match.players[0].match_attributes["length"], float("inf") + ) self.assertEqual(match._cache, {}) @given( @@ -56,7 +58,9 @@ def test_init_with_prob_end_and_turns(self, turns, prob_end, game): self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), game.RPST()) - self.assertEqual(match.players[0].match_attributes["length"], float("inf")) + self.assertEqual( + match.players[0].match_attributes["length"], float("inf") + ) self.assertEqual(match._cache, {}) def test_default_init(self): @@ -83,7 +87,9 @@ def test_example_prob_end(self): expected_lengths = [2, 1, 1] for seed, expected_length in zip(range(3), expected_lengths): match = axl.Match((p1, p2), prob_end=0.5, seed=seed) - self.assertEqual(match.players[0].match_attributes["length"], float("inf")) + self.assertEqual( + match.players[0].match_attributes["length"], float("inf") + ) self.assertEqual(len(match.play()), expected_length) self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), (3, 1, 0, 5)) @@ -117,7 +123,7 @@ def test_len_error(self): with self.assertRaises(TypeError): len(match) - @given(p=floats(min_value=1e-10, max_value=1-1e-10)) + @given(p=floats(min_value=1e-10, max_value=1 - 1e-10)) def test_stochastic(self, p): p1, p2 = axl.Cooperator(), axl.Cooperator() match = axl.Match((p1, p2), 5) @@ -130,7 +136,7 @@ def test_stochastic(self, p): match = axl.Match((p1, p2), 5) self.assertTrue(match._stochastic) - @given(p=floats(min_value=1e-10, max_value=1-1e-10)) + @given(p=floats(min_value=1e-10, max_value=1 - 1e-10)) def test_cache_update_required(self, p): p1, p2 = axl.Cooperator(), axl.Cooperator() match = axl.Match((p1, p2), 5, noise=p) @@ -180,8 +186,7 @@ def test_cache_grows(self): self.assertEqual(match.play(), expected_result_5_turn) # The cache should now hold the 5-turn result.. self.assertEqual( - cache[(axl.Cooperator(), axl.Defector())], - expected_result_5_turn + cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn ) def test_cache_doesnt_shrink(self): @@ -200,8 +205,7 @@ def test_cache_doesnt_shrink(self): self.assertEqual(match.play(), expected_result_3_turn) # The cache should still hold the 5. self.assertEqual( - cache[(axl.Cooperator(), axl.Defector())], - expected_result_5_turn + cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn ) def test_scores(self): @@ -361,7 +365,9 @@ def test_sample_length(self): ]: rng = RandomGenerator(seed) r = rng.random() - self.assertEqual(axl.match.sample_length(prob_end, r), expected_length) + self.assertEqual( + axl.match.sample_length(prob_end, r), expected_length + ) def test_sample_with_0_prob(self): self.assertEqual(axl.match.sample_length(0, 0.4), float("inf")) diff --git a/axelrod/tests/unit/test_match_generator.py b/axelrod/tests/unit/test_match_generator.py index e91a7eba8..730928216 100644 --- a/axelrod/tests/unit/test_match_generator.py +++ b/axelrod/tests/unit/test_match_generator.py @@ -148,7 +148,9 @@ def test_build_single_match_params_with_fixed_length_unknown(self): self.assertEqual(match_params["game"], test_game) self.assertEqual(match_params["prob_end"], None) self.assertEqual(match_params["noise"], 0) - self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) + self.assertEqual( + match_params["match_attributes"], {"length": float("inf")} + ) # Check that can build a match players = [axl.Cooperator(), axl.Defector()] @@ -166,7 +168,7 @@ def test_build_match_chunks(self, repetitions): players=self.players, turns=test_turns, game=test_game, - repetitions=repetitions + repetitions=repetitions, ) chunks = list(rr.build_match_chunks()) match_definitions = [ @@ -177,10 +179,14 @@ def test_build_match_chunks(self, repetitions): (i, j, repetitions) for i in range(5) for j in range(i, 5) ] - self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) + self.assertEqual( + sorted(match_definitions), sorted(expected_match_definitions) + ) - @given(repetitions=integers(min_value=1, max_value=test_repetitions), - seed=integers(min_value=1, max_value=4294967295),) + @given( + repetitions=integers(min_value=1, max_value=test_repetitions), + seed=integers(min_value=1, max_value=4294967295), + ) @settings(max_examples=5) def test_seeding_equality(self, repetitions, seed): rr1 = axl.MatchGenerator( @@ -188,7 +194,7 @@ def test_seeding_equality(self, repetitions, seed): turns=test_turns, game=test_game, repetitions=repetitions, - seed=seed + seed=seed, ) chunks1 = list(rr1.build_match_chunks()) rr2 = axl.MatchGenerator( @@ -196,7 +202,7 @@ def test_seeding_equality(self, repetitions, seed): turns=test_turns, game=test_game, repetitions=repetitions, - seed=seed + seed=seed, ) chunks2 = list(rr2.build_match_chunks()) self.assertEqual(chunks1, chunks2) @@ -207,7 +213,7 @@ def test_seeding_inequality(self, repetitions=10): turns=test_turns, game=test_game, repetitions=repetitions, - seed=0 + seed=0, ) chunks1 = list(rr1.build_match_chunks()) rr2 = axl.MatchGenerator( @@ -215,7 +221,7 @@ def test_seeding_inequality(self, repetitions=10): turns=test_turns, game=test_game, repetitions=repetitions, - seed=1 + seed=1, ) chunks2 = list(rr2.build_match_chunks()) self.assertNotEqual(chunks1, chunks2) @@ -239,7 +245,9 @@ def test_spatial_build_match_chunks(self, repetitions): ] expected_match_definitions = [(i, j, repetitions) for i, j in cycle] - self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) + self.assertEqual( + sorted(match_definitions), sorted(expected_match_definitions) + ) def test_len(self): turns = 5 diff --git a/axelrod/tests/unit/test_moran.py b/axelrod/tests/unit/test_moran.py index 8018683c6..3d5e02734 100644 --- a/axelrod/tests/unit/test_moran.py +++ b/axelrod/tests/unit/test_moran.py @@ -23,16 +23,21 @@ def test_init(self): self.assertEqual(mp.noise, 0) self.assertEqual(mp.initial_players, players) self.assertEqual(mp.players, list(players)) - self.assertEqual(mp.populations, [Counter({"Cooperator": 1, "Defector": 1})]) + self.assertEqual( + mp.populations, [Counter({"Cooperator": 1, "Defector": 1})] + ) self.assertIsNone(mp.winning_strategy_name) self.assertEqual(mp.mutation_rate, 0) self.assertEqual(mp.mode, "bd") self.assertEqual(mp.deterministic_cache, axl.DeterministicCache()) self.assertEqual( - mp.mutation_targets, {"Cooperator": [players[1]], "Defector": [players[0]]} + mp.mutation_targets, + {"Cooperator": [players[1]], "Defector": [players[0]]}, ) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (1, 0)]) - self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)]) + self.assertEqual( + mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)] + ) self.assertEqual(mp.fitness_transformation, None) self.assertEqual(mp.locations, [0, 1]) self.assertEqual(mp.index, {0: 0, 1: 1}) @@ -48,7 +53,9 @@ def test_init(self): sorted([(0, 1), (2, 0), (1, 2), (0, 0), (1, 1), (2, 2)]), ) - mp = axl.MoranProcess(players, interaction_graph=graph, reproduction_graph=graph) + mp = axl.MoranProcess( + players, interaction_graph=graph, reproduction_graph=graph + ) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (2, 0), (1, 2)]) self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (2, 0), (1, 2)]) @@ -225,8 +232,12 @@ def test_two_random_players(self): def test_two_players_with_mutation(self): p1, p2 = axl.Cooperator(), axl.Defector() - mp = MoranProcess((p1, p2), mutation_rate=0.2, stop_on_fixation=False, seed=5) - self.assertDictEqual(mp.mutation_targets, {str(p1): [p2], str(p2): [p1]}) + mp = MoranProcess( + (p1, p2), mutation_rate=0.2, stop_on_fixation=False, seed=5 + ) + self.assertDictEqual( + mp.mutation_targets, {str(p1): [p2], str(p2): [p1]} + ) # Test that mutation causes the population to alternate between # fixations counters = [ @@ -262,7 +273,9 @@ def test_three_players_with_mutation(self): p2 = axl.Random() p3 = axl.Defector() players = [p1, p2, p3] - mp = axl.MoranProcess(players, mutation_rate=0.2, stop_on_fixation=False) + mp = axl.MoranProcess( + players, mutation_rate=0.2, stop_on_fixation=False + ) self.assertDictEqual( mp.mutation_targets, {str(p1): [p3, p2], str(p2): [p1, p3], str(p3): [p1, p2]}, @@ -373,49 +386,61 @@ def test_cooperator_can_win_with_fitness_transformation(self): w = 0.95 fitness_transformation = lambda score: 1 - w + w * score mp = MoranProcess( - players, turns=10, fitness_transformation=fitness_transformation, - seed=3419 + players, + turns=10, + fitness_transformation=fitness_transformation, + seed=3419, ) populations = mp.play() self.assertEqual(mp.winning_strategy_name, "Cooperator") def test_atomic_mutation_fsm(self): - players = [axl.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C, - seed=4) - for _ in range(5)] + players = [ + axl.EvolvableFSMPlayer( + num_states=2, initial_state=1, initial_action=C, seed=4 + ) + for _ in range(5) + ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=12) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(sorted(mp.populations[-1].items()))[0][0], - 'EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643') + "EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643", + ) self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated) def test_atomic_mutation_cycler(self): cycle_length = 5 - players = [axl.EvolvableCycler(cycle_length=cycle_length, seed=4) - for _ in range(5)] + players = [ + axl.EvolvableCycler(cycle_length=cycle_length, seed=4) + for _ in range(5) + ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=10) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(mp.populations[-1].items())[0], - ('EvolvableCycler: CCDDD, 5, 0.2, 1, 1164244177', 1)) + ("EvolvableCycler: CCDDD, 5, 0.2, 1, 1164244177", 1), + ) self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated) def test_mutation_method_exceptions(self): cycle_length = 5 - players = [axl.EvolvableCycler(cycle_length=cycle_length, seed=4) - for _ in range(5)] + players = [ + axl.EvolvableCycler(cycle_length=cycle_length, seed=4) + for _ in range(5) + ] with self.assertRaises(ValueError): MoranProcess(players, turns=10, mutation_method="random", seed=10) - players = [axl.Cycler(cycle="CD" * random.randint(2, 10)) - for _ in range(10)] + players = [ + axl.Cycler(cycle="CD" * random.randint(2, 10)) for _ in range(10) + ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=53) with self.assertRaises(TypeError): for _ in range(10): @@ -442,9 +467,12 @@ def test_complete(self): mp = MoranProcess(players, seed=seed) mp.play() winner = mp.winning_strategy_name - mp = MoranProcess(players, interaction_graph=interaction_graph, - reproduction_graph=reproduction_graph, - seed=seed) + mp = MoranProcess( + players, + interaction_graph=interaction_graph, + reproduction_graph=reproduction_graph, + seed=seed, + ) mp.play() winner2 = mp.winning_strategy_name self.assertEqual(winner, winner2) @@ -483,14 +511,18 @@ def test_asymmetry(self): players.append(axl.Defector()) for seed, outcome in seeds: mp = MoranProcess( - players, interaction_graph=graph1, reproduction_graph=graph2, - seed=seed + players, + interaction_graph=graph1, + reproduction_graph=graph2, + seed=seed, ) mp.play() winner = mp.winning_strategy_name mp = MoranProcess( - players, interaction_graph=graph2, reproduction_graph=graph1, - seed=seed + players, + interaction_graph=graph2, + reproduction_graph=graph1, + seed=seed, ) mp.play() winner2 = mp.winning_strategy_name @@ -508,10 +540,14 @@ def test_cycle_death_birth(self): for _ in range(N // 2): players.append(axl.Defector()) for seed, outcome in seeds: - mp = MoranProcess(players, interaction_graph=graph, mode="bd", seed=seed) + mp = MoranProcess( + players, interaction_graph=graph, mode="bd", seed=seed + ) mp.play() winner = mp.winning_strategy_name - mp = MoranProcess(players, interaction_graph=graph, mode="db", seed=seed) + mp = MoranProcess( + players, interaction_graph=graph, mode="db", seed=seed + ) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome) @@ -541,7 +577,11 @@ def test_init(self): """Test the initialisation process""" self.assertEqual( set(self.amp.cached_outcomes.keys()), - {("Cooperator", "Defector"), ("Cooperator", "Cooperator"), ("Defector", "Defector")}, + { + ("Cooperator", "Defector"), + ("Cooperator", "Cooperator"), + ("Defector", "Defector"), + }, ) self.assertEqual(self.amp.players, self.players) self.assertEqual(self.amp.turns, 0) diff --git a/axelrod/tests/unit/test_pickling.py b/axelrod/tests/unit/test_pickling.py index 29f25d645..cec827819 100644 --- a/axelrod/tests/unit/test_pickling.py +++ b/axelrod/tests/unit/test_pickling.py @@ -10,7 +10,9 @@ # First set: special cases -PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()(axl.strategy_transformers.FlipTransformer()(axl.Cooperator)) +PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()( + axl.strategy_transformers.FlipTransformer()(axl.Cooperator) +) class MyDefector(axl.Player): @@ -111,7 +113,9 @@ class JossAnn(axl.Cooperator): probability = [0.2, 0.3] -@axl.strategy_transformers.MixedTransformer(probability, strategies, name_prefix=None) +@axl.strategy_transformers.MixedTransformer( + probability, strategies, name_prefix=None +) class Mixed(axl.Cooperator): pass @@ -159,7 +163,9 @@ def __init__(self): super().__init__(team=team) -TransformedMetaThue = axl.strategy_transformers.IdentityTransformer(name_prefix=None)(MetaThue) +TransformedMetaThue = axl.strategy_transformers.IdentityTransformer( + name_prefix=None +)(MetaThue) transformed_no_prefix = [ @@ -233,12 +239,24 @@ def test_parameterized_player(self): self.assert_original_equals_pickled(player) def test_sequence_player(self): - inline_transformed_thue = axl.strategy_transformers.IdentityTransformer(name_prefix="Transformed")(axl.ThueMorse)() - for player in [axl.ThueMorse(), axl.ThueMorseInverse(), MetaThue(), TransformedMetaThue(), - inline_transformed_thue, TransformedThue(), - ]: + inline_transformed_thue = axl.strategy_transformers.IdentityTransformer( + name_prefix="Transformed" + )(axl.ThueMorse)() + for player in [ + axl.ThueMorse(), + axl.ThueMorseInverse(), + MetaThue(), + TransformedMetaThue(), + inline_transformed_thue, + TransformedThue(), + ]: self.assert_equals_instance_from_pickling(player) - opponents = (axl.Defector, axl.Cooperator, axl.Random, axl.CyclerCCCDCD) + opponents = ( + axl.Defector, + axl.Cooperator, + axl.Random, + axl.CyclerCCCDCD, + ) for opponent_class in opponents: player.reset() opponent = opponent_class() @@ -271,9 +289,13 @@ def test_pickling_all_transformers_as_instance_called_on_a_class(self): self.assert_original_equals_pickled(player) def test_created_on_the_spot_multiple_transformers(self): - player_class = axl.strategy_transformers.FlipTransformer()(axl.Cooperator) + player_class = axl.strategy_transformers.FlipTransformer()( + axl.Cooperator + ) player_class = axl.strategy_transformers.DualTransformer()(player_class) - player = axl.strategy_transformers.FinalTransformer((C, D))(player_class)() + player = axl.strategy_transformers.FinalTransformer((C, D))( + player_class + )() self.assert_original_equals_pickled(player) @@ -290,9 +312,13 @@ def test_dual_transformer_regression_test(self): player_class = axl.WinStayLoseShift player_class = axl.strategy_transformers.DualTransformer()(player_class) - player_class = axl.strategy_transformers.InitialTransformer((C, D))(player_class) + player_class = axl.strategy_transformers.InitialTransformer((C, D))( + player_class + ) player_class = axl.strategy_transformers.DualTransformer()(player_class) - player_class = axl.strategy_transformers.TrackHistoryTransformer()(player_class) + player_class = axl.strategy_transformers.TrackHistoryTransformer()( + player_class + ) interspersed_dual_transformers = player_class() @@ -314,7 +340,8 @@ def test_class_and_instance_name_different_built_from_player_class(self): player = MyCooperator() class_names = [class_.__name__ for class_ in MyCooperator.mro()] self.assertEqual( - class_names, ["FlippedMyCooperator", "MyCooperator", "Player", "object"] + class_names, + ["FlippedMyCooperator", "MyCooperator", "Player", "object"], ) self.assert_original_equals_pickled(player) @@ -374,17 +401,23 @@ def test_with_various_name_prefixes(self): self.assertEqual(no_prefix.__class__.__name__, "Flip") self.assert_original_equals_pickled(no_prefix) - default_prefix = axl.strategy_transformers.FlipTransformer()(axl.Cooperator)() + default_prefix = axl.strategy_transformers.FlipTransformer()( + axl.Cooperator + )() self.assertEqual(default_prefix.__class__.__name__, "FlippedCooperator") self.assert_original_equals_pickled(default_prefix) - fliptastic = axl.strategy_transformers.FlipTransformer(name_prefix="Fliptastic") + fliptastic = axl.strategy_transformers.FlipTransformer( + name_prefix="Fliptastic" + ) new_prefix = fliptastic(axl.Cooperator)() self.assertEqual(new_prefix.__class__.__name__, "FliptasticCooperator") self.assert_original_equals_pickled(new_prefix) def test_dynamic_class_no_name_prefix(self): - player = axl.strategy_transformers.FlipTransformer(name_prefix=None)(axl.Cooperator)() + player = axl.strategy_transformers.FlipTransformer(name_prefix=None)( + axl.Cooperator + )() self.assertEqual(player.__class__.__name__, "Cooperator") self.assert_original_equals_pickled(player) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index 847419035..f5f621c55 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -32,7 +32,11 @@ def setUpClass(cls): [3 / 2 for _ in range(3)], ] cls.expected_boxplot_xticks_locations = [1, 2, 3, 4] - cls.expected_boxplot_xticks_labels = ["Defector", "Tit For Tat", "Alternator"] + cls.expected_boxplot_xticks_labels = [ + "Defector", + "Tit For Tat", + "Alternator", + ] cls.expected_lengthplot_dataset = [ [cls.turns for _ in range(3)], @@ -41,9 +45,21 @@ def setUpClass(cls): ] cls.expected_payoff_dataset = [ - [0, mean([9 / 5 for _ in range(3)]), mean([17 / 5 for _ in range(3)])], - [mean([4 / 5 for _ in range(3)]), 0, mean([13 / 5 for _ in range(3)])], - [mean([2 / 5 for _ in range(3)]), mean([13 / 5 for _ in range(3)]), 0], + [ + 0, + mean([9 / 5 for _ in range(3)]), + mean([17 / 5 for _ in range(3)]), + ], + [ + mean([4 / 5 for _ in range(3)]), + 0, + mean([13 / 5 for _ in range(3)]), + ], + [ + mean([2 / 5 for _ in range(3)]), + mean([13 / 5 for _ in range(3)]), + 0, + ], ] cls.expected_winplot_dataset = ( [[2, 2, 2], [0, 0, 0], [0, 0, 0]], @@ -104,12 +120,15 @@ def test_init_from_resulsetfromfile(self): def test_boxplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._boxplot_dataset, self.expected_boxplot_dataset) + self.assertSequenceEqual( + plot._boxplot_dataset, self.expected_boxplot_dataset + ) def test_boxplot_xticks_locations(self): plot = axl.Plot(self.test_result_set) self.assertEqual( - plot._boxplot_xticks_locations, self.expected_boxplot_xticks_locations + plot._boxplot_xticks_locations, + self.expected_boxplot_xticks_locations, ) def test_boxplot_xticks_labels(self): @@ -145,7 +164,9 @@ def test_boxplot_with_title(self): def test_winplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) + self.assertSequenceEqual( + plot._winplot_dataset, self.expected_winplot_dataset + ) def test_winplot(self): plot = axl.Plot(self.test_result_set) @@ -155,7 +176,9 @@ def test_winplot(self): def test_sdvplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._sdv_plot_dataset, self.expected_sdvplot_dataset) + self.assertSequenceEqual( + plot._sdv_plot_dataset, self.expected_sdvplot_dataset + ) def test_sdvplot(self): plot = axl.Plot(self.test_result_set) @@ -165,7 +188,9 @@ def test_sdvplot(self): def test_lengthplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) + self.assertSequenceEqual( + plot._winplot_dataset, self.expected_winplot_dataset + ) def test_lengthplot(self): plot = axl.Plot(self.test_result_set) @@ -181,7 +206,9 @@ def test_pdplot(self): def test_payoff_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._payoff_dataset, self.expected_payoff_dataset) + self.assertSequenceEqual( + plot._payoff_dataset, self.expected_payoff_dataset + ) def test_payoff(self): plot = axl.Plot(self.test_result_set) @@ -250,6 +277,8 @@ def test_all_plots(self): ) self.assertIsNone( plot.save_all_plots( - prefix="test_outputs/", title_prefix="A prefix", progress_bar=True + prefix="test_outputs/", + title_prefix="A prefix", + progress_bar=True, ) ) diff --git a/axelrod/tests/unit/test_property.py b/axelrod/tests/unit/test_property.py index ae992a035..945aae5be 100644 --- a/axelrod/tests/unit/test_property.py +++ b/axelrod/tests/unit/test_property.py @@ -12,7 +12,9 @@ ) from hypothesis import given, settings -stochastic_strategies = [s for s in axl.strategies if axl.Classifiers["stochastic"](s())] +stochastic_strategies = [ + s for s in axl.strategies if axl.Classifiers["stochastic"](s()) +] class TestStrategyList(unittest.TestCase): @@ -130,7 +132,11 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given(tournament=prob_end_tournaments(strategies=axl.basic_strategies, max_size=3)) + @given( + tournament=prob_end_tournaments( + strategies=axl.basic_strategies, max_size=3 + ) + ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axl.Tournament) @@ -165,7 +171,11 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given(tournament=spatial_tournaments(strategies=axl.basic_strategies, max_size=3)) + @given( + tournament=spatial_tournaments( + strategies=axl.basic_strategies, max_size=3 + ) + ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axl.Tournament) diff --git a/axelrod/tests/unit/test_resultset.py b/axelrod/tests/unit/test_resultset.py index 97563d3e9..bb0aa2091 100644 --- a/axelrod/tests/unit/test_resultset.py +++ b/axelrod/tests/unit/test_resultset.py @@ -71,15 +71,39 @@ def setUpClass(cls): # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ - [0, mean([13 / 5 for _ in range(3)]), mean([2 / 5 for _ in range(3)])], - [mean([13 / 5 for _ in range(3)]), 0, mean([4 / 5 for _ in range(3)])], - [mean([17 / 5 for _ in range(3)]), mean([9 / 5 for _ in range(3)]), 0], + [ + 0, + mean([13 / 5 for _ in range(3)]), + mean([2 / 5 for _ in range(3)]), + ], + [ + mean([13 / 5 for _ in range(3)]), + 0, + mean([4 / 5 for _ in range(3)]), + ], + [ + mean([17 / 5 for _ in range(3)]), + mean([9 / 5 for _ in range(3)]), + 0, + ], ] cls.expected_payoff_stddevs = [ - [0, std([13 / 5 for _ in range(3)]), std([2 / 5 for _ in range(3)])], - [std([13 / 5 for _ in range(3)]), 0, std([4 / 5 for _ in range(3)])], - [std([17 / 5 for _ in range(3)]), std([9 / 5 for _ in range(3)]), 0], + [ + 0, + std([13 / 5 for _ in range(3)]), + std([2 / 5 for _ in range(3)]), + ], + [ + std([13 / 5 for _ in range(3)]), + 0, + std([4 / 5 for _ in range(3)]), + ], + [ + std([17 / 5 for _ in range(3)]), + std([9 / 5 for _ in range(3)]), + 0, + ], ] cls.expected_cooperation = [[0, 9, 9], [9, 0, 3], [0, 0, 0]] @@ -88,8 +112,16 @@ def setUpClass(cls): cls.expected_initial_cooperation_rate = [1, 1, 0] cls.expected_normalised_cooperation = [ - [0, mean([3 / 5 for _ in range(3)]), mean([3 / 5 for _ in range(3)])], - [mean([3 / 5 for _ in range(3)]), 0, mean([1 / 5 for _ in range(3)])], + [ + 0, + mean([3 / 5 for _ in range(3)]), + mean([3 / 5 for _ in range(3)]), + ], + [ + mean([3 / 5 for _ in range(3)]), + 0, + mean([1 / 5 for _ in range(3)]), + ], [0, 0, 0], ] @@ -176,7 +208,11 @@ def setUpClass(cls): cls.expected_good_partner_rating = [1.0, 1.0, 0] - cls.expected_eigenjesus_rating = [0.5547001962252291, 0.8320502943378436, 0.0] + cls.expected_eigenjesus_rating = [ + 0.5547001962252291, + 0.8320502943378436, + 0.0, + ] cls.expected_eigenmoses_rating = [ -0.4578520302117101, @@ -337,7 +373,9 @@ def test_score_diffs(self): for i, row in enumerate(rs.score_diffs): for j, col in enumerate(row): for k, score in enumerate(col): - self.assertAlmostEqual(score, self.expected_score_diffs[i][j][k]) + self.assertAlmostEqual( + score, self.expected_score_diffs[i][j][k] + ) def test_payoff_diffs_means(self): rs = axl.ResultSet( @@ -347,7 +385,9 @@ def test_payoff_diffs_means(self): self.assertEqual(len(rs.payoff_diffs_means), rs.num_players) for i, row in enumerate(rs.payoff_diffs_means): for j, col in enumerate(row): - self.assertAlmostEqual(col, self.expected_payoff_diffs_means[i][j]) + self.assertAlmostEqual( + col, self.expected_payoff_diffs_means[i][j] + ) def test_payoff_stddevs(self): rs = axl.ResultSet( @@ -372,7 +412,8 @@ def test_initial_cooperation_count(self): self.assertIsInstance(rs.initial_cooperation_count, list) self.assertEqual(len(rs.initial_cooperation_count), rs.num_players) self.assertEqual( - rs.initial_cooperation_count, self.expected_initial_cooperation_count + rs.initial_cooperation_count, + self.expected_initial_cooperation_count, ) def test_normalised_cooperation(self): @@ -383,7 +424,9 @@ def test_normalised_cooperation(self): self.assertEqual(len(rs.normalised_cooperation), rs.num_players) for i, row in enumerate(rs.normalised_cooperation): for j, col in enumerate(row): - self.assertAlmostEqual(col, self.expected_normalised_cooperation[i][j]) + self.assertAlmostEqual( + col, self.expected_normalised_cooperation[i][j] + ) def test_initial_cooperation_rate(self): rs = axl.ResultSet( @@ -401,7 +444,9 @@ def test_state_distribution(self): ) self.assertIsInstance(rs.state_distribution, list) self.assertEqual(len(rs.state_distribution), rs.num_players) - self.assertEqual(rs.state_distribution, self.expected_state_distribution) + self.assertEqual( + rs.state_distribution, self.expected_state_distribution + ) def test_state_normalised_distribution(self): rs = axl.ResultSet( @@ -446,7 +491,9 @@ def test_vengeful_cooperation(self): self.assertEqual(len(rs.vengeful_cooperation), rs.num_players) for i, row in enumerate(rs.vengeful_cooperation): for j, col in enumerate(row): - self.assertAlmostEqual(col, self.expected_vengeful_cooperation[i][j]) + self.assertAlmostEqual( + col, self.expected_vengeful_cooperation[i][j] + ) def test_cooperating_rating(self): rs = axl.ResultSet( @@ -454,7 +501,9 @@ def test_cooperating_rating(self): ) self.assertIsInstance(rs.cooperating_rating, list) self.assertEqual(len(rs.cooperating_rating), rs.num_players) - self.assertEqual(rs.cooperating_rating, self.expected_cooperating_rating) + self.assertEqual( + rs.cooperating_rating, self.expected_cooperating_rating + ) def test_good_partner_matrix(self): rs = axl.ResultSet( @@ -462,7 +511,9 @@ def test_good_partner_matrix(self): ) self.assertIsInstance(rs.good_partner_matrix, list) self.assertEqual(len(rs.good_partner_matrix), rs.num_players) - self.assertEqual(rs.good_partner_matrix, self.expected_good_partner_matrix) + self.assertEqual( + rs.good_partner_matrix, self.expected_good_partner_matrix + ) def test_good_partner_rating(self): rs = axl.ResultSet( @@ -470,7 +521,9 @@ def test_good_partner_rating(self): ) self.assertIsInstance(rs.good_partner_rating, list) self.assertEqual(len(rs.good_partner_rating), rs.num_players) - self.assertEqual(rs.good_partner_rating, self.expected_good_partner_rating) + self.assertEqual( + rs.good_partner_rating, self.expected_good_partner_rating + ) def test_eigenjesus_rating(self): rs = axl.ResultSet( @@ -502,7 +555,10 @@ def test_self_interaction_for_random_strategies(self): def test_equality(self): rs_sets = [ axl.ResultSet( - self.filename, self.players, self.repetitions, progress_bar=False + self.filename, + self.players, + self.repetitions, + progress_bar=False, ) for _ in range(2) ] @@ -532,25 +588,34 @@ def test_summarise(self): [float(player.Median_score) for player in sd], ranked_median_scores ) - ranked_cooperation_rating = [rs.cooperating_rating[i] for i in rs.ranking] + ranked_cooperation_rating = [ + rs.cooperating_rating[i] for i in rs.ranking + ] self.assertEqual( [float(player.Cooperation_rating) for player in sd], ranked_cooperation_rating, ) ranked_median_wins = [nanmedian(rs.wins[i]) for i in rs.ranking] - self.assertEqual([float(player.Wins) for player in sd], ranked_median_wins) + self.assertEqual( + [float(player.Wins) for player in sd], ranked_median_wins + ) ranked_initial_coop_rates = [ self.expected_initial_cooperation_rate[i] for i in rs.ranking ] self.assertEqual( - [float(player.Initial_C_rate) for player in sd], ranked_initial_coop_rates + [float(player.Initial_C_rate) for player in sd], + ranked_initial_coop_rates, ) for player in sd: self.assertEqual( - player.CC_rate + player.CD_rate + player.DC_rate + player.DD_rate, 1 + player.CC_rate + + player.CD_rate + + player.DC_rate + + player.DD_rate, + 1, ) for rate in [ player.CC_to_C_rate, @@ -727,13 +792,21 @@ def setUpClass(cls): # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ - [0, mean([13 / 5 for _ in range(3)]), mean([2 / 5 for _ in range(3)])], + [ + 0, + mean([13 / 5 for _ in range(3)]), + mean([2 / 5 for _ in range(3)]), + ], [mean([13 / 5 for _ in range(3)]), 0, 0], [mean([17 / 5 for _ in range(3)]), 0, 0], ] cls.expected_payoff_stddevs = [ - [0, std([13 / 5 for _ in range(3)]), std([2 / 5 for _ in range(3)])], + [ + 0, + std([13 / 5 for _ in range(3)]), + std([2 / 5 for _ in range(3)]), + ], [std([13 / 5 for _ in range(3)]), 0, 0], [std([17 / 5 for _ in range(3)]), 0, 0], ] @@ -741,7 +814,11 @@ def setUpClass(cls): cls.expected_cooperation = [[0, 9, 9], [9, 0, 0], [0, 0, 0]] cls.expected_normalised_cooperation = [ - [0, mean([3 / 5 for _ in range(3)]), mean([3 / 5 for _ in range(3)])], + [ + 0, + mean([3 / 5 for _ in range(3)]), + mean([3 / 5 for _ in range(3)]), + ], [mean([3 / 5 for _ in range(3)]), 0, 0], [0, 0, 0], ] @@ -760,7 +837,11 @@ def setUpClass(cls): cls.expected_good_partner_rating = [1.0, 1.0, 0.0] - cls.expected_eigenjesus_rating = [0.447213595499958, 0.894427190999916, 0.0] + cls.expected_eigenjesus_rating = [ + 0.447213595499958, + 0.894427190999916, + 0.0, + ] cls.expected_eigenmoses_rating = [ -0.32929277996907086, @@ -784,7 +865,11 @@ def setUpClass(cls): Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), Counter({(C, D): 0.6, (D, D): 0.4}), ], - [Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), Counter(), Counter()], + [ + Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), + Counter(), + Counter(), + ], [Counter({(D, C): 0.6, (D, D): 0.4}), Counter(), Counter()], ] @@ -813,7 +898,11 @@ def setUpClass(cls): Counter(), Counter(), ], - [Counter({((D, C), D): 1.0, ((D, D), D): 1.0}), Counter(), Counter()], + [ + Counter({((D, C), D): 1.0, ((D, D), D): 1.0}), + Counter(), + Counter(), + ], ] def test_match_lengths(self): @@ -870,7 +959,8 @@ def setUpClass(cls): cls.edges = [(0, 1), (2, 3)] cls.expected_match_lengths = [ - [[0, 5, 0, 0], [5, 0, 0, 0], [0, 0, 0, 5], [0, 0, 5, 0]] for _ in range(3) + [[0, 5, 0, 0], [5, 0, 0, 0], [0, 0, 0, 5], [0, 0, 5, 0]] + for _ in range(3) ] cls.expected_scores = [ @@ -912,10 +1002,30 @@ def setUpClass(cls): ] cls.expected_score_diffs = [ - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [5.0, 5.0, 5.0]], - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [-5.0, -5.0, -5.0], [0.0, 0.0, 0.0]], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + ], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + ], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [5.0, 5.0, 5.0], + ], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [-5.0, -5.0, -5.0], + [0.0, 0.0, 0.0], + ], ] cls.expected_payoff_diffs_means = [ @@ -1072,14 +1182,17 @@ def setUpClass(cls): cls.edges = [(0, 0), (1, 1), (2, 2), (3, 3)] cls.expected_match_lengths = [ - [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]] for _ in range(3) + [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]] + for _ in range(3) ] cls.expected_scores = [[0 for _ in range(3)] for _ in range(4)] cls.expected_wins = [[0 for _ in range(3)] for _ in range(4)] - cls.expected_normalised_scores = [[0 for _ in range(3)] for i in range(4)] + cls.expected_normalised_scores = [ + [0 for _ in range(3)] for i in range(4) + ] cls.expected_ranking = [0, 1, 2, 3] @@ -1107,7 +1220,9 @@ def setUpClass(cls): [[0.0 for _ in range(3)] for _ in range(4)] for _ in range(4) ] - cls.expected_payoff_diffs_means = [[0.0 for _ in range(4)] for _ in range(4)] + cls.expected_payoff_diffs_means = [ + [0.0 for _ in range(4)] for _ in range(4) + ] # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ @@ -1148,7 +1263,9 @@ def setUpClass(cls): cls.expected_cooperating_rating = [0.0 for _ in range(4)] - cls.expected_good_partner_matrix = [[0.0 for _ in range(4)] for _ in range(4)] + cls.expected_good_partner_matrix = [ + [0.0 for _ in range(4)] for _ in range(4) + ] cls.expected_good_partner_rating = [0.0 for _ in range(4)] @@ -1216,7 +1333,9 @@ class TestSummary(unittest.TestCase): """Separate test to check that summary always builds without failures""" @given( - tournament=tournaments(min_size=2, max_size=5, max_turns=5, max_repetitions=3) + tournament=tournaments( + min_size=2, max_size=5, max_turns=5, max_repetitions=3 + ) ) @settings(max_examples=5, deadline=None) def test_summarise_without_failure(self, tournament): @@ -1227,7 +1346,11 @@ def test_summarise_without_failure(self, tournament): for player in sd: # round for numerical error total_rate = round( - player.CC_rate + player.CD_rate + player.DC_rate + player.DD_rate, 3 + player.CC_rate + + player.CD_rate + + player.DC_rate + + player.DD_rate, + 3, ) self.assertTrue(total_rate in [0, 1]) self.assertTrue(0 <= player.Initial_C_rate <= 1) @@ -1239,9 +1362,13 @@ class TestCreateCounterDict(unittest.TestCase): def test_basic_use(self): key_map = {"Col 1": "Var 1", "Col 2": "Var 2"} df = pd.DataFrame( - {"Col 1": [10, 20, 30], "Col 2": [1, 2, 0]}, index=[[5, 6, 7], [1, 2, 3]] + {"Col 1": [10, 20, 30], "Col 2": [1, 2, 0]}, + index=[[5, 6, 7], [1, 2, 3]], + ) + self.assertEqual( + create_counter_dict(df, 6, 2, key_map), + Counter({"Var 1": 20, "Var 2": 2}), ) self.assertEqual( - create_counter_dict(df, 6, 2, key_map), Counter({"Var 1": 20, "Var 2": 2}) + create_counter_dict(df, 7, 3, key_map), Counter({"Var 1": 30}) ) - self.assertEqual(create_counter_dict(df, 7, 3, key_map), Counter({"Var 1": 30})) diff --git a/axelrod/tests/unit/test_strategy_transformers.py b/axelrod/tests/unit/test_strategy_transformers.py index 7539ae8cd..ba244d2fe 100644 --- a/axelrod/tests/unit/test_strategy_transformers.py +++ b/axelrod/tests/unit/test_strategy_transformers.py @@ -20,6 +20,7 @@ class CanNotPickle(axl.Cooperator): class TestTransformers(TestMatch): """Test generic transformer properties.""" + def test_player_can_be_pickled(self): player = axl.Cooperator() self.assertTrue(player_can_be_pickled(player)) @@ -55,9 +56,13 @@ def test_DecoratorReBuilder(self): args = decorator.args kwargs = decorator.kwargs.copy() - new_decorator = DecoratorReBuilder()(factory_args, args, kwargs, new_prefix) + new_decorator = DecoratorReBuilder()( + factory_args, args, kwargs, new_prefix + ) - self.assertEqual(decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)()) + self.assertEqual( + decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)() + ) def test_StrategyReBuilder_declared_class_with_name_prefix(self): player = CanNotPickle() @@ -151,9 +156,15 @@ def test_repr(self): str(InitialTransformer([D, D, C])(axl.Alternator)()), "Initial Alternator: [D, D, C]", ) - self.assertEqual(str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1") self.assertEqual( - str(MixedTransformer(0.3, (axl.Alternator, axl.Bully))(axl.Random)(0.1)), + str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1" + ) + self.assertEqual( + str( + MixedTransformer(0.3, (axl.Alternator, axl.Bully))(axl.Random)( + 0.1 + ) + ), "Mutated Random: 0.1: 0.3, ['Alternator', 'Bully']", ) @@ -181,7 +192,9 @@ def test_composition(self): p2 = axl.Cooperator() self.versus_test(p1, p2, [D, D, C, C, C, C, D, D], [C] * 8) - cls1 = FinalTransformer([D, D])(InitialTransformer([D, D])(axl.Cooperator)) + cls1 = FinalTransformer([D, D])( + InitialTransformer([D, D])(axl.Cooperator) + ) p1 = cls1() p2 = axl.Cooperator() self.versus_test(p1, p2, [D, D, C, C, C, C, D, D], [C] * 8) @@ -208,9 +221,11 @@ def deterministic_reclassifier(original_classifier, *args): return original_classifier StochasticTransformer = StrategyTransformerFactory( - generic_strategy_wrapper, reclassifier=stochastic_reclassifier) + generic_strategy_wrapper, reclassifier=stochastic_reclassifier + ) DeterministicTransformer = StrategyTransformerFactory( - generic_strategy_wrapper, reclassifier=deterministic_reclassifier) + generic_strategy_wrapper, reclassifier=deterministic_reclassifier + ) # Cooperator is not stochastic self.assertFalse(axl.Cooperator().classifier["stochastic"]) @@ -219,12 +234,16 @@ def deterministic_reclassifier(original_classifier, *args): self.assertTrue(player.classifier["stochastic"]) # Composing transforms should return it to not being stochastic - cls1 = compose_transformers(DeterministicTransformer(), StochasticTransformer()) + cls1 = compose_transformers( + DeterministicTransformer(), StochasticTransformer() + ) player = cls1(axl.Cooperator)() self.assertFalse(player.classifier["stochastic"]) # Explicit composition - player = DeterministicTransformer()(StochasticTransformer()(axl.Cooperator))() + player = DeterministicTransformer()( + StochasticTransformer()(axl.Cooperator) + )() self.assertFalse(player.classifier["stochastic"]) # Random is stochastic @@ -235,12 +254,16 @@ def deterministic_reclassifier(original_classifier, *args): self.assertFalse(player.classifier["stochastic"]) # Composing transforms should return it to being stochastic - cls1 = compose_transformers(StochasticTransformer(), DeterministicTransformer()) + cls1 = compose_transformers( + StochasticTransformer(), DeterministicTransformer() + ) player = cls1(axl.Random)() self.assertTrue(player.classifier["stochastic"]) # Explicit composition - player = StochasticTransformer()(DeterministicTransformer()(axl.Random))() + player = StochasticTransformer()( + DeterministicTransformer()(axl.Random) + )() self.assertTrue(player.classifier["stochastic"]) def test_nilpotency(self): @@ -316,16 +339,20 @@ def test_deadlock_breaks(self): axl.TitForTat(), InitialTransformer([D])(axl.TitForTat)(), [C, D, C, D], - [D, C, D, C]) + [D, C, D, C], + ) # Now let's use the transformer to break the deadlock to achieve # Mutual cooperation # self.versus_test( self.versus_test( axl.TitForTat(), - DeadlockBreakingTransformer()(InitialTransformer([D])(axl.TitForTat))(), + DeadlockBreakingTransformer()( + InitialTransformer([D])(axl.TitForTat) + )(), [C, D, C, C], - [D, C, C, C]) + [D, C, C, C], + ) class TestDualTransformer(TestMatch): @@ -377,10 +404,12 @@ def test_dual_transformer_simple_play_regression_test(self): DualTransformer()(axl.Cooperator) )() - self.versus_test(multiple_dual_transformers, - dual_transformer_not_first, - [D, D, D], - [D, D, D]) + self.versus_test( + multiple_dual_transformers, + dual_transformer_not_first, + [D, D, D], + [D, D, D], + ) def test_dual_transformer_multiple_interspersed_regression_test(self): """DualTransformer has failed when there were multiple DualTransformers. @@ -406,7 +435,9 @@ def test_final_transformer(self): p2 = FinalTransformer([D, D, D])(axl.Cooperator)() self.assertEqual(axl.Classifiers["makes_use_of"](p2), set(["length"])) self.assertEqual(axl.Classifiers["memory_depth"](p2), 3) - self.assertEqual(axl.Classifiers["makes_use_of"](axl.Cooperator()), set([])) + self.assertEqual( + axl.Classifiers["makes_use_of"](axl.Cooperator()), set([]) + ) self.versus_test(p1, p2, [C] * 8, [C, C, C, C, C, D, D, D], turns=8) def test_infinite_memory_depth_transformed(self): @@ -419,8 +450,9 @@ def test_final_transformer_unknown_length(self): """Tests the FinalTransformer when tournament length is not known.""" p1 = axl.Defector() p2 = FinalTransformer([D, D])(axl.Cooperator)() - self.versus_test(p1, p2, [D] * 6, [C] * 6, - match_attributes={"length": -1}) + self.versus_test( + p1, p2, [D] * 6, [C] * 6, match_attributes={"length": -1} + ) class TestFlipTransformer(TestMatch): @@ -449,8 +481,9 @@ def test_forgiving_transformer(self): p1 = ForgiverTransformer(0.5)(axl.Alternator)() p2 = axl.Defector() turns = 10 - self.versus_test(p1, p2, [C, D, C, C, D, C, C, D, C, D], [D] * turns, - seed=8) + self.versus_test( + p1, p2, [C, D, C, C, D, C, C, D, C, D], [D] * turns, seed=8 + ) def test_stochastic_values_classifier(self): p1 = ForgiverTransformer(0.5)(axl.Alternator)() @@ -473,7 +506,9 @@ def test_grudging1(self): def test_grudging2(self): p1 = InitialTransformer([C])(axl.Defector)() p2 = GrudgeTransformer(2)(axl.Cooperator)() - self.versus_test(p1, p2, [C, D, D, D, D, D, D, D], [C, C, C, C, D, D, D, D], seed=11) + self.versus_test( + p1, p2, [C, D, D, D, D, D, D, D], [C, C, C, C, D, D, D, D], seed=11 + ) class TestHistoryTrackingTransformer(TestMatch): @@ -510,8 +545,12 @@ def test_generic(self): Defector2 = transformer(axl.Defector) turns = 100 - self.versus_test(axl.Cooperator(), Cooperator2(), [C] * turns, [C] * turns) - self.versus_test(axl.Cooperator(), Defector2(), [C] * turns, [D] * turns) + self.versus_test( + axl.Cooperator(), Cooperator2(), [C] * turns, [C] * turns + ) + self.versus_test( + axl.Cooperator(), Defector2(), [C] * turns, [D] * turns + ) class TestInitialTransformer(TestMatch): @@ -684,7 +723,9 @@ def test_noisy_transformer(self): p1 = axl.Cooperator() p2 = NoisyTransformer(0.5)(axl.Cooperator)() self.assertTrue(axl.Classifiers["stochastic"](p2)) - self.versus_test(p1, p2, [C] * 10, [D, C, C, D, C, D, C, D, D, C], seed=1) + self.versus_test( + p1, p2, [C] * 10, [D, C, C, D, C, D, C, D, D, C], seed=1 + ) def test_noisy_transformation_stochastic(self): """Depending on the value of the noise parameter, the strategy may become stochastic @@ -722,7 +763,9 @@ def test_retailiating_cooperator_against_2TFT(self): TwoTitsForTat = RetaliationTransformer(2)(axl.Cooperator) p1 = TwoTitsForTat() p2 = axl.CyclerCCD() - self.versus_test(p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D]) + self.versus_test( + p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D] + ) class TestRetailiateUntilApologyTransformer(TestMatch): @@ -746,6 +789,7 @@ def test_retaliation_until_apology_stochastic(self): # Run the standard Player tests on some specifically transformed players + class TestNullInitialTransformedCooperator(TestPlayer): player = InitialTransformer([])(axl.Cooperator) name = "Initial Cooperator: []" @@ -786,7 +830,9 @@ class TestFinalTransformedCooperator(TestPlayer): class TestInitialFinalTransformedCooperator(TestPlayer): - player = InitialTransformer([D, D])(FinalTransformer([D, D, D])(axl.Cooperator)) + player = InitialTransformer([D, D])( + FinalTransformer([D, D, D])(axl.Cooperator) + ) name = "Initial Final Cooperator: [D, D, D]: [D, D]" expected_classifier = { "memory_depth": 3, @@ -799,7 +845,9 @@ class TestInitialFinalTransformedCooperator(TestPlayer): class TestFinalInitialTransformedCooperator(TestPlayer): - player = FinalTransformer([D, D])(InitialTransformer([D, D, D])(axl.Cooperator)) + player = FinalTransformer([D, D])( + InitialTransformer([D, D, D])(axl.Cooperator) + ) name = "Final Initial Cooperator: [D, D, D]: [D, D]" expected_classifier = { "memory_depth": 3, @@ -911,7 +959,9 @@ class TestMixed0(TestDefector): class TestMixed1(TestDefector): - name = "Mutated Cooperator: 1, " + name = ( + "Mutated Cooperator: 1, " + ) player = MixedTransformer(1, axl.Defector)(axl.Cooperator) expected_classifier = { "memory_depth": 0, @@ -996,7 +1046,9 @@ class TestJossAnnDual(TestPlayer): class TestJossAnnOverwriteClassifier(TestPlayer): name = "Joss-Ann Final Random: 0.5: [D, D]: (1.0, 0.0)" - player = JossAnnTransformer((1., 0.))(FinalTransformer([D, D])(axl.Random)) + player = JossAnnTransformer((1.0, 0.0))( + FinalTransformer([D, D])(axl.Random) + ) expected_classifier = { "memory_depth": 0, "stochastic": False, diff --git a/axelrod/tests/unit/test_strategy_utils.py b/axelrod/tests/unit/test_strategy_utils.py index b3a4f7ef0..5cade0dba 100644 --- a/axelrod/tests/unit/test_strategy_utils.py +++ b/axelrod/tests/unit/test_strategy_utils.py @@ -35,7 +35,9 @@ def test_no_cycle(self): history = [D, D, C, C, C] self.assertIsNone(detect_cycle(history)) - def test_regression_test_can_detect_cycle_that_is_repeated_exactly_once(self): + def test_regression_test_can_detect_cycle_that_is_repeated_exactly_once( + self, + ): self.assertEqual(detect_cycle([C, D, C, D]), (C, D)) self.assertEqual(detect_cycle([C, D, C, D, C]), (C, D)) @@ -53,11 +55,14 @@ def test_min_size_greater_than_two_times_history_tail_returns_none(self): def test_min_size_greater_than_two_times_max_size_has_no_effect(self): self.assertEqual( - detect_cycle([C, C, C, C, C, C, C, C], min_size=2, max_size=3), (C, C) + detect_cycle([C, C, C, C, C, C, C, C], min_size=2, max_size=3), + (C, C), ) def test_cycle_greater_than_max_size_returns_none(self): - self.assertEqual(detect_cycle([C, C, D] * 2, min_size=1, max_size=3), (C, C, D)) + self.assertEqual( + detect_cycle([C, C, D] * 2, min_size=1, max_size=3), (C, C, D) + ) self.assertIsNone(detect_cycle([C, C, D] * 2, min_size=1, max_size=2)) @@ -81,7 +86,9 @@ def test_strategies_with_countermeasures_return_their_countermeasures(self): inspector = axl.Cooperator() match = axl.Match((d_geller, inspector), turns=1) match.play() - self.assertEqual(inspect_strategy(inspector=inspector, opponent=d_geller), D) + self.assertEqual( + inspect_strategy(inspector=inspector, opponent=d_geller), D + ) self.assertEqual(d_geller.strategy(inspector), C) diff --git a/axelrod/tests/unit/test_tournament.py b/axelrod/tests/unit/test_tournament.py index f8cca4bb4..e73003398 100644 --- a/axelrod/tests/unit/test_tournament.py +++ b/axelrod/tests/unit/test_tournament.py @@ -41,7 +41,9 @@ test_edges = [(0, 1), (1, 2), (3, 4)] deterministic_strategies = [ - s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s()) + s + for s in axl.short_run_time_strategies + if not axl.Classifiers["stochastic"](s()) ] @@ -107,7 +109,9 @@ def test_init(self): noise=0.2, ) self.assertEqual(len(tournament.players), len(test_strategies)) - self.assertIsInstance(tournament.players[0].match_attributes["game"], axl.Game) + self.assertIsInstance( + tournament.players[0].match_attributes["game"], axl.Game + ) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertEqual(tournament.turns, self.test_turns) self.assertEqual(tournament.repetitions, 10) @@ -123,7 +127,9 @@ def test_init_with_match_attributes(self): ) mg = tournament.match_generator match_params = mg.build_single_match_params() - self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) + self.assertEqual( + match_params["match_attributes"], {"length": float("inf")} + ) def test_warning(self): tournament = axl.Tournament( @@ -275,7 +281,11 @@ def test_serial_play_with_different_game(self): # Test that a non default game is passed to the result set game = axl.Game(p=-1, r=-1, s=-1, t=-1) tournament = axl.Tournament( - name=self.test_name, players=self.players, game=game, turns=1, repetitions=1 + name=self.test_name, + players=self.players, + game=game, + turns=1, + repetitions=1, ) results = tournament.play(progress_bar=False) self.assertLessEqual(np.max(results.scores), 0) @@ -409,7 +419,9 @@ def test_progress_bar_play_parallel(self): # these two examples were identified by hypothesis. @example( tournament=axl.Tournament( - players=[axl.BackStabber(), axl.MindReader()], turns=2, repetitions=1, + players=[axl.BackStabber(), axl.MindReader()], + turns=2, + repetitions=1, ) ) @example( @@ -541,7 +553,9 @@ def test_n_workers(self): tournament._n_workers(processes=max_processes + 2), max_processes ) - @unittest.skipIf(cpu_count() < 2, "not supported on single processor machines") + @unittest.skipIf( + cpu_count() < 2, "not supported on single processor machines" + ) def test_2_workers(self): # This is a separate test with a skip condition because we # cannot guarantee that the tests will always run on a machine @@ -725,9 +739,13 @@ def test_write_to_csv_without_results(self): turns=2, repetitions=2, ) - tournament.play(filename=self.filename, progress_bar=False, build_results=False) + tournament.play( + filename=self.filename, progress_bar=False, build_results=False + ) df = pd.read_csv(self.filename) - path = pathlib.Path("test_outputs/expected_test_tournament_no_results.csv") + path = pathlib.Path( + "test_outputs/expected_test_tournament_no_results.csv" + ) expected_df = pd.read_csv(axl_filename(path)) self.assertTrue(df.equals(expected_df)) @@ -747,7 +765,7 @@ def test_seeding_equality(self, seed): game=self.game, turns=10, repetitions=100, - seed=seed + seed=seed, ) tournament2 = axl.Tournament( name=self.test_name, @@ -755,7 +773,7 @@ def test_seeding_equality(self, seed): game=self.game, turns=10, repetitions=100, - seed=seed + seed=seed, ) for _ in range(4): results1 = tournament1.play(processes=2) @@ -770,7 +788,7 @@ def test_seeding_inequality(self): game=self.game, turns=2, repetitions=2, - seed=0 + seed=0, ) tournament2 = axl.Tournament( name=self.test_name, @@ -778,7 +796,7 @@ def test_seeding_inequality(self): game=self.game, turns=2, repetitions=2, - seed=10 + seed=10, ) results1 = tournament1.play() results2 = tournament2.play() @@ -802,7 +820,9 @@ def test_init(self): prob_end=self.test_prob_end, noise=0.2, ) - self.assertEqual(tournament.match_generator.prob_end, tournament.prob_end) + self.assertEqual( + tournament.match_generator.prob_end, tournament.prob_end + ) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertIsNone(tournament.turns) @@ -821,7 +841,7 @@ def test_init(self): max_prob_end=0.9, min_repetitions=2, max_repetitions=4, - seed=100 + seed=100, ) ) @settings(max_examples=5, deadline=None) @@ -830,7 +850,7 @@ def test_init(self): players=[s() for s in test_strategies], prob_end=0.2, repetitions=test_repetitions, - seed=101 + seed=101, ) ) # These two examples are to make sure #465 is fixed. @@ -841,7 +861,7 @@ def test_init(self): players=[axl.BackStabber(), axl.MindReader()], prob_end=0.2, repetitions=1, - seed=102 + seed=102, ) ) @example( @@ -849,7 +869,7 @@ def test_init(self): players=[axl.ThueMorse(), axl.MindReader()], prob_end=0.2, repetitions=1, - seed=103 + seed=103, ) ) def test_property_serial_play(self, tournament): @@ -902,7 +922,9 @@ def test_init(self): seed=integers(min_value=0, max_value=4294967295), ) @settings(max_examples=5, deadline=None) - def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): + def test_complete_tournament( + self, strategies, turns, repetitions, noise, seed + ): """ A test to check that a spatial tournament on the complete multigraph gives the same results as the round robin. @@ -917,13 +939,20 @@ def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): # create a round robin tournament tournament = axl.Tournament( - players, repetitions=repetitions, turns=turns, noise=noise, - seed=seed + players, + repetitions=repetitions, + turns=turns, + noise=noise, + seed=seed, ) # create a complete spatial tournament spatial_tournament = axl.Tournament( - players, repetitions=repetitions, turns=turns, noise=noise, edges=edges, - seed=seed + players, + repetitions=repetitions, + turns=turns, + noise=noise, + edges=edges, + seed=seed, ) results = tournament.play(progress_bar=False) @@ -932,16 +961,23 @@ def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): self.assertEqual(results.ranked_names, spatial_results.ranked_names) self.assertEqual(results.num_players, spatial_results.num_players) self.assertEqual(results.repetitions, spatial_results.repetitions) - self.assertEqual(results.payoff_diffs_means, spatial_results.payoff_diffs_means) + self.assertEqual( + results.payoff_diffs_means, spatial_results.payoff_diffs_means + ) self.assertEqual(results.payoff_matrix, spatial_results.payoff_matrix) self.assertEqual(results.payoff_stddevs, spatial_results.payoff_stddevs) self.assertEqual(results.payoffs, spatial_results.payoffs) - self.assertEqual(results.cooperating_rating, spatial_results.cooperating_rating) + self.assertEqual( + results.cooperating_rating, spatial_results.cooperating_rating + ) self.assertEqual(results.cooperation, spatial_results.cooperation) self.assertEqual( - results.normalised_cooperation, spatial_results.normalised_cooperation + results.normalised_cooperation, + spatial_results.normalised_cooperation, + ) + self.assertEqual( + results.normalised_scores, spatial_results.normalised_scores ) - self.assertEqual(results.normalised_scores, spatial_results.normalised_scores) self.assertEqual( results.good_partner_matrix, spatial_results.good_partner_matrix ) @@ -961,7 +997,12 @@ def test_particular_tournament(self): edges = [(0, 2), (0, 3), (1, 2), (1, 3)] tournament = axl.Tournament(players, edges=edges) results = tournament.play(progress_bar=False) - expected_ranked_names = ["Cooperator", "Tit For Tat", "Grudger", "Defector"] + expected_ranked_names = [ + "Cooperator", + "Tit For Tat", + "Grudger", + "Defector", + ] self.assertEqual(results.ranked_names, expected_ranked_names) # Check that this tournament runs with noise @@ -1018,17 +1059,19 @@ def test_complete_tournament(self, strategies, prob_end, seed, reps): # create a prob end round robin tournament - tournament = axl.Tournament(players, prob_end=prob_end, repetitions=reps, - seed=seed) + tournament = axl.Tournament( + players, prob_end=prob_end, repetitions=reps, seed=seed + ) results = tournament.play(progress_bar=False) # create a complete spatial tournament # edges - edges = [(i, j) for i in range(len(players)) for j in range(i, len(players))] + edges = [ + (i, j) for i in range(len(players)) for j in range(i, len(players)) + ] spatial_tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=reps, edges=edges, - seed=seed + players, prob_end=prob_end, repetitions=reps, edges=edges, seed=seed ) spatial_results = spatial_tournament.play(progress_bar=False) self.assertEqual(results.match_lengths, spatial_results.match_lengths) @@ -1063,7 +1106,9 @@ def test_one_turn_tournament(self, tournament, seed): one_turn_results = tournament.play(progress_bar=False) self.assertEqual(prob_end_results.scores, one_turn_results.scores) self.assertEqual(prob_end_results.wins, one_turn_results.wins) - self.assertEqual(prob_end_results.cooperation, one_turn_results.cooperation) + self.assertEqual( + prob_end_results.cooperation, one_turn_results.cooperation + ) class TestHelperFunctions(unittest.TestCase): diff --git a/axelrod/tournament.py b/axelrod/tournament.py index 925fe1477..41d7d45f0 100644 --- a/axelrod/tournament.py +++ b/axelrod/tournament.py @@ -33,7 +33,7 @@ def __init__( noise: float = 0, edges: List[Tuple] = None, match_attributes: dict = None, - seed: int = None + seed: int = None, ) -> None: """ Parameters @@ -87,7 +87,7 @@ def __init__( noise=self.noise, edges=edges, match_attributes=match_attributes, - seed=self.seed + seed=self.seed, ) self._logger = logging.getLogger(__name__) @@ -232,7 +232,9 @@ def _get_file_objects(self, build_results=True): def _get_progress_bar(self): if self.use_progress_bar: - return tqdm.tqdm(total=self.match_generator.size, desc="Playing matches") + return tqdm.tqdm( + total=self.match_generator.size, desc="Playing matches" + ) return None def _write_interactions_to_file(self, results, writer): @@ -256,8 +258,14 @@ def _write_interactions_to_file(self, results, writer): ) = results for index, player_index in enumerate(index_pair): opponent_index = index_pair[index - 1] - row = [self.num_interactions, player_index, opponent_index, repetition, - str(self.players[player_index]), str(self.players[opponent_index])] + row = [ + self.num_interactions, + player_index, + opponent_index, + repetition, + str(self.players[player_index]), + str(self.players[opponent_index]), + ] history = actions_to_str([i[index] for i in interaction]) row.append(history) @@ -277,16 +285,24 @@ def _write_interactions_to_file(self, results, writer): for state in states: row.append(state_distribution[state]) for state in states: - row.append(state_to_action_distributions[index][(state, C)]) - row.append(state_to_action_distributions[index][(state, D)]) + row.append( + state_to_action_distributions[index][(state, C)] + ) + row.append( + state_to_action_distributions[index][(state, D)] + ) - row.append(int(cooperations[index] >= cooperations[index - 1])) + row.append( + int(cooperations[index] >= cooperations[index - 1]) + ) writer.writerow(row) repetition += 1 self.num_interactions += 1 - def _run_parallel(self, processes: int = 2, build_results: bool = True) -> bool: + def _run_parallel( + self, processes: int = 2, build_results: bool = True + ) -> bool: """ Run all matches in parallel @@ -349,7 +365,8 @@ def _start_workers( """ for worker in range(workers): process = Process( - target=self._worker, args=(work_queue, done_queue, build_results) + target=self._worker, + args=(work_queue, done_queue, build_results), ) work_queue.put("STOP") process.start() @@ -387,7 +404,9 @@ def _process_done_queue( _close_objects(out_file, progress_bar) return True - def _worker(self, work_queue: Queue, done_queue: Queue, build_results: bool = True): + def _worker( + self, work_queue: Queue, done_queue: Queue, build_results: bool = True + ): """ The work for each parallel sub-process to execute. @@ -455,13 +474,17 @@ def _calculate_results(self, interactions): turns = len(interactions) results.append(turns) - score_per_turns = iu.compute_final_score_per_turn(interactions, self.game) + score_per_turns = iu.compute_final_score_per_turn( + interactions, self.game + ) results.append(score_per_turns) score_diffs_per_turns = score_diffs[0] / turns, score_diffs[1] / turns results.append(score_diffs_per_turns) - initial_coops = tuple(map(bool, iu.compute_cooperations(interactions[:1]))) + initial_coops = tuple( + map(bool, iu.compute_cooperations(interactions[:1])) + ) results.append(initial_coops) cooperations = iu.compute_cooperations(interactions) diff --git a/docs/conf.py b/docs/conf.py index 72d0a774c..0e33e21c3 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -231,7 +231,13 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ("index", "Axelrod.tex", "Axelrod Documentation", "Vincent Knight", "manual") + ( + "index", + "Axelrod.tex", + "Axelrod Documentation", + "Vincent Knight", + "manual", + ) ] # The name of an image file (relative to this directory) to place at the top of @@ -259,7 +265,9 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [("index", "axelrod", "Axelrod Documentation", ["Vincent Knight"], 1)] +man_pages = [ + ("index", "axelrod", "Axelrod Documentation", ["Vincent Knight"], 1) +] # If true, show URL addresses after external links. # man_show_urls = False diff --git a/doctests.py b/doctests.py index 8bc74f50f..3ac377571 100644 --- a/doctests.py +++ b/doctests.py @@ -27,7 +27,8 @@ def load_tests(loader, tests, ignore): tests.addTests( doctest.DocFileSuite( # ELLIPSIS option tells doctest to ignore portions of the verification value. - os.path.join(root, f), optionflags=doctest.ELLIPSIS + os.path.join(root, f), + optionflags=doctest.ELLIPSIS, ) ) From d3ee0c862a656debf01fb6c6c1a36a00d8440fd8 Mon Sep 17 00:00:00 2001 From: "T.J. Gaffney" Date: Fri, 10 Apr 2020 03:52:07 -0700 Subject: [PATCH 05/10] Additional documentation on running black --- docs/tutorials/contributing/guidelines.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/tutorials/contributing/guidelines.rst b/docs/tutorials/contributing/guidelines.rst index 42fc0923d..ad40aeb9a 100644 --- a/docs/tutorials/contributing/guidelines.rst +++ b/docs/tutorials/contributing/guidelines.rst @@ -12,8 +12,11 @@ The project follows the following guidelines: `_ which includes **using descriptive variable names**. 3. Code Format: Use the `Black formatter `_ to format - all code and the `isort utility `_ to - sort import statements. + all code and the `isort utility `_ to + sort import statements. You can run black on all code with:: + + $ python -m black -l 80 . + 4. Commits: Please try to use commit messages that give a meaningful history for anyone using git's log features. Try to use messages that complete sentence, "This commit will..." There is some excellent guidance on the subject From 10ab222b21151ac3cca703de6993334ecee74712 Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 11:28:58 +0100 Subject: [PATCH 06/10] Revert "Run black" This reverts commit c50a6e3427e2731167907e44610ace3c6d7c7c20. --- .prepare-commit-msg.py | 4 +- axelrod/evolvable_player.py | 3 +- axelrod/fingerprint.py | 40 +- axelrod/game.py | 11 +- axelrod/graph.py | 5 +- axelrod/history.py | 8 +- axelrod/interaction_utils.py | 3 +- axelrod/match.py | 5 +- axelrod/match_generator.py | 2 +- axelrod/moran.py | 46 +-- axelrod/player.py | 24 +- axelrod/plot.py | 8 +- axelrod/random_.py | 12 +- axelrod/result_set.py | 71 +--- axelrod/strategies/__init__.py | 4 +- axelrod/strategies/_filters.py | 18 +- axelrod/strategies/adaptor.py | 20 +- axelrod/strategies/ann.py | 89 ++-- axelrod/strategies/axelrod_first.py | 71 +--- axelrod/strategies/axelrod_second.py | 99 +---- axelrod/strategies/backstabber.py | 4 +- axelrod/strategies/bush_mosteller.py | 21 +- axelrod/strategies/calculator.py | 5 +- axelrod/strategies/cycler.py | 30 +- axelrod/strategies/dbs.py | 32 +- axelrod/strategies/defector.py | 5 +- axelrod/strategies/finite_state_machines.py | 95 ++--- axelrod/strategies/gambler.py | 10 +- axelrod/strategies/gobymajority.py | 4 +- axelrod/strategies/grudger.py | 2 +- axelrod/strategies/hmm.py | 148 ++----- axelrod/strategies/human.py | 12 +- axelrod/strategies/hunter.py | 14 +- axelrod/strategies/lookerup.py | 109 ++--- axelrod/strategies/memoryone.py | 8 +- axelrod/strategies/memorytwo.py | 25 +- axelrod/strategies/meta.py | 16 +- axelrod/strategies/prober.py | 2 +- axelrod/strategies/punisher.py | 8 +- axelrod/strategies/qlearner.py | 20 +- axelrod/strategies/shortmem.py | 2 +- axelrod/strategies/titfortat.py | 9 +- axelrod/strategies/zero_determinant.py | 20 +- axelrod/strategy_transformers.py | 31 +- axelrod/tests/integration/test_filtering.py | 16 +- axelrod/tests/integration/test_matches.py | 8 +- axelrod/tests/integration/test_names.py | 2 +- axelrod/tests/integration/test_tournament.py | 26 +- axelrod/tests/property.py | 85 ++-- axelrod/tests/strategies/test_adaptive.py | 14 +- axelrod/tests/strategies/test_ann.py | 5 +- axelrod/tests/strategies/test_apavlov.py | 105 +---- .../tests/strategies/test_axelrod_first.py | 159 ++----- .../tests/strategies/test_axelrod_second.py | 391 +++++------------- axelrod/tests/strategies/test_backstabber.py | 10 +- .../strategies/test_better_and_better.py | 12 +- axelrod/tests/strategies/test_calculator.py | 54 +-- axelrod/tests/strategies/test_cooperator.py | 10 +- axelrod/tests/strategies/test_cycler.py | 16 +- axelrod/tests/strategies/test_darwin.py | 12 +- axelrod/tests/strategies/test_dbs.py | 24 +- axelrod/tests/strategies/test_doubler.py | 4 +- .../tests/strategies/test_evolvable_player.py | 36 +- .../strategies/test_finite_state_machines.py | 107 ++--- axelrod/tests/strategies/test_forgiver.py | 20 +- axelrod/tests/strategies/test_gambler.py | 44 +- axelrod/tests/strategies/test_geller.py | 22 +- axelrod/tests/strategies/test_gobymajority.py | 38 +- axelrod/tests/strategies/test_grudger.py | 39 +- axelrod/tests/strategies/test_grumpy.py | 20 +- axelrod/tests/strategies/test_hmm.py | 22 +- axelrod/tests/strategies/test_human.py | 4 +- axelrod/tests/strategies/test_inverse.py | 18 +- axelrod/tests/strategies/test_lookerup.py | 55 +-- axelrod/tests/strategies/test_memoryone.py | 81 +--- axelrod/tests/strategies/test_memorytwo.py | 31 +- axelrod/tests/strategies/test_meta.py | 70 +--- axelrod/tests/strategies/test_negation.py | 4 +- axelrod/tests/strategies/test_player.py | 58 +-- axelrod/tests/strategies/test_prober.py | 31 +- axelrod/tests/strategies/test_qlearner.py | 32 +- axelrod/tests/strategies/test_rand.py | 8 +- axelrod/tests/strategies/test_retaliate.py | 12 +- .../tests/strategies/test_revised_downing.py | 1 - axelrod/tests/strategies/test_selfsteem.py | 31 +- axelrod/tests/strategies/test_shortmem.py | 22 +- axelrod/tests/strategies/test_stalker.py | 13 +- axelrod/tests/strategies/test_titfortat.py | 152 ++----- .../tests/strategies/test_worse_and_worse.py | 3 +- .../tests/strategies/test_zero_determinant.py | 83 +--- axelrod/tests/unit/test_classification.py | 47 +-- ...est_compute_finite_state_machine_memory.py | 2 +- .../tests/unit/test_deterministic_cache.py | 4 +- axelrod/tests/unit/test_filters.py | 38 +- axelrod/tests/unit/test_fingerprint.py | 101 ++--- axelrod/tests/unit/test_graph.py | 127 ++---- axelrod/tests/unit/test_history.py | 12 +- axelrod/tests/unit/test_interaction_utils.py | 81 +--- axelrod/tests/unit/test_match.py | 26 +- axelrod/tests/unit/test_match_generator.py | 28 +- axelrod/tests/unit/test_moran.py | 100 ++--- axelrod/tests/unit/test_pickling.py | 65 +-- axelrod/tests/unit/test_plot.py | 51 +-- axelrod/tests/unit/test_property.py | 16 +- axelrod/tests/unit/test_resultset.py | 213 ++-------- .../tests/unit/test_strategy_transformers.py | 116 ++---- axelrod/tests/unit/test_strategy_utils.py | 15 +- axelrod/tests/unit/test_tournament.py | 111 ++--- axelrod/tournament.py | 49 +-- docs/conf.py | 12 +- doctests.py | 3 +- 111 files changed, 1138 insertions(+), 3264 deletions(-) diff --git a/.prepare-commit-msg.py b/.prepare-commit-msg.py index dac608fd9..780f8594b 100755 --- a/.prepare-commit-msg.py +++ b/.prepare-commit-msg.py @@ -41,9 +41,7 @@ if branch.startswith(issue_prefix): issue_number = re.match("%s(.*)" % issue_prefix, branch).group(1) - print( - "prepare-commit-msg: Prepending [#%s] to commit message" % issue_number - ) + print("prepare-commit-msg: Prepending [#%s] to commit message" % issue_number) with open(commit_msg_filepath, "r+") as f: content = f.read() diff --git a/axelrod/evolvable_player.py b/axelrod/evolvable_player.py index ce32bf0e5..7dd4073b3 100644 --- a/axelrod/evolvable_player.py +++ b/axelrod/evolvable_player.py @@ -7,7 +7,6 @@ class InsufficientParametersError(Exception): """Error indicating that insufficient parameters were specified to initialize an Evolvable Player.""" - def __init__(self, *args): super().__init__(*args) @@ -50,7 +49,7 @@ def create_new(self, **kwargs): def serialize_parameters(self): """Serialize parameters.""" pickled = dumps(self.init_kwargs) # bytes - s = base64.b64encode(pickled).decode("utf8") # string + s = base64.b64encode(pickled).decode('utf8') # string return s @classmethod diff --git a/axelrod/fingerprint.py b/axelrod/fingerprint.py index 8ef34d60b..264856a15 100644 --- a/axelrod/fingerprint.py +++ b/axelrod/fingerprint.py @@ -42,7 +42,7 @@ def _create_points(step: float, progress_bar: bool = True) -> List[Point]: num = int((1 / step) // 1) + 1 if progress_bar: - p_bar = tqdm.tqdm(total=num ** 2, desc="Generating points") + p_bar = tqdm.tqdm(total=num**2, desc="Generating points") points = [] for x in np.linspace(0, 1, num): @@ -88,8 +88,8 @@ def _create_jossann(point: Point, probe: Any) -> Player: if x + y >= 1: joss_ann = DualTransformer()( - JossAnnTransformer((1 - x, 1 - y))(probe_class) - )(**init_kwargs) + JossAnnTransformer((1 - x, 1 - y))( + probe_class))(**init_kwargs) else: joss_ann = JossAnnTransformer((x, y))(probe_class)(**init_kwargs) return joss_ann @@ -177,10 +177,7 @@ def _generate_data(interactions: dict, points: list, edges: list) -> dict: """ edge_scores = [ np.mean( - [ - compute_final_score_per_turn(scores)[0] - for scores in interactions[edge] - ] + [compute_final_score_per_turn(scores)[0] for scores in interactions[edge]] ) for edge in edges ] @@ -218,9 +215,7 @@ def _reshape_data(data: dict, points: list, size: int) -> np.ndarray: class AshlockFingerprint(object): def __init__( - self, - strategy: Union[type, Player], - probe: Union[type, Player] = axl.TitForTat, + self, strategy: Union[type, Player], probe: Union[type, Player] = axl.TitForTat ) -> None: """ Parameters @@ -282,7 +277,7 @@ def fingerprint( processes: int = None, filename: str = None, progress_bar: bool = True, - seed: int = None, + seed: int = None ) -> dict: """Build and play the spatial tournament. @@ -328,11 +323,8 @@ def fingerprint( self.step = step self.spatial_tournament = axl.Tournament( - tourn_players, - turns=turns, - repetitions=repetitions, - edges=edges, - seed=seed, + tourn_players, turns=turns, repetitions=repetitions, edges=edges, + seed=seed ) self.spatial_tournament.play( build_results=False, @@ -440,7 +432,7 @@ def fingerprint( processes: int = None, filename: str = None, progress_bar: bool = True, - seed: int = None, + seed: int = None ) -> np.array: """Creates a spatial tournament to run the necessary matches to obtain fingerprint data. @@ -487,7 +479,7 @@ def fingerprint( turns=turns, noise=noise, repetitions=repetitions, - seed=seed, + seed=seed ) tournament.play( filename=filename, @@ -524,9 +516,7 @@ def analyse_cooperation_ratio(filename): opponent in each turn. The ith row corresponds to the ith opponent and the jth column the jth turn. """ - did_c = np.vectorize( - lambda actions: [int(action == "C") for action in actions] - ) + did_c = np.vectorize(lambda actions: [int(action == "C") for action in actions]) cooperation_rates = {} df = dd.read_csv(filename) @@ -535,10 +525,7 @@ def analyse_cooperation_ratio(filename): df = df[df["Player index"] == 0][["Opponent index", "Actions"]] for _, row in df.iterrows(): - opponent_index, player_history = ( - row["Opponent index"], - row["Actions"], - ) + opponent_index, player_history = row["Opponent index"], row["Actions"] if opponent_index in cooperation_rates: cooperation_rates[opponent_index].append(did_c(player_history)) else: @@ -603,8 +590,7 @@ def plot( if display_names: plt.yticks( - range(len(self.opponents)), - [str(player) for player in self.opponents], + range(len(self.opponents)), [str(player) for player in self.opponents] ) else: plt.yticks([0, len(self.opponents) - 1], [0, 1]) diff --git a/axelrod/game.py b/axelrod/game.py index af30aca29..1c3278275 100644 --- a/axelrod/game.py +++ b/axelrod/game.py @@ -16,9 +16,7 @@ class Game(object): The numerical score attribute to all combinations of action pairs. """ - def __init__( - self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1 - ) -> None: + def __init__(self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1) -> None: """Create a new game object. Parameters @@ -32,12 +30,7 @@ def __init__( p: int or float Score obtained by both player for mutual defection. """ - self.scores = { - (C, C): (r, r), - (D, D): (p, p), - (C, D): (s, t), - (D, C): (t, s), - } + self.scores = {(C, C): (r, r), (D, D): (p, p), (C, D): (s, t), (D, C): (t, s)} def RPST(self) -> Tuple[Score, Score, Score, Score]: """Returns game matrix values in Press and Dyson notation.""" diff --git a/axelrod/graph.py b/axelrod/graph.py index 6b91677b0..9f41bde4d 100644 --- a/axelrod/graph.py +++ b/axelrod/graph.py @@ -155,9 +155,8 @@ def attached_complete_graphs(length, loops=True, directed=False): for cluster in range(2): for i in range(length): for j in range(i + 1, length): - edges.append( - ("{}:{}".format(cluster, i), "{}:{}".format(cluster, j)) - ) + edges.append(("{}:{}".format(cluster, i), + "{}:{}".format(cluster, j))) # Attach at one node edges.append(("0:0", "1:0")) graph = Graph(directed=directed, edges=edges) diff --git a/axelrod/history.py b/axelrod/history.py index aea7b1f64..9c0ff2ef1 100644 --- a/axelrod/history.py +++ b/axelrod/history.py @@ -82,9 +82,7 @@ def __eq__(self, other): if isinstance(other, list): return self._plays == other elif isinstance(other, History): - return ( - self._plays == other._plays and self._coplays == other._coplays - ) + return self._plays == other._plays and self._coplays == other._coplays raise TypeError("Cannot compare types.") def __getitem__(self, key): @@ -123,9 +121,7 @@ def __init__(self, memory_depth, plays=None, coplays=None): def flip_plays(self): """Creates a flipped plays history for use with DualTransformer.""" flipped_plays = [action.flip() for action in self._plays] - return self.__class__( - self.memory_depth, plays=flipped_plays, coplays=self._coplays - ) + return self.__class__(self.memory_depth, plays=flipped_plays, coplays=self._coplays) def append(self, play, coplay): """Appends a new (play, coplay) pair an updates metadata for diff --git a/axelrod/interaction_utils.py b/axelrod/interaction_utils.py index 220053ee3..82159f94c 100644 --- a/axelrod/interaction_utils.py +++ b/axelrod/interaction_utils.py @@ -32,8 +32,7 @@ def compute_final_score(interactions, game=None): return None final_score = tuple( - sum([score[player_index] for score in scores]) - for player_index in [0, 1] + sum([score[player_index] for score in scores]) for player_index in [0, 1] ) return final_score diff --git a/axelrod/match.py b/axelrod/match.py index 19c83abd9..907e85f44 100644 --- a/axelrod/match.py +++ b/axelrod/match.py @@ -29,7 +29,7 @@ def __init__( noise=0, match_attributes=None, reset=True, - seed=None, + seed=None ): """ Parameters @@ -193,8 +193,7 @@ def play(self): result = [] for _ in range(turns): plays = self.simultaneous_play( - self.players[0], self.players[1], self.noise - ) + self.players[0], self.players[1], self.noise) result.append(plays) if self._cache_update_required: diff --git a/axelrod/match_generator.py b/axelrod/match_generator.py index 37038e7b5..c0ad188bd 100644 --- a/axelrod/match_generator.py +++ b/axelrod/match_generator.py @@ -12,7 +12,7 @@ def __init__( prob_end=None, edges=None, match_attributes=None, - seed=None, + seed=None ): """ A class to generate matches. This is used by the Tournament class which diff --git a/axelrod/moran.py b/axelrod/moran.py index af235e1d9..3c9876c40 100644 --- a/axelrod/moran.py +++ b/axelrod/moran.py @@ -28,7 +28,7 @@ def __init__( fitness_transformation: Callable = None, mutation_method="transition", stop_on_fixation=True, - seed=None, + seed=None ) -> None: """ An agent based Moran process class. In each round, each player plays a @@ -93,9 +93,7 @@ def __init__( if m in ["atomic", "transition"]: self.mutation_method = m else: - raise ValueError( - "Invalid mutation method {}".format(mutation_method) - ) + raise ValueError("Invalid mutation method {}".format(mutation_method)) assert (mutation_rate >= 0) and (mutation_rate <= 1) assert (noise >= 0) and (noise <= 1) mode = mode.lower() @@ -129,9 +127,7 @@ def __init__( d[str(p)] = p mutation_targets = dict() for key in sorted(keys): - mutation_targets[key] = [ - v for (k, v) in sorted(d.items()) if k != key - ] + mutation_targets[key] = [v for (k, v) in sorted(d.items()) if k != key] self.mutation_targets = mutation_targets if interaction_graph is None: @@ -150,18 +146,14 @@ def __init__( self.fitness_transformation = fitness_transformation # Map players to graph vertices self.locations = sorted(interaction_graph.vertices) - self.index = dict( - zip(sorted(interaction_graph.vertices), range(len(players))) - ) + self.index = dict(zip(sorted(interaction_graph.vertices), range(len(players)))) self.fixated = self.fixation_check() def set_players(self) -> None: """Copy the initial players into the first population, setting seeds as needed.""" self.players = [] for player in self.initial_players: - if (self.mutation_method == "atomic") and issubclass( - player.__class__, EvolvablePlayer - ): + if (self.mutation_method == "atomic") and issubclass(player.__class__, EvolvablePlayer): # For reproducibility, we generate random seeds for evolvable players. seed = next(self._bulk_random) new_player = player.create_new(seed=seed) @@ -171,9 +163,8 @@ def set_players(self) -> None: self.players.append(player) self.populations = [self.population_distribution()] - def fitness_proportionate_selection( - self, scores: List, fitness_transformation: Callable = None - ) -> int: + def fitness_proportionate_selection(self, + scores: List, fitness_transformation: Callable = None) -> int: """Randomly selects an individual proportionally to score. Parameters @@ -209,9 +200,7 @@ def mutate(self, index: int) -> Player: if self.mutation_method == "atomic": if not issubclass(self.players[index].__class__, EvolvablePlayer): - raise TypeError( - "Player is not evolvable. Use a subclass of EvolvablePlayer." - ) + raise TypeError("Player is not evolvable. Use a subclass of EvolvablePlayer.") return self.players[index].mutate() # Assuming mutation_method == "transition" @@ -248,9 +237,7 @@ def death(self, index: int = None) -> int: # Select locally # index is not None in this case vertex = self._random.choice( - sorted( - self.reproduction_graph.out_vertices(self.locations[index]) - ) + sorted(self.reproduction_graph.out_vertices(self.locations[index])) ) i = self.index[vertex] return i @@ -383,7 +370,7 @@ def score_all(self) -> List: noise=self.noise, game=self.game, deterministic_cache=self.deterministic_cache, - seed=next(self._bulk_random), + seed=next(self._bulk_random) ) match.play() match_scores = match.final_score_per_turn() @@ -497,11 +484,8 @@ class ApproximateMoranProcess(MoranProcess): """ def __init__( - self, - players: List[Player], - cached_outcomes: dict, - mutation_rate: float = 0, - seed: Optional[int] = None, + self, players: List[Player], cached_outcomes: dict, mutation_rate: float = 0, + seed: Optional[int] = None ) -> None: """ Parameters @@ -519,7 +503,7 @@ def __init__( noise=0, deterministic_cache=None, mutation_rate=mutation_rate, - seed=seed, + seed=seed ) self.cached_outcomes = cached_outcomes @@ -545,9 +529,7 @@ def score_all(self) -> List: scores = [0] * N for i in range(N): for j in range(i + 1, N): - player_names = tuple( - [str(self.players[i]), str(self.players[j])] - ) + player_names = tuple([str(self.players[i]), str(self.players[j])]) cached_score = self._get_scores_from_cache(player_names) scores[i] += cached_score[0] scores[j] += cached_score[1] diff --git a/axelrod/player.py b/axelrod/player.py index 87c08ddb8..6b7370be2 100644 --- a/axelrod/player.py +++ b/axelrod/player.py @@ -59,7 +59,6 @@ def _post_init(self): See here to learn more: https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ """ - def __call__(cls, *args, **kwargs): # This calls cls.__new__ and cls.__init__ obj = type.__call__(cls, *args, **kwargs) @@ -131,9 +130,7 @@ def __eq__(self, other): if self.__repr__() != other.__repr__(): return False - for attribute in set( - list(self.__dict__.keys()) + list(other.__dict__.keys()) - ): + for attribute in set(list(self.__dict__.keys()) + list(other.__dict__.keys())): value = getattr(self, attribute, None) other_value = getattr(other, attribute, None) @@ -151,20 +148,14 @@ def __eq__(self, other): ): # Split the original generator so it is not touched generator, original_value = itertools.tee(value) - other_generator, original_other_value = itertools.tee( - other_value - ) + other_generator, original_other_value = itertools.tee(other_value) if isinstance(value, types.GeneratorType): setattr(self, attribute, (ele for ele in original_value)) - setattr( - other, attribute, (ele for ele in original_other_value) - ) + setattr(other, attribute, (ele for ele in original_other_value)) else: setattr(self, attribute, itertools.cycle(original_value)) - setattr( - other, attribute, itertools.cycle(original_other_value) - ) + setattr(other, attribute, itertools.cycle(original_other_value)) for _ in range(200): try: @@ -199,8 +190,7 @@ def set_seed(self, seed): if seed is None: warnings.warn( "Initializing player with seed from Axelrod module random number generator. " - "Results may not be seed reproducible." - ) + "Results may not be seed reproducible.") self._seed = _module_random.random_seed_int() else: self._seed = seed @@ -211,9 +201,7 @@ def __repr__(self): Appends the `__init__` parameters to the strategy's name.""" name = self.name prefix = ": " - gen = ( - value for value in self.init_kwargs.values() if value is not None - ) + gen = (value for value in self.init_kwargs.values() if value is not None) for value in gen: try: if issubclass(value, Player): diff --git a/axelrod/plot.py b/axelrod/plot.py index feacae984..f56cc9f4f 100644 --- a/axelrod/plot.py +++ b/axelrod/plot.py @@ -97,9 +97,7 @@ def _winplot_dataset(self): # Sort wins by median wins = self.result_set.wins medians = map(median, wins) - medians = sorted( - [(m, i) for (i, m) in enumerate(medians)], reverse=True - ) + medians = sorted([(m, i) for (i, m) in enumerate(medians)], reverse=True) # Reorder and grab names wins = [wins[x[-1]] for x in medians] ranked_names = [str(self.players[x[-1]]) for x in medians] @@ -326,9 +324,7 @@ def save_all_plots( pbar = tqdm.tqdm(total=total, desc="Obtaining plots") for method, name in plots: - f = getattr(self, method)( - title="{} - {}".format(title_prefix, name) - ) + f = getattr(self, method)(title="{} - {}".format(title_prefix, name)) path = pathlib.Path("{}_{}.{}".format(prefix, method, filetype)) f.savefig(axl_filename(path)) plt.close(f) diff --git a/axelrod/random_.py b/axelrod/random_.py index 73289ff8f..23949c93e 100644 --- a/axelrod/random_.py +++ b/axelrod/random_.py @@ -11,7 +11,6 @@ class RandomGenerator(object): """Container around a random number generator. Enables reproducibility of player behavior, matches, and tournaments.""" - def __init__(self, seed: Optional[int] = None): # _random is the internal object that generators random values self._random = RandomState() @@ -29,7 +28,7 @@ def randint(self, *args, **kwargs): return self._random.randint(*args, **kwargs) def random_seed_int(self) -> int: - return self.randint(low=0, high=2 ** 32 - 1, dtype="uint64") + return self.randint(low=0, high=2**32-1, dtype="uint64") def choice(self, *args, **kwargs): return self._random.choice(*args, **kwargs) @@ -119,8 +118,7 @@ class BulkRandomGenerator(object): """Bulk generator of random integers for tournament seeding and reproducibility. Bulk generation of random values is more efficient. Use this class like a generator.""" - - def __init__(self, seed=None, batch_size: int = 1000): + def __init__(self, seed=None, batch_size:int = 1000): self._random_generator = RandomState() self._random_generator.seed(seed) self._ints = None @@ -132,8 +130,10 @@ def _fill_ints(self): # Generate more random values. Store as a list since generators # cannot be pickled. self._ints = self._random_generator.randint( - low=0, high=2 ** 32 - 1, size=self._batch_size, dtype="uint64" - ) + low=0, + high=2**32 - 1, + size=self._batch_size, + dtype="uint64") self._index = 0 def __next__(self): diff --git a/axelrod/result_set.py b/axelrod/result_set.py index ee6a515de..65353cc6d 100644 --- a/axelrod/result_set.py +++ b/axelrod/result_set.py @@ -117,15 +117,9 @@ def _reshape_out( alternative=0, ) - self.wins = self._reshape_two_dim_list( - sum_per_player_repetition_df["Win"] - ) - self.scores = self._reshape_two_dim_list( - sum_per_player_repetition_df["Score"] - ) - self.normalised_scores = self._reshape_two_dim_list( - normalised_scores_series - ) + self.wins = self._reshape_two_dim_list(sum_per_player_repetition_df["Win"]) + self.scores = self._reshape_two_dim_list(sum_per_player_repetition_df["Score"]) + self.normalised_scores = self._reshape_two_dim_list(normalised_scores_series) self.cooperation = self._build_cooperation( sum_per_player_opponent_df["Cooperation count"] @@ -138,9 +132,7 @@ def _reshape_out( self.state_distribution = self._build_state_distribution( sum_per_player_opponent_df[columns] ) - self.normalised_state_distribution = ( - self._build_normalised_state_distribution() - ) + self.normalised_state_distribution = self._build_normalised_state_distribution() columns = [ "CC to C count", @@ -174,9 +166,7 @@ def _reshape_out( self.ranked_names = self._build_ranked_names() self.payoff_matrix = self._build_summary_matrix(self.payoffs) - self.payoff_stddevs = self._build_summary_matrix( - self.payoffs, func=np.std - ) + self.payoff_stddevs = self._build_summary_matrix(self.payoffs, func=np.std) self.payoff_diffs_means = self._build_payoff_diffs_means() self.cooperating_rating = self._build_cooperating_rating() @@ -276,9 +266,7 @@ def _build_good_partner_matrix(self, good_partner_series): # interactions. row.append(0) else: - row.append( - good_partner_dict.get((player_index, opponent_index), 0) - ) + row.append(good_partner_dict.get((player_index, opponent_index), 0)) good_partner_matrix.append(row) return good_partner_matrix @@ -346,17 +334,13 @@ def _build_normalised_state_distribution(self): for counter in player: total = sum(counter.values()) counters.append( - Counter( - {key: value / total for key, value in counter.items()} - ) + Counter({key: value / total for key, value in counter.items()}) ) normalised_state_distribution.append(counters) return normalised_state_distribution @update_progress_bar - def _build_state_to_action_distribution( - self, state_to_action_distribution_series - ): + def _build_state_to_action_distribution(self, state_to_action_distribution_series): state_to_action_key_map = { "CC to C count": ((C, C), C), "CC to D count": ((C, C), D), @@ -412,12 +396,8 @@ def _build_normalised_state_to_action_distribution(self): return normalised_state_to_action_distribution @update_progress_bar - def _build_initial_cooperation_count( - self, initial_cooperation_count_series - ): - initial_cooperation_count_dict = ( - initial_cooperation_count_series.to_dict() - ) + def _build_initial_cooperation_count(self, initial_cooperation_count_series): + initial_cooperation_count_dict = initial_cooperation_count_series.to_dict() initial_cooperation_count = [ initial_cooperation_count_dict.get(player_index, 0) for player_index in range(self.num_players) @@ -447,8 +427,7 @@ def _build_initial_cooperation_rate(self, interactions_series): warnings.simplefilter("ignore") initial_cooperation_rate = list( np.nan_to_num( - np.array(self.initial_cooperation_count) - / interactions_array + np.array(self.initial_cooperation_count) / interactions_array ) ) return initial_cooperation_rate @@ -474,9 +453,7 @@ def _build_eigenmoses_rating(self): The eigenmoses rating as defined in: http://www.scottaaronson.com/morality.pdf """ - eigenvector, eigenvalue = eigen.principal_eigenvector( - self.vengeful_cooperation - ) + eigenvector, eigenvalue = eigen.principal_eigenvector(self.vengeful_cooperation) return eigenvector.tolist() @@ -600,9 +577,7 @@ def _build_tasks(self, df): ] sum_per_player_opponent_task = df.groupby(groups)[columns].sum() - ignore_self_interactions_task = ( - df["Player index"] != df["Opponent index"] - ) + ignore_self_interactions_task = df["Player index"] != df["Opponent index"] adf = df[ignore_self_interactions_task] groups = ["Player index", "Repetition"] @@ -616,9 +591,7 @@ def _build_tasks(self, df): groups = ["Player index"] column = "Initial cooperation" initial_cooperation_count_task = adf.groupby(groups)[column].sum() - interactions_count_task = adf.groupby("Player index")[ - "Player index" - ].count() + interactions_count_task = adf.groupby("Player index")["Player index"].count() return ( mean_per_reps_player_opponent_task, @@ -669,12 +642,8 @@ def list_equal_with_nans(v1: List[float], v2: List[float]) -> bool: self.cooperating_rating == other.cooperating_rating, self.good_partner_matrix == other.good_partner_matrix, self.good_partner_rating == other.good_partner_rating, - list_equal_with_nans( - self.eigenmoses_rating, other.eigenmoses_rating - ), - list_equal_with_nans( - self.eigenjesus_rating, other.eigenjesus_rating - ), + list_equal_with_nans(self.eigenmoses_rating, other.eigenmoses_rating), + list_equal_with_nans(self.eigenjesus_rating, other.eigenjesus_rating), ] ) @@ -744,9 +713,7 @@ def summarise(self): rates = [] for state in states: counts = [ - counter[(state, C)] - for counter in player - if counter[(state, C)] > 0 + counter[(state, C)] for counter in player if counter[(state, C)] > 0 ] if len(counts) > 0: @@ -769,9 +736,7 @@ def summarise(self): summary_data = [] for rank, i in enumerate(self.ranking): - data = ( - list(summary_measures[i]) + state_prob[i] + state_to_C_prob[i] - ) + data = list(summary_measures[i]) + state_prob[i] + state_to_C_prob[i] summary_data.append(self.player(rank, *data)) return summary_data diff --git a/axelrod/strategies/__init__.py b/axelrod/strategies/__init__.py index 537ee89bc..a7635596e 100644 --- a/axelrod/strategies/__init__.py +++ b/axelrod/strategies/__init__.py @@ -90,9 +90,7 @@ short_run_time_strategies = [ s for s in strategies if not Classifiers["long_run_time"](s()) ] -cheating_strategies = [ - s for s in all_strategies if not Classifiers.obey_axelrod(s()) -] +cheating_strategies = [s for s in all_strategies if not Classifiers.obey_axelrod(s())] ordinary_strategies = strategies # This is a legacy and will be removed diff --git a/axelrod/strategies/_filters.py b/axelrod/strategies/_filters.py index 2dc7a88b6..c9c199bf7 100644 --- a/axelrod/strategies/_filters.py +++ b/axelrod/strategies/_filters.py @@ -155,24 +155,15 @@ class ExampleStrategy(Player): ), "manipulates_state": FilterFunction( function=passes_operator_filter, - kwargs={ - "classifier_key": "manipulates_state", - "operator": operator.eq, - }, + kwargs={"classifier_key": "manipulates_state", "operator": operator.eq}, ), "manipulates_source": FilterFunction( function=passes_operator_filter, - kwargs={ - "classifier_key": "manipulates_source", - "operator": operator.eq, - }, + kwargs={"classifier_key": "manipulates_source", "operator": operator.eq}, ), "inspects_source": FilterFunction( function=passes_operator_filter, - kwargs={ - "classifier_key": "inspects_source", - "operator": operator.eq, - }, + kwargs={"classifier_key": "inspects_source", "operator": operator.eq}, ), "memory_depth": FilterFunction( function=passes_operator_filter, @@ -187,8 +178,7 @@ class ExampleStrategy(Player): kwargs={"classifier_key": "memory_depth", "operator": operator.le}, ), "makes_use_of": FilterFunction( - function=passes_in_list_filter, - kwargs={"classifier_key": "makes_use_of"}, + function=passes_in_list_filter, kwargs={"classifier_key": "makes_use_of"} ), } diff --git a/axelrod/strategies/adaptor.py b/axelrod/strategies/adaptor.py index 5b4026c8c..f8c550f61 100644 --- a/axelrod/strategies/adaptor.py +++ b/axelrod/strategies/adaptor.py @@ -36,13 +36,12 @@ class AbstractAdaptor(Player): "manipulates_state": False, } - def __init__( - self, delta: Dict[Tuple[Action, Action], float], perr: float = 0.01 - ) -> None: + def __init__(self, delta: Dict[Tuple[Action, Action], float], + perr: float = 0.01) -> None: super().__init__() self.perr = perr self.delta = delta - self.s = 0.0 + self.s = 0. def strategy(self, opponent: Player) -> Action: if self.history: @@ -52,8 +51,7 @@ def strategy(self, opponent: Player) -> Action: # Compute probability of Cooperation p = self.perr + (1.0 - 2 * self.perr) * ( - heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1) - ) + heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1)) # Draw action action = self._random.random_choice(p) return action @@ -73,10 +71,10 @@ class AdaptorBrief(AbstractAdaptor): def __init__(self) -> None: delta = { - (C, C): 0.0, # R + (C, C): 0., # R (C, D): -1.001505, # S - (D, C): 0.992107, # T - (D, D): -0.638734, # P + (D, C): 0.992107, # T + (D, D): -0.638734 # P } super().__init__(delta=delta) @@ -95,9 +93,9 @@ class AdaptorLong(AbstractAdaptor): def __init__(self) -> None: delta = { - (C, C): 0.0, # R + (C, C): 0., # R (C, D): 1.888159, # S (D, C): 1.858883, # T - (D, D): -0.995703, # P + (D, D): -0.995703 # P } super().__init__(delta=delta) diff --git a/axelrod/strategies/ann.py b/axelrod/strategies/ann.py index 0079a8f1e..74bd1ab7e 100644 --- a/axelrod/strategies/ann.py +++ b/axelrod/strategies/ann.py @@ -113,10 +113,7 @@ def compute_features(player: Player, opponent: Player) -> List[int]: def activate( - bias: List[float], - hidden: List[float], - output: List[float], - inputs: List[int], + bias: List[float], hidden: List[float], output: List[float], inputs: List[int] ) -> float: """ Compute the output of the neural network: @@ -196,7 +193,8 @@ class ANN(Player): } def __init__( - self, num_features: int, num_hidden: int, weights: List[float] = None + self, num_features: int, num_hidden: int, + weights: List[float] = None ) -> None: Player.__init__(self) self.num_features = num_features @@ -226,68 +224,42 @@ def strategy(self, opponent: Player) -> Action: class EvolvableANN(ANN, EvolvablePlayer): """Evolvable version of ANN.""" - name = "EvolvableANN" def __init__( - self, - num_features: int, - num_hidden: int, + self, num_features: int, num_hidden: int, weights: List[float] = None, mutation_probability: float = None, mutation_distance: int = 5, - seed: int = None, + seed: int = None ) -> None: EvolvablePlayer.__init__(self, seed=seed) - ( - num_features, - num_hidden, - weights, - mutation_probability, - ) = self._normalize_parameters( - num_features, num_hidden, weights, mutation_probability - ) - ANN.__init__( - self, - num_features=num_features, - num_hidden=num_hidden, - weights=weights, - ) + num_features, num_hidden, weights, mutation_probability = self._normalize_parameters( + num_features, num_hidden, weights, mutation_probability) + ANN.__init__(self, + num_features=num_features, + num_hidden=num_hidden, + weights=weights) self.mutation_probability = mutation_probability self.mutation_distance = mutation_distance self.overwrite_init_kwargs( num_features=num_features, num_hidden=num_hidden, weights=weights, - mutation_probability=mutation_probability, - ) + mutation_probability=mutation_probability) - def _normalize_parameters( - self, - num_features=None, - num_hidden=None, - weights=None, - mutation_probability=None, - ): + def _normalize_parameters(self, num_features=None, num_hidden=None, weights=None, mutation_probability=None): if not (num_features and num_hidden): - raise InsufficientParametersError( - "Insufficient Parameters to instantiate EvolvableANN" - ) + raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableANN") size = num_weights(num_features, num_hidden) if not weights: weights = [self._random.uniform(-1, 1) for _ in range(size)] if mutation_probability is None: - mutation_probability = 10.0 / size + mutation_probability = 10. / size return num_features, num_hidden, weights, mutation_probability - def mutate_weights( - self, - weights, - num_features, - num_hidden, - mutation_probability, - mutation_distance, - ): + def mutate_weights(self, weights, num_features, num_hidden, mutation_probability, + mutation_distance): size = num_weights(num_features, num_hidden) randoms = self._random.random(size) for i, r in enumerate(randoms): @@ -298,19 +270,13 @@ def mutate_weights( def mutate(self): weights = self.mutate_weights( - self.weights, - self.num_features, - self.num_hidden, - self.mutation_probability, - self.mutation_distance, - ) + self.weights, self.num_features, self.num_hidden, + self.mutation_probability, self.mutation_distance) return self.create_new(weights=weights) def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError( - "Crossover must be between the same player classes." - ) + raise TypeError("Crossover must be between the same player classes.") weights = crossover_lists(self.weights, other.weights, self._random) return self.create_new(weights=weights) @@ -333,8 +299,9 @@ class EvolvedANN(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN"] super().__init__( - num_features=num_features, num_hidden=num_hidden, weights=weights - ) + num_features=num_features, + num_hidden=num_hidden, + weights=weights) class EvolvedANN5(ANN): @@ -355,8 +322,9 @@ class EvolvedANN5(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN 5"] super().__init__( - num_features=num_features, num_hidden=num_hidden, weights=weights - ) + num_features=num_features, + num_hidden=num_hidden, + weights=weights) class EvolvedANNNoise05(ANN): @@ -377,5 +345,6 @@ class EvolvedANNNoise05(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN 5 Noise 05"] super().__init__( - num_features=num_features, num_hidden=num_hidden, weights=weights - ) + num_features=num_features, + num_hidden=num_hidden, + weights=weights) diff --git a/axelrod/strategies/axelrod_first.py b/axelrod/strategies/axelrod_first.py index 65a6a7776..0cc16d4e6 100644 --- a/axelrod/strategies/axelrod_first.py +++ b/axelrod/strategies/axelrod_first.py @@ -66,7 +66,7 @@ def strategy(self, opponent: Player) -> Action: opponent ever plays D.""" if len(self.history) < self._rounds_to_cooperate: return C - if opponent.defections > 0: #  Implement Grudger + if opponent.defections > 0: # Implement Grudger return D return C @@ -262,14 +262,12 @@ def strategy(self, opponent: Player) -> Action: # Adding 1 to cooperations for assumption that first opponent move # being a response to a cooperation. See docstring for more # information. - alpha = self.number_opponent_cooperations_in_response_to_C / ( - self.cooperations + 1 - ) + alpha = (self.number_opponent_cooperations_in_response_to_C / + (self.cooperations + 1)) # Adding 2 to defections on the assumption that the first two # moves are defections, which may not be true in a noisy match - beta = self.number_opponent_cooperations_in_response_to_D / max( - self.defections, 2 - ) + beta = (self.number_opponent_cooperations_in_response_to_D / + max(self.defections, 2)) R, P, S, T = self.match_attributes["game"].RPST() expected_value_of_cooperating = alpha * R + (1 - alpha) * S @@ -432,34 +430,25 @@ def strategy(self, opponent: Player) -> Action: # Check if opponent plays randomly, if so, defect for the rest of the game p_value = chisquare([opponent.cooperations, opponent.defections]).pvalue - self.opponent_is_random = ( - p_value >= self.alpha - ) or self.opponent_is_random + self.opponent_is_random = (p_value >= self.alpha) or self.opponent_is_random if self.opponent_is_random: return D - if ( - all( - opponent.history[i] == self.history[i - 1] - for i in range(1, len(self.history)) - ) - or opponent.history == self.history - ): + if all( + opponent.history[i] == self.history[i - 1] + for i in range(1, len(self.history)) + ) or opponent.history == self.history: # Check if opponent plays Tit for Tat or a clone of itself. if opponent.history[-1] == D: return D return C if self.next_random_defection_turn is None: - self.next_random_defection_turn = self._random.randint(5, 15) + len( - self.history - ) + self.next_random_defection_turn = self._random.randint(5, 15) + len(self.history) if len(self.history) == self.next_random_defection_turn: # resample the next defection turn - self.next_random_defection_turn = self._random.randint(5, 15) + len( - self.history - ) + self.next_random_defection_turn = self._random.randint(5, 15) + len(self.history) return D return C @@ -591,27 +580,7 @@ class FirstByNydegger(Player): } def __init__(self) -> None: - self.As = [ - 1, - 6, - 7, - 17, - 22, - 23, - 26, - 29, - 30, - 31, - 33, - 38, - 39, - 45, - 49, - 54, - 55, - 58, - 61, - ] + self.As = [1, 6, 7, 17, 22, 23, 26, 29, 30, 31, 33, 38, 39, 45, 49, 54, 55, 58, 61] self.score_map = {(C, C): 0, (C, D): 2, (D, C): 1, (D, D): 3} super().__init__() @@ -641,9 +610,7 @@ def strategy(self, opponent: Player) -> Action: else: # TFT return D if opponent.history[-1] == D else C - A = self.score_history( - self.history[-3:], opponent.history[-3:], self.score_map - ) + A = self.score_history(self.history[-3:], opponent.history[-3:], self.score_map) if A in self.As: return D return C @@ -882,9 +849,7 @@ def strategy(self, opponent: Player) -> Action: return opponent.history[-1] if round_number % 15 == 0: - p_value = chisquare( - [opponent.cooperations, opponent.defections] - ).pvalue + p_value = chisquare([opponent.cooperations, opponent.defections]).pvalue self.opponent_is_random = p_value >= self.alpha if self.opponent_is_random: @@ -1017,10 +982,8 @@ def strategy(self, opponent: Player) -> Action: std_deviation = (N ** (1 / 2)) / 2 lower = N / 2 - 3 * std_deviation upper = N / 2 + 3 * std_deviation - if ( - self.remembered_number_of_opponent_defectioons <= lower - or self.remembered_number_of_opponent_defectioons >= upper - ): + if (self.remembered_number_of_opponent_defectioons <= lower or + self.remembered_number_of_opponent_defectioons >= upper): # Opponent deserves a fresh start self.last_fresh_start = current_round self._fresh_start() diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index 3d4f4b6c8..3ab54f40a 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -327,12 +327,8 @@ def __init__(self): self.opponent_consecutive_defections = 0 # equal to S variable self.one_turn_after_good_defection_ratio = 5 # equal to AD variable self.two_turns_after_good_defection_ratio = 0 # equal to NO variable - self.one_turn_after_good_defection_ratio_count = ( - 1 # equal to AK variable - ) - self.two_turns_after_good_defection_ratio_count = ( - 1 # equal to NK variable - ) + self.one_turn_after_good_defection_ratio_count = 1 # equal to AK variable + self.two_turns_after_good_defection_ratio_count = 1 # equal to NK variable # All above variables correspond to those in original Fotran Code self.dict = {C: 0, D: 1} @@ -357,12 +353,7 @@ def update_state(self, opponent): ) + (3 - (3 * self.dict[opponent.history[-1]])) + (2 * self.dict[self.history[-1]]) - - ( - ( - self.dict[opponent.history[-1]] - * self.dict[self.history[-1]] - ) - ) + - ((self.dict[opponent.history[-1]] * self.dict[self.history[-1]])) ) / (self.two_turns_after_good_defection_ratio_count + 1) self.two_turns_after_good_defection_ratio_count += 1 elif self.num_turns_after_good_defection == 1: @@ -374,10 +365,7 @@ def update_state(self, opponent): ) + (3 - (3 * self.dict[opponent.history[-1]])) + (2 * self.dict[self.history[-1]]) - - ( - self.dict[opponent.history[-1]] - * self.dict[self.history[-1]] - ) + - (self.dict[opponent.history[-1]] * self.dict[self.history[-1]]) ) / (self.one_turn_after_good_defection_ratio_count + 1) self.one_turn_after_good_defection_ratio_count += 1 @@ -414,10 +402,7 @@ def strategy(self, opponent: Player) -> Action: return D if (current_score[0] / ((len(self.history)) + 1)) >= 1.75: probability = ( - ( - 0.25 - + ((opponent.cooperations + 1) / ((len(self.history)) + 1)) - ) + (0.25 + ((opponent.cooperations + 1) / ((len(self.history)) + 1))) - (self.opponent_consecutive_defections * 0.25) + ((current_score[0] - current_score[1]) / 100) + (4 / ((len(self.history)) + 1)) @@ -531,12 +516,7 @@ class SecondByKluepfel(Player): def __init__(self): super().__init__() - self.cd_counts, self.dd_counts, self.dc_counts, self.cc_counts = ( - 0, - 0, - 0, - 0, - ) + self.cd_counts, self.dd_counts, self.dc_counts, self.cc_counts = 0, 0, 0, 0 def strategy(self, opponent: Player) -> Action: # First update the response matrix. @@ -554,9 +534,7 @@ def strategy(self, opponent: Player) -> Action: # Check for randomness if len(self.history) > 26: - if self.cd_counts >= ( - self.cd_counts + self.dd_counts - ) / 2 - 0.75 * np.sqrt( + if self.cd_counts >= (self.cd_counts + self.dd_counts) / 2 - 0.75 * np.sqrt( self.cd_counts + self.dd_counts ) and self.dc_counts >= ( self.dc_counts + self.cc_counts @@ -991,10 +969,7 @@ def strategy(self, opponent: Player) -> Action: if self.forgive_flag: self.forgive_flag = False self.defect_padding = 0 - if ( - self.grudge < len(self.history) + 1 - and opponent.history[-1] == D - ): + if self.grudge < len(self.history) + 1 and opponent.history[-1] == D: # Then override self.grudge += 20 return self.try_return(C) @@ -1120,9 +1095,7 @@ def __init__(self): self.mode = "Normal" self.recorded_defects = 0 # Count opponent defects after turn 1 self.exit_defect_meter = 0 # When >= 11, then exit defect mode. - self.coops_in_first_36 = ( - None # On turn 37, count cooperations in first 36 - ) + self.coops_in_first_36 = None # On turn 37, count cooperations in first 36 self.was_defective = False # Previously in Defect mode self.prob = 0.25 # After turn 37, probability that we'll defect @@ -1132,9 +1105,7 @@ def __init__(self): self.more_coop = 0 # This schedules cooperation for future turns # Initial last_generous_n_turns_ago to 3 because this counts up and # triggers a strategy change at 2. - self.last_generous_n_turns_ago = ( - 3 # How many tuns ago was a "generous" move - ) + self.last_generous_n_turns_ago = 3 # How many tuns ago was a "generous" move self.burned = False self.defect_streak = 0 @@ -1143,9 +1114,7 @@ def __init__(self): 0, ] # Counters that get (almost) alternatively incremented. self.parity_bit = 0 # Which parity_streak to increment - self.parity_limit = ( - 5 # When a parity streak hits this limit, alter strategy. - ) + self.parity_limit = 5 # When a parity streak hits this limit, alter strategy. self.parity_hits = 0 # Counts how many times a parity_limit was hit. # After hitting parity_hits 8 times, lower parity_limit to 3. @@ -1184,9 +1153,7 @@ def calculate_chi_squared(self, turn): denom = turn - 2 expected_matrix = ( - np.outer( - self.move_history.sum(axis=1), self.move_history.sum(axis=0) - ) + np.outer(self.move_history.sum(axis=1), self.move_history.sum(axis=0)) / denom ) @@ -1195,9 +1162,7 @@ def calculate_chi_squared(self, turn): for j in range(2): expect = expected_matrix[i, j] if expect > 1.0: - chi_squared += ( - expect - self.move_history[i, j] - ) ** 2 / expect + chi_squared += (expect - self.move_history[i, j]) ** 2 / expect return chi_squared @@ -1219,10 +1184,7 @@ def detect_random(self, turn): if self.move_history[0, 0] / denom >= 0.8: return False - if ( - self.recorded_defects / denom < 0.25 - or self.recorded_defects / denom > 0.75 - ): + if self.recorded_defects / denom < 0.25 or self.recorded_defects / denom > 0.75: return False if self.calculate_chi_squared(turn) > 3: @@ -1314,11 +1276,7 @@ def strategy(self, opponent: Player) -> Action: # Only enter Fair-weather mode if the opponent Cooperated the first 37 # turns then Defected on the 38th. - if ( - turn == 38 - and opponent.history[-1] == D - and opponent.cooperations == 36 - ): + if turn == 38 and opponent.history[-1] == D and opponent.cooperations == 36: self.mode = "Fair-weather" return self.try_return(to_return=C, lower_flags=False) @@ -1339,9 +1297,7 @@ def strategy(self, opponent: Player) -> Action: self.parity_streak[ self.parity_bit ] = 0 # Reset `parity_streak` when we hit the limit. - self.parity_hits += ( - 1 # Keep track of how many times we hit the limit. - ) + self.parity_hits += 1 # Keep track of how many times we hit the limit. if self.parity_hits >= 8: # After 8 times, lower the limit. self.parity_limit = 3 return self.try_return( @@ -1565,9 +1521,7 @@ def strategy(self, opponent: Player) -> Action: recent_history[-go_back] = opponent.history[-go_back] return self._random.random_choice( - self.prob_coop[ - (recent_history[-3], recent_history[-2], recent_history[-1]) - ] + self.prob_coop[(recent_history[-3], recent_history[-2], recent_history[-1])] ) @@ -1750,10 +1704,7 @@ def strategy(self, opponent: Player) -> Action: else: self.def_after_ab_count += 1 self.streak_needed = ( - np.floor( - 20.0 * self.def_after_ab_count / self.coop_after_ab_count - ) - + 1 + np.floor(20.0 * self.def_after_ab_count / self.coop_after_ab_count) + 1 ) self.current_streak = 0 return C @@ -1853,9 +1804,7 @@ def strategy(self, opponent: Player) -> Action: # Update history if turn >= 3: - self.count_them_us_them[ - (them_three_ago, us_two_ago, them_two_ago) - ] += 1 + self.count_them_us_them[(them_three_ago, us_two_ago, them_two_ago)] += 1 if ( self.count_them_us_them[(them_two_ago, us_last, C)] @@ -1917,9 +1866,7 @@ def __init__(self) -> None: (10, D, 7, C), ) - super().__init__( - transitions=transitions, initial_state=0, initial_action=C - ) + super().__init__(transitions=transitions, initial_state=0, initial_action=C) class SecondByMikkelson(FSMPlayer): @@ -2153,8 +2100,6 @@ def strategy(self, opponent: Player) -> Action: # Calculate the probability that the opponent cooperated last turn given # what we know two turns ago. - prob_coop = ( - self.opp_c_after_x[us_two_turns_ago] - / self.total_num_of_x[us_two_turns_ago] - ) + prob_coop = self.opp_c_after_x[us_two_turns_ago] / self.total_num_of_x[ + us_two_turns_ago] return self._random.random_choice(prob_coop) diff --git a/axelrod/strategies/backstabber.py b/axelrod/strategies/backstabber.py index 3150d8974..0d9d821d9 100644 --- a/axelrod/strategies/backstabber.py +++ b/axelrod/strategies/backstabber.py @@ -97,9 +97,7 @@ def _opponent_triggers_alt_strategy(opponent: Player) -> bool: return before_alt_strategy < current_round <= last_round_of_alt_strategy -def _opponent_defected_in_first_n_rounds( - opponent: Player, first_n_rounds: int -) -> bool: +def _opponent_defected_in_first_n_rounds(opponent: Player, first_n_rounds: int) -> bool: """ If opponent defected in the first N rounds, return True. Else return False. """ diff --git a/axelrod/strategies/bush_mosteller.py b/axelrod/strategies/bush_mosteller.py index 154d02571..3fed4127b 100644 --- a/axelrod/strategies/bush_mosteller.py +++ b/axelrod/strategies/bush_mosteller.py @@ -62,10 +62,7 @@ def __init__( self._c_prob, self._d_prob = c_prob, d_prob self._init_c_prob, self._init_d_prob = c_prob, d_prob self._aspiration_level = abs( - ( - max(self.match_attributes["game"].RPST()) - / aspiration_level_divider - ) + (max(self.match_attributes["game"].RPST()) / aspiration_level_divider) ) self._stimulus = 0.0 @@ -107,9 +104,7 @@ def stimulus_update(self, opponent: Player): ) elif self._stimulus < 0: - self._c_prob += ( - self._learning_rate * self._stimulus * self._c_prob - ) + self._c_prob += self._learning_rate * self._stimulus * self._c_prob # Updates probability following previous choice D if self.history[-1] == D: @@ -119,21 +114,15 @@ def stimulus_update(self, opponent: Player): ) elif self._stimulus < 0: - self._d_prob += ( - self._learning_rate * self._stimulus * self._d_prob - ) + self._d_prob += self._learning_rate * self._stimulus * self._d_prob def strategy(self, opponent: Player) -> Action: # First turn if len(self.history) == 0: - return self._random.random_choice( - self._c_prob / (self._c_prob + self._d_prob) - ) + return self._random.random_choice(self._c_prob / (self._c_prob + self._d_prob)) # Updating stimulus depending on his own latest choice self.stimulus_update(opponent) - return self._random.random_choice( - self._c_prob / (self._c_prob + self._d_prob) - ) + return self._random.random_choice(self._c_prob / (self._c_prob + self._d_prob)) diff --git a/axelrod/strategies/calculator.py b/axelrod/strategies/calculator.py index 259bccb91..05cdba01e 100644 --- a/axelrod/strategies/calculator.py +++ b/axelrod/strategies/calculator.py @@ -39,9 +39,8 @@ def set_seed(self, seed: int = None): def strategy(self, opponent: Player) -> Action: turn = len(self.history) if turn > 0: - self.joss_instance.history.append( - self.history[-1], opponent.history[-1] - ) + self.joss_instance.history.append(self.history[-1], + opponent.history[-1]) if turn == 20: self.cycle = detect_cycle(opponent.history) return self.extended_strategy(opponent) diff --git a/axelrod/strategies/cycler.py b/axelrod/strategies/cycler.py index ead3e7127..bff8c83d4 100644 --- a/axelrod/strategies/cycler.py +++ b/axelrod/strategies/cycler.py @@ -111,25 +111,23 @@ def __init__( cycle_length: int = None, mutation_probability: float = 0.2, mutation_potency: int = 1, - seed: int = None, + seed: int = None ) -> None: EvolvablePlayer.__init__(self, seed=seed) cycle, cycle_length = self._normalize_parameters(cycle, cycle_length) Cycler.__init__(self, cycle=cycle) # Overwrite init_kwargs in the case that we generated a new cycle from cycle_length - self.overwrite_init_kwargs(cycle=cycle, cycle_length=cycle_length) + self.overwrite_init_kwargs( + cycle=cycle, + cycle_length=cycle_length) self.mutation_probability = mutation_probability self.mutation_potency = mutation_potency - def _normalize_parameters( - self, cycle=None, cycle_length=None - ) -> Tuple[str, int]: + def _normalize_parameters(self, cycle=None, cycle_length=None) -> Tuple[str, int]: """Compute other parameters from those that may be missing, to ensure proper cloning.""" if not cycle: if not cycle_length: - raise InsufficientParametersError( - "Insufficient Parameters to instantiate EvolvableCycler" - ) + raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableCycler") cycle = self._generate_random_cycle(cycle_length) cycle_length = len(cycle) return cycle, cycle_length @@ -138,9 +136,7 @@ def _generate_random_cycle(self, cycle_length: int) -> str: """ Generate a sequence of random moves """ - return actions_to_str( - self._random.choice(actions) for _ in range(cycle_length) - ) + return actions_to_str(self._random.choice(actions) for _ in range(cycle_length)) def mutate(self) -> EvolvablePlayer: """ @@ -149,12 +145,8 @@ def mutate(self) -> EvolvablePlayer: if self._random.random() <= self.mutation_probability: mutated_sequence = list(str_to_actions(self.cycle)) for _ in range(self.mutation_potency): - index_to_change = self._random.randint( - 0, len(mutated_sequence) - 1 - ) - mutated_sequence[index_to_change] = mutated_sequence[ - index_to_change - ].flip() + index_to_change = self._random.randint(0, len(mutated_sequence) - 1) + mutated_sequence[index_to_change] = mutated_sequence[index_to_change].flip() cycle = actions_to_str(mutated_sequence) else: cycle = self.cycle @@ -166,9 +158,7 @@ def crossover(self, other) -> EvolvablePlayer: Creates and returns a new Player instance with a single crossover point. """ if other.__class__ != self.__class__: - raise TypeError( - "Crossover must be between the same player classes." - ) + raise TypeError("Crossover must be between the same player classes.") cycle_list = crossover_lists(self.cycle, other.cycle, self._random) cycle = "".join(cycle_list) cycle, _ = self._normalize_parameters(cycle) diff --git a/axelrod/strategies/dbs.py b/axelrod/strategies/dbs.py index 0d4f6742d..8e7916718 100644 --- a/axelrod/strategies/dbs.py +++ b/axelrod/strategies/dbs.py @@ -266,9 +266,7 @@ def strategy(self, opponent: Player) -> Action: if r_minus_in_Rd: self.v += 1 # If the number of violations is superior to a threshold, clean Rd. - if (self.v > self.reject_threshold) or ( - r_plus_in_Rc and r_minus_in_Rd - ): + if (self.v > self.reject_threshold) or (r_plus_in_Rc and r_minus_in_Rd): self.Rd.clear() self.v = 0 @@ -276,9 +274,7 @@ def strategy(self, opponent: Player) -> Action: Rp = {} all_cond = [(C, C), (C, D), (D, C), (D, D)] for outcome in all_cond: - if (outcome not in self.Rc.keys()) and ( - outcome not in self.Rd.keys() - ): + if (outcome not in self.Rc.keys()) and (outcome not in self.Rd.keys()): # Compute opponent's C answer probability. Rp[outcome] = self.compute_prob_rule(outcome, self.alpha) @@ -331,12 +327,8 @@ def get_siblings(self): siblings which are DeterministicNodes, their depth is equal to current node depth's + 1. """ - opponent_c_choice = DeterministicNode( - self.own_action, C, self.depth + 1 - ) - opponent_d_choice = DeterministicNode( - self.own_action, D, self.depth + 1 - ) + opponent_c_choice = DeterministicNode(self.own_action, C, self.depth + 1) + opponent_d_choice = DeterministicNode(self.own_action, D, self.depth + 1) return opponent_c_choice, opponent_d_choice def is_stochastic(self): @@ -362,12 +354,8 @@ def get_siblings(self, policy): of the same depth as the current node. Their probabilities pC are defined by the policy argument. """ - c_choice = StochasticNode( - C, policy[(self.action1, self.action2)], self.depth - ) - d_choice = StochasticNode( - D, policy[(self.action1, self.action2)], self.depth - ) + c_choice = StochasticNode(C, policy[(self.action1, self.action2)], self.depth) + d_choice = StochasticNode(D, policy[(self.action1, self.action2)], self.depth) return c_choice, d_choice def is_stochastic(self): @@ -413,9 +401,7 @@ def minimax_tree_search(begin_node, policy, max_depth): # The stochastic node value is the expected value of siblings. node_value = begin_node.pC * minimax_tree_search( siblings[0], policy, max_depth - ) + (1 - begin_node.pC) * minimax_tree_search( - siblings[1], policy, max_depth - ) + ) + (1 - begin_node.pC) * minimax_tree_search(siblings[1], policy, max_depth) return node_value else: # Deterministic node if begin_node.depth == max_depth: @@ -447,9 +433,7 @@ def move_gen(outcome, policy, depth_search_tree=5): using tree-search procedure. """ current_node = DeterministicNode(outcome[0], outcome[1], depth=0) - values_of_choices = minimax_tree_search( - current_node, policy, depth_search_tree - ) + values_of_choices = minimax_tree_search(current_node, policy, depth_search_tree) # Returns the Action which correspond to the best choice in terms of # expected value. In case value(C) == value(D), returns C. actions_tuple = (C, D) diff --git a/axelrod/strategies/defector.py b/axelrod/strategies/defector.py index 514e5bed8..7b4831443 100644 --- a/axelrod/strategies/defector.py +++ b/axelrod/strategies/defector.py @@ -54,9 +54,6 @@ def strategy(self, opponent: Player) -> Action: Defect if opponent has cooperated at least once in the past and has defected for the last 3 turns in a row. """ - if ( - opponent.history.cooperations > 0 - and opponent.history[-3:] == [D] * 3 - ): + if opponent.history.cooperations > 0 and opponent.history[-3:] == [D] * 3: return C return D diff --git a/axelrod/strategies/finite_state_machines.py b/axelrod/strategies/finite_state_machines.py index ec49ad2f4..29fa1a291 100644 --- a/axelrod/strategies/finite_state_machines.py +++ b/axelrod/strategies/finite_state_machines.py @@ -70,9 +70,7 @@ def state_transitions(self) -> dict: return self._state_transitions.copy() def transitions(self) -> list: - return [ - [x[0], x[1], y[0], y[1]] for x, y in self._state_transitions.items() - ] + return [[x[0], x[1], y[0], y[1]] for x, y in self._state_transitions.items()] def move(self, opponent_action: Action) -> Action: """Computes the response move and changes state.""" @@ -114,7 +112,7 @@ def __init__( self, transitions: Tuple[Transition, ...] = ((1, C, 1, C), (1, D, 1, D)), initial_state: int = 1, - initial_action: Action = C, + initial_action: Action = C ) -> None: Player.__init__(self) self.initial_state = initial_state @@ -149,62 +147,39 @@ def __init__( initial_action: Action = None, num_states: int = None, mutation_probability: float = 0.1, - seed: int = None, + seed: int = None ) -> None: """If transitions, initial_state, and initial_action are None then generate random parameters using num_states.""" EvolvablePlayer.__init__(self, seed=seed) - ( - transitions, - initial_state, - initial_action, - num_states, - ) = self._normalize_parameters( - transitions, initial_state, initial_action, num_states - ) + transitions, initial_state, initial_action, num_states = self._normalize_parameters( + transitions, initial_state, initial_action, num_states) FSMPlayer.__init__( self, transitions=transitions, initial_state=initial_state, - initial_action=initial_action, - ) + initial_action=initial_action) self.mutation_probability = mutation_probability self.overwrite_init_kwargs( transitions=transitions, initial_state=initial_state, initial_action=initial_action, - num_states=self.num_states, - ) + num_states=self.num_states) @classmethod - def normalize_transitions( - cls, transitions: Sequence[Sequence] - ) -> Tuple[Tuple[Any, ...], ...]: + def normalize_transitions(cls, transitions: Sequence[Sequence]) -> Tuple[Tuple[Any, ...], ...]: """Translate a list of lists to a tuple of tuples.""" normalized = [] for t in transitions: normalized.append(tuple(t)) return tuple(normalized) - def _normalize_parameters( - self, - transitions: Tuple = None, - initial_state: int = None, - initial_action: Action = None, - num_states: int = None, - ) -> Tuple[Tuple, int, Action, int]: - if not ( - (transitions is not None) - and (initial_state is not None) - and (initial_action is not None) - ): + def _normalize_parameters(self, transitions: Tuple = None, initial_state: int = None, initial_action: Action = None, + num_states: int = None) -> Tuple[Tuple, int, Action, int]: + if not ((transitions is not None) and (initial_state is not None) and (initial_action is not None)): if not num_states: - raise InsufficientParametersError( - "Insufficient Parameters to instantiate EvolvableFSMPlayer" - ) - transitions, initial_state, initial_action = self.random_params( - num_states - ) + raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableFSMPlayer") + transitions, initial_state, initial_action = self.random_params(num_states) transitions = self.normalize_transitions(transitions) num_states = len(transitions) // 2 return transitions, initial_state, initial_action, num_states @@ -213,9 +188,7 @@ def _normalize_parameters( def num_states(self) -> int: return self.fsm.num_states() - def random_params( - self, num_states: int - ) -> Tuple[Tuple[Transition, ...], int, Action]: + def random_params(self, num_states: int) -> Tuple[Tuple[Transition, ...], int, Action]: rows = [] for j in range(num_states): for action in actions: @@ -252,14 +225,10 @@ def mutate(self): if self._random.random() < self.mutation_probability / 10: initial_action = self.initial_action.flip() initial_state = self.initial_state - if self._random.random() < self.mutation_probability / ( - 10 * self.num_states - ): + if self._random.random() < self.mutation_probability / (10 * self.num_states): initial_state = self._random.randint(0, self.num_states) try: - transitions = self.mutate_rows( - self.fsm.transitions(), self.mutation_probability - ) + transitions = self.mutate_rows(self.fsm.transitions(), self.mutation_probability) self.fsm = SimpleFSM(transitions, self.initial_state) except ValueError: # If the FSM is malformed, try again. @@ -270,9 +239,7 @@ def mutate(self): initial_action=initial_action, ) - def crossover_rows( - self, rows1: List[List], rows2: List[List] - ) -> List[List]: + def crossover_rows(self, rows1: List[List], rows2: List[List]) -> List[List]: num_states = len(rows1) // 2 cross_point = 2 * self._random.randint(0, num_states) new_rows = copy_lists(rows1[:cross_point]) @@ -281,12 +248,8 @@ def crossover_rows( def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError( - "Crossover must be between the same player classes." - ) - transitions = self.crossover_rows( - self.fsm.transitions(), other.fsm.transitions() - ) + raise TypeError("Crossover must be between the same player classes.") + transitions = self.crossover_rows(self.fsm.transitions(), other.fsm.transitions()) transitions = self.normalize_transitions(transitions) return self.create_new(transitions=transitions) @@ -304,28 +267,22 @@ def receive_vector(self, vector): Finally, a probability to determine the player's first move. """ num_states = self.fsm.num_states() - state_scale = vector[: num_states * 2] + state_scale = vector[:num_states * 2] next_states = [int(s * (num_states - 1)) for s in state_scale] - actions = vector[num_states * 2 : -1] + actions = vector[num_states * 2: -1] self.initial_action = C if round(vector[-1]) == 0 else D self.initial_state = 1 transitions = [] - for i, (initial_state, action) in enumerate( - itertools.product(range(num_states), [C, D]) - ): + for i, (initial_state, action) in enumerate(itertools.product(range(num_states), [C, D])): next_action = C if round(actions[i]) == 0 else D - transitions.append( - [initial_state, action, next_states[i], next_action] - ) + transitions.append([initial_state, action, next_states[i], next_action]) transitions = self.normalize_transitions(transitions) self.fsm = SimpleFSM(transitions, self.initial_state) - self.overwrite_init_kwargs( - transitions=transitions, - initial_state=self.initial_state, - initial_action=self.initial_action, - ) + self.overwrite_init_kwargs(transitions=transitions, + initial_state=self.initial_state, + initial_action=self.initial_action) def create_vector_bounds(self): """Creates the bounds for the decision variables.""" diff --git a/axelrod/strategies/gambler.py b/axelrod/strategies/gambler.py index 69bf9221b..3e50a76b9 100644 --- a/axelrod/strategies/gambler.py +++ b/axelrod/strategies/gambler.py @@ -59,7 +59,7 @@ def __init__( pattern: Any = None, # pattern is str or tuple of Actions. parameters: Plays = None, mutation_probability: float = None, - seed: int = None, + seed: int = None ) -> None: EvolvableLookerUp.__init__( self, @@ -68,7 +68,7 @@ def __init__( pattern=pattern, parameters=parameters, mutation_probability=mutation_probability, - seed=seed, + seed=seed ) self.pattern = list(self.pattern) Gambler.__init__( @@ -76,7 +76,7 @@ def __init__( lookup_dict=self.lookup_dict, initial_actions=self.initial_actions, pattern=self.pattern, - parameters=self.parameters, + parameters=self.parameters ) self.overwrite_init_kwargs( lookup_dict=self.lookup_dict, @@ -105,9 +105,7 @@ def receive_vector(self, vector): """Receives a vector and updates the player's pattern. Ignores extra parameters.""" self.pattern = vector self_depth, op_depth, op_openings_depth = self.parameters - self._lookup = LookupTable.from_pattern( - self.pattern, self_depth, op_depth, op_openings_depth - ) + self._lookup = LookupTable.from_pattern(self.pattern, self_depth, op_depth, op_openings_depth) def create_vector_bounds(self): """Creates the bounds for the decision variables. Ignores extra parameters.""" diff --git a/axelrod/strategies/gobymajority.py b/axelrod/strategies/gobymajority.py index 9a7692c5c..7617b571f 100644 --- a/axelrod/strategies/gobymajority.py +++ b/axelrod/strategies/gobymajority.py @@ -60,9 +60,7 @@ def __init__( self.memory = self.classifier["memory_depth"] else: self.memory = 0 - self.name = "Go By Majority" + (self.memory > 0) * ( - ": %i" % self.memory - ) + self.name = "Go By Majority" + (self.memory > 0) * (": %i" % self.memory) if self.soft: self.name = "Soft " + self.name else: diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index 41d1479c1..a646851af 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -24,7 +24,7 @@ class Grudger(Player): name = "Grudger" classifier = { - "memory_depth": float("inf"), + "memory_depth": float('inf'), "stochastic": False, "long_run_time": False, "inspects_source": False, diff --git a/axelrod/strategies/hmm.py b/axelrod/strategies/hmm.py index 8eccef194..ad64ddb55 100644 --- a/axelrod/strategies/hmm.py +++ b/axelrod/strategies/hmm.py @@ -59,11 +59,7 @@ class SimpleHMM(object): """ def __init__( - self, - transitions_C, - transitions_D, - emission_probabilities, - initial_state, + self, transitions_C, transitions_D, emission_probabilities, initial_state ) -> None: """ Params @@ -137,17 +133,13 @@ def move(self, opponent_action: Action) -> Action: next_state = self._cache_C[self.state] except KeyError: num_states = len(self.emission_probabilities) - next_state = self._random.choice( - num_states, 1, p=self.transitions_C[self.state] - )[0] + next_state = self._random.choice(num_states, 1, p=self.transitions_C[self.state])[0] else: try: next_state = self._cache_D[self.state] except KeyError: num_states = len(self.emission_probabilities) - next_state = self._random.choice( - num_states, 1, p=self.transitions_D[self.state] - )[0] + next_state = self._random.choice(num_states, 1, p=self.transitions_D[self.state])[0] self.state = next_state # Choose action to emit. @@ -186,7 +178,7 @@ def __init__( transitions_D=None, emission_probabilities=None, initial_state=0, - initial_action=C, + initial_action=C ) -> None: Player.__init__(self) if not transitions_C: @@ -197,10 +189,7 @@ def __init__( self.initial_state = initial_state self.initial_action = initial_action self.hmm = SimpleHMM( - copy_lists(transitions_C), - copy_lists(transitions_D), - list(emission_probabilities), - initial_state, + copy_lists(transitions_C), copy_lists(transitions_D), list(emission_probabilities), initial_state ) assert self.hmm.is_well_formed() self.state = self.hmm.state @@ -241,7 +230,6 @@ def set_seed(self, seed=None): class EvolvableHMMPlayer(HMMPlayer, EvolvablePlayer): """Evolvable version of HMMPlayer.""" - name = "EvolvableHMMPlayer" def __init__( @@ -253,35 +241,18 @@ def __init__( initial_action=C, num_states=None, mutation_probability=None, - seed: int = None, + seed: int = None ) -> None: EvolvablePlayer.__init__(self, seed=seed) - ( - transitions_C, - transitions_D, - emission_probabilities, - initial_state, - initial_action, - num_states, - mutation_probability, - ) = self._normalize_parameters( - transitions_C, - transitions_D, - emission_probabilities, - initial_state, - initial_action, - num_states, - mutation_probability, - ) + transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability = self._normalize_parameters( + transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability) self.mutation_probability = mutation_probability - HMMPlayer.__init__( - self, - transitions_C=transitions_C, - transitions_D=transitions_D, - emission_probabilities=emission_probabilities, - initial_state=initial_state, - initial_action=initial_action, - ) + HMMPlayer.__init__(self, + transitions_C=transitions_C, + transitions_D=transitions_D, + emission_probabilities=emission_probabilities, + initial_state=initial_state, + initial_action=initial_action) self.hmm._random = self._random self.overwrite_init_kwargs( transitions_C=transitions_C, @@ -290,35 +261,16 @@ def __init__( initial_state=initial_state, initial_action=initial_action, num_states=num_states, - mutation_probability=mutation_probability, + mutation_probability=mutation_probability ) - def _normalize_parameters( - self, - transitions_C=None, - transitions_D=None, - emission_probabilities=None, - initial_state=None, - initial_action=None, - num_states=None, - mutation_probability=None, - ): - if not ( - (transitions_C and transitions_D and emission_probabilities) - and (initial_state is not None) - and (initial_action is not None) - ): + def _normalize_parameters(self, transitions_C=None, transitions_D=None, emission_probabilities=None, + initial_state=None, initial_action=None, num_states=None, mutation_probability=None): + if not ((transitions_C and transitions_D and emission_probabilities) and (initial_state is not None) and (initial_action is not None)): if not num_states: - raise InsufficientParametersError( - "Insufficient Parameters to instantiate EvolvableHMMPlayer" - ) - ( - transitions_C, - transitions_D, - emission_probabilities, - initial_state, - initial_action, - ) = self.random_params(num_states) + raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableHMMPlayer") + transitions_C, transitions_D, emission_probabilities, initial_state, initial_action = self.random_params( + num_states) # Normalize types of various matrices for m in [transitions_C, transitions_D]: for i in range(len(m)): @@ -329,15 +281,7 @@ def _normalize_parameters( mutation_probability = 10 / (num_states ** 2) else: mutation_probability = mutation_probability - return ( - transitions_C, - transitions_D, - emission_probabilities, - initial_state, - initial_action, - num_states, - mutation_probability, - ) + return transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability def random_params(self, num_states): transitions_C = [] @@ -349,13 +293,7 @@ def random_params(self, num_states): emission_probabilities.append(self._random.random()) initial_state = self._random.randint(0, num_states) initial_action = C - return ( - transitions_C, - transitions_D, - emission_probabilities, - initial_state, - initial_action, - ) + return transitions_C, transitions_D, emission_probabilities, initial_state, initial_action @property def num_states(self): @@ -369,23 +307,16 @@ def mutate_rows(self, rows, mutation_probability): def mutate(self): transitions_C = self.mutate_rows( - self.hmm.transitions_C, self.mutation_probability - ) + self.hmm.transitions_C, self.mutation_probability) transitions_D = self.mutate_rows( - self.hmm.transitions_D, self.mutation_probability - ) + self.hmm.transitions_D, self.mutation_probability) emission_probabilities = mutate_row( - self.hmm.emission_probabilities, - self.mutation_probability, - self._random, - ) + self.hmm.emission_probabilities, self.mutation_probability, self._random) initial_action = self.initial_action if self._random.random() < self.mutation_probability / 10: initial_action = self.initial_action.flip() initial_state = self.initial_state - if self._random.random() < self.mutation_probability / ( - 10 * self.num_states - ): + if self._random.random() < self.mutation_probability / (10 * self.num_states): initial_state = self._random.randint(0, self.num_states) return self.create_new( transitions_C=transitions_C, @@ -397,24 +328,15 @@ def mutate(self): def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError( - "Crossover must be between the same player classes." - ) - transitions_C = crossover_lists( - self.hmm.transitions_C, other.hmm.transitions_C, self._random - ) - transitions_D = crossover_lists( - self.hmm.transitions_D, other.hmm.transitions_D, self._random - ) + raise TypeError("Crossover must be between the same player classes.") + transitions_C = crossover_lists(self.hmm.transitions_C, other.hmm.transitions_C, self._random) + transitions_D = crossover_lists(self.hmm.transitions_D, other.hmm.transitions_D, self._random) emission_probabilities = crossover_lists( - self.hmm.emission_probabilities, - other.hmm.emission_probabilities, - self._random, - ) + self.hmm.emission_probabilities, other.hmm.emission_probabilities, self._random) return self.create_new( transitions_C=transitions_C, transitions_D=transitions_D, - emission_probabilities=emission_probabilities, + emission_probabilities=emission_probabilities ) def receive_vector(self, vector): @@ -431,12 +353,12 @@ class with self.num_states. entry is the initial_action. """ - assert len(vector) == 2 * self.num_states ** 2 + self.num_states + 1 + assert(len(vector) == 2 * self.num_states ** 2 + self.num_states + 1) def deserialize(vector): matrix = [] for i in range(self.num_states): - row = vector[self.num_states * i : self.num_states * (i + 1)] + row = vector[self.num_states * i: self.num_states * (i + 1)] row = normalize_vector(row) matrix.append(row) return matrix @@ -449,7 +371,7 @@ def deserialize(vector): deserialize(vector[0:break_tc]), deserialize(vector[break_tc:break_td]), normalize_vector(vector[break_td:break_ep]), - initial_state, + initial_state ) self.initial_action = C if round(vector[-1]) == 0 else D self.initial_state = initial_state diff --git a/axelrod/strategies/human.py b/axelrod/strategies/human.py index 20d921782..b3a0e9564 100644 --- a/axelrod/strategies/human.py +++ b/axelrod/strategies/human.py @@ -36,9 +36,7 @@ def validate(self, document) -> None: text = document.text if text and text.upper() not in ["C", "D"]: - raise ValidationError( - message="Action must be C or D", cursor_position=0 - ) + raise ValidationError(message="Action must be C or D", cursor_position=0) class Human(Player): @@ -85,14 +83,10 @@ def _history_toolbar(self): Described at http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-a-bottom-toolbar """ my_history = [self.symbols[action] for action in self.history] - opponent_history = [ - self.symbols[action] for action in self.history.coplays - ] + opponent_history = [self.symbols[action] for action in self.history.coplays] history = list(zip(my_history, opponent_history)) if self.history: - content = "History ({}, opponent): {}".format( - self.human_name, history - ) + content = "History ({}, opponent): {}".format(self.human_name, history) else: content = "" return content diff --git a/axelrod/strategies/hunter.py b/axelrod/strategies/hunter.py index e53ead1f3..51c6968d7 100644 --- a/axelrod/strategies/hunter.py +++ b/axelrod/strategies/hunter.py @@ -26,10 +26,7 @@ class DefectorHunter(Player): } def strategy(self, opponent: Player) -> Action: - if ( - len(self.history) >= 4 - and len(opponent.history) == opponent.defections - ): + if len(self.history) >= 4 and len(opponent.history) == opponent.defections: return D return C @@ -53,10 +50,7 @@ class CooperatorHunter(Player): } def strategy(self, opponent: Player) -> Action: - if ( - len(self.history) >= 4 - and len(opponent.history) == opponent.cooperations - ): + if len(self.history) >= 4 and len(opponent.history) == opponent.cooperations: return D return C @@ -250,8 +244,6 @@ def strategy(self, opponent: Player) -> Action: probabilities.append(self.countCC / self.cooperations) if self.defections > 5: probabilities.append(self.countDD / self.defections) - if probabilities and all( - [abs(p - 0.5) < 0.25 for p in probabilities] - ): + if probabilities and all([abs(p - 0.5) < 0.25 for p in probabilities]): return D return C diff --git a/axelrod/strategies/lookerup.py b/axelrod/strategies/lookerup.py index 8bbed1e60..1f57dc685 100644 --- a/axelrod/strategies/lookerup.py +++ b/axelrod/strategies/lookerup.py @@ -101,11 +101,7 @@ def _raise_error_for_bad_lookup_dict(self): @classmethod def from_pattern( - cls, - pattern: tuple, - player_depth: int, - op_depth: int, - op_openings_depth: int, + cls, pattern: tuple, player_depth: int, op_depth: int, op_openings_depth: int ): keys = create_lookup_table_keys( player_depth=player_depth, @@ -155,9 +151,7 @@ def display( """ def sorter(plays): - return tuple( - actions_to_str(getattr(plays, field) for field in sort_by) - ) + return tuple(actions_to_str(getattr(plays, field) for field in sort_by)) col_width = 11 sorted_keys = sorted(self._dict, key=sorter) @@ -324,7 +318,7 @@ def __init__( lookup_dict: dict = None, initial_actions: tuple = None, pattern: Any = None, # pattern is str or tuple of Action's. - parameters: Plays = None, + parameters: Plays = None ) -> None: Player.__init__(self) @@ -410,21 +404,11 @@ def __init__( pattern: Any = None, # pattern is str or tuple of Action's. parameters: Plays = None, mutation_probability: float = None, - seed: int = None, + seed: int = None ) -> None: EvolvablePlayer.__init__(self, seed=seed) - ( - lookup_dict, - initial_actions, - pattern, - parameters, - mutation_probability, - ) = self._normalize_parameters( - lookup_dict, - initial_actions, - pattern, - parameters, - mutation_probability, + lookup_dict, initial_actions, pattern, parameters, mutation_probability = self._normalize_parameters( + lookup_dict, initial_actions, pattern, parameters, mutation_probability ) LookerUp.__init__( self, @@ -442,50 +426,30 @@ def __init__( mutation_probability=mutation_probability, ) - def _normalize_parameters( - self, - lookup_dict=None, - initial_actions=None, - pattern=None, - parameters=None, - mutation_probability=None, - ): + def _normalize_parameters(self, lookup_dict=None, initial_actions=None, pattern=None, parameters=None, + mutation_probability=None): if lookup_dict and initial_actions: # Compute the associated pattern and parameters # Map the table keys to namedTuple Plays - lookup_table = self._get_lookup_table( - lookup_dict, pattern, parameters - ) + lookup_table = self._get_lookup_table(lookup_dict, pattern, parameters) lookup_dict = lookup_table.dictionary - parameters = ( - lookup_table.player_depth, - lookup_table.op_depth, - lookup_table.op_openings_depth, - ) + parameters = (lookup_table.player_depth, lookup_table.op_depth, lookup_table.op_openings_depth) pattern = tuple(v for k, v in sorted(lookup_dict.items())) elif pattern and parameters and initial_actions: # Compute the associated lookup table plays, op_plays, op_start_plays = parameters - lookup_table = self._get_lookup_table( - lookup_dict, pattern, parameters - ) + lookup_table = self._get_lookup_table(lookup_dict, pattern, parameters) lookup_dict = lookup_table.dictionary elif parameters: # Generate a random pattern and (maybe) initial actions plays, op_plays, op_start_plays = parameters - pattern, lookup_table = self.random_params( - plays, op_plays, op_start_plays - ) + pattern, lookup_table = self.random_params(plays, op_plays, op_start_plays) lookup_dict = lookup_table.dictionary if not initial_actions: num_actions = max([plays, op_plays, op_start_plays]) - initial_actions = tuple( - [self._random.choice((C, D)) for _ in range(num_actions)] - ) + initial_actions = tuple([self._random.choice((C, D)) for _ in range(num_actions)]) else: - raise InsufficientParametersError( - "Insufficient Parameters to instantiate EvolvableLookerUp" - ) + raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableLookerUp") # Normalize pattern if isinstance(pattern, str): pattern = str_to_actions(pattern) @@ -493,14 +457,8 @@ def _normalize_parameters( if mutation_probability is None: plays, op_plays, op_start_plays = parameters keys = create_lookup_table_keys(plays, op_plays, op_start_plays) - mutation_probability = 2.0 / len(keys) - return ( - lookup_dict, - initial_actions, - pattern, - parameters, - mutation_probability, - ) + mutation_probability = 2. / len(keys) + return lookup_dict, initial_actions, pattern, parameters, mutation_probability def random_value(self): return self._random.choice(actions) @@ -525,9 +483,7 @@ def mutate_table(self, table, mutation_probability): return table def mutate(self): - lookup_dict = self.mutate_table( - self.lookup_dict, self.mutation_probability - ) + lookup_dict = self.mutate_table(self.lookup_dict, self.mutation_probability) # Add in starting moves initial_actions = list(self.initial_actions) for i in range(len(initial_actions)): @@ -535,17 +491,14 @@ def mutate(self): if r < self.mutation_probability: initial_actions[i] = initial_actions[i].flip() return self.create_new( - lookup_dict=lookup_dict, initial_actions=tuple(initial_actions), + lookup_dict=lookup_dict, + initial_actions=tuple(initial_actions), ) def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError( - "Crossover must be between the same player classes." - ) - lookup_dict = crossover_dictionaries( - self.lookup_dict, other.lookup_dict, self._random - ) + raise TypeError("Crossover must be between the same player classes.") + lookup_dict = crossover_dictionaries(self.lookup_dict, other.lookup_dict, self._random) return self.create_new(lookup_dict=lookup_dict) @@ -562,9 +515,7 @@ class EvolvedLookerUp1_1_1(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=1, op_openings=1) - super().__init__( - parameters=params, pattern="CDDDDCDD", initial_actions=(C,) - ) + super().__init__(parameters=params, pattern="CDDDDCDD", initial_actions=(C,)) class EvolvedLookerUp2_2_2(LookerUp): @@ -580,12 +531,8 @@ class EvolvedLookerUp2_2_2(LookerUp): def __init__(self) -> None: params = Plays(self_plays=2, op_plays=2, op_openings=2) - pattern = ( - "CDDCDCDDCDDDCDDDDDCDCDCCCDDCCDCDDDCCCCCDDDCDDDDDDDDDCCDDCDDDCCCD" - ) - super().__init__( - parameters=params, pattern=pattern, initial_actions=(C, C) - ) + pattern = "CDDCDCDDCDDDCDDDDDCDCDCCCDDCCDCDDDCCCCCDDDCDDDDDDDDDCCDDCDDDCCCD" + super().__init__(parameters=params, pattern=pattern, initial_actions=(C, C)) class Winner12(LookerUp): @@ -602,9 +549,7 @@ class Winner12(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=2, op_openings=0) pattern = "CDCDDCDD" - super().__init__( - parameters=params, pattern=pattern, initial_actions=(C, C) - ) + super().__init__(parameters=params, pattern=pattern, initial_actions=(C, C)) class Winner21(LookerUp): @@ -621,9 +566,7 @@ class Winner21(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=2, op_openings=0) pattern = "CDCDCDDD" - super().__init__( - parameters=params, pattern=pattern, initial_actions=(D, C) - ) + super().__init__(parameters=params, pattern=pattern, initial_actions=(D, C)) def get_last_n_plays(player: Player, depth: int) -> tuple: diff --git a/axelrod/strategies/memoryone.py b/axelrod/strategies/memoryone.py index 7773cd63f..a8ad1c571 100644 --- a/axelrod/strategies/memoryone.py +++ b/axelrod/strategies/memoryone.py @@ -66,9 +66,7 @@ class MemoryOnePlayer(Player): } def __init__( - self, - four_vector: Tuple[float, float, float, float] = None, - initial: Action = C, + self, four_vector: Tuple[float, float, float, float] = None, initial: Action = C ) -> None: """ Parameters @@ -111,9 +109,7 @@ def set_four_vector(self, four_vector: Tuple[float, float, float, float]): "An element in the probability vector, {}, is not " "between 0 and 1.".format(str(four_vector)) ) - self._four_vector = dict( - zip([(C, C), (C, D), (D, C), (D, D)], four_vector) - ) + self._four_vector = dict(zip([(C, C), (C, D), (D, C), (D, D)], four_vector)) def _post_init(self): # Adjust classifiers diff --git a/axelrod/strategies/memorytwo.py b/axelrod/strategies/memorytwo.py index 43b5e539d..9573c308f 100644 --- a/axelrod/strategies/memorytwo.py +++ b/axelrod/strategies/memorytwo.py @@ -53,9 +53,7 @@ class MemoryTwoPlayer(Player): } def __init__( - self, - sixteen_vector: Optional[Tuple[float, ...]] = None, - initial: Optional[Tuple[Action, Action]] = None, + self, sixteen_vector: Optional[Tuple[float, ...]] = None, initial: Optional[Tuple[Action, Action]] = None ) -> None: """ Parameters @@ -71,9 +69,7 @@ def __init__( self._initial = initial self.set_initial_sixteen_vector(sixteen_vector) - def set_initial_sixteen_vector( - self, sixteen_vector: Optional[Tuple[float, ...]] - ): + def set_initial_sixteen_vector(self, sixteen_vector: Optional[Tuple[float, ...]]): if sixteen_vector is None: sixteen_vector = tuple([1] * 16) warnings.warn("Memory two player is set to default, Cooperator.") @@ -88,8 +84,7 @@ def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]): ) states = [ - (hist[:2], hist[2:]) - for hist in list(itertools.product((C, D), repeat=4)) + (hist[:2], hist[2:]) for hist in list(itertools.product((C, D), repeat=4)) ] self._sixteen_vector = dict( @@ -97,9 +92,7 @@ def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]): ) # type: Dict[tuple, float] @staticmethod - def compute_memory_depth( - sixteen_vector: Dict[Tuple[Action, Action], float] - ) -> int: + def compute_memory_depth(sixteen_vector: Dict[Tuple[Action, Action], float]) -> int: values = set(list(sixteen_vector.values())) # Memory-depth 0 @@ -126,9 +119,7 @@ def compute_memory_depth( def _post_init(self): values = set(self._sixteen_vector.values()) self.classifier["stochastic"] = any(0 < x < 1 for x in values) - self.classifier["memory_depth"] = self.compute_memory_depth( - self._sixteen_vector - ) + self.classifier["memory_depth"] = self.compute_memory_depth(self._sixteen_vector) def strategy(self, opponent: Player) -> Action: turn = len(self.history) @@ -268,11 +259,7 @@ class MEM2(Player): def __init__(self) -> None: super().__init__() - self.players = { - "TFT": TitForTat(), - "TFTT": TitFor2Tats(), - "ALLD": Defector(), - } + self.players = {"TFT": TitForTat(), "TFTT": TitFor2Tats(), "ALLD": Defector()} self.play_as = "TFT" self.shift_counter = 3 self.alld_counter = 0 diff --git a/axelrod/strategies/meta.py b/axelrod/strategies/meta.py index b27907529..5f15b340e 100644 --- a/axelrod/strategies/meta.py +++ b/axelrod/strategies/meta.py @@ -104,9 +104,7 @@ def update_histories(self, coplay): # As a sanity check, look for at least one reclassifier, otherwise # this try-except clause could hide a bug. if len(self._reclassifiers) == 0: - raise TypeError( - "MetaClass update_histories issue, expected a reclassifier." - ) + raise TypeError("MetaClass update_histories issue, expected a reclassifier.") # Otherwise just update with C always, so at least the histories have the # expected length. for player in self.team: @@ -236,9 +234,7 @@ def _post_init(self): # If the team has repeated identical members, then it reduces to a singular team # and it may not actually be stochastic. if team and len(set(team)) == 1: - self.classifier["stochastic"] = Classifiers["stochastic"]( - self.team[0] - ) + self.classifier["stochastic"] = Classifiers["stochastic"](self.team[0]) self.singular = True def meta_strategy(self, results, opponent): @@ -555,9 +551,7 @@ def _post_init(self): if distribution and len(set(distribution)) > 1: self.classifier["stochastic"] = True if len(self.team) == 1: - self.classifier["stochastic"] = Classifiers["stochastic"]( - self.team[0] - ) + self.classifier["stochastic"] = Classifiers["stochastic"](self.team[0]) # Overwrite strategy to avoid use of _random. This will ignore self.meta_strategy. self.index = 0 self.strategy = self.index_strategy @@ -570,9 +564,7 @@ def _post_init(self): if 1 in distribution: self.index = list(distribution).index(1) # It's potentially deterministic. - self.classifier["stochastic"] = Classifiers["stochastic"]( - self.team[self.index] - ) + self.classifier["stochastic"] = Classifiers["stochastic"](self.team[self.index]) # Overwrite strategy to avoid use of _random. This will ignore self.meta_strategy. self.strategy = self.index_strategy diff --git a/axelrod/strategies/prober.py b/axelrod/strategies/prober.py index dcfb828a1..0f5293c61 100644 --- a/axelrod/strategies/prober.py +++ b/axelrod/strategies/prober.py @@ -80,7 +80,7 @@ def strategy(self, opponent: Player) -> Action: return self.initial_actions[hist_size] if D not in opponent.history[:init_size]: return D - return opponent.history[-1] # TFT + return opponent.history[-1] # TFT class Prober(Player): diff --git a/axelrod/strategies/punisher.py b/axelrod/strategies/punisher.py index f976bf370..262901675 100644 --- a/axelrod/strategies/punisher.py +++ b/axelrod/strategies/punisher.py @@ -51,9 +51,7 @@ def strategy(self, opponent: Player) -> Action: return D elif D in opponent.history[-1:]: - self.mem_length = (opponent.defections * 20) // len( - opponent.history - ) + self.mem_length = (opponent.defections * 20) // len(opponent.history) self.grudged = True return D @@ -103,9 +101,7 @@ def strategy(self, opponent: Player) -> Action: self.grudge_memory += 1 return D elif D in opponent.history[-1:]: - self.mem_length = (opponent.cooperations * 20) // len( - opponent.history - ) + self.mem_length = (opponent.cooperations * 20) // len(opponent.history) if self.mem_length == 0: self.mem_length += 1 self.grudged = True diff --git a/axelrod/strategies/qlearner.py b/axelrod/strategies/qlearner.py index 1a81ae227..ef2819e01 100644 --- a/axelrod/strategies/qlearner.py +++ b/axelrod/strategies/qlearner.py @@ -65,9 +65,7 @@ def strategy(self, opponent: Player) -> Action: if state not in self.Qs: self.Qs[state] = OrderedDict(zip([C, D], [0, 0])) self.Vs[state] = 0 - self.perform_q_learning( - self.prev_state, state, self.prev_action, reward - ) + self.perform_q_learning(self.prev_state, state, self.prev_action, reward) action = self.select_action(state) self.prev_state = state self.prev_action = action @@ -92,22 +90,16 @@ def find_state(self, opponent: Player) -> str: action_str = actions_to_str(opponent.history[-self.memory_length :]) return action_str + prob - def perform_q_learning( - self, prev_state: str, state: str, action: Action, reward - ): + def perform_q_learning(self, prev_state: str, state: str, action: Action, reward): """ Performs the qlearning algorithm """ - self.Qs[prev_state][action] = (1.0 - self.learning_rate) * self.Qs[ - prev_state - ][action] + self.learning_rate * ( - reward + self.discount_rate * self.Vs[state] - ) + self.Qs[prev_state][action] = (1.0 - self.learning_rate) * self.Qs[prev_state][ + action + ] + self.learning_rate * (reward + self.discount_rate * self.Vs[state]) self.Vs[prev_state] = max(self.Qs[prev_state].values()) - def find_reward( - self, opponent: Player - ) -> Dict[Action, Dict[Action, Score]]: + def find_reward(self, opponent: Player) -> Dict[Action, Dict[Action, Score]]: """ Finds the reward gained on the last iteration """ diff --git a/axelrod/strategies/shortmem.py b/axelrod/strategies/shortmem.py index e1cd7a6fb..f110f56b8 100644 --- a/axelrod/strategies/shortmem.py +++ b/axelrod/strategies/shortmem.py @@ -22,7 +22,7 @@ class ShortMem(Player): name = "ShortMem" classifier = { - "memory_depth": float("inf"), + "memory_depth": float('inf'), "stochastic": False, "long_run_time": False, "inspects_source": False, diff --git a/axelrod/strategies/titfortat.py b/axelrod/strategies/titfortat.py index d3ab2d68d..31c20b639 100644 --- a/axelrod/strategies/titfortat.py +++ b/axelrod/strategies/titfortat.py @@ -1,9 +1,6 @@ from axelrod.action import Action, actions_to_str from axelrod.player import Player -from axelrod.strategy_transformers import ( - FinalTransformer, - TrackHistoryTransformer, -) +from axelrod.strategy_transformers import FinalTransformer, TrackHistoryTransformer C, D = Action.C, Action.D @@ -131,9 +128,7 @@ def strategy(self, opponent): return C if D in opponent.history[-2:]: # Probability of cooperating regardless - return self._random.random_choice( - opponent.cooperations / len(opponent.history) - ) + return self._random.random_choice(opponent.cooperations / len(opponent.history)) else: return C diff --git a/axelrod/strategies/zero_determinant.py b/axelrod/strategies/zero_determinant.py index 89852ae20..7b849eadb 100644 --- a/axelrod/strategies/zero_determinant.py +++ b/axelrod/strategies/zero_determinant.py @@ -151,9 +151,7 @@ class ZDExtort2v2(LRPlayer): name = "ZD-Extort-2 v2" - def __init__( - self, phi: float = 1 / 8, s: float = 0.5, l: float = 1 - ) -> None: + def __init__(self, phi: float = 1 / 8, s: float = 0.5, l: float = 1) -> None: super().__init__(phi, s, l) @@ -170,9 +168,7 @@ class ZDExtort3(LRPlayer): name = "ZD-Extort3" - def __init__( - self, phi: float = 3 / 26, s: float = 1 / 3, l: float = 1 - ) -> None: + def __init__(self, phi: float = 3 / 26, s: float = 1 / 3, l: float = 1) -> None: super().__init__(phi, s, l) @@ -189,9 +185,7 @@ class ZDExtort4(LRPlayer): name = "ZD-Extort-4" - def __init__( - self, phi: float = 4 / 17, s: float = 0.25, l: float = 1 - ) -> None: + def __init__(self, phi: float = 4 / 17, s: float = 0.25, l: float = 1) -> None: super().__init__(phi, s, l) @@ -206,9 +200,7 @@ class ZDGen2(LRPlayer): name = "ZD-GEN-2" - def __init__( - self, phi: float = 1 / 8, s: float = 0.5, l: float = 3 - ) -> None: + def __init__(self, phi: float = 1 / 8, s: float = 0.5, l: float = 3) -> None: super().__init__(phi, s, l) @@ -259,7 +251,5 @@ class ZDSet2(LRPlayer): name = "ZD-SET-2" - def __init__( - self, phi: float = 1 / 4, s: float = 0.0, l: float = 2 - ) -> None: + def __init__(self, phi: float = 1 / 4, s: float = 0.0, l: float = 2) -> None: super().__init__(phi, s, l) diff --git a/axelrod/strategy_transformers.py b/axelrod/strategy_transformers.py index 70773ff60..a7297382d 100644 --- a/axelrod/strategy_transformers.py +++ b/axelrod/strategy_transformers.py @@ -107,30 +107,20 @@ def __call__(self, PlayerClass): reclassifiers = PlayerClass._reclassifiers.copy() if reclassifier is not None: - reclassifiers.append( - ( - makes_use_of_reclassifier, - (PlayerClass, strategy_wrapper), - {}, - ) - ) + reclassifiers.append((makes_use_of_reclassifier, (PlayerClass, strategy_wrapper), {})) # This one is second on the assumption that the wrapper reclassifier knows best. reclassifiers.append((reclassifier, args, kwargs)) # First handle the case where the strategy method is static. if is_strategy_static(PlayerClass): - def inner_strategy(self, opponent): return PlayerClass.strategy(opponent) - else: - def inner_strategy(self, opponent): return PlayerClass.strategy(self, opponent) # For the dual wrapper, we flip the history before and after the transform. if strategy_wrapper == dual_wrapper: - def dual_inner_strategy(self, opponent): """The dual wrapper requires flipping the history. It may be more efficient to use a custom History class that tracks a flipped history and swaps labels.""" @@ -138,19 +128,16 @@ def dual_inner_strategy(self, opponent): proposed_action = inner_strategy(self, opponent) self._history = self.history.flip_plays() return proposed_action - outer_strategy = dual_inner_strategy # For the JossAnn transformer, we want to avoid calling the wrapped strategy, # in the cases where it is unnecessary, to avoid affecting stochasticity. elif strategy_wrapper == joss_ann_wrapper: - def joss_ann_inner_strategy(self, opponent): if not self.classifier["stochastic"]: proposed_action = C else: proposed_action = inner_strategy(self, opponent) return proposed_action - outer_strategy = joss_ann_inner_strategy else: outer_strategy = inner_strategy @@ -740,15 +727,13 @@ def __call__(self, player, opponent, action, retaliations): def retailiation_reclassifier(original_classifier, retaliations): if retaliations > 0: original_classifier["memory_depth"] = max( - retaliations, original_classifier["memory_depth"] - ) + retaliations, original_classifier["memory_depth"]) return original_classifier RetaliationTransformer = StrategyTransformerFactory( - RetaliationWrapper(), - name_prefix="Retaliating", - reclassifier=retailiation_reclassifier, + RetaliationWrapper(), name_prefix="Retaliating", + reclassifier=retailiation_reclassifier ) @@ -766,13 +751,11 @@ def __call__(self, player, opponent, action): def rua_reclassifier(original_classifier): original_classifier["memory_depth"] = max( - 1, original_classifier["memory_depth"] - ) + 1, original_classifier["memory_depth"]) return original_classifier RetaliateUntilApologyTransformer = StrategyTransformerFactory( - RetaliationUntilApologyWrapper(), - name_prefix="RUA", - reclassifier=rua_reclassifier, + RetaliationUntilApologyWrapper(), name_prefix="RUA", + reclassifier=rua_reclassifier ) diff --git a/axelrod/tests/integration/test_filtering.py b/axelrod/tests/integration/test_filtering.py index 4f3b57453..43226caf6 100644 --- a/axelrod/tests/integration/test_filtering.py +++ b/axelrod/tests/integration/test_filtering.py @@ -36,9 +36,7 @@ def test_boolean_filtering(self, strategies): for classifier in classifiers: comprehension = set(filter(axl.Classifiers[classifier], strategies)) filterset = {classifier: True} - filtered = set( - axl.filtered_strategies(filterset, strategies=strategies) - ) + filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered) @given( @@ -92,9 +90,7 @@ def test_memory_depth_filtering( ] ) filterset = {"memory_depth": memory_depth} - filtered = set( - axl.filtered_strategies(filterset, strategies=strategies) - ) + filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered) @given(strategies=strategy_lists(min_size=20, max_size=20)) @@ -110,16 +106,12 @@ def test_makes_use_of_filtering(self, strategies): [ s for s in strategies - if set(classifier).issubset( - set(axl.Classifiers["makes_use_of"](s)) - ) + if set(classifier).issubset(set(axl.Classifiers["makes_use_of"](s))) ] ) filterset = {"makes_use_of": classifier} - filtered = set( - axl.filtered_strategies(filterset, strategies=strategies) - ) + filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) diff --git a/axelrod/tests/integration/test_matches.py b/axelrod/tests/integration/test_matches.py index 46f4e318f..d46ded024 100644 --- a/axelrod/tests/integration/test_matches.py +++ b/axelrod/tests/integration/test_matches.py @@ -9,14 +9,10 @@ C, D = axl.Action.C, axl.Action.D deterministic_strategies = [ - s - for s in axl.short_run_time_strategies - if not axl.Classifiers["stochastic"](s()) + s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s()) ] stochastic_strategies = [ - s - for s in axl.short_run_time_strategies - if axl.Classifiers["stochastic"](s()) + s for s in axl.short_run_time_strategies if axl.Classifiers["stochastic"](s()) ] diff --git a/axelrod/tests/integration/test_names.py b/axelrod/tests/integration/test_names.py index ad4ff85af..56061b959 100644 --- a/axelrod/tests/integration/test_names.py +++ b/axelrod/tests/integration/test_names.py @@ -5,7 +5,7 @@ class TestNames(unittest.TestCase): def test_all_strategies_have_names(self): - names = [s.name for s in axl.all_strategies if s.name != "Player"] + names = [s.name for s in axl.all_strategies if s.name != 'Player'] self.assertEqual(len(names), len(axl.all_strategies)) def test_all_names_are_unique(self): diff --git a/axelrod/tests/integration/test_tournament.py b/axelrod/tests/integration/test_tournament.py index 8568e165d..d2d26cf56 100644 --- a/axelrod/tests/integration/test_tournament.py +++ b/axelrod/tests/integration/test_tournament.py @@ -51,9 +51,7 @@ def test_big_tournaments(self, tournament): path = pathlib.Path("test_outputs/test_tournament.csv") filename = axl_filename(path) self.assertIsNone( - tournament.play( - progress_bar=False, filename=filename, build_results=False - ) + tournament.play(progress_bar=False, filename=filename, build_results=False) ) def test_serial_play(self): @@ -96,13 +94,9 @@ def test_repeat_tournament_deterministic(self): turns=2, repetitions=2, ) - path = pathlib.Path( - "test_outputs/stochastic_tournament_{}.csv".format(_) - ) + path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_)) files.append(axl_filename(path)) - tournament.play( - progress_bar=False, filename=files[-1], build_results=False - ) + tournament.play(progress_bar=False, filename=files[-1], build_results=False) self.assertTrue(filecmp.cmp(files[0], files[1])) def test_repeat_tournament_stochastic(self): @@ -122,15 +116,11 @@ def test_repeat_tournament_stochastic(self): game=self.game, turns=2, repetitions=2, - seed=17, - ) - path = pathlib.Path( - "test_outputs/stochastic_tournament_{}.csv".format(_) + seed=17 ) + path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_)) files.append(axl_filename(path)) - tournament.play( - progress_bar=False, filename=files[-1], build_results=False - ) + tournament.play(progress_bar=False, filename=files[-1], build_results=False) self.assertTrue(filecmp.cmp(files[0], files[1])) @@ -172,9 +162,7 @@ def test_matches_have_different_length(self): p2 = axl.Cooperator() p3 = axl.Cooperator() players = [p1, p2, p3] - tournament = axl.Tournament( - players, prob_end=0.5, repetitions=2, seed=3 - ) + tournament = axl.Tournament(players, prob_end=0.5, repetitions=2, seed=3) results = tournament.play(progress_bar=False) # Check that match length are different across the repetitions self.assertNotEqual(results.match_lengths[0], results.match_lengths[1]) diff --git a/axelrod/tests/property.py b/axelrod/tests/property.py index d5d1919d9..c3dc009cc 100644 --- a/axelrod/tests/property.py +++ b/axelrod/tests/property.py @@ -4,21 +4,12 @@ import itertools import axelrod as axl -from hypothesis.strategies import ( - composite, - floats, - integers, - lists, - sampled_from, -) +from hypothesis.strategies import composite, floats, integers, lists, sampled_from @composite def strategy_lists( - draw, - strategies=axl.short_run_time_strategies, - min_size=1, - max_size=len(axl.strategies), + draw, strategies=axl.short_run_time_strategies, min_size=1, max_size=len(axl.strategies) ): """ A hypothesis decorator to return a list of strategies @@ -115,20 +106,14 @@ def tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists( - strategies=strategies, min_size=min_size, max_size=max_size - ) + strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) ) players = [s() for s in strategies] turns = draw(integers(min_value=min_turns, max_value=max_turns)) - repetitions = draw( - integers(min_value=min_repetitions, max_value=max_repetitions) - ) + repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = axl.Tournament( - players, turns=turns, repetitions=repetitions, noise=noise - ) + tournament = axl.Tournament(players, turns=turns, repetitions=repetitions, noise=noise) return tournament @@ -144,7 +129,7 @@ def prob_end_tournaments( max_noise=1, min_repetitions=1, max_repetitions=20, - seed=None, + seed=None ): """ A hypothesis decorator to return a tournament, @@ -173,23 +158,15 @@ def prob_end_tournaments( Random seed """ strategies = draw( - strategy_lists( - strategies=strategies, min_size=min_size, max_size=max_size - ) + strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) ) players = [s() for s in strategies] prob_end = draw(floats(min_value=min_prob_end, max_value=max_prob_end)) - repetitions = draw( - integers(min_value=min_repetitions, max_value=max_repetitions) - ) + repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( - players, - prob_end=prob_end, - repetitions=repetitions, - noise=noise, - seed=seed, + players, prob_end=prob_end, repetitions=repetitions, noise=noise, seed=seed ) return tournament @@ -232,30 +209,29 @@ def spatial_tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists( - strategies=strategies, min_size=min_size, max_size=max_size - ) + strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) ) players = [s() for s in strategies] player_indices = list(range(len(players))) all_potential_edges = list(itertools.combinations(player_indices, 2)) all_potential_edges.extend([(i, i) for i in player_indices]) # Loops - edges = draw(lists(sampled_from(all_potential_edges), unique=True,)) + edges = draw( + lists( + sampled_from(all_potential_edges), + unique=True, + ) + ) # Ensure all players/nodes are connected: node_indices = sorted(set([node for edge in edges for node in edge])) - missing_nodes = [ - index for index in player_indices if index not in node_indices - ] + missing_nodes = [index for index in player_indices if index not in node_indices] for index in missing_nodes: opponent = draw(sampled_from(player_indices)) edges.append((index, opponent)) turns = draw(integers(min_value=min_turns, max_value=max_turns)) - repetitions = draw( - integers(min_value=min_repetitions, max_value=max_repetitions) - ) + repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( @@ -302,38 +278,33 @@ def prob_end_spatial_tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists( - strategies=strategies, min_size=min_size, max_size=max_size - ) + strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) ) players = [s() for s in strategies] player_indices = list(range(len(players))) all_potential_edges = list(itertools.combinations(player_indices, 2)) all_potential_edges.extend([(i, i) for i in player_indices]) # Loops - edges = draw(lists(sampled_from(all_potential_edges), unique=True,)) + edges = draw( + lists( + sampled_from(all_potential_edges), + unique=True, + ) + ) # Ensure all players/nodes are connected: node_indices = sorted(set([node for edge in edges for node in edge])) - missing_nodes = [ - index for index in player_indices if index not in node_indices - ] + missing_nodes = [index for index in player_indices if index not in node_indices] for index in missing_nodes: opponent = draw(sampled_from(player_indices)) edges.append((index, opponent)) prob_end = draw(floats(min_value=min_prob_end, max_value=max_prob_end)) - repetitions = draw( - integers(min_value=min_repetitions, max_value=max_repetitions) - ) + repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( - players, - prob_end=prob_end, - repetitions=repetitions, - noise=noise, - edges=edges, + players, prob_end=prob_end, repetitions=repetitions, noise=noise, edges=edges ) return tournament diff --git a/axelrod/tests/strategies/test_adaptive.py b/axelrod/tests/strategies/test_adaptive.py index 330195ca8..20befc067 100644 --- a/axelrod/tests/strategies/test_adaptive.py +++ b/axelrod/tests/strategies/test_adaptive.py @@ -54,9 +54,7 @@ def test_scoring_with_default_game(self): opponent = axl.Cooperator() attrs = {"scores": {C: 3, D: 0}} expected_actions = list(zip([C, C], [C, C])) - self.versus_test( - opponent, expected_actions, turns=2, attrs=attrs, seed=9 - ) + self.versus_test(opponent, expected_actions, turns=2, attrs=attrs, seed=9) def test_scoring_with_alternate_game(self): """Tests that the alternate game is used in scoring.""" @@ -65,11 +63,5 @@ def test_scoring_with_alternate_game(self): expected_actions = list(zip([C, C, C], [C, D, C])) attrs = {"scores": {C: 7, D: 0}} match_attributes = {"game": axl.Game(-3, 10, 10, 10)} - self.versus_test( - opponent, - expected_actions, - turns=3, - attrs=attrs, - seed=9, - match_attributes=match_attributes, - ) + self.versus_test(opponent, expected_actions, turns=3, attrs=attrs, seed=9, + match_attributes=match_attributes) diff --git a/axelrod/tests/strategies/test_ann.py b/axelrod/tests/strategies/test_ann.py index c2a8290fe..985462e34 100644 --- a/axelrod/tests/strategies/test_ann.py +++ b/axelrod/tests/strategies/test_ann.py @@ -18,7 +18,6 @@ class TestSplitWeights(unittest.TestCase): def test_split_weights(self): with self.assertRaises(ValueError): split_weights([0] * 20, 12, 10) - # Doesn't Raise split_weights([0] * 70, 5, 10) split_weights([0] * 12, 10, 1) @@ -131,7 +130,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): init_parameters = { "num_features": nn_weights["Evolved ANN 5"][0], "num_hidden": nn_weights["Evolved ANN 5"][1], - "weights": nn_weights["Evolved ANN 5"][2], + "weights": nn_weights["Evolved ANN 5"][2] } @@ -140,7 +139,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): axl.EvolvableANN, num_features=num_features, num_hidden=num_hidden, - weights=weights, + weights=weights ) diff --git a/axelrod/tests/strategies/test_apavlov.py b/axelrod/tests/strategies/test_apavlov.py index 3b37f1278..8d3288a93 100644 --- a/axelrod/tests/strategies/test_apavlov.py +++ b/axelrod/tests/strategies/test_apavlov.py @@ -34,9 +34,7 @@ def test_strategy_versus_mock_player(self): opponent = axl.MockPlayer(actions=[C] * 6 + [D]) actions = [(C, C)] * 6 + [(C, D), (D, C)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "Cooperative"}, + opponent, expected_actions=actions, attrs={"opponent_class": "Cooperative"} ) def test_strategy_versus_defector(self): @@ -72,47 +70,30 @@ def test_strategy_PavlovD(self): opponent = axl.Cycler(cycle="DDC") actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "PavlovD"}, + opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} ) def test_strategy_PavlovD2(self): """Tests that PavolvD is identified by DDCDDC and that the response is D then C""" opponent = axl.MockPlayer(actions=[D, D, C, D, D, C, D]) - actions = [ - (C, D), - (D, D), - (D, C), - (C, D), - (D, D), - (D, C), - (D, D), - (C, D), - ] + actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D), (C, D)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "PavlovD"}, + opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} ) def test_strategy_random(self): opponent = axl.MockPlayer(actions=[C, C, C, D, D, D]) actions = [(C, C), (C, C), (C, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "Random"}, + opponent, expected_actions=actions, attrs={"opponent_class": "Random"} ) def test_strategy_random2(self): opponent = axl.MockPlayer(actions=[D, D, D, C, C, C]) actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (D, D)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "Random"}, + opponent, expected_actions=actions, attrs={"opponent_class": "Random"} ) @@ -162,100 +143,42 @@ def test_strategy_defector3(self): def test_strategy_defector4(self): opponent = axl.MockPlayer(actions=[C, D, D, C, D, D, D]) - actions = [ - (C, C), - (C, D), - (D, D), - (D, C), - (C, D), - (D, D), - (D, D), - (D, C), - ] + actions = [(C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"} ) def test_strategy_stft(self): opponent = axl.MockPlayer(actions=[C, D, D, C, C, D, D]) - actions = [ - (C, C), - (C, D), - (D, D), - (D, C), - (C, C), - (C, D), - (C, D), - (D, C), - ] + actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_stft2(self): opponent = axl.MockPlayer(actions=[C, D, C, D, C, D, D]) - actions = [ - (C, C), - (C, D), - (D, C), - (C, D), - (D, C), - (C, D), - (C, D), - (D, C), - ] + actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_stft3(self): opponent = axl.MockPlayer(actions=[D, D, D, C, C, C, C]) - actions = [ - (C, D), - (D, D), - (D, D), - (D, C), - (C, C), - (C, C), - (C, C), - (C, D), - ] + actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (C, C), (C, D)] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_random(self): opponent = axl.MockPlayer(actions=[C, C, C, C, D, D]) - actions = [ - (C, C), - (C, C), - (C, C), - (C, C), - (C, D), - (D, D), - (D, C), - (D, C), - ] + actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, D), (D, C), (D, C)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "Random"}, + opponent, expected_actions=actions, attrs={"opponent_class": "Random"} ) def test_strategy_random2(self): opponent = axl.MockPlayer(actions=[D, D, C, C, C, C]) - actions = [ - (C, D), - (D, D), - (D, C), - (C, C), - (C, C), - (C, C), - (D, D), - (D, D), - ] + actions = [(C, D), (D, D), (D, C), (C, C), (C, C), (C, C), (D, D), (D, D)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_class": "Random"}, + opponent, expected_actions=actions, attrs={"opponent_class": "Random"} ) diff --git a/axelrod/tests/strategies/test_axelrod_first.py b/axelrod/tests/strategies/test_axelrod_first.py index dfb1bd0b9..40f09e80c 100644 --- a/axelrod/tests/strategies/test_axelrod_first.py +++ b/axelrod/tests/strategies/test_axelrod_first.py @@ -107,9 +107,7 @@ class TestFirstByFeld(TestPlayer): def test_cooperation_probability(self): # Test cooperation probabilities - p1 = self.player( - start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100 - ) + p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100) self.assertEqual(1.0, p1._cooperation_probability()) p2 = axl.Cooperator() match = axl.Match((p1, p2), turns=50) @@ -120,9 +118,7 @@ def test_cooperation_probability(self): self.assertEqual(0.8, p1._cooperation_probability()) # Test cooperation probabilities, second set of params - p1 = self.player( - start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200 - ) + p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200) self.assertEqual(1.0, p1._cooperation_probability()) match = axl.Match((p1, p2), turns=100) match.play() @@ -135,14 +131,10 @@ def test_decay(self): # Test beyond 200 rounds for opponent in [axl.Cooperator(), axl.Defector()]: player = self.player() - self.assertEqual( - player._cooperation_probability(), player._start_coop_prob - ) + self.assertEqual(player._cooperation_probability(), player._start_coop_prob) match = axl.Match((player, opponent), turns=201) match.play() - self.assertEqual( - player._cooperation_probability(), player._end_coop_prob - ) + self.assertEqual(player._cooperation_probability(), player._end_coop_prob) def test_stochastic_behavior(self): actions = [(C, C)] * 13 + [(D, C)] @@ -184,28 +176,19 @@ def test_strategy(self): actions += [(D, C)] # 51 turns actions += [(C, D), (D, C)] * 2 + [(C, D)] # 56 turns self.versus_test( - axl.Alternator(), - expected_actions=actions, - attrs=expected_attrs, - seed=0, + axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=0 ) # Against defector actions = [(C, D)] + [(D, D)] * 55 # 56 turns self.versus_test( - axl.Defector(), - expected_actions=actions, - attrs=expected_attrs, - seed=0, + axl.Defector(), expected_actions=actions, attrs=expected_attrs, seed=0 ) # Against cooperator actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs=expected_attrs, - seed=0, + axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=0 ) # Test recognition of random player @@ -215,21 +198,12 @@ def test_strategy(self): } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 # 56 turns self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs=expected_attrs, - seed=1, + axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=1 ) - expected_attrs = { - "opponent_is_random": False, - "next_random_defection_turn": 63, - } + expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 63} actions += [(C, C)] # 57 turns self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs=expected_attrs, - seed=8, + axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=8 ) expected_attrs = { @@ -241,17 +215,11 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 57 turns actions += [(D, D)] self.versus_test( - axl.Alternator(), - expected_actions=actions, - attrs=expected_attrs, - seed=3, + axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=3 ) actions += [(D, C), (D, D)] * 5 self.versus_test( - axl.Alternator(), - expected_actions=actions, - attrs=expected_attrs, - seed=4, + axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=4 ) # Test versus TfT @@ -263,35 +231,20 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 56 turns actions += [(C, D), (D, C)] * 50 self.versus_test( - axl.TitForTat(), - expected_actions=actions, - seed=0, - attrs=expected_attrs, + axl.TitForTat(), expected_actions=actions, seed=0, attrs=expected_attrs ) # Test random defections - expected_attrs = { - "opponent_is_random": False, - "next_random_defection_turn": 76, - } + expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 76} actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 15 + [(D, C)] + [(C, C)] self.versus_test( - axl.Cooperator(), - expected_actions=actions, - seed=0, - attrs=expected_attrs, + axl.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs ) - expected_attrs = { - "opponent_is_random": False, - "next_random_defection_turn": 79, - } + expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 79} actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 14 + [(D, C)] + [(C, C)] self.versus_test( - axl.Cooperator(), - expected_actions=actions, - seed=5, - attrs=expected_attrs, + axl.Cooperator(), expected_actions=actions, seed=5, attrs=expected_attrs ) @@ -317,29 +270,11 @@ def test_strategy(self): self.versus_test(axl.Alternator(), expected_actions=actions, seed=0) opponent = axl.MockPlayer(actions=[D] * 8) - actions = [ - (C, D), - (C, D), - (D, D), - (C, D), - (D, D), - (C, D), - (D, D), - (C, D), - ] + actions = [(C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=1) opponent = axl.MockPlayer(actions=[D] * 8) - actions = [ - (C, D), - (D, D), - (C, D), - (D, D), - (C, D), - (D, D), - (C, D), - (C, D), - ] + actions = [(C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -414,41 +349,14 @@ def test_strategy(self): actions = [(C, C)] * 9 self.versus_test(axl.Cooperator(), expected_actions=actions) - actions = [ - (C, D), - (D, D), - (D, D), - (C, D), - (C, D), - (C, D), - (C, D), - (C, D), - ] + actions = [(C, D), (D, D), (D, D), (C, D), (C, D), (C, D), (C, D), (C, D)] self.versus_test(axl.Defector(), expected_actions=actions) - actions = [ - (C, C), - (C, D), - (D, C), - (C, D), - (D, C), - (C, D), - (D, C), - (C, D), - ] + actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C), (C, D)] self.versus_test(axl.Alternator(), expected_actions=actions) opponent = axl.MockPlayer(actions=[D, C]) - actions = [ - (C, D), - (D, C), - (D, D), - (D, C), - (D, D), - (D, C), - (D, D), - (D, C), - ] + actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (D, D), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -529,9 +437,7 @@ def test_strategy(self): # Test beyond 10 rounds opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 5 + [C, D] * 5) - actions = ( - [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)] - ) + actions = [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)] self.versus_test(opponent, expected_actions=actions, seed=20) @@ -588,9 +494,7 @@ def test_strategy(self): opponent = axl.Cooperator() actions = [(C, C)] * 17 + [(D, C)] * 2 self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_is_random": False}, + opponent, expected_actions=actions, attrs={"opponent_is_random": False} ) actions = actions[:-2] + [(C, C)] * 2 @@ -606,9 +510,7 @@ def test_strategy(self): opponent = axl.Defector() actions = [(C, D)] * 4 + [(D, D)] * 15 self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_is_random": False}, + opponent, expected_actions=actions, attrs={"opponent_is_random": False} ) # SteinAndRapoport vs Alternator @@ -623,9 +525,7 @@ def test_strategy(self): actions += [(D, D), (D, C), (D, D), (D, C)] self.versus_test( - opponent, - expected_actions=actions, - attrs={"opponent_is_random": True}, + opponent, expected_actions=actions, attrs={"opponent_is_random": True} ) # The test is carried out again every 15 rounds. @@ -670,11 +570,8 @@ def test_strategy(self): # Cooperator Test does noot defect if game length is unknown opponent = axl.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] - self.versus_test( - opponent, - expected_actions=actions, - match_attributes={"length": float("inf")}, - ) + self.versus_test(opponent, expected_actions=actions, + match_attributes={"length": float("inf")}) # Defector Test opponent = axl.Defector() diff --git a/axelrod/tests/strategies/test_axelrod_second.py b/axelrod/tests/strategies/test_axelrod_second.py index 5172eb89a..26fa8078e 100644 --- a/axelrod/tests/strategies/test_axelrod_second.py +++ b/axelrod/tests/strategies/test_axelrod_second.py @@ -112,9 +112,7 @@ def test_strategy(self): # Now play TfT opponent = axl.MockPlayer(actions=[C, D, C, D, D, C]) actions = [(D, C), (C, D), (C, C), (C, D), (D, D), (D, C), (C, C)] - self.versus_test( - opponent, expected_actions=actions, attrs={"is_TFT": True} - ) + self.versus_test(opponent, expected_actions=actions, attrs={"is_TFT": True}) class TestGladstein(TestPlayer): @@ -153,15 +151,11 @@ def test_strategy(self): # Ratio is 1/3 when MockPlayer defected for the first time. opponent = axl.MockPlayer(actions=[C, C, C, D, D]) actions = [(D, C), (C, C), (C, C), (D, D), (C, D)] - self.versus_test( - opponent, expected_actions=actions, attrs={"patsy": False} - ) + self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) opponent = axl.AntiTitForTat() actions = [(D, C), (C, C), (C, D), (C, D), (D, D)] - self.versus_test( - opponent, expected_actions=actions, attrs={"patsy": False} - ) + self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) class TestTranquilizer(TestPlayer): @@ -190,16 +184,7 @@ def test_initialised_variables(self): def test_strategy(self): opponent = axl.Bully() - actions = [ - (C, D), - (D, D), - (D, C), - (C, C), - (C, D), - (D, D), - (D, C), - (C, C), - ] + actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, D), (D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, @@ -207,9 +192,7 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test( - opponent, expected_actions=actions, attrs=expected_attrs - ) + self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) # Tests whether TitForTat is played given score is below 1.75 @@ -222,14 +205,10 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test( - opponent, expected_actions=actions, attrs=expected_attrs - ) + self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) opponent = axl.MockPlayer([C] * 2 + [D] * 8 + [C] * 4) - actions = ( - [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 - ) + actions = [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, @@ -237,9 +216,7 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test( - opponent, expected_actions=actions, attrs=expected_attrs - ) + self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) def test_strategy2(self): # If score is between 1.75 and 2.25, may cooperate or defect @@ -379,9 +356,7 @@ def test_strategy10(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test( - opponent, expected_actions=actions, attrs=expected_attrs - ) + self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) class TestGrofman(TestPlayer): @@ -628,9 +603,7 @@ def test_strategy(self): # Tries to cooperate every third time until detecting defective actions = ( - [(C, D), (D, D), (D, D), (D, D)] * 6 - + [(C, D), (D, D)] - + [(D, D)] * 100 + [(C, D), (D, D), (D, D), (D, D)] * 6 + [(C, D), (D, D)] + [(D, D)] * 100 ) self.versus_test(axl.Defector(), expected_actions=actions) @@ -683,9 +656,7 @@ def test_stochastic_behavior(self): """Test random responses on turns 1 through 17.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match( - (axl.SecondByCave(), axl.Alternator()), turns=30, seed=1 - ) + match = axl.Match((axl.SecondByCave(), axl.Alternator()), turns=30, seed=1) match.play() player_history = [round[0] for round in match.result] self.assertTrue(C in player_history[1:17]) @@ -696,9 +667,7 @@ def test_serial_defection_against_defector(self): to respond D->D.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match( - (axl.SecondByCave(), axl.Defector()), turns=30, seed=1 - ) + match = axl.Match((axl.SecondByCave(), axl.Defector()), turns=30, seed=1) result = match.play() self.assertEqual(result[0], (C, D)) self.assertEqual(result[18:], [(D, D)] * 12) @@ -710,9 +679,7 @@ def test_serial_defection_against_mostly_defector(self): # makes finding seeds reproducibly difficult. opponent_actions = [D] * 17 + [C, C, C, C] almost_defector = axl.MockPlayer(actions=opponent_actions) - match = axl.Match( - (axl.SecondByCave(), almost_defector), turns=21, seed=1 - ) + match = axl.Match((axl.SecondByCave(), almost_defector), turns=21, seed=1) result = match.play() self.assertEqual(result[0], (C, D)) self.assertEqual(result[-3], (C, C)) @@ -723,12 +690,10 @@ def test_versus_alternator(self): random and defect.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match( - (axl.SecondByCave(), axl.Alternator()), turns=100, seed=1 - ) + match = axl.Match((axl.SecondByCave(), axl.Alternator()), turns=100, seed=1) result = match.play() self.assertEqual(result[0], (C, C)) - self.assertEqual(result[37:40], [(C, D), (D, C), (D, D)]) + self.assertEqual(result[37: 40], [(C, D), (D, C), (D, D)]) self.assertEqual(result[40:], [(D, C), (D, D)] * 30) @@ -958,10 +923,7 @@ def test_exit_fair_weather(self): # Immediately exit Fair-weather actions += [(D, C), (C, C), (D, C), (C, C)] self.versus_test( - Defect37_big, - expected_actions=actions, - seed=10, - attrs={"mode": "Normal"}, + Defect37_big, expected_actions=actions, seed=10, attrs={"mode": "Normal"} ) def test_exit_fair_weather2(self): @@ -972,10 +934,7 @@ def test_exit_fair_weather2(self): opponent_actions = [C] * 36 + [D] + [C] * 100 + [D] + [C] * 4 Defect37_big = axl.MockPlayer(actions=opponent_actions) self.versus_test( - Defect37_big, - expected_actions=actions, - seed=1, - attrs={"mode": "Normal"}, + Defect37_big, expected_actions=actions, seed=1, attrs={"mode": "Normal"} ) def test_non_fair_weather(self): @@ -1056,10 +1015,8 @@ def test_parity_limit_shortening(self): # Now hit the limit sooner actions += [(C, D), (D, C), (C, D), (C, C)] * 5 self.versus_test( - AsyncAlternator, - expected_actions=actions, - attrs={"parity_limit": 3}, - seed=10, + AsyncAlternator, expected_actions=actions, attrs={"parity_limit": 3}, + seed=10 ) def test_detect_streak(self): @@ -1079,15 +1036,14 @@ def test_detect_streak(self): axl.Defector(), expected_actions=actions, attrs={"recorded_defects": 119}, - seed=10, + seed=10 ) def test_detect_random(self): """Tests that detect_random() is triggered on a Random opponent and that the strategy defects thereafter.""" - match = axl.Match( - (axl.SecondByHarrington(), axl.Random()), seed=10, turns=31 - ) + match = axl.Match((axl.SecondByHarrington(), axl.Random()), seed=10, + turns=31) match.play() player = match.players[0] # Check that detect_random(30) is True. @@ -1193,18 +1149,14 @@ def test_strategy(self): actions = [(C, D)] + [(D, D)] * 8 self.versus_test( - axl.Defector(), - expected_actions=actions, - attrs={"score_to_beat_inc": 5}, + axl.Defector(), expected_actions=actions, attrs={"score_to_beat_inc": 5} ) actions = [(C, D)] + [(D, D)] * 8 # On tenth turn, try a fresh start actions += [(C, D), (C, D)] + [(D, D)] * 2 self.versus_test( - axl.Defector(), - expected_actions=actions, - attrs={"last_fresh_start": 11}, + axl.Defector(), expected_actions=actions, attrs={"last_fresh_start": 11} ) actions = [(C, C), (C, D)] @@ -1381,28 +1333,9 @@ def test_defects_on_turn_10_against_defector(self): self.versus_test(axl.Defector(), expected_actions=actions) def test_defection_logic_triggered(self): - actions = [ - (C, D), - (C, D), - (C, C), - (C, D), - (C, D), - (C, C), - (C, D), - (C, D), - (C, C), - (C, D), - (D, D), - (D, C), - (C, D), - (D, D), - (D, C), - (C, D), - (D, D), - (D, C), - (C, D), - (D, D), - ] + actions = [(C, D), (C, D), (C, C), (C, D), (C, D), (C, C), (C, D), (C, D), + (C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (C, D), + (D, D), (D, C), (C, D), (D, D)] self.versus_test(axl.CyclerDDC(), expected_actions=actions) def test_defection_logic_not_triggered(self): @@ -1476,9 +1409,7 @@ class TestRichardHufford(TestPlayer): def test_strategy(self): actions = [(C, C)] * 19 + [(D, C), (C, C), (C, C)] self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs={"streak_needed": 14}, + axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 14} ) actions = [(C, C)] * 19 + [(D, C), (C, C)] @@ -1487,18 +1418,14 @@ def test_strategy(self): ] # This is the first Cooperation that gets counted on the new streak actions += [(C, C)] * 13 + [(D, C), (C, C), (C, C)] self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs={"streak_needed": 11}, + axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 11} ) opponent_actions = [C] * 20 + [D] BoredCooperator = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 19 + [(D, C), (C, D), (C, C)] self.versus_test( - BoredCooperator, - expected_actions=actions, - attrs={"streak_needed": 31}, + BoredCooperator, expected_actions=actions, attrs={"streak_needed": 31} ) actions = [(C, D)] # "Disagreement" @@ -1507,9 +1434,7 @@ def test_strategy(self): actions += [(C, D)] # TFT. Disagreement actions += [(D, C)] # Three of last four are disagreements. actions += [(C, C)] # TFT. Disagreement - actions += [ - (D, D) - ] # Three of last four are disagreements. Disagreement + actions += [(D, D)] # Three of last four are disagreements. Disagreement actions += [(D, D)] # Three of last four are disagreements. actions += [(D, D)] # Now there are 5/9 disagreements, so Defect. self.versus_test( @@ -1545,39 +1470,23 @@ def test_strategy(self): # It's actually impossible to Defect on the third move. actions += [(D, D)] # (D, C, *) gets updated, then checked. actions += [(C, D)] # (D, C, *) gets updated, but (D, D, *) checked. - actions += [ - (D, D) - ] * 30 # (D, D, *) gets updated and checked from here on. + actions += [(D, D)] * 30 # (D, D, *) gets updated and checked from here on. self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D)] - actions += [ - (C, C) - ] # Increment (C, C, C). Check (C, C, *). Cooperate. + actions += [(C, C)] # Increment (C, C, C). Check (C, C, *). Cooperate. # Reminder that first C is default value and last C is opponent's first move. - actions += [ - (C, D) - ] # Increment (C, C, D). Check (D, C, *) = 0. Cooperate. - actions += [ - (C, C) - ] # Increment (D, C, C). Check (C, C, *) = 0. Cooperate. + actions += [(C, D)] # Increment (C, C, D). Check (D, C, *) = 0. Cooperate. + actions += [(C, C)] # Increment (D, C, C). Check (C, C, *) = 0. Cooperate. # There is one Defection and one Cooperation in this scenario, # but the Cooperation was due to a default value only. We can see where this is going. - actions += [ - (C, D) - ] # Increment (C, C, D). Check (D, C, *) = 1. Cooperate. - actions += [ - (D, C) - ] # Increment (D, C, C). Check (C, C, *) = -1. Defect. + actions += [(C, D)] # Increment (C, C, D). Check (D, C, *) = 1. Cooperate. + actions += [(D, C)] # Increment (D, C, C). Check (C, C, *) = -1. Defect. actions += [ (C, D) ] # Increment (C, C, D). Check (D, D, *) = 0 (New). Cooperate. - actions += [ - (D, C) - ] # Increment (D, D, C). Check (C, C, *) < 0. Defect. - actions += [ - (C, D) - ] # Increment (C, C, D). Check (D, D, *) > 0. Cooperate. + actions += [(D, C)] # Increment (D, D, C). Check (C, C, *) < 0. Defect. + actions += [(C, D)] # Increment (C, C, D). Check (D, D, *) > 0. Cooperate. actions += [(D, C), (C, D)] * 15 # This pattern continues for a while. actions += [ (D, C), @@ -1761,9 +1670,7 @@ def test_strategy(self): opponent_actions = [C] * 100 + [D] * 10 Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 100 + [(C, D)] * 4 - self.versus_test( - Change_of_Heart, expected_actions=actions, attrs={"credit": 2} - ) + self.versus_test(Change_of_Heart, expected_actions=actions, attrs={"credit": 2}) Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions += [(C, D)] * 2 self.versus_test( @@ -1771,7 +1678,6 @@ def test_strategy(self): ) # Still Cooperate, because Defect rate is low - class TestRowsam(TestPlayer): name = "Second by Rowsam" player = axl.SecondByRowsam @@ -1793,18 +1699,12 @@ def test_strategy(self): # Against a Defector should eventually enter Defect mode actions = [(C, D)] * 5 actions += [(D, D), (C, D), (D, D)] # Do a Coop-Def cycle - self.versus_test( - axl.Defector(), - expected_actions=actions, - attrs={"distrust_points": 5}, - ) + self.versus_test(axl.Defector(), expected_actions=actions, attrs={ + "distrust_points": 5}) actions += [(C, D)] * 3 # Continue for now actions += [(D, D)] * 100 # Now Defect mode - self.versus_test( - axl.Defector(), - expected_actions=actions, - attrs={"distrust_points": 10, "mode": "Defect"}, - ) + self.versus_test(axl.Defector(), expected_actions=actions, attrs={ + "distrust_points": 10, "mode": "Defect"}) # Test specific score scenarios # 5 Defects @@ -1812,11 +1712,8 @@ def test_strategy(self): custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 5 actions += [(D, C)] - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 5, "current_score": 0}, - ) + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 5, "current_score": 0}) # 3 Defects opponent_actions = [D] * 3 + [C] * 100 @@ -1824,11 +1721,8 @@ def test_strategy(self): actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 3, "current_score": 6}, - ) + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 3, "current_score": 6}) # 2 Defects opponent_actions = [D] * 2 + [C] * 100 @@ -1836,11 +1730,8 @@ def test_strategy(self): actions = [(C, D)] * 2 actions += [(C, C)] * 3 actions += [(D, C)] - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 2, "current_score": 9}, - ) + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 2, "current_score": 9}) # 1 Defect opponent_actions = [D] * 1 + [C] * 100 @@ -1848,11 +1739,8 @@ def test_strategy(self): actions = [(C, D)] * 1 actions += [(C, C)] * 4 actions += [(D, C)] - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 1, "current_score": 12}, - ) + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 1, "current_score": 12}) # Test that some distrust_points wear off. opponent_actions = [D] * 3 + [C] * 100 @@ -1860,42 +1748,27 @@ def test_strategy(self): actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 3, "current_score": 6}, - ) + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 3, "current_score": 6}) custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 3 actions += [(D, C)] - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 4, "current_score": 28}, - ) + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 4, "current_score": 28}) custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 4 # No defect or cycle this time. - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 3, "current_score": 50}, - ) # One point wears off. + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 3, "current_score": 50}) # One point wears off. custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 2}, - ) # Second point wears off + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 2}) # Second point wears off custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 - self.versus_test( - custom_opponent, - expected_actions=actions, - attrs={"distrust_points": 2}, - ) # But no more + self.versus_test(custom_opponent, expected_actions=actions, attrs={ + "distrust_points": 2}) # But no more class TestAppold(TestPlayer): @@ -1914,93 +1787,64 @@ class TestAppold(TestPlayer): def test_cooperate_against_cooperating_opponent(self): """Strategy should cooperate 100% of the time with a fully cooperating opponent.""" actions = [(C, C)] * 100 - self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs={ - "first_opp_def": False, - "total_num_of_x": {C: 99, D: 1}, - "opp_c_after_x": {C: 99, D: 1}, - }, - ) + self.versus_test(axl.Cooperator(), expected_actions=actions, + attrs={ + "first_opp_def": False, + "total_num_of_x": {C: 99, D: 1}, + "opp_c_after_x": {C: 99, D: 1} + }) def test_cooperate_on_first_four_turns(self): """Strategy will cooperate on the first four turns regardless of opponent.""" # Hypothesis opportunity: choose random opponent player_expected_actions = [C, C, C, C] coplayer_expected_actions = [D, D, D, D] - expected_actions = list( - zip(player_expected_actions, coplayer_expected_actions) - ) - self.versus_test( - axl.Defector(), - turns=4, - expected_actions=expected_actions, - attrs={ - "first_opp_def": False, - "total_num_of_x": {C: 3, D: 1}, - "opp_c_after_x": {C: 0, D: 1}, - }, - ) + expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) + self.versus_test(axl.Defector(), turns=4, expected_actions=expected_actions, + attrs={ + "first_opp_def": False, + "total_num_of_x": {C: 3, D: 1}, + "opp_c_after_x": {C: 0, D: 1} + }) def test_fifth_move_cooperate(self): """Strategy will cooperate on a fifth move defection and set first_opp_def.""" player_expected_actions = [C, C, C, C, C, C] coplayer_expected_actions = [C, C, C, C, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list( - zip(player_expected_actions, coplayer_expected_actions) - ) - self.versus_test( - coplayer, - turns=6, - expected_actions=expected_actions, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 5, D: 1}, - "opp_c_after_x": {C: 4, D: 1}, - }, - ) + expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) + self.versus_test(coplayer, turns=6, expected_actions=expected_actions, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 5, D: 1}, + "opp_c_after_x": {C: 4, D: 1} + }) def test_sixth_move_cooperate(self): """Strategy will cooperate on a sixth move defection if it is the first.""" player_expected_actions = [C, C, C, C, C, C, C] coplayer_expected_actions = [C, C, C, C, C, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list( - zip(player_expected_actions, coplayer_expected_actions) - ) - self.versus_test( - coplayer, - turns=7, - expected_actions=expected_actions, - seed=1, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 6, D: 1}, - "opp_c_after_x": {C: 5, D: 1}, - }, - ) + expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) + self.versus_test(coplayer, turns=7, expected_actions=expected_actions, seed=1, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 6, D: 1}, + "opp_c_after_x": {C: 5, D: 1} + }) def test_sixth_move_defect(self): """Strategy will defect on a sixth move defection if it is not the first.""" player_expected_actions = [C, C, C, C, C, C, D] coplayer_expected_actions = [C, C, C, C, D, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list( - zip(player_expected_actions, coplayer_expected_actions) - ) - self.versus_test( - coplayer, - turns=7, - expected_actions=expected_actions, - seed=10, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 6, D: 1}, - "opp_c_after_x": {C: 4, D: 1}, - }, - ) + expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) + self.versus_test(coplayer, turns=7, expected_actions=expected_actions, seed=10, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 6, D: 1}, + "opp_c_after_x": {C: 4, D: 1} + }) def test_later_single_defection_forgiveness(self): # An opponent who defects after a long time, then tries cooperating @@ -2012,12 +1856,8 @@ def test_later_single_defection_forgiveness(self): actions += [(C, D)] # But we forgive it (and record it). actions += [(C, C)] * 10 - self.versus_test( - MostlyCooperates, - expected_actions=actions, - seed=1, - attrs={"first_opp_def": True}, - ) + self.versus_test(MostlyCooperates, expected_actions=actions, seed=1, + attrs={"first_opp_def": True}) def test_stochastic_behavior(self): opponent = axl.Defector() @@ -2031,24 +1871,19 @@ def test_stochastic_behavior(self): # Then defect most of the time, depending on the random number. We # don't defect 100% of the time, because of the way that initialize # opp_c_after_x. - actions += [ - (D, D), - (C, D), - (D, D), - (D, D), # C can never be two moves after a C. - (D, D), - (D, D), - (D, D), - (D, D), - (D, D), - (D, D), - (C, D), - (C, D), - (D, D), + actions += [(D, D), + (C, D), + (D, D), + (D, D), # C can never be two moves after a C. + (D, D), + (D, D), + (D, D), + (D, D), + (D, D), + (D, D), + (C, D), + (C, D), + (D, D), ] - self.versus_test( - opponent, - expected_actions=actions, - seed=1018, - attrs={"first_opp_def": True}, - ) + self.versus_test(opponent, expected_actions=actions, seed=1018, + attrs={"first_opp_def": True}) diff --git a/axelrod/tests/strategies/test_backstabber.py b/axelrod/tests/strategies/test_backstabber.py index c4a988aec..1f580c9cf 100644 --- a/axelrod/tests/strategies/test_backstabber.py +++ b/axelrod/tests/strategies/test_backstabber.py @@ -117,13 +117,9 @@ def test_when_alt_strategy_is_triggered(self): def test_starting_defect_keeps_alt_strategy_from_triggering(self): opponent_actions_suffix = [C, D, C, D, D] + 3 * [C] - expected_actions_suffix = [ - (C, C), - (C, D), - (C, C), - (C, D), - (C, D), - ] + 3 * [(D, C)] + expected_actions_suffix = [(C, C), (C, D), (C, C), (C, D), (C, D)] + 3 * [ + (D, C) + ] defects_on_first = [D] + [C] * 6 defects_on_first_actions = [(C, D)] + [(C, C)] * 6 diff --git a/axelrod/tests/strategies/test_better_and_better.py b/axelrod/tests/strategies/test_better_and_better.py index 6b351fe7e..2499eb4a3 100644 --- a/axelrod/tests/strategies/test_better_and_better.py +++ b/axelrod/tests/strategies/test_better_and_better.py @@ -25,15 +25,21 @@ def test_strategy(self): """Tests that the strategy gives expected behaviour.""" expected_actions = [(D, D)] * 90 + [(C, D)] self.versus_test( - axl.Defector(), expected_actions=expected_actions, seed=6, + axl.Defector(), + expected_actions=expected_actions, + seed=6, ) expected_actions = [(D, C)] * 10 self.versus_test( - axl.Cooperator(), expected_actions=expected_actions, seed=8, + axl.Cooperator(), + expected_actions=expected_actions, + seed=8, ) expected_actions = [(D, D)] * 41 + [(C, D)] self.versus_test( - axl.Defector(), expected_actions=expected_actions, seed=13, + axl.Defector(), + expected_actions=expected_actions, + seed=13, ) expected_indices = [18, 39, 49, 67, 77, 116, 139, 142, 149] m = axl.Match((self.player(), axl.Defector()), turns=150, seed=111) diff --git a/axelrod/tests/strategies/test_calculator.py b/axelrod/tests/strategies/test_calculator.py index e99114600..0d5eedfb3 100644 --- a/axelrod/tests/strategies/test_calculator.py +++ b/axelrod/tests/strategies/test_calculator.py @@ -25,9 +25,7 @@ class TestCalculator(TestPlayer): def test_twenty_rounds_joss_for_cyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match( - (axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed - ) + match = axl.Match((axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed) match.play() self.versus_test( axl.Alternator(), expected_actions=match.result, seed=seed @@ -36,24 +34,17 @@ def test_twenty_rounds_joss_for_cyclers(self): def test_twenty_rounds_joss_then_defects_for_cyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match( - (axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed - ) + match = axl.Match((axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed) match.play() expected_actions = match.result + [(D, C), (D, D), (D, C), (D, D)] self.versus_test( - axl.Alternator(), - expected_actions=expected_actions, - seed=seed, - turns=24, + axl.Alternator(), expected_actions=expected_actions, seed=seed, turns=24, ) def test_twenty_rounds_joss_for_noncyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match( - (axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed - ) + match = axl.Match((axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed) match.play() self.versus_test( axl.AntiCycler(), expected_actions=match.result, seed=seed @@ -62,29 +53,16 @@ def test_twenty_rounds_joss_for_noncyclers(self): def test_twenty_rounds_joss_then_tft_for_noncyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match( - (axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed - ) + match = axl.Match((axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed) match.play() - expected_actions = match.result + [ - (C, C), - (C, C), - (C, D), - (D, C), - (C, C), - ] + expected_actions = match.result + [(C, C), (C, C), (C, D), (D, C), (C, C)] self.versus_test( - axl.AntiCycler(), - expected_actions=expected_actions, - seed=seed, - turns=24, + axl.AntiCycler(), expected_actions=expected_actions, seed=seed, turns=24, ) def test_edge_case_calculator_sees_cycles_of_size_ten(self): ten_length_cycle = [C, D, C, C, D, C, C, C, D, C] - self.assertEqual( - detect_cycle((ten_length_cycle * 2)), tuple(ten_length_cycle) - ) + self.assertEqual(detect_cycle((ten_length_cycle * 2)), tuple(ten_length_cycle)) ten_cycle_twenty_rounds = get_joss_strategy_actions( ten_length_cycle * 2, indices_to_flip=[16] @@ -107,9 +85,7 @@ def test_edge_case_calculator_ignores_cycles_gt_len_ten(self): ) opponent_actions = twenty_rounds_of_eleven_len_cycle[:-1] + [D] + [C, D] - self.assertEqual( - detect_cycle(opponent_actions), tuple(eleven_length_cycle) - ) + self.assertEqual(detect_cycle(opponent_actions), tuple(eleven_length_cycle)) uses_tit_for_tat_after_twenty_rounds = twenty_rounds + [(D, C), (C, D)] self.versus_test( @@ -129,19 +105,13 @@ def test_get_joss_strategy_actions(self): self.assertEqual(get_joss_strategy_actions(opponent, []), without_flip) self.assertEqual( - get_joss_strategy_actions( - opponent, flip_never_occurs_at_index_zero - ), + get_joss_strategy_actions(opponent, flip_never_occurs_at_index_zero), without_flip, ) - self.assertEqual( - get_joss_strategy_actions(opponent, flip_indices), with_flip - ) + self.assertEqual(get_joss_strategy_actions(opponent, flip_indices), with_flip) -def get_joss_strategy_actions( - opponent_moves: list, indices_to_flip: list -) -> list: +def get_joss_strategy_actions(opponent_moves: list, indices_to_flip: list) -> list: """ Takes a list of opponent moves and returns a tuple list of [(Joss moves, opponent moves)]. "indices_to_flip" are the indices where Joss differs from it's expected TitForTat. diff --git a/axelrod/tests/strategies/test_cooperator.py b/axelrod/tests/strategies/test_cooperator.py index 7dd4e7d3b..aca2290ae 100644 --- a/axelrod/tests/strategies/test_cooperator.py +++ b/axelrod/tests/strategies/test_cooperator.py @@ -41,9 +41,7 @@ class TestTrickyCooperator(TestPlayer): def test_strategy(self): # Test if it tries to trick opponent. - self.versus_test( - axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)] - ) + self.versus_test(axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)]) opponent_actions = [C, C, C, C, D, D] expected_actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (C, D)] @@ -70,7 +68,7 @@ def test_cooperates_in_first_three_rounds(self): self.versus_test(axl.Alternator(), expected_actions=against_alternator) def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_depth_ten( - self, + self ): against_cooperator = [(C, C)] * 3 + [(D, C)] * 20 self.versus_test(axl.Cooperator(), expected_actions=against_cooperator) @@ -78,6 +76,4 @@ def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_d def test_defects_when_opponent_has_no_defections_to_history_depth_ten(self): opponent_actions = [D] + [C] * 10 + [D, C] expected_actions = [(C, D)] + [(C, C)] * 10 + [(D, D), (C, C)] - self.versus_test( - axl.MockPlayer(actions=opponent_actions), expected_actions - ) + self.versus_test(axl.MockPlayer(actions=opponent_actions), expected_actions) diff --git a/axelrod/tests/strategies/test_cycler.py b/axelrod/tests/strategies/test_cycler.py index a12b14bc3..675d4b4d5 100644 --- a/axelrod/tests/strategies/test_cycler.py +++ b/axelrod/tests/strategies/test_cycler.py @@ -98,9 +98,7 @@ def test_memory_depth_is_len_cycle_minus_one(self): def test_cycler_works_as_expected(self): expected = [(C, D), (D, D), (D, D), (C, D)] * 2 self.versus_test( - axl.Defector(), - expected_actions=expected, - init_kwargs={"cycle": "CDDC"}, + axl.Defector(), expected_actions=expected, init_kwargs={"cycle": "CDDC"} ) def test_cycle_raises_value_error_on_bad_cycle_str(self): @@ -174,17 +172,11 @@ def test_normalized_parameters(self): ) cycle = "C" * random.randint(0, 20) + "D" * random.randint(0, 20) - self.assertEqual( - self.player_class(cycle=cycle, seed=1)._normalize_parameters( - cycle=cycle - ), - (cycle, len(cycle)), - ) + self.assertEqual(self.player_class(cycle=cycle, seed=1)._normalize_parameters(cycle=cycle), + (cycle, len(cycle))) cycle_length = random.randint(1, 20) - random_cycle, cycle_length2 = self.player_class( - cycle=cycle, seed=1 - )._normalize_parameters(cycle_length=cycle_length) + random_cycle, cycle_length2 = self.player_class(cycle=cycle, seed=1)._normalize_parameters(cycle_length=cycle_length) self.assertEqual(len(random_cycle), cycle_length) self.assertEqual(cycle_length, cycle_length2) diff --git a/axelrod/tests/strategies/test_darwin.py b/axelrod/tests/strategies/test_darwin.py index 146489f8a..50c0c4f2b 100644 --- a/axelrod/tests/strategies/test_darwin.py +++ b/axelrod/tests/strategies/test_darwin.py @@ -76,9 +76,7 @@ def test_against_geller_and_mindreader(self): def test_reset_history_and_attributes(self): # Overwrite this method because Darwin does not reset - self.versus_test( - axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 - ) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) p1 = self.player() self.assertEqual(p1.genome, [D, C, C, C, D]) @@ -91,9 +89,7 @@ def test_all_darwin_instances_share_one_genome(self): p2 = self.player() self.assertIs(p1.genome, p2.genome) - self.versus_test( - axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 - ) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) self.assertEqual(p2.genome, [D, C, C, C, D]) self.assertIs(p1.genome, p2.genome) @@ -101,9 +97,7 @@ def test_all_darwin_instances_share_one_genome(self): self.assertIs(p3.genome, p2.genome) def test_reset_genome(self): - self.versus_test( - axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 - ) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) self.player.reset_genome() self.assertEqual(self.player().genome, [C]) diff --git a/axelrod/tests/strategies/test_dbs.py b/axelrod/tests/strategies/test_dbs.py index a3afb81a4..93d61e41a 100644 --- a/axelrod/tests/strategies/test_dbs.py +++ b/axelrod/tests/strategies/test_dbs.py @@ -67,9 +67,7 @@ def test_move_gen_cooperator(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen( - inp, self.cooperator_policy, depth_search_tree=5 - ) + out_move = dbs.move_gen(inp, self.cooperator_policy, depth_search_tree=5) self.assertEqual(out_move, out) def test_minimaxTreeSearch_defector(self): @@ -91,9 +89,7 @@ def test_move_gen_defector(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen( - inp, self.defector_policy, depth_search_tree=5 - ) + out_move = dbs.move_gen(inp, self.defector_policy, depth_search_tree=5) self.assertEqual(out_move, out) def test_minimaxTreeSearch_titForTat(self): @@ -129,9 +125,7 @@ def test_move_gen_titForTat(self): """ expected_output = [C, C, C, C] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen( - inp, self.titForTat_policy, depth_search_tree=5 - ) + out_move = dbs.move_gen(inp, self.titForTat_policy, depth_search_tree=5) self.assertEqual(out_move, out) def test_minimaxTreeSearch_alternator(self): @@ -153,9 +147,7 @@ def test_move_gen_alternator(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen( - inp, self.random_policy, depth_search_tree=5 - ) + out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5) self.assertEqual(out_move, out) def test_minimaxTreeSearch_random(self): @@ -177,9 +169,7 @@ def test_move_gen_random(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen( - inp, self.random_policy, depth_search_tree=5 - ) + out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5) self.assertEqual(out_move, out) def test_minimaxTreeSearch_grudger(self): @@ -202,9 +192,7 @@ def test_move_gen_grudger(self): """ expected_output = [C, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen( - inp, self.grudger_policy, depth_search_tree=5 - ) + out_move = dbs.move_gen(inp, self.grudger_policy, depth_search_tree=5) self.assertEqual(out_move, out) diff --git a/axelrod/tests/strategies/test_doubler.py b/axelrod/tests/strategies/test_doubler.py index a02f42695..e3d436302 100644 --- a/axelrod/tests/strategies/test_doubler.py +++ b/axelrod/tests/strategies/test_doubler.py @@ -22,7 +22,7 @@ class TestDoubler(TestPlayer): } def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperations( - self, + self ): opponent_plays = [C] * 7 + [D] * 4 + [C] actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, C)] @@ -31,7 +31,7 @@ def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperat ) def test_defects_if_opponent_last_play_D_and_defections_equal_two_times_cooperations( - self, + self ): opponent_plays = [C] * 8 + [D] * 4 + [C] actions = [(C, C)] * 8 + [(C, D)] * 4 + [(D, C)] diff --git a/axelrod/tests/strategies/test_evolvable_player.py b/axelrod/tests/strategies/test_evolvable_player.py index ea0fb77fc..6ed840b0b 100644 --- a/axelrod/tests/strategies/test_evolvable_player.py +++ b/axelrod/tests/strategies/test_evolvable_player.py @@ -3,11 +3,7 @@ import axelrod as axl from axelrod.action import Action -from axelrod.evolvable_player import ( - copy_lists, - crossover_dictionaries, - crossover_lists, -) +from axelrod.evolvable_player import copy_lists, crossover_dictionaries, crossover_lists from .test_player import TestPlayer @@ -21,7 +17,8 @@ class PartialedClass(cls): seed = kwargs["seed"] except KeyError: kwargs["seed"] = 1 - __init__ = functools.partialmethod(cls.__init__, **kwargs) + __init__ = functools.partialmethod( + cls.__init__, **kwargs) return PartialedClass @@ -48,9 +45,7 @@ def mutate(self): def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError( - "Crossover must be between the same player classes." - ) + raise TypeError("Crossover must be between the same player classes.") value = self.value + other.value return EvolvableTestOpponent(value) @@ -130,11 +125,7 @@ def test_crossover(self): players.append(player) player1, player2 = players crossed = player1.crossover(player2) - if ( - player1 != crossed - and player2 != crossed - and crossed == crossed.clone() - ): + if player1 != crossed and player2 != crossed and crossed == crossed.clone(): return # Should never get here unless a change breaks the test, so don't include in coverage. self.assertFalse(True) # pragma: no cover @@ -149,9 +140,7 @@ def test_serialization(self): """Serializing and deserializing should return the original player.""" player = self.player(seed=1) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters( - serialized - ) + deserialized_player = player.__class__.deserialize_parameters(serialized) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -160,7 +149,7 @@ def test_serialization_csv(self): player = self.player(seed=1) serialized = player.serialize_parameters() s = "0, 1, {}, 3".format(serialized) - s2 = s.split(",")[2] + s2 = s.split(',')[2] deserialized_player = player.__class__.deserialize_parameters(s2) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -189,9 +178,7 @@ def test_behavior(self): self.behavior_test(player, parent_player) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters( - serialized - ) + deserialized_player = player.__class__.deserialize_parameters(serialized) self.behavior_test(deserialized_player, parent_player) def test_seed_propagation(self): @@ -215,6 +202,7 @@ def test_seed_preservation(self): class TestUtilityFunctions(unittest.TestCase): + def test_copy_lists(self): l1 = [list(range(10)), list(range(20))] l2 = copy_lists(l1) @@ -233,12 +221,12 @@ def test_crossover_lists(self): self.assertEqual(crossed, list1[:1] + list2[1:]) def test_crossover_dictionaries(self): - dict1 = {"1": 1, "2": 2, "3": 3} - dict2 = {"1": "a", "2": "b", "3": "c"} + dict1 = {'1': 1, '2': 2, '3': 3} + dict2 = {'1': 'a', '2': 'b', '3': 'c'} rng = axl.RandomGenerator(seed=1) crossed = crossover_dictionaries(dict1, dict2, rng) - self.assertEqual(crossed, {"1": 1, "2": "b", "3": "c"}) + self.assertEqual(crossed, {'1': 1, '2': 'b', '3': 'c'}) rng = axl.RandomGenerator(seed=2) crossed = crossover_dictionaries(dict1, dict2, rng) diff --git a/axelrod/tests/strategies/test_finite_state_machines.py b/axelrod/tests/strategies/test_finite_state_machines.py index f7eab3029..f3525b0e1 100644 --- a/axelrod/tests/strategies/test_finite_state_machines.py +++ b/axelrod/tests/strategies/test_finite_state_machines.py @@ -2,9 +2,7 @@ import unittest import axelrod as axl -from axelrod.compute_finite_state_machine_memory import ( - get_memory_from_transitions, -) +from axelrod.compute_finite_state_machine_memory import get_memory_from_transitions from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.finite_state_machines import ( EvolvableFSMPlayer, @@ -48,15 +46,8 @@ def test__eq__false_by_state(self): self.assertFalse(new_two_state.__eq__(self.two_state)) def test__eq__false_by_transition(self): - different_transitions = ( - (1, C, 0, D), - (1, D, 0, D), - (0, C, 1, D), - (0, D, 1, C), - ) - new_two_state = SimpleFSM( - transitions=different_transitions, initial_state=1 - ) + different_transitions = ((1, C, 0, D), (1, D, 0, D), (0, C, 1, D), (0, D, 1, C)) + new_two_state = SimpleFSM(transitions=different_transitions, initial_state=1) self.assertFalse(new_two_state.__eq__(self.two_state)) @@ -100,9 +91,7 @@ def test_state_setter_raises_error_for_bad_input(self): with self.assertRaises(ValueError) as cm: self.two_state.state = 5 error_msg = cm.exception.args[0] - self.assertEqual( - error_msg, "state: 5 does not have values for both C and D" - ) + self.assertEqual(error_msg, "state: 5 does not have values for both C and D") class TestSampleFSMPlayer(TestPlayer): @@ -168,12 +157,7 @@ def test_wsls(self): """Tests that the player defined by the table for TFT is in fact WSLS (also known as Pavlov.""" wsls_init_kwargs = { - "transitions": ( - (1, C, 1, C), - (1, D, 2, D), - (2, C, 2, D), - (2, D, 1, C), - ), + "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), "initial_state": 1, "initial_action": C, } @@ -288,9 +272,7 @@ def test_strategy(self): opponent = axl.MockPlayer([D, D, C, C, D]) actions = [(C, D), (C, D), (C, C), (D, C), (C, D)] self.versus_test( - opponent, - expected_actions=actions, - init_kwargs={"transitions": transitions}, + opponent, expected_actions=actions, init_kwargs={"transitions": transitions} ) def test_memory(self): @@ -298,10 +280,7 @@ def test_memory(self): Test the memory depth using implemented algorithm """ transitions = self.player().fsm._state_transitions - self.assertEqual( - get_memory_from_transitions(transitions), - self.expected_classifier["memory_depth"], - ) + self.assertEqual(get_memory_from_transitions(transitions), self.expected_classifier["memory_depth"]) class TestFortress3(TestFSMPlayer): @@ -332,15 +311,7 @@ def test_strategy(self): state_and_actions = [(1, C), (1, D), (2, C), (1, C)] self.transitions_test(state_and_actions) - state_and_actions = [ - (1, D), - (2, D), - (3, C), - (3, C), - (3, C), - (3, D), - (1, C), - ] * 2 + state_and_actions = [(1, D), (2, D), (3, C), (3, C), (3, C), (3, D), (1, C)] * 2 self.transitions_test(state_and_actions) @unittest.expectedFailure @@ -444,15 +415,11 @@ def test_strategy(self): ] + [(7, D), (7, C), (8, C), (8, D), (6, D)] * 3 self.transitions_test(state_and_actions) - state_and_actions = [ - (0, D), - (1, C), - (2, D), - (3, C), - (5, D), - (3, C), - (5, C), - ] + [(7, C), (8, D), (6, C),] * 5 + state_and_actions = [(0, D), (1, C), (2, D), (3, C), (5, D), (3, C), (5, C)] + [ + (7, C), + (8, D), + (6, C), + ] * 5 self.transitions_test(state_and_actions) state_and_actions = ( @@ -553,9 +520,7 @@ class TestRipoff(TestFSMPlayer): """ def test_strategy(self): - state_and_actions = ( - [(1, C), (2, C)] * 3 + [(1, D)] + [(3, C), (3, D)] * 5 - ) + state_and_actions = [(1, C), (2, C)] * 3 + [(1, D)] + [(3, C), (3, D)] * 5 self.transitions_test(state_and_actions) state_and_actions = [(1, C), (2, D)] + [(3, D)] * 5 @@ -656,11 +621,7 @@ class TestSolutionB1(TestFSMPlayer): def test_strategy(self): state_and_actions = ( - [(1, D)] * 3 - + [(1, C)] - + [(2, C)] * 3 - + [(2, D)] - + [(3, C), (3, D)] * 3 + [(1, D)] * 3 + [(1, C)] + [(2, C)] * 3 + [(2, D)] + [(3, C), (3, D)] * 3 ) self.transitions_test(state_and_actions) @@ -841,9 +802,7 @@ class TestEvolvedFSM16(TestFSMPlayer): def test_strategy(self): # finished: 0, - state_and_actions = ( - [(0, C)] * 3 + [(0, D)] + [(12, D), (11, D), (5, C)] * 3 - ) + state_and_actions = [(0, C)] * 3 + [(0, D)] + [(12, D), (11, D), (5, C)] * 3 self.transitions_test(state_and_actions) # finished: 0, 5, 10 @@ -890,13 +849,7 @@ def test_strategy(self): self.transitions_test(state_and_actions) # finished: 0, 1, 2, 3, 5, 10, 11, 12, 13, 14, 15 - state_and_actions = to_state_seven + [ - (7, D), - (1, D), - (6, C), - (5, D), - (10, C), - ] + state_and_actions = to_state_seven + [(7, D), (1, D), (6, C), (5, D), (10, C)] self.transitions_test(state_and_actions) # finished: 0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15 @@ -1026,12 +979,7 @@ def test_strategy(self): # finished 0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 13, 15 to_state_eleven = [(0, D), (3, C), (10, D), (1, D), (15, D)] - state_and_actions = to_state_eleven + [ - (11, C), - (14, C), - (3, C), - (10, D), - ] + state_and_actions = to_state_eleven + [(11, C), (14, C), (3, C), (10, D)] self.transitions_test(state_and_actions) # finished 0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 15 @@ -1110,12 +1058,7 @@ def test_normalized_parameters(self): self.assertRaises( InsufficientParametersError, self.player_class, - transitions=[ - [0, C, 1, D], - [0, D, 0, D], - [1, C, 1, C], - [1, D, 1, D], - ], + transitions=[[0, C, 1, D], [0, D, 0, D], [1, C, 1, C], [1, D, 1, D]], seed=1, # To prevent exception from unset seed. ) @@ -1140,9 +1083,7 @@ def test_vector_to_instance(self): self.assertIsInstance(player, axl.EvolvableFSMPlayer) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters( - serialized - ) + deserialized_player = player.__class__.deserialize_parameters(serialized) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -1157,8 +1098,7 @@ def test_mutate(self): """Test to trigger random lines in mutate""" for seed in [18, 22]: player = axl.EvolvableFSMPlayer( - num_states=4, mutation_probability=0.5, seed=seed - ) + num_states=4, mutation_probability=0.5, seed=seed) player.mutate() @@ -1186,7 +1126,7 @@ class TestEvolvableFSMPlayer4(TestEvolvablePlayer): init_parameters = { "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), "initial_state": 1, - "initial_action": C, + "initial_action": C } @@ -1195,8 +1135,7 @@ class TestEvolvableFSMPlayer4(TestEvolvablePlayer): EvolvableFSMPlayer, transitions=((1, C, 1, C), (1, D, 1, D)), initial_state=1, - initial_action=C, -) + initial_action=C) class EvolvableFSMAsFSM(TestFSMPlayer): diff --git a/axelrod/tests/strategies/test_forgiver.py b/axelrod/tests/strategies/test_forgiver.py index 43754f5b0..e83b86d76 100644 --- a/axelrod/tests/strategies/test_forgiver.py +++ b/axelrod/tests/strategies/test_forgiver.py @@ -25,19 +25,15 @@ def test_strategy(self): # If opponent has defected more than 10 percent of the time, defect. self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 10) - self.versus_test( - axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10 - ) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10) def test_cooperates_if_opponent_defections_is_ten_pct_and_defects_if_opponent_defections_gt_ten_pct( - self, + self ): final_action_lowers_defections_to_ten_percent = [D] + [C] * 9 expected = [(C, D)] + [(D, C)] * 9 self.versus_test( - axl.MockPlayer( - actions=final_action_lowers_defections_to_ten_percent - ), + axl.MockPlayer(actions=final_action_lowers_defections_to_ten_percent), expected_actions=expected * 5, ) @@ -66,9 +62,7 @@ class TestForgivingTitForTat(TestPlayer): def test_strategy(self): self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test( - axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5 - ) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5) self.versus_test( axl.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5 ) @@ -83,11 +77,7 @@ def test_never_defects_if_opponent_defections_le_ten_percent(self): def test_plays_tit_for_tat_while_defections_gt_ten_percent(self): before_tft = (18 * [C] + [D]) * 3 + [D, D, D] - only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [ - (C, D), - (C, D), - (C, D), - ] + only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [(C, D), (C, D), (C, D)] self.versus_test( axl.MockPlayer(actions=before_tft), expected_actions=only_cooperates ) diff --git a/axelrod/tests/strategies/test_gambler.py b/axelrod/tests/strategies/test_gambler.py index e9ab3b140..cc58083e3 100755 --- a/axelrod/tests/strategies/test_gambler.py +++ b/axelrod/tests/strategies/test_gambler.py @@ -415,7 +415,9 @@ def test_vs_DCDDC2(self): (D, D), # different than above test ] self.versus_test( - axl.MockPlayer(opponent_actions), expected_actions=expected, seed=5, + axl.MockPlayer(opponent_actions), + expected_actions=expected, + seed=5, ) def test_vs_DCDDC3(self): @@ -518,17 +520,14 @@ def test_vs_alternator2(self): class TestEvolvableGambler(unittest.TestCase): + def test_receive_vector(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1 - ) + parameters=(plays, op_plays, op_start_plays), seed=1) - self.assertRaises( - AttributeError, - axl.EvolvableGambler.__getattribute__, - *[player, "vector"] - ) + self.assertRaises(AttributeError, axl.EvolvableGambler.__getattribute__, + *[player, 'vector']) vector = [random.random() for _ in range(8)] player.receive_vector(vector) @@ -537,24 +536,19 @@ def test_receive_vector(self): def test_vector_to_instance(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1 - ) + parameters=(plays, op_plays, op_start_plays), seed=1) vector = [random.random() for _ in range(8)] player.receive_vector(vector) - keys = create_lookup_table_keys( - player_depth=plays, - op_depth=op_plays, - op_openings_depth=op_start_plays, - ) + keys = create_lookup_table_keys(player_depth=plays, op_depth=op_plays, + op_openings_depth=op_start_plays) action_dict = dict(zip(keys, vector)) self.assertEqual(player._lookup.dictionary, action_dict) def test_create_vector_bounds(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1 - ) + parameters=(plays, op_plays, op_start_plays), seed=1) lb, ub = player.create_vector_bounds() self.assertIsInstance(lb, list) self.assertIsInstance(ub, list) @@ -572,7 +566,8 @@ class TestEvolvableGambler2(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (1, 1, 1), "initial_actions": (C,)} + init_parameters = {"parameters": (1, 1, 1), + "initial_actions": (C,)} class TestEvolvableGambler3(TestEvolvablePlayer): @@ -580,7 +575,8 @@ class TestEvolvableGambler3(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (3, 2, 1), "initial_actions": (C, C, C,)} + init_parameters = {"parameters": (3, 2, 1), + "initial_actions": (C, C, C,)} class TestEvolvableGambler4(TestEvolvablePlayer): @@ -588,11 +584,9 @@ class TestEvolvableGambler4(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = { - "parameters": (2, 2, 2), - "pattern": [random.random() for _ in range(64)], - "initial_actions": (C, C,), - } + init_parameters = {"parameters": (2, 2, 2), + "pattern": [random.random() for _ in range(64)], + "initial_actions": (C, C,)} # Substitute EvolvableHMMPlayer as a regular HMMPlayer. @@ -600,7 +594,7 @@ class TestEvolvableGambler4(TestEvolvablePlayer): axl.EvolvableGambler, pattern=tables[("PSO Gambler 2_2_2", 2, 2, 2)], parameters=(2, 2, 2), - initial_actions=(C, C,), + initial_actions=(C, C,) ) diff --git a/axelrod/tests/strategies/test_geller.py b/axelrod/tests/strategies/test_geller.py index 0cb60d7c0..38491fe08 100644 --- a/axelrod/tests/strategies/test_geller.py +++ b/axelrod/tests/strategies/test_geller.py @@ -43,9 +43,7 @@ def test_strategy(self): """Should cooperate against cooperators and defect against defectors.""" self.versus_test(axl.Defector(), expected_actions=[(D, D)] * 5) self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test( - axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5 - ) + self.versus_test(axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5) def test_strategy_against_lookerup_players(self): """ @@ -66,9 +64,7 @@ def test_returns_foil_inspection_strategy_of_opponent(self): seed=2, ) - self.versus_test( - axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)], seed=3 - ) + self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)], seed=3) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)], seed=1 @@ -95,13 +91,10 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axl.GellerDefector(), - expected_actions=[(D, C), (D, C), (D, C), (D, C)], + axl.GellerDefector(), expected_actions=[(D, C), (D, C), (D, C), (D, C)] ) - self.versus_test( - axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)] - ) + self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] @@ -129,13 +122,10 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axl.GellerDefector(), - expected_actions=[(D, D), (D, D), (D, D), (D, D)], + axl.GellerDefector(), expected_actions=[(D, D), (D, D), (D, D), (D, D)] ) - self.versus_test( - axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)] - ) + self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] diff --git a/axelrod/tests/strategies/test_gobymajority.py b/axelrod/tests/strategies/test_gobymajority.py index 88c5c555a..6cd553880 100644 --- a/axelrod/tests/strategies/test_gobymajority.py +++ b/axelrod/tests/strategies/test_gobymajority.py @@ -38,9 +38,7 @@ def test_memory_depth_infinite_soft_is_false(self): + [(C, C)] ) opponent = axl.MockPlayer(actions=opponent_actions) - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_memory_depth_even_soft_is_false(self): memory_depth = 4 @@ -48,9 +46,7 @@ def test_memory_depth_even_soft_is_false(self): if self.default_soft: init_kwargs["soft"] = False - opponent = axl.MockPlayer( - actions=[C] * memory_depth + [D] * memory_depth - ) + opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) actions = ( [(D, C)] + [(C, C)] * 3 @@ -59,9 +55,7 @@ def test_memory_depth_even_soft_is_false(self): + [(D, C)] * 3 + [(C, C)] ) - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_memory_depth_odd(self): memory_depth = 5 @@ -70,9 +64,7 @@ def test_memory_depth_odd(self): first_action = [(C, C)] else: first_action = [(D, C)] - opponent = axl.MockPlayer( - actions=[C] * memory_depth + [D] * memory_depth - ) + opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) actions = ( first_action + [(C, C)] * 4 @@ -81,9 +73,7 @@ def test_memory_depth_odd(self): + [(D, C)] * 3 + [(C, C)] * 2 ) - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_default_values(self): player = self.player() @@ -100,11 +90,7 @@ class TestGoByMajority(TestHardGoByMajority): def test_memory_depth_infinite_soft_is_true(self): opponent_actions = [C] * 50 + [D] * 100 + [C] * 52 actions = ( - [(C, C)] * 50 - + [(C, D)] * 51 - + [(D, D)] * 49 - + [(D, C)] * 50 - + [(C, C)] * 2 + [(C, C)] * 50 + [(C, D)] * 51 + [(D, D)] * 49 + [(D, C)] * 50 + [(C, C)] * 2 ) opponent = axl.MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions) @@ -114,12 +100,8 @@ def test_memory_depth_even_soft_is_true(self): init_kwargs = {"memory_depth": memory_depth} opponent = axl.MockPlayer([C] * memory_depth + [D] * memory_depth) - actions = ( - [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 - ) - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + actions = [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_name(self): player = self.player(soft=True) @@ -179,9 +161,7 @@ def test_strategy(self): else: cooperations = int(memory_depth * 1.5) - 1 defections = len(opponent_actions) - cooperations - 1 - player_actions = ( - first_player_action + [C] * cooperations + [D] * defections - ) + player_actions = first_player_action + [C] * cooperations + [D] * defections actions = list(zip(player_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_grudger.py b/axelrod/tests/strategies/test_grudger.py index 3ec1d6ac4..79194b5d8 100644 --- a/axelrod/tests/strategies/test_grudger.py +++ b/axelrod/tests/strategies/test_grudger.py @@ -12,7 +12,7 @@ class TestGrudger(TestPlayer): name = "Grudger" player = axl.Grudger expected_classifier = { - "memory_depth": float("inf"), + "memory_depth": float('inf'), "stochastic": False, "makes_use_of": set(), "long_run_time": False, @@ -258,31 +258,11 @@ def test_strategy(self): """Test strategy with multiple initial parameters""" # Testing default parameters of n=1, d=4, c=2 (same as Soft Grudger) - actions = [ - (C, D), - (D, D), - (D, C), - (D, C), - (D, D), - (C, D), - (C, C), - (C, C), - ] - self.versus_test( - axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions - ) + actions = [(C, D), (D, D), (D, C), (D, C), (D, D), (C, D), (C, C), (C, C)] + self.versus_test(axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions) # Testing n=2, d=4, c=2 - actions = [ - (C, D), - (C, D), - (D, C), - (D, C), - (D, D), - (D, D), - (C, C), - (C, C), - ] + actions = [(C, D), (C, D), (D, C), (D, C), (D, D), (D, D), (C, C), (C, C)] self.versus_test( axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions, @@ -290,16 +270,7 @@ def test_strategy(self): ) # Testing n=1, d=1, c=1 - actions = [ - (C, D), - (D, D), - (C, C), - (C, C), - (C, D), - (D, D), - (C, C), - (C, C), - ] + actions = [(C, D), (D, D), (C, C), (C, C), (C, D), (D, D), (C, C), (C, C)] self.versus_test( axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions, diff --git a/axelrod/tests/strategies/test_grumpy.py b/axelrod/tests/strategies/test_grumpy.py index fa0e5b327..1fba6bbdd 100644 --- a/axelrod/tests/strategies/test_grumpy.py +++ b/axelrod/tests/strategies/test_grumpy.py @@ -46,42 +46,32 @@ def test_starting_state(self): actions = ([(C, D)] * 11 + [(D, C)] * 22 + [(C, D)] * 11) * 3 init_kwargs = {"starting_state": "Nice"} - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) opponent = axl.MockPlayer(actions=opponent_actions) grumpy_starting = [(D, D)] * 11 + [(D, C)] * 22 + [(C, D)] * 11 actions = grumpy_starting + actions init_kwargs = {"starting_state": "Grumpy"} - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_thresholds(self): init_kwargs = {"grumpy_threshold": 3, "nice_threshold": -2} opponent_actions = [D] * 4 + [C] * 7 + [D] * 3 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 4 + [(D, C)] * 7 + [(C, D)] * 3) * 3 - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) init_kwargs = {"grumpy_threshold": 0, "nice_threshold": -2} opponent_actions = [D] * 1 + [C] * 4 + [D] * 3 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 1 + [(D, C)] * 4 + [(C, D)] * 3) * 3 - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) init_kwargs = {"grumpy_threshold": 3, "nice_threshold": 0} opponent_actions = [D] * 4 + [C] * 5 + [D] * 1 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 4 + [(D, C)] * 5 + [(C, D)] * 1) * 3 - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) def test_reset_state_with_non_default_init(self): player = axl.Grumpy(starting_state="Grumpy") diff --git a/axelrod/tests/strategies/test_hmm.py b/axelrod/tests/strategies/test_hmm.py index beffae2c5..e60d4e3dd 100644 --- a/axelrod/tests/strategies/test_hmm.py +++ b/axelrod/tests/strategies/test_hmm.py @@ -193,23 +193,17 @@ class TestEvolvedHMM5(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) class TestEvolvedHMM5vsCooperator(TestMatch): def test_rounds(self): - self.versus_test( - axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5, seed=3 - ) + self.versus_test(axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5, seed=3) class TestEvolvedHMM5vsDefector(TestMatch): def test_rounds(self): - self.versus_test( - axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D], seed=5 - ) + self.versus_test(axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D], seed=5) class TestEvolvableHMMPlayer(unittest.TestCase): @@ -224,7 +218,9 @@ def test_normalized_parameters(self): initial_action = C self.assertRaises( - InsufficientParametersError, self.player_class, seed=1 + InsufficientParametersError, + self.player_class, + seed=1 ) self.assertRaises( InsufficientParametersError, @@ -233,7 +229,7 @@ def test_normalized_parameters(self): transitions_D=transitions_D, emission_probabilities=emission_probabilities, initial_state=None, - seed=1, + seed=1 ) self.assertRaises( InsufficientParametersError, @@ -242,14 +238,14 @@ def test_normalized_parameters(self): transitions_D=transitions_D, emission_probabilities=emission_probabilities, initial_action=None, - seed=1, + seed=1 ) self.assertRaises( InsufficientParametersError, self.player_class, initial_state=initial_state, initial_action=initial_action, - seed=1, + seed=1 ) def test_vector_to_instance(self): diff --git a/axelrod/tests/strategies/test_human.py b/axelrod/tests/strategies/test_human.py index a372b25ca..e0e2ca811 100644 --- a/axelrod/tests/strategies/test_human.py +++ b/axelrod/tests/strategies/test_human.py @@ -27,9 +27,7 @@ def test_validator(self): ActionValidator().validate(test_document) test_document = TestDocument("E") - self.assertRaises( - ValidationError, ActionValidator().validate, test_document - ) + self.assertRaises(ValidationError, ActionValidator().validate, test_document) class TestHumanClass(TestPlayer): diff --git a/axelrod/tests/strategies/test_inverse.py b/axelrod/tests/strategies/test_inverse.py index 3d89e11dd..95ac95033 100644 --- a/axelrod/tests/strategies/test_inverse.py +++ b/axelrod/tests/strategies/test_inverse.py @@ -28,22 +28,10 @@ def test_strategy(self): # Tests that if opponent has played all D then player chooses D, # regardless of the random seed. - self.versus_test( - axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9, seed=None - ) + self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9, seed=None) - expected_actions = [ - (C, D), - (D, C), - (D, C), - (C, D), - (D, C), - (C, C), - (D, C), - (C, C), - (C, D), - (D, D), - ] + expected_actions = [(C, D), (D, C), (D, C), (C, D), (D, C), (C, C), + (D, C), (C, C), (C, D), (D, D)] self.versus_test( axl.MockPlayer(actions=[a[1] for a in expected_actions]), expected_actions=expected_actions, diff --git a/axelrod/tests/strategies/test_lookerup.py b/axelrod/tests/strategies/test_lookerup.py index b3fdf6c9c..c48474388 100755 --- a/axelrod/tests/strategies/test_lookerup.py +++ b/axelrod/tests/strategies/test_lookerup.py @@ -20,7 +20,6 @@ C, D = axl.Action.C, axl.Action.D random = axl.RandomGenerator() - class TestLookupTable(unittest.TestCase): lookup_dict = { ((C, C), (C,), ()): C, @@ -70,9 +69,7 @@ def test_from_pattern(self): table = LookupTable.from_pattern( pattern, player_depth=2, op_depth=1, op_openings_depth=0 ) - self.assertEqual( - table.dictionary, make_keys_into_plays(self.lookup_dict) - ) + self.assertEqual(table.dictionary, make_keys_into_plays(self.lookup_dict)) def test_from_pattern_raises_error_pattern_len_ne_dict_size(self): too_big = (C,) * 17 @@ -154,9 +151,7 @@ def test_plays_equals_tuple(self): self.assertEqual(Plays(1, 2, 3), (1, 2, 3)) def test_plays_assign_values(self): - self.assertEqual( - Plays(op_plays=2, self_plays=1, op_openings=3), Plays(1, 2, 3) - ) + self.assertEqual(Plays(op_plays=2, self_plays=1, op_openings=3), Plays(1, 2, 3)) def test_make_keys_into_plays(self): old = {((C, D), (C,), ()): 1, ((D, D), (D,), ()): 2} @@ -312,9 +307,7 @@ def test_set_memory_depth(self): self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_3), 3) mem_depth_inf = axl.LookerUp(pattern="CC", parameters=Plays(0, 0, 1)) - self.assertEqual( - axl.Classifiers["memory_depth"](mem_depth_inf), float("inf") - ) + self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_inf), float("inf")) def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D)] @@ -382,13 +375,10 @@ def test_opponent_starting_moves_table(self): def test_lookup_table_display(self): player = axl.LookerUp( - pattern="CCCC", - parameters=Plays(self_plays=2, op_plays=0, op_openings=0), + pattern="CCCC", parameters=Plays(self_plays=2, op_plays=0, op_openings=0) ) self.assertEqual( - player.lookup_table_display( - ("self_plays", "op_plays", "op_openings") - ), + player.lookup_table_display(("self_plays", "op_plays", "op_openings")), ( "self_plays | op_plays |op_openings\n" + " C, C , , : C,\n" @@ -430,16 +420,12 @@ def test_new_data(self): def test_vs_initial_defector(self): opponent = [D, C, C, D, D, C] expected = [(C, D), (D, C), (C, C), (D, D), (D, D), (D, C)] - self.versus_test( - axl.MockPlayer(actions=opponent), expected_actions=expected - ) + self.versus_test(axl.MockPlayer(actions=opponent), expected_actions=expected) def test_vs_initial_cooperator(self): opponent = [C, D, D, C, C, D] expected = [(C, C), (C, D), (D, D), (D, C), (D, C), (D, D)] - self.versus_test( - axl.MockPlayer(actions=opponent), expected_actions=expected - ) + self.versus_test(axl.MockPlayer(actions=opponent), expected_actions=expected) class TestEvolvedLookerUp2_2_2(TestPlayer): @@ -535,14 +521,7 @@ def test_vs_initial_defector(self): def test_vs_initial_d_c(self): opponent_actions = [D, C] + [C, D] * 3 - expected = [(C, D), (C, C)] + [ - (D, C), - (C, D), - (C, C), - (D, D), - (C, C), - (C, D), - ] + expected = [(C, D), (C, C)] + [(D, C), (C, D), (C, C), (D, D), (C, C), (C, D)] self.versus_test( axl.MockPlayer(actions=opponent_actions), expected_actions=expected ) @@ -625,9 +604,7 @@ def test_strategy(self): vs_alternator = [(D, C), (C, D)] + [(D, C), (D, D)] * 5 self.versus_test(axl.Alternator(), expected_actions=vs_alternator) - self.versus_test( - axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10 - ) + self.versus_test(axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10) self.versus_test( axl.Defector(), expected_actions=([(D, D), (C, D)] + [(D, D)] * 10) @@ -639,11 +616,7 @@ def test_convert_key(self): opponent_starting_plays = "" player_last_plays = "CC" opponent_last_plays = "D" - old_key = ( - opponent_starting_plays, - player_last_plays, - opponent_last_plays, - ) + old_key = (opponent_starting_plays, player_last_plays, opponent_last_plays) new_key = Plays(self_plays=(C, C), op_plays=(D,), op_openings=()) @@ -692,20 +665,22 @@ def test_normalized_parameters(self): pattern = ("".join([random.choice(("C", "D")) for _ in range(8)]),) self.assertRaises( - InsufficientParametersError, self.player_class, seed=1 + InsufficientParametersError, + self.player_class, + seed=1 ) self.assertRaises( InsufficientParametersError, self.player_class, pattern=pattern, initial_actions=initial_actions, - seed=1, + seed=1 ) self.assertRaises( InsufficientParametersError, self.player_class, lookup_dict=lookup_dict, - seed=1, + seed=1 ) diff --git a/axelrod/tests/strategies/test_memoryone.py b/axelrod/tests/strategies/test_memoryone.py index f1e6513e9..01e720618 100644 --- a/axelrod/tests/strategies/test_memoryone.py +++ b/axelrod/tests/strategies/test_memoryone.py @@ -69,15 +69,11 @@ class TestGTFT(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) def test_strategy2(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) def test_four_vector(self): (R, P, S, T) = axl.Game().RPST() @@ -115,15 +111,11 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, D), (D, D), (D, D), (C, D)] - self.versus_test( - opponent=axl.Defector(), expected_actions=actions, seed=10 - ) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=10) def test_strategy3(self): actions = [(C, D), (D, D), (C, D), (D, D), (D, D)] - self.versus_test( - opponent=axl.Defector(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=1) class TestStochasticCooperator(TestPlayer): @@ -151,27 +143,19 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (C, C), (D, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=113 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=113) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) def test_strategy3(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=5 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=5) def test_strategy4(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (C, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=12 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=12) class TestStochasticWSLS(TestPlayer): @@ -190,27 +174,19 @@ class TestStochasticWSLS(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (D, C), (D, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=50 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=50) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (D, C), (D, D), (C, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=23 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=23) def test_four_vector(self): player = self.player() @@ -228,8 +204,7 @@ class TestMemoryOnePlayer(unittest.TestCase): def test_default_if_four_vector_not_set(self): player = MemoryOnePlayer() self.assertEqual( - player._four_vector, - {(C, C): 1.0, (C, D): 0.0, (D, C): 0.0, (D, D): 1.0}, + player._four_vector, {(C, C): 1.0, (C, D): 0.0, (D, C): 0.0, (D, D): 1.0} ) def test_exception_if_four_vector_not_set(self): @@ -271,15 +246,11 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) def test_strategy2(self): actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=5 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) class TestALLCorALLD(TestPlayer): @@ -298,15 +269,11 @@ class TestALLCorALLD(TestPlayer): def test_strategy(self): actions = [(D, C)] * 10 - self.versus_test( - opponent=axl.Cooperator(), expected_actions=actions, seed=0 - ) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=0) def test_strategy2(self): actions = [(C, C)] * 10 - self.versus_test( - opponent=axl.Cooperator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=1) class TestGenericReactiveStrategy(unittest.TestCase): @@ -320,16 +287,13 @@ class TestGenericReactiveStrategy(unittest.TestCase): def test_four_vector(self): self.assertEqual( - self.p1._four_vector, - {(C, D): 0.0, (D, C): 0.0, (C, C): 0.0, (D, D): 0.0}, + self.p1._four_vector, {(C, D): 0.0, (D, C): 0.0, (C, C): 0.0, (D, D): 0.0} ) self.assertEqual( - self.p2._four_vector, - {(C, D): 0.0, (D, C): 1.0, (C, C): 1.0, (D, D): 0.0}, + self.p2._four_vector, {(C, D): 0.0, (D, C): 1.0, (C, C): 1.0, (D, D): 0.0} ) self.assertEqual( - self.p3._four_vector, - {(C, D): 0.5, (D, C): 1.0, (C, C): 1.0, (D, D): 0.5}, + self.p3._four_vector, {(C, D): 0.5, (D, C): 1.0, (C, C): 1.0, (D, D): 0.5} ) def test_subclass(self): @@ -340,7 +304,6 @@ def test_subclass(self): class TestMemoryOneAlternator(TestAlternator): """Alternator is equivalent to MemoryOnePlayer((0, 0, 1, 1), C)""" - name = "Generic Memory One Player: (0, 0, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 1, 1)) expected_classifier = { @@ -356,7 +319,6 @@ class TestMemoryOneAlternator(TestAlternator): class TestMemoryOneCooperator(TestCooperator): """Cooperator is equivalent to MemoryOnePlayer((1, 1, 1, 1), C)""" - name = "Generic Memory One Player: (1, 1, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 1, 1, 1)) expected_classifier = { @@ -372,7 +334,6 @@ class TestMemoryOneCooperator(TestCooperator): class TestMemoryOneDefector(TestDefector): """Defector is equivalent to MemoryOnePlayer((0, 0, 0, 0), D)""" - name = "Generic Memory One Player: (0, 0, 0, 0), D" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0), initial=D) expected_classifier = { @@ -388,7 +349,6 @@ class TestMemoryOneDefector(TestDefector): class TestMemoryOneTitForTat(TestTitForTat): """TitForTat is equivalent to MemoryOnePlayer((1, 0, 1, 0), C)""" - name = "Generic Memory One Player: (1, 0, 1, 0), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) expected_classifier = { @@ -404,7 +364,6 @@ class TestMemoryOneTitForTat(TestTitForTat): class TestMemoryOneWSLS(TestWinStayLoseShift): """WinStayLoseShift is equivalent to MemoryOnePlayer((1, 0, 0, 1), C)""" - name = "Generic Memory One Player: (1, 0, 0, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 0, 1)) expected_classifier = { diff --git a/axelrod/tests/strategies/test_memorytwo.py b/axelrod/tests/strategies/test_memorytwo.py index ee5265eac..339aef728 100644 --- a/axelrod/tests/strategies/test_memorytwo.py +++ b/axelrod/tests/strategies/test_memorytwo.py @@ -80,7 +80,9 @@ def test_exception_if_probability_vector_outside_valid_values(self): class TestMemoryStochastic(TestPlayer): - name = "Generic Memory Two Player" + name = ( + "Generic Memory Two Player" + ) player = axl.MemoryTwoPlayer expected_classifier = { "memory_depth": 0, # Memory-two Sixteen-Vector @@ -154,9 +156,7 @@ def test_strategy(self): # tests states 3, 5 and 12 actions = [(C, D), (C, C), (D, C), (D, D), (D, D), (C, D)] - self.versus_test( - opponent=axl.SuspiciousTitForTat(), expected_actions=actions - ) + self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions) # tests state 1 actions = [(C, C), (C, C), (C, C), (C, C)] @@ -189,15 +189,12 @@ def test_strategy_mutually_cooperative(self): # tests states 1, 4 and 8 actions = [(C, D), (C, D), (D, D), (C, C), (C, C), (C, D)] self.versus_test( - opponent=axl.Cycler(["D", "D", "D", "C", "C"]), - expected_actions=actions, + opponent=axl.Cycler(["D", "D", "D", "C", "C"]), expected_actions=actions ) # tests states 3, 5 actions = [(C, D), (C, C), (D, C), (D, D), (C, D)] - self.versus_test( - opponent=axl.SuspiciousTitForTat(), expected_actions=actions - ) + self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions) class TestMEM2(TestPlayer): @@ -259,11 +256,8 @@ def test_strategy(self): class TestMemoryTwoCooperator(TestCooperator): """Cooperator is equivalent to MemoryTwoPlayer((1, 1, ..., 1), C)""" - name = "Generic Memory Two Player: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], (C, C)" - player = lambda x: axl.MemoryTwoPlayer( - sixteen_vector=[1] * 16, initial=(C, C) - ) + player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=[1] * 16, initial=(C, C)) expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -277,11 +271,8 @@ class TestMemoryTwoCooperator(TestCooperator): class TestMemoryTwoDefector(TestDefector): """Defector is equivalent to MemoryTwoPlayer((0, 0, ..., 0), D)""" - name = "Generic Memory Two Player: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], (D, D)" - player = lambda x: axl.MemoryTwoPlayer( - sixteen_vector=[0] * 16, initial=(D, D) - ) + player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=[0] * 16, initial=(D, D)) expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -301,12 +292,8 @@ def four_vector_to_sixteen_vector(four_vector): class TestMemoryTwoAlternator(TestAlternator): """Alternator is equivalent to MemoryTwoPlayer(0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1), C).""" - name = "Generic Memory Two Player: [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1], (C, D)" - player = lambda x: axl.MemoryTwoPlayer( - sixteen_vector=four_vector_to_sixteen_vector((0, 0, 1, 1)), - initial=(C, D), - ) + player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=four_vector_to_sixteen_vector((0, 0, 1, 1)), initial=(C, D)) expected_classifier = { "memory_depth": 1, "stochastic": False, diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 3cb2c8241..6ec22db6b 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -41,9 +41,7 @@ def classifier_test(self, expected_class_classifier=None): for t in player.team: try: - classifier["makes_use_of"].update( - axl.Classifiers["make_use_of"](t) - ) + classifier["makes_use_of"].update(axl.Classifiers["make_use_of"](t)) except KeyError: pass @@ -252,6 +250,7 @@ class TestNiceMetaWinnerEnsemble(TestMetaPlayer): "manipulates_state": False, } + def test_strategy(self): actions = [(C, C)] * 8 self.versus_test( @@ -371,9 +370,7 @@ def test_strategy(self): opponent = axl.MockPlayer([C] * 100 + [D]) actions = [(C, C)] * 100 + [(C, D), (D, C)] self.versus_test( - opponent=opponent, - expected_actions=actions, - init_kwargs={"team": team}, + opponent=opponent, expected_actions=actions, init_kwargs={"team": team} ) @@ -392,9 +389,7 @@ class TestMetaMajorityMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) class TestMetaMajorityFiniteMemory(TestMetaPlayer): @@ -412,9 +407,7 @@ class TestMetaMajorityFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) class TestMetaMajorityLongMemory(TestMetaPlayer): @@ -432,15 +425,11 @@ class TestMetaMajorityLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=0 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=0) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) class TestMetaWinnerMemoryOne(TestMetaPlayer): @@ -458,9 +447,7 @@ class TestMetaWinnerMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) class TestMetaWinnerFiniteMemory(TestMetaPlayer): @@ -478,9 +465,7 @@ class TestMetaWinnerFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) class TestMetaWinnerLongMemory(TestMetaPlayer): @@ -498,9 +483,7 @@ class TestMetaWinnerLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=4 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=4) class TestMetaWinnerDeterministic(TestMetaPlayer): @@ -536,9 +519,7 @@ class TestMetaWinnerStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) class TestMetaMixer(TestMetaPlayer): @@ -614,12 +595,7 @@ def test_strategy(self): ) team.append(axl.Defector) - distribution = [ - 0.2, - 0.5, - 0.3, - 0, - ] # If add a defector but does not occur + distribution = [0.2, 0.5, 0.3, 0] # If add a defector but does not occur self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, @@ -663,9 +639,7 @@ def classifier_test(self, expected_class_classifier=None): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=11 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=11) class TestNMWEStochastic(TestMetaPlayer): @@ -683,9 +657,7 @@ class TestNMWEStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=16 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=16) class TestNMWEFiniteMemory(TestMetaPlayer): @@ -703,9 +675,7 @@ class TestNMWEFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=7 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=7) class TestNMWELongMemory(TestMetaPlayer): @@ -723,9 +693,7 @@ class TestNMWELongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=3 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=3) class TestNMWEMemoryOne(TestMetaPlayer): @@ -743,9 +711,7 @@ class TestNMWEMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.Alternator(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) class TestMemoryDecay(TestPlayer): @@ -854,5 +820,5 @@ def test_memory_alter_delete(self): opponent, expected_actions=actions, init_kwargs={"start_strategy": axl.Cooperator}, - seed=11, + seed=11 ) diff --git a/axelrod/tests/strategies/test_negation.py b/axelrod/tests/strategies/test_negation.py index f74211b46..8c7542aaa 100644 --- a/axelrod/tests/strategies/test_negation.py +++ b/axelrod/tests/strategies/test_negation.py @@ -36,6 +36,4 @@ def test_strategy(self): opponent=axl.Cooperator(), expected_actions=actions, seed=1 ) actions = [(D, D), (C, D), (C, D)] - self.versus_test( - opponent=axl.Defector(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=2) diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index 76c06e945..ec9fe051c 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -458,22 +458,14 @@ def test_reset_clone(self): clone = player.clone() self.assertEqual(player, clone) - @given( - seed=integers(min_value=1, max_value=20000000), - turns=integers(min_value=5, max_value=10), - noise=integers(min_value=0, max_value=10), - ) + @given(seed=integers(min_value=1, max_value=20000000), + turns=integers(min_value=5, max_value=10), + noise=integers(min_value=0, max_value=10)) @settings(max_examples=1, deadline=None) def test_clone_reproducible_play(self, seed, turns, noise): # Test that the cloned player produces identical play player = self.player() - if player.name in [ - "Darwin", - "Human", - "Mind Bender", - "Mind Controller", - "Mind Warper", - ]: + if player.name in ["Darwin", "Human", "Mind Bender", "Mind Controller", "Mind Warper"]: # Known exceptions return @@ -487,15 +479,8 @@ def test_clone_reproducible_play(self, seed, turns, noise): player_clone = player.clone() op = op.clone() op_clone = op.clone() - m1 = axl.Match( - (player, op), turns=turns, seed=seed, noise=noise / 100.0 - ) - m2 = axl.Match( - (player_clone, op_clone), - turns=turns, - seed=seed, - noise=noise / 100.0, - ) + m1 = axl.Match((player, op), turns=turns, seed=seed, noise=noise/100.) + m2 = axl.Match((player_clone, op_clone), turns=turns, seed=seed, noise=noise/100.) m1.play() m2.play() self.assertEqual(m1.result, m2.result) @@ -592,7 +577,7 @@ def versus_test( noise=noise, seed=seed, attrs=attrs, - match_attributes=match_attributes, + match_attributes=match_attributes ) def classifier_test(self, expected_class_classifier=None): @@ -625,14 +610,13 @@ def classifier_test(self, expected_class_classifier=None): axl.Classifiers[key](player), self.expected_classifier[key], msg="%s - Behaviour: %s != Expected Behaviour: %s" - % ( - key, - axl.Classifiers[key](player), - self.expected_classifier[key], - ), + % ( + key, + axl.Classifiers[key](player), + self.expected_classifier[key], + ), ) - class TestMatch(unittest.TestCase): """Test class for heads up play between two given players. Plays an axelrod match between the two players.""" @@ -647,7 +631,7 @@ def versus_test( noise=None, seed=None, match_attributes=None, - attrs=None, + attrs=None ): """Tests a sequence of outcomes for two given players.""" if len(expected_actions1) != len(expected_actions2): @@ -656,22 +640,14 @@ def versus_test( turns = len(expected_actions1) match = axl.Match( - (player1, player2), - turns=turns, - noise=noise, - seed=seed, - match_attributes=match_attributes, - ) + (player1, player2), turns=turns, noise=noise, seed=seed, + match_attributes=match_attributes) match.play() # Test expected sequence of plays from the match is as expected. - for i, (play, expected_play) in enumerate( - zip(player1.history, expected_actions1) - ): + for i, (play, expected_play) in enumerate(zip(player1.history, expected_actions1)): self.assertEqual((i, play), (i, expected_play)) - for i, (play, expected_play) in enumerate( - zip(player2.history, expected_actions2) - ): + for i, (play, expected_play) in enumerate(zip(player2.history, expected_actions2)): self.assertEqual((i, play), (i, expected_play)) # Test final player attributes are as expected diff --git a/axelrod/tests/strategies/test_prober.py b/axelrod/tests/strategies/test_prober.py index 7f9e3dd48..229b3fe1e 100644 --- a/axelrod/tests/strategies/test_prober.py +++ b/axelrod/tests/strategies/test_prober.py @@ -194,28 +194,7 @@ class TestProber4(TestPlayer): "manipulates_source": False, "manipulates_state": False, } - initial_sequence = [ - C, - C, - D, - C, - D, - D, - D, - C, - C, - D, - C, - D, - C, - C, - D, - C, - D, - D, - C, - D, - ] + initial_sequence = [C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D] def test_strategy(self): # Starts by playing CCDCDDDCCDCDCCDCDDCD. @@ -235,9 +214,7 @@ def test_strategy(self): for history in provocative_histories: opponent = axl.MockPlayer(history + [C] * 5) actions = list(zip(self.initial_sequence, history)) + [(D, C)] * 5 - self.versus_test( - opponent=opponent, expected_actions=actions, attrs=attrs - ) + self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) # Otherwise cooperates for 5 rounds and plays TfT afterwards unprovocative_histories = [ @@ -253,9 +230,7 @@ def test_strategy(self): opponent = axl.MockPlayer(history + [D] * 5 + [C, C]) actions = list(zip(self.initial_sequence, history)) + [(C, D)] * 5 actions += [(D, C), (C, C)] - self.versus_test( - opponent=opponent, expected_actions=actions, attrs=attrs - ) + self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) class TestHardProber(TestPlayer): diff --git a/axelrod/tests/strategies/test_qlearner.py b/axelrod/tests/strategies/test_qlearner.py index c44c64c77..948c8c02e 100644 --- a/axelrod/tests/strategies/test_qlearner.py +++ b/axelrod/tests/strategies/test_qlearner.py @@ -41,13 +41,7 @@ def test_strategy(self): "CC2.0": {C: 2.7, D: 0}, "CCC3.0": {C: 0, D: 0}, }, - "Vs": { - "": 0.9, - "0.0": 2.7, - "C1.0": 4.5, - "CC2.0": 2.7, - "CCC3.0": 0, - }, + "Vs": {"": 0.9, "0.0": 2.7, "C1.0": 4.5, "CC2.0": 2.7, "CCC3.0": 0}, "prev_state": "CCC3.0", }, ) @@ -81,13 +75,7 @@ def test_strategy(self): "CC2.0": {C: 2.7, D: 0}, "CCC3.0": {C: 0, D: 0}, }, - "Vs": { - "": 0.9, - "0.0": 2.7, - "C1.0": 4.5, - "CC2.0": 2.7, - "CCC3.0": 0, - }, + "Vs": {"": 0.9, "0.0": 2.7, "C1.0": 4.5, "CC2.0": 2.7, "CCC3.0": 0}, "prev_state": "CCC3.0", }, ) @@ -121,13 +109,7 @@ def test_strategy(self): "DD0.0": {C: 0, D: 0}, "DDD0.0": {C: 0, D: 0}, }, - "Vs": { - "": 0.1, - "0.0": 0.0, - "D0.0": 0.1, - "DD0.0": 0.0, - "DDD0.0": 0, - }, + "Vs": {"": 0.1, "0.0": 0.0, "D0.0": 0.1, "DD0.0": 0.0, "DDD0.0": 0}, "prev_state": "DDD0.0", }, ) @@ -161,13 +143,7 @@ def test_strategy(self): "DD0.0": {C: 0, D: 0}, "DDD0.0": {C: 0, D: 0}, }, - "Vs": { - "": 0.1, - "0.0": 0.0, - "D0.0": 0.1, - "DD0.0": 0.0, - "DDD0.0": 0, - }, + "Vs": {"": 0.1, "0.0": 0.0, "D0.0": 0.1, "DD0.0": 0.0, "DDD0.0": 0}, "prev_state": "DDD0.0", }, ) diff --git a/axelrod/tests/strategies/test_rand.py b/axelrod/tests/strategies/test_rand.py index 03754a169..c9341ab87 100644 --- a/axelrod/tests/strategies/test_rand.py +++ b/axelrod/tests/strategies/test_rand.py @@ -23,14 +23,10 @@ class TestRandom(TestPlayer): def test_deterministic(self): actions = [(D, C), (D, C), (D, C)] - self.versus_test( - axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0} - ) + self.versus_test(axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0}) actions = [(C, C), (C, C), (C, C)] - self.versus_test( - axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1} - ) + self.versus_test(axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1}) def test_stochastic_behavior1(self): """Test that strategy is randomly picked (not affected by history).""" diff --git a/axelrod/tests/strategies/test_retaliate.py b/axelrod/tests/strategies/test_retaliate.py index e3918f1ff..cd8660733 100644 --- a/axelrod/tests/strategies/test_retaliate.py +++ b/axelrod/tests/strategies/test_retaliate.py @@ -93,18 +93,14 @@ def test_strategy(self): opponent = axl.Cooperator() actions = [(C, C)] * 5 self.versus_test( - opponent=opponent, - expected_actions=actions, - attrs={"retaliating": False}, + opponent=opponent, expected_actions=actions, attrs={"retaliating": False} ) # Retaliate after a (C, D) round. opponent = axl.MockPlayer([C, C, C, D, C]) actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)] self.versus_test( - opponent=opponent, - expected_actions=actions, - attrs={"retaliating": True}, + opponent=opponent, expected_actions=actions, attrs={"retaliating": True} ) opponent = axl.Alternator() @@ -112,9 +108,7 @@ def test_strategy(self): # Count retaliations actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] self.versus_test( - opponent=opponent, - expected_actions=actions, - attrs={"retaliation_count": 3}, + opponent=opponent, expected_actions=actions, attrs={"retaliation_count": 3} ) opponent = axl.Alternator() diff --git a/axelrod/tests/strategies/test_revised_downing.py b/axelrod/tests/strategies/test_revised_downing.py index 8006e03ef..fa6897a85 100644 --- a/axelrod/tests/strategies/test_revised_downing.py +++ b/axelrod/tests/strategies/test_revised_downing.py @@ -4,7 +4,6 @@ C, D = axl.Action.C, axl.Action.D - class TestRevisedDowning(TestPlayer): name = "Revised Downing" diff --git a/axelrod/tests/strategies/test_selfsteem.py b/axelrod/tests/strategies/test_selfsteem.py index 76b6e5db2..d45730247 100644 --- a/axelrod/tests/strategies/test_selfsteem.py +++ b/axelrod/tests/strategies/test_selfsteem.py @@ -23,33 +23,20 @@ class TestSelfSteem(TestPlayer): def test_strategy1(self): # Check for f > 0.95, defect actions = ( - [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] - + [(C, C)] * 4 - + [(D, C)] + [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] + [(C, C)] * 4 + [(D, C)] ) self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) def test_strategy2(self): # Check for f < -0.95, cooperate - actions = [ - (D, C), - (C, C), - (D, C), - (D, C), - (C, C), - (D, C), - (C, C), - (C, C), - ] + actions = [(D, C), (C, C), (D, C), (D, C), (C, C), (D, C), (C, C), (C, C)] self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, seed=10 ) def test_strategy3(self): actions = [(D, D)] + [(D, D)] * 5 + [(D, D), (C, D), (C, D)] - self.versus_test( - opponent=axl.Defector(), expected_actions=actions, seed=10 - ) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=10) def test_strategy4(self): # Check for -0.3 < f < 0.3, random @@ -70,9 +57,7 @@ def test_strategy5(self): + [(D, D)] * 8 + [(C, D), (C, D), (D, D), (D, D), (D, D)] ) - self.versus_test( - opponent=axl.Defector(), expected_actions=actions, seed=32 - ) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=32) def test_strategy6(self): # Check for 0.95 > abs(f) > 0.3, follows TitForTat @@ -81,9 +66,7 @@ def test_strategy6(self): + [(C, D), (D, D), (C, D), (C, D), (D, D), (C, D)] + [(D, D)] * 5 ) - self.versus_test( - opponent=axl.Defector(), expected_actions=actions, seed=17 - ) + self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=17) def test_strategy7(self): actions = [ @@ -98,6 +81,4 @@ def test_strategy7(self): (C, C), (C, C), ] - self.versus_test( - opponent=axl.Cooperator(), expected_actions=actions, seed=10 - ) + self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=10) diff --git a/axelrod/tests/strategies/test_shortmem.py b/axelrod/tests/strategies/test_shortmem.py index 525aea843..48dcd0138 100644 --- a/axelrod/tests/strategies/test_shortmem.py +++ b/axelrod/tests/strategies/test_shortmem.py @@ -12,7 +12,7 @@ class TestShortMem(TestPlayer): name = "ShortMem" player = axl.ShortMem expected_classifier = { - "memory_depth": float("inf"), + "memory_depth": float('inf'), "stochastic": False, "makes_use_of": set(), "inspects_source": False, @@ -44,26 +44,14 @@ def test_strategy(self): ) # If neither of the above conditions are met, apply TitForTat - actions = ( - [(C, D)] * 5 - + [(C, C)] * 6 - + [(C, D), (D, D), (D, D), (D, C), (C, C)] - ) + actions = [(C, D)] * 5 + [(C, C)] * 6 + [(C, D), (D, D), (D, D), (D, C), (C, C)] self.versus_test( - opponent=axl.MockPlayer( - actions=[D] * 5 + [C] * 6 + [D, D, D, C, C] - ), + opponent=axl.MockPlayer(actions=[D] * 5 + [C] * 6 + [D, D, D, C, C]), expected_actions=actions, ) - actions = ( - [(C, C)] * 5 - + [(C, D)] * 6 - + [(D, C), (C, C), (C, C), (C, D), (D, D)] - ) + actions = [(C, C)] * 5 + [(C, D)] * 6 + [(D, C), (C, C), (C, C), (C, D), (D, D)] self.versus_test( - opponent=axl.MockPlayer( - actions=[C] * 5 + [D] * 6 + [C, C, C, D, D] - ), + opponent=axl.MockPlayer(actions=[C] * 5 + [D] * 6 + [C, C, C, D, D]), expected_actions=actions, ) diff --git a/axelrod/tests/strategies/test_stalker.py b/axelrod/tests/strategies/test_stalker.py index 21e90dfde..59b6d1370 100644 --- a/axelrod/tests/strategies/test_stalker.py +++ b/axelrod/tests/strategies/test_stalker.py @@ -28,14 +28,12 @@ def test_strategy(self): # wish_score < current_average_score < very_good_score actions = [(C, C)] * 7 + [(C, D), (C, D), (C, C), (C, C), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 2), - expected_actions=actions, + opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 2), expected_actions=actions ) actions = [(C, C)] * 7 + [(C, D), (C, C), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D]), - expected_actions=actions, + opponent=axl.MockPlayer(actions=[C] * 7 + [D]), expected_actions=actions ) # current_average_score > 2 @@ -45,8 +43,7 @@ def test_strategy(self): # 1 < current_average_score < 2 actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, D)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 5), - expected_actions=actions, + opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 5), expected_actions=actions ) def test_strategy2(self): @@ -78,9 +75,7 @@ def test_strategy4(self): # defect in last round actions = [(C, C)] * 199 + [(D, C)] self.versus_test( - axl.Cooperator(), - expected_actions=actions, - match_attributes={"length": 200}, + axl.Cooperator(), expected_actions=actions, match_attributes={"length": 200} ) # Force a defection before the end of the actual match which ensures diff --git a/axelrod/tests/strategies/test_titfortat.py b/axelrod/tests/strategies/test_titfortat.py index a13c06568..fb984ea4b 100644 --- a/axelrod/tests/strategies/test_titfortat.py +++ b/axelrod/tests/strategies/test_titfortat.py @@ -386,16 +386,7 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) - actions = [ - (C, D), - (D, C), - (C, D), - (C, C), - (C, C), - (C, C), - (C, D), - (D, C), - ] + actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, @@ -403,16 +394,7 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) - actions = [ - (C, D), - (D, C), - (C, D), - (C, C), - (C, C), - (C, D), - (D, D), - (D, D), - ] + actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] self.versus_test( opponent, expected_actions=actions, @@ -489,9 +471,7 @@ def test_specific_set_of_results(self): to a memory one player that start by defecting and only cooperates if both players cooperated in the previous round. """ - mistrust_with_bug = axl.MemoryOnePlayer( - initial=D, four_vector=(1, 0, 0, 0), - ) + mistrust_with_bug = axl.MemoryOnePlayer(initial=D, four_vector=(1, 0, 0, 0),) players = [ self.player(), axl.TitForTat(), @@ -634,16 +614,7 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) - actions = [ - (C, D), - (D, C), - (C, D), - (C, C), - (C, C), - (C, C), - (C, D), - (D, C), - ] + actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, @@ -656,16 +627,7 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) - actions = [ - (C, D), - (D, C), - (C, D), - (C, C), - (C, C), - (C, D), - (D, D), - (D, D), - ] + actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] self.versus_test( opponent, expected_actions=actions, @@ -707,9 +669,7 @@ def test_output_from_literature(self): ] turns = 1000 - tournament = axl.Tournament( - players, turns=turns, repetitions=1, seed=75 - ) + tournament = axl.Tournament(players, turns=turns, repetitions=1, seed=75) results = tournament.play(progress_bar=False) scores = [ round(average_score_per_turn * 1000, 1) @@ -754,10 +714,8 @@ def test_init(self): self.assertEqual(player._recorded_history, []) @given( - strategies=strategy_lists( - strategies=deterministic_strategies, max_size=1 - ), - turns=integers(min_value=1, max_value=20), + strategies=strategy_lists(strategies=deterministic_strategies, max_size=1), + turns=integers(min_value=1, max_value=20) ) @settings(deadline=None) def test_is_tit_for_tat_with_no_noise(self, strategies, turns): @@ -769,59 +727,33 @@ def test_is_tit_for_tat_with_no_noise(self, strategies, turns): self.assertEqual(m1.play(), m2.play()) def test_strategy_with_noise1(self): - self.versus_test( - axl.Defector(), - [(C, D)], - turns=1, - seed=9, - attrs={"_recorded_history": [C]}, - ) + self.versus_test(axl.Defector(), [(C, D)], turns=1, seed=9, + attrs={"_recorded_history": [C]}) def test_strategy_with_noise2(self): - self.versus_test( - axl.Defector(), - [(D, C)], - turns=1, - noise=0.5, - seed=11, - attrs={"_recorded_history": [C]}, - ) + self.versus_test(axl.Defector(), [(D, C)], turns=1, noise=0.5, seed=11, + attrs={"_recorded_history": [C]}) def test_strategy_with_noise3(self): # After noise: is contrite actions = list(zip([D, C], [C, D])) - self.versus_test( - axl.Defector(), - actions, - turns=2, - noise=0.5, - seed=49, - attrs={"_recorded_history": [C, C], "contrite": True}, - ) + self.versus_test(axl.Defector(), actions, turns=2, noise=0.5, seed=49, + attrs={"_recorded_history": [C, C], + "contrite": True}) def test_strategy_with_noise4(self): # Cooperates and no longer contrite actions = list(zip([D, C, C], [C, D, D])) - self.versus_test( - axl.Defector(), - actions, - turns=3, - noise=0.5, - seed=49, - attrs={"_recorded_history": [C, C, C], "contrite": False}, - ) + self.versus_test(axl.Defector(), actions, turns=3, noise=0.5, seed=49, + attrs={"_recorded_history": [C, C, C], + "contrite": False}) def test_strategy_with_noise5(self): # Defects and no longer contrite actions = list(zip([D, C, C, D], [C, D, D, D])) - self.versus_test( - axl.Defector(), - actions, - turns=4, - noise=0.5, - seed=158, - attrs={"_recorded_history": [C, C, C, D], "contrite": False}, - ) + self.versus_test(axl.Defector(), actions, turns=4, noise=0.5, seed=158, + attrs={"_recorded_history": [C, C, C, D], + "contrite": False}) class TestAdaptiveTitForTat(TestPlayer): @@ -968,9 +900,7 @@ class TestEugineNier(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (D, C)] self.versus_test( - axl.Cooperator(), - expected_actions=actions, - attrs={"is_defector": False}, + axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False} ) actions = [(C, C), (C, C), (C, C), (C, C)] @@ -984,9 +914,7 @@ def test_strategy(self): # Plays TfT and defects in last round actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)] self.versus_test( - axl.Alternator(), - expected_actions=actions, - attrs={"is_defector": False}, + axl.Alternator(), expected_actions=actions, attrs={"is_defector": False} ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] @@ -999,16 +927,7 @@ def test_strategy(self): # Becomes defector after 5 defections opponent = axl.MockPlayer(actions=[D, C, D, D, D, D, C, C]) - actions = [ - (C, D), - (D, C), - (C, D), - (D, D), - (D, D), - (D, D), - (D, C), - (D, C), - ] + actions = [(C, D), (D, C), (C, D), (D, D), (D, D), (D, D), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions) @@ -1036,17 +955,13 @@ def test_strategy(self): init_kwargs = {"N": 1, "M": 2} opponent = axl.MockPlayer(actions=[D, D, D, C, C]) actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) # TwoTitsForTat test_strategy init_kwargs = {"N": 2, "M": 1} opponent = axl.MockPlayer(actions=[D, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] - self.versus_test( - opponent, expected_actions=actions, init_kwargs=init_kwargs - ) + self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) actions = [(C, C), (C, C)] self.versus_test( opponent=axl.Cooperator(), @@ -1055,9 +970,7 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (D, D)] self.versus_test( - opponent=axl.Defector(), - expected_actions=actions, - init_kwargs=init_kwargs, + opponent=axl.Defector(), expected_actions=actions, init_kwargs=init_kwargs, ) # Cooperator test_strategy @@ -1104,7 +1017,6 @@ def test_varying_memory_depth(self): class Test1TitsFor1TatsIsTFT(TestTitForTat): """Tests that for N = 1 = M, all the TFT tests are passed.""" - name = "N Tit(s) For M Tat(s): 1, 1" player = lambda x: axl.NTitsForMTats(1, 1) expected_classifier = { @@ -1120,7 +1032,6 @@ class Test1TitsFor1TatsIsTFT(TestTitForTat): class Test1TitsFor2TatsIsTF2T(TestTitFor2Tats): """Tests that for N = 1, M = 2, all the TF2T tests are passed.""" - name = "N Tit(s) For M Tat(s): 1, 2" player = lambda x: axl.NTitsForMTats(1, 2) expected_classifier = { @@ -1136,7 +1047,6 @@ class Test1TitsFor2TatsIsTF2T(TestTitFor2Tats): class Test2TitsFor1TatsIs2TFT(TestTwoTitsForTat): """Tests that for N = 2, M = 1, all the 2TFT tests are passed.""" - name = "N Tit(s) For M Tat(s): 2, 1" player = lambda x: axl.NTitsForMTats(2, 1) expected_classifier = { @@ -1255,9 +1165,7 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (D, D)] - self.versus_test( - axl.Defector(), expected_actions=actions, init_kwargs={"p": 0} - ) + self.versus_test(axl.Defector(), expected_actions=actions, init_kwargs={"p": 0}) actions = [(C, C), (C, C), (D, C), (C, C)] self.versus_test( @@ -1265,9 +1173,7 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (C, D), (D, D)] - self.versus_test( - axl.Defector(), expected_actions=actions, init_kwargs={"p": 1} - ) + self.versus_test(axl.Defector(), expected_actions=actions, init_kwargs={"p": 1}) actions = [(C, C), (C, C), (D, C), (C, C), (C, C), (C, C)] self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) diff --git a/axelrod/tests/strategies/test_worse_and_worse.py b/axelrod/tests/strategies/test_worse_and_worse.py index fa87429ef..1fd2d5ef8 100644 --- a/axelrod/tests/strategies/test_worse_and_worse.py +++ b/axelrod/tests/strategies/test_worse_and_worse.py @@ -101,8 +101,7 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, C), (C, C), (C, D), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C, C, D, C]), - expected_actions=actions, + opponent=axl.MockPlayer(actions=[C, C, D, C]), expected_actions=actions ) def test_strategy3(self): diff --git a/axelrod/tests/strategies/test_zero_determinant.py b/axelrod/tests/strategies/test_zero_determinant.py index 2d9bbac5c..75b98fb5f 100644 --- a/axelrod/tests/strategies/test_zero_determinant.py +++ b/axelrod/tests/strategies/test_zero_determinant.py @@ -32,12 +32,7 @@ class TestZDExtortion(TestPlayer): } def test_four_vector(self): - expected_dictionary = { - (C, C): 0.64, - (C, D): 0.18, - (D, C): 0.28, - (D, D): 0, - } + expected_dictionary = {(C, C): 0.64, (C, D): 0.18, (D, C): 0.28, (D, D): 0} test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -48,9 +43,7 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=1 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=1) class TestZDExtort2(TestPlayer): @@ -68,12 +61,7 @@ class TestZDExtort2(TestPlayer): } def test_four_vector(self): - expected_dictionary = { - (C, C): 8 / 9, - (C, D): 0.5, - (D, C): 1 / 3, - (D, D): 0.0, - } + expected_dictionary = {(C, C): 8 / 9, (C, D): 0.5, (D, C): 1 / 3, (D, D): 0.0} test_four_vector(self, expected_dictionary) def test_receive_match_attributes(self): @@ -95,15 +83,11 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (D, C), (C, D), (C, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=10 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=7 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=7) class TestZDExtort2v2(TestPlayer): @@ -137,9 +121,7 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) class TestZDExtort3(TestPlayer): @@ -172,9 +154,7 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=2 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) class TestZDExtort4(TestPlayer): @@ -192,12 +172,7 @@ class TestZDExtort4(TestPlayer): } def test_four_vector(self): - expected_dictionary = { - (C, C): 11 / 17, - (C, D): 0, - (D, C): 8 / 17, - (D, D): 0.0, - } + expected_dictionary = {(C, C): 11 / 17, (C, D): 0, (D, C): 8 / 17, (D, D): 0.0} test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -208,9 +183,7 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=10 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) class TestZDGen2(TestPlayer): @@ -228,12 +201,7 @@ class TestZDGen2(TestPlayer): } def test_four_vector(self): - expected_dictionary = { - (C, C): 1, - (C, D): 9 / 16, - (D, C): 1 / 2, - (D, D): 1 / 8, - } + expected_dictionary = {(C, C): 1, (C, D): 9 / 16, (D, C): 1 / 2, (D, D): 1 / 8} test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -250,15 +218,11 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (C, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=10 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=3 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=3) class TestZDGTFT2(TestPlayer): @@ -275,12 +239,7 @@ class TestZDGTFT2(TestPlayer): } def test_four_vector(self): - expected_dictionary = { - (C, C): 1.0, - (C, D): 1 / 8, - (D, C): 1.0, - (D, D): 0.25, - } + expected_dictionary = {(C, C): 1.0, (C, D): 1 / 8, (D, C): 1.0, (D, D): 0.25} test_four_vector(self, expected_dictionary) def test_receive_match_attributes(self): @@ -302,15 +261,11 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=4 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=4) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=23 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=23) class TestZDMischief(TestPlayer): @@ -339,9 +294,7 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=4 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=4) class TestZDSet2(TestPlayer): @@ -375,6 +328,4 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test( - opponent=axl.CyclerDC(), expected_actions=actions, seed=12 - ) + self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=12) diff --git a/axelrod/tests/unit/test_classification.py b/axelrod/tests/unit/test_classification.py index c44d23516..c5d90df5f 100644 --- a/axelrod/tests/unit/test_classification.py +++ b/axelrod/tests/unit/test_classification.py @@ -50,9 +50,7 @@ def tearDown(self) -> None: def test_classifier_build(self): dirname = os.path.dirname(__file__) - test_path = os.path.join( - dirname, "../../../test_outputs/classifier_test.yaml" - ) + test_path = os.path.join(dirname, "../../../test_outputs/classifier_test.yaml") # Just returns the name of the player. For testing. name_classifier = Classifier[Text]("name", lambda player: player.name) @@ -68,10 +66,7 @@ def test_classifier_build(self): self.assertDictEqual( all_player_dicts, - { - "Cooperator": {"name": "Cooperator"}, - "Defector": {"name": "Defector"}, - }, + {"Cooperator": {"name": "Cooperator"}, "Defector": {"name": "Defector"}}, ) def test_singletonity_of_classifiers_class(self): @@ -96,9 +91,7 @@ def test_key_error_on_uknown_classifier(self): Classifiers["invalid_key"](axl.TitForTat) def test_will_lookup_key_in_dict(self): - self.assertEqual( - Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1 - ) + self.assertEqual(Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1) def test_will_lookup_key_for_classes_that_cant_init(self): with self.assertRaises(Exception) as exptn: @@ -121,9 +114,7 @@ def test_known_classifiers(self): for s in axl.all_strategies: s = s() - self.assertTrue( - None not in [Classifiers[key](s) for key in known_keys] - ) + self.assertTrue(None not in [Classifiers[key](s) for key in known_keys]) def test_multiple_instances(self): """Certain instances of classes of strategies will have different @@ -194,19 +185,13 @@ def test_obey_axelrod(self): ] for strategy in known_cheaters: - self.assertFalse( - axl.Classifiers.obey_axelrod(strategy()), msg=strategy - ) + self.assertFalse(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) for strategy in known_basic: - self.assertTrue( - axl.Classifiers.obey_axelrod(strategy()), msg=strategy - ) + self.assertTrue(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) for strategy in known_ordinary: - self.assertTrue( - axl.Classifiers.obey_axelrod(strategy()), msg=strategy - ) + self.assertTrue(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) def test_is_basic(self): """A test that verifies if the is_basic function works correctly""" @@ -309,9 +294,7 @@ def test_inclusion_of_strategy_lists(self): axl.basic_strategies, axl.long_run_time_strategies, ]: - self.assertTrue( - str_reps(strategy_list).issubset(str_reps(strategies_set)) - ) + self.assertTrue(str_reps(strategy_list).issubset(str_reps(strategies_set))) def test_long_run_strategies(self): long_run_time_strategies = [ @@ -336,8 +319,7 @@ def test_long_run_strategies(self): ] self.assertEqual( - str_reps(long_run_time_strategies), - str_reps(axl.long_run_time_strategies), + str_reps(long_run_time_strategies), str_reps(axl.long_run_time_strategies) ) self.assertTrue( all(map(Classifiers["long_run_time"], axl.long_run_time_strategies)) @@ -349,13 +331,10 @@ def test_short_run_strategies(self): ] self.assertEqual( - str_reps(short_run_time_strategies), - str_reps(axl.short_run_time_strategies), + str_reps(short_run_time_strategies), str_reps(axl.short_run_time_strategies) ) self.assertFalse( - any( - map(Classifiers["long_run_time"], axl.short_run_time_strategies) - ) + any(map(Classifiers["long_run_time"], axl.short_run_time_strategies)) ) def test_meta_inclusion(self): @@ -374,6 +353,4 @@ def test_demo_strategies(self): axl.Grudger, axl.Random, ] - self.assertTrue( - str_reps(demo_strategies), str_reps(axl.demo_strategies) - ) + self.assertTrue(str_reps(demo_strategies), str_reps(axl.demo_strategies)) diff --git a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py index 8d46c2ced..2b8ac5408 100644 --- a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py +++ b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py @@ -98,7 +98,7 @@ def test_three_state_tft(self): (1, C, 2, C), (1, D, 0, D), (2, C, 0, C), - (2, D, 2, D), + (2, D, 2, D) ) trans_dict = self.transitions_to_dict(transitions) diff --git a/axelrod/tests/unit/test_deterministic_cache.py b/axelrod/tests/unit/test_deterministic_cache.py index 7d3929a2d..e2c1d1ae3 100644 --- a/axelrod/tests/unit/test_deterministic_cache.py +++ b/axelrod/tests/unit/test_deterministic_cache.py @@ -18,9 +18,7 @@ def setUpClass(cls): cls.test_save_file = axl_filename(save_path) load_path = pathlib.Path("test_outputs/test_cache_load.txt") cls.test_load_file = axl_filename(load_path) - test_data_to_pickle = { - ("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)] - } + test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]} cls.test_pickle = pickle.dumps(test_data_to_pickle) with open(cls.test_load_file, "wb") as f: diff --git a/axelrod/tests/unit/test_filters.py b/axelrod/tests/unit/test_filters.py index 70e11ed6e..88133a6b0 100644 --- a/axelrod/tests/unit/test_filters.py +++ b/axelrod/tests/unit/test_filters.py @@ -18,14 +18,10 @@ class TestStrategy(Player): def test_equality_filter(self): self.assertTrue( - passes_operator_filter( - self.TestStrategy, "stochastic", True, operator.eq - ) + passes_operator_filter(self.TestStrategy, "stochastic", True, operator.eq) ) self.assertFalse( - passes_operator_filter( - self.TestStrategy, "stochastic", False, operator.eq - ) + passes_operator_filter(self.TestStrategy, "stochastic", False, operator.eq) ) self.assertTrue( passes_operator_filter( @@ -74,9 +70,7 @@ def test_list_filter(self): passes_in_list_filter(self.TestStrategy, "makes_use_of", ["length"]) ) self.assertTrue( - passes_in_list_filter( - self.TestStrategy, "makes_use_of", ["game", "length"] - ) + passes_in_list_filter(self.TestStrategy, "makes_use_of", ["game", "length"]) ) self.assertFalse( passes_in_list_filter(self.TestStrategy, "makes_use_of", "test") @@ -125,21 +119,11 @@ def test_passes_filterset(self, smaller, larger): "min_memory_depth": smaller, } - self.assertTrue( - passes_filterset(self.TestStrategy, full_passing_filterset_1) - ) - self.assertTrue( - passes_filterset(self.TestStrategy, full_passing_filterset_2) - ) - self.assertTrue( - passes_filterset(self.TestStrategy, sparse_passing_filterset) - ) - self.assertFalse( - passes_filterset(self.TestStrategy, full_failing_filterset) - ) - self.assertFalse( - passes_filterset(self.TestStrategy, sparse_failing_filterset) - ) + self.assertTrue(passes_filterset(self.TestStrategy, full_passing_filterset_1)) + self.assertTrue(passes_filterset(self.TestStrategy, full_passing_filterset_2)) + self.assertTrue(passes_filterset(self.TestStrategy, sparse_passing_filterset)) + self.assertFalse(passes_filterset(self.TestStrategy, full_failing_filterset)) + self.assertFalse(passes_filterset(self.TestStrategy, sparse_failing_filterset)) def test_filtered_strategies(self): class StochasticTestStrategy(Player): @@ -150,11 +134,7 @@ class StochasticTestStrategy(Player): } class MemoryDepth2TestStrategy(Player): - classifier = { - "stochastic": False, - "memory_depth": 2, - "makes_use_of": [], - } + classifier = {"stochastic": False, "memory_depth": 2, "makes_use_of": []} class UsesLengthTestStrategy(Player): classifier = { diff --git a/axelrod/tests/unit/test_fingerprint.py b/axelrod/tests/unit/test_fingerprint.py index 8f9e25b43..9c727f91e 100644 --- a/axelrod/tests/unit/test_fingerprint.py +++ b/axelrod/tests/unit/test_fingerprint.py @@ -90,9 +90,7 @@ def test_fingerprint_player(self): self.assertEqual(af.points, self.points_when_using_half_step) self.assertEqual(af.spatial_tournament.turns, 5) self.assertEqual(af.spatial_tournament.repetitions, 3) - self.assertEqual( - af.spatial_tournament.edges, self.edges_when_using_half_step - ) + self.assertEqual(af.spatial_tournament.edges, self.edges_when_using_half_step) # The first player is the fingerprinted one, the rest are probes. self.assertIsInstance(af.spatial_tournament.players[0], axl.Cooperator) @@ -131,17 +129,7 @@ def test_fingerprint_interactions_cooperator(self): # The keys are edges between players, values are repetitions. self.assertCountEqual( af.interactions.keys(), - [ - (0, 1), - (0, 2), - (0, 3), - (0, 4), - (0, 5), - (0, 6), - (0, 7), - (0, 8), - (0, 9), - ], + [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9)], ) self.assertEqual(len(af.interactions.values()), 9) @@ -199,9 +187,7 @@ def test_fingerprint_interactions_titfortat(self): def test_progress_bar_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) - data = af.fingerprint( - turns=10, repetitions=2, step=0.5, progress_bar=True - ) + data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=True) self.assertEqual(sorted(data.keys()), self.points_when_using_half_step) @patch("axelrod.fingerprint.mkstemp", RecordedMksTemp.mkstemp) @@ -230,11 +216,7 @@ def test_fingerprint_with_filename(self): filename = axl_filename(path) af = AshlockFingerprint(axl.TitForTat) af.fingerprint( - turns=1, - repetitions=1, - step=0.5, - progress_bar=False, - filename=filename, + turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename ) with open(filename, "r") as out: data = out.read() @@ -242,9 +224,7 @@ def test_fingerprint_with_filename(self): def test_serial_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) - data = af.fingerprint( - turns=10, repetitions=2, step=0.5, progress_bar=False - ) + data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=False) edge_keys = sorted(list(af.interactions.keys())) coord_keys = sorted(list(data.keys())) self.assertEqual(af.step, 0.5) @@ -264,13 +244,9 @@ def test_parallel_fingerprint(self): def test_plot_data(self): af = AshlockFingerprint(axl.Cooperator()) - af.fingerprint( - turns=5, repetitions=3, step=0.5, progress_bar=False, seed=0 - ) + af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False, seed=0) - reshaped_data = np.array( - [[0.0, 0.0, 0.0], [1.0, 2.0, 1.6], [3.0, 3.0, 3.0]] - ) + reshaped_data = np.array([[0.0, 0.0, 0.0], [1., 2., 1.6], [3.0, 3.0, 3.0]]) plotted_data = af.plot().gca().images[0].get_array() np.testing.assert_allclose(plotted_data, reshaped_data) @@ -316,13 +292,12 @@ def test_wsls_fingerprint(self): Point(x=1.0, y=0.25): 4.86, Point(x=1.0, y=0.5): 4.36, Point(x=1.0, y=0.75): 4.05, - Point(x=1.0, y=1.0): 1.3, + Point(x=1.0, y=1.0): 1.3 } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) - data = af.fingerprint( - turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 - ) + data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, + seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -353,13 +328,12 @@ def test_tft_fingerprint(self): Point(x=1.0, y=0.25): 2.68, Point(x=1.0, y=0.5): 2.51, Point(x=1.0, y=0.75): 2.41, - Point(x=1.0, y=1.0): 2.18, + Point(x=1.0, y=1.0): 2.18 } af = axl.AshlockFingerprint(axl.TitForTat(), axl.TitForTat) - data = af.fingerprint( - turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 - ) + data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, + seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -390,13 +364,12 @@ def test_majority_fingerprint(self): Point(x=1.0, y=0.25): 2.12, Point(x=1.0, y=0.5): 1.8599999999999999, Point(x=1.0, y=0.75): 2.0300000000000002, - Point(x=1.0, y=1.0): 2.26, + Point(x=1.0, y=1.0): 2.26 } af = axl.AshlockFingerprint(axl.GoByMajority, axl.TitForTat) - data = af.fingerprint( - turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 - ) + data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, + seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -410,30 +383,22 @@ def test_pair_fingerprints(self, strategy_pair): """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe) - data = af.fingerprint( - turns=2, repetitions=2, step=0.5, progress_bar=False, seed=1 - ) + data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=1) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe) - data = af.fingerprint( - turns=2, repetitions=2, step=0.5, progress_bar=False, seed=2 - ) + data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=2) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe()) - data = af.fingerprint( - turns=2, repetitions=2, step=0.5, progress_bar=False, seed=3 - ) + data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=3) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data = af.fingerprint( - turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 - ) + data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) self.assertIsInstance(data, dict) @given(strategy_pair=strategy_lists(min_size=2, max_size=2)) @@ -445,15 +410,11 @@ def test_fingerprint_reproducibility(self, strategy_pair): """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data = af.fingerprint( - turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 - ) + data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data2 = af.fingerprint( - turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 - ) + data2 = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) self.assertEqual(data, data2) @@ -464,16 +425,13 @@ def test_init(self): fingerprint = axl.TransitiveFingerprint(strategy=player) self.assertEqual(fingerprint.strategy, player) self.assertEqual( - fingerprint.opponents, - [axl.Random(p) for p in np.linspace(0, 1, 50)], + fingerprint.opponents, [axl.Random(p) for p in np.linspace(0, 1, 50)] ) def test_init_with_opponents(self): player = axl.TitForTat() opponents = [s() for s in axl.demo_strategies] - fingerprint = axl.TransitiveFingerprint( - strategy=player, opponents=opponents - ) + fingerprint = axl.TransitiveFingerprint(strategy=player, opponents=opponents) self.assertEqual(fingerprint.strategy, player) self.assertEqual(fingerprint.opponents, opponents) @@ -485,8 +443,7 @@ def test_init_with_not_default_number(self): ) self.assertEqual(fingerprint.strategy, player) self.assertEqual( - fingerprint.opponents, - [axl.Random(p) for p in np.linspace(0, 1, 10)], + fingerprint.opponents, [axl.Random(p) for p in np.linspace(0, 1, 10)] ) def test_fingerprint_with_filename(self): @@ -494,9 +451,7 @@ def test_fingerprint_with_filename(self): filename = axl_filename(path) strategy = axl.TitForTat() tf = TransitiveFingerprint(strategy) - tf.fingerprint( - turns=1, repetitions=1, progress_bar=False, filename=filename - ) + tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename) with open(filename, "r") as out: data = out.read() self.assertEqual(len(data.split("\n")), 102) @@ -506,7 +461,9 @@ def test_serial_fingerprint(self): tf = TransitiveFingerprint(strategy) path = pathlib.Path("test_outputs/test_fingerprint.csv") tf.fingerprint( - repetitions=1, progress_bar=False, filename=axl_filename(path), + repetitions=1, + progress_bar=False, + filename=axl_filename(path), ) self.assertEqual(tf.data.shape, (50, 50)) diff --git a/axelrod/tests/unit/test_graph.py b/axelrod/tests/unit/test_graph.py index 2ca72af31..41f7d6d62 100644 --- a/axelrod/tests/unit/test_graph.py +++ b/axelrod/tests/unit/test_graph.py @@ -39,12 +39,8 @@ def test_undirected_graph_with_vertices_and_unweighted_edges(self): self.assertEqual(str(g), "") self.assertEqual(g._edges, [(1, 2), (2, 1), (2, 3), (3, 2)]) - self.assert_out_mapping( - g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}} - ) - self.assert_in_mapping( - g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}} - ) + self.assert_out_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) + self.assert_in_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) def test_undirected_graph_with_vertices_and_weighted_edges(self): g = axl.graph.Graph(edges=[[1, 2, 10], [2, 3, 5]]) @@ -95,11 +91,7 @@ def test_add_loops_with_existing_loop_and_using_strings(self): g.add_loops() self.assertEqual( list(sorted(g._edges)), - list( - sorted( - [("a", "b"), ("b", "a"), ("c", "c"), ("a", "a"), ("b", "b")] - ) - ), + list(sorted([("a", "b"), ("b", "a"), ("c", "c"), ("a", "a"), ("b", "b")])), ) @@ -155,19 +147,9 @@ def test_length_4_undirected(self): edges = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2), (3, 0), (0, 3)] self.assertEqual(g.vertices, [0, 1, 2, 3]) self.assertEqual(g.edges, edges) - for vertex, neighbors in [ - (0, (1, 3)), - (1, (0, 2)), - (2, (1, 3)), - (3, (0, 2)), - ]: + for vertex, neighbors in [(0, (1, 3)), (1, (0, 2)), (2, (1, 3)), (3, (0, 2))]: self.assertEqual(set(g.out_vertices(vertex)), set(neighbors)) - for vertex, neighbors in [ - (0, (1, 3)), - (1, (0, 2)), - (2, (1, 3)), - (3, (0, 2)), - ]: + for vertex, neighbors in [(0, (1, 3)), (1, (0, 2)), (2, (1, 3)), (3, (0, 2))]: self.assertEqual(set(g.in_vertices(vertex)), set(neighbors)) @@ -228,17 +210,7 @@ def test_size_2_with_loops(self): def test_size_3_with_loops(self): g = axl.graph.complete_graph(3, loops=True) self.assertEqual(g.vertices, [0, 1, 2]) - edges = [ - (0, 1), - (1, 0), - (0, 2), - (2, 0), - (1, 2), - (2, 1), - (0, 0), - (1, 1), - (2, 2), - ] + edges = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1), (0, 0), (1, 1), (2, 2)] self.assertEqual(g.edges, edges) self.assertEqual(g.directed, False) @@ -274,70 +246,59 @@ def test_size_4_with_loops(self): class TestAttachedComplete(unittest.TestCase): def test_size_2(self): g = axl.graph.attached_complete_graphs(2, loops=False) - self.assertEqual(g.vertices, ["0:0", "0:1", "1:0", "1:1"]) + self.assertEqual(g.vertices, ['0:0', '0:1', '1:0', '1:1']) self.assertEqual( g.edges, - [ - ("0:0", "0:1"), - ("0:1", "0:0"), - ("1:0", "1:1"), - ("1:1", "1:0"), - ("0:0", "1:0"), - ("1:0", "0:0"), - ], + [('0:0', '0:1'), ('0:1', '0:0'), ('1:0', '1:1'), ('1:1', '1:0'), ('0:0', '1:0'), ('1:0', '0:0')] ) self.assertEqual(g.directed, False) def test_size_3(self): g = axl.graph.attached_complete_graphs(3, loops=False) - self.assertEqual(g.vertices, ["0:0", "0:1", "0:2", "1:0", "1:1", "1:2"]) + self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) self.assertEqual( g.edges, - [ - ("0:0", "0:1"), - ("0:1", "0:0"), - ("0:0", "0:2"), - ("0:2", "0:0"), - ("0:1", "0:2"), - ("0:2", "0:1"), - ("1:0", "1:1"), - ("1:1", "1:0"), - ("1:0", "1:2"), - ("1:2", "1:0"), - ("1:1", "1:2"), - ("1:2", "1:1"), - ("0:0", "1:0"), - ("1:0", "0:0"), - ], + [('0:0', '0:1'), + ('0:1', '0:0'), + ('0:0', '0:2'), + ('0:2', '0:0'), + ('0:1', '0:2'), + ('0:2', '0:1'), + ('1:0', '1:1'), + ('1:1', '1:0'), + ('1:0', '1:2'), + ('1:2', '1:0'), + ('1:1', '1:2'), + ('1:2', '1:1'), + ('0:0', '1:0'), + ('1:0', '0:0')] ) self.assertEqual(g.directed, False) def test_size_3_with_loops(self): g = axl.graph.attached_complete_graphs(3, loops=True) - self.assertEqual(g.vertices, ["0:0", "0:1", "0:2", "1:0", "1:1", "1:2"]) + self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) self.assertEqual( g.edges, - [ - ("0:0", "0:1"), - ("0:1", "0:0"), - ("0:0", "0:2"), - ("0:2", "0:0"), - ("0:1", "0:2"), - ("0:2", "0:1"), - ("1:0", "1:1"), - ("1:1", "1:0"), - ("1:0", "1:2"), - ("1:2", "1:0"), - ("1:1", "1:2"), - ("1:2", "1:1"), - ("0:0", "1:0"), - ("1:0", "0:0"), - ("0:0", "0:0"), - ("0:1", "0:1"), - ("0:2", "0:2"), - ("1:0", "1:0"), - ("1:1", "1:1"), - ("1:2", "1:2"), - ], + [('0:0', '0:1'), + ('0:1', '0:0'), + ('0:0', '0:2'), + ('0:2', '0:0'), + ('0:1', '0:2'), + ('0:2', '0:1'), + ('1:0', '1:1'), + ('1:1', '1:0'), + ('1:0', '1:2'), + ('1:2', '1:0'), + ('1:1', '1:2'), + ('1:2', '1:1'), + ('0:0', '1:0'), + ('1:0', '0:0'), + ('0:0', '0:0'), + ('0:1', '0:1'), + ('0:2', '0:2'), + ('1:0', '1:0'), + ('1:1', '1:1'), + ('1:2', '1:2')] ) self.assertEqual(g.directed, False) diff --git a/axelrod/tests/unit/test_history.py b/axelrod/tests/unit/test_history.py index 11cd2445c..e7b105c31 100644 --- a/axelrod/tests/unit/test_history.py +++ b/axelrod/tests/unit/test_history.py @@ -83,7 +83,8 @@ def test_flip_plays(self): self.assertEqual(flipped_history, [D, C, D, C, D]) self.assertEqual(flipped_history.cooperations, 2) self.assertEqual(flipped_history.defections, 3) - self.assertEqual(flipped_history.state_distribution, new_distribution) + self.assertEqual(flipped_history.state_distribution, + new_distribution) # Flip operation is idempotent flipped_flipped_history = flipped_history.flip_plays() @@ -93,6 +94,7 @@ def test_flip_plays(self): class TestLimitedHistory(unittest.TestCase): + def test_memory_depth(self): h = LimitedHistory(memory_depth=3) h.append(C, C) @@ -103,9 +105,8 @@ def test_memory_depth(self): self.assertEqual(len(h), 3) self.assertEqual(h.cooperations, 2) self.assertEqual(h.defections, 1) - self.assertEqual( - h.state_distribution, Counter({(C, C): 1, (D, D): 1, (C, D): 1}) - ) + self.assertEqual(h.state_distribution, + Counter({(C, C): 1, (D, D): 1, (C, D): 1})) h.append(D, C) self.assertEqual(len(h), 3) self.assertEqual(h._plays, [D, C, D]) @@ -114,5 +115,4 @@ def test_memory_depth(self): self.assertEqual(h.defections, 2) self.assertEqual( h.state_distribution, - Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0}), - ) + Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0})) diff --git a/axelrod/tests/unit/test_interaction_utils.py b/axelrod/tests/unit/test_interaction_utils.py index d56e62cce..49706fe01 100644 --- a/axelrod/tests/unit/test_interaction_utils.py +++ b/axelrod/tests/unit/test_interaction_utils.py @@ -49,65 +49,39 @@ def test_compute_scores(self): def test_compute_final_score(self): for inter, final_score in zip(self.interactions, self.final_scores): - self.assertEqual( - final_score, axl.interaction_utils.compute_final_score(inter) - ) + self.assertEqual(final_score, axl.interaction_utils.compute_final_score(inter)) def test_compute_final_score_per_turn(self): for inter, final_score_per_round in zip( self.interactions, self.final_score_per_turn ): self.assertEqual( - final_score_per_round, - axl.interaction_utils.compute_final_score_per_turn(inter), + final_score_per_round, axl.interaction_utils.compute_final_score_per_turn(inter) ) def test_compute_winner_index(self): for inter, winner in zip(self.interactions, self.winners): - self.assertEqual( - winner, axl.interaction_utils.compute_winner_index(inter) - ) + self.assertEqual(winner, axl.interaction_utils.compute_winner_index(inter)) def test_compute_cooperations(self): for inter, coop in zip(self.interactions, self.cooperations): - self.assertEqual( - coop, axl.interaction_utils.compute_cooperations(inter) - ) + self.assertEqual(coop, axl.interaction_utils.compute_cooperations(inter)) def test_compute_normalised_cooperations(self): for inter, coop in zip(self.interactions, self.normalised_cooperations): - self.assertEqual( - coop, - axl.interaction_utils.compute_normalised_cooperation(inter), - ) + self.assertEqual(coop, axl.interaction_utils.compute_normalised_cooperation(inter)) def test_compute_state_distribution(self): for inter, dist in zip(self.interactions, self.state_distribution): - self.assertEqual( - dist, axl.interaction_utils.compute_state_distribution(inter) - ) + self.assertEqual(dist, axl.interaction_utils.compute_state_distribution(inter)) def test_compute_normalised_state_distribution(self): - for inter, dist in zip( - self.interactions, self.normalised_state_distribution - ): - self.assertEqual( - dist, - axl.interaction_utils.compute_normalised_state_distribution( - inter - ), - ) + for inter, dist in zip(self.interactions, self.normalised_state_distribution): + self.assertEqual(dist, axl.interaction_utils.compute_normalised_state_distribution(inter)) def test_compute_state_to_action_distribution(self): - for inter, dist in zip( - self.interactions, self.state_to_action_distribution - ): - self.assertEqual( - dist, - axl.interaction_utils.compute_state_to_action_distribution( - inter - ), - ) + for inter, dist in zip(self.interactions, self.state_to_action_distribution): + self.assertEqual(dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ Counter( @@ -119,25 +93,17 @@ def test_compute_state_to_action_distribution(self): ((D, D), C): 1, } ), - Counter( - {((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1} - ), + Counter({((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1}), ] - self.assertEqual( - expected_dist, - axl.interaction_utils.compute_state_to_action_distribution(inter), - ) + self.assertEqual(expected_dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) def test_compute_normalised_state_to_action_distribution(self): for inter, dist in zip( self.interactions, self.normalised_state_to_action_distribution ): self.assertEqual( - dist, - axl.interaction_utils.compute_normalised_state_to_action_distribution( - inter - ), + dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) ) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ @@ -150,22 +116,15 @@ def test_compute_normalised_state_to_action_distribution(self): ((D, D), C): 1, } ), - Counter( - {((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1} - ), + Counter({((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1}), ] self.assertEqual( - expected_dist, - axl.interaction_utils.compute_normalised_state_to_action_distribution( - inter - ), + expected_dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) ) def test_compute_sparklines(self): for inter, spark in zip(self.interactions, self.sparklines): - self.assertEqual( - spark, axl.interaction_utils.compute_sparklines(inter) - ) + self.assertEqual(spark, axl.interaction_utils.compute_sparklines(inter)) def test_read_interactions_from_file(self): tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=False) @@ -178,14 +137,10 @@ def test_read_interactions_from_file(self): (0, 1): [[(C, D), (C, D)] for _ in range(3)], (1, 1): [[(D, D), (D, D)] for _ in range(3)], } - interactions = axl.interaction_utils.read_interactions_from_file( - tmp_file.name, progress_bar=False - ) + interactions = axl.interaction_utils.read_interactions_from_file(tmp_file.name, progress_bar=False) self.assertEqual(expected_interactions, interactions) def test_string_to_interactions(self): string = "CDCDDD" interactions = [(C, D), (C, D), (D, D)] - self.assertEqual( - axl.interaction_utils.string_to_interactions(string), interactions - ) + self.assertEqual(axl.interaction_utils.string_to_interactions(string), interactions) diff --git a/axelrod/tests/unit/test_match.py b/axelrod/tests/unit/test_match.py index 6012ce32f..84040eaa5 100644 --- a/axelrod/tests/unit/test_match.py +++ b/axelrod/tests/unit/test_match.py @@ -38,9 +38,7 @@ def test_init_with_prob_end(self, prob_end, game): self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), game.RPST()) - self.assertEqual( - match.players[0].match_attributes["length"], float("inf") - ) + self.assertEqual(match.players[0].match_attributes["length"], float("inf")) self.assertEqual(match._cache, {}) @given( @@ -58,9 +56,7 @@ def test_init_with_prob_end_and_turns(self, turns, prob_end, game): self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), game.RPST()) - self.assertEqual( - match.players[0].match_attributes["length"], float("inf") - ) + self.assertEqual(match.players[0].match_attributes["length"], float("inf")) self.assertEqual(match._cache, {}) def test_default_init(self): @@ -87,9 +83,7 @@ def test_example_prob_end(self): expected_lengths = [2, 1, 1] for seed, expected_length in zip(range(3), expected_lengths): match = axl.Match((p1, p2), prob_end=0.5, seed=seed) - self.assertEqual( - match.players[0].match_attributes["length"], float("inf") - ) + self.assertEqual(match.players[0].match_attributes["length"], float("inf")) self.assertEqual(len(match.play()), expected_length) self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), (3, 1, 0, 5)) @@ -123,7 +117,7 @@ def test_len_error(self): with self.assertRaises(TypeError): len(match) - @given(p=floats(min_value=1e-10, max_value=1 - 1e-10)) + @given(p=floats(min_value=1e-10, max_value=1-1e-10)) def test_stochastic(self, p): p1, p2 = axl.Cooperator(), axl.Cooperator() match = axl.Match((p1, p2), 5) @@ -136,7 +130,7 @@ def test_stochastic(self, p): match = axl.Match((p1, p2), 5) self.assertTrue(match._stochastic) - @given(p=floats(min_value=1e-10, max_value=1 - 1e-10)) + @given(p=floats(min_value=1e-10, max_value=1-1e-10)) def test_cache_update_required(self, p): p1, p2 = axl.Cooperator(), axl.Cooperator() match = axl.Match((p1, p2), 5, noise=p) @@ -186,7 +180,8 @@ def test_cache_grows(self): self.assertEqual(match.play(), expected_result_5_turn) # The cache should now hold the 5-turn result.. self.assertEqual( - cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn + cache[(axl.Cooperator(), axl.Defector())], + expected_result_5_turn ) def test_cache_doesnt_shrink(self): @@ -205,7 +200,8 @@ def test_cache_doesnt_shrink(self): self.assertEqual(match.play(), expected_result_3_turn) # The cache should still hold the 5. self.assertEqual( - cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn + cache[(axl.Cooperator(), axl.Defector())], + expected_result_5_turn ) def test_scores(self): @@ -365,9 +361,7 @@ def test_sample_length(self): ]: rng = RandomGenerator(seed) r = rng.random() - self.assertEqual( - axl.match.sample_length(prob_end, r), expected_length - ) + self.assertEqual(axl.match.sample_length(prob_end, r), expected_length) def test_sample_with_0_prob(self): self.assertEqual(axl.match.sample_length(0, 0.4), float("inf")) diff --git a/axelrod/tests/unit/test_match_generator.py b/axelrod/tests/unit/test_match_generator.py index 730928216..e91a7eba8 100644 --- a/axelrod/tests/unit/test_match_generator.py +++ b/axelrod/tests/unit/test_match_generator.py @@ -148,9 +148,7 @@ def test_build_single_match_params_with_fixed_length_unknown(self): self.assertEqual(match_params["game"], test_game) self.assertEqual(match_params["prob_end"], None) self.assertEqual(match_params["noise"], 0) - self.assertEqual( - match_params["match_attributes"], {"length": float("inf")} - ) + self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) # Check that can build a match players = [axl.Cooperator(), axl.Defector()] @@ -168,7 +166,7 @@ def test_build_match_chunks(self, repetitions): players=self.players, turns=test_turns, game=test_game, - repetitions=repetitions, + repetitions=repetitions ) chunks = list(rr.build_match_chunks()) match_definitions = [ @@ -179,14 +177,10 @@ def test_build_match_chunks(self, repetitions): (i, j, repetitions) for i in range(5) for j in range(i, 5) ] - self.assertEqual( - sorted(match_definitions), sorted(expected_match_definitions) - ) + self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) - @given( - repetitions=integers(min_value=1, max_value=test_repetitions), - seed=integers(min_value=1, max_value=4294967295), - ) + @given(repetitions=integers(min_value=1, max_value=test_repetitions), + seed=integers(min_value=1, max_value=4294967295),) @settings(max_examples=5) def test_seeding_equality(self, repetitions, seed): rr1 = axl.MatchGenerator( @@ -194,7 +188,7 @@ def test_seeding_equality(self, repetitions, seed): turns=test_turns, game=test_game, repetitions=repetitions, - seed=seed, + seed=seed ) chunks1 = list(rr1.build_match_chunks()) rr2 = axl.MatchGenerator( @@ -202,7 +196,7 @@ def test_seeding_equality(self, repetitions, seed): turns=test_turns, game=test_game, repetitions=repetitions, - seed=seed, + seed=seed ) chunks2 = list(rr2.build_match_chunks()) self.assertEqual(chunks1, chunks2) @@ -213,7 +207,7 @@ def test_seeding_inequality(self, repetitions=10): turns=test_turns, game=test_game, repetitions=repetitions, - seed=0, + seed=0 ) chunks1 = list(rr1.build_match_chunks()) rr2 = axl.MatchGenerator( @@ -221,7 +215,7 @@ def test_seeding_inequality(self, repetitions=10): turns=test_turns, game=test_game, repetitions=repetitions, - seed=1, + seed=1 ) chunks2 = list(rr2.build_match_chunks()) self.assertNotEqual(chunks1, chunks2) @@ -245,9 +239,7 @@ def test_spatial_build_match_chunks(self, repetitions): ] expected_match_definitions = [(i, j, repetitions) for i, j in cycle] - self.assertEqual( - sorted(match_definitions), sorted(expected_match_definitions) - ) + self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) def test_len(self): turns = 5 diff --git a/axelrod/tests/unit/test_moran.py b/axelrod/tests/unit/test_moran.py index 3d5e02734..8018683c6 100644 --- a/axelrod/tests/unit/test_moran.py +++ b/axelrod/tests/unit/test_moran.py @@ -23,21 +23,16 @@ def test_init(self): self.assertEqual(mp.noise, 0) self.assertEqual(mp.initial_players, players) self.assertEqual(mp.players, list(players)) - self.assertEqual( - mp.populations, [Counter({"Cooperator": 1, "Defector": 1})] - ) + self.assertEqual(mp.populations, [Counter({"Cooperator": 1, "Defector": 1})]) self.assertIsNone(mp.winning_strategy_name) self.assertEqual(mp.mutation_rate, 0) self.assertEqual(mp.mode, "bd") self.assertEqual(mp.deterministic_cache, axl.DeterministicCache()) self.assertEqual( - mp.mutation_targets, - {"Cooperator": [players[1]], "Defector": [players[0]]}, + mp.mutation_targets, {"Cooperator": [players[1]], "Defector": [players[0]]} ) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (1, 0)]) - self.assertEqual( - mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)] - ) + self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)]) self.assertEqual(mp.fitness_transformation, None) self.assertEqual(mp.locations, [0, 1]) self.assertEqual(mp.index, {0: 0, 1: 1}) @@ -53,9 +48,7 @@ def test_init(self): sorted([(0, 1), (2, 0), (1, 2), (0, 0), (1, 1), (2, 2)]), ) - mp = axl.MoranProcess( - players, interaction_graph=graph, reproduction_graph=graph - ) + mp = axl.MoranProcess(players, interaction_graph=graph, reproduction_graph=graph) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (2, 0), (1, 2)]) self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (2, 0), (1, 2)]) @@ -232,12 +225,8 @@ def test_two_random_players(self): def test_two_players_with_mutation(self): p1, p2 = axl.Cooperator(), axl.Defector() - mp = MoranProcess( - (p1, p2), mutation_rate=0.2, stop_on_fixation=False, seed=5 - ) - self.assertDictEqual( - mp.mutation_targets, {str(p1): [p2], str(p2): [p1]} - ) + mp = MoranProcess((p1, p2), mutation_rate=0.2, stop_on_fixation=False, seed=5) + self.assertDictEqual(mp.mutation_targets, {str(p1): [p2], str(p2): [p1]}) # Test that mutation causes the population to alternate between # fixations counters = [ @@ -273,9 +262,7 @@ def test_three_players_with_mutation(self): p2 = axl.Random() p3 = axl.Defector() players = [p1, p2, p3] - mp = axl.MoranProcess( - players, mutation_rate=0.2, stop_on_fixation=False - ) + mp = axl.MoranProcess(players, mutation_rate=0.2, stop_on_fixation=False) self.assertDictEqual( mp.mutation_targets, {str(p1): [p3, p2], str(p2): [p1, p3], str(p3): [p1, p2]}, @@ -386,61 +373,49 @@ def test_cooperator_can_win_with_fitness_transformation(self): w = 0.95 fitness_transformation = lambda score: 1 - w + w * score mp = MoranProcess( - players, - turns=10, - fitness_transformation=fitness_transformation, - seed=3419, + players, turns=10, fitness_transformation=fitness_transformation, + seed=3419 ) populations = mp.play() self.assertEqual(mp.winning_strategy_name, "Cooperator") def test_atomic_mutation_fsm(self): - players = [ - axl.EvolvableFSMPlayer( - num_states=2, initial_state=1, initial_action=C, seed=4 - ) - for _ in range(5) - ] + players = [axl.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C, + seed=4) + for _ in range(5)] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=12) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(sorted(mp.populations[-1].items()))[0][0], - "EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643", - ) + 'EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643') self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated) def test_atomic_mutation_cycler(self): cycle_length = 5 - players = [ - axl.EvolvableCycler(cycle_length=cycle_length, seed=4) - for _ in range(5) - ] + players = [axl.EvolvableCycler(cycle_length=cycle_length, seed=4) + for _ in range(5)] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=10) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(mp.populations[-1].items())[0], - ("EvolvableCycler: CCDDD, 5, 0.2, 1, 1164244177", 1), - ) + ('EvolvableCycler: CCDDD, 5, 0.2, 1, 1164244177', 1)) self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated) def test_mutation_method_exceptions(self): cycle_length = 5 - players = [ - axl.EvolvableCycler(cycle_length=cycle_length, seed=4) - for _ in range(5) - ] + players = [axl.EvolvableCycler(cycle_length=cycle_length, seed=4) + for _ in range(5)] with self.assertRaises(ValueError): MoranProcess(players, turns=10, mutation_method="random", seed=10) - players = [ - axl.Cycler(cycle="CD" * random.randint(2, 10)) for _ in range(10) - ] + players = [axl.Cycler(cycle="CD" * random.randint(2, 10)) + for _ in range(10)] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=53) with self.assertRaises(TypeError): for _ in range(10): @@ -467,12 +442,9 @@ def test_complete(self): mp = MoranProcess(players, seed=seed) mp.play() winner = mp.winning_strategy_name - mp = MoranProcess( - players, - interaction_graph=interaction_graph, - reproduction_graph=reproduction_graph, - seed=seed, - ) + mp = MoranProcess(players, interaction_graph=interaction_graph, + reproduction_graph=reproduction_graph, + seed=seed) mp.play() winner2 = mp.winning_strategy_name self.assertEqual(winner, winner2) @@ -511,18 +483,14 @@ def test_asymmetry(self): players.append(axl.Defector()) for seed, outcome in seeds: mp = MoranProcess( - players, - interaction_graph=graph1, - reproduction_graph=graph2, - seed=seed, + players, interaction_graph=graph1, reproduction_graph=graph2, + seed=seed ) mp.play() winner = mp.winning_strategy_name mp = MoranProcess( - players, - interaction_graph=graph2, - reproduction_graph=graph1, - seed=seed, + players, interaction_graph=graph2, reproduction_graph=graph1, + seed=seed ) mp.play() winner2 = mp.winning_strategy_name @@ -540,14 +508,10 @@ def test_cycle_death_birth(self): for _ in range(N // 2): players.append(axl.Defector()) for seed, outcome in seeds: - mp = MoranProcess( - players, interaction_graph=graph, mode="bd", seed=seed - ) + mp = MoranProcess(players, interaction_graph=graph, mode="bd", seed=seed) mp.play() winner = mp.winning_strategy_name - mp = MoranProcess( - players, interaction_graph=graph, mode="db", seed=seed - ) + mp = MoranProcess(players, interaction_graph=graph, mode="db", seed=seed) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome) @@ -577,11 +541,7 @@ def test_init(self): """Test the initialisation process""" self.assertEqual( set(self.amp.cached_outcomes.keys()), - { - ("Cooperator", "Defector"), - ("Cooperator", "Cooperator"), - ("Defector", "Defector"), - }, + {("Cooperator", "Defector"), ("Cooperator", "Cooperator"), ("Defector", "Defector")}, ) self.assertEqual(self.amp.players, self.players) self.assertEqual(self.amp.turns, 0) diff --git a/axelrod/tests/unit/test_pickling.py b/axelrod/tests/unit/test_pickling.py index cec827819..29f25d645 100644 --- a/axelrod/tests/unit/test_pickling.py +++ b/axelrod/tests/unit/test_pickling.py @@ -10,9 +10,7 @@ # First set: special cases -PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()( - axl.strategy_transformers.FlipTransformer()(axl.Cooperator) -) +PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()(axl.strategy_transformers.FlipTransformer()(axl.Cooperator)) class MyDefector(axl.Player): @@ -113,9 +111,7 @@ class JossAnn(axl.Cooperator): probability = [0.2, 0.3] -@axl.strategy_transformers.MixedTransformer( - probability, strategies, name_prefix=None -) +@axl.strategy_transformers.MixedTransformer(probability, strategies, name_prefix=None) class Mixed(axl.Cooperator): pass @@ -163,9 +159,7 @@ def __init__(self): super().__init__(team=team) -TransformedMetaThue = axl.strategy_transformers.IdentityTransformer( - name_prefix=None -)(MetaThue) +TransformedMetaThue = axl.strategy_transformers.IdentityTransformer(name_prefix=None)(MetaThue) transformed_no_prefix = [ @@ -239,24 +233,12 @@ def test_parameterized_player(self): self.assert_original_equals_pickled(player) def test_sequence_player(self): - inline_transformed_thue = axl.strategy_transformers.IdentityTransformer( - name_prefix="Transformed" - )(axl.ThueMorse)() - for player in [ - axl.ThueMorse(), - axl.ThueMorseInverse(), - MetaThue(), - TransformedMetaThue(), - inline_transformed_thue, - TransformedThue(), - ]: + inline_transformed_thue = axl.strategy_transformers.IdentityTransformer(name_prefix="Transformed")(axl.ThueMorse)() + for player in [axl.ThueMorse(), axl.ThueMorseInverse(), MetaThue(), TransformedMetaThue(), + inline_transformed_thue, TransformedThue(), + ]: self.assert_equals_instance_from_pickling(player) - opponents = ( - axl.Defector, - axl.Cooperator, - axl.Random, - axl.CyclerCCCDCD, - ) + opponents = (axl.Defector, axl.Cooperator, axl.Random, axl.CyclerCCCDCD) for opponent_class in opponents: player.reset() opponent = opponent_class() @@ -289,13 +271,9 @@ def test_pickling_all_transformers_as_instance_called_on_a_class(self): self.assert_original_equals_pickled(player) def test_created_on_the_spot_multiple_transformers(self): - player_class = axl.strategy_transformers.FlipTransformer()( - axl.Cooperator - ) + player_class = axl.strategy_transformers.FlipTransformer()(axl.Cooperator) player_class = axl.strategy_transformers.DualTransformer()(player_class) - player = axl.strategy_transformers.FinalTransformer((C, D))( - player_class - )() + player = axl.strategy_transformers.FinalTransformer((C, D))(player_class)() self.assert_original_equals_pickled(player) @@ -312,13 +290,9 @@ def test_dual_transformer_regression_test(self): player_class = axl.WinStayLoseShift player_class = axl.strategy_transformers.DualTransformer()(player_class) - player_class = axl.strategy_transformers.InitialTransformer((C, D))( - player_class - ) + player_class = axl.strategy_transformers.InitialTransformer((C, D))(player_class) player_class = axl.strategy_transformers.DualTransformer()(player_class) - player_class = axl.strategy_transformers.TrackHistoryTransformer()( - player_class - ) + player_class = axl.strategy_transformers.TrackHistoryTransformer()(player_class) interspersed_dual_transformers = player_class() @@ -340,8 +314,7 @@ def test_class_and_instance_name_different_built_from_player_class(self): player = MyCooperator() class_names = [class_.__name__ for class_ in MyCooperator.mro()] self.assertEqual( - class_names, - ["FlippedMyCooperator", "MyCooperator", "Player", "object"], + class_names, ["FlippedMyCooperator", "MyCooperator", "Player", "object"] ) self.assert_original_equals_pickled(player) @@ -401,23 +374,17 @@ def test_with_various_name_prefixes(self): self.assertEqual(no_prefix.__class__.__name__, "Flip") self.assert_original_equals_pickled(no_prefix) - default_prefix = axl.strategy_transformers.FlipTransformer()( - axl.Cooperator - )() + default_prefix = axl.strategy_transformers.FlipTransformer()(axl.Cooperator)() self.assertEqual(default_prefix.__class__.__name__, "FlippedCooperator") self.assert_original_equals_pickled(default_prefix) - fliptastic = axl.strategy_transformers.FlipTransformer( - name_prefix="Fliptastic" - ) + fliptastic = axl.strategy_transformers.FlipTransformer(name_prefix="Fliptastic") new_prefix = fliptastic(axl.Cooperator)() self.assertEqual(new_prefix.__class__.__name__, "FliptasticCooperator") self.assert_original_equals_pickled(new_prefix) def test_dynamic_class_no_name_prefix(self): - player = axl.strategy_transformers.FlipTransformer(name_prefix=None)( - axl.Cooperator - )() + player = axl.strategy_transformers.FlipTransformer(name_prefix=None)(axl.Cooperator)() self.assertEqual(player.__class__.__name__, "Cooperator") self.assert_original_equals_pickled(player) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index f5f621c55..847419035 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -32,11 +32,7 @@ def setUpClass(cls): [3 / 2 for _ in range(3)], ] cls.expected_boxplot_xticks_locations = [1, 2, 3, 4] - cls.expected_boxplot_xticks_labels = [ - "Defector", - "Tit For Tat", - "Alternator", - ] + cls.expected_boxplot_xticks_labels = ["Defector", "Tit For Tat", "Alternator"] cls.expected_lengthplot_dataset = [ [cls.turns for _ in range(3)], @@ -45,21 +41,9 @@ def setUpClass(cls): ] cls.expected_payoff_dataset = [ - [ - 0, - mean([9 / 5 for _ in range(3)]), - mean([17 / 5 for _ in range(3)]), - ], - [ - mean([4 / 5 for _ in range(3)]), - 0, - mean([13 / 5 for _ in range(3)]), - ], - [ - mean([2 / 5 for _ in range(3)]), - mean([13 / 5 for _ in range(3)]), - 0, - ], + [0, mean([9 / 5 for _ in range(3)]), mean([17 / 5 for _ in range(3)])], + [mean([4 / 5 for _ in range(3)]), 0, mean([13 / 5 for _ in range(3)])], + [mean([2 / 5 for _ in range(3)]), mean([13 / 5 for _ in range(3)]), 0], ] cls.expected_winplot_dataset = ( [[2, 2, 2], [0, 0, 0], [0, 0, 0]], @@ -120,15 +104,12 @@ def test_init_from_resulsetfromfile(self): def test_boxplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual( - plot._boxplot_dataset, self.expected_boxplot_dataset - ) + self.assertSequenceEqual(plot._boxplot_dataset, self.expected_boxplot_dataset) def test_boxplot_xticks_locations(self): plot = axl.Plot(self.test_result_set) self.assertEqual( - plot._boxplot_xticks_locations, - self.expected_boxplot_xticks_locations, + plot._boxplot_xticks_locations, self.expected_boxplot_xticks_locations ) def test_boxplot_xticks_labels(self): @@ -164,9 +145,7 @@ def test_boxplot_with_title(self): def test_winplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual( - plot._winplot_dataset, self.expected_winplot_dataset - ) + self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) def test_winplot(self): plot = axl.Plot(self.test_result_set) @@ -176,9 +155,7 @@ def test_winplot(self): def test_sdvplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual( - plot._sdv_plot_dataset, self.expected_sdvplot_dataset - ) + self.assertSequenceEqual(plot._sdv_plot_dataset, self.expected_sdvplot_dataset) def test_sdvplot(self): plot = axl.Plot(self.test_result_set) @@ -188,9 +165,7 @@ def test_sdvplot(self): def test_lengthplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual( - plot._winplot_dataset, self.expected_winplot_dataset - ) + self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) def test_lengthplot(self): plot = axl.Plot(self.test_result_set) @@ -206,9 +181,7 @@ def test_pdplot(self): def test_payoff_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual( - plot._payoff_dataset, self.expected_payoff_dataset - ) + self.assertSequenceEqual(plot._payoff_dataset, self.expected_payoff_dataset) def test_payoff(self): plot = axl.Plot(self.test_result_set) @@ -277,8 +250,6 @@ def test_all_plots(self): ) self.assertIsNone( plot.save_all_plots( - prefix="test_outputs/", - title_prefix="A prefix", - progress_bar=True, + prefix="test_outputs/", title_prefix="A prefix", progress_bar=True ) ) diff --git a/axelrod/tests/unit/test_property.py b/axelrod/tests/unit/test_property.py index 945aae5be..ae992a035 100644 --- a/axelrod/tests/unit/test_property.py +++ b/axelrod/tests/unit/test_property.py @@ -12,9 +12,7 @@ ) from hypothesis import given, settings -stochastic_strategies = [ - s for s in axl.strategies if axl.Classifiers["stochastic"](s()) -] +stochastic_strategies = [s for s in axl.strategies if axl.Classifiers["stochastic"](s())] class TestStrategyList(unittest.TestCase): @@ -132,11 +130,7 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given( - tournament=prob_end_tournaments( - strategies=axl.basic_strategies, max_size=3 - ) - ) + @given(tournament=prob_end_tournaments(strategies=axl.basic_strategies, max_size=3)) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axl.Tournament) @@ -171,11 +165,7 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given( - tournament=spatial_tournaments( - strategies=axl.basic_strategies, max_size=3 - ) - ) + @given(tournament=spatial_tournaments(strategies=axl.basic_strategies, max_size=3)) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axl.Tournament) diff --git a/axelrod/tests/unit/test_resultset.py b/axelrod/tests/unit/test_resultset.py index bb0aa2091..97563d3e9 100644 --- a/axelrod/tests/unit/test_resultset.py +++ b/axelrod/tests/unit/test_resultset.py @@ -71,39 +71,15 @@ def setUpClass(cls): # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ - [ - 0, - mean([13 / 5 for _ in range(3)]), - mean([2 / 5 for _ in range(3)]), - ], - [ - mean([13 / 5 for _ in range(3)]), - 0, - mean([4 / 5 for _ in range(3)]), - ], - [ - mean([17 / 5 for _ in range(3)]), - mean([9 / 5 for _ in range(3)]), - 0, - ], + [0, mean([13 / 5 for _ in range(3)]), mean([2 / 5 for _ in range(3)])], + [mean([13 / 5 for _ in range(3)]), 0, mean([4 / 5 for _ in range(3)])], + [mean([17 / 5 for _ in range(3)]), mean([9 / 5 for _ in range(3)]), 0], ] cls.expected_payoff_stddevs = [ - [ - 0, - std([13 / 5 for _ in range(3)]), - std([2 / 5 for _ in range(3)]), - ], - [ - std([13 / 5 for _ in range(3)]), - 0, - std([4 / 5 for _ in range(3)]), - ], - [ - std([17 / 5 for _ in range(3)]), - std([9 / 5 for _ in range(3)]), - 0, - ], + [0, std([13 / 5 for _ in range(3)]), std([2 / 5 for _ in range(3)])], + [std([13 / 5 for _ in range(3)]), 0, std([4 / 5 for _ in range(3)])], + [std([17 / 5 for _ in range(3)]), std([9 / 5 for _ in range(3)]), 0], ] cls.expected_cooperation = [[0, 9, 9], [9, 0, 3], [0, 0, 0]] @@ -112,16 +88,8 @@ def setUpClass(cls): cls.expected_initial_cooperation_rate = [1, 1, 0] cls.expected_normalised_cooperation = [ - [ - 0, - mean([3 / 5 for _ in range(3)]), - mean([3 / 5 for _ in range(3)]), - ], - [ - mean([3 / 5 for _ in range(3)]), - 0, - mean([1 / 5 for _ in range(3)]), - ], + [0, mean([3 / 5 for _ in range(3)]), mean([3 / 5 for _ in range(3)])], + [mean([3 / 5 for _ in range(3)]), 0, mean([1 / 5 for _ in range(3)])], [0, 0, 0], ] @@ -208,11 +176,7 @@ def setUpClass(cls): cls.expected_good_partner_rating = [1.0, 1.0, 0] - cls.expected_eigenjesus_rating = [ - 0.5547001962252291, - 0.8320502943378436, - 0.0, - ] + cls.expected_eigenjesus_rating = [0.5547001962252291, 0.8320502943378436, 0.0] cls.expected_eigenmoses_rating = [ -0.4578520302117101, @@ -373,9 +337,7 @@ def test_score_diffs(self): for i, row in enumerate(rs.score_diffs): for j, col in enumerate(row): for k, score in enumerate(col): - self.assertAlmostEqual( - score, self.expected_score_diffs[i][j][k] - ) + self.assertAlmostEqual(score, self.expected_score_diffs[i][j][k]) def test_payoff_diffs_means(self): rs = axl.ResultSet( @@ -385,9 +347,7 @@ def test_payoff_diffs_means(self): self.assertEqual(len(rs.payoff_diffs_means), rs.num_players) for i, row in enumerate(rs.payoff_diffs_means): for j, col in enumerate(row): - self.assertAlmostEqual( - col, self.expected_payoff_diffs_means[i][j] - ) + self.assertAlmostEqual(col, self.expected_payoff_diffs_means[i][j]) def test_payoff_stddevs(self): rs = axl.ResultSet( @@ -412,8 +372,7 @@ def test_initial_cooperation_count(self): self.assertIsInstance(rs.initial_cooperation_count, list) self.assertEqual(len(rs.initial_cooperation_count), rs.num_players) self.assertEqual( - rs.initial_cooperation_count, - self.expected_initial_cooperation_count, + rs.initial_cooperation_count, self.expected_initial_cooperation_count ) def test_normalised_cooperation(self): @@ -424,9 +383,7 @@ def test_normalised_cooperation(self): self.assertEqual(len(rs.normalised_cooperation), rs.num_players) for i, row in enumerate(rs.normalised_cooperation): for j, col in enumerate(row): - self.assertAlmostEqual( - col, self.expected_normalised_cooperation[i][j] - ) + self.assertAlmostEqual(col, self.expected_normalised_cooperation[i][j]) def test_initial_cooperation_rate(self): rs = axl.ResultSet( @@ -444,9 +401,7 @@ def test_state_distribution(self): ) self.assertIsInstance(rs.state_distribution, list) self.assertEqual(len(rs.state_distribution), rs.num_players) - self.assertEqual( - rs.state_distribution, self.expected_state_distribution - ) + self.assertEqual(rs.state_distribution, self.expected_state_distribution) def test_state_normalised_distribution(self): rs = axl.ResultSet( @@ -491,9 +446,7 @@ def test_vengeful_cooperation(self): self.assertEqual(len(rs.vengeful_cooperation), rs.num_players) for i, row in enumerate(rs.vengeful_cooperation): for j, col in enumerate(row): - self.assertAlmostEqual( - col, self.expected_vengeful_cooperation[i][j] - ) + self.assertAlmostEqual(col, self.expected_vengeful_cooperation[i][j]) def test_cooperating_rating(self): rs = axl.ResultSet( @@ -501,9 +454,7 @@ def test_cooperating_rating(self): ) self.assertIsInstance(rs.cooperating_rating, list) self.assertEqual(len(rs.cooperating_rating), rs.num_players) - self.assertEqual( - rs.cooperating_rating, self.expected_cooperating_rating - ) + self.assertEqual(rs.cooperating_rating, self.expected_cooperating_rating) def test_good_partner_matrix(self): rs = axl.ResultSet( @@ -511,9 +462,7 @@ def test_good_partner_matrix(self): ) self.assertIsInstance(rs.good_partner_matrix, list) self.assertEqual(len(rs.good_partner_matrix), rs.num_players) - self.assertEqual( - rs.good_partner_matrix, self.expected_good_partner_matrix - ) + self.assertEqual(rs.good_partner_matrix, self.expected_good_partner_matrix) def test_good_partner_rating(self): rs = axl.ResultSet( @@ -521,9 +470,7 @@ def test_good_partner_rating(self): ) self.assertIsInstance(rs.good_partner_rating, list) self.assertEqual(len(rs.good_partner_rating), rs.num_players) - self.assertEqual( - rs.good_partner_rating, self.expected_good_partner_rating - ) + self.assertEqual(rs.good_partner_rating, self.expected_good_partner_rating) def test_eigenjesus_rating(self): rs = axl.ResultSet( @@ -555,10 +502,7 @@ def test_self_interaction_for_random_strategies(self): def test_equality(self): rs_sets = [ axl.ResultSet( - self.filename, - self.players, - self.repetitions, - progress_bar=False, + self.filename, self.players, self.repetitions, progress_bar=False ) for _ in range(2) ] @@ -588,34 +532,25 @@ def test_summarise(self): [float(player.Median_score) for player in sd], ranked_median_scores ) - ranked_cooperation_rating = [ - rs.cooperating_rating[i] for i in rs.ranking - ] + ranked_cooperation_rating = [rs.cooperating_rating[i] for i in rs.ranking] self.assertEqual( [float(player.Cooperation_rating) for player in sd], ranked_cooperation_rating, ) ranked_median_wins = [nanmedian(rs.wins[i]) for i in rs.ranking] - self.assertEqual( - [float(player.Wins) for player in sd], ranked_median_wins - ) + self.assertEqual([float(player.Wins) for player in sd], ranked_median_wins) ranked_initial_coop_rates = [ self.expected_initial_cooperation_rate[i] for i in rs.ranking ] self.assertEqual( - [float(player.Initial_C_rate) for player in sd], - ranked_initial_coop_rates, + [float(player.Initial_C_rate) for player in sd], ranked_initial_coop_rates ) for player in sd: self.assertEqual( - player.CC_rate - + player.CD_rate - + player.DC_rate - + player.DD_rate, - 1, + player.CC_rate + player.CD_rate + player.DC_rate + player.DD_rate, 1 ) for rate in [ player.CC_to_C_rate, @@ -792,21 +727,13 @@ def setUpClass(cls): # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ - [ - 0, - mean([13 / 5 for _ in range(3)]), - mean([2 / 5 for _ in range(3)]), - ], + [0, mean([13 / 5 for _ in range(3)]), mean([2 / 5 for _ in range(3)])], [mean([13 / 5 for _ in range(3)]), 0, 0], [mean([17 / 5 for _ in range(3)]), 0, 0], ] cls.expected_payoff_stddevs = [ - [ - 0, - std([13 / 5 for _ in range(3)]), - std([2 / 5 for _ in range(3)]), - ], + [0, std([13 / 5 for _ in range(3)]), std([2 / 5 for _ in range(3)])], [std([13 / 5 for _ in range(3)]), 0, 0], [std([17 / 5 for _ in range(3)]), 0, 0], ] @@ -814,11 +741,7 @@ def setUpClass(cls): cls.expected_cooperation = [[0, 9, 9], [9, 0, 0], [0, 0, 0]] cls.expected_normalised_cooperation = [ - [ - 0, - mean([3 / 5 for _ in range(3)]), - mean([3 / 5 for _ in range(3)]), - ], + [0, mean([3 / 5 for _ in range(3)]), mean([3 / 5 for _ in range(3)])], [mean([3 / 5 for _ in range(3)]), 0, 0], [0, 0, 0], ] @@ -837,11 +760,7 @@ def setUpClass(cls): cls.expected_good_partner_rating = [1.0, 1.0, 0.0] - cls.expected_eigenjesus_rating = [ - 0.447213595499958, - 0.894427190999916, - 0.0, - ] + cls.expected_eigenjesus_rating = [0.447213595499958, 0.894427190999916, 0.0] cls.expected_eigenmoses_rating = [ -0.32929277996907086, @@ -865,11 +784,7 @@ def setUpClass(cls): Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), Counter({(C, D): 0.6, (D, D): 0.4}), ], - [ - Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), - Counter(), - Counter(), - ], + [Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), Counter(), Counter()], [Counter({(D, C): 0.6, (D, D): 0.4}), Counter(), Counter()], ] @@ -898,11 +813,7 @@ def setUpClass(cls): Counter(), Counter(), ], - [ - Counter({((D, C), D): 1.0, ((D, D), D): 1.0}), - Counter(), - Counter(), - ], + [Counter({((D, C), D): 1.0, ((D, D), D): 1.0}), Counter(), Counter()], ] def test_match_lengths(self): @@ -959,8 +870,7 @@ def setUpClass(cls): cls.edges = [(0, 1), (2, 3)] cls.expected_match_lengths = [ - [[0, 5, 0, 0], [5, 0, 0, 0], [0, 0, 0, 5], [0, 0, 5, 0]] - for _ in range(3) + [[0, 5, 0, 0], [5, 0, 0, 0], [0, 0, 0, 5], [0, 0, 5, 0]] for _ in range(3) ] cls.expected_scores = [ @@ -1002,30 +912,10 @@ def setUpClass(cls): ] cls.expected_score_diffs = [ - [ - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - ], - [ - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - ], - [ - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [5.0, 5.0, 5.0], - ], - [ - [0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [-5.0, -5.0, -5.0], - [0.0, 0.0, 0.0], - ], + [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], + [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], + [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [5.0, 5.0, 5.0]], + [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [-5.0, -5.0, -5.0], [0.0, 0.0, 0.0]], ] cls.expected_payoff_diffs_means = [ @@ -1182,17 +1072,14 @@ def setUpClass(cls): cls.edges = [(0, 0), (1, 1), (2, 2), (3, 3)] cls.expected_match_lengths = [ - [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]] - for _ in range(3) + [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]] for _ in range(3) ] cls.expected_scores = [[0 for _ in range(3)] for _ in range(4)] cls.expected_wins = [[0 for _ in range(3)] for _ in range(4)] - cls.expected_normalised_scores = [ - [0 for _ in range(3)] for i in range(4) - ] + cls.expected_normalised_scores = [[0 for _ in range(3)] for i in range(4)] cls.expected_ranking = [0, 1, 2, 3] @@ -1220,9 +1107,7 @@ def setUpClass(cls): [[0.0 for _ in range(3)] for _ in range(4)] for _ in range(4) ] - cls.expected_payoff_diffs_means = [ - [0.0 for _ in range(4)] for _ in range(4) - ] + cls.expected_payoff_diffs_means = [[0.0 for _ in range(4)] for _ in range(4)] # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ @@ -1263,9 +1148,7 @@ def setUpClass(cls): cls.expected_cooperating_rating = [0.0 for _ in range(4)] - cls.expected_good_partner_matrix = [ - [0.0 for _ in range(4)] for _ in range(4) - ] + cls.expected_good_partner_matrix = [[0.0 for _ in range(4)] for _ in range(4)] cls.expected_good_partner_rating = [0.0 for _ in range(4)] @@ -1333,9 +1216,7 @@ class TestSummary(unittest.TestCase): """Separate test to check that summary always builds without failures""" @given( - tournament=tournaments( - min_size=2, max_size=5, max_turns=5, max_repetitions=3 - ) + tournament=tournaments(min_size=2, max_size=5, max_turns=5, max_repetitions=3) ) @settings(max_examples=5, deadline=None) def test_summarise_without_failure(self, tournament): @@ -1346,11 +1227,7 @@ def test_summarise_without_failure(self, tournament): for player in sd: # round for numerical error total_rate = round( - player.CC_rate - + player.CD_rate - + player.DC_rate - + player.DD_rate, - 3, + player.CC_rate + player.CD_rate + player.DC_rate + player.DD_rate, 3 ) self.assertTrue(total_rate in [0, 1]) self.assertTrue(0 <= player.Initial_C_rate <= 1) @@ -1362,13 +1239,9 @@ class TestCreateCounterDict(unittest.TestCase): def test_basic_use(self): key_map = {"Col 1": "Var 1", "Col 2": "Var 2"} df = pd.DataFrame( - {"Col 1": [10, 20, 30], "Col 2": [1, 2, 0]}, - index=[[5, 6, 7], [1, 2, 3]], - ) - self.assertEqual( - create_counter_dict(df, 6, 2, key_map), - Counter({"Var 1": 20, "Var 2": 2}), + {"Col 1": [10, 20, 30], "Col 2": [1, 2, 0]}, index=[[5, 6, 7], [1, 2, 3]] ) self.assertEqual( - create_counter_dict(df, 7, 3, key_map), Counter({"Var 1": 30}) + create_counter_dict(df, 6, 2, key_map), Counter({"Var 1": 20, "Var 2": 2}) ) + self.assertEqual(create_counter_dict(df, 7, 3, key_map), Counter({"Var 1": 30})) diff --git a/axelrod/tests/unit/test_strategy_transformers.py b/axelrod/tests/unit/test_strategy_transformers.py index ba244d2fe..7539ae8cd 100644 --- a/axelrod/tests/unit/test_strategy_transformers.py +++ b/axelrod/tests/unit/test_strategy_transformers.py @@ -20,7 +20,6 @@ class CanNotPickle(axl.Cooperator): class TestTransformers(TestMatch): """Test generic transformer properties.""" - def test_player_can_be_pickled(self): player = axl.Cooperator() self.assertTrue(player_can_be_pickled(player)) @@ -56,13 +55,9 @@ def test_DecoratorReBuilder(self): args = decorator.args kwargs = decorator.kwargs.copy() - new_decorator = DecoratorReBuilder()( - factory_args, args, kwargs, new_prefix - ) + new_decorator = DecoratorReBuilder()(factory_args, args, kwargs, new_prefix) - self.assertEqual( - decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)() - ) + self.assertEqual(decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)()) def test_StrategyReBuilder_declared_class_with_name_prefix(self): player = CanNotPickle() @@ -156,15 +151,9 @@ def test_repr(self): str(InitialTransformer([D, D, C])(axl.Alternator)()), "Initial Alternator: [D, D, C]", ) + self.assertEqual(str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1") self.assertEqual( - str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1" - ) - self.assertEqual( - str( - MixedTransformer(0.3, (axl.Alternator, axl.Bully))(axl.Random)( - 0.1 - ) - ), + str(MixedTransformer(0.3, (axl.Alternator, axl.Bully))(axl.Random)(0.1)), "Mutated Random: 0.1: 0.3, ['Alternator', 'Bully']", ) @@ -192,9 +181,7 @@ def test_composition(self): p2 = axl.Cooperator() self.versus_test(p1, p2, [D, D, C, C, C, C, D, D], [C] * 8) - cls1 = FinalTransformer([D, D])( - InitialTransformer([D, D])(axl.Cooperator) - ) + cls1 = FinalTransformer([D, D])(InitialTransformer([D, D])(axl.Cooperator)) p1 = cls1() p2 = axl.Cooperator() self.versus_test(p1, p2, [D, D, C, C, C, C, D, D], [C] * 8) @@ -221,11 +208,9 @@ def deterministic_reclassifier(original_classifier, *args): return original_classifier StochasticTransformer = StrategyTransformerFactory( - generic_strategy_wrapper, reclassifier=stochastic_reclassifier - ) + generic_strategy_wrapper, reclassifier=stochastic_reclassifier) DeterministicTransformer = StrategyTransformerFactory( - generic_strategy_wrapper, reclassifier=deterministic_reclassifier - ) + generic_strategy_wrapper, reclassifier=deterministic_reclassifier) # Cooperator is not stochastic self.assertFalse(axl.Cooperator().classifier["stochastic"]) @@ -234,16 +219,12 @@ def deterministic_reclassifier(original_classifier, *args): self.assertTrue(player.classifier["stochastic"]) # Composing transforms should return it to not being stochastic - cls1 = compose_transformers( - DeterministicTransformer(), StochasticTransformer() - ) + cls1 = compose_transformers(DeterministicTransformer(), StochasticTransformer()) player = cls1(axl.Cooperator)() self.assertFalse(player.classifier["stochastic"]) # Explicit composition - player = DeterministicTransformer()( - StochasticTransformer()(axl.Cooperator) - )() + player = DeterministicTransformer()(StochasticTransformer()(axl.Cooperator))() self.assertFalse(player.classifier["stochastic"]) # Random is stochastic @@ -254,16 +235,12 @@ def deterministic_reclassifier(original_classifier, *args): self.assertFalse(player.classifier["stochastic"]) # Composing transforms should return it to being stochastic - cls1 = compose_transformers( - StochasticTransformer(), DeterministicTransformer() - ) + cls1 = compose_transformers(StochasticTransformer(), DeterministicTransformer()) player = cls1(axl.Random)() self.assertTrue(player.classifier["stochastic"]) # Explicit composition - player = StochasticTransformer()( - DeterministicTransformer()(axl.Random) - )() + player = StochasticTransformer()(DeterministicTransformer()(axl.Random))() self.assertTrue(player.classifier["stochastic"]) def test_nilpotency(self): @@ -339,20 +316,16 @@ def test_deadlock_breaks(self): axl.TitForTat(), InitialTransformer([D])(axl.TitForTat)(), [C, D, C, D], - [D, C, D, C], - ) + [D, C, D, C]) # Now let's use the transformer to break the deadlock to achieve # Mutual cooperation # self.versus_test( self.versus_test( axl.TitForTat(), - DeadlockBreakingTransformer()( - InitialTransformer([D])(axl.TitForTat) - )(), + DeadlockBreakingTransformer()(InitialTransformer([D])(axl.TitForTat))(), [C, D, C, C], - [D, C, C, C], - ) + [D, C, C, C]) class TestDualTransformer(TestMatch): @@ -404,12 +377,10 @@ def test_dual_transformer_simple_play_regression_test(self): DualTransformer()(axl.Cooperator) )() - self.versus_test( - multiple_dual_transformers, - dual_transformer_not_first, - [D, D, D], - [D, D, D], - ) + self.versus_test(multiple_dual_transformers, + dual_transformer_not_first, + [D, D, D], + [D, D, D]) def test_dual_transformer_multiple_interspersed_regression_test(self): """DualTransformer has failed when there were multiple DualTransformers. @@ -435,9 +406,7 @@ def test_final_transformer(self): p2 = FinalTransformer([D, D, D])(axl.Cooperator)() self.assertEqual(axl.Classifiers["makes_use_of"](p2), set(["length"])) self.assertEqual(axl.Classifiers["memory_depth"](p2), 3) - self.assertEqual( - axl.Classifiers["makes_use_of"](axl.Cooperator()), set([]) - ) + self.assertEqual(axl.Classifiers["makes_use_of"](axl.Cooperator()), set([])) self.versus_test(p1, p2, [C] * 8, [C, C, C, C, C, D, D, D], turns=8) def test_infinite_memory_depth_transformed(self): @@ -450,9 +419,8 @@ def test_final_transformer_unknown_length(self): """Tests the FinalTransformer when tournament length is not known.""" p1 = axl.Defector() p2 = FinalTransformer([D, D])(axl.Cooperator)() - self.versus_test( - p1, p2, [D] * 6, [C] * 6, match_attributes={"length": -1} - ) + self.versus_test(p1, p2, [D] * 6, [C] * 6, + match_attributes={"length": -1}) class TestFlipTransformer(TestMatch): @@ -481,9 +449,8 @@ def test_forgiving_transformer(self): p1 = ForgiverTransformer(0.5)(axl.Alternator)() p2 = axl.Defector() turns = 10 - self.versus_test( - p1, p2, [C, D, C, C, D, C, C, D, C, D], [D] * turns, seed=8 - ) + self.versus_test(p1, p2, [C, D, C, C, D, C, C, D, C, D], [D] * turns, + seed=8) def test_stochastic_values_classifier(self): p1 = ForgiverTransformer(0.5)(axl.Alternator)() @@ -506,9 +473,7 @@ def test_grudging1(self): def test_grudging2(self): p1 = InitialTransformer([C])(axl.Defector)() p2 = GrudgeTransformer(2)(axl.Cooperator)() - self.versus_test( - p1, p2, [C, D, D, D, D, D, D, D], [C, C, C, C, D, D, D, D], seed=11 - ) + self.versus_test(p1, p2, [C, D, D, D, D, D, D, D], [C, C, C, C, D, D, D, D], seed=11) class TestHistoryTrackingTransformer(TestMatch): @@ -545,12 +510,8 @@ def test_generic(self): Defector2 = transformer(axl.Defector) turns = 100 - self.versus_test( - axl.Cooperator(), Cooperator2(), [C] * turns, [C] * turns - ) - self.versus_test( - axl.Cooperator(), Defector2(), [C] * turns, [D] * turns - ) + self.versus_test(axl.Cooperator(), Cooperator2(), [C] * turns, [C] * turns) + self.versus_test(axl.Cooperator(), Defector2(), [C] * turns, [D] * turns) class TestInitialTransformer(TestMatch): @@ -723,9 +684,7 @@ def test_noisy_transformer(self): p1 = axl.Cooperator() p2 = NoisyTransformer(0.5)(axl.Cooperator)() self.assertTrue(axl.Classifiers["stochastic"](p2)) - self.versus_test( - p1, p2, [C] * 10, [D, C, C, D, C, D, C, D, D, C], seed=1 - ) + self.versus_test(p1, p2, [C] * 10, [D, C, C, D, C, D, C, D, D, C], seed=1) def test_noisy_transformation_stochastic(self): """Depending on the value of the noise parameter, the strategy may become stochastic @@ -763,9 +722,7 @@ def test_retailiating_cooperator_against_2TFT(self): TwoTitsForTat = RetaliationTransformer(2)(axl.Cooperator) p1 = TwoTitsForTat() p2 = axl.CyclerCCD() - self.versus_test( - p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D] - ) + self.versus_test(p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D]) class TestRetailiateUntilApologyTransformer(TestMatch): @@ -789,7 +746,6 @@ def test_retaliation_until_apology_stochastic(self): # Run the standard Player tests on some specifically transformed players - class TestNullInitialTransformedCooperator(TestPlayer): player = InitialTransformer([])(axl.Cooperator) name = "Initial Cooperator: []" @@ -830,9 +786,7 @@ class TestFinalTransformedCooperator(TestPlayer): class TestInitialFinalTransformedCooperator(TestPlayer): - player = InitialTransformer([D, D])( - FinalTransformer([D, D, D])(axl.Cooperator) - ) + player = InitialTransformer([D, D])(FinalTransformer([D, D, D])(axl.Cooperator)) name = "Initial Final Cooperator: [D, D, D]: [D, D]" expected_classifier = { "memory_depth": 3, @@ -845,9 +799,7 @@ class TestInitialFinalTransformedCooperator(TestPlayer): class TestFinalInitialTransformedCooperator(TestPlayer): - player = FinalTransformer([D, D])( - InitialTransformer([D, D, D])(axl.Cooperator) - ) + player = FinalTransformer([D, D])(InitialTransformer([D, D, D])(axl.Cooperator)) name = "Final Initial Cooperator: [D, D, D]: [D, D]" expected_classifier = { "memory_depth": 3, @@ -959,9 +911,7 @@ class TestMixed0(TestDefector): class TestMixed1(TestDefector): - name = ( - "Mutated Cooperator: 1, " - ) + name = "Mutated Cooperator: 1, " player = MixedTransformer(1, axl.Defector)(axl.Cooperator) expected_classifier = { "memory_depth": 0, @@ -1046,9 +996,7 @@ class TestJossAnnDual(TestPlayer): class TestJossAnnOverwriteClassifier(TestPlayer): name = "Joss-Ann Final Random: 0.5: [D, D]: (1.0, 0.0)" - player = JossAnnTransformer((1.0, 0.0))( - FinalTransformer([D, D])(axl.Random) - ) + player = JossAnnTransformer((1., 0.))(FinalTransformer([D, D])(axl.Random)) expected_classifier = { "memory_depth": 0, "stochastic": False, diff --git a/axelrod/tests/unit/test_strategy_utils.py b/axelrod/tests/unit/test_strategy_utils.py index 5cade0dba..b3a4f7ef0 100644 --- a/axelrod/tests/unit/test_strategy_utils.py +++ b/axelrod/tests/unit/test_strategy_utils.py @@ -35,9 +35,7 @@ def test_no_cycle(self): history = [D, D, C, C, C] self.assertIsNone(detect_cycle(history)) - def test_regression_test_can_detect_cycle_that_is_repeated_exactly_once( - self, - ): + def test_regression_test_can_detect_cycle_that_is_repeated_exactly_once(self): self.assertEqual(detect_cycle([C, D, C, D]), (C, D)) self.assertEqual(detect_cycle([C, D, C, D, C]), (C, D)) @@ -55,14 +53,11 @@ def test_min_size_greater_than_two_times_history_tail_returns_none(self): def test_min_size_greater_than_two_times_max_size_has_no_effect(self): self.assertEqual( - detect_cycle([C, C, C, C, C, C, C, C], min_size=2, max_size=3), - (C, C), + detect_cycle([C, C, C, C, C, C, C, C], min_size=2, max_size=3), (C, C) ) def test_cycle_greater_than_max_size_returns_none(self): - self.assertEqual( - detect_cycle([C, C, D] * 2, min_size=1, max_size=3), (C, C, D) - ) + self.assertEqual(detect_cycle([C, C, D] * 2, min_size=1, max_size=3), (C, C, D)) self.assertIsNone(detect_cycle([C, C, D] * 2, min_size=1, max_size=2)) @@ -86,9 +81,7 @@ def test_strategies_with_countermeasures_return_their_countermeasures(self): inspector = axl.Cooperator() match = axl.Match((d_geller, inspector), turns=1) match.play() - self.assertEqual( - inspect_strategy(inspector=inspector, opponent=d_geller), D - ) + self.assertEqual(inspect_strategy(inspector=inspector, opponent=d_geller), D) self.assertEqual(d_geller.strategy(inspector), C) diff --git a/axelrod/tests/unit/test_tournament.py b/axelrod/tests/unit/test_tournament.py index e73003398..f8cca4bb4 100644 --- a/axelrod/tests/unit/test_tournament.py +++ b/axelrod/tests/unit/test_tournament.py @@ -41,9 +41,7 @@ test_edges = [(0, 1), (1, 2), (3, 4)] deterministic_strategies = [ - s - for s in axl.short_run_time_strategies - if not axl.Classifiers["stochastic"](s()) + s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s()) ] @@ -109,9 +107,7 @@ def test_init(self): noise=0.2, ) self.assertEqual(len(tournament.players), len(test_strategies)) - self.assertIsInstance( - tournament.players[0].match_attributes["game"], axl.Game - ) + self.assertIsInstance(tournament.players[0].match_attributes["game"], axl.Game) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertEqual(tournament.turns, self.test_turns) self.assertEqual(tournament.repetitions, 10) @@ -127,9 +123,7 @@ def test_init_with_match_attributes(self): ) mg = tournament.match_generator match_params = mg.build_single_match_params() - self.assertEqual( - match_params["match_attributes"], {"length": float("inf")} - ) + self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) def test_warning(self): tournament = axl.Tournament( @@ -281,11 +275,7 @@ def test_serial_play_with_different_game(self): # Test that a non default game is passed to the result set game = axl.Game(p=-1, r=-1, s=-1, t=-1) tournament = axl.Tournament( - name=self.test_name, - players=self.players, - game=game, - turns=1, - repetitions=1, + name=self.test_name, players=self.players, game=game, turns=1, repetitions=1 ) results = tournament.play(progress_bar=False) self.assertLessEqual(np.max(results.scores), 0) @@ -419,9 +409,7 @@ def test_progress_bar_play_parallel(self): # these two examples were identified by hypothesis. @example( tournament=axl.Tournament( - players=[axl.BackStabber(), axl.MindReader()], - turns=2, - repetitions=1, + players=[axl.BackStabber(), axl.MindReader()], turns=2, repetitions=1, ) ) @example( @@ -553,9 +541,7 @@ def test_n_workers(self): tournament._n_workers(processes=max_processes + 2), max_processes ) - @unittest.skipIf( - cpu_count() < 2, "not supported on single processor machines" - ) + @unittest.skipIf(cpu_count() < 2, "not supported on single processor machines") def test_2_workers(self): # This is a separate test with a skip condition because we # cannot guarantee that the tests will always run on a machine @@ -739,13 +725,9 @@ def test_write_to_csv_without_results(self): turns=2, repetitions=2, ) - tournament.play( - filename=self.filename, progress_bar=False, build_results=False - ) + tournament.play(filename=self.filename, progress_bar=False, build_results=False) df = pd.read_csv(self.filename) - path = pathlib.Path( - "test_outputs/expected_test_tournament_no_results.csv" - ) + path = pathlib.Path("test_outputs/expected_test_tournament_no_results.csv") expected_df = pd.read_csv(axl_filename(path)) self.assertTrue(df.equals(expected_df)) @@ -765,7 +747,7 @@ def test_seeding_equality(self, seed): game=self.game, turns=10, repetitions=100, - seed=seed, + seed=seed ) tournament2 = axl.Tournament( name=self.test_name, @@ -773,7 +755,7 @@ def test_seeding_equality(self, seed): game=self.game, turns=10, repetitions=100, - seed=seed, + seed=seed ) for _ in range(4): results1 = tournament1.play(processes=2) @@ -788,7 +770,7 @@ def test_seeding_inequality(self): game=self.game, turns=2, repetitions=2, - seed=0, + seed=0 ) tournament2 = axl.Tournament( name=self.test_name, @@ -796,7 +778,7 @@ def test_seeding_inequality(self): game=self.game, turns=2, repetitions=2, - seed=10, + seed=10 ) results1 = tournament1.play() results2 = tournament2.play() @@ -820,9 +802,7 @@ def test_init(self): prob_end=self.test_prob_end, noise=0.2, ) - self.assertEqual( - tournament.match_generator.prob_end, tournament.prob_end - ) + self.assertEqual(tournament.match_generator.prob_end, tournament.prob_end) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertIsNone(tournament.turns) @@ -841,7 +821,7 @@ def test_init(self): max_prob_end=0.9, min_repetitions=2, max_repetitions=4, - seed=100, + seed=100 ) ) @settings(max_examples=5, deadline=None) @@ -850,7 +830,7 @@ def test_init(self): players=[s() for s in test_strategies], prob_end=0.2, repetitions=test_repetitions, - seed=101, + seed=101 ) ) # These two examples are to make sure #465 is fixed. @@ -861,7 +841,7 @@ def test_init(self): players=[axl.BackStabber(), axl.MindReader()], prob_end=0.2, repetitions=1, - seed=102, + seed=102 ) ) @example( @@ -869,7 +849,7 @@ def test_init(self): players=[axl.ThueMorse(), axl.MindReader()], prob_end=0.2, repetitions=1, - seed=103, + seed=103 ) ) def test_property_serial_play(self, tournament): @@ -922,9 +902,7 @@ def test_init(self): seed=integers(min_value=0, max_value=4294967295), ) @settings(max_examples=5, deadline=None) - def test_complete_tournament( - self, strategies, turns, repetitions, noise, seed - ): + def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): """ A test to check that a spatial tournament on the complete multigraph gives the same results as the round robin. @@ -939,20 +917,13 @@ def test_complete_tournament( # create a round robin tournament tournament = axl.Tournament( - players, - repetitions=repetitions, - turns=turns, - noise=noise, - seed=seed, + players, repetitions=repetitions, turns=turns, noise=noise, + seed=seed ) # create a complete spatial tournament spatial_tournament = axl.Tournament( - players, - repetitions=repetitions, - turns=turns, - noise=noise, - edges=edges, - seed=seed, + players, repetitions=repetitions, turns=turns, noise=noise, edges=edges, + seed=seed ) results = tournament.play(progress_bar=False) @@ -961,23 +932,16 @@ def test_complete_tournament( self.assertEqual(results.ranked_names, spatial_results.ranked_names) self.assertEqual(results.num_players, spatial_results.num_players) self.assertEqual(results.repetitions, spatial_results.repetitions) - self.assertEqual( - results.payoff_diffs_means, spatial_results.payoff_diffs_means - ) + self.assertEqual(results.payoff_diffs_means, spatial_results.payoff_diffs_means) self.assertEqual(results.payoff_matrix, spatial_results.payoff_matrix) self.assertEqual(results.payoff_stddevs, spatial_results.payoff_stddevs) self.assertEqual(results.payoffs, spatial_results.payoffs) - self.assertEqual( - results.cooperating_rating, spatial_results.cooperating_rating - ) + self.assertEqual(results.cooperating_rating, spatial_results.cooperating_rating) self.assertEqual(results.cooperation, spatial_results.cooperation) self.assertEqual( - results.normalised_cooperation, - spatial_results.normalised_cooperation, - ) - self.assertEqual( - results.normalised_scores, spatial_results.normalised_scores + results.normalised_cooperation, spatial_results.normalised_cooperation ) + self.assertEqual(results.normalised_scores, spatial_results.normalised_scores) self.assertEqual( results.good_partner_matrix, spatial_results.good_partner_matrix ) @@ -997,12 +961,7 @@ def test_particular_tournament(self): edges = [(0, 2), (0, 3), (1, 2), (1, 3)] tournament = axl.Tournament(players, edges=edges) results = tournament.play(progress_bar=False) - expected_ranked_names = [ - "Cooperator", - "Tit For Tat", - "Grudger", - "Defector", - ] + expected_ranked_names = ["Cooperator", "Tit For Tat", "Grudger", "Defector"] self.assertEqual(results.ranked_names, expected_ranked_names) # Check that this tournament runs with noise @@ -1059,19 +1018,17 @@ def test_complete_tournament(self, strategies, prob_end, seed, reps): # create a prob end round robin tournament - tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=reps, seed=seed - ) + tournament = axl.Tournament(players, prob_end=prob_end, repetitions=reps, + seed=seed) results = tournament.play(progress_bar=False) # create a complete spatial tournament # edges - edges = [ - (i, j) for i in range(len(players)) for j in range(i, len(players)) - ] + edges = [(i, j) for i in range(len(players)) for j in range(i, len(players))] spatial_tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=reps, edges=edges, seed=seed + players, prob_end=prob_end, repetitions=reps, edges=edges, + seed=seed ) spatial_results = spatial_tournament.play(progress_bar=False) self.assertEqual(results.match_lengths, spatial_results.match_lengths) @@ -1106,9 +1063,7 @@ def test_one_turn_tournament(self, tournament, seed): one_turn_results = tournament.play(progress_bar=False) self.assertEqual(prob_end_results.scores, one_turn_results.scores) self.assertEqual(prob_end_results.wins, one_turn_results.wins) - self.assertEqual( - prob_end_results.cooperation, one_turn_results.cooperation - ) + self.assertEqual(prob_end_results.cooperation, one_turn_results.cooperation) class TestHelperFunctions(unittest.TestCase): diff --git a/axelrod/tournament.py b/axelrod/tournament.py index 41d7d45f0..925fe1477 100644 --- a/axelrod/tournament.py +++ b/axelrod/tournament.py @@ -33,7 +33,7 @@ def __init__( noise: float = 0, edges: List[Tuple] = None, match_attributes: dict = None, - seed: int = None, + seed: int = None ) -> None: """ Parameters @@ -87,7 +87,7 @@ def __init__( noise=self.noise, edges=edges, match_attributes=match_attributes, - seed=self.seed, + seed=self.seed ) self._logger = logging.getLogger(__name__) @@ -232,9 +232,7 @@ def _get_file_objects(self, build_results=True): def _get_progress_bar(self): if self.use_progress_bar: - return tqdm.tqdm( - total=self.match_generator.size, desc="Playing matches" - ) + return tqdm.tqdm(total=self.match_generator.size, desc="Playing matches") return None def _write_interactions_to_file(self, results, writer): @@ -258,14 +256,8 @@ def _write_interactions_to_file(self, results, writer): ) = results for index, player_index in enumerate(index_pair): opponent_index = index_pair[index - 1] - row = [ - self.num_interactions, - player_index, - opponent_index, - repetition, - str(self.players[player_index]), - str(self.players[opponent_index]), - ] + row = [self.num_interactions, player_index, opponent_index, repetition, + str(self.players[player_index]), str(self.players[opponent_index])] history = actions_to_str([i[index] for i in interaction]) row.append(history) @@ -285,24 +277,16 @@ def _write_interactions_to_file(self, results, writer): for state in states: row.append(state_distribution[state]) for state in states: - row.append( - state_to_action_distributions[index][(state, C)] - ) - row.append( - state_to_action_distributions[index][(state, D)] - ) + row.append(state_to_action_distributions[index][(state, C)]) + row.append(state_to_action_distributions[index][(state, D)]) - row.append( - int(cooperations[index] >= cooperations[index - 1]) - ) + row.append(int(cooperations[index] >= cooperations[index - 1])) writer.writerow(row) repetition += 1 self.num_interactions += 1 - def _run_parallel( - self, processes: int = 2, build_results: bool = True - ) -> bool: + def _run_parallel(self, processes: int = 2, build_results: bool = True) -> bool: """ Run all matches in parallel @@ -365,8 +349,7 @@ def _start_workers( """ for worker in range(workers): process = Process( - target=self._worker, - args=(work_queue, done_queue, build_results), + target=self._worker, args=(work_queue, done_queue, build_results) ) work_queue.put("STOP") process.start() @@ -404,9 +387,7 @@ def _process_done_queue( _close_objects(out_file, progress_bar) return True - def _worker( - self, work_queue: Queue, done_queue: Queue, build_results: bool = True - ): + def _worker(self, work_queue: Queue, done_queue: Queue, build_results: bool = True): """ The work for each parallel sub-process to execute. @@ -474,17 +455,13 @@ def _calculate_results(self, interactions): turns = len(interactions) results.append(turns) - score_per_turns = iu.compute_final_score_per_turn( - interactions, self.game - ) + score_per_turns = iu.compute_final_score_per_turn(interactions, self.game) results.append(score_per_turns) score_diffs_per_turns = score_diffs[0] / turns, score_diffs[1] / turns results.append(score_diffs_per_turns) - initial_coops = tuple( - map(bool, iu.compute_cooperations(interactions[:1])) - ) + initial_coops = tuple(map(bool, iu.compute_cooperations(interactions[:1]))) results.append(initial_coops) cooperations = iu.compute_cooperations(interactions) diff --git a/docs/conf.py b/docs/conf.py index 0e33e21c3..72d0a774c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -231,13 +231,7 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ( - "index", - "Axelrod.tex", - "Axelrod Documentation", - "Vincent Knight", - "manual", - ) + ("index", "Axelrod.tex", "Axelrod Documentation", "Vincent Knight", "manual") ] # The name of an image file (relative to this directory) to place at the top of @@ -265,9 +259,7 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - ("index", "axelrod", "Axelrod Documentation", ["Vincent Knight"], 1) -] +man_pages = [("index", "axelrod", "Axelrod Documentation", ["Vincent Knight"], 1)] # If true, show URL addresses after external links. # man_show_urls = False diff --git a/doctests.py b/doctests.py index 3ac377571..8bc74f50f 100644 --- a/doctests.py +++ b/doctests.py @@ -27,8 +27,7 @@ def load_tests(loader, tests, ignore): tests.addTests( doctest.DocFileSuite( # ELLIPSIS option tells doctest to ignore portions of the verification value. - os.path.join(root, f), - optionflags=doctest.ELLIPSIS, + os.path.join(root, f), optionflags=doctest.ELLIPSIS ) ) From bbc95fbd2463ec9b8befc1b3d3c0e47bbace2278 Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 11:31:26 +0100 Subject: [PATCH 07/10] Remove unnecessary installs. --- .github/workflows/config.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/config.yml b/.github/workflows/config.yml index 0f9a5eea1..c1c0c9698 100644 --- a/.github/workflows/config.yml +++ b/.github/workflows/config.yml @@ -33,10 +33,6 @@ jobs: python -m pip install docutils==0.12 python -m pip install mock cd docs; make clean; make html; cd ..; - - name: Install testing dependencies - run: | - python -m pip install coverage - python -m pip install hypothesis==3.2 - name: Run tests run: | python -m pip install coverage From 8b99e16ce8264f7f3b05a15a5c05ebb9c8426f34 Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 12:26:17 +0100 Subject: [PATCH 08/10] Revert "Revert "Run black"" This reverts commit 10ab222b21151ac3cca703de6993334ecee74712. --- .prepare-commit-msg.py | 4 +- axelrod/evolvable_player.py | 3 +- axelrod/fingerprint.py | 40 +- axelrod/game.py | 11 +- axelrod/graph.py | 5 +- axelrod/history.py | 8 +- axelrod/interaction_utils.py | 3 +- axelrod/match.py | 5 +- axelrod/match_generator.py | 2 +- axelrod/moran.py | 46 ++- axelrod/player.py | 24 +- axelrod/plot.py | 8 +- axelrod/random_.py | 12 +- axelrod/result_set.py | 71 +++- axelrod/strategies/__init__.py | 4 +- axelrod/strategies/_filters.py | 18 +- axelrod/strategies/adaptor.py | 20 +- axelrod/strategies/ann.py | 89 ++-- axelrod/strategies/axelrod_first.py | 71 +++- axelrod/strategies/axelrod_second.py | 99 ++++- axelrod/strategies/backstabber.py | 4 +- axelrod/strategies/bush_mosteller.py | 21 +- axelrod/strategies/calculator.py | 5 +- axelrod/strategies/cycler.py | 30 +- axelrod/strategies/dbs.py | 32 +- axelrod/strategies/defector.py | 5 +- axelrod/strategies/finite_state_machines.py | 95 +++-- axelrod/strategies/gambler.py | 10 +- axelrod/strategies/gobymajority.py | 4 +- axelrod/strategies/grudger.py | 2 +- axelrod/strategies/hmm.py | 148 +++++-- axelrod/strategies/human.py | 12 +- axelrod/strategies/hunter.py | 14 +- axelrod/strategies/lookerup.py | 109 +++-- axelrod/strategies/memoryone.py | 8 +- axelrod/strategies/memorytwo.py | 25 +- axelrod/strategies/meta.py | 16 +- axelrod/strategies/prober.py | 2 +- axelrod/strategies/punisher.py | 8 +- axelrod/strategies/qlearner.py | 20 +- axelrod/strategies/shortmem.py | 2 +- axelrod/strategies/titfortat.py | 9 +- axelrod/strategies/zero_determinant.py | 20 +- axelrod/strategy_transformers.py | 31 +- axelrod/tests/integration/test_filtering.py | 16 +- axelrod/tests/integration/test_matches.py | 8 +- axelrod/tests/integration/test_names.py | 2 +- axelrod/tests/integration/test_tournament.py | 26 +- axelrod/tests/property.py | 85 ++-- axelrod/tests/strategies/test_adaptive.py | 14 +- axelrod/tests/strategies/test_ann.py | 5 +- axelrod/tests/strategies/test_apavlov.py | 105 ++++- .../tests/strategies/test_axelrod_first.py | 159 +++++-- .../tests/strategies/test_axelrod_second.py | 391 +++++++++++++----- axelrod/tests/strategies/test_backstabber.py | 10 +- .../strategies/test_better_and_better.py | 12 +- axelrod/tests/strategies/test_calculator.py | 54 ++- axelrod/tests/strategies/test_cooperator.py | 10 +- axelrod/tests/strategies/test_cycler.py | 16 +- axelrod/tests/strategies/test_darwin.py | 12 +- axelrod/tests/strategies/test_dbs.py | 24 +- axelrod/tests/strategies/test_doubler.py | 4 +- .../tests/strategies/test_evolvable_player.py | 36 +- .../strategies/test_finite_state_machines.py | 107 +++-- axelrod/tests/strategies/test_forgiver.py | 20 +- axelrod/tests/strategies/test_gambler.py | 44 +- axelrod/tests/strategies/test_geller.py | 22 +- axelrod/tests/strategies/test_gobymajority.py | 38 +- axelrod/tests/strategies/test_grudger.py | 39 +- axelrod/tests/strategies/test_grumpy.py | 20 +- axelrod/tests/strategies/test_hmm.py | 22 +- axelrod/tests/strategies/test_human.py | 4 +- axelrod/tests/strategies/test_inverse.py | 18 +- axelrod/tests/strategies/test_lookerup.py | 55 ++- axelrod/tests/strategies/test_memoryone.py | 81 +++- axelrod/tests/strategies/test_memorytwo.py | 31 +- axelrod/tests/strategies/test_meta.py | 70 +++- axelrod/tests/strategies/test_negation.py | 4 +- axelrod/tests/strategies/test_player.py | 58 ++- axelrod/tests/strategies/test_prober.py | 31 +- axelrod/tests/strategies/test_qlearner.py | 32 +- axelrod/tests/strategies/test_rand.py | 8 +- axelrod/tests/strategies/test_retaliate.py | 12 +- .../tests/strategies/test_revised_downing.py | 1 + axelrod/tests/strategies/test_selfsteem.py | 31 +- axelrod/tests/strategies/test_shortmem.py | 22 +- axelrod/tests/strategies/test_stalker.py | 13 +- axelrod/tests/strategies/test_titfortat.py | 152 +++++-- .../tests/strategies/test_worse_and_worse.py | 3 +- .../tests/strategies/test_zero_determinant.py | 83 +++- axelrod/tests/unit/test_classification.py | 47 ++- ...est_compute_finite_state_machine_memory.py | 2 +- .../tests/unit/test_deterministic_cache.py | 4 +- axelrod/tests/unit/test_filters.py | 38 +- axelrod/tests/unit/test_fingerprint.py | 101 +++-- axelrod/tests/unit/test_graph.py | 127 ++++-- axelrod/tests/unit/test_history.py | 12 +- axelrod/tests/unit/test_interaction_utils.py | 81 +++- axelrod/tests/unit/test_match.py | 26 +- axelrod/tests/unit/test_match_generator.py | 28 +- axelrod/tests/unit/test_moran.py | 100 +++-- axelrod/tests/unit/test_pickling.py | 65 ++- axelrod/tests/unit/test_plot.py | 51 ++- axelrod/tests/unit/test_property.py | 16 +- axelrod/tests/unit/test_resultset.py | 213 ++++++++-- .../tests/unit/test_strategy_transformers.py | 116 ++++-- axelrod/tests/unit/test_strategy_utils.py | 15 +- axelrod/tests/unit/test_tournament.py | 111 +++-- axelrod/tournament.py | 49 ++- docs/conf.py | 12 +- doctests.py | 3 +- 111 files changed, 3264 insertions(+), 1138 deletions(-) diff --git a/.prepare-commit-msg.py b/.prepare-commit-msg.py index 780f8594b..dac608fd9 100755 --- a/.prepare-commit-msg.py +++ b/.prepare-commit-msg.py @@ -41,7 +41,9 @@ if branch.startswith(issue_prefix): issue_number = re.match("%s(.*)" % issue_prefix, branch).group(1) - print("prepare-commit-msg: Prepending [#%s] to commit message" % issue_number) + print( + "prepare-commit-msg: Prepending [#%s] to commit message" % issue_number + ) with open(commit_msg_filepath, "r+") as f: content = f.read() diff --git a/axelrod/evolvable_player.py b/axelrod/evolvable_player.py index 7dd4073b3..ce32bf0e5 100644 --- a/axelrod/evolvable_player.py +++ b/axelrod/evolvable_player.py @@ -7,6 +7,7 @@ class InsufficientParametersError(Exception): """Error indicating that insufficient parameters were specified to initialize an Evolvable Player.""" + def __init__(self, *args): super().__init__(*args) @@ -49,7 +50,7 @@ def create_new(self, **kwargs): def serialize_parameters(self): """Serialize parameters.""" pickled = dumps(self.init_kwargs) # bytes - s = base64.b64encode(pickled).decode('utf8') # string + s = base64.b64encode(pickled).decode("utf8") # string return s @classmethod diff --git a/axelrod/fingerprint.py b/axelrod/fingerprint.py index 264856a15..8ef34d60b 100644 --- a/axelrod/fingerprint.py +++ b/axelrod/fingerprint.py @@ -42,7 +42,7 @@ def _create_points(step: float, progress_bar: bool = True) -> List[Point]: num = int((1 / step) // 1) + 1 if progress_bar: - p_bar = tqdm.tqdm(total=num**2, desc="Generating points") + p_bar = tqdm.tqdm(total=num ** 2, desc="Generating points") points = [] for x in np.linspace(0, 1, num): @@ -88,8 +88,8 @@ def _create_jossann(point: Point, probe: Any) -> Player: if x + y >= 1: joss_ann = DualTransformer()( - JossAnnTransformer((1 - x, 1 - y))( - probe_class))(**init_kwargs) + JossAnnTransformer((1 - x, 1 - y))(probe_class) + )(**init_kwargs) else: joss_ann = JossAnnTransformer((x, y))(probe_class)(**init_kwargs) return joss_ann @@ -177,7 +177,10 @@ def _generate_data(interactions: dict, points: list, edges: list) -> dict: """ edge_scores = [ np.mean( - [compute_final_score_per_turn(scores)[0] for scores in interactions[edge]] + [ + compute_final_score_per_turn(scores)[0] + for scores in interactions[edge] + ] ) for edge in edges ] @@ -215,7 +218,9 @@ def _reshape_data(data: dict, points: list, size: int) -> np.ndarray: class AshlockFingerprint(object): def __init__( - self, strategy: Union[type, Player], probe: Union[type, Player] = axl.TitForTat + self, + strategy: Union[type, Player], + probe: Union[type, Player] = axl.TitForTat, ) -> None: """ Parameters @@ -277,7 +282,7 @@ def fingerprint( processes: int = None, filename: str = None, progress_bar: bool = True, - seed: int = None + seed: int = None, ) -> dict: """Build and play the spatial tournament. @@ -323,8 +328,11 @@ def fingerprint( self.step = step self.spatial_tournament = axl.Tournament( - tourn_players, turns=turns, repetitions=repetitions, edges=edges, - seed=seed + tourn_players, + turns=turns, + repetitions=repetitions, + edges=edges, + seed=seed, ) self.spatial_tournament.play( build_results=False, @@ -432,7 +440,7 @@ def fingerprint( processes: int = None, filename: str = None, progress_bar: bool = True, - seed: int = None + seed: int = None, ) -> np.array: """Creates a spatial tournament to run the necessary matches to obtain fingerprint data. @@ -479,7 +487,7 @@ def fingerprint( turns=turns, noise=noise, repetitions=repetitions, - seed=seed + seed=seed, ) tournament.play( filename=filename, @@ -516,7 +524,9 @@ def analyse_cooperation_ratio(filename): opponent in each turn. The ith row corresponds to the ith opponent and the jth column the jth turn. """ - did_c = np.vectorize(lambda actions: [int(action == "C") for action in actions]) + did_c = np.vectorize( + lambda actions: [int(action == "C") for action in actions] + ) cooperation_rates = {} df = dd.read_csv(filename) @@ -525,7 +535,10 @@ def analyse_cooperation_ratio(filename): df = df[df["Player index"] == 0][["Opponent index", "Actions"]] for _, row in df.iterrows(): - opponent_index, player_history = row["Opponent index"], row["Actions"] + opponent_index, player_history = ( + row["Opponent index"], + row["Actions"], + ) if opponent_index in cooperation_rates: cooperation_rates[opponent_index].append(did_c(player_history)) else: @@ -590,7 +603,8 @@ def plot( if display_names: plt.yticks( - range(len(self.opponents)), [str(player) for player in self.opponents] + range(len(self.opponents)), + [str(player) for player in self.opponents], ) else: plt.yticks([0, len(self.opponents) - 1], [0, 1]) diff --git a/axelrod/game.py b/axelrod/game.py index 1c3278275..af30aca29 100644 --- a/axelrod/game.py +++ b/axelrod/game.py @@ -16,7 +16,9 @@ class Game(object): The numerical score attribute to all combinations of action pairs. """ - def __init__(self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1) -> None: + def __init__( + self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1 + ) -> None: """Create a new game object. Parameters @@ -30,7 +32,12 @@ def __init__(self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1) -> No p: int or float Score obtained by both player for mutual defection. """ - self.scores = {(C, C): (r, r), (D, D): (p, p), (C, D): (s, t), (D, C): (t, s)} + self.scores = { + (C, C): (r, r), + (D, D): (p, p), + (C, D): (s, t), + (D, C): (t, s), + } def RPST(self) -> Tuple[Score, Score, Score, Score]: """Returns game matrix values in Press and Dyson notation.""" diff --git a/axelrod/graph.py b/axelrod/graph.py index 9f41bde4d..6b91677b0 100644 --- a/axelrod/graph.py +++ b/axelrod/graph.py @@ -155,8 +155,9 @@ def attached_complete_graphs(length, loops=True, directed=False): for cluster in range(2): for i in range(length): for j in range(i + 1, length): - edges.append(("{}:{}".format(cluster, i), - "{}:{}".format(cluster, j))) + edges.append( + ("{}:{}".format(cluster, i), "{}:{}".format(cluster, j)) + ) # Attach at one node edges.append(("0:0", "1:0")) graph = Graph(directed=directed, edges=edges) diff --git a/axelrod/history.py b/axelrod/history.py index 9c0ff2ef1..aea7b1f64 100644 --- a/axelrod/history.py +++ b/axelrod/history.py @@ -82,7 +82,9 @@ def __eq__(self, other): if isinstance(other, list): return self._plays == other elif isinstance(other, History): - return self._plays == other._plays and self._coplays == other._coplays + return ( + self._plays == other._plays and self._coplays == other._coplays + ) raise TypeError("Cannot compare types.") def __getitem__(self, key): @@ -121,7 +123,9 @@ def __init__(self, memory_depth, plays=None, coplays=None): def flip_plays(self): """Creates a flipped plays history for use with DualTransformer.""" flipped_plays = [action.flip() for action in self._plays] - return self.__class__(self.memory_depth, plays=flipped_plays, coplays=self._coplays) + return self.__class__( + self.memory_depth, plays=flipped_plays, coplays=self._coplays + ) def append(self, play, coplay): """Appends a new (play, coplay) pair an updates metadata for diff --git a/axelrod/interaction_utils.py b/axelrod/interaction_utils.py index 82159f94c..220053ee3 100644 --- a/axelrod/interaction_utils.py +++ b/axelrod/interaction_utils.py @@ -32,7 +32,8 @@ def compute_final_score(interactions, game=None): return None final_score = tuple( - sum([score[player_index] for score in scores]) for player_index in [0, 1] + sum([score[player_index] for score in scores]) + for player_index in [0, 1] ) return final_score diff --git a/axelrod/match.py b/axelrod/match.py index 907e85f44..19c83abd9 100644 --- a/axelrod/match.py +++ b/axelrod/match.py @@ -29,7 +29,7 @@ def __init__( noise=0, match_attributes=None, reset=True, - seed=None + seed=None, ): """ Parameters @@ -193,7 +193,8 @@ def play(self): result = [] for _ in range(turns): plays = self.simultaneous_play( - self.players[0], self.players[1], self.noise) + self.players[0], self.players[1], self.noise + ) result.append(plays) if self._cache_update_required: diff --git a/axelrod/match_generator.py b/axelrod/match_generator.py index c0ad188bd..37038e7b5 100644 --- a/axelrod/match_generator.py +++ b/axelrod/match_generator.py @@ -12,7 +12,7 @@ def __init__( prob_end=None, edges=None, match_attributes=None, - seed=None + seed=None, ): """ A class to generate matches. This is used by the Tournament class which diff --git a/axelrod/moran.py b/axelrod/moran.py index 3c9876c40..af235e1d9 100644 --- a/axelrod/moran.py +++ b/axelrod/moran.py @@ -28,7 +28,7 @@ def __init__( fitness_transformation: Callable = None, mutation_method="transition", stop_on_fixation=True, - seed=None + seed=None, ) -> None: """ An agent based Moran process class. In each round, each player plays a @@ -93,7 +93,9 @@ def __init__( if m in ["atomic", "transition"]: self.mutation_method = m else: - raise ValueError("Invalid mutation method {}".format(mutation_method)) + raise ValueError( + "Invalid mutation method {}".format(mutation_method) + ) assert (mutation_rate >= 0) and (mutation_rate <= 1) assert (noise >= 0) and (noise <= 1) mode = mode.lower() @@ -127,7 +129,9 @@ def __init__( d[str(p)] = p mutation_targets = dict() for key in sorted(keys): - mutation_targets[key] = [v for (k, v) in sorted(d.items()) if k != key] + mutation_targets[key] = [ + v for (k, v) in sorted(d.items()) if k != key + ] self.mutation_targets = mutation_targets if interaction_graph is None: @@ -146,14 +150,18 @@ def __init__( self.fitness_transformation = fitness_transformation # Map players to graph vertices self.locations = sorted(interaction_graph.vertices) - self.index = dict(zip(sorted(interaction_graph.vertices), range(len(players)))) + self.index = dict( + zip(sorted(interaction_graph.vertices), range(len(players))) + ) self.fixated = self.fixation_check() def set_players(self) -> None: """Copy the initial players into the first population, setting seeds as needed.""" self.players = [] for player in self.initial_players: - if (self.mutation_method == "atomic") and issubclass(player.__class__, EvolvablePlayer): + if (self.mutation_method == "atomic") and issubclass( + player.__class__, EvolvablePlayer + ): # For reproducibility, we generate random seeds for evolvable players. seed = next(self._bulk_random) new_player = player.create_new(seed=seed) @@ -163,8 +171,9 @@ def set_players(self) -> None: self.players.append(player) self.populations = [self.population_distribution()] - def fitness_proportionate_selection(self, - scores: List, fitness_transformation: Callable = None) -> int: + def fitness_proportionate_selection( + self, scores: List, fitness_transformation: Callable = None + ) -> int: """Randomly selects an individual proportionally to score. Parameters @@ -200,7 +209,9 @@ def mutate(self, index: int) -> Player: if self.mutation_method == "atomic": if not issubclass(self.players[index].__class__, EvolvablePlayer): - raise TypeError("Player is not evolvable. Use a subclass of EvolvablePlayer.") + raise TypeError( + "Player is not evolvable. Use a subclass of EvolvablePlayer." + ) return self.players[index].mutate() # Assuming mutation_method == "transition" @@ -237,7 +248,9 @@ def death(self, index: int = None) -> int: # Select locally # index is not None in this case vertex = self._random.choice( - sorted(self.reproduction_graph.out_vertices(self.locations[index])) + sorted( + self.reproduction_graph.out_vertices(self.locations[index]) + ) ) i = self.index[vertex] return i @@ -370,7 +383,7 @@ def score_all(self) -> List: noise=self.noise, game=self.game, deterministic_cache=self.deterministic_cache, - seed=next(self._bulk_random) + seed=next(self._bulk_random), ) match.play() match_scores = match.final_score_per_turn() @@ -484,8 +497,11 @@ class ApproximateMoranProcess(MoranProcess): """ def __init__( - self, players: List[Player], cached_outcomes: dict, mutation_rate: float = 0, - seed: Optional[int] = None + self, + players: List[Player], + cached_outcomes: dict, + mutation_rate: float = 0, + seed: Optional[int] = None, ) -> None: """ Parameters @@ -503,7 +519,7 @@ def __init__( noise=0, deterministic_cache=None, mutation_rate=mutation_rate, - seed=seed + seed=seed, ) self.cached_outcomes = cached_outcomes @@ -529,7 +545,9 @@ def score_all(self) -> List: scores = [0] * N for i in range(N): for j in range(i + 1, N): - player_names = tuple([str(self.players[i]), str(self.players[j])]) + player_names = tuple( + [str(self.players[i]), str(self.players[j])] + ) cached_score = self._get_scores_from_cache(player_names) scores[i] += cached_score[0] scores[j] += cached_score[1] diff --git a/axelrod/player.py b/axelrod/player.py index 6b7370be2..87c08ddb8 100644 --- a/axelrod/player.py +++ b/axelrod/player.py @@ -59,6 +59,7 @@ def _post_init(self): See here to learn more: https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ """ + def __call__(cls, *args, **kwargs): # This calls cls.__new__ and cls.__init__ obj = type.__call__(cls, *args, **kwargs) @@ -130,7 +131,9 @@ def __eq__(self, other): if self.__repr__() != other.__repr__(): return False - for attribute in set(list(self.__dict__.keys()) + list(other.__dict__.keys())): + for attribute in set( + list(self.__dict__.keys()) + list(other.__dict__.keys()) + ): value = getattr(self, attribute, None) other_value = getattr(other, attribute, None) @@ -148,14 +151,20 @@ def __eq__(self, other): ): # Split the original generator so it is not touched generator, original_value = itertools.tee(value) - other_generator, original_other_value = itertools.tee(other_value) + other_generator, original_other_value = itertools.tee( + other_value + ) if isinstance(value, types.GeneratorType): setattr(self, attribute, (ele for ele in original_value)) - setattr(other, attribute, (ele for ele in original_other_value)) + setattr( + other, attribute, (ele for ele in original_other_value) + ) else: setattr(self, attribute, itertools.cycle(original_value)) - setattr(other, attribute, itertools.cycle(original_other_value)) + setattr( + other, attribute, itertools.cycle(original_other_value) + ) for _ in range(200): try: @@ -190,7 +199,8 @@ def set_seed(self, seed): if seed is None: warnings.warn( "Initializing player with seed from Axelrod module random number generator. " - "Results may not be seed reproducible.") + "Results may not be seed reproducible." + ) self._seed = _module_random.random_seed_int() else: self._seed = seed @@ -201,7 +211,9 @@ def __repr__(self): Appends the `__init__` parameters to the strategy's name.""" name = self.name prefix = ": " - gen = (value for value in self.init_kwargs.values() if value is not None) + gen = ( + value for value in self.init_kwargs.values() if value is not None + ) for value in gen: try: if issubclass(value, Player): diff --git a/axelrod/plot.py b/axelrod/plot.py index f56cc9f4f..feacae984 100644 --- a/axelrod/plot.py +++ b/axelrod/plot.py @@ -97,7 +97,9 @@ def _winplot_dataset(self): # Sort wins by median wins = self.result_set.wins medians = map(median, wins) - medians = sorted([(m, i) for (i, m) in enumerate(medians)], reverse=True) + medians = sorted( + [(m, i) for (i, m) in enumerate(medians)], reverse=True + ) # Reorder and grab names wins = [wins[x[-1]] for x in medians] ranked_names = [str(self.players[x[-1]]) for x in medians] @@ -324,7 +326,9 @@ def save_all_plots( pbar = tqdm.tqdm(total=total, desc="Obtaining plots") for method, name in plots: - f = getattr(self, method)(title="{} - {}".format(title_prefix, name)) + f = getattr(self, method)( + title="{} - {}".format(title_prefix, name) + ) path = pathlib.Path("{}_{}.{}".format(prefix, method, filetype)) f.savefig(axl_filename(path)) plt.close(f) diff --git a/axelrod/random_.py b/axelrod/random_.py index 23949c93e..73289ff8f 100644 --- a/axelrod/random_.py +++ b/axelrod/random_.py @@ -11,6 +11,7 @@ class RandomGenerator(object): """Container around a random number generator. Enables reproducibility of player behavior, matches, and tournaments.""" + def __init__(self, seed: Optional[int] = None): # _random is the internal object that generators random values self._random = RandomState() @@ -28,7 +29,7 @@ def randint(self, *args, **kwargs): return self._random.randint(*args, **kwargs) def random_seed_int(self) -> int: - return self.randint(low=0, high=2**32-1, dtype="uint64") + return self.randint(low=0, high=2 ** 32 - 1, dtype="uint64") def choice(self, *args, **kwargs): return self._random.choice(*args, **kwargs) @@ -118,7 +119,8 @@ class BulkRandomGenerator(object): """Bulk generator of random integers for tournament seeding and reproducibility. Bulk generation of random values is more efficient. Use this class like a generator.""" - def __init__(self, seed=None, batch_size:int = 1000): + + def __init__(self, seed=None, batch_size: int = 1000): self._random_generator = RandomState() self._random_generator.seed(seed) self._ints = None @@ -130,10 +132,8 @@ def _fill_ints(self): # Generate more random values. Store as a list since generators # cannot be pickled. self._ints = self._random_generator.randint( - low=0, - high=2**32 - 1, - size=self._batch_size, - dtype="uint64") + low=0, high=2 ** 32 - 1, size=self._batch_size, dtype="uint64" + ) self._index = 0 def __next__(self): diff --git a/axelrod/result_set.py b/axelrod/result_set.py index 65353cc6d..ee6a515de 100644 --- a/axelrod/result_set.py +++ b/axelrod/result_set.py @@ -117,9 +117,15 @@ def _reshape_out( alternative=0, ) - self.wins = self._reshape_two_dim_list(sum_per_player_repetition_df["Win"]) - self.scores = self._reshape_two_dim_list(sum_per_player_repetition_df["Score"]) - self.normalised_scores = self._reshape_two_dim_list(normalised_scores_series) + self.wins = self._reshape_two_dim_list( + sum_per_player_repetition_df["Win"] + ) + self.scores = self._reshape_two_dim_list( + sum_per_player_repetition_df["Score"] + ) + self.normalised_scores = self._reshape_two_dim_list( + normalised_scores_series + ) self.cooperation = self._build_cooperation( sum_per_player_opponent_df["Cooperation count"] @@ -132,7 +138,9 @@ def _reshape_out( self.state_distribution = self._build_state_distribution( sum_per_player_opponent_df[columns] ) - self.normalised_state_distribution = self._build_normalised_state_distribution() + self.normalised_state_distribution = ( + self._build_normalised_state_distribution() + ) columns = [ "CC to C count", @@ -166,7 +174,9 @@ def _reshape_out( self.ranked_names = self._build_ranked_names() self.payoff_matrix = self._build_summary_matrix(self.payoffs) - self.payoff_stddevs = self._build_summary_matrix(self.payoffs, func=np.std) + self.payoff_stddevs = self._build_summary_matrix( + self.payoffs, func=np.std + ) self.payoff_diffs_means = self._build_payoff_diffs_means() self.cooperating_rating = self._build_cooperating_rating() @@ -266,7 +276,9 @@ def _build_good_partner_matrix(self, good_partner_series): # interactions. row.append(0) else: - row.append(good_partner_dict.get((player_index, opponent_index), 0)) + row.append( + good_partner_dict.get((player_index, opponent_index), 0) + ) good_partner_matrix.append(row) return good_partner_matrix @@ -334,13 +346,17 @@ def _build_normalised_state_distribution(self): for counter in player: total = sum(counter.values()) counters.append( - Counter({key: value / total for key, value in counter.items()}) + Counter( + {key: value / total for key, value in counter.items()} + ) ) normalised_state_distribution.append(counters) return normalised_state_distribution @update_progress_bar - def _build_state_to_action_distribution(self, state_to_action_distribution_series): + def _build_state_to_action_distribution( + self, state_to_action_distribution_series + ): state_to_action_key_map = { "CC to C count": ((C, C), C), "CC to D count": ((C, C), D), @@ -396,8 +412,12 @@ def _build_normalised_state_to_action_distribution(self): return normalised_state_to_action_distribution @update_progress_bar - def _build_initial_cooperation_count(self, initial_cooperation_count_series): - initial_cooperation_count_dict = initial_cooperation_count_series.to_dict() + def _build_initial_cooperation_count( + self, initial_cooperation_count_series + ): + initial_cooperation_count_dict = ( + initial_cooperation_count_series.to_dict() + ) initial_cooperation_count = [ initial_cooperation_count_dict.get(player_index, 0) for player_index in range(self.num_players) @@ -427,7 +447,8 @@ def _build_initial_cooperation_rate(self, interactions_series): warnings.simplefilter("ignore") initial_cooperation_rate = list( np.nan_to_num( - np.array(self.initial_cooperation_count) / interactions_array + np.array(self.initial_cooperation_count) + / interactions_array ) ) return initial_cooperation_rate @@ -453,7 +474,9 @@ def _build_eigenmoses_rating(self): The eigenmoses rating as defined in: http://www.scottaaronson.com/morality.pdf """ - eigenvector, eigenvalue = eigen.principal_eigenvector(self.vengeful_cooperation) + eigenvector, eigenvalue = eigen.principal_eigenvector( + self.vengeful_cooperation + ) return eigenvector.tolist() @@ -577,7 +600,9 @@ def _build_tasks(self, df): ] sum_per_player_opponent_task = df.groupby(groups)[columns].sum() - ignore_self_interactions_task = df["Player index"] != df["Opponent index"] + ignore_self_interactions_task = ( + df["Player index"] != df["Opponent index"] + ) adf = df[ignore_self_interactions_task] groups = ["Player index", "Repetition"] @@ -591,7 +616,9 @@ def _build_tasks(self, df): groups = ["Player index"] column = "Initial cooperation" initial_cooperation_count_task = adf.groupby(groups)[column].sum() - interactions_count_task = adf.groupby("Player index")["Player index"].count() + interactions_count_task = adf.groupby("Player index")[ + "Player index" + ].count() return ( mean_per_reps_player_opponent_task, @@ -642,8 +669,12 @@ def list_equal_with_nans(v1: List[float], v2: List[float]) -> bool: self.cooperating_rating == other.cooperating_rating, self.good_partner_matrix == other.good_partner_matrix, self.good_partner_rating == other.good_partner_rating, - list_equal_with_nans(self.eigenmoses_rating, other.eigenmoses_rating), - list_equal_with_nans(self.eigenjesus_rating, other.eigenjesus_rating), + list_equal_with_nans( + self.eigenmoses_rating, other.eigenmoses_rating + ), + list_equal_with_nans( + self.eigenjesus_rating, other.eigenjesus_rating + ), ] ) @@ -713,7 +744,9 @@ def summarise(self): rates = [] for state in states: counts = [ - counter[(state, C)] for counter in player if counter[(state, C)] > 0 + counter[(state, C)] + for counter in player + if counter[(state, C)] > 0 ] if len(counts) > 0: @@ -736,7 +769,9 @@ def summarise(self): summary_data = [] for rank, i in enumerate(self.ranking): - data = list(summary_measures[i]) + state_prob[i] + state_to_C_prob[i] + data = ( + list(summary_measures[i]) + state_prob[i] + state_to_C_prob[i] + ) summary_data.append(self.player(rank, *data)) return summary_data diff --git a/axelrod/strategies/__init__.py b/axelrod/strategies/__init__.py index a7635596e..537ee89bc 100644 --- a/axelrod/strategies/__init__.py +++ b/axelrod/strategies/__init__.py @@ -90,7 +90,9 @@ short_run_time_strategies = [ s for s in strategies if not Classifiers["long_run_time"](s()) ] -cheating_strategies = [s for s in all_strategies if not Classifiers.obey_axelrod(s())] +cheating_strategies = [ + s for s in all_strategies if not Classifiers.obey_axelrod(s()) +] ordinary_strategies = strategies # This is a legacy and will be removed diff --git a/axelrod/strategies/_filters.py b/axelrod/strategies/_filters.py index c9c199bf7..2dc7a88b6 100644 --- a/axelrod/strategies/_filters.py +++ b/axelrod/strategies/_filters.py @@ -155,15 +155,24 @@ class ExampleStrategy(Player): ), "manipulates_state": FilterFunction( function=passes_operator_filter, - kwargs={"classifier_key": "manipulates_state", "operator": operator.eq}, + kwargs={ + "classifier_key": "manipulates_state", + "operator": operator.eq, + }, ), "manipulates_source": FilterFunction( function=passes_operator_filter, - kwargs={"classifier_key": "manipulates_source", "operator": operator.eq}, + kwargs={ + "classifier_key": "manipulates_source", + "operator": operator.eq, + }, ), "inspects_source": FilterFunction( function=passes_operator_filter, - kwargs={"classifier_key": "inspects_source", "operator": operator.eq}, + kwargs={ + "classifier_key": "inspects_source", + "operator": operator.eq, + }, ), "memory_depth": FilterFunction( function=passes_operator_filter, @@ -178,7 +187,8 @@ class ExampleStrategy(Player): kwargs={"classifier_key": "memory_depth", "operator": operator.le}, ), "makes_use_of": FilterFunction( - function=passes_in_list_filter, kwargs={"classifier_key": "makes_use_of"} + function=passes_in_list_filter, + kwargs={"classifier_key": "makes_use_of"}, ), } diff --git a/axelrod/strategies/adaptor.py b/axelrod/strategies/adaptor.py index f8c550f61..5b4026c8c 100644 --- a/axelrod/strategies/adaptor.py +++ b/axelrod/strategies/adaptor.py @@ -36,12 +36,13 @@ class AbstractAdaptor(Player): "manipulates_state": False, } - def __init__(self, delta: Dict[Tuple[Action, Action], float], - perr: float = 0.01) -> None: + def __init__( + self, delta: Dict[Tuple[Action, Action], float], perr: float = 0.01 + ) -> None: super().__init__() self.perr = perr self.delta = delta - self.s = 0. + self.s = 0.0 def strategy(self, opponent: Player) -> Action: if self.history: @@ -51,7 +52,8 @@ def strategy(self, opponent: Player) -> Action: # Compute probability of Cooperation p = self.perr + (1.0 - 2 * self.perr) * ( - heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1)) + heaviside(self.s + 1, 1) - heaviside(self.s - 1, 1) + ) # Draw action action = self._random.random_choice(p) return action @@ -71,10 +73,10 @@ class AdaptorBrief(AbstractAdaptor): def __init__(self) -> None: delta = { - (C, C): 0., # R + (C, C): 0.0, # R (C, D): -1.001505, # S - (D, C): 0.992107, # T - (D, D): -0.638734 # P + (D, C): 0.992107, # T + (D, D): -0.638734, # P } super().__init__(delta=delta) @@ -93,9 +95,9 @@ class AdaptorLong(AbstractAdaptor): def __init__(self) -> None: delta = { - (C, C): 0., # R + (C, C): 0.0, # R (C, D): 1.888159, # S (D, C): 1.858883, # T - (D, D): -0.995703 # P + (D, D): -0.995703, # P } super().__init__(delta=delta) diff --git a/axelrod/strategies/ann.py b/axelrod/strategies/ann.py index 74bd1ab7e..0079a8f1e 100644 --- a/axelrod/strategies/ann.py +++ b/axelrod/strategies/ann.py @@ -113,7 +113,10 @@ def compute_features(player: Player, opponent: Player) -> List[int]: def activate( - bias: List[float], hidden: List[float], output: List[float], inputs: List[int] + bias: List[float], + hidden: List[float], + output: List[float], + inputs: List[int], ) -> float: """ Compute the output of the neural network: @@ -193,8 +196,7 @@ class ANN(Player): } def __init__( - self, num_features: int, num_hidden: int, - weights: List[float] = None + self, num_features: int, num_hidden: int, weights: List[float] = None ) -> None: Player.__init__(self) self.num_features = num_features @@ -224,42 +226,68 @@ def strategy(self, opponent: Player) -> Action: class EvolvableANN(ANN, EvolvablePlayer): """Evolvable version of ANN.""" + name = "EvolvableANN" def __init__( - self, num_features: int, num_hidden: int, + self, + num_features: int, + num_hidden: int, weights: List[float] = None, mutation_probability: float = None, mutation_distance: int = 5, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) - num_features, num_hidden, weights, mutation_probability = self._normalize_parameters( - num_features, num_hidden, weights, mutation_probability) - ANN.__init__(self, - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + ( + num_features, + num_hidden, + weights, + mutation_probability, + ) = self._normalize_parameters( + num_features, num_hidden, weights, mutation_probability + ) + ANN.__init__( + self, + num_features=num_features, + num_hidden=num_hidden, + weights=weights, + ) self.mutation_probability = mutation_probability self.mutation_distance = mutation_distance self.overwrite_init_kwargs( num_features=num_features, num_hidden=num_hidden, weights=weights, - mutation_probability=mutation_probability) + mutation_probability=mutation_probability, + ) - def _normalize_parameters(self, num_features=None, num_hidden=None, weights=None, mutation_probability=None): + def _normalize_parameters( + self, + num_features=None, + num_hidden=None, + weights=None, + mutation_probability=None, + ): if not (num_features and num_hidden): - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableANN") + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableANN" + ) size = num_weights(num_features, num_hidden) if not weights: weights = [self._random.uniform(-1, 1) for _ in range(size)] if mutation_probability is None: - mutation_probability = 10. / size + mutation_probability = 10.0 / size return num_features, num_hidden, weights, mutation_probability - def mutate_weights(self, weights, num_features, num_hidden, mutation_probability, - mutation_distance): + def mutate_weights( + self, + weights, + num_features, + num_hidden, + mutation_probability, + mutation_distance, + ): size = num_weights(num_features, num_hidden) randoms = self._random.random(size) for i, r in enumerate(randoms): @@ -270,13 +298,19 @@ def mutate_weights(self, weights, num_features, num_hidden, mutation_probability def mutate(self): weights = self.mutate_weights( - self.weights, self.num_features, self.num_hidden, - self.mutation_probability, self.mutation_distance) + self.weights, + self.num_features, + self.num_hidden, + self.mutation_probability, + self.mutation_distance, + ) return self.create_new(weights=weights) def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") + raise TypeError( + "Crossover must be between the same player classes." + ) weights = crossover_lists(self.weights, other.weights, self._random) return self.create_new(weights=weights) @@ -299,9 +333,8 @@ class EvolvedANN(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN"] super().__init__( - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + num_features=num_features, num_hidden=num_hidden, weights=weights + ) class EvolvedANN5(ANN): @@ -322,9 +355,8 @@ class EvolvedANN5(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN 5"] super().__init__( - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + num_features=num_features, num_hidden=num_hidden, weights=weights + ) class EvolvedANNNoise05(ANN): @@ -345,6 +377,5 @@ class EvolvedANNNoise05(ANN): def __init__(self) -> None: num_features, num_hidden, weights = nn_weights["Evolved ANN 5 Noise 05"] super().__init__( - num_features=num_features, - num_hidden=num_hidden, - weights=weights) + num_features=num_features, num_hidden=num_hidden, weights=weights + ) diff --git a/axelrod/strategies/axelrod_first.py b/axelrod/strategies/axelrod_first.py index 0cc16d4e6..65a6a7776 100644 --- a/axelrod/strategies/axelrod_first.py +++ b/axelrod/strategies/axelrod_first.py @@ -66,7 +66,7 @@ def strategy(self, opponent: Player) -> Action: opponent ever plays D.""" if len(self.history) < self._rounds_to_cooperate: return C - if opponent.defections > 0: # Implement Grudger + if opponent.defections > 0: #  Implement Grudger return D return C @@ -262,12 +262,14 @@ def strategy(self, opponent: Player) -> Action: # Adding 1 to cooperations for assumption that first opponent move # being a response to a cooperation. See docstring for more # information. - alpha = (self.number_opponent_cooperations_in_response_to_C / - (self.cooperations + 1)) + alpha = self.number_opponent_cooperations_in_response_to_C / ( + self.cooperations + 1 + ) # Adding 2 to defections on the assumption that the first two # moves are defections, which may not be true in a noisy match - beta = (self.number_opponent_cooperations_in_response_to_D / - max(self.defections, 2)) + beta = self.number_opponent_cooperations_in_response_to_D / max( + self.defections, 2 + ) R, P, S, T = self.match_attributes["game"].RPST() expected_value_of_cooperating = alpha * R + (1 - alpha) * S @@ -430,25 +432,34 @@ def strategy(self, opponent: Player) -> Action: # Check if opponent plays randomly, if so, defect for the rest of the game p_value = chisquare([opponent.cooperations, opponent.defections]).pvalue - self.opponent_is_random = (p_value >= self.alpha) or self.opponent_is_random + self.opponent_is_random = ( + p_value >= self.alpha + ) or self.opponent_is_random if self.opponent_is_random: return D - if all( - opponent.history[i] == self.history[i - 1] - for i in range(1, len(self.history)) - ) or opponent.history == self.history: + if ( + all( + opponent.history[i] == self.history[i - 1] + for i in range(1, len(self.history)) + ) + or opponent.history == self.history + ): # Check if opponent plays Tit for Tat or a clone of itself. if opponent.history[-1] == D: return D return C if self.next_random_defection_turn is None: - self.next_random_defection_turn = self._random.randint(5, 15) + len(self.history) + self.next_random_defection_turn = self._random.randint(5, 15) + len( + self.history + ) if len(self.history) == self.next_random_defection_turn: # resample the next defection turn - self.next_random_defection_turn = self._random.randint(5, 15) + len(self.history) + self.next_random_defection_turn = self._random.randint(5, 15) + len( + self.history + ) return D return C @@ -580,7 +591,27 @@ class FirstByNydegger(Player): } def __init__(self) -> None: - self.As = [1, 6, 7, 17, 22, 23, 26, 29, 30, 31, 33, 38, 39, 45, 49, 54, 55, 58, 61] + self.As = [ + 1, + 6, + 7, + 17, + 22, + 23, + 26, + 29, + 30, + 31, + 33, + 38, + 39, + 45, + 49, + 54, + 55, + 58, + 61, + ] self.score_map = {(C, C): 0, (C, D): 2, (D, C): 1, (D, D): 3} super().__init__() @@ -610,7 +641,9 @@ def strategy(self, opponent: Player) -> Action: else: # TFT return D if opponent.history[-1] == D else C - A = self.score_history(self.history[-3:], opponent.history[-3:], self.score_map) + A = self.score_history( + self.history[-3:], opponent.history[-3:], self.score_map + ) if A in self.As: return D return C @@ -849,7 +882,9 @@ def strategy(self, opponent: Player) -> Action: return opponent.history[-1] if round_number % 15 == 0: - p_value = chisquare([opponent.cooperations, opponent.defections]).pvalue + p_value = chisquare( + [opponent.cooperations, opponent.defections] + ).pvalue self.opponent_is_random = p_value >= self.alpha if self.opponent_is_random: @@ -982,8 +1017,10 @@ def strategy(self, opponent: Player) -> Action: std_deviation = (N ** (1 / 2)) / 2 lower = N / 2 - 3 * std_deviation upper = N / 2 + 3 * std_deviation - if (self.remembered_number_of_opponent_defectioons <= lower or - self.remembered_number_of_opponent_defectioons >= upper): + if ( + self.remembered_number_of_opponent_defectioons <= lower + or self.remembered_number_of_opponent_defectioons >= upper + ): # Opponent deserves a fresh start self.last_fresh_start = current_round self._fresh_start() diff --git a/axelrod/strategies/axelrod_second.py b/axelrod/strategies/axelrod_second.py index 3ab54f40a..3d4f4b6c8 100644 --- a/axelrod/strategies/axelrod_second.py +++ b/axelrod/strategies/axelrod_second.py @@ -327,8 +327,12 @@ def __init__(self): self.opponent_consecutive_defections = 0 # equal to S variable self.one_turn_after_good_defection_ratio = 5 # equal to AD variable self.two_turns_after_good_defection_ratio = 0 # equal to NO variable - self.one_turn_after_good_defection_ratio_count = 1 # equal to AK variable - self.two_turns_after_good_defection_ratio_count = 1 # equal to NK variable + self.one_turn_after_good_defection_ratio_count = ( + 1 # equal to AK variable + ) + self.two_turns_after_good_defection_ratio_count = ( + 1 # equal to NK variable + ) # All above variables correspond to those in original Fotran Code self.dict = {C: 0, D: 1} @@ -353,7 +357,12 @@ def update_state(self, opponent): ) + (3 - (3 * self.dict[opponent.history[-1]])) + (2 * self.dict[self.history[-1]]) - - ((self.dict[opponent.history[-1]] * self.dict[self.history[-1]])) + - ( + ( + self.dict[opponent.history[-1]] + * self.dict[self.history[-1]] + ) + ) ) / (self.two_turns_after_good_defection_ratio_count + 1) self.two_turns_after_good_defection_ratio_count += 1 elif self.num_turns_after_good_defection == 1: @@ -365,7 +374,10 @@ def update_state(self, opponent): ) + (3 - (3 * self.dict[opponent.history[-1]])) + (2 * self.dict[self.history[-1]]) - - (self.dict[opponent.history[-1]] * self.dict[self.history[-1]]) + - ( + self.dict[opponent.history[-1]] + * self.dict[self.history[-1]] + ) ) / (self.one_turn_after_good_defection_ratio_count + 1) self.one_turn_after_good_defection_ratio_count += 1 @@ -402,7 +414,10 @@ def strategy(self, opponent: Player) -> Action: return D if (current_score[0] / ((len(self.history)) + 1)) >= 1.75: probability = ( - (0.25 + ((opponent.cooperations + 1) / ((len(self.history)) + 1))) + ( + 0.25 + + ((opponent.cooperations + 1) / ((len(self.history)) + 1)) + ) - (self.opponent_consecutive_defections * 0.25) + ((current_score[0] - current_score[1]) / 100) + (4 / ((len(self.history)) + 1)) @@ -516,7 +531,12 @@ class SecondByKluepfel(Player): def __init__(self): super().__init__() - self.cd_counts, self.dd_counts, self.dc_counts, self.cc_counts = 0, 0, 0, 0 + self.cd_counts, self.dd_counts, self.dc_counts, self.cc_counts = ( + 0, + 0, + 0, + 0, + ) def strategy(self, opponent: Player) -> Action: # First update the response matrix. @@ -534,7 +554,9 @@ def strategy(self, opponent: Player) -> Action: # Check for randomness if len(self.history) > 26: - if self.cd_counts >= (self.cd_counts + self.dd_counts) / 2 - 0.75 * np.sqrt( + if self.cd_counts >= ( + self.cd_counts + self.dd_counts + ) / 2 - 0.75 * np.sqrt( self.cd_counts + self.dd_counts ) and self.dc_counts >= ( self.dc_counts + self.cc_counts @@ -969,7 +991,10 @@ def strategy(self, opponent: Player) -> Action: if self.forgive_flag: self.forgive_flag = False self.defect_padding = 0 - if self.grudge < len(self.history) + 1 and opponent.history[-1] == D: + if ( + self.grudge < len(self.history) + 1 + and opponent.history[-1] == D + ): # Then override self.grudge += 20 return self.try_return(C) @@ -1095,7 +1120,9 @@ def __init__(self): self.mode = "Normal" self.recorded_defects = 0 # Count opponent defects after turn 1 self.exit_defect_meter = 0 # When >= 11, then exit defect mode. - self.coops_in_first_36 = None # On turn 37, count cooperations in first 36 + self.coops_in_first_36 = ( + None # On turn 37, count cooperations in first 36 + ) self.was_defective = False # Previously in Defect mode self.prob = 0.25 # After turn 37, probability that we'll defect @@ -1105,7 +1132,9 @@ def __init__(self): self.more_coop = 0 # This schedules cooperation for future turns # Initial last_generous_n_turns_ago to 3 because this counts up and # triggers a strategy change at 2. - self.last_generous_n_turns_ago = 3 # How many tuns ago was a "generous" move + self.last_generous_n_turns_ago = ( + 3 # How many tuns ago was a "generous" move + ) self.burned = False self.defect_streak = 0 @@ -1114,7 +1143,9 @@ def __init__(self): 0, ] # Counters that get (almost) alternatively incremented. self.parity_bit = 0 # Which parity_streak to increment - self.parity_limit = 5 # When a parity streak hits this limit, alter strategy. + self.parity_limit = ( + 5 # When a parity streak hits this limit, alter strategy. + ) self.parity_hits = 0 # Counts how many times a parity_limit was hit. # After hitting parity_hits 8 times, lower parity_limit to 3. @@ -1153,7 +1184,9 @@ def calculate_chi_squared(self, turn): denom = turn - 2 expected_matrix = ( - np.outer(self.move_history.sum(axis=1), self.move_history.sum(axis=0)) + np.outer( + self.move_history.sum(axis=1), self.move_history.sum(axis=0) + ) / denom ) @@ -1162,7 +1195,9 @@ def calculate_chi_squared(self, turn): for j in range(2): expect = expected_matrix[i, j] if expect > 1.0: - chi_squared += (expect - self.move_history[i, j]) ** 2 / expect + chi_squared += ( + expect - self.move_history[i, j] + ) ** 2 / expect return chi_squared @@ -1184,7 +1219,10 @@ def detect_random(self, turn): if self.move_history[0, 0] / denom >= 0.8: return False - if self.recorded_defects / denom < 0.25 or self.recorded_defects / denom > 0.75: + if ( + self.recorded_defects / denom < 0.25 + or self.recorded_defects / denom > 0.75 + ): return False if self.calculate_chi_squared(turn) > 3: @@ -1276,7 +1314,11 @@ def strategy(self, opponent: Player) -> Action: # Only enter Fair-weather mode if the opponent Cooperated the first 37 # turns then Defected on the 38th. - if turn == 38 and opponent.history[-1] == D and opponent.cooperations == 36: + if ( + turn == 38 + and opponent.history[-1] == D + and opponent.cooperations == 36 + ): self.mode = "Fair-weather" return self.try_return(to_return=C, lower_flags=False) @@ -1297,7 +1339,9 @@ def strategy(self, opponent: Player) -> Action: self.parity_streak[ self.parity_bit ] = 0 # Reset `parity_streak` when we hit the limit. - self.parity_hits += 1 # Keep track of how many times we hit the limit. + self.parity_hits += ( + 1 # Keep track of how many times we hit the limit. + ) if self.parity_hits >= 8: # After 8 times, lower the limit. self.parity_limit = 3 return self.try_return( @@ -1521,7 +1565,9 @@ def strategy(self, opponent: Player) -> Action: recent_history[-go_back] = opponent.history[-go_back] return self._random.random_choice( - self.prob_coop[(recent_history[-3], recent_history[-2], recent_history[-1])] + self.prob_coop[ + (recent_history[-3], recent_history[-2], recent_history[-1]) + ] ) @@ -1704,7 +1750,10 @@ def strategy(self, opponent: Player) -> Action: else: self.def_after_ab_count += 1 self.streak_needed = ( - np.floor(20.0 * self.def_after_ab_count / self.coop_after_ab_count) + 1 + np.floor( + 20.0 * self.def_after_ab_count / self.coop_after_ab_count + ) + + 1 ) self.current_streak = 0 return C @@ -1804,7 +1853,9 @@ def strategy(self, opponent: Player) -> Action: # Update history if turn >= 3: - self.count_them_us_them[(them_three_ago, us_two_ago, them_two_ago)] += 1 + self.count_them_us_them[ + (them_three_ago, us_two_ago, them_two_ago) + ] += 1 if ( self.count_them_us_them[(them_two_ago, us_last, C)] @@ -1866,7 +1917,9 @@ def __init__(self) -> None: (10, D, 7, C), ) - super().__init__(transitions=transitions, initial_state=0, initial_action=C) + super().__init__( + transitions=transitions, initial_state=0, initial_action=C + ) class SecondByMikkelson(FSMPlayer): @@ -2100,6 +2153,8 @@ def strategy(self, opponent: Player) -> Action: # Calculate the probability that the opponent cooperated last turn given # what we know two turns ago. - prob_coop = self.opp_c_after_x[us_two_turns_ago] / self.total_num_of_x[ - us_two_turns_ago] + prob_coop = ( + self.opp_c_after_x[us_two_turns_ago] + / self.total_num_of_x[us_two_turns_ago] + ) return self._random.random_choice(prob_coop) diff --git a/axelrod/strategies/backstabber.py b/axelrod/strategies/backstabber.py index 0d9d821d9..3150d8974 100644 --- a/axelrod/strategies/backstabber.py +++ b/axelrod/strategies/backstabber.py @@ -97,7 +97,9 @@ def _opponent_triggers_alt_strategy(opponent: Player) -> bool: return before_alt_strategy < current_round <= last_round_of_alt_strategy -def _opponent_defected_in_first_n_rounds(opponent: Player, first_n_rounds: int) -> bool: +def _opponent_defected_in_first_n_rounds( + opponent: Player, first_n_rounds: int +) -> bool: """ If opponent defected in the first N rounds, return True. Else return False. """ diff --git a/axelrod/strategies/bush_mosteller.py b/axelrod/strategies/bush_mosteller.py index 3fed4127b..154d02571 100644 --- a/axelrod/strategies/bush_mosteller.py +++ b/axelrod/strategies/bush_mosteller.py @@ -62,7 +62,10 @@ def __init__( self._c_prob, self._d_prob = c_prob, d_prob self._init_c_prob, self._init_d_prob = c_prob, d_prob self._aspiration_level = abs( - (max(self.match_attributes["game"].RPST()) / aspiration_level_divider) + ( + max(self.match_attributes["game"].RPST()) + / aspiration_level_divider + ) ) self._stimulus = 0.0 @@ -104,7 +107,9 @@ def stimulus_update(self, opponent: Player): ) elif self._stimulus < 0: - self._c_prob += self._learning_rate * self._stimulus * self._c_prob + self._c_prob += ( + self._learning_rate * self._stimulus * self._c_prob + ) # Updates probability following previous choice D if self.history[-1] == D: @@ -114,15 +119,21 @@ def stimulus_update(self, opponent: Player): ) elif self._stimulus < 0: - self._d_prob += self._learning_rate * self._stimulus * self._d_prob + self._d_prob += ( + self._learning_rate * self._stimulus * self._d_prob + ) def strategy(self, opponent: Player) -> Action: # First turn if len(self.history) == 0: - return self._random.random_choice(self._c_prob / (self._c_prob + self._d_prob)) + return self._random.random_choice( + self._c_prob / (self._c_prob + self._d_prob) + ) # Updating stimulus depending on his own latest choice self.stimulus_update(opponent) - return self._random.random_choice(self._c_prob / (self._c_prob + self._d_prob)) + return self._random.random_choice( + self._c_prob / (self._c_prob + self._d_prob) + ) diff --git a/axelrod/strategies/calculator.py b/axelrod/strategies/calculator.py index 05cdba01e..259bccb91 100644 --- a/axelrod/strategies/calculator.py +++ b/axelrod/strategies/calculator.py @@ -39,8 +39,9 @@ def set_seed(self, seed: int = None): def strategy(self, opponent: Player) -> Action: turn = len(self.history) if turn > 0: - self.joss_instance.history.append(self.history[-1], - opponent.history[-1]) + self.joss_instance.history.append( + self.history[-1], opponent.history[-1] + ) if turn == 20: self.cycle = detect_cycle(opponent.history) return self.extended_strategy(opponent) diff --git a/axelrod/strategies/cycler.py b/axelrod/strategies/cycler.py index bff8c83d4..ead3e7127 100644 --- a/axelrod/strategies/cycler.py +++ b/axelrod/strategies/cycler.py @@ -111,23 +111,25 @@ def __init__( cycle_length: int = None, mutation_probability: float = 0.2, mutation_potency: int = 1, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) cycle, cycle_length = self._normalize_parameters(cycle, cycle_length) Cycler.__init__(self, cycle=cycle) # Overwrite init_kwargs in the case that we generated a new cycle from cycle_length - self.overwrite_init_kwargs( - cycle=cycle, - cycle_length=cycle_length) + self.overwrite_init_kwargs(cycle=cycle, cycle_length=cycle_length) self.mutation_probability = mutation_probability self.mutation_potency = mutation_potency - def _normalize_parameters(self, cycle=None, cycle_length=None) -> Tuple[str, int]: + def _normalize_parameters( + self, cycle=None, cycle_length=None + ) -> Tuple[str, int]: """Compute other parameters from those that may be missing, to ensure proper cloning.""" if not cycle: if not cycle_length: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableCycler") + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableCycler" + ) cycle = self._generate_random_cycle(cycle_length) cycle_length = len(cycle) return cycle, cycle_length @@ -136,7 +138,9 @@ def _generate_random_cycle(self, cycle_length: int) -> str: """ Generate a sequence of random moves """ - return actions_to_str(self._random.choice(actions) for _ in range(cycle_length)) + return actions_to_str( + self._random.choice(actions) for _ in range(cycle_length) + ) def mutate(self) -> EvolvablePlayer: """ @@ -145,8 +149,12 @@ def mutate(self) -> EvolvablePlayer: if self._random.random() <= self.mutation_probability: mutated_sequence = list(str_to_actions(self.cycle)) for _ in range(self.mutation_potency): - index_to_change = self._random.randint(0, len(mutated_sequence) - 1) - mutated_sequence[index_to_change] = mutated_sequence[index_to_change].flip() + index_to_change = self._random.randint( + 0, len(mutated_sequence) - 1 + ) + mutated_sequence[index_to_change] = mutated_sequence[ + index_to_change + ].flip() cycle = actions_to_str(mutated_sequence) else: cycle = self.cycle @@ -158,7 +166,9 @@ def crossover(self, other) -> EvolvablePlayer: Creates and returns a new Player instance with a single crossover point. """ if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") + raise TypeError( + "Crossover must be between the same player classes." + ) cycle_list = crossover_lists(self.cycle, other.cycle, self._random) cycle = "".join(cycle_list) cycle, _ = self._normalize_parameters(cycle) diff --git a/axelrod/strategies/dbs.py b/axelrod/strategies/dbs.py index 8e7916718..0d4f6742d 100644 --- a/axelrod/strategies/dbs.py +++ b/axelrod/strategies/dbs.py @@ -266,7 +266,9 @@ def strategy(self, opponent: Player) -> Action: if r_minus_in_Rd: self.v += 1 # If the number of violations is superior to a threshold, clean Rd. - if (self.v > self.reject_threshold) or (r_plus_in_Rc and r_minus_in_Rd): + if (self.v > self.reject_threshold) or ( + r_plus_in_Rc and r_minus_in_Rd + ): self.Rd.clear() self.v = 0 @@ -274,7 +276,9 @@ def strategy(self, opponent: Player) -> Action: Rp = {} all_cond = [(C, C), (C, D), (D, C), (D, D)] for outcome in all_cond: - if (outcome not in self.Rc.keys()) and (outcome not in self.Rd.keys()): + if (outcome not in self.Rc.keys()) and ( + outcome not in self.Rd.keys() + ): # Compute opponent's C answer probability. Rp[outcome] = self.compute_prob_rule(outcome, self.alpha) @@ -327,8 +331,12 @@ def get_siblings(self): siblings which are DeterministicNodes, their depth is equal to current node depth's + 1. """ - opponent_c_choice = DeterministicNode(self.own_action, C, self.depth + 1) - opponent_d_choice = DeterministicNode(self.own_action, D, self.depth + 1) + opponent_c_choice = DeterministicNode( + self.own_action, C, self.depth + 1 + ) + opponent_d_choice = DeterministicNode( + self.own_action, D, self.depth + 1 + ) return opponent_c_choice, opponent_d_choice def is_stochastic(self): @@ -354,8 +362,12 @@ def get_siblings(self, policy): of the same depth as the current node. Their probabilities pC are defined by the policy argument. """ - c_choice = StochasticNode(C, policy[(self.action1, self.action2)], self.depth) - d_choice = StochasticNode(D, policy[(self.action1, self.action2)], self.depth) + c_choice = StochasticNode( + C, policy[(self.action1, self.action2)], self.depth + ) + d_choice = StochasticNode( + D, policy[(self.action1, self.action2)], self.depth + ) return c_choice, d_choice def is_stochastic(self): @@ -401,7 +413,9 @@ def minimax_tree_search(begin_node, policy, max_depth): # The stochastic node value is the expected value of siblings. node_value = begin_node.pC * minimax_tree_search( siblings[0], policy, max_depth - ) + (1 - begin_node.pC) * minimax_tree_search(siblings[1], policy, max_depth) + ) + (1 - begin_node.pC) * minimax_tree_search( + siblings[1], policy, max_depth + ) return node_value else: # Deterministic node if begin_node.depth == max_depth: @@ -433,7 +447,9 @@ def move_gen(outcome, policy, depth_search_tree=5): using tree-search procedure. """ current_node = DeterministicNode(outcome[0], outcome[1], depth=0) - values_of_choices = minimax_tree_search(current_node, policy, depth_search_tree) + values_of_choices = minimax_tree_search( + current_node, policy, depth_search_tree + ) # Returns the Action which correspond to the best choice in terms of # expected value. In case value(C) == value(D), returns C. actions_tuple = (C, D) diff --git a/axelrod/strategies/defector.py b/axelrod/strategies/defector.py index 7b4831443..514e5bed8 100644 --- a/axelrod/strategies/defector.py +++ b/axelrod/strategies/defector.py @@ -54,6 +54,9 @@ def strategy(self, opponent: Player) -> Action: Defect if opponent has cooperated at least once in the past and has defected for the last 3 turns in a row. """ - if opponent.history.cooperations > 0 and opponent.history[-3:] == [D] * 3: + if ( + opponent.history.cooperations > 0 + and opponent.history[-3:] == [D] * 3 + ): return C return D diff --git a/axelrod/strategies/finite_state_machines.py b/axelrod/strategies/finite_state_machines.py index 29fa1a291..ec49ad2f4 100644 --- a/axelrod/strategies/finite_state_machines.py +++ b/axelrod/strategies/finite_state_machines.py @@ -70,7 +70,9 @@ def state_transitions(self) -> dict: return self._state_transitions.copy() def transitions(self) -> list: - return [[x[0], x[1], y[0], y[1]] for x, y in self._state_transitions.items()] + return [ + [x[0], x[1], y[0], y[1]] for x, y in self._state_transitions.items() + ] def move(self, opponent_action: Action) -> Action: """Computes the response move and changes state.""" @@ -112,7 +114,7 @@ def __init__( self, transitions: Tuple[Transition, ...] = ((1, C, 1, C), (1, D, 1, D)), initial_state: int = 1, - initial_action: Action = C + initial_action: Action = C, ) -> None: Player.__init__(self) self.initial_state = initial_state @@ -147,39 +149,62 @@ def __init__( initial_action: Action = None, num_states: int = None, mutation_probability: float = 0.1, - seed: int = None + seed: int = None, ) -> None: """If transitions, initial_state, and initial_action are None then generate random parameters using num_states.""" EvolvablePlayer.__init__(self, seed=seed) - transitions, initial_state, initial_action, num_states = self._normalize_parameters( - transitions, initial_state, initial_action, num_states) + ( + transitions, + initial_state, + initial_action, + num_states, + ) = self._normalize_parameters( + transitions, initial_state, initial_action, num_states + ) FSMPlayer.__init__( self, transitions=transitions, initial_state=initial_state, - initial_action=initial_action) + initial_action=initial_action, + ) self.mutation_probability = mutation_probability self.overwrite_init_kwargs( transitions=transitions, initial_state=initial_state, initial_action=initial_action, - num_states=self.num_states) + num_states=self.num_states, + ) @classmethod - def normalize_transitions(cls, transitions: Sequence[Sequence]) -> Tuple[Tuple[Any, ...], ...]: + def normalize_transitions( + cls, transitions: Sequence[Sequence] + ) -> Tuple[Tuple[Any, ...], ...]: """Translate a list of lists to a tuple of tuples.""" normalized = [] for t in transitions: normalized.append(tuple(t)) return tuple(normalized) - def _normalize_parameters(self, transitions: Tuple = None, initial_state: int = None, initial_action: Action = None, - num_states: int = None) -> Tuple[Tuple, int, Action, int]: - if not ((transitions is not None) and (initial_state is not None) and (initial_action is not None)): + def _normalize_parameters( + self, + transitions: Tuple = None, + initial_state: int = None, + initial_action: Action = None, + num_states: int = None, + ) -> Tuple[Tuple, int, Action, int]: + if not ( + (transitions is not None) + and (initial_state is not None) + and (initial_action is not None) + ): if not num_states: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableFSMPlayer") - transitions, initial_state, initial_action = self.random_params(num_states) + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableFSMPlayer" + ) + transitions, initial_state, initial_action = self.random_params( + num_states + ) transitions = self.normalize_transitions(transitions) num_states = len(transitions) // 2 return transitions, initial_state, initial_action, num_states @@ -188,7 +213,9 @@ def _normalize_parameters(self, transitions: Tuple = None, initial_state: int = def num_states(self) -> int: return self.fsm.num_states() - def random_params(self, num_states: int) -> Tuple[Tuple[Transition, ...], int, Action]: + def random_params( + self, num_states: int + ) -> Tuple[Tuple[Transition, ...], int, Action]: rows = [] for j in range(num_states): for action in actions: @@ -225,10 +252,14 @@ def mutate(self): if self._random.random() < self.mutation_probability / 10: initial_action = self.initial_action.flip() initial_state = self.initial_state - if self._random.random() < self.mutation_probability / (10 * self.num_states): + if self._random.random() < self.mutation_probability / ( + 10 * self.num_states + ): initial_state = self._random.randint(0, self.num_states) try: - transitions = self.mutate_rows(self.fsm.transitions(), self.mutation_probability) + transitions = self.mutate_rows( + self.fsm.transitions(), self.mutation_probability + ) self.fsm = SimpleFSM(transitions, self.initial_state) except ValueError: # If the FSM is malformed, try again. @@ -239,7 +270,9 @@ def mutate(self): initial_action=initial_action, ) - def crossover_rows(self, rows1: List[List], rows2: List[List]) -> List[List]: + def crossover_rows( + self, rows1: List[List], rows2: List[List] + ) -> List[List]: num_states = len(rows1) // 2 cross_point = 2 * self._random.randint(0, num_states) new_rows = copy_lists(rows1[:cross_point]) @@ -248,8 +281,12 @@ def crossover_rows(self, rows1: List[List], rows2: List[List]) -> List[List]: def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") - transitions = self.crossover_rows(self.fsm.transitions(), other.fsm.transitions()) + raise TypeError( + "Crossover must be between the same player classes." + ) + transitions = self.crossover_rows( + self.fsm.transitions(), other.fsm.transitions() + ) transitions = self.normalize_transitions(transitions) return self.create_new(transitions=transitions) @@ -267,22 +304,28 @@ def receive_vector(self, vector): Finally, a probability to determine the player's first move. """ num_states = self.fsm.num_states() - state_scale = vector[:num_states * 2] + state_scale = vector[: num_states * 2] next_states = [int(s * (num_states - 1)) for s in state_scale] - actions = vector[num_states * 2: -1] + actions = vector[num_states * 2 : -1] self.initial_action = C if round(vector[-1]) == 0 else D self.initial_state = 1 transitions = [] - for i, (initial_state, action) in enumerate(itertools.product(range(num_states), [C, D])): + for i, (initial_state, action) in enumerate( + itertools.product(range(num_states), [C, D]) + ): next_action = C if round(actions[i]) == 0 else D - transitions.append([initial_state, action, next_states[i], next_action]) + transitions.append( + [initial_state, action, next_states[i], next_action] + ) transitions = self.normalize_transitions(transitions) self.fsm = SimpleFSM(transitions, self.initial_state) - self.overwrite_init_kwargs(transitions=transitions, - initial_state=self.initial_state, - initial_action=self.initial_action) + self.overwrite_init_kwargs( + transitions=transitions, + initial_state=self.initial_state, + initial_action=self.initial_action, + ) def create_vector_bounds(self): """Creates the bounds for the decision variables.""" diff --git a/axelrod/strategies/gambler.py b/axelrod/strategies/gambler.py index 3e50a76b9..69bf9221b 100644 --- a/axelrod/strategies/gambler.py +++ b/axelrod/strategies/gambler.py @@ -59,7 +59,7 @@ def __init__( pattern: Any = None, # pattern is str or tuple of Actions. parameters: Plays = None, mutation_probability: float = None, - seed: int = None + seed: int = None, ) -> None: EvolvableLookerUp.__init__( self, @@ -68,7 +68,7 @@ def __init__( pattern=pattern, parameters=parameters, mutation_probability=mutation_probability, - seed=seed + seed=seed, ) self.pattern = list(self.pattern) Gambler.__init__( @@ -76,7 +76,7 @@ def __init__( lookup_dict=self.lookup_dict, initial_actions=self.initial_actions, pattern=self.pattern, - parameters=self.parameters + parameters=self.parameters, ) self.overwrite_init_kwargs( lookup_dict=self.lookup_dict, @@ -105,7 +105,9 @@ def receive_vector(self, vector): """Receives a vector and updates the player's pattern. Ignores extra parameters.""" self.pattern = vector self_depth, op_depth, op_openings_depth = self.parameters - self._lookup = LookupTable.from_pattern(self.pattern, self_depth, op_depth, op_openings_depth) + self._lookup = LookupTable.from_pattern( + self.pattern, self_depth, op_depth, op_openings_depth + ) def create_vector_bounds(self): """Creates the bounds for the decision variables. Ignores extra parameters.""" diff --git a/axelrod/strategies/gobymajority.py b/axelrod/strategies/gobymajority.py index 7617b571f..9a7692c5c 100644 --- a/axelrod/strategies/gobymajority.py +++ b/axelrod/strategies/gobymajority.py @@ -60,7 +60,9 @@ def __init__( self.memory = self.classifier["memory_depth"] else: self.memory = 0 - self.name = "Go By Majority" + (self.memory > 0) * (": %i" % self.memory) + self.name = "Go By Majority" + (self.memory > 0) * ( + ": %i" % self.memory + ) if self.soft: self.name = "Soft " + self.name else: diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index a646851af..41d1479c1 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -24,7 +24,7 @@ class Grudger(Player): name = "Grudger" classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "long_run_time": False, "inspects_source": False, diff --git a/axelrod/strategies/hmm.py b/axelrod/strategies/hmm.py index ad64ddb55..8eccef194 100644 --- a/axelrod/strategies/hmm.py +++ b/axelrod/strategies/hmm.py @@ -59,7 +59,11 @@ class SimpleHMM(object): """ def __init__( - self, transitions_C, transitions_D, emission_probabilities, initial_state + self, + transitions_C, + transitions_D, + emission_probabilities, + initial_state, ) -> None: """ Params @@ -133,13 +137,17 @@ def move(self, opponent_action: Action) -> Action: next_state = self._cache_C[self.state] except KeyError: num_states = len(self.emission_probabilities) - next_state = self._random.choice(num_states, 1, p=self.transitions_C[self.state])[0] + next_state = self._random.choice( + num_states, 1, p=self.transitions_C[self.state] + )[0] else: try: next_state = self._cache_D[self.state] except KeyError: num_states = len(self.emission_probabilities) - next_state = self._random.choice(num_states, 1, p=self.transitions_D[self.state])[0] + next_state = self._random.choice( + num_states, 1, p=self.transitions_D[self.state] + )[0] self.state = next_state # Choose action to emit. @@ -178,7 +186,7 @@ def __init__( transitions_D=None, emission_probabilities=None, initial_state=0, - initial_action=C + initial_action=C, ) -> None: Player.__init__(self) if not transitions_C: @@ -189,7 +197,10 @@ def __init__( self.initial_state = initial_state self.initial_action = initial_action self.hmm = SimpleHMM( - copy_lists(transitions_C), copy_lists(transitions_D), list(emission_probabilities), initial_state + copy_lists(transitions_C), + copy_lists(transitions_D), + list(emission_probabilities), + initial_state, ) assert self.hmm.is_well_formed() self.state = self.hmm.state @@ -230,6 +241,7 @@ def set_seed(self, seed=None): class EvolvableHMMPlayer(HMMPlayer, EvolvablePlayer): """Evolvable version of HMMPlayer.""" + name = "EvolvableHMMPlayer" def __init__( @@ -241,18 +253,35 @@ def __init__( initial_action=C, num_states=None, mutation_probability=None, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) - transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability = self._normalize_parameters( - transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability) + ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + num_states, + mutation_probability, + ) = self._normalize_parameters( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + num_states, + mutation_probability, + ) self.mutation_probability = mutation_probability - HMMPlayer.__init__(self, - transitions_C=transitions_C, - transitions_D=transitions_D, - emission_probabilities=emission_probabilities, - initial_state=initial_state, - initial_action=initial_action) + HMMPlayer.__init__( + self, + transitions_C=transitions_C, + transitions_D=transitions_D, + emission_probabilities=emission_probabilities, + initial_state=initial_state, + initial_action=initial_action, + ) self.hmm._random = self._random self.overwrite_init_kwargs( transitions_C=transitions_C, @@ -261,16 +290,35 @@ def __init__( initial_state=initial_state, initial_action=initial_action, num_states=num_states, - mutation_probability=mutation_probability + mutation_probability=mutation_probability, ) - def _normalize_parameters(self, transitions_C=None, transitions_D=None, emission_probabilities=None, - initial_state=None, initial_action=None, num_states=None, mutation_probability=None): - if not ((transitions_C and transitions_D and emission_probabilities) and (initial_state is not None) and (initial_action is not None)): + def _normalize_parameters( + self, + transitions_C=None, + transitions_D=None, + emission_probabilities=None, + initial_state=None, + initial_action=None, + num_states=None, + mutation_probability=None, + ): + if not ( + (transitions_C and transitions_D and emission_probabilities) + and (initial_state is not None) + and (initial_action is not None) + ): if not num_states: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableHMMPlayer") - transitions_C, transitions_D, emission_probabilities, initial_state, initial_action = self.random_params( - num_states) + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableHMMPlayer" + ) + ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + ) = self.random_params(num_states) # Normalize types of various matrices for m in [transitions_C, transitions_D]: for i in range(len(m)): @@ -281,7 +329,15 @@ def _normalize_parameters(self, transitions_C=None, transitions_D=None, emission mutation_probability = 10 / (num_states ** 2) else: mutation_probability = mutation_probability - return transitions_C, transitions_D, emission_probabilities, initial_state, initial_action, num_states, mutation_probability + return ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + num_states, + mutation_probability, + ) def random_params(self, num_states): transitions_C = [] @@ -293,7 +349,13 @@ def random_params(self, num_states): emission_probabilities.append(self._random.random()) initial_state = self._random.randint(0, num_states) initial_action = C - return transitions_C, transitions_D, emission_probabilities, initial_state, initial_action + return ( + transitions_C, + transitions_D, + emission_probabilities, + initial_state, + initial_action, + ) @property def num_states(self): @@ -307,16 +369,23 @@ def mutate_rows(self, rows, mutation_probability): def mutate(self): transitions_C = self.mutate_rows( - self.hmm.transitions_C, self.mutation_probability) + self.hmm.transitions_C, self.mutation_probability + ) transitions_D = self.mutate_rows( - self.hmm.transitions_D, self.mutation_probability) + self.hmm.transitions_D, self.mutation_probability + ) emission_probabilities = mutate_row( - self.hmm.emission_probabilities, self.mutation_probability, self._random) + self.hmm.emission_probabilities, + self.mutation_probability, + self._random, + ) initial_action = self.initial_action if self._random.random() < self.mutation_probability / 10: initial_action = self.initial_action.flip() initial_state = self.initial_state - if self._random.random() < self.mutation_probability / (10 * self.num_states): + if self._random.random() < self.mutation_probability / ( + 10 * self.num_states + ): initial_state = self._random.randint(0, self.num_states) return self.create_new( transitions_C=transitions_C, @@ -328,15 +397,24 @@ def mutate(self): def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") - transitions_C = crossover_lists(self.hmm.transitions_C, other.hmm.transitions_C, self._random) - transitions_D = crossover_lists(self.hmm.transitions_D, other.hmm.transitions_D, self._random) + raise TypeError( + "Crossover must be between the same player classes." + ) + transitions_C = crossover_lists( + self.hmm.transitions_C, other.hmm.transitions_C, self._random + ) + transitions_D = crossover_lists( + self.hmm.transitions_D, other.hmm.transitions_D, self._random + ) emission_probabilities = crossover_lists( - self.hmm.emission_probabilities, other.hmm.emission_probabilities, self._random) + self.hmm.emission_probabilities, + other.hmm.emission_probabilities, + self._random, + ) return self.create_new( transitions_C=transitions_C, transitions_D=transitions_D, - emission_probabilities=emission_probabilities + emission_probabilities=emission_probabilities, ) def receive_vector(self, vector): @@ -353,12 +431,12 @@ class with self.num_states. entry is the initial_action. """ - assert(len(vector) == 2 * self.num_states ** 2 + self.num_states + 1) + assert len(vector) == 2 * self.num_states ** 2 + self.num_states + 1 def deserialize(vector): matrix = [] for i in range(self.num_states): - row = vector[self.num_states * i: self.num_states * (i + 1)] + row = vector[self.num_states * i : self.num_states * (i + 1)] row = normalize_vector(row) matrix.append(row) return matrix @@ -371,7 +449,7 @@ def deserialize(vector): deserialize(vector[0:break_tc]), deserialize(vector[break_tc:break_td]), normalize_vector(vector[break_td:break_ep]), - initial_state + initial_state, ) self.initial_action = C if round(vector[-1]) == 0 else D self.initial_state = initial_state diff --git a/axelrod/strategies/human.py b/axelrod/strategies/human.py index b3a0e9564..20d921782 100644 --- a/axelrod/strategies/human.py +++ b/axelrod/strategies/human.py @@ -36,7 +36,9 @@ def validate(self, document) -> None: text = document.text if text and text.upper() not in ["C", "D"]: - raise ValidationError(message="Action must be C or D", cursor_position=0) + raise ValidationError( + message="Action must be C or D", cursor_position=0 + ) class Human(Player): @@ -83,10 +85,14 @@ def _history_toolbar(self): Described at http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-a-bottom-toolbar """ my_history = [self.symbols[action] for action in self.history] - opponent_history = [self.symbols[action] for action in self.history.coplays] + opponent_history = [ + self.symbols[action] for action in self.history.coplays + ] history = list(zip(my_history, opponent_history)) if self.history: - content = "History ({}, opponent): {}".format(self.human_name, history) + content = "History ({}, opponent): {}".format( + self.human_name, history + ) else: content = "" return content diff --git a/axelrod/strategies/hunter.py b/axelrod/strategies/hunter.py index 51c6968d7..e53ead1f3 100644 --- a/axelrod/strategies/hunter.py +++ b/axelrod/strategies/hunter.py @@ -26,7 +26,10 @@ class DefectorHunter(Player): } def strategy(self, opponent: Player) -> Action: - if len(self.history) >= 4 and len(opponent.history) == opponent.defections: + if ( + len(self.history) >= 4 + and len(opponent.history) == opponent.defections + ): return D return C @@ -50,7 +53,10 @@ class CooperatorHunter(Player): } def strategy(self, opponent: Player) -> Action: - if len(self.history) >= 4 and len(opponent.history) == opponent.cooperations: + if ( + len(self.history) >= 4 + and len(opponent.history) == opponent.cooperations + ): return D return C @@ -244,6 +250,8 @@ def strategy(self, opponent: Player) -> Action: probabilities.append(self.countCC / self.cooperations) if self.defections > 5: probabilities.append(self.countDD / self.defections) - if probabilities and all([abs(p - 0.5) < 0.25 for p in probabilities]): + if probabilities and all( + [abs(p - 0.5) < 0.25 for p in probabilities] + ): return D return C diff --git a/axelrod/strategies/lookerup.py b/axelrod/strategies/lookerup.py index 1f57dc685..8bbed1e60 100644 --- a/axelrod/strategies/lookerup.py +++ b/axelrod/strategies/lookerup.py @@ -101,7 +101,11 @@ def _raise_error_for_bad_lookup_dict(self): @classmethod def from_pattern( - cls, pattern: tuple, player_depth: int, op_depth: int, op_openings_depth: int + cls, + pattern: tuple, + player_depth: int, + op_depth: int, + op_openings_depth: int, ): keys = create_lookup_table_keys( player_depth=player_depth, @@ -151,7 +155,9 @@ def display( """ def sorter(plays): - return tuple(actions_to_str(getattr(plays, field) for field in sort_by)) + return tuple( + actions_to_str(getattr(plays, field) for field in sort_by) + ) col_width = 11 sorted_keys = sorted(self._dict, key=sorter) @@ -318,7 +324,7 @@ def __init__( lookup_dict: dict = None, initial_actions: tuple = None, pattern: Any = None, # pattern is str or tuple of Action's. - parameters: Plays = None + parameters: Plays = None, ) -> None: Player.__init__(self) @@ -404,11 +410,21 @@ def __init__( pattern: Any = None, # pattern is str or tuple of Action's. parameters: Plays = None, mutation_probability: float = None, - seed: int = None + seed: int = None, ) -> None: EvolvablePlayer.__init__(self, seed=seed) - lookup_dict, initial_actions, pattern, parameters, mutation_probability = self._normalize_parameters( - lookup_dict, initial_actions, pattern, parameters, mutation_probability + ( + lookup_dict, + initial_actions, + pattern, + parameters, + mutation_probability, + ) = self._normalize_parameters( + lookup_dict, + initial_actions, + pattern, + parameters, + mutation_probability, ) LookerUp.__init__( self, @@ -426,30 +442,50 @@ def __init__( mutation_probability=mutation_probability, ) - def _normalize_parameters(self, lookup_dict=None, initial_actions=None, pattern=None, parameters=None, - mutation_probability=None): + def _normalize_parameters( + self, + lookup_dict=None, + initial_actions=None, + pattern=None, + parameters=None, + mutation_probability=None, + ): if lookup_dict and initial_actions: # Compute the associated pattern and parameters # Map the table keys to namedTuple Plays - lookup_table = self._get_lookup_table(lookup_dict, pattern, parameters) + lookup_table = self._get_lookup_table( + lookup_dict, pattern, parameters + ) lookup_dict = lookup_table.dictionary - parameters = (lookup_table.player_depth, lookup_table.op_depth, lookup_table.op_openings_depth) + parameters = ( + lookup_table.player_depth, + lookup_table.op_depth, + lookup_table.op_openings_depth, + ) pattern = tuple(v for k, v in sorted(lookup_dict.items())) elif pattern and parameters and initial_actions: # Compute the associated lookup table plays, op_plays, op_start_plays = parameters - lookup_table = self._get_lookup_table(lookup_dict, pattern, parameters) + lookup_table = self._get_lookup_table( + lookup_dict, pattern, parameters + ) lookup_dict = lookup_table.dictionary elif parameters: # Generate a random pattern and (maybe) initial actions plays, op_plays, op_start_plays = parameters - pattern, lookup_table = self.random_params(plays, op_plays, op_start_plays) + pattern, lookup_table = self.random_params( + plays, op_plays, op_start_plays + ) lookup_dict = lookup_table.dictionary if not initial_actions: num_actions = max([plays, op_plays, op_start_plays]) - initial_actions = tuple([self._random.choice((C, D)) for _ in range(num_actions)]) + initial_actions = tuple( + [self._random.choice((C, D)) for _ in range(num_actions)] + ) else: - raise InsufficientParametersError("Insufficient Parameters to instantiate EvolvableLookerUp") + raise InsufficientParametersError( + "Insufficient Parameters to instantiate EvolvableLookerUp" + ) # Normalize pattern if isinstance(pattern, str): pattern = str_to_actions(pattern) @@ -457,8 +493,14 @@ def _normalize_parameters(self, lookup_dict=None, initial_actions=None, pattern= if mutation_probability is None: plays, op_plays, op_start_plays = parameters keys = create_lookup_table_keys(plays, op_plays, op_start_plays) - mutation_probability = 2. / len(keys) - return lookup_dict, initial_actions, pattern, parameters, mutation_probability + mutation_probability = 2.0 / len(keys) + return ( + lookup_dict, + initial_actions, + pattern, + parameters, + mutation_probability, + ) def random_value(self): return self._random.choice(actions) @@ -483,7 +525,9 @@ def mutate_table(self, table, mutation_probability): return table def mutate(self): - lookup_dict = self.mutate_table(self.lookup_dict, self.mutation_probability) + lookup_dict = self.mutate_table( + self.lookup_dict, self.mutation_probability + ) # Add in starting moves initial_actions = list(self.initial_actions) for i in range(len(initial_actions)): @@ -491,14 +535,17 @@ def mutate(self): if r < self.mutation_probability: initial_actions[i] = initial_actions[i].flip() return self.create_new( - lookup_dict=lookup_dict, - initial_actions=tuple(initial_actions), + lookup_dict=lookup_dict, initial_actions=tuple(initial_actions), ) def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") - lookup_dict = crossover_dictionaries(self.lookup_dict, other.lookup_dict, self._random) + raise TypeError( + "Crossover must be between the same player classes." + ) + lookup_dict = crossover_dictionaries( + self.lookup_dict, other.lookup_dict, self._random + ) return self.create_new(lookup_dict=lookup_dict) @@ -515,7 +562,9 @@ class EvolvedLookerUp1_1_1(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=1, op_openings=1) - super().__init__(parameters=params, pattern="CDDDDCDD", initial_actions=(C,)) + super().__init__( + parameters=params, pattern="CDDDDCDD", initial_actions=(C,) + ) class EvolvedLookerUp2_2_2(LookerUp): @@ -531,8 +580,12 @@ class EvolvedLookerUp2_2_2(LookerUp): def __init__(self) -> None: params = Plays(self_plays=2, op_plays=2, op_openings=2) - pattern = "CDDCDCDDCDDDCDDDDDCDCDCCCDDCCDCDDDCCCCCDDDCDDDDDDDDDCCDDCDDDCCCD" - super().__init__(parameters=params, pattern=pattern, initial_actions=(C, C)) + pattern = ( + "CDDCDCDDCDDDCDDDDDCDCDCCCDDCCDCDDDCCCCCDDDCDDDDDDDDDCCDDCDDDCCCD" + ) + super().__init__( + parameters=params, pattern=pattern, initial_actions=(C, C) + ) class Winner12(LookerUp): @@ -549,7 +602,9 @@ class Winner12(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=2, op_openings=0) pattern = "CDCDDCDD" - super().__init__(parameters=params, pattern=pattern, initial_actions=(C, C)) + super().__init__( + parameters=params, pattern=pattern, initial_actions=(C, C) + ) class Winner21(LookerUp): @@ -566,7 +621,9 @@ class Winner21(LookerUp): def __init__(self) -> None: params = Plays(self_plays=1, op_plays=2, op_openings=0) pattern = "CDCDCDDD" - super().__init__(parameters=params, pattern=pattern, initial_actions=(D, C)) + super().__init__( + parameters=params, pattern=pattern, initial_actions=(D, C) + ) def get_last_n_plays(player: Player, depth: int) -> tuple: diff --git a/axelrod/strategies/memoryone.py b/axelrod/strategies/memoryone.py index a8ad1c571..7773cd63f 100644 --- a/axelrod/strategies/memoryone.py +++ b/axelrod/strategies/memoryone.py @@ -66,7 +66,9 @@ class MemoryOnePlayer(Player): } def __init__( - self, four_vector: Tuple[float, float, float, float] = None, initial: Action = C + self, + four_vector: Tuple[float, float, float, float] = None, + initial: Action = C, ) -> None: """ Parameters @@ -109,7 +111,9 @@ def set_four_vector(self, four_vector: Tuple[float, float, float, float]): "An element in the probability vector, {}, is not " "between 0 and 1.".format(str(four_vector)) ) - self._four_vector = dict(zip([(C, C), (C, D), (D, C), (D, D)], four_vector)) + self._four_vector = dict( + zip([(C, C), (C, D), (D, C), (D, D)], four_vector) + ) def _post_init(self): # Adjust classifiers diff --git a/axelrod/strategies/memorytwo.py b/axelrod/strategies/memorytwo.py index 9573c308f..43b5e539d 100644 --- a/axelrod/strategies/memorytwo.py +++ b/axelrod/strategies/memorytwo.py @@ -53,7 +53,9 @@ class MemoryTwoPlayer(Player): } def __init__( - self, sixteen_vector: Optional[Tuple[float, ...]] = None, initial: Optional[Tuple[Action, Action]] = None + self, + sixteen_vector: Optional[Tuple[float, ...]] = None, + initial: Optional[Tuple[Action, Action]] = None, ) -> None: """ Parameters @@ -69,7 +71,9 @@ def __init__( self._initial = initial self.set_initial_sixteen_vector(sixteen_vector) - def set_initial_sixteen_vector(self, sixteen_vector: Optional[Tuple[float, ...]]): + def set_initial_sixteen_vector( + self, sixteen_vector: Optional[Tuple[float, ...]] + ): if sixteen_vector is None: sixteen_vector = tuple([1] * 16) warnings.warn("Memory two player is set to default, Cooperator.") @@ -84,7 +88,8 @@ def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]): ) states = [ - (hist[:2], hist[2:]) for hist in list(itertools.product((C, D), repeat=4)) + (hist[:2], hist[2:]) + for hist in list(itertools.product((C, D), repeat=4)) ] self._sixteen_vector = dict( @@ -92,7 +97,9 @@ def set_sixteen_vector(self, sixteen_vector: Tuple[float, ...]): ) # type: Dict[tuple, float] @staticmethod - def compute_memory_depth(sixteen_vector: Dict[Tuple[Action, Action], float]) -> int: + def compute_memory_depth( + sixteen_vector: Dict[Tuple[Action, Action], float] + ) -> int: values = set(list(sixteen_vector.values())) # Memory-depth 0 @@ -119,7 +126,9 @@ def compute_memory_depth(sixteen_vector: Dict[Tuple[Action, Action], float]) -> def _post_init(self): values = set(self._sixteen_vector.values()) self.classifier["stochastic"] = any(0 < x < 1 for x in values) - self.classifier["memory_depth"] = self.compute_memory_depth(self._sixteen_vector) + self.classifier["memory_depth"] = self.compute_memory_depth( + self._sixteen_vector + ) def strategy(self, opponent: Player) -> Action: turn = len(self.history) @@ -259,7 +268,11 @@ class MEM2(Player): def __init__(self) -> None: super().__init__() - self.players = {"TFT": TitForTat(), "TFTT": TitFor2Tats(), "ALLD": Defector()} + self.players = { + "TFT": TitForTat(), + "TFTT": TitFor2Tats(), + "ALLD": Defector(), + } self.play_as = "TFT" self.shift_counter = 3 self.alld_counter = 0 diff --git a/axelrod/strategies/meta.py b/axelrod/strategies/meta.py index 5f15b340e..b27907529 100644 --- a/axelrod/strategies/meta.py +++ b/axelrod/strategies/meta.py @@ -104,7 +104,9 @@ def update_histories(self, coplay): # As a sanity check, look for at least one reclassifier, otherwise # this try-except clause could hide a bug. if len(self._reclassifiers) == 0: - raise TypeError("MetaClass update_histories issue, expected a reclassifier.") + raise TypeError( + "MetaClass update_histories issue, expected a reclassifier." + ) # Otherwise just update with C always, so at least the histories have the # expected length. for player in self.team: @@ -234,7 +236,9 @@ def _post_init(self): # If the team has repeated identical members, then it reduces to a singular team # and it may not actually be stochastic. if team and len(set(team)) == 1: - self.classifier["stochastic"] = Classifiers["stochastic"](self.team[0]) + self.classifier["stochastic"] = Classifiers["stochastic"]( + self.team[0] + ) self.singular = True def meta_strategy(self, results, opponent): @@ -551,7 +555,9 @@ def _post_init(self): if distribution and len(set(distribution)) > 1: self.classifier["stochastic"] = True if len(self.team) == 1: - self.classifier["stochastic"] = Classifiers["stochastic"](self.team[0]) + self.classifier["stochastic"] = Classifiers["stochastic"]( + self.team[0] + ) # Overwrite strategy to avoid use of _random. This will ignore self.meta_strategy. self.index = 0 self.strategy = self.index_strategy @@ -564,7 +570,9 @@ def _post_init(self): if 1 in distribution: self.index = list(distribution).index(1) # It's potentially deterministic. - self.classifier["stochastic"] = Classifiers["stochastic"](self.team[self.index]) + self.classifier["stochastic"] = Classifiers["stochastic"]( + self.team[self.index] + ) # Overwrite strategy to avoid use of _random. This will ignore self.meta_strategy. self.strategy = self.index_strategy diff --git a/axelrod/strategies/prober.py b/axelrod/strategies/prober.py index 0f5293c61..dcfb828a1 100644 --- a/axelrod/strategies/prober.py +++ b/axelrod/strategies/prober.py @@ -80,7 +80,7 @@ def strategy(self, opponent: Player) -> Action: return self.initial_actions[hist_size] if D not in opponent.history[:init_size]: return D - return opponent.history[-1] # TFT + return opponent.history[-1] # TFT class Prober(Player): diff --git a/axelrod/strategies/punisher.py b/axelrod/strategies/punisher.py index 262901675..f976bf370 100644 --- a/axelrod/strategies/punisher.py +++ b/axelrod/strategies/punisher.py @@ -51,7 +51,9 @@ def strategy(self, opponent: Player) -> Action: return D elif D in opponent.history[-1:]: - self.mem_length = (opponent.defections * 20) // len(opponent.history) + self.mem_length = (opponent.defections * 20) // len( + opponent.history + ) self.grudged = True return D @@ -101,7 +103,9 @@ def strategy(self, opponent: Player) -> Action: self.grudge_memory += 1 return D elif D in opponent.history[-1:]: - self.mem_length = (opponent.cooperations * 20) // len(opponent.history) + self.mem_length = (opponent.cooperations * 20) // len( + opponent.history + ) if self.mem_length == 0: self.mem_length += 1 self.grudged = True diff --git a/axelrod/strategies/qlearner.py b/axelrod/strategies/qlearner.py index ef2819e01..1a81ae227 100644 --- a/axelrod/strategies/qlearner.py +++ b/axelrod/strategies/qlearner.py @@ -65,7 +65,9 @@ def strategy(self, opponent: Player) -> Action: if state not in self.Qs: self.Qs[state] = OrderedDict(zip([C, D], [0, 0])) self.Vs[state] = 0 - self.perform_q_learning(self.prev_state, state, self.prev_action, reward) + self.perform_q_learning( + self.prev_state, state, self.prev_action, reward + ) action = self.select_action(state) self.prev_state = state self.prev_action = action @@ -90,16 +92,22 @@ def find_state(self, opponent: Player) -> str: action_str = actions_to_str(opponent.history[-self.memory_length :]) return action_str + prob - def perform_q_learning(self, prev_state: str, state: str, action: Action, reward): + def perform_q_learning( + self, prev_state: str, state: str, action: Action, reward + ): """ Performs the qlearning algorithm """ - self.Qs[prev_state][action] = (1.0 - self.learning_rate) * self.Qs[prev_state][ - action - ] + self.learning_rate * (reward + self.discount_rate * self.Vs[state]) + self.Qs[prev_state][action] = (1.0 - self.learning_rate) * self.Qs[ + prev_state + ][action] + self.learning_rate * ( + reward + self.discount_rate * self.Vs[state] + ) self.Vs[prev_state] = max(self.Qs[prev_state].values()) - def find_reward(self, opponent: Player) -> Dict[Action, Dict[Action, Score]]: + def find_reward( + self, opponent: Player + ) -> Dict[Action, Dict[Action, Score]]: """ Finds the reward gained on the last iteration """ diff --git a/axelrod/strategies/shortmem.py b/axelrod/strategies/shortmem.py index f110f56b8..e1cd7a6fb 100644 --- a/axelrod/strategies/shortmem.py +++ b/axelrod/strategies/shortmem.py @@ -22,7 +22,7 @@ class ShortMem(Player): name = "ShortMem" classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "long_run_time": False, "inspects_source": False, diff --git a/axelrod/strategies/titfortat.py b/axelrod/strategies/titfortat.py index 31c20b639..d3ab2d68d 100644 --- a/axelrod/strategies/titfortat.py +++ b/axelrod/strategies/titfortat.py @@ -1,6 +1,9 @@ from axelrod.action import Action, actions_to_str from axelrod.player import Player -from axelrod.strategy_transformers import FinalTransformer, TrackHistoryTransformer +from axelrod.strategy_transformers import ( + FinalTransformer, + TrackHistoryTransformer, +) C, D = Action.C, Action.D @@ -128,7 +131,9 @@ def strategy(self, opponent): return C if D in opponent.history[-2:]: # Probability of cooperating regardless - return self._random.random_choice(opponent.cooperations / len(opponent.history)) + return self._random.random_choice( + opponent.cooperations / len(opponent.history) + ) else: return C diff --git a/axelrod/strategies/zero_determinant.py b/axelrod/strategies/zero_determinant.py index 7b849eadb..89852ae20 100644 --- a/axelrod/strategies/zero_determinant.py +++ b/axelrod/strategies/zero_determinant.py @@ -151,7 +151,9 @@ class ZDExtort2v2(LRPlayer): name = "ZD-Extort-2 v2" - def __init__(self, phi: float = 1 / 8, s: float = 0.5, l: float = 1) -> None: + def __init__( + self, phi: float = 1 / 8, s: float = 0.5, l: float = 1 + ) -> None: super().__init__(phi, s, l) @@ -168,7 +170,9 @@ class ZDExtort3(LRPlayer): name = "ZD-Extort3" - def __init__(self, phi: float = 3 / 26, s: float = 1 / 3, l: float = 1) -> None: + def __init__( + self, phi: float = 3 / 26, s: float = 1 / 3, l: float = 1 + ) -> None: super().__init__(phi, s, l) @@ -185,7 +189,9 @@ class ZDExtort4(LRPlayer): name = "ZD-Extort-4" - def __init__(self, phi: float = 4 / 17, s: float = 0.25, l: float = 1) -> None: + def __init__( + self, phi: float = 4 / 17, s: float = 0.25, l: float = 1 + ) -> None: super().__init__(phi, s, l) @@ -200,7 +206,9 @@ class ZDGen2(LRPlayer): name = "ZD-GEN-2" - def __init__(self, phi: float = 1 / 8, s: float = 0.5, l: float = 3) -> None: + def __init__( + self, phi: float = 1 / 8, s: float = 0.5, l: float = 3 + ) -> None: super().__init__(phi, s, l) @@ -251,5 +259,7 @@ class ZDSet2(LRPlayer): name = "ZD-SET-2" - def __init__(self, phi: float = 1 / 4, s: float = 0.0, l: float = 2) -> None: + def __init__( + self, phi: float = 1 / 4, s: float = 0.0, l: float = 2 + ) -> None: super().__init__(phi, s, l) diff --git a/axelrod/strategy_transformers.py b/axelrod/strategy_transformers.py index a7297382d..70773ff60 100644 --- a/axelrod/strategy_transformers.py +++ b/axelrod/strategy_transformers.py @@ -107,20 +107,30 @@ def __call__(self, PlayerClass): reclassifiers = PlayerClass._reclassifiers.copy() if reclassifier is not None: - reclassifiers.append((makes_use_of_reclassifier, (PlayerClass, strategy_wrapper), {})) + reclassifiers.append( + ( + makes_use_of_reclassifier, + (PlayerClass, strategy_wrapper), + {}, + ) + ) # This one is second on the assumption that the wrapper reclassifier knows best. reclassifiers.append((reclassifier, args, kwargs)) # First handle the case where the strategy method is static. if is_strategy_static(PlayerClass): + def inner_strategy(self, opponent): return PlayerClass.strategy(opponent) + else: + def inner_strategy(self, opponent): return PlayerClass.strategy(self, opponent) # For the dual wrapper, we flip the history before and after the transform. if strategy_wrapper == dual_wrapper: + def dual_inner_strategy(self, opponent): """The dual wrapper requires flipping the history. It may be more efficient to use a custom History class that tracks a flipped history and swaps labels.""" @@ -128,16 +138,19 @@ def dual_inner_strategy(self, opponent): proposed_action = inner_strategy(self, opponent) self._history = self.history.flip_plays() return proposed_action + outer_strategy = dual_inner_strategy # For the JossAnn transformer, we want to avoid calling the wrapped strategy, # in the cases where it is unnecessary, to avoid affecting stochasticity. elif strategy_wrapper == joss_ann_wrapper: + def joss_ann_inner_strategy(self, opponent): if not self.classifier["stochastic"]: proposed_action = C else: proposed_action = inner_strategy(self, opponent) return proposed_action + outer_strategy = joss_ann_inner_strategy else: outer_strategy = inner_strategy @@ -727,13 +740,15 @@ def __call__(self, player, opponent, action, retaliations): def retailiation_reclassifier(original_classifier, retaliations): if retaliations > 0: original_classifier["memory_depth"] = max( - retaliations, original_classifier["memory_depth"]) + retaliations, original_classifier["memory_depth"] + ) return original_classifier RetaliationTransformer = StrategyTransformerFactory( - RetaliationWrapper(), name_prefix="Retaliating", - reclassifier=retailiation_reclassifier + RetaliationWrapper(), + name_prefix="Retaliating", + reclassifier=retailiation_reclassifier, ) @@ -751,11 +766,13 @@ def __call__(self, player, opponent, action): def rua_reclassifier(original_classifier): original_classifier["memory_depth"] = max( - 1, original_classifier["memory_depth"]) + 1, original_classifier["memory_depth"] + ) return original_classifier RetaliateUntilApologyTransformer = StrategyTransformerFactory( - RetaliationUntilApologyWrapper(), name_prefix="RUA", - reclassifier=rua_reclassifier + RetaliationUntilApologyWrapper(), + name_prefix="RUA", + reclassifier=rua_reclassifier, ) diff --git a/axelrod/tests/integration/test_filtering.py b/axelrod/tests/integration/test_filtering.py index 43226caf6..4f3b57453 100644 --- a/axelrod/tests/integration/test_filtering.py +++ b/axelrod/tests/integration/test_filtering.py @@ -36,7 +36,9 @@ def test_boolean_filtering(self, strategies): for classifier in classifiers: comprehension = set(filter(axl.Classifiers[classifier], strategies)) filterset = {classifier: True} - filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) + filtered = set( + axl.filtered_strategies(filterset, strategies=strategies) + ) self.assertEqual(comprehension, filtered) @given( @@ -90,7 +92,9 @@ def test_memory_depth_filtering( ] ) filterset = {"memory_depth": memory_depth} - filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) + filtered = set( + axl.filtered_strategies(filterset, strategies=strategies) + ) self.assertEqual(comprehension, filtered) @given(strategies=strategy_lists(min_size=20, max_size=20)) @@ -106,12 +110,16 @@ def test_makes_use_of_filtering(self, strategies): [ s for s in strategies - if set(classifier).issubset(set(axl.Classifiers["makes_use_of"](s))) + if set(classifier).issubset( + set(axl.Classifiers["makes_use_of"](s)) + ) ] ) filterset = {"makes_use_of": classifier} - filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) + filtered = set( + axl.filtered_strategies(filterset, strategies=strategies) + ) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) diff --git a/axelrod/tests/integration/test_matches.py b/axelrod/tests/integration/test_matches.py index d46ded024..46f4e318f 100644 --- a/axelrod/tests/integration/test_matches.py +++ b/axelrod/tests/integration/test_matches.py @@ -9,10 +9,14 @@ C, D = axl.Action.C, axl.Action.D deterministic_strategies = [ - s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s()) + s + for s in axl.short_run_time_strategies + if not axl.Classifiers["stochastic"](s()) ] stochastic_strategies = [ - s for s in axl.short_run_time_strategies if axl.Classifiers["stochastic"](s()) + s + for s in axl.short_run_time_strategies + if axl.Classifiers["stochastic"](s()) ] diff --git a/axelrod/tests/integration/test_names.py b/axelrod/tests/integration/test_names.py index 56061b959..ad4ff85af 100644 --- a/axelrod/tests/integration/test_names.py +++ b/axelrod/tests/integration/test_names.py @@ -5,7 +5,7 @@ class TestNames(unittest.TestCase): def test_all_strategies_have_names(self): - names = [s.name for s in axl.all_strategies if s.name != 'Player'] + names = [s.name for s in axl.all_strategies if s.name != "Player"] self.assertEqual(len(names), len(axl.all_strategies)) def test_all_names_are_unique(self): diff --git a/axelrod/tests/integration/test_tournament.py b/axelrod/tests/integration/test_tournament.py index d2d26cf56..8568e165d 100644 --- a/axelrod/tests/integration/test_tournament.py +++ b/axelrod/tests/integration/test_tournament.py @@ -51,7 +51,9 @@ def test_big_tournaments(self, tournament): path = pathlib.Path("test_outputs/test_tournament.csv") filename = axl_filename(path) self.assertIsNone( - tournament.play(progress_bar=False, filename=filename, build_results=False) + tournament.play( + progress_bar=False, filename=filename, build_results=False + ) ) def test_serial_play(self): @@ -94,9 +96,13 @@ def test_repeat_tournament_deterministic(self): turns=2, repetitions=2, ) - path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_)) + path = pathlib.Path( + "test_outputs/stochastic_tournament_{}.csv".format(_) + ) files.append(axl_filename(path)) - tournament.play(progress_bar=False, filename=files[-1], build_results=False) + tournament.play( + progress_bar=False, filename=files[-1], build_results=False + ) self.assertTrue(filecmp.cmp(files[0], files[1])) def test_repeat_tournament_stochastic(self): @@ -116,11 +122,15 @@ def test_repeat_tournament_stochastic(self): game=self.game, turns=2, repetitions=2, - seed=17 + seed=17, + ) + path = pathlib.Path( + "test_outputs/stochastic_tournament_{}.csv".format(_) ) - path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_)) files.append(axl_filename(path)) - tournament.play(progress_bar=False, filename=files[-1], build_results=False) + tournament.play( + progress_bar=False, filename=files[-1], build_results=False + ) self.assertTrue(filecmp.cmp(files[0], files[1])) @@ -162,7 +172,9 @@ def test_matches_have_different_length(self): p2 = axl.Cooperator() p3 = axl.Cooperator() players = [p1, p2, p3] - tournament = axl.Tournament(players, prob_end=0.5, repetitions=2, seed=3) + tournament = axl.Tournament( + players, prob_end=0.5, repetitions=2, seed=3 + ) results = tournament.play(progress_bar=False) # Check that match length are different across the repetitions self.assertNotEqual(results.match_lengths[0], results.match_lengths[1]) diff --git a/axelrod/tests/property.py b/axelrod/tests/property.py index c3dc009cc..d5d1919d9 100644 --- a/axelrod/tests/property.py +++ b/axelrod/tests/property.py @@ -4,12 +4,21 @@ import itertools import axelrod as axl -from hypothesis.strategies import composite, floats, integers, lists, sampled_from +from hypothesis.strategies import ( + composite, + floats, + integers, + lists, + sampled_from, +) @composite def strategy_lists( - draw, strategies=axl.short_run_time_strategies, min_size=1, max_size=len(axl.strategies) + draw, + strategies=axl.short_run_time_strategies, + min_size=1, + max_size=len(axl.strategies), ): """ A hypothesis decorator to return a list of strategies @@ -106,14 +115,20 @@ def tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] turns = draw(integers(min_value=min_turns, max_value=max_turns)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) - tournament = axl.Tournament(players, turns=turns, repetitions=repetitions, noise=noise) + tournament = axl.Tournament( + players, turns=turns, repetitions=repetitions, noise=noise + ) return tournament @@ -129,7 +144,7 @@ def prob_end_tournaments( max_noise=1, min_repetitions=1, max_repetitions=20, - seed=None + seed=None, ): """ A hypothesis decorator to return a tournament, @@ -158,15 +173,23 @@ def prob_end_tournaments( Random seed """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] prob_end = draw(floats(min_value=min_prob_end, max_value=max_prob_end)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=repetitions, noise=noise, seed=seed + players, + prob_end=prob_end, + repetitions=repetitions, + noise=noise, + seed=seed, ) return tournament @@ -209,29 +232,30 @@ def spatial_tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] player_indices = list(range(len(players))) all_potential_edges = list(itertools.combinations(player_indices, 2)) all_potential_edges.extend([(i, i) for i in player_indices]) # Loops - edges = draw( - lists( - sampled_from(all_potential_edges), - unique=True, - ) - ) + edges = draw(lists(sampled_from(all_potential_edges), unique=True,)) # Ensure all players/nodes are connected: node_indices = sorted(set([node for edge in edges for node in edge])) - missing_nodes = [index for index in player_indices if index not in node_indices] + missing_nodes = [ + index for index in player_indices if index not in node_indices + ] for index in missing_nodes: opponent = draw(sampled_from(player_indices)) edges.append((index, opponent)) turns = draw(integers(min_value=min_turns, max_value=max_turns)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( @@ -278,33 +302,38 @@ def prob_end_spatial_tournaments( The maximum number of repetitions """ strategies = draw( - strategy_lists(strategies=strategies, min_size=min_size, max_size=max_size) + strategy_lists( + strategies=strategies, min_size=min_size, max_size=max_size + ) ) players = [s() for s in strategies] player_indices = list(range(len(players))) all_potential_edges = list(itertools.combinations(player_indices, 2)) all_potential_edges.extend([(i, i) for i in player_indices]) # Loops - edges = draw( - lists( - sampled_from(all_potential_edges), - unique=True, - ) - ) + edges = draw(lists(sampled_from(all_potential_edges), unique=True,)) # Ensure all players/nodes are connected: node_indices = sorted(set([node for edge in edges for node in edge])) - missing_nodes = [index for index in player_indices if index not in node_indices] + missing_nodes = [ + index for index in player_indices if index not in node_indices + ] for index in missing_nodes: opponent = draw(sampled_from(player_indices)) edges.append((index, opponent)) prob_end = draw(floats(min_value=min_prob_end, max_value=max_prob_end)) - repetitions = draw(integers(min_value=min_repetitions, max_value=max_repetitions)) + repetitions = draw( + integers(min_value=min_repetitions, max_value=max_repetitions) + ) noise = draw(floats(min_value=min_noise, max_value=max_noise)) tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=repetitions, noise=noise, edges=edges + players, + prob_end=prob_end, + repetitions=repetitions, + noise=noise, + edges=edges, ) return tournament diff --git a/axelrod/tests/strategies/test_adaptive.py b/axelrod/tests/strategies/test_adaptive.py index 20befc067..330195ca8 100644 --- a/axelrod/tests/strategies/test_adaptive.py +++ b/axelrod/tests/strategies/test_adaptive.py @@ -54,7 +54,9 @@ def test_scoring_with_default_game(self): opponent = axl.Cooperator() attrs = {"scores": {C: 3, D: 0}} expected_actions = list(zip([C, C], [C, C])) - self.versus_test(opponent, expected_actions, turns=2, attrs=attrs, seed=9) + self.versus_test( + opponent, expected_actions, turns=2, attrs=attrs, seed=9 + ) def test_scoring_with_alternate_game(self): """Tests that the alternate game is used in scoring.""" @@ -63,5 +65,11 @@ def test_scoring_with_alternate_game(self): expected_actions = list(zip([C, C, C], [C, D, C])) attrs = {"scores": {C: 7, D: 0}} match_attributes = {"game": axl.Game(-3, 10, 10, 10)} - self.versus_test(opponent, expected_actions, turns=3, attrs=attrs, seed=9, - match_attributes=match_attributes) + self.versus_test( + opponent, + expected_actions, + turns=3, + attrs=attrs, + seed=9, + match_attributes=match_attributes, + ) diff --git a/axelrod/tests/strategies/test_ann.py b/axelrod/tests/strategies/test_ann.py index 985462e34..c2a8290fe 100644 --- a/axelrod/tests/strategies/test_ann.py +++ b/axelrod/tests/strategies/test_ann.py @@ -18,6 +18,7 @@ class TestSplitWeights(unittest.TestCase): def test_split_weights(self): with self.assertRaises(ValueError): split_weights([0] * 20, 12, 10) + # Doesn't Raise split_weights([0] * 70, 5, 10) split_weights([0] * 12, 10, 1) @@ -130,7 +131,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): init_parameters = { "num_features": nn_weights["Evolved ANN 5"][0], "num_hidden": nn_weights["Evolved ANN 5"][1], - "weights": nn_weights["Evolved ANN 5"][2] + "weights": nn_weights["Evolved ANN 5"][2], } @@ -139,7 +140,7 @@ class TestEvolvableANN3(TestEvolvablePlayer): axl.EvolvableANN, num_features=num_features, num_hidden=num_hidden, - weights=weights + weights=weights, ) diff --git a/axelrod/tests/strategies/test_apavlov.py b/axelrod/tests/strategies/test_apavlov.py index 8d3288a93..3b37f1278 100644 --- a/axelrod/tests/strategies/test_apavlov.py +++ b/axelrod/tests/strategies/test_apavlov.py @@ -34,7 +34,9 @@ def test_strategy_versus_mock_player(self): opponent = axl.MockPlayer(actions=[C] * 6 + [D]) actions = [(C, C)] * 6 + [(C, D), (D, C)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Cooperative"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Cooperative"}, ) def test_strategy_versus_defector(self): @@ -70,30 +72,47 @@ def test_strategy_PavlovD(self): opponent = axl.Cycler(cycle="DDC") actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "PavlovD"}, ) def test_strategy_PavlovD2(self): """Tests that PavolvD is identified by DDCDDC and that the response is D then C""" opponent = axl.MockPlayer(actions=[D, D, C, D, D, C, D]) - actions = [(C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (D, D), (C, D)] + actions = [ + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, C), + (D, D), + (C, D), + ] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "PavlovD"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "PavlovD"}, ) def test_strategy_random(self): opponent = axl.MockPlayer(actions=[C, C, C, D, D, D]) actions = [(C, C), (C, C), (C, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) def test_strategy_random2(self): opponent = axl.MockPlayer(actions=[D, D, D, C, C, C]) actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (D, D)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) @@ -143,42 +162,100 @@ def test_strategy_defector3(self): def test_strategy_defector4(self): opponent = axl.MockPlayer(actions=[C, D, D, C, D, D, D]) - actions = [(C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, D), (D, C)] + actions = [ + (C, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"} ) def test_strategy_stft(self): opponent = axl.MockPlayer(actions=[C, D, D, C, C, D, D]) - actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (C, D), (D, C)] + actions = [ + (C, C), + (C, D), + (D, D), + (D, C), + (C, C), + (C, D), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_stft2(self): opponent = axl.MockPlayer(actions=[C, D, C, D, C, D, D]) - actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, C)] + actions = [ + (C, C), + (C, D), + (D, C), + (C, D), + (D, C), + (C, D), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_stft3(self): opponent = axl.MockPlayer(actions=[D, D, D, C, C, C, C]) - actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (C, C), (C, D)] + actions = [ + (C, D), + (D, D), + (D, D), + (D, C), + (C, C), + (C, C), + (C, C), + (C, D), + ] self.versus_test( opponent, expected_actions=actions, attrs={"opponent_class": "STFT"} ) def test_strategy_random(self): opponent = axl.MockPlayer(actions=[C, C, C, C, D, D]) - actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, D), (D, C), (D, C)] + actions = [ + (C, C), + (C, C), + (C, C), + (C, C), + (C, D), + (D, D), + (D, C), + (D, C), + ] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) def test_strategy_random2(self): opponent = axl.MockPlayer(actions=[D, D, C, C, C, C]) - actions = [(C, D), (D, D), (D, C), (C, C), (C, C), (C, C), (D, D), (D, D)] + actions = [ + (C, D), + (D, D), + (D, C), + (C, C), + (C, C), + (C, C), + (D, D), + (D, D), + ] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_class": "Random"} + opponent, + expected_actions=actions, + attrs={"opponent_class": "Random"}, ) diff --git a/axelrod/tests/strategies/test_axelrod_first.py b/axelrod/tests/strategies/test_axelrod_first.py index 40f09e80c..dfb1bd0b9 100644 --- a/axelrod/tests/strategies/test_axelrod_first.py +++ b/axelrod/tests/strategies/test_axelrod_first.py @@ -107,7 +107,9 @@ class TestFirstByFeld(TestPlayer): def test_cooperation_probability(self): # Test cooperation probabilities - p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100) + p1 = self.player( + start_coop_prob=1.0, end_coop_prob=0.8, rounds_of_decay=100 + ) self.assertEqual(1.0, p1._cooperation_probability()) p2 = axl.Cooperator() match = axl.Match((p1, p2), turns=50) @@ -118,7 +120,9 @@ def test_cooperation_probability(self): self.assertEqual(0.8, p1._cooperation_probability()) # Test cooperation probabilities, second set of params - p1 = self.player(start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200) + p1 = self.player( + start_coop_prob=1.0, end_coop_prob=0.5, rounds_of_decay=200 + ) self.assertEqual(1.0, p1._cooperation_probability()) match = axl.Match((p1, p2), turns=100) match.play() @@ -131,10 +135,14 @@ def test_decay(self): # Test beyond 200 rounds for opponent in [axl.Cooperator(), axl.Defector()]: player = self.player() - self.assertEqual(player._cooperation_probability(), player._start_coop_prob) + self.assertEqual( + player._cooperation_probability(), player._start_coop_prob + ) match = axl.Match((player, opponent), turns=201) match.play() - self.assertEqual(player._cooperation_probability(), player._end_coop_prob) + self.assertEqual( + player._cooperation_probability(), player._end_coop_prob + ) def test_stochastic_behavior(self): actions = [(C, C)] * 13 + [(D, C)] @@ -176,19 +184,28 @@ def test_strategy(self): actions += [(D, C)] # 51 turns actions += [(C, D), (D, C)] * 2 + [(C, D)] # 56 turns self.versus_test( - axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=0 + axl.Alternator(), + expected_actions=actions, + attrs=expected_attrs, + seed=0, ) # Against defector actions = [(C, D)] + [(D, D)] * 55 # 56 turns self.versus_test( - axl.Defector(), expected_actions=actions, attrs=expected_attrs, seed=0 + axl.Defector(), + expected_actions=actions, + attrs=expected_attrs, + seed=0, ) # Against cooperator actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=0 + axl.Cooperator(), + expected_actions=actions, + attrs=expected_attrs, + seed=0, ) # Test recognition of random player @@ -198,12 +215,21 @@ def test_strategy(self): } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 # 56 turns self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=1 + axl.Cooperator(), + expected_actions=actions, + attrs=expected_attrs, + seed=1, ) - expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 63} + expected_attrs = { + "opponent_is_random": False, + "next_random_defection_turn": 63, + } actions += [(C, C)] # 57 turns self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs=expected_attrs, seed=8 + axl.Cooperator(), + expected_actions=actions, + attrs=expected_attrs, + seed=8, ) expected_attrs = { @@ -215,11 +241,17 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 57 turns actions += [(D, D)] self.versus_test( - axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=3 + axl.Alternator(), + expected_actions=actions, + attrs=expected_attrs, + seed=3, ) actions += [(D, C), (D, D)] * 5 self.versus_test( - axl.Alternator(), expected_actions=actions, attrs=expected_attrs, seed=4 + axl.Alternator(), + expected_actions=actions, + attrs=expected_attrs, + seed=4, ) # Test versus TfT @@ -231,20 +263,35 @@ def test_strategy(self): actions += [(C, D), (D, C)] * 3 # 56 turns actions += [(C, D), (D, C)] * 50 self.versus_test( - axl.TitForTat(), expected_actions=actions, seed=0, attrs=expected_attrs + axl.TitForTat(), + expected_actions=actions, + seed=0, + attrs=expected_attrs, ) # Test random defections - expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 76} + expected_attrs = { + "opponent_is_random": False, + "next_random_defection_turn": 76, + } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 15 + [(D, C)] + [(C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs + axl.Cooperator(), + expected_actions=actions, + seed=0, + attrs=expected_attrs, ) - expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 79} + expected_attrs = { + "opponent_is_random": False, + "next_random_defection_turn": 79, + } actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 14 + [(D, C)] + [(C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, seed=5, attrs=expected_attrs + axl.Cooperator(), + expected_actions=actions, + seed=5, + attrs=expected_attrs, ) @@ -270,11 +317,29 @@ def test_strategy(self): self.versus_test(axl.Alternator(), expected_actions=actions, seed=0) opponent = axl.MockPlayer(actions=[D] * 8) - actions = [(C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D)] + actions = [ + (C, D), + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + ] self.versus_test(opponent, expected_actions=actions, seed=1) opponent = axl.MockPlayer(actions=[D] * 8) - actions = [(C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (C, D), (C, D)] + actions = [ + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + (D, D), + (C, D), + (C, D), + ] self.versus_test(opponent, expected_actions=actions, seed=2) @@ -349,14 +414,41 @@ def test_strategy(self): actions = [(C, C)] * 9 self.versus_test(axl.Cooperator(), expected_actions=actions) - actions = [(C, D), (D, D), (D, D), (C, D), (C, D), (C, D), (C, D), (C, D)] + actions = [ + (C, D), + (D, D), + (D, D), + (C, D), + (C, D), + (C, D), + (C, D), + (C, D), + ] self.versus_test(axl.Defector(), expected_actions=actions) - actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C), (C, D)] + actions = [ + (C, C), + (C, D), + (D, C), + (C, D), + (D, C), + (C, D), + (D, C), + (C, D), + ] self.versus_test(axl.Alternator(), expected_actions=actions) opponent = axl.MockPlayer(actions=[D, C]) - actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (D, D), (D, C)] + actions = [ + (C, D), + (D, C), + (D, D), + (D, C), + (D, D), + (D, C), + (D, D), + (D, C), + ] self.versus_test(opponent, expected_actions=actions) @@ -437,7 +529,9 @@ def test_strategy(self): # Test beyond 10 rounds opponent = axl.MockPlayer(actions=[D] * 5 + [C] * 5 + [C, D] * 5) - actions = [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)] + actions = ( + [(C, D)] * 5 + [(C, C)] * 6 + [(D, D)] + [(D, C), (C, D), (C, C)] + ) self.versus_test(opponent, expected_actions=actions, seed=20) @@ -494,7 +588,9 @@ def test_strategy(self): opponent = axl.Cooperator() actions = [(C, C)] * 17 + [(D, C)] * 2 self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_is_random": False} + opponent, + expected_actions=actions, + attrs={"opponent_is_random": False}, ) actions = actions[:-2] + [(C, C)] * 2 @@ -510,7 +606,9 @@ def test_strategy(self): opponent = axl.Defector() actions = [(C, D)] * 4 + [(D, D)] * 15 self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_is_random": False} + opponent, + expected_actions=actions, + attrs={"opponent_is_random": False}, ) # SteinAndRapoport vs Alternator @@ -525,7 +623,9 @@ def test_strategy(self): actions += [(D, D), (D, C), (D, D), (D, C)] self.versus_test( - opponent, expected_actions=actions, attrs={"opponent_is_random": True} + opponent, + expected_actions=actions, + attrs={"opponent_is_random": True}, ) # The test is carried out again every 15 rounds. @@ -570,8 +670,11 @@ def test_strategy(self): # Cooperator Test does noot defect if game length is unknown opponent = axl.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] - self.versus_test(opponent, expected_actions=actions, - match_attributes={"length": float("inf")}) + self.versus_test( + opponent, + expected_actions=actions, + match_attributes={"length": float("inf")}, + ) # Defector Test opponent = axl.Defector() diff --git a/axelrod/tests/strategies/test_axelrod_second.py b/axelrod/tests/strategies/test_axelrod_second.py index 26fa8078e..5172eb89a 100644 --- a/axelrod/tests/strategies/test_axelrod_second.py +++ b/axelrod/tests/strategies/test_axelrod_second.py @@ -112,7 +112,9 @@ def test_strategy(self): # Now play TfT opponent = axl.MockPlayer(actions=[C, D, C, D, D, C]) actions = [(D, C), (C, D), (C, C), (C, D), (D, D), (D, C), (C, C)] - self.versus_test(opponent, expected_actions=actions, attrs={"is_TFT": True}) + self.versus_test( + opponent, expected_actions=actions, attrs={"is_TFT": True} + ) class TestGladstein(TestPlayer): @@ -151,11 +153,15 @@ def test_strategy(self): # Ratio is 1/3 when MockPlayer defected for the first time. opponent = axl.MockPlayer(actions=[C, C, C, D, D]) actions = [(D, C), (C, C), (C, C), (D, D), (C, D)] - self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) + self.versus_test( + opponent, expected_actions=actions, attrs={"patsy": False} + ) opponent = axl.AntiTitForTat() actions = [(D, C), (C, C), (C, D), (C, D), (D, D)] - self.versus_test(opponent, expected_actions=actions, attrs={"patsy": False}) + self.versus_test( + opponent, expected_actions=actions, attrs={"patsy": False} + ) class TestTranquilizer(TestPlayer): @@ -184,7 +190,16 @@ def test_initialised_variables(self): def test_strategy(self): opponent = axl.Bully() - actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, D), (D, C), (C, C)] + actions = [ + (C, D), + (D, D), + (D, C), + (C, C), + (C, D), + (D, D), + (D, C), + (C, C), + ] expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, @@ -192,7 +207,9 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) # Tests whether TitForTat is played given score is below 1.75 @@ -205,10 +222,14 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) opponent = axl.MockPlayer([C] * 2 + [D] * 8 + [C] * 4) - actions = [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 + actions = ( + [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 + ) expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, @@ -216,7 +237,9 @@ def test_strategy(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) def test_strategy2(self): # If score is between 1.75 and 2.25, may cooperate or defect @@ -356,7 +379,9 @@ def test_strategy10(self): "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1, } - self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) + self.versus_test( + opponent, expected_actions=actions, attrs=expected_attrs + ) class TestGrofman(TestPlayer): @@ -603,7 +628,9 @@ def test_strategy(self): # Tries to cooperate every third time until detecting defective actions = ( - [(C, D), (D, D), (D, D), (D, D)] * 6 + [(C, D), (D, D)] + [(D, D)] * 100 + [(C, D), (D, D), (D, D), (D, D)] * 6 + + [(C, D), (D, D)] + + [(D, D)] * 100 ) self.versus_test(axl.Defector(), expected_actions=actions) @@ -656,7 +683,9 @@ def test_stochastic_behavior(self): """Test random responses on turns 1 through 17.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match((axl.SecondByCave(), axl.Alternator()), turns=30, seed=1) + match = axl.Match( + (axl.SecondByCave(), axl.Alternator()), turns=30, seed=1 + ) match.play() player_history = [round[0] for round in match.result] self.assertTrue(C in player_history[1:17]) @@ -667,7 +696,9 @@ def test_serial_defection_against_defector(self): to respond D->D.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match((axl.SecondByCave(), axl.Defector()), turns=30, seed=1) + match = axl.Match( + (axl.SecondByCave(), axl.Defector()), turns=30, seed=1 + ) result = match.play() self.assertEqual(result[0], (C, D)) self.assertEqual(result[18:], [(D, D)] * 12) @@ -679,7 +710,9 @@ def test_serial_defection_against_mostly_defector(self): # makes finding seeds reproducibly difficult. opponent_actions = [D] * 17 + [C, C, C, C] almost_defector = axl.MockPlayer(actions=opponent_actions) - match = axl.Match((axl.SecondByCave(), almost_defector), turns=21, seed=1) + match = axl.Match( + (axl.SecondByCave(), almost_defector), turns=21, seed=1 + ) result = match.play() self.assertEqual(result[0], (C, D)) self.assertEqual(result[-3], (C, C)) @@ -690,10 +723,12 @@ def test_versus_alternator(self): random and defect.""" # Use an explicit match because the random behavior in turns 1 through 17 # makes finding seeds reproducibly difficult. - match = axl.Match((axl.SecondByCave(), axl.Alternator()), turns=100, seed=1) + match = axl.Match( + (axl.SecondByCave(), axl.Alternator()), turns=100, seed=1 + ) result = match.play() self.assertEqual(result[0], (C, C)) - self.assertEqual(result[37: 40], [(C, D), (D, C), (D, D)]) + self.assertEqual(result[37:40], [(C, D), (D, C), (D, D)]) self.assertEqual(result[40:], [(D, C), (D, D)] * 30) @@ -923,7 +958,10 @@ def test_exit_fair_weather(self): # Immediately exit Fair-weather actions += [(D, C), (C, C), (D, C), (C, C)] self.versus_test( - Defect37_big, expected_actions=actions, seed=10, attrs={"mode": "Normal"} + Defect37_big, + expected_actions=actions, + seed=10, + attrs={"mode": "Normal"}, ) def test_exit_fair_weather2(self): @@ -934,7 +972,10 @@ def test_exit_fair_weather2(self): opponent_actions = [C] * 36 + [D] + [C] * 100 + [D] + [C] * 4 Defect37_big = axl.MockPlayer(actions=opponent_actions) self.versus_test( - Defect37_big, expected_actions=actions, seed=1, attrs={"mode": "Normal"} + Defect37_big, + expected_actions=actions, + seed=1, + attrs={"mode": "Normal"}, ) def test_non_fair_weather(self): @@ -1015,8 +1056,10 @@ def test_parity_limit_shortening(self): # Now hit the limit sooner actions += [(C, D), (D, C), (C, D), (C, C)] * 5 self.versus_test( - AsyncAlternator, expected_actions=actions, attrs={"parity_limit": 3}, - seed=10 + AsyncAlternator, + expected_actions=actions, + attrs={"parity_limit": 3}, + seed=10, ) def test_detect_streak(self): @@ -1036,14 +1079,15 @@ def test_detect_streak(self): axl.Defector(), expected_actions=actions, attrs={"recorded_defects": 119}, - seed=10 + seed=10, ) def test_detect_random(self): """Tests that detect_random() is triggered on a Random opponent and that the strategy defects thereafter.""" - match = axl.Match((axl.SecondByHarrington(), axl.Random()), seed=10, - turns=31) + match = axl.Match( + (axl.SecondByHarrington(), axl.Random()), seed=10, turns=31 + ) match.play() player = match.players[0] # Check that detect_random(30) is True. @@ -1149,14 +1193,18 @@ def test_strategy(self): actions = [(C, D)] + [(D, D)] * 8 self.versus_test( - axl.Defector(), expected_actions=actions, attrs={"score_to_beat_inc": 5} + axl.Defector(), + expected_actions=actions, + attrs={"score_to_beat_inc": 5}, ) actions = [(C, D)] + [(D, D)] * 8 # On tenth turn, try a fresh start actions += [(C, D), (C, D)] + [(D, D)] * 2 self.versus_test( - axl.Defector(), expected_actions=actions, attrs={"last_fresh_start": 11} + axl.Defector(), + expected_actions=actions, + attrs={"last_fresh_start": 11}, ) actions = [(C, C), (C, D)] @@ -1333,9 +1381,28 @@ def test_defects_on_turn_10_against_defector(self): self.versus_test(axl.Defector(), expected_actions=actions) def test_defection_logic_triggered(self): - actions = [(C, D), (C, D), (C, C), (C, D), (C, D), (C, C), (C, D), (C, D), - (C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, C), (C, D), - (D, D), (D, C), (C, D), (D, D)] + actions = [ + (C, D), + (C, D), + (C, C), + (C, D), + (C, D), + (C, C), + (C, D), + (C, D), + (C, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + (D, C), + (C, D), + (D, D), + ] self.versus_test(axl.CyclerDDC(), expected_actions=actions) def test_defection_logic_not_triggered(self): @@ -1409,7 +1476,9 @@ class TestRichardHufford(TestPlayer): def test_strategy(self): actions = [(C, C)] * 19 + [(D, C), (C, C), (C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 14} + axl.Cooperator(), + expected_actions=actions, + attrs={"streak_needed": 14}, ) actions = [(C, C)] * 19 + [(D, C), (C, C)] @@ -1418,14 +1487,18 @@ def test_strategy(self): ] # This is the first Cooperation that gets counted on the new streak actions += [(C, C)] * 13 + [(D, C), (C, C), (C, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs={"streak_needed": 11} + axl.Cooperator(), + expected_actions=actions, + attrs={"streak_needed": 11}, ) opponent_actions = [C] * 20 + [D] BoredCooperator = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 19 + [(D, C), (C, D), (C, C)] self.versus_test( - BoredCooperator, expected_actions=actions, attrs={"streak_needed": 31} + BoredCooperator, + expected_actions=actions, + attrs={"streak_needed": 31}, ) actions = [(C, D)] # "Disagreement" @@ -1434,7 +1507,9 @@ def test_strategy(self): actions += [(C, D)] # TFT. Disagreement actions += [(D, C)] # Three of last four are disagreements. actions += [(C, C)] # TFT. Disagreement - actions += [(D, D)] # Three of last four are disagreements. Disagreement + actions += [ + (D, D) + ] # Three of last four are disagreements. Disagreement actions += [(D, D)] # Three of last four are disagreements. actions += [(D, D)] # Now there are 5/9 disagreements, so Defect. self.versus_test( @@ -1470,23 +1545,39 @@ def test_strategy(self): # It's actually impossible to Defect on the third move. actions += [(D, D)] # (D, C, *) gets updated, then checked. actions += [(C, D)] # (D, C, *) gets updated, but (D, D, *) checked. - actions += [(D, D)] * 30 # (D, D, *) gets updated and checked from here on. + actions += [ + (D, D) + ] * 30 # (D, D, *) gets updated and checked from here on. self.versus_test(axl.Defector(), expected_actions=actions) actions = [(C, C), (C, D)] - actions += [(C, C)] # Increment (C, C, C). Check (C, C, *). Cooperate. + actions += [ + (C, C) + ] # Increment (C, C, C). Check (C, C, *). Cooperate. # Reminder that first C is default value and last C is opponent's first move. - actions += [(C, D)] # Increment (C, C, D). Check (D, C, *) = 0. Cooperate. - actions += [(C, C)] # Increment (D, C, C). Check (C, C, *) = 0. Cooperate. + actions += [ + (C, D) + ] # Increment (C, C, D). Check (D, C, *) = 0. Cooperate. + actions += [ + (C, C) + ] # Increment (D, C, C). Check (C, C, *) = 0. Cooperate. # There is one Defection and one Cooperation in this scenario, # but the Cooperation was due to a default value only. We can see where this is going. - actions += [(C, D)] # Increment (C, C, D). Check (D, C, *) = 1. Cooperate. - actions += [(D, C)] # Increment (D, C, C). Check (C, C, *) = -1. Defect. + actions += [ + (C, D) + ] # Increment (C, C, D). Check (D, C, *) = 1. Cooperate. + actions += [ + (D, C) + ] # Increment (D, C, C). Check (C, C, *) = -1. Defect. actions += [ (C, D) ] # Increment (C, C, D). Check (D, D, *) = 0 (New). Cooperate. - actions += [(D, C)] # Increment (D, D, C). Check (C, C, *) < 0. Defect. - actions += [(C, D)] # Increment (C, C, D). Check (D, D, *) > 0. Cooperate. + actions += [ + (D, C) + ] # Increment (D, D, C). Check (C, C, *) < 0. Defect. + actions += [ + (C, D) + ] # Increment (C, C, D). Check (D, D, *) > 0. Cooperate. actions += [(D, C), (C, D)] * 15 # This pattern continues for a while. actions += [ (D, C), @@ -1670,7 +1761,9 @@ def test_strategy(self): opponent_actions = [C] * 100 + [D] * 10 Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 100 + [(C, D)] * 4 - self.versus_test(Change_of_Heart, expected_actions=actions, attrs={"credit": 2}) + self.versus_test( + Change_of_Heart, expected_actions=actions, attrs={"credit": 2} + ) Change_of_Heart = axl.MockPlayer(actions=opponent_actions) actions += [(C, D)] * 2 self.versus_test( @@ -1678,6 +1771,7 @@ def test_strategy(self): ) # Still Cooperate, because Defect rate is low + class TestRowsam(TestPlayer): name = "Second by Rowsam" player = axl.SecondByRowsam @@ -1699,12 +1793,18 @@ def test_strategy(self): # Against a Defector should eventually enter Defect mode actions = [(C, D)] * 5 actions += [(D, D), (C, D), (D, D)] # Do a Coop-Def cycle - self.versus_test(axl.Defector(), expected_actions=actions, attrs={ - "distrust_points": 5}) + self.versus_test( + axl.Defector(), + expected_actions=actions, + attrs={"distrust_points": 5}, + ) actions += [(C, D)] * 3 # Continue for now actions += [(D, D)] * 100 # Now Defect mode - self.versus_test(axl.Defector(), expected_actions=actions, attrs={ - "distrust_points": 10, "mode": "Defect"}) + self.versus_test( + axl.Defector(), + expected_actions=actions, + attrs={"distrust_points": 10, "mode": "Defect"}, + ) # Test specific score scenarios # 5 Defects @@ -1712,8 +1812,11 @@ def test_strategy(self): custom_opponent = axl.MockPlayer(actions=opponent_actions) actions = [(C, D)] * 5 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 5, "current_score": 0}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 5, "current_score": 0}, + ) # 3 Defects opponent_actions = [D] * 3 + [C] * 100 @@ -1721,8 +1824,11 @@ def test_strategy(self): actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 3, "current_score": 6}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 3, "current_score": 6}, + ) # 2 Defects opponent_actions = [D] * 2 + [C] * 100 @@ -1730,8 +1836,11 @@ def test_strategy(self): actions = [(C, D)] * 2 actions += [(C, C)] * 3 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 2, "current_score": 9}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 2, "current_score": 9}, + ) # 1 Defect opponent_actions = [D] * 1 + [C] * 100 @@ -1739,8 +1848,11 @@ def test_strategy(self): actions = [(C, D)] * 1 actions += [(C, C)] * 4 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 1, "current_score": 12}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 1, "current_score": 12}, + ) # Test that some distrust_points wear off. opponent_actions = [D] * 3 + [C] * 100 @@ -1748,27 +1860,42 @@ def test_strategy(self): actions = [(C, D)] * 3 actions += [(C, C)] * 2 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 3, "current_score": 6}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 3, "current_score": 6}, + ) custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 3 actions += [(D, C)] - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 4, "current_score": 28}) + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 4, "current_score": 28}, + ) custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C), (D, C)] # Complete Coop-Def cycle actions += [(C, C)] * 4 # No defect or cycle this time. - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 3, "current_score": 50}) # One point wears off. + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 3, "current_score": 50}, + ) # One point wears off. custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 2}) # Second point wears off + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 2}, + ) # Second point wears off custom_opponent = axl.MockPlayer(actions=opponent_actions) actions += [(C, C)] * 18 - self.versus_test(custom_opponent, expected_actions=actions, attrs={ - "distrust_points": 2}) # But no more + self.versus_test( + custom_opponent, + expected_actions=actions, + attrs={"distrust_points": 2}, + ) # But no more class TestAppold(TestPlayer): @@ -1787,64 +1914,93 @@ class TestAppold(TestPlayer): def test_cooperate_against_cooperating_opponent(self): """Strategy should cooperate 100% of the time with a fully cooperating opponent.""" actions = [(C, C)] * 100 - self.versus_test(axl.Cooperator(), expected_actions=actions, - attrs={ - "first_opp_def": False, - "total_num_of_x": {C: 99, D: 1}, - "opp_c_after_x": {C: 99, D: 1} - }) + self.versus_test( + axl.Cooperator(), + expected_actions=actions, + attrs={ + "first_opp_def": False, + "total_num_of_x": {C: 99, D: 1}, + "opp_c_after_x": {C: 99, D: 1}, + }, + ) def test_cooperate_on_first_four_turns(self): """Strategy will cooperate on the first four turns regardless of opponent.""" # Hypothesis opportunity: choose random opponent player_expected_actions = [C, C, C, C] coplayer_expected_actions = [D, D, D, D] - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(axl.Defector(), turns=4, expected_actions=expected_actions, - attrs={ - "first_opp_def": False, - "total_num_of_x": {C: 3, D: 1}, - "opp_c_after_x": {C: 0, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + axl.Defector(), + turns=4, + expected_actions=expected_actions, + attrs={ + "first_opp_def": False, + "total_num_of_x": {C: 3, D: 1}, + "opp_c_after_x": {C: 0, D: 1}, + }, + ) def test_fifth_move_cooperate(self): """Strategy will cooperate on a fifth move defection and set first_opp_def.""" player_expected_actions = [C, C, C, C, C, C] coplayer_expected_actions = [C, C, C, C, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(coplayer, turns=6, expected_actions=expected_actions, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 5, D: 1}, - "opp_c_after_x": {C: 4, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + coplayer, + turns=6, + expected_actions=expected_actions, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 5, D: 1}, + "opp_c_after_x": {C: 4, D: 1}, + }, + ) def test_sixth_move_cooperate(self): """Strategy will cooperate on a sixth move defection if it is the first.""" player_expected_actions = [C, C, C, C, C, C, C] coplayer_expected_actions = [C, C, C, C, C, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(coplayer, turns=7, expected_actions=expected_actions, seed=1, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 6, D: 1}, - "opp_c_after_x": {C: 5, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + coplayer, + turns=7, + expected_actions=expected_actions, + seed=1, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 6, D: 1}, + "opp_c_after_x": {C: 5, D: 1}, + }, + ) def test_sixth_move_defect(self): """Strategy will defect on a sixth move defection if it is not the first.""" player_expected_actions = [C, C, C, C, C, C, D] coplayer_expected_actions = [C, C, C, C, D, D, C] coplayer = axl.MockPlayer(actions=coplayer_expected_actions) - expected_actions = list(zip(player_expected_actions, coplayer_expected_actions)) - self.versus_test(coplayer, turns=7, expected_actions=expected_actions, seed=10, - attrs={ - "first_opp_def": True, - "total_num_of_x": {C: 6, D: 1}, - "opp_c_after_x": {C: 4, D: 1} - }) + expected_actions = list( + zip(player_expected_actions, coplayer_expected_actions) + ) + self.versus_test( + coplayer, + turns=7, + expected_actions=expected_actions, + seed=10, + attrs={ + "first_opp_def": True, + "total_num_of_x": {C: 6, D: 1}, + "opp_c_after_x": {C: 4, D: 1}, + }, + ) def test_later_single_defection_forgiveness(self): # An opponent who defects after a long time, then tries cooperating @@ -1856,8 +2012,12 @@ def test_later_single_defection_forgiveness(self): actions += [(C, D)] # But we forgive it (and record it). actions += [(C, C)] * 10 - self.versus_test(MostlyCooperates, expected_actions=actions, seed=1, - attrs={"first_opp_def": True}) + self.versus_test( + MostlyCooperates, + expected_actions=actions, + seed=1, + attrs={"first_opp_def": True}, + ) def test_stochastic_behavior(self): opponent = axl.Defector() @@ -1871,19 +2031,24 @@ def test_stochastic_behavior(self): # Then defect most of the time, depending on the random number. We # don't defect 100% of the time, because of the way that initialize # opp_c_after_x. - actions += [(D, D), - (C, D), - (D, D), - (D, D), # C can never be two moves after a C. - (D, D), - (D, D), - (D, D), - (D, D), - (D, D), - (D, D), - (C, D), - (C, D), - (D, D), + actions += [ + (D, D), + (C, D), + (D, D), + (D, D), # C can never be two moves after a C. + (D, D), + (D, D), + (D, D), + (D, D), + (D, D), + (D, D), + (C, D), + (C, D), + (D, D), ] - self.versus_test(opponent, expected_actions=actions, seed=1018, - attrs={"first_opp_def": True}) + self.versus_test( + opponent, + expected_actions=actions, + seed=1018, + attrs={"first_opp_def": True}, + ) diff --git a/axelrod/tests/strategies/test_backstabber.py b/axelrod/tests/strategies/test_backstabber.py index 1f580c9cf..c4a988aec 100644 --- a/axelrod/tests/strategies/test_backstabber.py +++ b/axelrod/tests/strategies/test_backstabber.py @@ -117,9 +117,13 @@ def test_when_alt_strategy_is_triggered(self): def test_starting_defect_keeps_alt_strategy_from_triggering(self): opponent_actions_suffix = [C, D, C, D, D] + 3 * [C] - expected_actions_suffix = [(C, C), (C, D), (C, C), (C, D), (C, D)] + 3 * [ - (D, C) - ] + expected_actions_suffix = [ + (C, C), + (C, D), + (C, C), + (C, D), + (C, D), + ] + 3 * [(D, C)] defects_on_first = [D] + [C] * 6 defects_on_first_actions = [(C, D)] + [(C, C)] * 6 diff --git a/axelrod/tests/strategies/test_better_and_better.py b/axelrod/tests/strategies/test_better_and_better.py index 2499eb4a3..6b351fe7e 100644 --- a/axelrod/tests/strategies/test_better_and_better.py +++ b/axelrod/tests/strategies/test_better_and_better.py @@ -25,21 +25,15 @@ def test_strategy(self): """Tests that the strategy gives expected behaviour.""" expected_actions = [(D, D)] * 90 + [(C, D)] self.versus_test( - axl.Defector(), - expected_actions=expected_actions, - seed=6, + axl.Defector(), expected_actions=expected_actions, seed=6, ) expected_actions = [(D, C)] * 10 self.versus_test( - axl.Cooperator(), - expected_actions=expected_actions, - seed=8, + axl.Cooperator(), expected_actions=expected_actions, seed=8, ) expected_actions = [(D, D)] * 41 + [(C, D)] self.versus_test( - axl.Defector(), - expected_actions=expected_actions, - seed=13, + axl.Defector(), expected_actions=expected_actions, seed=13, ) expected_indices = [18, 39, 49, 67, 77, 116, 139, 142, 149] m = axl.Match((self.player(), axl.Defector()), turns=150, seed=111) diff --git a/axelrod/tests/strategies/test_calculator.py b/axelrod/tests/strategies/test_calculator.py index 0d5eedfb3..e99114600 100644 --- a/axelrod/tests/strategies/test_calculator.py +++ b/axelrod/tests/strategies/test_calculator.py @@ -25,7 +25,9 @@ class TestCalculator(TestPlayer): def test_twenty_rounds_joss_for_cyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed + ) match.play() self.versus_test( axl.Alternator(), expected_actions=match.result, seed=seed @@ -34,17 +36,24 @@ def test_twenty_rounds_joss_for_cyclers(self): def test_twenty_rounds_joss_then_defects_for_cyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.Alternator()), turns=20, seed=seed + ) match.play() expected_actions = match.result + [(D, C), (D, D), (D, C), (D, D)] self.versus_test( - axl.Alternator(), expected_actions=expected_actions, seed=seed, turns=24, + axl.Alternator(), + expected_actions=expected_actions, + seed=seed, + turns=24, ) def test_twenty_rounds_joss_for_noncyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed + ) match.play() self.versus_test( axl.AntiCycler(), expected_actions=match.result, seed=seed @@ -53,16 +62,29 @@ def test_twenty_rounds_joss_for_noncyclers(self): def test_twenty_rounds_joss_then_tft_for_noncyclers(self): """Uses axelrod.strategies.axelrod_first.FirstByJoss strategy for first 20 rounds""" seed = 4 - match = axl.Match((axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed) + match = axl.Match( + (axl.FirstByJoss(), axl.AntiCycler()), turns=20, seed=seed + ) match.play() - expected_actions = match.result + [(C, C), (C, C), (C, D), (D, C), (C, C)] + expected_actions = match.result + [ + (C, C), + (C, C), + (C, D), + (D, C), + (C, C), + ] self.versus_test( - axl.AntiCycler(), expected_actions=expected_actions, seed=seed, turns=24, + axl.AntiCycler(), + expected_actions=expected_actions, + seed=seed, + turns=24, ) def test_edge_case_calculator_sees_cycles_of_size_ten(self): ten_length_cycle = [C, D, C, C, D, C, C, C, D, C] - self.assertEqual(detect_cycle((ten_length_cycle * 2)), tuple(ten_length_cycle)) + self.assertEqual( + detect_cycle((ten_length_cycle * 2)), tuple(ten_length_cycle) + ) ten_cycle_twenty_rounds = get_joss_strategy_actions( ten_length_cycle * 2, indices_to_flip=[16] @@ -85,7 +107,9 @@ def test_edge_case_calculator_ignores_cycles_gt_len_ten(self): ) opponent_actions = twenty_rounds_of_eleven_len_cycle[:-1] + [D] + [C, D] - self.assertEqual(detect_cycle(opponent_actions), tuple(eleven_length_cycle)) + self.assertEqual( + detect_cycle(opponent_actions), tuple(eleven_length_cycle) + ) uses_tit_for_tat_after_twenty_rounds = twenty_rounds + [(D, C), (C, D)] self.versus_test( @@ -105,13 +129,19 @@ def test_get_joss_strategy_actions(self): self.assertEqual(get_joss_strategy_actions(opponent, []), without_flip) self.assertEqual( - get_joss_strategy_actions(opponent, flip_never_occurs_at_index_zero), + get_joss_strategy_actions( + opponent, flip_never_occurs_at_index_zero + ), without_flip, ) - self.assertEqual(get_joss_strategy_actions(opponent, flip_indices), with_flip) + self.assertEqual( + get_joss_strategy_actions(opponent, flip_indices), with_flip + ) -def get_joss_strategy_actions(opponent_moves: list, indices_to_flip: list) -> list: +def get_joss_strategy_actions( + opponent_moves: list, indices_to_flip: list +) -> list: """ Takes a list of opponent moves and returns a tuple list of [(Joss moves, opponent moves)]. "indices_to_flip" are the indices where Joss differs from it's expected TitForTat. diff --git a/axelrod/tests/strategies/test_cooperator.py b/axelrod/tests/strategies/test_cooperator.py index aca2290ae..7dd4e7d3b 100644 --- a/axelrod/tests/strategies/test_cooperator.py +++ b/axelrod/tests/strategies/test_cooperator.py @@ -41,7 +41,9 @@ class TestTrickyCooperator(TestPlayer): def test_strategy(self): # Test if it tries to trick opponent. - self.versus_test(axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)]) + self.versus_test( + axl.Cooperator(), [(C, C), (C, C), (C, C), (D, C), (D, C)] + ) opponent_actions = [C, C, C, C, D, D] expected_actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (C, D)] @@ -68,7 +70,7 @@ def test_cooperates_in_first_three_rounds(self): self.versus_test(axl.Alternator(), expected_actions=against_alternator) def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_depth_ten( - self + self, ): against_cooperator = [(C, C)] * 3 + [(D, C)] * 20 self.versus_test(axl.Cooperator(), expected_actions=against_cooperator) @@ -76,4 +78,6 @@ def test_defects_after_three_rounds_if_opponent_only_cooperated_in_max_history_d def test_defects_when_opponent_has_no_defections_to_history_depth_ten(self): opponent_actions = [D] + [C] * 10 + [D, C] expected_actions = [(C, D)] + [(C, C)] * 10 + [(D, D), (C, C)] - self.versus_test(axl.MockPlayer(actions=opponent_actions), expected_actions) + self.versus_test( + axl.MockPlayer(actions=opponent_actions), expected_actions + ) diff --git a/axelrod/tests/strategies/test_cycler.py b/axelrod/tests/strategies/test_cycler.py index 675d4b4d5..a12b14bc3 100644 --- a/axelrod/tests/strategies/test_cycler.py +++ b/axelrod/tests/strategies/test_cycler.py @@ -98,7 +98,9 @@ def test_memory_depth_is_len_cycle_minus_one(self): def test_cycler_works_as_expected(self): expected = [(C, D), (D, D), (D, D), (C, D)] * 2 self.versus_test( - axl.Defector(), expected_actions=expected, init_kwargs={"cycle": "CDDC"} + axl.Defector(), + expected_actions=expected, + init_kwargs={"cycle": "CDDC"}, ) def test_cycle_raises_value_error_on_bad_cycle_str(self): @@ -172,11 +174,17 @@ def test_normalized_parameters(self): ) cycle = "C" * random.randint(0, 20) + "D" * random.randint(0, 20) - self.assertEqual(self.player_class(cycle=cycle, seed=1)._normalize_parameters(cycle=cycle), - (cycle, len(cycle))) + self.assertEqual( + self.player_class(cycle=cycle, seed=1)._normalize_parameters( + cycle=cycle + ), + (cycle, len(cycle)), + ) cycle_length = random.randint(1, 20) - random_cycle, cycle_length2 = self.player_class(cycle=cycle, seed=1)._normalize_parameters(cycle_length=cycle_length) + random_cycle, cycle_length2 = self.player_class( + cycle=cycle, seed=1 + )._normalize_parameters(cycle_length=cycle_length) self.assertEqual(len(random_cycle), cycle_length) self.assertEqual(cycle_length, cycle_length2) diff --git a/axelrod/tests/strategies/test_darwin.py b/axelrod/tests/strategies/test_darwin.py index 50c0c4f2b..146489f8a 100644 --- a/axelrod/tests/strategies/test_darwin.py +++ b/axelrod/tests/strategies/test_darwin.py @@ -76,7 +76,9 @@ def test_against_geller_and_mindreader(self): def test_reset_history_and_attributes(self): # Overwrite this method because Darwin does not reset - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 + ) p1 = self.player() self.assertEqual(p1.genome, [D, C, C, C, D]) @@ -89,7 +91,9 @@ def test_all_darwin_instances_share_one_genome(self): p2 = self.player() self.assertIs(p1.genome, p2.genome) - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 + ) self.assertEqual(p2.genome, [D, C, C, C, D]) self.assertIs(p1.genome, p2.genome) @@ -97,7 +101,9 @@ def test_all_darwin_instances_share_one_genome(self): self.assertIs(p3.genome, p2.genome) def test_reset_genome(self): - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4 + ) self.player.reset_genome() self.assertEqual(self.player().genome, [C]) diff --git a/axelrod/tests/strategies/test_dbs.py b/axelrod/tests/strategies/test_dbs.py index 93d61e41a..a3afb81a4 100644 --- a/axelrod/tests/strategies/test_dbs.py +++ b/axelrod/tests/strategies/test_dbs.py @@ -67,7 +67,9 @@ def test_move_gen_cooperator(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.cooperator_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.cooperator_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_defector(self): @@ -89,7 +91,9 @@ def test_move_gen_defector(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.defector_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.defector_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_titForTat(self): @@ -125,7 +129,9 @@ def test_move_gen_titForTat(self): """ expected_output = [C, C, C, C] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.titForTat_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.titForTat_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_alternator(self): @@ -147,7 +153,9 @@ def test_move_gen_alternator(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.random_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_random(self): @@ -169,7 +177,9 @@ def test_move_gen_random(self): """ expected_output = [D, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.random_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.random_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) def test_minimaxTreeSearch_grudger(self): @@ -192,7 +202,9 @@ def test_move_gen_grudger(self): """ expected_output = [C, D, D, D] for inp, out in zip(self.input_pos, expected_output): - out_move = dbs.move_gen(inp, self.grudger_policy, depth_search_tree=5) + out_move = dbs.move_gen( + inp, self.grudger_policy, depth_search_tree=5 + ) self.assertEqual(out_move, out) diff --git a/axelrod/tests/strategies/test_doubler.py b/axelrod/tests/strategies/test_doubler.py index e3d436302..a02f42695 100644 --- a/axelrod/tests/strategies/test_doubler.py +++ b/axelrod/tests/strategies/test_doubler.py @@ -22,7 +22,7 @@ class TestDoubler(TestPlayer): } def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperations( - self + self, ): opponent_plays = [C] * 7 + [D] * 4 + [C] actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, C)] @@ -31,7 +31,7 @@ def test_defects_if_opponent_last_play_is_D_and_defections_gt_two_times_cooperat ) def test_defects_if_opponent_last_play_D_and_defections_equal_two_times_cooperations( - self + self, ): opponent_plays = [C] * 8 + [D] * 4 + [C] actions = [(C, C)] * 8 + [(C, D)] * 4 + [(D, C)] diff --git a/axelrod/tests/strategies/test_evolvable_player.py b/axelrod/tests/strategies/test_evolvable_player.py index 6ed840b0b..ea0fb77fc 100644 --- a/axelrod/tests/strategies/test_evolvable_player.py +++ b/axelrod/tests/strategies/test_evolvable_player.py @@ -3,7 +3,11 @@ import axelrod as axl from axelrod.action import Action -from axelrod.evolvable_player import copy_lists, crossover_dictionaries, crossover_lists +from axelrod.evolvable_player import ( + copy_lists, + crossover_dictionaries, + crossover_lists, +) from .test_player import TestPlayer @@ -17,8 +21,7 @@ class PartialedClass(cls): seed = kwargs["seed"] except KeyError: kwargs["seed"] = 1 - __init__ = functools.partialmethod( - cls.__init__, **kwargs) + __init__ = functools.partialmethod(cls.__init__, **kwargs) return PartialedClass @@ -45,7 +48,9 @@ def mutate(self): def crossover(self, other): if other.__class__ != self.__class__: - raise TypeError("Crossover must be between the same player classes.") + raise TypeError( + "Crossover must be between the same player classes." + ) value = self.value + other.value return EvolvableTestOpponent(value) @@ -125,7 +130,11 @@ def test_crossover(self): players.append(player) player1, player2 = players crossed = player1.crossover(player2) - if player1 != crossed and player2 != crossed and crossed == crossed.clone(): + if ( + player1 != crossed + and player2 != crossed + and crossed == crossed.clone() + ): return # Should never get here unless a change breaks the test, so don't include in coverage. self.assertFalse(True) # pragma: no cover @@ -140,7 +149,9 @@ def test_serialization(self): """Serializing and deserializing should return the original player.""" player = self.player(seed=1) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters(serialized) + deserialized_player = player.__class__.deserialize_parameters( + serialized + ) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -149,7 +160,7 @@ def test_serialization_csv(self): player = self.player(seed=1) serialized = player.serialize_parameters() s = "0, 1, {}, 3".format(serialized) - s2 = s.split(',')[2] + s2 = s.split(",")[2] deserialized_player = player.__class__.deserialize_parameters(s2) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -178,7 +189,9 @@ def test_behavior(self): self.behavior_test(player, parent_player) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters(serialized) + deserialized_player = player.__class__.deserialize_parameters( + serialized + ) self.behavior_test(deserialized_player, parent_player) def test_seed_propagation(self): @@ -202,7 +215,6 @@ def test_seed_preservation(self): class TestUtilityFunctions(unittest.TestCase): - def test_copy_lists(self): l1 = [list(range(10)), list(range(20))] l2 = copy_lists(l1) @@ -221,12 +233,12 @@ def test_crossover_lists(self): self.assertEqual(crossed, list1[:1] + list2[1:]) def test_crossover_dictionaries(self): - dict1 = {'1': 1, '2': 2, '3': 3} - dict2 = {'1': 'a', '2': 'b', '3': 'c'} + dict1 = {"1": 1, "2": 2, "3": 3} + dict2 = {"1": "a", "2": "b", "3": "c"} rng = axl.RandomGenerator(seed=1) crossed = crossover_dictionaries(dict1, dict2, rng) - self.assertEqual(crossed, {'1': 1, '2': 'b', '3': 'c'}) + self.assertEqual(crossed, {"1": 1, "2": "b", "3": "c"}) rng = axl.RandomGenerator(seed=2) crossed = crossover_dictionaries(dict1, dict2, rng) diff --git a/axelrod/tests/strategies/test_finite_state_machines.py b/axelrod/tests/strategies/test_finite_state_machines.py index f3525b0e1..f7eab3029 100644 --- a/axelrod/tests/strategies/test_finite_state_machines.py +++ b/axelrod/tests/strategies/test_finite_state_machines.py @@ -2,7 +2,9 @@ import unittest import axelrod as axl -from axelrod.compute_finite_state_machine_memory import get_memory_from_transitions +from axelrod.compute_finite_state_machine_memory import ( + get_memory_from_transitions, +) from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.finite_state_machines import ( EvolvableFSMPlayer, @@ -46,8 +48,15 @@ def test__eq__false_by_state(self): self.assertFalse(new_two_state.__eq__(self.two_state)) def test__eq__false_by_transition(self): - different_transitions = ((1, C, 0, D), (1, D, 0, D), (0, C, 1, D), (0, D, 1, C)) - new_two_state = SimpleFSM(transitions=different_transitions, initial_state=1) + different_transitions = ( + (1, C, 0, D), + (1, D, 0, D), + (0, C, 1, D), + (0, D, 1, C), + ) + new_two_state = SimpleFSM( + transitions=different_transitions, initial_state=1 + ) self.assertFalse(new_two_state.__eq__(self.two_state)) @@ -91,7 +100,9 @@ def test_state_setter_raises_error_for_bad_input(self): with self.assertRaises(ValueError) as cm: self.two_state.state = 5 error_msg = cm.exception.args[0] - self.assertEqual(error_msg, "state: 5 does not have values for both C and D") + self.assertEqual( + error_msg, "state: 5 does not have values for both C and D" + ) class TestSampleFSMPlayer(TestPlayer): @@ -157,7 +168,12 @@ def test_wsls(self): """Tests that the player defined by the table for TFT is in fact WSLS (also known as Pavlov.""" wsls_init_kwargs = { - "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), + "transitions": ( + (1, C, 1, C), + (1, D, 2, D), + (2, C, 2, D), + (2, D, 1, C), + ), "initial_state": 1, "initial_action": C, } @@ -272,7 +288,9 @@ def test_strategy(self): opponent = axl.MockPlayer([D, D, C, C, D]) actions = [(C, D), (C, D), (C, C), (D, C), (C, D)] self.versus_test( - opponent, expected_actions=actions, init_kwargs={"transitions": transitions} + opponent, + expected_actions=actions, + init_kwargs={"transitions": transitions}, ) def test_memory(self): @@ -280,7 +298,10 @@ def test_memory(self): Test the memory depth using implemented algorithm """ transitions = self.player().fsm._state_transitions - self.assertEqual(get_memory_from_transitions(transitions), self.expected_classifier["memory_depth"]) + self.assertEqual( + get_memory_from_transitions(transitions), + self.expected_classifier["memory_depth"], + ) class TestFortress3(TestFSMPlayer): @@ -311,7 +332,15 @@ def test_strategy(self): state_and_actions = [(1, C), (1, D), (2, C), (1, C)] self.transitions_test(state_and_actions) - state_and_actions = [(1, D), (2, D), (3, C), (3, C), (3, C), (3, D), (1, C)] * 2 + state_and_actions = [ + (1, D), + (2, D), + (3, C), + (3, C), + (3, C), + (3, D), + (1, C), + ] * 2 self.transitions_test(state_and_actions) @unittest.expectedFailure @@ -415,11 +444,15 @@ def test_strategy(self): ] + [(7, D), (7, C), (8, C), (8, D), (6, D)] * 3 self.transitions_test(state_and_actions) - state_and_actions = [(0, D), (1, C), (2, D), (3, C), (5, D), (3, C), (5, C)] + [ - (7, C), - (8, D), - (6, C), - ] * 5 + state_and_actions = [ + (0, D), + (1, C), + (2, D), + (3, C), + (5, D), + (3, C), + (5, C), + ] + [(7, C), (8, D), (6, C),] * 5 self.transitions_test(state_and_actions) state_and_actions = ( @@ -520,7 +553,9 @@ class TestRipoff(TestFSMPlayer): """ def test_strategy(self): - state_and_actions = [(1, C), (2, C)] * 3 + [(1, D)] + [(3, C), (3, D)] * 5 + state_and_actions = ( + [(1, C), (2, C)] * 3 + [(1, D)] + [(3, C), (3, D)] * 5 + ) self.transitions_test(state_and_actions) state_and_actions = [(1, C), (2, D)] + [(3, D)] * 5 @@ -621,7 +656,11 @@ class TestSolutionB1(TestFSMPlayer): def test_strategy(self): state_and_actions = ( - [(1, D)] * 3 + [(1, C)] + [(2, C)] * 3 + [(2, D)] + [(3, C), (3, D)] * 3 + [(1, D)] * 3 + + [(1, C)] + + [(2, C)] * 3 + + [(2, D)] + + [(3, C), (3, D)] * 3 ) self.transitions_test(state_and_actions) @@ -802,7 +841,9 @@ class TestEvolvedFSM16(TestFSMPlayer): def test_strategy(self): # finished: 0, - state_and_actions = [(0, C)] * 3 + [(0, D)] + [(12, D), (11, D), (5, C)] * 3 + state_and_actions = ( + [(0, C)] * 3 + [(0, D)] + [(12, D), (11, D), (5, C)] * 3 + ) self.transitions_test(state_and_actions) # finished: 0, 5, 10 @@ -849,7 +890,13 @@ def test_strategy(self): self.transitions_test(state_and_actions) # finished: 0, 1, 2, 3, 5, 10, 11, 12, 13, 14, 15 - state_and_actions = to_state_seven + [(7, D), (1, D), (6, C), (5, D), (10, C)] + state_and_actions = to_state_seven + [ + (7, D), + (1, D), + (6, C), + (5, D), + (10, C), + ] self.transitions_test(state_and_actions) # finished: 0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15 @@ -979,7 +1026,12 @@ def test_strategy(self): # finished 0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 13, 15 to_state_eleven = [(0, D), (3, C), (10, D), (1, D), (15, D)] - state_and_actions = to_state_eleven + [(11, C), (14, C), (3, C), (10, D)] + state_and_actions = to_state_eleven + [ + (11, C), + (14, C), + (3, C), + (10, D), + ] self.transitions_test(state_and_actions) # finished 0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 15 @@ -1058,7 +1110,12 @@ def test_normalized_parameters(self): self.assertRaises( InsufficientParametersError, self.player_class, - transitions=[[0, C, 1, D], [0, D, 0, D], [1, C, 1, C], [1, D, 1, D]], + transitions=[ + [0, C, 1, D], + [0, D, 0, D], + [1, C, 1, C], + [1, D, 1, D], + ], seed=1, # To prevent exception from unset seed. ) @@ -1083,7 +1140,9 @@ def test_vector_to_instance(self): self.assertIsInstance(player, axl.EvolvableFSMPlayer) serialized = player.serialize_parameters() - deserialized_player = player.__class__.deserialize_parameters(serialized) + deserialized_player = player.__class__.deserialize_parameters( + serialized + ) self.assertEqual(player, deserialized_player) self.assertEqual(deserialized_player, deserialized_player.clone()) @@ -1098,7 +1157,8 @@ def test_mutate(self): """Test to trigger random lines in mutate""" for seed in [18, 22]: player = axl.EvolvableFSMPlayer( - num_states=4, mutation_probability=0.5, seed=seed) + num_states=4, mutation_probability=0.5, seed=seed + ) player.mutate() @@ -1126,7 +1186,7 @@ class TestEvolvableFSMPlayer4(TestEvolvablePlayer): init_parameters = { "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), "initial_state": 1, - "initial_action": C + "initial_action": C, } @@ -1135,7 +1195,8 @@ class TestEvolvableFSMPlayer4(TestEvolvablePlayer): EvolvableFSMPlayer, transitions=((1, C, 1, C), (1, D, 1, D)), initial_state=1, - initial_action=C) + initial_action=C, +) class EvolvableFSMAsFSM(TestFSMPlayer): diff --git a/axelrod/tests/strategies/test_forgiver.py b/axelrod/tests/strategies/test_forgiver.py index e83b86d76..43754f5b0 100644 --- a/axelrod/tests/strategies/test_forgiver.py +++ b/axelrod/tests/strategies/test_forgiver.py @@ -25,15 +25,19 @@ def test_strategy(self): # If opponent has defected more than 10 percent of the time, defect. self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 10) - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 10 + ) def test_cooperates_if_opponent_defections_is_ten_pct_and_defects_if_opponent_defections_gt_ten_pct( - self + self, ): final_action_lowers_defections_to_ten_percent = [D] + [C] * 9 expected = [(C, D)] + [(D, C)] * 9 self.versus_test( - axl.MockPlayer(actions=final_action_lowers_defections_to_ten_percent), + axl.MockPlayer( + actions=final_action_lowers_defections_to_ten_percent + ), expected_actions=expected * 5, ) @@ -62,7 +66,9 @@ class TestForgivingTitForTat(TestPlayer): def test_strategy(self): self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 5 + ) self.versus_test( axl.Alternator(), expected_actions=[(C, C)] + [(C, D), (D, C)] * 5 ) @@ -77,7 +83,11 @@ def test_never_defects_if_opponent_defections_le_ten_percent(self): def test_plays_tit_for_tat_while_defections_gt_ten_percent(self): before_tft = (18 * [C] + [D]) * 3 + [D, D, D] - only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [(C, D), (C, D), (C, D)] + only_cooperates = ([(C, C)] * 18 + [(C, D)]) * 3 + [ + (C, D), + (C, D), + (C, D), + ] self.versus_test( axl.MockPlayer(actions=before_tft), expected_actions=only_cooperates ) diff --git a/axelrod/tests/strategies/test_gambler.py b/axelrod/tests/strategies/test_gambler.py index cc58083e3..e9ab3b140 100755 --- a/axelrod/tests/strategies/test_gambler.py +++ b/axelrod/tests/strategies/test_gambler.py @@ -415,9 +415,7 @@ def test_vs_DCDDC2(self): (D, D), # different than above test ] self.versus_test( - axl.MockPlayer(opponent_actions), - expected_actions=expected, - seed=5, + axl.MockPlayer(opponent_actions), expected_actions=expected, seed=5, ) def test_vs_DCDDC3(self): @@ -520,14 +518,17 @@ def test_vs_alternator2(self): class TestEvolvableGambler(unittest.TestCase): - def test_receive_vector(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1) + parameters=(plays, op_plays, op_start_plays), seed=1 + ) - self.assertRaises(AttributeError, axl.EvolvableGambler.__getattribute__, - *[player, 'vector']) + self.assertRaises( + AttributeError, + axl.EvolvableGambler.__getattribute__, + *[player, "vector"] + ) vector = [random.random() for _ in range(8)] player.receive_vector(vector) @@ -536,19 +537,24 @@ def test_receive_vector(self): def test_vector_to_instance(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1) + parameters=(plays, op_plays, op_start_plays), seed=1 + ) vector = [random.random() for _ in range(8)] player.receive_vector(vector) - keys = create_lookup_table_keys(player_depth=plays, op_depth=op_plays, - op_openings_depth=op_start_plays) + keys = create_lookup_table_keys( + player_depth=plays, + op_depth=op_plays, + op_openings_depth=op_start_plays, + ) action_dict = dict(zip(keys, vector)) self.assertEqual(player._lookup.dictionary, action_dict) def test_create_vector_bounds(self): plays, op_plays, op_start_plays = 1, 1, 1 player = axl.EvolvableGambler( - parameters=(plays, op_plays, op_start_plays), seed=1) + parameters=(plays, op_plays, op_start_plays), seed=1 + ) lb, ub = player.create_vector_bounds() self.assertIsInstance(lb, list) self.assertIsInstance(ub, list) @@ -566,8 +572,7 @@ class TestEvolvableGambler2(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (1, 1, 1), - "initial_actions": (C,)} + init_parameters = {"parameters": (1, 1, 1), "initial_actions": (C,)} class TestEvolvableGambler3(TestEvolvablePlayer): @@ -575,8 +580,7 @@ class TestEvolvableGambler3(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (3, 2, 1), - "initial_actions": (C, C, C,)} + init_parameters = {"parameters": (3, 2, 1), "initial_actions": (C, C, C,)} class TestEvolvableGambler4(TestEvolvablePlayer): @@ -584,9 +588,11 @@ class TestEvolvableGambler4(TestEvolvablePlayer): player_class = axl.EvolvableGambler parent_class = axl.Gambler parent_kwargs = ["lookup_dict"] - init_parameters = {"parameters": (2, 2, 2), - "pattern": [random.random() for _ in range(64)], - "initial_actions": (C, C,)} + init_parameters = { + "parameters": (2, 2, 2), + "pattern": [random.random() for _ in range(64)], + "initial_actions": (C, C,), + } # Substitute EvolvableHMMPlayer as a regular HMMPlayer. @@ -594,7 +600,7 @@ class TestEvolvableGambler4(TestEvolvablePlayer): axl.EvolvableGambler, pattern=tables[("PSO Gambler 2_2_2", 2, 2, 2)], parameters=(2, 2, 2), - initial_actions=(C, C,) + initial_actions=(C, C,), ) diff --git a/axelrod/tests/strategies/test_geller.py b/axelrod/tests/strategies/test_geller.py index 38491fe08..0cb60d7c0 100644 --- a/axelrod/tests/strategies/test_geller.py +++ b/axelrod/tests/strategies/test_geller.py @@ -43,7 +43,9 @@ def test_strategy(self): """Should cooperate against cooperators and defect against defectors.""" self.versus_test(axl.Defector(), expected_actions=[(D, D)] * 5) self.versus_test(axl.Cooperator(), expected_actions=[(C, C)] * 5) - self.versus_test(axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5) + self.versus_test( + axl.Alternator(), expected_actions=[(C, C), (D, D)] * 5 + ) def test_strategy_against_lookerup_players(self): """ @@ -64,7 +66,9 @@ def test_returns_foil_inspection_strategy_of_opponent(self): seed=2, ) - self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)], seed=3) + self.versus_test( + axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)], seed=3 + ) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)], seed=1 @@ -91,10 +95,13 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axl.GellerDefector(), expected_actions=[(D, C), (D, C), (D, C), (D, C)] + axl.GellerDefector(), + expected_actions=[(D, C), (D, C), (D, C), (D, C)], ) - self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test( + axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)] + ) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] @@ -122,10 +129,13 @@ def test_foil_strategy_inspection(self): def test_returns_foil_inspection_strategy_of_opponent(self): self.versus_test( - axl.GellerDefector(), expected_actions=[(D, D), (D, D), (D, D), (D, D)] + axl.GellerDefector(), + expected_actions=[(D, D), (D, D), (D, D), (D, D)], ) - self.versus_test(axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)]) + self.versus_test( + axl.Darwin(), expected_actions=[(C, C), (C, C), (C, C)] + ) self.versus_test( axl.MindReader(), expected_actions=[(D, D), (D, D), (D, D)] diff --git a/axelrod/tests/strategies/test_gobymajority.py b/axelrod/tests/strategies/test_gobymajority.py index 6cd553880..88c5c555a 100644 --- a/axelrod/tests/strategies/test_gobymajority.py +++ b/axelrod/tests/strategies/test_gobymajority.py @@ -38,7 +38,9 @@ def test_memory_depth_infinite_soft_is_false(self): + [(C, C)] ) opponent = axl.MockPlayer(actions=opponent_actions) - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_memory_depth_even_soft_is_false(self): memory_depth = 4 @@ -46,7 +48,9 @@ def test_memory_depth_even_soft_is_false(self): if self.default_soft: init_kwargs["soft"] = False - opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer( + actions=[C] * memory_depth + [D] * memory_depth + ) actions = ( [(D, C)] + [(C, C)] * 3 @@ -55,7 +59,9 @@ def test_memory_depth_even_soft_is_false(self): + [(D, C)] * 3 + [(C, C)] ) - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_memory_depth_odd(self): memory_depth = 5 @@ -64,7 +70,9 @@ def test_memory_depth_odd(self): first_action = [(C, C)] else: first_action = [(D, C)] - opponent = axl.MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) + opponent = axl.MockPlayer( + actions=[C] * memory_depth + [D] * memory_depth + ) actions = ( first_action + [(C, C)] * 4 @@ -73,7 +81,9 @@ def test_memory_depth_odd(self): + [(D, C)] * 3 + [(C, C)] * 2 ) - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_default_values(self): player = self.player() @@ -90,7 +100,11 @@ class TestGoByMajority(TestHardGoByMajority): def test_memory_depth_infinite_soft_is_true(self): opponent_actions = [C] * 50 + [D] * 100 + [C] * 52 actions = ( - [(C, C)] * 50 + [(C, D)] * 51 + [(D, D)] * 49 + [(D, C)] * 50 + [(C, C)] * 2 + [(C, C)] * 50 + + [(C, D)] * 51 + + [(D, D)] * 49 + + [(D, C)] * 50 + + [(C, C)] * 2 ) opponent = axl.MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions) @@ -100,8 +114,12 @@ def test_memory_depth_even_soft_is_true(self): init_kwargs = {"memory_depth": memory_depth} opponent = axl.MockPlayer([C] * memory_depth + [D] * memory_depth) - actions = [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + actions = ( + [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 + ) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_name(self): player = self.player(soft=True) @@ -161,7 +179,9 @@ def test_strategy(self): else: cooperations = int(memory_depth * 1.5) - 1 defections = len(opponent_actions) - cooperations - 1 - player_actions = first_player_action + [C] * cooperations + [D] * defections + player_actions = ( + first_player_action + [C] * cooperations + [D] * defections + ) actions = list(zip(player_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) diff --git a/axelrod/tests/strategies/test_grudger.py b/axelrod/tests/strategies/test_grudger.py index 79194b5d8..3ec1d6ac4 100644 --- a/axelrod/tests/strategies/test_grudger.py +++ b/axelrod/tests/strategies/test_grudger.py @@ -12,7 +12,7 @@ class TestGrudger(TestPlayer): name = "Grudger" player = axl.Grudger expected_classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "makes_use_of": set(), "long_run_time": False, @@ -258,11 +258,31 @@ def test_strategy(self): """Test strategy with multiple initial parameters""" # Testing default parameters of n=1, d=4, c=2 (same as Soft Grudger) - actions = [(C, D), (D, D), (D, C), (D, C), (D, D), (C, D), (C, C), (C, C)] - self.versus_test(axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions) + actions = [ + (C, D), + (D, D), + (D, C), + (D, C), + (D, D), + (C, D), + (C, C), + (C, C), + ] + self.versus_test( + axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions + ) # Testing n=2, d=4, c=2 - actions = [(C, D), (C, D), (D, C), (D, C), (D, D), (D, D), (C, C), (C, C)] + actions = [ + (C, D), + (C, D), + (D, C), + (D, C), + (D, D), + (D, D), + (C, C), + (C, C), + ] self.versus_test( axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions, @@ -270,7 +290,16 @@ def test_strategy(self): ) # Testing n=1, d=1, c=1 - actions = [(C, D), (D, D), (C, C), (C, C), (C, D), (D, D), (C, C), (C, C)] + actions = [ + (C, D), + (D, D), + (C, C), + (C, C), + (C, D), + (D, D), + (C, C), + (C, C), + ] self.versus_test( axl.MockPlayer(actions=[D, D, C, C]), expected_actions=actions, diff --git a/axelrod/tests/strategies/test_grumpy.py b/axelrod/tests/strategies/test_grumpy.py index 1fba6bbdd..fa0e5b327 100644 --- a/axelrod/tests/strategies/test_grumpy.py +++ b/axelrod/tests/strategies/test_grumpy.py @@ -46,32 +46,42 @@ def test_starting_state(self): actions = ([(C, D)] * 11 + [(D, C)] * 22 + [(C, D)] * 11) * 3 init_kwargs = {"starting_state": "Nice"} - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) opponent = axl.MockPlayer(actions=opponent_actions) grumpy_starting = [(D, D)] * 11 + [(D, C)] * 22 + [(C, D)] * 11 actions = grumpy_starting + actions init_kwargs = {"starting_state": "Grumpy"} - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_thresholds(self): init_kwargs = {"grumpy_threshold": 3, "nice_threshold": -2} opponent_actions = [D] * 4 + [C] * 7 + [D] * 3 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 4 + [(D, C)] * 7 + [(C, D)] * 3) * 3 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) init_kwargs = {"grumpy_threshold": 0, "nice_threshold": -2} opponent_actions = [D] * 1 + [C] * 4 + [D] * 3 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 1 + [(D, C)] * 4 + [(C, D)] * 3) * 3 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) init_kwargs = {"grumpy_threshold": 3, "nice_threshold": 0} opponent_actions = [D] * 4 + [C] * 5 + [D] * 1 opponent = axl.MockPlayer(actions=opponent_actions) actions = ([(C, D)] * 4 + [(D, C)] * 5 + [(C, D)] * 1) * 3 - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) def test_reset_state_with_non_default_init(self): player = axl.Grumpy(starting_state="Grumpy") diff --git a/axelrod/tests/strategies/test_hmm.py b/axelrod/tests/strategies/test_hmm.py index e60d4e3dd..beffae2c5 100644 --- a/axelrod/tests/strategies/test_hmm.py +++ b/axelrod/tests/strategies/test_hmm.py @@ -193,17 +193,23 @@ class TestEvolvedHMM5(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) class TestEvolvedHMM5vsCooperator(TestMatch): def test_rounds(self): - self.versus_test(axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5, seed=3) + self.versus_test( + axl.EvolvedHMM5(), axl.Cooperator(), [C] * 5, [C] * 5, seed=3 + ) class TestEvolvedHMM5vsDefector(TestMatch): def test_rounds(self): - self.versus_test(axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D], seed=5) + self.versus_test( + axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D], seed=5 + ) class TestEvolvableHMMPlayer(unittest.TestCase): @@ -218,9 +224,7 @@ def test_normalized_parameters(self): initial_action = C self.assertRaises( - InsufficientParametersError, - self.player_class, - seed=1 + InsufficientParametersError, self.player_class, seed=1 ) self.assertRaises( InsufficientParametersError, @@ -229,7 +233,7 @@ def test_normalized_parameters(self): transitions_D=transitions_D, emission_probabilities=emission_probabilities, initial_state=None, - seed=1 + seed=1, ) self.assertRaises( InsufficientParametersError, @@ -238,14 +242,14 @@ def test_normalized_parameters(self): transitions_D=transitions_D, emission_probabilities=emission_probabilities, initial_action=None, - seed=1 + seed=1, ) self.assertRaises( InsufficientParametersError, self.player_class, initial_state=initial_state, initial_action=initial_action, - seed=1 + seed=1, ) def test_vector_to_instance(self): diff --git a/axelrod/tests/strategies/test_human.py b/axelrod/tests/strategies/test_human.py index e0e2ca811..a372b25ca 100644 --- a/axelrod/tests/strategies/test_human.py +++ b/axelrod/tests/strategies/test_human.py @@ -27,7 +27,9 @@ def test_validator(self): ActionValidator().validate(test_document) test_document = TestDocument("E") - self.assertRaises(ValidationError, ActionValidator().validate, test_document) + self.assertRaises( + ValidationError, ActionValidator().validate, test_document + ) class TestHumanClass(TestPlayer): diff --git a/axelrod/tests/strategies/test_inverse.py b/axelrod/tests/strategies/test_inverse.py index 95ac95033..3d89e11dd 100644 --- a/axelrod/tests/strategies/test_inverse.py +++ b/axelrod/tests/strategies/test_inverse.py @@ -28,10 +28,22 @@ def test_strategy(self): # Tests that if opponent has played all D then player chooses D, # regardless of the random seed. - self.versus_test(axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9, seed=None) + self.versus_test( + axl.Defector(), expected_actions=[(C, D)] + [(D, D)] * 9, seed=None + ) - expected_actions = [(C, D), (D, C), (D, C), (C, D), (D, C), (C, C), - (D, C), (C, C), (C, D), (D, D)] + expected_actions = [ + (C, D), + (D, C), + (D, C), + (C, D), + (D, C), + (C, C), + (D, C), + (C, C), + (C, D), + (D, D), + ] self.versus_test( axl.MockPlayer(actions=[a[1] for a in expected_actions]), expected_actions=expected_actions, diff --git a/axelrod/tests/strategies/test_lookerup.py b/axelrod/tests/strategies/test_lookerup.py index c48474388..b3fdf6c9c 100755 --- a/axelrod/tests/strategies/test_lookerup.py +++ b/axelrod/tests/strategies/test_lookerup.py @@ -20,6 +20,7 @@ C, D = axl.Action.C, axl.Action.D random = axl.RandomGenerator() + class TestLookupTable(unittest.TestCase): lookup_dict = { ((C, C), (C,), ()): C, @@ -69,7 +70,9 @@ def test_from_pattern(self): table = LookupTable.from_pattern( pattern, player_depth=2, op_depth=1, op_openings_depth=0 ) - self.assertEqual(table.dictionary, make_keys_into_plays(self.lookup_dict)) + self.assertEqual( + table.dictionary, make_keys_into_plays(self.lookup_dict) + ) def test_from_pattern_raises_error_pattern_len_ne_dict_size(self): too_big = (C,) * 17 @@ -151,7 +154,9 @@ def test_plays_equals_tuple(self): self.assertEqual(Plays(1, 2, 3), (1, 2, 3)) def test_plays_assign_values(self): - self.assertEqual(Plays(op_plays=2, self_plays=1, op_openings=3), Plays(1, 2, 3)) + self.assertEqual( + Plays(op_plays=2, self_plays=1, op_openings=3), Plays(1, 2, 3) + ) def test_make_keys_into_plays(self): old = {((C, D), (C,), ()): 1, ((D, D), (D,), ()): 2} @@ -307,7 +312,9 @@ def test_set_memory_depth(self): self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_3), 3) mem_depth_inf = axl.LookerUp(pattern="CC", parameters=Plays(0, 0, 1)) - self.assertEqual(axl.Classifiers["memory_depth"](mem_depth_inf), float("inf")) + self.assertEqual( + axl.Classifiers["memory_depth"](mem_depth_inf), float("inf") + ) def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D)] @@ -375,10 +382,13 @@ def test_opponent_starting_moves_table(self): def test_lookup_table_display(self): player = axl.LookerUp( - pattern="CCCC", parameters=Plays(self_plays=2, op_plays=0, op_openings=0) + pattern="CCCC", + parameters=Plays(self_plays=2, op_plays=0, op_openings=0), ) self.assertEqual( - player.lookup_table_display(("self_plays", "op_plays", "op_openings")), + player.lookup_table_display( + ("self_plays", "op_plays", "op_openings") + ), ( "self_plays | op_plays |op_openings\n" + " C, C , , : C,\n" @@ -420,12 +430,16 @@ def test_new_data(self): def test_vs_initial_defector(self): opponent = [D, C, C, D, D, C] expected = [(C, D), (D, C), (C, C), (D, D), (D, D), (D, C)] - self.versus_test(axl.MockPlayer(actions=opponent), expected_actions=expected) + self.versus_test( + axl.MockPlayer(actions=opponent), expected_actions=expected + ) def test_vs_initial_cooperator(self): opponent = [C, D, D, C, C, D] expected = [(C, C), (C, D), (D, D), (D, C), (D, C), (D, D)] - self.versus_test(axl.MockPlayer(actions=opponent), expected_actions=expected) + self.versus_test( + axl.MockPlayer(actions=opponent), expected_actions=expected + ) class TestEvolvedLookerUp2_2_2(TestPlayer): @@ -521,7 +535,14 @@ def test_vs_initial_defector(self): def test_vs_initial_d_c(self): opponent_actions = [D, C] + [C, D] * 3 - expected = [(C, D), (C, C)] + [(D, C), (C, D), (C, C), (D, D), (C, C), (C, D)] + expected = [(C, D), (C, C)] + [ + (D, C), + (C, D), + (C, C), + (D, D), + (C, C), + (C, D), + ] self.versus_test( axl.MockPlayer(actions=opponent_actions), expected_actions=expected ) @@ -604,7 +625,9 @@ def test_strategy(self): vs_alternator = [(D, C), (C, D)] + [(D, C), (D, D)] * 5 self.versus_test(axl.Alternator(), expected_actions=vs_alternator) - self.versus_test(axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10) + self.versus_test( + axl.Cooperator(), expected_actions=[(D, C)] + [(C, C)] * 10 + ) self.versus_test( axl.Defector(), expected_actions=([(D, D), (C, D)] + [(D, D)] * 10) @@ -616,7 +639,11 @@ def test_convert_key(self): opponent_starting_plays = "" player_last_plays = "CC" opponent_last_plays = "D" - old_key = (opponent_starting_plays, player_last_plays, opponent_last_plays) + old_key = ( + opponent_starting_plays, + player_last_plays, + opponent_last_plays, + ) new_key = Plays(self_plays=(C, C), op_plays=(D,), op_openings=()) @@ -665,22 +692,20 @@ def test_normalized_parameters(self): pattern = ("".join([random.choice(("C", "D")) for _ in range(8)]),) self.assertRaises( - InsufficientParametersError, - self.player_class, - seed=1 + InsufficientParametersError, self.player_class, seed=1 ) self.assertRaises( InsufficientParametersError, self.player_class, pattern=pattern, initial_actions=initial_actions, - seed=1 + seed=1, ) self.assertRaises( InsufficientParametersError, self.player_class, lookup_dict=lookup_dict, - seed=1 + seed=1, ) diff --git a/axelrod/tests/strategies/test_memoryone.py b/axelrod/tests/strategies/test_memoryone.py index 01e720618..f1e6513e9 100644 --- a/axelrod/tests/strategies/test_memoryone.py +++ b/axelrod/tests/strategies/test_memoryone.py @@ -69,11 +69,15 @@ class TestGTFT(TestPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) def test_strategy2(self): actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) def test_four_vector(self): (R, P, S, T) = axl.Game().RPST() @@ -111,11 +115,15 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, D), (D, D), (D, D), (C, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=10 + ) def test_strategy3(self): actions = [(C, D), (D, D), (C, D), (D, D), (D, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=1 + ) class TestStochasticCooperator(TestPlayer): @@ -143,19 +151,27 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (C, C), (D, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=113) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=113 + ) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) def test_strategy3(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=5) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=5 + ) def test_strategy4(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=12) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=12 + ) class TestStochasticWSLS(TestPlayer): @@ -174,19 +190,27 @@ class TestStochasticWSLS(TestPlayer): def test_strategy(self): actions = [(C, C), (D, D), (C, C), (C, D), (D, C), (D, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=50) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=50 + ) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=2 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (D, C), (D, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=23) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=23 + ) def test_four_vector(self): player = self.player() @@ -204,7 +228,8 @@ class TestMemoryOnePlayer(unittest.TestCase): def test_default_if_four_vector_not_set(self): player = MemoryOnePlayer() self.assertEqual( - player._four_vector, {(C, C): 1.0, (C, D): 0.0, (D, C): 0.0, (D, D): 1.0} + player._four_vector, + {(C, C): 1.0, (C, D): 0.0, (D, C): 0.0, (D, D): 1.0}, ) def test_exception_if_four_vector_not_set(self): @@ -246,11 +271,15 @@ def test_four_vector(self): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) def test_strategy2(self): actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=5) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=5 + ) class TestALLCorALLD(TestPlayer): @@ -269,11 +298,15 @@ class TestALLCorALLD(TestPlayer): def test_strategy(self): actions = [(D, C)] * 10 - self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=0) + self.versus_test( + opponent=axl.Cooperator(), expected_actions=actions, seed=0 + ) def test_strategy2(self): actions = [(C, C)] * 10 - self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Cooperator(), expected_actions=actions, seed=1 + ) class TestGenericReactiveStrategy(unittest.TestCase): @@ -287,13 +320,16 @@ class TestGenericReactiveStrategy(unittest.TestCase): def test_four_vector(self): self.assertEqual( - self.p1._four_vector, {(C, D): 0.0, (D, C): 0.0, (C, C): 0.0, (D, D): 0.0} + self.p1._four_vector, + {(C, D): 0.0, (D, C): 0.0, (C, C): 0.0, (D, D): 0.0}, ) self.assertEqual( - self.p2._four_vector, {(C, D): 0.0, (D, C): 1.0, (C, C): 1.0, (D, D): 0.0} + self.p2._four_vector, + {(C, D): 0.0, (D, C): 1.0, (C, C): 1.0, (D, D): 0.0}, ) self.assertEqual( - self.p3._four_vector, {(C, D): 0.5, (D, C): 1.0, (C, C): 1.0, (D, D): 0.5} + self.p3._four_vector, + {(C, D): 0.5, (D, C): 1.0, (C, C): 1.0, (D, D): 0.5}, ) def test_subclass(self): @@ -304,6 +340,7 @@ def test_subclass(self): class TestMemoryOneAlternator(TestAlternator): """Alternator is equivalent to MemoryOnePlayer((0, 0, 1, 1), C)""" + name = "Generic Memory One Player: (0, 0, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 1, 1)) expected_classifier = { @@ -319,6 +356,7 @@ class TestMemoryOneAlternator(TestAlternator): class TestMemoryOneCooperator(TestCooperator): """Cooperator is equivalent to MemoryOnePlayer((1, 1, 1, 1), C)""" + name = "Generic Memory One Player: (1, 1, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 1, 1, 1)) expected_classifier = { @@ -334,6 +372,7 @@ class TestMemoryOneCooperator(TestCooperator): class TestMemoryOneDefector(TestDefector): """Defector is equivalent to MemoryOnePlayer((0, 0, 0, 0), D)""" + name = "Generic Memory One Player: (0, 0, 0, 0), D" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0), initial=D) expected_classifier = { @@ -349,6 +388,7 @@ class TestMemoryOneDefector(TestDefector): class TestMemoryOneTitForTat(TestTitForTat): """TitForTat is equivalent to MemoryOnePlayer((1, 0, 1, 0), C)""" + name = "Generic Memory One Player: (1, 0, 1, 0), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) expected_classifier = { @@ -364,6 +404,7 @@ class TestMemoryOneTitForTat(TestTitForTat): class TestMemoryOneWSLS(TestWinStayLoseShift): """WinStayLoseShift is equivalent to MemoryOnePlayer((1, 0, 0, 1), C)""" + name = "Generic Memory One Player: (1, 0, 0, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 0, 1)) expected_classifier = { diff --git a/axelrod/tests/strategies/test_memorytwo.py b/axelrod/tests/strategies/test_memorytwo.py index 339aef728..ee5265eac 100644 --- a/axelrod/tests/strategies/test_memorytwo.py +++ b/axelrod/tests/strategies/test_memorytwo.py @@ -80,9 +80,7 @@ def test_exception_if_probability_vector_outside_valid_values(self): class TestMemoryStochastic(TestPlayer): - name = ( - "Generic Memory Two Player" - ) + name = "Generic Memory Two Player" player = axl.MemoryTwoPlayer expected_classifier = { "memory_depth": 0, # Memory-two Sixteen-Vector @@ -156,7 +154,9 @@ def test_strategy(self): # tests states 3, 5 and 12 actions = [(C, D), (C, C), (D, C), (D, D), (D, D), (C, D)] - self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions) + self.versus_test( + opponent=axl.SuspiciousTitForTat(), expected_actions=actions + ) # tests state 1 actions = [(C, C), (C, C), (C, C), (C, C)] @@ -189,12 +189,15 @@ def test_strategy_mutually_cooperative(self): # tests states 1, 4 and 8 actions = [(C, D), (C, D), (D, D), (C, C), (C, C), (C, D)] self.versus_test( - opponent=axl.Cycler(["D", "D", "D", "C", "C"]), expected_actions=actions + opponent=axl.Cycler(["D", "D", "D", "C", "C"]), + expected_actions=actions, ) # tests states 3, 5 actions = [(C, D), (C, C), (D, C), (D, D), (C, D)] - self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions) + self.versus_test( + opponent=axl.SuspiciousTitForTat(), expected_actions=actions + ) class TestMEM2(TestPlayer): @@ -256,8 +259,11 @@ def test_strategy(self): class TestMemoryTwoCooperator(TestCooperator): """Cooperator is equivalent to MemoryTwoPlayer((1, 1, ..., 1), C)""" + name = "Generic Memory Two Player: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], (C, C)" - player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=[1] * 16, initial=(C, C)) + player = lambda x: axl.MemoryTwoPlayer( + sixteen_vector=[1] * 16, initial=(C, C) + ) expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -271,8 +277,11 @@ class TestMemoryTwoCooperator(TestCooperator): class TestMemoryTwoDefector(TestDefector): """Defector is equivalent to MemoryTwoPlayer((0, 0, ..., 0), D)""" + name = "Generic Memory Two Player: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], (D, D)" - player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=[0] * 16, initial=(D, D)) + player = lambda x: axl.MemoryTwoPlayer( + sixteen_vector=[0] * 16, initial=(D, D) + ) expected_classifier = { "memory_depth": 0, "stochastic": False, @@ -292,8 +301,12 @@ def four_vector_to_sixteen_vector(four_vector): class TestMemoryTwoAlternator(TestAlternator): """Alternator is equivalent to MemoryTwoPlayer(0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1), C).""" + name = "Generic Memory Two Player: [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1], (C, D)" - player = lambda x: axl.MemoryTwoPlayer(sixteen_vector=four_vector_to_sixteen_vector((0, 0, 1, 1)), initial=(C, D)) + player = lambda x: axl.MemoryTwoPlayer( + sixteen_vector=four_vector_to_sixteen_vector((0, 0, 1, 1)), + initial=(C, D), + ) expected_classifier = { "memory_depth": 1, "stochastic": False, diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 6ec22db6b..3cb2c8241 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -41,7 +41,9 @@ def classifier_test(self, expected_class_classifier=None): for t in player.team: try: - classifier["makes_use_of"].update(axl.Classifiers["make_use_of"](t)) + classifier["makes_use_of"].update( + axl.Classifiers["make_use_of"](t) + ) except KeyError: pass @@ -250,7 +252,6 @@ class TestNiceMetaWinnerEnsemble(TestMetaPlayer): "manipulates_state": False, } - def test_strategy(self): actions = [(C, C)] * 8 self.versus_test( @@ -370,7 +371,9 @@ def test_strategy(self): opponent = axl.MockPlayer([C] * 100 + [D]) actions = [(C, C)] * 100 + [(C, D), (D, C)] self.versus_test( - opponent=opponent, expected_actions=actions, init_kwargs={"team": team} + opponent=opponent, + expected_actions=actions, + init_kwargs={"team": team}, ) @@ -389,7 +392,9 @@ class TestMetaMajorityMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaMajorityFiniteMemory(TestMetaPlayer): @@ -407,7 +412,9 @@ class TestMetaMajorityFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) class TestMetaMajorityLongMemory(TestMetaPlayer): @@ -425,11 +432,15 @@ class TestMetaMajorityLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=0) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=0 + ) def test_strategy2(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaWinnerMemoryOne(TestMetaPlayer): @@ -447,7 +458,9 @@ class TestMetaWinnerMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaWinnerFiniteMemory(TestMetaPlayer): @@ -465,7 +478,9 @@ class TestMetaWinnerFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaWinnerLongMemory(TestMetaPlayer): @@ -483,7 +498,9 @@ class TestMetaWinnerLongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=4) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=4 + ) class TestMetaWinnerDeterministic(TestMetaPlayer): @@ -519,7 +536,9 @@ class TestMetaWinnerStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=1 + ) class TestMetaMixer(TestMetaPlayer): @@ -595,7 +614,12 @@ def test_strategy(self): ) team.append(axl.Defector) - distribution = [0.2, 0.5, 0.3, 0] # If add a defector but does not occur + distribution = [ + 0.2, + 0.5, + 0.3, + 0, + ] # If add a defector but does not occur self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, @@ -639,7 +663,9 @@ def classifier_test(self, expected_class_classifier=None): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=11) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=11 + ) class TestNMWEStochastic(TestMetaPlayer): @@ -657,7 +683,9 @@ class TestNMWEStochastic(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=16) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=16 + ) class TestNMWEFiniteMemory(TestMetaPlayer): @@ -675,7 +703,9 @@ class TestNMWEFiniteMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=7) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=7 + ) class TestNMWELongMemory(TestMetaPlayer): @@ -693,7 +723,9 @@ class TestNMWELongMemory(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=3) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=3 + ) class TestNMWEMemoryOne(TestMetaPlayer): @@ -711,7 +743,9 @@ class TestNMWEMemoryOne(TestMetaPlayer): def test_strategy(self): actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] - self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Alternator(), expected_actions=actions, seed=2 + ) class TestMemoryDecay(TestPlayer): @@ -820,5 +854,5 @@ def test_memory_alter_delete(self): opponent, expected_actions=actions, init_kwargs={"start_strategy": axl.Cooperator}, - seed=11 + seed=11, ) diff --git a/axelrod/tests/strategies/test_negation.py b/axelrod/tests/strategies/test_negation.py index 8c7542aaa..f74211b46 100644 --- a/axelrod/tests/strategies/test_negation.py +++ b/axelrod/tests/strategies/test_negation.py @@ -36,4 +36,6 @@ def test_strategy(self): opponent=axl.Cooperator(), expected_actions=actions, seed=1 ) actions = [(D, D), (C, D), (C, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=2 + ) diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index ec9fe051c..76c06e945 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -458,14 +458,22 @@ def test_reset_clone(self): clone = player.clone() self.assertEqual(player, clone) - @given(seed=integers(min_value=1, max_value=20000000), - turns=integers(min_value=5, max_value=10), - noise=integers(min_value=0, max_value=10)) + @given( + seed=integers(min_value=1, max_value=20000000), + turns=integers(min_value=5, max_value=10), + noise=integers(min_value=0, max_value=10), + ) @settings(max_examples=1, deadline=None) def test_clone_reproducible_play(self, seed, turns, noise): # Test that the cloned player produces identical play player = self.player() - if player.name in ["Darwin", "Human", "Mind Bender", "Mind Controller", "Mind Warper"]: + if player.name in [ + "Darwin", + "Human", + "Mind Bender", + "Mind Controller", + "Mind Warper", + ]: # Known exceptions return @@ -479,8 +487,15 @@ def test_clone_reproducible_play(self, seed, turns, noise): player_clone = player.clone() op = op.clone() op_clone = op.clone() - m1 = axl.Match((player, op), turns=turns, seed=seed, noise=noise/100.) - m2 = axl.Match((player_clone, op_clone), turns=turns, seed=seed, noise=noise/100.) + m1 = axl.Match( + (player, op), turns=turns, seed=seed, noise=noise / 100.0 + ) + m2 = axl.Match( + (player_clone, op_clone), + turns=turns, + seed=seed, + noise=noise / 100.0, + ) m1.play() m2.play() self.assertEqual(m1.result, m2.result) @@ -577,7 +592,7 @@ def versus_test( noise=noise, seed=seed, attrs=attrs, - match_attributes=match_attributes + match_attributes=match_attributes, ) def classifier_test(self, expected_class_classifier=None): @@ -610,13 +625,14 @@ def classifier_test(self, expected_class_classifier=None): axl.Classifiers[key](player), self.expected_classifier[key], msg="%s - Behaviour: %s != Expected Behaviour: %s" - % ( - key, - axl.Classifiers[key](player), - self.expected_classifier[key], - ), + % ( + key, + axl.Classifiers[key](player), + self.expected_classifier[key], + ), ) + class TestMatch(unittest.TestCase): """Test class for heads up play between two given players. Plays an axelrod match between the two players.""" @@ -631,7 +647,7 @@ def versus_test( noise=None, seed=None, match_attributes=None, - attrs=None + attrs=None, ): """Tests a sequence of outcomes for two given players.""" if len(expected_actions1) != len(expected_actions2): @@ -640,14 +656,22 @@ def versus_test( turns = len(expected_actions1) match = axl.Match( - (player1, player2), turns=turns, noise=noise, seed=seed, - match_attributes=match_attributes) + (player1, player2), + turns=turns, + noise=noise, + seed=seed, + match_attributes=match_attributes, + ) match.play() # Test expected sequence of plays from the match is as expected. - for i, (play, expected_play) in enumerate(zip(player1.history, expected_actions1)): + for i, (play, expected_play) in enumerate( + zip(player1.history, expected_actions1) + ): self.assertEqual((i, play), (i, expected_play)) - for i, (play, expected_play) in enumerate(zip(player2.history, expected_actions2)): + for i, (play, expected_play) in enumerate( + zip(player2.history, expected_actions2) + ): self.assertEqual((i, play), (i, expected_play)) # Test final player attributes are as expected diff --git a/axelrod/tests/strategies/test_prober.py b/axelrod/tests/strategies/test_prober.py index 229b3fe1e..7f9e3dd48 100644 --- a/axelrod/tests/strategies/test_prober.py +++ b/axelrod/tests/strategies/test_prober.py @@ -194,7 +194,28 @@ class TestProber4(TestPlayer): "manipulates_source": False, "manipulates_state": False, } - initial_sequence = [C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D] + initial_sequence = [ + C, + C, + D, + C, + D, + D, + D, + C, + C, + D, + C, + D, + C, + C, + D, + C, + D, + D, + C, + D, + ] def test_strategy(self): # Starts by playing CCDCDDDCCDCDCCDCDDCD. @@ -214,7 +235,9 @@ def test_strategy(self): for history in provocative_histories: opponent = axl.MockPlayer(history + [C] * 5) actions = list(zip(self.initial_sequence, history)) + [(D, C)] * 5 - self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) + self.versus_test( + opponent=opponent, expected_actions=actions, attrs=attrs + ) # Otherwise cooperates for 5 rounds and plays TfT afterwards unprovocative_histories = [ @@ -230,7 +253,9 @@ def test_strategy(self): opponent = axl.MockPlayer(history + [D] * 5 + [C, C]) actions = list(zip(self.initial_sequence, history)) + [(C, D)] * 5 actions += [(D, C), (C, C)] - self.versus_test(opponent=opponent, expected_actions=actions, attrs=attrs) + self.versus_test( + opponent=opponent, expected_actions=actions, attrs=attrs + ) class TestHardProber(TestPlayer): diff --git a/axelrod/tests/strategies/test_qlearner.py b/axelrod/tests/strategies/test_qlearner.py index 948c8c02e..c44c64c77 100644 --- a/axelrod/tests/strategies/test_qlearner.py +++ b/axelrod/tests/strategies/test_qlearner.py @@ -41,7 +41,13 @@ def test_strategy(self): "CC2.0": {C: 2.7, D: 0}, "CCC3.0": {C: 0, D: 0}, }, - "Vs": {"": 0.9, "0.0": 2.7, "C1.0": 4.5, "CC2.0": 2.7, "CCC3.0": 0}, + "Vs": { + "": 0.9, + "0.0": 2.7, + "C1.0": 4.5, + "CC2.0": 2.7, + "CCC3.0": 0, + }, "prev_state": "CCC3.0", }, ) @@ -75,7 +81,13 @@ def test_strategy(self): "CC2.0": {C: 2.7, D: 0}, "CCC3.0": {C: 0, D: 0}, }, - "Vs": {"": 0.9, "0.0": 2.7, "C1.0": 4.5, "CC2.0": 2.7, "CCC3.0": 0}, + "Vs": { + "": 0.9, + "0.0": 2.7, + "C1.0": 4.5, + "CC2.0": 2.7, + "CCC3.0": 0, + }, "prev_state": "CCC3.0", }, ) @@ -109,7 +121,13 @@ def test_strategy(self): "DD0.0": {C: 0, D: 0}, "DDD0.0": {C: 0, D: 0}, }, - "Vs": {"": 0.1, "0.0": 0.0, "D0.0": 0.1, "DD0.0": 0.0, "DDD0.0": 0}, + "Vs": { + "": 0.1, + "0.0": 0.0, + "D0.0": 0.1, + "DD0.0": 0.0, + "DDD0.0": 0, + }, "prev_state": "DDD0.0", }, ) @@ -143,7 +161,13 @@ def test_strategy(self): "DD0.0": {C: 0, D: 0}, "DDD0.0": {C: 0, D: 0}, }, - "Vs": {"": 0.1, "0.0": 0.0, "D0.0": 0.1, "DD0.0": 0.0, "DDD0.0": 0}, + "Vs": { + "": 0.1, + "0.0": 0.0, + "D0.0": 0.1, + "DD0.0": 0.0, + "DDD0.0": 0, + }, "prev_state": "DDD0.0", }, ) diff --git a/axelrod/tests/strategies/test_rand.py b/axelrod/tests/strategies/test_rand.py index c9341ab87..03754a169 100644 --- a/axelrod/tests/strategies/test_rand.py +++ b/axelrod/tests/strategies/test_rand.py @@ -23,10 +23,14 @@ class TestRandom(TestPlayer): def test_deterministic(self): actions = [(D, C), (D, C), (D, C)] - self.versus_test(axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0}) + self.versus_test( + axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 0} + ) actions = [(C, C), (C, C), (C, C)] - self.versus_test(axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1}) + self.versus_test( + axl.Cooperator(), expected_actions=actions, init_kwargs={"p": 1} + ) def test_stochastic_behavior1(self): """Test that strategy is randomly picked (not affected by history).""" diff --git a/axelrod/tests/strategies/test_retaliate.py b/axelrod/tests/strategies/test_retaliate.py index cd8660733..e3918f1ff 100644 --- a/axelrod/tests/strategies/test_retaliate.py +++ b/axelrod/tests/strategies/test_retaliate.py @@ -93,14 +93,18 @@ def test_strategy(self): opponent = axl.Cooperator() actions = [(C, C)] * 5 self.versus_test( - opponent=opponent, expected_actions=actions, attrs={"retaliating": False} + opponent=opponent, + expected_actions=actions, + attrs={"retaliating": False}, ) # Retaliate after a (C, D) round. opponent = axl.MockPlayer([C, C, C, D, C]) actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)] self.versus_test( - opponent=opponent, expected_actions=actions, attrs={"retaliating": True} + opponent=opponent, + expected_actions=actions, + attrs={"retaliating": True}, ) opponent = axl.Alternator() @@ -108,7 +112,9 @@ def test_strategy(self): # Count retaliations actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] self.versus_test( - opponent=opponent, expected_actions=actions, attrs={"retaliation_count": 3} + opponent=opponent, + expected_actions=actions, + attrs={"retaliation_count": 3}, ) opponent = axl.Alternator() diff --git a/axelrod/tests/strategies/test_revised_downing.py b/axelrod/tests/strategies/test_revised_downing.py index fa6897a85..8006e03ef 100644 --- a/axelrod/tests/strategies/test_revised_downing.py +++ b/axelrod/tests/strategies/test_revised_downing.py @@ -4,6 +4,7 @@ C, D = axl.Action.C, axl.Action.D + class TestRevisedDowning(TestPlayer): name = "Revised Downing" diff --git a/axelrod/tests/strategies/test_selfsteem.py b/axelrod/tests/strategies/test_selfsteem.py index d45730247..76b6e5db2 100644 --- a/axelrod/tests/strategies/test_selfsteem.py +++ b/axelrod/tests/strategies/test_selfsteem.py @@ -23,20 +23,33 @@ class TestSelfSteem(TestPlayer): def test_strategy1(self): # Check for f > 0.95, defect actions = ( - [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] + [(C, C)] * 4 + [(D, C)] + [(C, C), (C, C), (D, C), (D, C), (C, C), (D, C)] + + [(C, C)] * 4 + + [(D, C)] ) self.versus_test(axl.Cooperator(), expected_actions=actions, seed=1) def test_strategy2(self): # Check for f < -0.95, cooperate - actions = [(D, C), (C, C), (D, C), (D, C), (C, C), (D, C), (C, C), (C, C)] + actions = [ + (D, C), + (C, C), + (D, C), + (D, C), + (C, C), + (D, C), + (C, C), + (C, C), + ] self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, seed=10 ) def test_strategy3(self): actions = [(D, D)] + [(D, D)] * 5 + [(D, D), (C, D), (C, D)] - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=10 + ) def test_strategy4(self): # Check for -0.3 < f < 0.3, random @@ -57,7 +70,9 @@ def test_strategy5(self): + [(D, D)] * 8 + [(C, D), (C, D), (D, D), (D, D), (D, D)] ) - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=32) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=32 + ) def test_strategy6(self): # Check for 0.95 > abs(f) > 0.3, follows TitForTat @@ -66,7 +81,9 @@ def test_strategy6(self): + [(C, D), (D, D), (C, D), (C, D), (D, D), (C, D)] + [(D, D)] * 5 ) - self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=17) + self.versus_test( + opponent=axl.Defector(), expected_actions=actions, seed=17 + ) def test_strategy7(self): actions = [ @@ -81,4 +98,6 @@ def test_strategy7(self): (C, C), (C, C), ] - self.versus_test(opponent=axl.Cooperator(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.Cooperator(), expected_actions=actions, seed=10 + ) diff --git a/axelrod/tests/strategies/test_shortmem.py b/axelrod/tests/strategies/test_shortmem.py index 48dcd0138..525aea843 100644 --- a/axelrod/tests/strategies/test_shortmem.py +++ b/axelrod/tests/strategies/test_shortmem.py @@ -12,7 +12,7 @@ class TestShortMem(TestPlayer): name = "ShortMem" player = axl.ShortMem expected_classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "makes_use_of": set(), "inspects_source": False, @@ -44,14 +44,26 @@ def test_strategy(self): ) # If neither of the above conditions are met, apply TitForTat - actions = [(C, D)] * 5 + [(C, C)] * 6 + [(C, D), (D, D), (D, D), (D, C), (C, C)] + actions = ( + [(C, D)] * 5 + + [(C, C)] * 6 + + [(C, D), (D, D), (D, D), (D, C), (C, C)] + ) self.versus_test( - opponent=axl.MockPlayer(actions=[D] * 5 + [C] * 6 + [D, D, D, C, C]), + opponent=axl.MockPlayer( + actions=[D] * 5 + [C] * 6 + [D, D, D, C, C] + ), expected_actions=actions, ) - actions = [(C, C)] * 5 + [(C, D)] * 6 + [(D, C), (C, C), (C, C), (C, D), (D, D)] + actions = ( + [(C, C)] * 5 + + [(C, D)] * 6 + + [(D, C), (C, C), (C, C), (C, D), (D, D)] + ) self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 5 + [D] * 6 + [C, C, C, D, D]), + opponent=axl.MockPlayer( + actions=[C] * 5 + [D] * 6 + [C, C, C, D, D] + ), expected_actions=actions, ) diff --git a/axelrod/tests/strategies/test_stalker.py b/axelrod/tests/strategies/test_stalker.py index 59b6d1370..21e90dfde 100644 --- a/axelrod/tests/strategies/test_stalker.py +++ b/axelrod/tests/strategies/test_stalker.py @@ -28,12 +28,14 @@ def test_strategy(self): # wish_score < current_average_score < very_good_score actions = [(C, C)] * 7 + [(C, D), (C, D), (C, C), (C, C), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 2), expected_actions=actions + opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 2), + expected_actions=actions, ) actions = [(C, C)] * 7 + [(C, D), (C, C), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D]), expected_actions=actions + opponent=axl.MockPlayer(actions=[C] * 7 + [D]), + expected_actions=actions, ) # current_average_score > 2 @@ -43,7 +45,8 @@ def test_strategy(self): # 1 < current_average_score < 2 actions = [(C, C)] * 7 + [(C, D)] * 4 + [(D, D)] self.versus_test( - opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 5), expected_actions=actions + opponent=axl.MockPlayer(actions=[C] * 7 + [D] * 5), + expected_actions=actions, ) def test_strategy2(self): @@ -75,7 +78,9 @@ def test_strategy4(self): # defect in last round actions = [(C, C)] * 199 + [(D, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, match_attributes={"length": 200} + axl.Cooperator(), + expected_actions=actions, + match_attributes={"length": 200}, ) # Force a defection before the end of the actual match which ensures diff --git a/axelrod/tests/strategies/test_titfortat.py b/axelrod/tests/strategies/test_titfortat.py index fb984ea4b..a13c06568 100644 --- a/axelrod/tests/strategies/test_titfortat.py +++ b/axelrod/tests/strategies/test_titfortat.py @@ -386,7 +386,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, C), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, @@ -394,7 +403,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, D), + (D, D), + (D, D), + ] self.versus_test( opponent, expected_actions=actions, @@ -471,7 +489,9 @@ def test_specific_set_of_results(self): to a memory one player that start by defecting and only cooperates if both players cooperated in the previous round. """ - mistrust_with_bug = axl.MemoryOnePlayer(initial=D, four_vector=(1, 0, 0, 0),) + mistrust_with_bug = axl.MemoryOnePlayer( + initial=D, four_vector=(1, 0, 0, 0), + ) players = [ self.player(), axl.TitForTat(), @@ -614,7 +634,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, C), + (C, D), + (D, C), + ] self.versus_test( opponent, expected_actions=actions, @@ -627,7 +656,16 @@ def test_strategy(self): ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) - actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] + actions = [ + (C, D), + (D, C), + (C, D), + (C, C), + (C, C), + (C, D), + (D, D), + (D, D), + ] self.versus_test( opponent, expected_actions=actions, @@ -669,7 +707,9 @@ def test_output_from_literature(self): ] turns = 1000 - tournament = axl.Tournament(players, turns=turns, repetitions=1, seed=75) + tournament = axl.Tournament( + players, turns=turns, repetitions=1, seed=75 + ) results = tournament.play(progress_bar=False) scores = [ round(average_score_per_turn * 1000, 1) @@ -714,8 +754,10 @@ def test_init(self): self.assertEqual(player._recorded_history, []) @given( - strategies=strategy_lists(strategies=deterministic_strategies, max_size=1), - turns=integers(min_value=1, max_value=20) + strategies=strategy_lists( + strategies=deterministic_strategies, max_size=1 + ), + turns=integers(min_value=1, max_value=20), ) @settings(deadline=None) def test_is_tit_for_tat_with_no_noise(self, strategies, turns): @@ -727,33 +769,59 @@ def test_is_tit_for_tat_with_no_noise(self, strategies, turns): self.assertEqual(m1.play(), m2.play()) def test_strategy_with_noise1(self): - self.versus_test(axl.Defector(), [(C, D)], turns=1, seed=9, - attrs={"_recorded_history": [C]}) + self.versus_test( + axl.Defector(), + [(C, D)], + turns=1, + seed=9, + attrs={"_recorded_history": [C]}, + ) def test_strategy_with_noise2(self): - self.versus_test(axl.Defector(), [(D, C)], turns=1, noise=0.5, seed=11, - attrs={"_recorded_history": [C]}) + self.versus_test( + axl.Defector(), + [(D, C)], + turns=1, + noise=0.5, + seed=11, + attrs={"_recorded_history": [C]}, + ) def test_strategy_with_noise3(self): # After noise: is contrite actions = list(zip([D, C], [C, D])) - self.versus_test(axl.Defector(), actions, turns=2, noise=0.5, seed=49, - attrs={"_recorded_history": [C, C], - "contrite": True}) + self.versus_test( + axl.Defector(), + actions, + turns=2, + noise=0.5, + seed=49, + attrs={"_recorded_history": [C, C], "contrite": True}, + ) def test_strategy_with_noise4(self): # Cooperates and no longer contrite actions = list(zip([D, C, C], [C, D, D])) - self.versus_test(axl.Defector(), actions, turns=3, noise=0.5, seed=49, - attrs={"_recorded_history": [C, C, C], - "contrite": False}) + self.versus_test( + axl.Defector(), + actions, + turns=3, + noise=0.5, + seed=49, + attrs={"_recorded_history": [C, C, C], "contrite": False}, + ) def test_strategy_with_noise5(self): # Defects and no longer contrite actions = list(zip([D, C, C, D], [C, D, D, D])) - self.versus_test(axl.Defector(), actions, turns=4, noise=0.5, seed=158, - attrs={"_recorded_history": [C, C, C, D], - "contrite": False}) + self.versus_test( + axl.Defector(), + actions, + turns=4, + noise=0.5, + seed=158, + attrs={"_recorded_history": [C, C, C, D], "contrite": False}, + ) class TestAdaptiveTitForTat(TestPlayer): @@ -900,7 +968,9 @@ class TestEugineNier(TestPlayer): def test_strategy(self): actions = [(C, C), (C, C), (C, C), (D, C)] self.versus_test( - axl.Cooperator(), expected_actions=actions, attrs={"is_defector": False} + axl.Cooperator(), + expected_actions=actions, + attrs={"is_defector": False}, ) actions = [(C, C), (C, C), (C, C), (C, C)] @@ -914,7 +984,9 @@ def test_strategy(self): # Plays TfT and defects in last round actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)] self.versus_test( - axl.Alternator(), expected_actions=actions, attrs={"is_defector": False} + axl.Alternator(), + expected_actions=actions, + attrs={"is_defector": False}, ) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)] @@ -927,7 +999,16 @@ def test_strategy(self): # Becomes defector after 5 defections opponent = axl.MockPlayer(actions=[D, C, D, D, D, D, C, C]) - actions = [(C, D), (D, C), (C, D), (D, D), (D, D), (D, D), (D, C), (D, C)] + actions = [ + (C, D), + (D, C), + (C, D), + (D, D), + (D, D), + (D, D), + (D, C), + (D, C), + ] self.versus_test(opponent, expected_actions=actions) @@ -955,13 +1036,17 @@ def test_strategy(self): init_kwargs = {"N": 1, "M": 2} opponent = axl.MockPlayer(actions=[D, D, D, C, C]) actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) # TwoTitsForTat test_strategy init_kwargs = {"N": 2, "M": 1} opponent = axl.MockPlayer(actions=[D, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] - self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) + self.versus_test( + opponent, expected_actions=actions, init_kwargs=init_kwargs + ) actions = [(C, C), (C, C)] self.versus_test( opponent=axl.Cooperator(), @@ -970,7 +1055,9 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (D, D)] self.versus_test( - opponent=axl.Defector(), expected_actions=actions, init_kwargs=init_kwargs, + opponent=axl.Defector(), + expected_actions=actions, + init_kwargs=init_kwargs, ) # Cooperator test_strategy @@ -1017,6 +1104,7 @@ def test_varying_memory_depth(self): class Test1TitsFor1TatsIsTFT(TestTitForTat): """Tests that for N = 1 = M, all the TFT tests are passed.""" + name = "N Tit(s) For M Tat(s): 1, 1" player = lambda x: axl.NTitsForMTats(1, 1) expected_classifier = { @@ -1032,6 +1120,7 @@ class Test1TitsFor1TatsIsTFT(TestTitForTat): class Test1TitsFor2TatsIsTF2T(TestTitFor2Tats): """Tests that for N = 1, M = 2, all the TF2T tests are passed.""" + name = "N Tit(s) For M Tat(s): 1, 2" player = lambda x: axl.NTitsForMTats(1, 2) expected_classifier = { @@ -1047,6 +1136,7 @@ class Test1TitsFor2TatsIsTF2T(TestTitFor2Tats): class Test2TitsFor1TatsIs2TFT(TestTwoTitsForTat): """Tests that for N = 2, M = 1, all the 2TFT tests are passed.""" + name = "N Tit(s) For M Tat(s): 2, 1" player = lambda x: axl.NTitsForMTats(2, 1) expected_classifier = { @@ -1165,7 +1255,9 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (D, D)] - self.versus_test(axl.Defector(), expected_actions=actions, init_kwargs={"p": 0}) + self.versus_test( + axl.Defector(), expected_actions=actions, init_kwargs={"p": 0} + ) actions = [(C, C), (C, C), (D, C), (C, C)] self.versus_test( @@ -1173,7 +1265,9 @@ def test_strategy(self): ) actions = [(C, D), (D, D), (C, D), (D, D)] - self.versus_test(axl.Defector(), expected_actions=actions, init_kwargs={"p": 1}) + self.versus_test( + axl.Defector(), expected_actions=actions, init_kwargs={"p": 1} + ) actions = [(C, C), (C, C), (D, C), (C, C), (C, C), (C, C)] self.versus_test(axl.Cooperator(), expected_actions=actions, seed=2) diff --git a/axelrod/tests/strategies/test_worse_and_worse.py b/axelrod/tests/strategies/test_worse_and_worse.py index 1fd2d5ef8..fa87429ef 100644 --- a/axelrod/tests/strategies/test_worse_and_worse.py +++ b/axelrod/tests/strategies/test_worse_and_worse.py @@ -101,7 +101,8 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, C), (C, C), (C, D), (D, C)] self.versus_test( - opponent=axl.MockPlayer(actions=[C, C, D, C]), expected_actions=actions + opponent=axl.MockPlayer(actions=[C, C, D, C]), + expected_actions=actions, ) def test_strategy3(self): diff --git a/axelrod/tests/strategies/test_zero_determinant.py b/axelrod/tests/strategies/test_zero_determinant.py index 75b98fb5f..2d9bbac5c 100644 --- a/axelrod/tests/strategies/test_zero_determinant.py +++ b/axelrod/tests/strategies/test_zero_determinant.py @@ -32,7 +32,12 @@ class TestZDExtortion(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 0.64, (C, D): 0.18, (D, C): 0.28, (D, D): 0} + expected_dictionary = { + (C, C): 0.64, + (C, D): 0.18, + (D, C): 0.28, + (D, D): 0, + } test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -43,7 +48,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=1) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=1 + ) class TestZDExtort2(TestPlayer): @@ -61,7 +68,12 @@ class TestZDExtort2(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 8 / 9, (C, D): 0.5, (D, C): 1 / 3, (D, D): 0.0} + expected_dictionary = { + (C, C): 8 / 9, + (C, D): 0.5, + (D, C): 1 / 3, + (D, D): 0.0, + } test_four_vector(self, expected_dictionary) def test_receive_match_attributes(self): @@ -83,11 +95,15 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (D, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=10 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=7) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=7 + ) class TestZDExtort2v2(TestPlayer): @@ -121,7 +137,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=2 + ) class TestZDExtort3(TestPlayer): @@ -154,7 +172,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=2) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=2 + ) class TestZDExtort4(TestPlayer): @@ -172,7 +192,12 @@ class TestZDExtort4(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 11 / 17, (C, D): 0, (D, C): 8 / 17, (D, D): 0.0} + expected_dictionary = { + (C, C): 11 / 17, + (C, D): 0, + (D, C): 8 / 17, + (D, D): 0.0, + } test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -183,7 +208,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=10 + ) class TestZDGen2(TestPlayer): @@ -201,7 +228,12 @@ class TestZDGen2(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 1, (C, D): 9 / 16, (D, C): 1 / 2, (D, D): 1 / 8} + expected_dictionary = { + (C, C): 1, + (C, D): 9 / 16, + (D, C): 1 / 2, + (D, D): 1 / 8, + } test_four_vector(self, expected_dictionary) def test_strategy(self): @@ -218,11 +250,15 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (D, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=10) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=10 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=3) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=3 + ) class TestZDGTFT2(TestPlayer): @@ -239,7 +275,12 @@ class TestZDGTFT2(TestPlayer): } def test_four_vector(self): - expected_dictionary = {(C, C): 1.0, (C, D): 1 / 8, (D, C): 1.0, (D, D): 0.25} + expected_dictionary = { + (C, C): 1.0, + (C, D): 1 / 8, + (D, C): 1.0, + (D, D): 0.25, + } test_four_vector(self, expected_dictionary) def test_receive_match_attributes(self): @@ -261,11 +302,15 @@ def test_strategy2(self): def test_strategy3(self): actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=4) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=4 + ) def test_strategy4(self): actions = [(C, D), (C, C), (C, D), (C, C), (C, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=23) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=23 + ) class TestZDMischief(TestPlayer): @@ -294,7 +339,9 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=4) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=4 + ) class TestZDSet2(TestPlayer): @@ -328,4 +375,6 @@ def test_strategy(self): def test_strategy2(self): actions = [(C, D), (D, C), (D, D), (D, C), (D, D), (D, C)] - self.versus_test(opponent=axl.CyclerDC(), expected_actions=actions, seed=12) + self.versus_test( + opponent=axl.CyclerDC(), expected_actions=actions, seed=12 + ) diff --git a/axelrod/tests/unit/test_classification.py b/axelrod/tests/unit/test_classification.py index c5d90df5f..c44d23516 100644 --- a/axelrod/tests/unit/test_classification.py +++ b/axelrod/tests/unit/test_classification.py @@ -50,7 +50,9 @@ def tearDown(self) -> None: def test_classifier_build(self): dirname = os.path.dirname(__file__) - test_path = os.path.join(dirname, "../../../test_outputs/classifier_test.yaml") + test_path = os.path.join( + dirname, "../../../test_outputs/classifier_test.yaml" + ) # Just returns the name of the player. For testing. name_classifier = Classifier[Text]("name", lambda player: player.name) @@ -66,7 +68,10 @@ def test_classifier_build(self): self.assertDictEqual( all_player_dicts, - {"Cooperator": {"name": "Cooperator"}, "Defector": {"name": "Defector"}}, + { + "Cooperator": {"name": "Cooperator"}, + "Defector": {"name": "Defector"}, + }, ) def test_singletonity_of_classifiers_class(self): @@ -91,7 +96,9 @@ def test_key_error_on_uknown_classifier(self): Classifiers["invalid_key"](axl.TitForTat) def test_will_lookup_key_in_dict(self): - self.assertEqual(Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1) + self.assertEqual( + Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1 + ) def test_will_lookup_key_for_classes_that_cant_init(self): with self.assertRaises(Exception) as exptn: @@ -114,7 +121,9 @@ def test_known_classifiers(self): for s in axl.all_strategies: s = s() - self.assertTrue(None not in [Classifiers[key](s) for key in known_keys]) + self.assertTrue( + None not in [Classifiers[key](s) for key in known_keys] + ) def test_multiple_instances(self): """Certain instances of classes of strategies will have different @@ -185,13 +194,19 @@ def test_obey_axelrod(self): ] for strategy in known_cheaters: - self.assertFalse(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) + self.assertFalse( + axl.Classifiers.obey_axelrod(strategy()), msg=strategy + ) for strategy in known_basic: - self.assertTrue(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) + self.assertTrue( + axl.Classifiers.obey_axelrod(strategy()), msg=strategy + ) for strategy in known_ordinary: - self.assertTrue(axl.Classifiers.obey_axelrod(strategy()), msg=strategy) + self.assertTrue( + axl.Classifiers.obey_axelrod(strategy()), msg=strategy + ) def test_is_basic(self): """A test that verifies if the is_basic function works correctly""" @@ -294,7 +309,9 @@ def test_inclusion_of_strategy_lists(self): axl.basic_strategies, axl.long_run_time_strategies, ]: - self.assertTrue(str_reps(strategy_list).issubset(str_reps(strategies_set))) + self.assertTrue( + str_reps(strategy_list).issubset(str_reps(strategies_set)) + ) def test_long_run_strategies(self): long_run_time_strategies = [ @@ -319,7 +336,8 @@ def test_long_run_strategies(self): ] self.assertEqual( - str_reps(long_run_time_strategies), str_reps(axl.long_run_time_strategies) + str_reps(long_run_time_strategies), + str_reps(axl.long_run_time_strategies), ) self.assertTrue( all(map(Classifiers["long_run_time"], axl.long_run_time_strategies)) @@ -331,10 +349,13 @@ def test_short_run_strategies(self): ] self.assertEqual( - str_reps(short_run_time_strategies), str_reps(axl.short_run_time_strategies) + str_reps(short_run_time_strategies), + str_reps(axl.short_run_time_strategies), ) self.assertFalse( - any(map(Classifiers["long_run_time"], axl.short_run_time_strategies)) + any( + map(Classifiers["long_run_time"], axl.short_run_time_strategies) + ) ) def test_meta_inclusion(self): @@ -353,4 +374,6 @@ def test_demo_strategies(self): axl.Grudger, axl.Random, ] - self.assertTrue(str_reps(demo_strategies), str_reps(axl.demo_strategies)) + self.assertTrue( + str_reps(demo_strategies), str_reps(axl.demo_strategies) + ) diff --git a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py index 2b8ac5408..8d46c2ced 100644 --- a/axelrod/tests/unit/test_compute_finite_state_machine_memory.py +++ b/axelrod/tests/unit/test_compute_finite_state_machine_memory.py @@ -98,7 +98,7 @@ def test_three_state_tft(self): (1, C, 2, C), (1, D, 0, D), (2, C, 0, C), - (2, D, 2, D) + (2, D, 2, D), ) trans_dict = self.transitions_to_dict(transitions) diff --git a/axelrod/tests/unit/test_deterministic_cache.py b/axelrod/tests/unit/test_deterministic_cache.py index e2c1d1ae3..7d3929a2d 100644 --- a/axelrod/tests/unit/test_deterministic_cache.py +++ b/axelrod/tests/unit/test_deterministic_cache.py @@ -18,7 +18,9 @@ def setUpClass(cls): cls.test_save_file = axl_filename(save_path) load_path = pathlib.Path("test_outputs/test_cache_load.txt") cls.test_load_file = axl_filename(load_path) - test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]} + test_data_to_pickle = { + ("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)] + } cls.test_pickle = pickle.dumps(test_data_to_pickle) with open(cls.test_load_file, "wb") as f: diff --git a/axelrod/tests/unit/test_filters.py b/axelrod/tests/unit/test_filters.py index 88133a6b0..70e11ed6e 100644 --- a/axelrod/tests/unit/test_filters.py +++ b/axelrod/tests/unit/test_filters.py @@ -18,10 +18,14 @@ class TestStrategy(Player): def test_equality_filter(self): self.assertTrue( - passes_operator_filter(self.TestStrategy, "stochastic", True, operator.eq) + passes_operator_filter( + self.TestStrategy, "stochastic", True, operator.eq + ) ) self.assertFalse( - passes_operator_filter(self.TestStrategy, "stochastic", False, operator.eq) + passes_operator_filter( + self.TestStrategy, "stochastic", False, operator.eq + ) ) self.assertTrue( passes_operator_filter( @@ -70,7 +74,9 @@ def test_list_filter(self): passes_in_list_filter(self.TestStrategy, "makes_use_of", ["length"]) ) self.assertTrue( - passes_in_list_filter(self.TestStrategy, "makes_use_of", ["game", "length"]) + passes_in_list_filter( + self.TestStrategy, "makes_use_of", ["game", "length"] + ) ) self.assertFalse( passes_in_list_filter(self.TestStrategy, "makes_use_of", "test") @@ -119,11 +125,21 @@ def test_passes_filterset(self, smaller, larger): "min_memory_depth": smaller, } - self.assertTrue(passes_filterset(self.TestStrategy, full_passing_filterset_1)) - self.assertTrue(passes_filterset(self.TestStrategy, full_passing_filterset_2)) - self.assertTrue(passes_filterset(self.TestStrategy, sparse_passing_filterset)) - self.assertFalse(passes_filterset(self.TestStrategy, full_failing_filterset)) - self.assertFalse(passes_filterset(self.TestStrategy, sparse_failing_filterset)) + self.assertTrue( + passes_filterset(self.TestStrategy, full_passing_filterset_1) + ) + self.assertTrue( + passes_filterset(self.TestStrategy, full_passing_filterset_2) + ) + self.assertTrue( + passes_filterset(self.TestStrategy, sparse_passing_filterset) + ) + self.assertFalse( + passes_filterset(self.TestStrategy, full_failing_filterset) + ) + self.assertFalse( + passes_filterset(self.TestStrategy, sparse_failing_filterset) + ) def test_filtered_strategies(self): class StochasticTestStrategy(Player): @@ -134,7 +150,11 @@ class StochasticTestStrategy(Player): } class MemoryDepth2TestStrategy(Player): - classifier = {"stochastic": False, "memory_depth": 2, "makes_use_of": []} + classifier = { + "stochastic": False, + "memory_depth": 2, + "makes_use_of": [], + } class UsesLengthTestStrategy(Player): classifier = { diff --git a/axelrod/tests/unit/test_fingerprint.py b/axelrod/tests/unit/test_fingerprint.py index 9c727f91e..8f9e25b43 100644 --- a/axelrod/tests/unit/test_fingerprint.py +++ b/axelrod/tests/unit/test_fingerprint.py @@ -90,7 +90,9 @@ def test_fingerprint_player(self): self.assertEqual(af.points, self.points_when_using_half_step) self.assertEqual(af.spatial_tournament.turns, 5) self.assertEqual(af.spatial_tournament.repetitions, 3) - self.assertEqual(af.spatial_tournament.edges, self.edges_when_using_half_step) + self.assertEqual( + af.spatial_tournament.edges, self.edges_when_using_half_step + ) # The first player is the fingerprinted one, the rest are probes. self.assertIsInstance(af.spatial_tournament.players[0], axl.Cooperator) @@ -129,7 +131,17 @@ def test_fingerprint_interactions_cooperator(self): # The keys are edges between players, values are repetitions. self.assertCountEqual( af.interactions.keys(), - [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9)], + [ + (0, 1), + (0, 2), + (0, 3), + (0, 4), + (0, 5), + (0, 6), + (0, 7), + (0, 8), + (0, 9), + ], ) self.assertEqual(len(af.interactions.values()), 9) @@ -187,7 +199,9 @@ def test_fingerprint_interactions_titfortat(self): def test_progress_bar_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) - data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=True) + data = af.fingerprint( + turns=10, repetitions=2, step=0.5, progress_bar=True + ) self.assertEqual(sorted(data.keys()), self.points_when_using_half_step) @patch("axelrod.fingerprint.mkstemp", RecordedMksTemp.mkstemp) @@ -216,7 +230,11 @@ def test_fingerprint_with_filename(self): filename = axl_filename(path) af = AshlockFingerprint(axl.TitForTat) af.fingerprint( - turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename + turns=1, + repetitions=1, + step=0.5, + progress_bar=False, + filename=filename, ) with open(filename, "r") as out: data = out.read() @@ -224,7 +242,9 @@ def test_fingerprint_with_filename(self): def test_serial_fingerprint(self): af = AshlockFingerprint(axl.TitForTat) - data = af.fingerprint(turns=10, repetitions=2, step=0.5, progress_bar=False) + data = af.fingerprint( + turns=10, repetitions=2, step=0.5, progress_bar=False + ) edge_keys = sorted(list(af.interactions.keys())) coord_keys = sorted(list(data.keys())) self.assertEqual(af.step, 0.5) @@ -244,9 +264,13 @@ def test_parallel_fingerprint(self): def test_plot_data(self): af = AshlockFingerprint(axl.Cooperator()) - af.fingerprint(turns=5, repetitions=3, step=0.5, progress_bar=False, seed=0) + af.fingerprint( + turns=5, repetitions=3, step=0.5, progress_bar=False, seed=0 + ) - reshaped_data = np.array([[0.0, 0.0, 0.0], [1., 2., 1.6], [3.0, 3.0, 3.0]]) + reshaped_data = np.array( + [[0.0, 0.0, 0.0], [1.0, 2.0, 1.6], [3.0, 3.0, 3.0]] + ) plotted_data = af.plot().gca().images[0].get_array() np.testing.assert_allclose(plotted_data, reshaped_data) @@ -292,12 +316,13 @@ def test_wsls_fingerprint(self): Point(x=1.0, y=0.25): 4.86, Point(x=1.0, y=0.5): 4.36, Point(x=1.0, y=0.75): 4.05, - Point(x=1.0, y=1.0): 1.3 + Point(x=1.0, y=1.0): 1.3, } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) - data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, - seed=0) + data = af.fingerprint( + turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 + ) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -328,12 +353,13 @@ def test_tft_fingerprint(self): Point(x=1.0, y=0.25): 2.68, Point(x=1.0, y=0.5): 2.51, Point(x=1.0, y=0.75): 2.41, - Point(x=1.0, y=1.0): 2.18 + Point(x=1.0, y=1.0): 2.18, } af = axl.AshlockFingerprint(axl.TitForTat(), axl.TitForTat) - data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, - seed=0) + data = af.fingerprint( + turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 + ) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -364,12 +390,13 @@ def test_majority_fingerprint(self): Point(x=1.0, y=0.25): 2.12, Point(x=1.0, y=0.5): 1.8599999999999999, Point(x=1.0, y=0.75): 2.0300000000000002, - Point(x=1.0, y=1.0): 2.26 + Point(x=1.0, y=1.0): 2.26, } af = axl.AshlockFingerprint(axl.GoByMajority, axl.TitForTat) - data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, - seed=0) + data = af.fingerprint( + turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0 + ) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2) @@ -383,22 +410,30 @@ def test_pair_fingerprints(self, strategy_pair): """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=1) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=1 + ) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=2) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=2 + ) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy, probe()) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=3) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=3 + ) self.assertIsInstance(data, dict) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 + ) self.assertIsInstance(data, dict) @given(strategy_pair=strategy_lists(min_size=2, max_size=2)) @@ -410,11 +445,15 @@ def test_fingerprint_reproducibility(self, strategy_pair): """ strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) + data = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 + ) strategy, probe = strategy_pair af = AshlockFingerprint(strategy(), probe()) - data2 = af.fingerprint(turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4) + data2 = af.fingerprint( + turns=2, repetitions=2, step=0.5, progress_bar=False, seed=4 + ) self.assertEqual(data, data2) @@ -425,13 +464,16 @@ def test_init(self): fingerprint = axl.TransitiveFingerprint(strategy=player) self.assertEqual(fingerprint.strategy, player) self.assertEqual( - fingerprint.opponents, [axl.Random(p) for p in np.linspace(0, 1, 50)] + fingerprint.opponents, + [axl.Random(p) for p in np.linspace(0, 1, 50)], ) def test_init_with_opponents(self): player = axl.TitForTat() opponents = [s() for s in axl.demo_strategies] - fingerprint = axl.TransitiveFingerprint(strategy=player, opponents=opponents) + fingerprint = axl.TransitiveFingerprint( + strategy=player, opponents=opponents + ) self.assertEqual(fingerprint.strategy, player) self.assertEqual(fingerprint.opponents, opponents) @@ -443,7 +485,8 @@ def test_init_with_not_default_number(self): ) self.assertEqual(fingerprint.strategy, player) self.assertEqual( - fingerprint.opponents, [axl.Random(p) for p in np.linspace(0, 1, 10)] + fingerprint.opponents, + [axl.Random(p) for p in np.linspace(0, 1, 10)], ) def test_fingerprint_with_filename(self): @@ -451,7 +494,9 @@ def test_fingerprint_with_filename(self): filename = axl_filename(path) strategy = axl.TitForTat() tf = TransitiveFingerprint(strategy) - tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename) + tf.fingerprint( + turns=1, repetitions=1, progress_bar=False, filename=filename + ) with open(filename, "r") as out: data = out.read() self.assertEqual(len(data.split("\n")), 102) @@ -461,9 +506,7 @@ def test_serial_fingerprint(self): tf = TransitiveFingerprint(strategy) path = pathlib.Path("test_outputs/test_fingerprint.csv") tf.fingerprint( - repetitions=1, - progress_bar=False, - filename=axl_filename(path), + repetitions=1, progress_bar=False, filename=axl_filename(path), ) self.assertEqual(tf.data.shape, (50, 50)) diff --git a/axelrod/tests/unit/test_graph.py b/axelrod/tests/unit/test_graph.py index 41f7d6d62..2ca72af31 100644 --- a/axelrod/tests/unit/test_graph.py +++ b/axelrod/tests/unit/test_graph.py @@ -39,8 +39,12 @@ def test_undirected_graph_with_vertices_and_unweighted_edges(self): self.assertEqual(str(g), "") self.assertEqual(g._edges, [(1, 2), (2, 1), (2, 3), (3, 2)]) - self.assert_out_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) - self.assert_in_mapping(g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}}) + self.assert_out_mapping( + g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}} + ) + self.assert_in_mapping( + g, {1: {2: None}, 2: {1: None, 3: None}, 3: {2: None}} + ) def test_undirected_graph_with_vertices_and_weighted_edges(self): g = axl.graph.Graph(edges=[[1, 2, 10], [2, 3, 5]]) @@ -91,7 +95,11 @@ def test_add_loops_with_existing_loop_and_using_strings(self): g.add_loops() self.assertEqual( list(sorted(g._edges)), - list(sorted([("a", "b"), ("b", "a"), ("c", "c"), ("a", "a"), ("b", "b")])), + list( + sorted( + [("a", "b"), ("b", "a"), ("c", "c"), ("a", "a"), ("b", "b")] + ) + ), ) @@ -147,9 +155,19 @@ def test_length_4_undirected(self): edges = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2), (3, 0), (0, 3)] self.assertEqual(g.vertices, [0, 1, 2, 3]) self.assertEqual(g.edges, edges) - for vertex, neighbors in [(0, (1, 3)), (1, (0, 2)), (2, (1, 3)), (3, (0, 2))]: + for vertex, neighbors in [ + (0, (1, 3)), + (1, (0, 2)), + (2, (1, 3)), + (3, (0, 2)), + ]: self.assertEqual(set(g.out_vertices(vertex)), set(neighbors)) - for vertex, neighbors in [(0, (1, 3)), (1, (0, 2)), (2, (1, 3)), (3, (0, 2))]: + for vertex, neighbors in [ + (0, (1, 3)), + (1, (0, 2)), + (2, (1, 3)), + (3, (0, 2)), + ]: self.assertEqual(set(g.in_vertices(vertex)), set(neighbors)) @@ -210,7 +228,17 @@ def test_size_2_with_loops(self): def test_size_3_with_loops(self): g = axl.graph.complete_graph(3, loops=True) self.assertEqual(g.vertices, [0, 1, 2]) - edges = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1), (0, 0), (1, 1), (2, 2)] + edges = [ + (0, 1), + (1, 0), + (0, 2), + (2, 0), + (1, 2), + (2, 1), + (0, 0), + (1, 1), + (2, 2), + ] self.assertEqual(g.edges, edges) self.assertEqual(g.directed, False) @@ -246,59 +274,70 @@ def test_size_4_with_loops(self): class TestAttachedComplete(unittest.TestCase): def test_size_2(self): g = axl.graph.attached_complete_graphs(2, loops=False) - self.assertEqual(g.vertices, ['0:0', '0:1', '1:0', '1:1']) + self.assertEqual(g.vertices, ["0:0", "0:1", "1:0", "1:1"]) self.assertEqual( g.edges, - [('0:0', '0:1'), ('0:1', '0:0'), ('1:0', '1:1'), ('1:1', '1:0'), ('0:0', '1:0'), ('1:0', '0:0')] + [ + ("0:0", "0:1"), + ("0:1", "0:0"), + ("1:0", "1:1"), + ("1:1", "1:0"), + ("0:0", "1:0"), + ("1:0", "0:0"), + ], ) self.assertEqual(g.directed, False) def test_size_3(self): g = axl.graph.attached_complete_graphs(3, loops=False) - self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) + self.assertEqual(g.vertices, ["0:0", "0:1", "0:2", "1:0", "1:1", "1:2"]) self.assertEqual( g.edges, - [('0:0', '0:1'), - ('0:1', '0:0'), - ('0:0', '0:2'), - ('0:2', '0:0'), - ('0:1', '0:2'), - ('0:2', '0:1'), - ('1:0', '1:1'), - ('1:1', '1:0'), - ('1:0', '1:2'), - ('1:2', '1:0'), - ('1:1', '1:2'), - ('1:2', '1:1'), - ('0:0', '1:0'), - ('1:0', '0:0')] + [ + ("0:0", "0:1"), + ("0:1", "0:0"), + ("0:0", "0:2"), + ("0:2", "0:0"), + ("0:1", "0:2"), + ("0:2", "0:1"), + ("1:0", "1:1"), + ("1:1", "1:0"), + ("1:0", "1:2"), + ("1:2", "1:0"), + ("1:1", "1:2"), + ("1:2", "1:1"), + ("0:0", "1:0"), + ("1:0", "0:0"), + ], ) self.assertEqual(g.directed, False) def test_size_3_with_loops(self): g = axl.graph.attached_complete_graphs(3, loops=True) - self.assertEqual(g.vertices, ['0:0', '0:1', '0:2', '1:0', '1:1', '1:2']) + self.assertEqual(g.vertices, ["0:0", "0:1", "0:2", "1:0", "1:1", "1:2"]) self.assertEqual( g.edges, - [('0:0', '0:1'), - ('0:1', '0:0'), - ('0:0', '0:2'), - ('0:2', '0:0'), - ('0:1', '0:2'), - ('0:2', '0:1'), - ('1:0', '1:1'), - ('1:1', '1:0'), - ('1:0', '1:2'), - ('1:2', '1:0'), - ('1:1', '1:2'), - ('1:2', '1:1'), - ('0:0', '1:0'), - ('1:0', '0:0'), - ('0:0', '0:0'), - ('0:1', '0:1'), - ('0:2', '0:2'), - ('1:0', '1:0'), - ('1:1', '1:1'), - ('1:2', '1:2')] + [ + ("0:0", "0:1"), + ("0:1", "0:0"), + ("0:0", "0:2"), + ("0:2", "0:0"), + ("0:1", "0:2"), + ("0:2", "0:1"), + ("1:0", "1:1"), + ("1:1", "1:0"), + ("1:0", "1:2"), + ("1:2", "1:0"), + ("1:1", "1:2"), + ("1:2", "1:1"), + ("0:0", "1:0"), + ("1:0", "0:0"), + ("0:0", "0:0"), + ("0:1", "0:1"), + ("0:2", "0:2"), + ("1:0", "1:0"), + ("1:1", "1:1"), + ("1:2", "1:2"), + ], ) self.assertEqual(g.directed, False) diff --git a/axelrod/tests/unit/test_history.py b/axelrod/tests/unit/test_history.py index e7b105c31..11cd2445c 100644 --- a/axelrod/tests/unit/test_history.py +++ b/axelrod/tests/unit/test_history.py @@ -83,8 +83,7 @@ def test_flip_plays(self): self.assertEqual(flipped_history, [D, C, D, C, D]) self.assertEqual(flipped_history.cooperations, 2) self.assertEqual(flipped_history.defections, 3) - self.assertEqual(flipped_history.state_distribution, - new_distribution) + self.assertEqual(flipped_history.state_distribution, new_distribution) # Flip operation is idempotent flipped_flipped_history = flipped_history.flip_plays() @@ -94,7 +93,6 @@ def test_flip_plays(self): class TestLimitedHistory(unittest.TestCase): - def test_memory_depth(self): h = LimitedHistory(memory_depth=3) h.append(C, C) @@ -105,8 +103,9 @@ def test_memory_depth(self): self.assertEqual(len(h), 3) self.assertEqual(h.cooperations, 2) self.assertEqual(h.defections, 1) - self.assertEqual(h.state_distribution, - Counter({(C, C): 1, (D, D): 1, (C, D): 1})) + self.assertEqual( + h.state_distribution, Counter({(C, C): 1, (D, D): 1, (C, D): 1}) + ) h.append(D, C) self.assertEqual(len(h), 3) self.assertEqual(h._plays, [D, C, D]) @@ -115,4 +114,5 @@ def test_memory_depth(self): self.assertEqual(h.defections, 2) self.assertEqual( h.state_distribution, - Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0})) + Counter({(D, D): 1, (C, D): 1, (D, C): 1, (C, C): 0}), + ) diff --git a/axelrod/tests/unit/test_interaction_utils.py b/axelrod/tests/unit/test_interaction_utils.py index 49706fe01..d56e62cce 100644 --- a/axelrod/tests/unit/test_interaction_utils.py +++ b/axelrod/tests/unit/test_interaction_utils.py @@ -49,39 +49,65 @@ def test_compute_scores(self): def test_compute_final_score(self): for inter, final_score in zip(self.interactions, self.final_scores): - self.assertEqual(final_score, axl.interaction_utils.compute_final_score(inter)) + self.assertEqual( + final_score, axl.interaction_utils.compute_final_score(inter) + ) def test_compute_final_score_per_turn(self): for inter, final_score_per_round in zip( self.interactions, self.final_score_per_turn ): self.assertEqual( - final_score_per_round, axl.interaction_utils.compute_final_score_per_turn(inter) + final_score_per_round, + axl.interaction_utils.compute_final_score_per_turn(inter), ) def test_compute_winner_index(self): for inter, winner in zip(self.interactions, self.winners): - self.assertEqual(winner, axl.interaction_utils.compute_winner_index(inter)) + self.assertEqual( + winner, axl.interaction_utils.compute_winner_index(inter) + ) def test_compute_cooperations(self): for inter, coop in zip(self.interactions, self.cooperations): - self.assertEqual(coop, axl.interaction_utils.compute_cooperations(inter)) + self.assertEqual( + coop, axl.interaction_utils.compute_cooperations(inter) + ) def test_compute_normalised_cooperations(self): for inter, coop in zip(self.interactions, self.normalised_cooperations): - self.assertEqual(coop, axl.interaction_utils.compute_normalised_cooperation(inter)) + self.assertEqual( + coop, + axl.interaction_utils.compute_normalised_cooperation(inter), + ) def test_compute_state_distribution(self): for inter, dist in zip(self.interactions, self.state_distribution): - self.assertEqual(dist, axl.interaction_utils.compute_state_distribution(inter)) + self.assertEqual( + dist, axl.interaction_utils.compute_state_distribution(inter) + ) def test_compute_normalised_state_distribution(self): - for inter, dist in zip(self.interactions, self.normalised_state_distribution): - self.assertEqual(dist, axl.interaction_utils.compute_normalised_state_distribution(inter)) + for inter, dist in zip( + self.interactions, self.normalised_state_distribution + ): + self.assertEqual( + dist, + axl.interaction_utils.compute_normalised_state_distribution( + inter + ), + ) def test_compute_state_to_action_distribution(self): - for inter, dist in zip(self.interactions, self.state_to_action_distribution): - self.assertEqual(dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) + for inter, dist in zip( + self.interactions, self.state_to_action_distribution + ): + self.assertEqual( + dist, + axl.interaction_utils.compute_state_to_action_distribution( + inter + ), + ) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ Counter( @@ -93,17 +119,25 @@ def test_compute_state_to_action_distribution(self): ((D, D), C): 1, } ), - Counter({((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1}), + Counter( + {((C, C), D): 1, ((C, D), C): 2, ((D, C), D): 2, ((D, D), C): 1} + ), ] - self.assertEqual(expected_dist, axl.interaction_utils.compute_state_to_action_distribution(inter)) + self.assertEqual( + expected_dist, + axl.interaction_utils.compute_state_to_action_distribution(inter), + ) def test_compute_normalised_state_to_action_distribution(self): for inter, dist in zip( self.interactions, self.normalised_state_to_action_distribution ): self.assertEqual( - dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) + dist, + axl.interaction_utils.compute_normalised_state_to_action_distribution( + inter + ), ) inter = [(C, D), (D, C), (C, D), (D, C), (D, D), (C, C), (C, D)] expected_dist = [ @@ -116,15 +150,22 @@ def test_compute_normalised_state_to_action_distribution(self): ((D, D), C): 1, } ), - Counter({((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1}), + Counter( + {((C, C), D): 1, ((C, D), C): 1, ((D, C), D): 1, ((D, D), C): 1} + ), ] self.assertEqual( - expected_dist, axl.interaction_utils.compute_normalised_state_to_action_distribution(inter) + expected_dist, + axl.interaction_utils.compute_normalised_state_to_action_distribution( + inter + ), ) def test_compute_sparklines(self): for inter, spark in zip(self.interactions, self.sparklines): - self.assertEqual(spark, axl.interaction_utils.compute_sparklines(inter)) + self.assertEqual( + spark, axl.interaction_utils.compute_sparklines(inter) + ) def test_read_interactions_from_file(self): tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=False) @@ -137,10 +178,14 @@ def test_read_interactions_from_file(self): (0, 1): [[(C, D), (C, D)] for _ in range(3)], (1, 1): [[(D, D), (D, D)] for _ in range(3)], } - interactions = axl.interaction_utils.read_interactions_from_file(tmp_file.name, progress_bar=False) + interactions = axl.interaction_utils.read_interactions_from_file( + tmp_file.name, progress_bar=False + ) self.assertEqual(expected_interactions, interactions) def test_string_to_interactions(self): string = "CDCDDD" interactions = [(C, D), (C, D), (D, D)] - self.assertEqual(axl.interaction_utils.string_to_interactions(string), interactions) + self.assertEqual( + axl.interaction_utils.string_to_interactions(string), interactions + ) diff --git a/axelrod/tests/unit/test_match.py b/axelrod/tests/unit/test_match.py index 84040eaa5..6012ce32f 100644 --- a/axelrod/tests/unit/test_match.py +++ b/axelrod/tests/unit/test_match.py @@ -38,7 +38,9 @@ def test_init_with_prob_end(self, prob_end, game): self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), game.RPST()) - self.assertEqual(match.players[0].match_attributes["length"], float("inf")) + self.assertEqual( + match.players[0].match_attributes["length"], float("inf") + ) self.assertEqual(match._cache, {}) @given( @@ -56,7 +58,9 @@ def test_init_with_prob_end_and_turns(self, turns, prob_end, game): self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), game.RPST()) - self.assertEqual(match.players[0].match_attributes["length"], float("inf")) + self.assertEqual( + match.players[0].match_attributes["length"], float("inf") + ) self.assertEqual(match._cache, {}) def test_default_init(self): @@ -83,7 +87,9 @@ def test_example_prob_end(self): expected_lengths = [2, 1, 1] for seed, expected_length in zip(range(3), expected_lengths): match = axl.Match((p1, p2), prob_end=0.5, seed=seed) - self.assertEqual(match.players[0].match_attributes["length"], float("inf")) + self.assertEqual( + match.players[0].match_attributes["length"], float("inf") + ) self.assertEqual(len(match.play()), expected_length) self.assertEqual(match.noise, 0) self.assertEqual(match.game.RPST(), (3, 1, 0, 5)) @@ -117,7 +123,7 @@ def test_len_error(self): with self.assertRaises(TypeError): len(match) - @given(p=floats(min_value=1e-10, max_value=1-1e-10)) + @given(p=floats(min_value=1e-10, max_value=1 - 1e-10)) def test_stochastic(self, p): p1, p2 = axl.Cooperator(), axl.Cooperator() match = axl.Match((p1, p2), 5) @@ -130,7 +136,7 @@ def test_stochastic(self, p): match = axl.Match((p1, p2), 5) self.assertTrue(match._stochastic) - @given(p=floats(min_value=1e-10, max_value=1-1e-10)) + @given(p=floats(min_value=1e-10, max_value=1 - 1e-10)) def test_cache_update_required(self, p): p1, p2 = axl.Cooperator(), axl.Cooperator() match = axl.Match((p1, p2), 5, noise=p) @@ -180,8 +186,7 @@ def test_cache_grows(self): self.assertEqual(match.play(), expected_result_5_turn) # The cache should now hold the 5-turn result.. self.assertEqual( - cache[(axl.Cooperator(), axl.Defector())], - expected_result_5_turn + cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn ) def test_cache_doesnt_shrink(self): @@ -200,8 +205,7 @@ def test_cache_doesnt_shrink(self): self.assertEqual(match.play(), expected_result_3_turn) # The cache should still hold the 5. self.assertEqual( - cache[(axl.Cooperator(), axl.Defector())], - expected_result_5_turn + cache[(axl.Cooperator(), axl.Defector())], expected_result_5_turn ) def test_scores(self): @@ -361,7 +365,9 @@ def test_sample_length(self): ]: rng = RandomGenerator(seed) r = rng.random() - self.assertEqual(axl.match.sample_length(prob_end, r), expected_length) + self.assertEqual( + axl.match.sample_length(prob_end, r), expected_length + ) def test_sample_with_0_prob(self): self.assertEqual(axl.match.sample_length(0, 0.4), float("inf")) diff --git a/axelrod/tests/unit/test_match_generator.py b/axelrod/tests/unit/test_match_generator.py index e91a7eba8..730928216 100644 --- a/axelrod/tests/unit/test_match_generator.py +++ b/axelrod/tests/unit/test_match_generator.py @@ -148,7 +148,9 @@ def test_build_single_match_params_with_fixed_length_unknown(self): self.assertEqual(match_params["game"], test_game) self.assertEqual(match_params["prob_end"], None) self.assertEqual(match_params["noise"], 0) - self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) + self.assertEqual( + match_params["match_attributes"], {"length": float("inf")} + ) # Check that can build a match players = [axl.Cooperator(), axl.Defector()] @@ -166,7 +168,7 @@ def test_build_match_chunks(self, repetitions): players=self.players, turns=test_turns, game=test_game, - repetitions=repetitions + repetitions=repetitions, ) chunks = list(rr.build_match_chunks()) match_definitions = [ @@ -177,10 +179,14 @@ def test_build_match_chunks(self, repetitions): (i, j, repetitions) for i in range(5) for j in range(i, 5) ] - self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) + self.assertEqual( + sorted(match_definitions), sorted(expected_match_definitions) + ) - @given(repetitions=integers(min_value=1, max_value=test_repetitions), - seed=integers(min_value=1, max_value=4294967295),) + @given( + repetitions=integers(min_value=1, max_value=test_repetitions), + seed=integers(min_value=1, max_value=4294967295), + ) @settings(max_examples=5) def test_seeding_equality(self, repetitions, seed): rr1 = axl.MatchGenerator( @@ -188,7 +194,7 @@ def test_seeding_equality(self, repetitions, seed): turns=test_turns, game=test_game, repetitions=repetitions, - seed=seed + seed=seed, ) chunks1 = list(rr1.build_match_chunks()) rr2 = axl.MatchGenerator( @@ -196,7 +202,7 @@ def test_seeding_equality(self, repetitions, seed): turns=test_turns, game=test_game, repetitions=repetitions, - seed=seed + seed=seed, ) chunks2 = list(rr2.build_match_chunks()) self.assertEqual(chunks1, chunks2) @@ -207,7 +213,7 @@ def test_seeding_inequality(self, repetitions=10): turns=test_turns, game=test_game, repetitions=repetitions, - seed=0 + seed=0, ) chunks1 = list(rr1.build_match_chunks()) rr2 = axl.MatchGenerator( @@ -215,7 +221,7 @@ def test_seeding_inequality(self, repetitions=10): turns=test_turns, game=test_game, repetitions=repetitions, - seed=1 + seed=1, ) chunks2 = list(rr2.build_match_chunks()) self.assertNotEqual(chunks1, chunks2) @@ -239,7 +245,9 @@ def test_spatial_build_match_chunks(self, repetitions): ] expected_match_definitions = [(i, j, repetitions) for i, j in cycle] - self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) + self.assertEqual( + sorted(match_definitions), sorted(expected_match_definitions) + ) def test_len(self): turns = 5 diff --git a/axelrod/tests/unit/test_moran.py b/axelrod/tests/unit/test_moran.py index 8018683c6..3d5e02734 100644 --- a/axelrod/tests/unit/test_moran.py +++ b/axelrod/tests/unit/test_moran.py @@ -23,16 +23,21 @@ def test_init(self): self.assertEqual(mp.noise, 0) self.assertEqual(mp.initial_players, players) self.assertEqual(mp.players, list(players)) - self.assertEqual(mp.populations, [Counter({"Cooperator": 1, "Defector": 1})]) + self.assertEqual( + mp.populations, [Counter({"Cooperator": 1, "Defector": 1})] + ) self.assertIsNone(mp.winning_strategy_name) self.assertEqual(mp.mutation_rate, 0) self.assertEqual(mp.mode, "bd") self.assertEqual(mp.deterministic_cache, axl.DeterministicCache()) self.assertEqual( - mp.mutation_targets, {"Cooperator": [players[1]], "Defector": [players[0]]} + mp.mutation_targets, + {"Cooperator": [players[1]], "Defector": [players[0]]}, ) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (1, 0)]) - self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)]) + self.assertEqual( + mp.reproduction_graph._edges, [(0, 1), (1, 0), (0, 0), (1, 1)] + ) self.assertEqual(mp.fitness_transformation, None) self.assertEqual(mp.locations, [0, 1]) self.assertEqual(mp.index, {0: 0, 1: 1}) @@ -48,7 +53,9 @@ def test_init(self): sorted([(0, 1), (2, 0), (1, 2), (0, 0), (1, 1), (2, 2)]), ) - mp = axl.MoranProcess(players, interaction_graph=graph, reproduction_graph=graph) + mp = axl.MoranProcess( + players, interaction_graph=graph, reproduction_graph=graph + ) self.assertEqual(mp.interaction_graph._edges, [(0, 1), (2, 0), (1, 2)]) self.assertEqual(mp.reproduction_graph._edges, [(0, 1), (2, 0), (1, 2)]) @@ -225,8 +232,12 @@ def test_two_random_players(self): def test_two_players_with_mutation(self): p1, p2 = axl.Cooperator(), axl.Defector() - mp = MoranProcess((p1, p2), mutation_rate=0.2, stop_on_fixation=False, seed=5) - self.assertDictEqual(mp.mutation_targets, {str(p1): [p2], str(p2): [p1]}) + mp = MoranProcess( + (p1, p2), mutation_rate=0.2, stop_on_fixation=False, seed=5 + ) + self.assertDictEqual( + mp.mutation_targets, {str(p1): [p2], str(p2): [p1]} + ) # Test that mutation causes the population to alternate between # fixations counters = [ @@ -262,7 +273,9 @@ def test_three_players_with_mutation(self): p2 = axl.Random() p3 = axl.Defector() players = [p1, p2, p3] - mp = axl.MoranProcess(players, mutation_rate=0.2, stop_on_fixation=False) + mp = axl.MoranProcess( + players, mutation_rate=0.2, stop_on_fixation=False + ) self.assertDictEqual( mp.mutation_targets, {str(p1): [p3, p2], str(p2): [p1, p3], str(p3): [p1, p2]}, @@ -373,49 +386,61 @@ def test_cooperator_can_win_with_fitness_transformation(self): w = 0.95 fitness_transformation = lambda score: 1 - w + w * score mp = MoranProcess( - players, turns=10, fitness_transformation=fitness_transformation, - seed=3419 + players, + turns=10, + fitness_transformation=fitness_transformation, + seed=3419, ) populations = mp.play() self.assertEqual(mp.winning_strategy_name, "Cooperator") def test_atomic_mutation_fsm(self): - players = [axl.EvolvableFSMPlayer(num_states=2, initial_state=1, initial_action=C, - seed=4) - for _ in range(5)] + players = [ + axl.EvolvableFSMPlayer( + num_states=2, initial_state=1, initial_action=C, seed=4 + ) + for _ in range(5) + ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=12) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(sorted(mp.populations[-1].items()))[0][0], - 'EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643') + "EvolvableFSMPlayer: ((0, C, 0, C), (0, D, 1, C), (1, C, 1, D), (1, D, 1, D)), 0, D, 2, 0.1, 2240802643", + ) self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated) def test_atomic_mutation_cycler(self): cycle_length = 5 - players = [axl.EvolvableCycler(cycle_length=cycle_length, seed=4) - for _ in range(5)] + players = [ + axl.EvolvableCycler(cycle_length=cycle_length, seed=4) + for _ in range(5) + ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=10) rounds = 10 for _ in range(rounds): next(mp) self.assertEqual( list(mp.populations[-1].items())[0], - ('EvolvableCycler: CCDDD, 5, 0.2, 1, 1164244177', 1)) + ("EvolvableCycler: CCDDD, 5, 0.2, 1, 1164244177", 1), + ) self.assertEqual(len(mp.populations), 11) self.assertFalse(mp.fixated) def test_mutation_method_exceptions(self): cycle_length = 5 - players = [axl.EvolvableCycler(cycle_length=cycle_length, seed=4) - for _ in range(5)] + players = [ + axl.EvolvableCycler(cycle_length=cycle_length, seed=4) + for _ in range(5) + ] with self.assertRaises(ValueError): MoranProcess(players, turns=10, mutation_method="random", seed=10) - players = [axl.Cycler(cycle="CD" * random.randint(2, 10)) - for _ in range(10)] + players = [ + axl.Cycler(cycle="CD" * random.randint(2, 10)) for _ in range(10) + ] mp = MoranProcess(players, turns=10, mutation_method="atomic", seed=53) with self.assertRaises(TypeError): for _ in range(10): @@ -442,9 +467,12 @@ def test_complete(self): mp = MoranProcess(players, seed=seed) mp.play() winner = mp.winning_strategy_name - mp = MoranProcess(players, interaction_graph=interaction_graph, - reproduction_graph=reproduction_graph, - seed=seed) + mp = MoranProcess( + players, + interaction_graph=interaction_graph, + reproduction_graph=reproduction_graph, + seed=seed, + ) mp.play() winner2 = mp.winning_strategy_name self.assertEqual(winner, winner2) @@ -483,14 +511,18 @@ def test_asymmetry(self): players.append(axl.Defector()) for seed, outcome in seeds: mp = MoranProcess( - players, interaction_graph=graph1, reproduction_graph=graph2, - seed=seed + players, + interaction_graph=graph1, + reproduction_graph=graph2, + seed=seed, ) mp.play() winner = mp.winning_strategy_name mp = MoranProcess( - players, interaction_graph=graph2, reproduction_graph=graph1, - seed=seed + players, + interaction_graph=graph2, + reproduction_graph=graph1, + seed=seed, ) mp.play() winner2 = mp.winning_strategy_name @@ -508,10 +540,14 @@ def test_cycle_death_birth(self): for _ in range(N // 2): players.append(axl.Defector()) for seed, outcome in seeds: - mp = MoranProcess(players, interaction_graph=graph, mode="bd", seed=seed) + mp = MoranProcess( + players, interaction_graph=graph, mode="bd", seed=seed + ) mp.play() winner = mp.winning_strategy_name - mp = MoranProcess(players, interaction_graph=graph, mode="db", seed=seed) + mp = MoranProcess( + players, interaction_graph=graph, mode="db", seed=seed + ) mp.play() winner2 = mp.winning_strategy_name self.assertEqual((winner == winner2), outcome) @@ -541,7 +577,11 @@ def test_init(self): """Test the initialisation process""" self.assertEqual( set(self.amp.cached_outcomes.keys()), - {("Cooperator", "Defector"), ("Cooperator", "Cooperator"), ("Defector", "Defector")}, + { + ("Cooperator", "Defector"), + ("Cooperator", "Cooperator"), + ("Defector", "Defector"), + }, ) self.assertEqual(self.amp.players, self.players) self.assertEqual(self.amp.turns, 0) diff --git a/axelrod/tests/unit/test_pickling.py b/axelrod/tests/unit/test_pickling.py index 29f25d645..cec827819 100644 --- a/axelrod/tests/unit/test_pickling.py +++ b/axelrod/tests/unit/test_pickling.py @@ -10,7 +10,9 @@ # First set: special cases -PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()(axl.strategy_transformers.FlipTransformer()(axl.Cooperator)) +PointerToWrappedStrategy = axl.strategy_transformers.FlipTransformer()( + axl.strategy_transformers.FlipTransformer()(axl.Cooperator) +) class MyDefector(axl.Player): @@ -111,7 +113,9 @@ class JossAnn(axl.Cooperator): probability = [0.2, 0.3] -@axl.strategy_transformers.MixedTransformer(probability, strategies, name_prefix=None) +@axl.strategy_transformers.MixedTransformer( + probability, strategies, name_prefix=None +) class Mixed(axl.Cooperator): pass @@ -159,7 +163,9 @@ def __init__(self): super().__init__(team=team) -TransformedMetaThue = axl.strategy_transformers.IdentityTransformer(name_prefix=None)(MetaThue) +TransformedMetaThue = axl.strategy_transformers.IdentityTransformer( + name_prefix=None +)(MetaThue) transformed_no_prefix = [ @@ -233,12 +239,24 @@ def test_parameterized_player(self): self.assert_original_equals_pickled(player) def test_sequence_player(self): - inline_transformed_thue = axl.strategy_transformers.IdentityTransformer(name_prefix="Transformed")(axl.ThueMorse)() - for player in [axl.ThueMorse(), axl.ThueMorseInverse(), MetaThue(), TransformedMetaThue(), - inline_transformed_thue, TransformedThue(), - ]: + inline_transformed_thue = axl.strategy_transformers.IdentityTransformer( + name_prefix="Transformed" + )(axl.ThueMorse)() + for player in [ + axl.ThueMorse(), + axl.ThueMorseInverse(), + MetaThue(), + TransformedMetaThue(), + inline_transformed_thue, + TransformedThue(), + ]: self.assert_equals_instance_from_pickling(player) - opponents = (axl.Defector, axl.Cooperator, axl.Random, axl.CyclerCCCDCD) + opponents = ( + axl.Defector, + axl.Cooperator, + axl.Random, + axl.CyclerCCCDCD, + ) for opponent_class in opponents: player.reset() opponent = opponent_class() @@ -271,9 +289,13 @@ def test_pickling_all_transformers_as_instance_called_on_a_class(self): self.assert_original_equals_pickled(player) def test_created_on_the_spot_multiple_transformers(self): - player_class = axl.strategy_transformers.FlipTransformer()(axl.Cooperator) + player_class = axl.strategy_transformers.FlipTransformer()( + axl.Cooperator + ) player_class = axl.strategy_transformers.DualTransformer()(player_class) - player = axl.strategy_transformers.FinalTransformer((C, D))(player_class)() + player = axl.strategy_transformers.FinalTransformer((C, D))( + player_class + )() self.assert_original_equals_pickled(player) @@ -290,9 +312,13 @@ def test_dual_transformer_regression_test(self): player_class = axl.WinStayLoseShift player_class = axl.strategy_transformers.DualTransformer()(player_class) - player_class = axl.strategy_transformers.InitialTransformer((C, D))(player_class) + player_class = axl.strategy_transformers.InitialTransformer((C, D))( + player_class + ) player_class = axl.strategy_transformers.DualTransformer()(player_class) - player_class = axl.strategy_transformers.TrackHistoryTransformer()(player_class) + player_class = axl.strategy_transformers.TrackHistoryTransformer()( + player_class + ) interspersed_dual_transformers = player_class() @@ -314,7 +340,8 @@ def test_class_and_instance_name_different_built_from_player_class(self): player = MyCooperator() class_names = [class_.__name__ for class_ in MyCooperator.mro()] self.assertEqual( - class_names, ["FlippedMyCooperator", "MyCooperator", "Player", "object"] + class_names, + ["FlippedMyCooperator", "MyCooperator", "Player", "object"], ) self.assert_original_equals_pickled(player) @@ -374,17 +401,23 @@ def test_with_various_name_prefixes(self): self.assertEqual(no_prefix.__class__.__name__, "Flip") self.assert_original_equals_pickled(no_prefix) - default_prefix = axl.strategy_transformers.FlipTransformer()(axl.Cooperator)() + default_prefix = axl.strategy_transformers.FlipTransformer()( + axl.Cooperator + )() self.assertEqual(default_prefix.__class__.__name__, "FlippedCooperator") self.assert_original_equals_pickled(default_prefix) - fliptastic = axl.strategy_transformers.FlipTransformer(name_prefix="Fliptastic") + fliptastic = axl.strategy_transformers.FlipTransformer( + name_prefix="Fliptastic" + ) new_prefix = fliptastic(axl.Cooperator)() self.assertEqual(new_prefix.__class__.__name__, "FliptasticCooperator") self.assert_original_equals_pickled(new_prefix) def test_dynamic_class_no_name_prefix(self): - player = axl.strategy_transformers.FlipTransformer(name_prefix=None)(axl.Cooperator)() + player = axl.strategy_transformers.FlipTransformer(name_prefix=None)( + axl.Cooperator + )() self.assertEqual(player.__class__.__name__, "Cooperator") self.assert_original_equals_pickled(player) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index 847419035..f5f621c55 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -32,7 +32,11 @@ def setUpClass(cls): [3 / 2 for _ in range(3)], ] cls.expected_boxplot_xticks_locations = [1, 2, 3, 4] - cls.expected_boxplot_xticks_labels = ["Defector", "Tit For Tat", "Alternator"] + cls.expected_boxplot_xticks_labels = [ + "Defector", + "Tit For Tat", + "Alternator", + ] cls.expected_lengthplot_dataset = [ [cls.turns for _ in range(3)], @@ -41,9 +45,21 @@ def setUpClass(cls): ] cls.expected_payoff_dataset = [ - [0, mean([9 / 5 for _ in range(3)]), mean([17 / 5 for _ in range(3)])], - [mean([4 / 5 for _ in range(3)]), 0, mean([13 / 5 for _ in range(3)])], - [mean([2 / 5 for _ in range(3)]), mean([13 / 5 for _ in range(3)]), 0], + [ + 0, + mean([9 / 5 for _ in range(3)]), + mean([17 / 5 for _ in range(3)]), + ], + [ + mean([4 / 5 for _ in range(3)]), + 0, + mean([13 / 5 for _ in range(3)]), + ], + [ + mean([2 / 5 for _ in range(3)]), + mean([13 / 5 for _ in range(3)]), + 0, + ], ] cls.expected_winplot_dataset = ( [[2, 2, 2], [0, 0, 0], [0, 0, 0]], @@ -104,12 +120,15 @@ def test_init_from_resulsetfromfile(self): def test_boxplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._boxplot_dataset, self.expected_boxplot_dataset) + self.assertSequenceEqual( + plot._boxplot_dataset, self.expected_boxplot_dataset + ) def test_boxplot_xticks_locations(self): plot = axl.Plot(self.test_result_set) self.assertEqual( - plot._boxplot_xticks_locations, self.expected_boxplot_xticks_locations + plot._boxplot_xticks_locations, + self.expected_boxplot_xticks_locations, ) def test_boxplot_xticks_labels(self): @@ -145,7 +164,9 @@ def test_boxplot_with_title(self): def test_winplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) + self.assertSequenceEqual( + plot._winplot_dataset, self.expected_winplot_dataset + ) def test_winplot(self): plot = axl.Plot(self.test_result_set) @@ -155,7 +176,9 @@ def test_winplot(self): def test_sdvplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._sdv_plot_dataset, self.expected_sdvplot_dataset) + self.assertSequenceEqual( + plot._sdv_plot_dataset, self.expected_sdvplot_dataset + ) def test_sdvplot(self): plot = axl.Plot(self.test_result_set) @@ -165,7 +188,9 @@ def test_sdvplot(self): def test_lengthplot_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._winplot_dataset, self.expected_winplot_dataset) + self.assertSequenceEqual( + plot._winplot_dataset, self.expected_winplot_dataset + ) def test_lengthplot(self): plot = axl.Plot(self.test_result_set) @@ -181,7 +206,9 @@ def test_pdplot(self): def test_payoff_dataset(self): plot = axl.Plot(self.test_result_set) - self.assertSequenceEqual(plot._payoff_dataset, self.expected_payoff_dataset) + self.assertSequenceEqual( + plot._payoff_dataset, self.expected_payoff_dataset + ) def test_payoff(self): plot = axl.Plot(self.test_result_set) @@ -250,6 +277,8 @@ def test_all_plots(self): ) self.assertIsNone( plot.save_all_plots( - prefix="test_outputs/", title_prefix="A prefix", progress_bar=True + prefix="test_outputs/", + title_prefix="A prefix", + progress_bar=True, ) ) diff --git a/axelrod/tests/unit/test_property.py b/axelrod/tests/unit/test_property.py index ae992a035..945aae5be 100644 --- a/axelrod/tests/unit/test_property.py +++ b/axelrod/tests/unit/test_property.py @@ -12,7 +12,9 @@ ) from hypothesis import given, settings -stochastic_strategies = [s for s in axl.strategies if axl.Classifiers["stochastic"](s())] +stochastic_strategies = [ + s for s in axl.strategies if axl.Classifiers["stochastic"](s()) +] class TestStrategyList(unittest.TestCase): @@ -130,7 +132,11 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given(tournament=prob_end_tournaments(strategies=axl.basic_strategies, max_size=3)) + @given( + tournament=prob_end_tournaments( + strategies=axl.basic_strategies, max_size=3 + ) + ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axl.Tournament) @@ -165,7 +171,11 @@ def test_decorator(self, tournament): self.assertLessEqual(tournament.repetitions, 50) self.assertGreaterEqual(tournament.repetitions, 2) - @given(tournament=spatial_tournaments(strategies=axl.basic_strategies, max_size=3)) + @given( + tournament=spatial_tournaments( + strategies=axl.basic_strategies, max_size=3 + ) + ) @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axl.Tournament) diff --git a/axelrod/tests/unit/test_resultset.py b/axelrod/tests/unit/test_resultset.py index 97563d3e9..bb0aa2091 100644 --- a/axelrod/tests/unit/test_resultset.py +++ b/axelrod/tests/unit/test_resultset.py @@ -71,15 +71,39 @@ def setUpClass(cls): # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ - [0, mean([13 / 5 for _ in range(3)]), mean([2 / 5 for _ in range(3)])], - [mean([13 / 5 for _ in range(3)]), 0, mean([4 / 5 for _ in range(3)])], - [mean([17 / 5 for _ in range(3)]), mean([9 / 5 for _ in range(3)]), 0], + [ + 0, + mean([13 / 5 for _ in range(3)]), + mean([2 / 5 for _ in range(3)]), + ], + [ + mean([13 / 5 for _ in range(3)]), + 0, + mean([4 / 5 for _ in range(3)]), + ], + [ + mean([17 / 5 for _ in range(3)]), + mean([9 / 5 for _ in range(3)]), + 0, + ], ] cls.expected_payoff_stddevs = [ - [0, std([13 / 5 for _ in range(3)]), std([2 / 5 for _ in range(3)])], - [std([13 / 5 for _ in range(3)]), 0, std([4 / 5 for _ in range(3)])], - [std([17 / 5 for _ in range(3)]), std([9 / 5 for _ in range(3)]), 0], + [ + 0, + std([13 / 5 for _ in range(3)]), + std([2 / 5 for _ in range(3)]), + ], + [ + std([13 / 5 for _ in range(3)]), + 0, + std([4 / 5 for _ in range(3)]), + ], + [ + std([17 / 5 for _ in range(3)]), + std([9 / 5 for _ in range(3)]), + 0, + ], ] cls.expected_cooperation = [[0, 9, 9], [9, 0, 3], [0, 0, 0]] @@ -88,8 +112,16 @@ def setUpClass(cls): cls.expected_initial_cooperation_rate = [1, 1, 0] cls.expected_normalised_cooperation = [ - [0, mean([3 / 5 for _ in range(3)]), mean([3 / 5 for _ in range(3)])], - [mean([3 / 5 for _ in range(3)]), 0, mean([1 / 5 for _ in range(3)])], + [ + 0, + mean([3 / 5 for _ in range(3)]), + mean([3 / 5 for _ in range(3)]), + ], + [ + mean([3 / 5 for _ in range(3)]), + 0, + mean([1 / 5 for _ in range(3)]), + ], [0, 0, 0], ] @@ -176,7 +208,11 @@ def setUpClass(cls): cls.expected_good_partner_rating = [1.0, 1.0, 0] - cls.expected_eigenjesus_rating = [0.5547001962252291, 0.8320502943378436, 0.0] + cls.expected_eigenjesus_rating = [ + 0.5547001962252291, + 0.8320502943378436, + 0.0, + ] cls.expected_eigenmoses_rating = [ -0.4578520302117101, @@ -337,7 +373,9 @@ def test_score_diffs(self): for i, row in enumerate(rs.score_diffs): for j, col in enumerate(row): for k, score in enumerate(col): - self.assertAlmostEqual(score, self.expected_score_diffs[i][j][k]) + self.assertAlmostEqual( + score, self.expected_score_diffs[i][j][k] + ) def test_payoff_diffs_means(self): rs = axl.ResultSet( @@ -347,7 +385,9 @@ def test_payoff_diffs_means(self): self.assertEqual(len(rs.payoff_diffs_means), rs.num_players) for i, row in enumerate(rs.payoff_diffs_means): for j, col in enumerate(row): - self.assertAlmostEqual(col, self.expected_payoff_diffs_means[i][j]) + self.assertAlmostEqual( + col, self.expected_payoff_diffs_means[i][j] + ) def test_payoff_stddevs(self): rs = axl.ResultSet( @@ -372,7 +412,8 @@ def test_initial_cooperation_count(self): self.assertIsInstance(rs.initial_cooperation_count, list) self.assertEqual(len(rs.initial_cooperation_count), rs.num_players) self.assertEqual( - rs.initial_cooperation_count, self.expected_initial_cooperation_count + rs.initial_cooperation_count, + self.expected_initial_cooperation_count, ) def test_normalised_cooperation(self): @@ -383,7 +424,9 @@ def test_normalised_cooperation(self): self.assertEqual(len(rs.normalised_cooperation), rs.num_players) for i, row in enumerate(rs.normalised_cooperation): for j, col in enumerate(row): - self.assertAlmostEqual(col, self.expected_normalised_cooperation[i][j]) + self.assertAlmostEqual( + col, self.expected_normalised_cooperation[i][j] + ) def test_initial_cooperation_rate(self): rs = axl.ResultSet( @@ -401,7 +444,9 @@ def test_state_distribution(self): ) self.assertIsInstance(rs.state_distribution, list) self.assertEqual(len(rs.state_distribution), rs.num_players) - self.assertEqual(rs.state_distribution, self.expected_state_distribution) + self.assertEqual( + rs.state_distribution, self.expected_state_distribution + ) def test_state_normalised_distribution(self): rs = axl.ResultSet( @@ -446,7 +491,9 @@ def test_vengeful_cooperation(self): self.assertEqual(len(rs.vengeful_cooperation), rs.num_players) for i, row in enumerate(rs.vengeful_cooperation): for j, col in enumerate(row): - self.assertAlmostEqual(col, self.expected_vengeful_cooperation[i][j]) + self.assertAlmostEqual( + col, self.expected_vengeful_cooperation[i][j] + ) def test_cooperating_rating(self): rs = axl.ResultSet( @@ -454,7 +501,9 @@ def test_cooperating_rating(self): ) self.assertIsInstance(rs.cooperating_rating, list) self.assertEqual(len(rs.cooperating_rating), rs.num_players) - self.assertEqual(rs.cooperating_rating, self.expected_cooperating_rating) + self.assertEqual( + rs.cooperating_rating, self.expected_cooperating_rating + ) def test_good_partner_matrix(self): rs = axl.ResultSet( @@ -462,7 +511,9 @@ def test_good_partner_matrix(self): ) self.assertIsInstance(rs.good_partner_matrix, list) self.assertEqual(len(rs.good_partner_matrix), rs.num_players) - self.assertEqual(rs.good_partner_matrix, self.expected_good_partner_matrix) + self.assertEqual( + rs.good_partner_matrix, self.expected_good_partner_matrix + ) def test_good_partner_rating(self): rs = axl.ResultSet( @@ -470,7 +521,9 @@ def test_good_partner_rating(self): ) self.assertIsInstance(rs.good_partner_rating, list) self.assertEqual(len(rs.good_partner_rating), rs.num_players) - self.assertEqual(rs.good_partner_rating, self.expected_good_partner_rating) + self.assertEqual( + rs.good_partner_rating, self.expected_good_partner_rating + ) def test_eigenjesus_rating(self): rs = axl.ResultSet( @@ -502,7 +555,10 @@ def test_self_interaction_for_random_strategies(self): def test_equality(self): rs_sets = [ axl.ResultSet( - self.filename, self.players, self.repetitions, progress_bar=False + self.filename, + self.players, + self.repetitions, + progress_bar=False, ) for _ in range(2) ] @@ -532,25 +588,34 @@ def test_summarise(self): [float(player.Median_score) for player in sd], ranked_median_scores ) - ranked_cooperation_rating = [rs.cooperating_rating[i] for i in rs.ranking] + ranked_cooperation_rating = [ + rs.cooperating_rating[i] for i in rs.ranking + ] self.assertEqual( [float(player.Cooperation_rating) for player in sd], ranked_cooperation_rating, ) ranked_median_wins = [nanmedian(rs.wins[i]) for i in rs.ranking] - self.assertEqual([float(player.Wins) for player in sd], ranked_median_wins) + self.assertEqual( + [float(player.Wins) for player in sd], ranked_median_wins + ) ranked_initial_coop_rates = [ self.expected_initial_cooperation_rate[i] for i in rs.ranking ] self.assertEqual( - [float(player.Initial_C_rate) for player in sd], ranked_initial_coop_rates + [float(player.Initial_C_rate) for player in sd], + ranked_initial_coop_rates, ) for player in sd: self.assertEqual( - player.CC_rate + player.CD_rate + player.DC_rate + player.DD_rate, 1 + player.CC_rate + + player.CD_rate + + player.DC_rate + + player.DD_rate, + 1, ) for rate in [ player.CC_to_C_rate, @@ -727,13 +792,21 @@ def setUpClass(cls): # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ - [0, mean([13 / 5 for _ in range(3)]), mean([2 / 5 for _ in range(3)])], + [ + 0, + mean([13 / 5 for _ in range(3)]), + mean([2 / 5 for _ in range(3)]), + ], [mean([13 / 5 for _ in range(3)]), 0, 0], [mean([17 / 5 for _ in range(3)]), 0, 0], ] cls.expected_payoff_stddevs = [ - [0, std([13 / 5 for _ in range(3)]), std([2 / 5 for _ in range(3)])], + [ + 0, + std([13 / 5 for _ in range(3)]), + std([2 / 5 for _ in range(3)]), + ], [std([13 / 5 for _ in range(3)]), 0, 0], [std([17 / 5 for _ in range(3)]), 0, 0], ] @@ -741,7 +814,11 @@ def setUpClass(cls): cls.expected_cooperation = [[0, 9, 9], [9, 0, 0], [0, 0, 0]] cls.expected_normalised_cooperation = [ - [0, mean([3 / 5 for _ in range(3)]), mean([3 / 5 for _ in range(3)])], + [ + 0, + mean([3 / 5 for _ in range(3)]), + mean([3 / 5 for _ in range(3)]), + ], [mean([3 / 5 for _ in range(3)]), 0, 0], [0, 0, 0], ] @@ -760,7 +837,11 @@ def setUpClass(cls): cls.expected_good_partner_rating = [1.0, 1.0, 0.0] - cls.expected_eigenjesus_rating = [0.447213595499958, 0.894427190999916, 0.0] + cls.expected_eigenjesus_rating = [ + 0.447213595499958, + 0.894427190999916, + 0.0, + ] cls.expected_eigenmoses_rating = [ -0.32929277996907086, @@ -784,7 +865,11 @@ def setUpClass(cls): Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), Counter({(C, D): 0.6, (D, D): 0.4}), ], - [Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), Counter(), Counter()], + [ + Counter({(C, C): 0.2, (C, D): 0.4, (D, C): 0.4}), + Counter(), + Counter(), + ], [Counter({(D, C): 0.6, (D, D): 0.4}), Counter(), Counter()], ] @@ -813,7 +898,11 @@ def setUpClass(cls): Counter(), Counter(), ], - [Counter({((D, C), D): 1.0, ((D, D), D): 1.0}), Counter(), Counter()], + [ + Counter({((D, C), D): 1.0, ((D, D), D): 1.0}), + Counter(), + Counter(), + ], ] def test_match_lengths(self): @@ -870,7 +959,8 @@ def setUpClass(cls): cls.edges = [(0, 1), (2, 3)] cls.expected_match_lengths = [ - [[0, 5, 0, 0], [5, 0, 0, 0], [0, 0, 0, 5], [0, 0, 5, 0]] for _ in range(3) + [[0, 5, 0, 0], [5, 0, 0, 0], [0, 0, 0, 5], [0, 0, 5, 0]] + for _ in range(3) ] cls.expected_scores = [ @@ -912,10 +1002,30 @@ def setUpClass(cls): ] cls.expected_score_diffs = [ - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [5.0, 5.0, 5.0]], - [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [-5.0, -5.0, -5.0], [0.0, 0.0, 0.0]], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + ], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + ], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [5.0, 5.0, 5.0], + ], + [ + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [-5.0, -5.0, -5.0], + [0.0, 0.0, 0.0], + ], ] cls.expected_payoff_diffs_means = [ @@ -1072,14 +1182,17 @@ def setUpClass(cls): cls.edges = [(0, 0), (1, 1), (2, 2), (3, 3)] cls.expected_match_lengths = [ - [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]] for _ in range(3) + [[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]] + for _ in range(3) ] cls.expected_scores = [[0 for _ in range(3)] for _ in range(4)] cls.expected_wins = [[0 for _ in range(3)] for _ in range(4)] - cls.expected_normalised_scores = [[0 for _ in range(3)] for i in range(4)] + cls.expected_normalised_scores = [ + [0 for _ in range(3)] for i in range(4) + ] cls.expected_ranking = [0, 1, 2, 3] @@ -1107,7 +1220,9 @@ def setUpClass(cls): [[0.0 for _ in range(3)] for _ in range(4)] for _ in range(4) ] - cls.expected_payoff_diffs_means = [[0.0 for _ in range(4)] for _ in range(4)] + cls.expected_payoff_diffs_means = [ + [0.0 for _ in range(4)] for _ in range(4) + ] # Recalculating to deal with numeric imprecision cls.expected_payoff_matrix = [ @@ -1148,7 +1263,9 @@ def setUpClass(cls): cls.expected_cooperating_rating = [0.0 for _ in range(4)] - cls.expected_good_partner_matrix = [[0.0 for _ in range(4)] for _ in range(4)] + cls.expected_good_partner_matrix = [ + [0.0 for _ in range(4)] for _ in range(4) + ] cls.expected_good_partner_rating = [0.0 for _ in range(4)] @@ -1216,7 +1333,9 @@ class TestSummary(unittest.TestCase): """Separate test to check that summary always builds without failures""" @given( - tournament=tournaments(min_size=2, max_size=5, max_turns=5, max_repetitions=3) + tournament=tournaments( + min_size=2, max_size=5, max_turns=5, max_repetitions=3 + ) ) @settings(max_examples=5, deadline=None) def test_summarise_without_failure(self, tournament): @@ -1227,7 +1346,11 @@ def test_summarise_without_failure(self, tournament): for player in sd: # round for numerical error total_rate = round( - player.CC_rate + player.CD_rate + player.DC_rate + player.DD_rate, 3 + player.CC_rate + + player.CD_rate + + player.DC_rate + + player.DD_rate, + 3, ) self.assertTrue(total_rate in [0, 1]) self.assertTrue(0 <= player.Initial_C_rate <= 1) @@ -1239,9 +1362,13 @@ class TestCreateCounterDict(unittest.TestCase): def test_basic_use(self): key_map = {"Col 1": "Var 1", "Col 2": "Var 2"} df = pd.DataFrame( - {"Col 1": [10, 20, 30], "Col 2": [1, 2, 0]}, index=[[5, 6, 7], [1, 2, 3]] + {"Col 1": [10, 20, 30], "Col 2": [1, 2, 0]}, + index=[[5, 6, 7], [1, 2, 3]], + ) + self.assertEqual( + create_counter_dict(df, 6, 2, key_map), + Counter({"Var 1": 20, "Var 2": 2}), ) self.assertEqual( - create_counter_dict(df, 6, 2, key_map), Counter({"Var 1": 20, "Var 2": 2}) + create_counter_dict(df, 7, 3, key_map), Counter({"Var 1": 30}) ) - self.assertEqual(create_counter_dict(df, 7, 3, key_map), Counter({"Var 1": 30})) diff --git a/axelrod/tests/unit/test_strategy_transformers.py b/axelrod/tests/unit/test_strategy_transformers.py index 7539ae8cd..ba244d2fe 100644 --- a/axelrod/tests/unit/test_strategy_transformers.py +++ b/axelrod/tests/unit/test_strategy_transformers.py @@ -20,6 +20,7 @@ class CanNotPickle(axl.Cooperator): class TestTransformers(TestMatch): """Test generic transformer properties.""" + def test_player_can_be_pickled(self): player = axl.Cooperator() self.assertTrue(player_can_be_pickled(player)) @@ -55,9 +56,13 @@ def test_DecoratorReBuilder(self): args = decorator.args kwargs = decorator.kwargs.copy() - new_decorator = DecoratorReBuilder()(factory_args, args, kwargs, new_prefix) + new_decorator = DecoratorReBuilder()( + factory_args, args, kwargs, new_prefix + ) - self.assertEqual(decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)()) + self.assertEqual( + decorator(axl.Cooperator)(), new_decorator(axl.Cooperator)() + ) def test_StrategyReBuilder_declared_class_with_name_prefix(self): player = CanNotPickle() @@ -151,9 +156,15 @@ def test_repr(self): str(InitialTransformer([D, D, C])(axl.Alternator)()), "Initial Alternator: [D, D, C]", ) - self.assertEqual(str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1") self.assertEqual( - str(MixedTransformer(0.3, (axl.Alternator, axl.Bully))(axl.Random)(0.1)), + str(FlipTransformer()(axl.Random)(0.1)), "Flipped Random: 0.1" + ) + self.assertEqual( + str( + MixedTransformer(0.3, (axl.Alternator, axl.Bully))(axl.Random)( + 0.1 + ) + ), "Mutated Random: 0.1: 0.3, ['Alternator', 'Bully']", ) @@ -181,7 +192,9 @@ def test_composition(self): p2 = axl.Cooperator() self.versus_test(p1, p2, [D, D, C, C, C, C, D, D], [C] * 8) - cls1 = FinalTransformer([D, D])(InitialTransformer([D, D])(axl.Cooperator)) + cls1 = FinalTransformer([D, D])( + InitialTransformer([D, D])(axl.Cooperator) + ) p1 = cls1() p2 = axl.Cooperator() self.versus_test(p1, p2, [D, D, C, C, C, C, D, D], [C] * 8) @@ -208,9 +221,11 @@ def deterministic_reclassifier(original_classifier, *args): return original_classifier StochasticTransformer = StrategyTransformerFactory( - generic_strategy_wrapper, reclassifier=stochastic_reclassifier) + generic_strategy_wrapper, reclassifier=stochastic_reclassifier + ) DeterministicTransformer = StrategyTransformerFactory( - generic_strategy_wrapper, reclassifier=deterministic_reclassifier) + generic_strategy_wrapper, reclassifier=deterministic_reclassifier + ) # Cooperator is not stochastic self.assertFalse(axl.Cooperator().classifier["stochastic"]) @@ -219,12 +234,16 @@ def deterministic_reclassifier(original_classifier, *args): self.assertTrue(player.classifier["stochastic"]) # Composing transforms should return it to not being stochastic - cls1 = compose_transformers(DeterministicTransformer(), StochasticTransformer()) + cls1 = compose_transformers( + DeterministicTransformer(), StochasticTransformer() + ) player = cls1(axl.Cooperator)() self.assertFalse(player.classifier["stochastic"]) # Explicit composition - player = DeterministicTransformer()(StochasticTransformer()(axl.Cooperator))() + player = DeterministicTransformer()( + StochasticTransformer()(axl.Cooperator) + )() self.assertFalse(player.classifier["stochastic"]) # Random is stochastic @@ -235,12 +254,16 @@ def deterministic_reclassifier(original_classifier, *args): self.assertFalse(player.classifier["stochastic"]) # Composing transforms should return it to being stochastic - cls1 = compose_transformers(StochasticTransformer(), DeterministicTransformer()) + cls1 = compose_transformers( + StochasticTransformer(), DeterministicTransformer() + ) player = cls1(axl.Random)() self.assertTrue(player.classifier["stochastic"]) # Explicit composition - player = StochasticTransformer()(DeterministicTransformer()(axl.Random))() + player = StochasticTransformer()( + DeterministicTransformer()(axl.Random) + )() self.assertTrue(player.classifier["stochastic"]) def test_nilpotency(self): @@ -316,16 +339,20 @@ def test_deadlock_breaks(self): axl.TitForTat(), InitialTransformer([D])(axl.TitForTat)(), [C, D, C, D], - [D, C, D, C]) + [D, C, D, C], + ) # Now let's use the transformer to break the deadlock to achieve # Mutual cooperation # self.versus_test( self.versus_test( axl.TitForTat(), - DeadlockBreakingTransformer()(InitialTransformer([D])(axl.TitForTat))(), + DeadlockBreakingTransformer()( + InitialTransformer([D])(axl.TitForTat) + )(), [C, D, C, C], - [D, C, C, C]) + [D, C, C, C], + ) class TestDualTransformer(TestMatch): @@ -377,10 +404,12 @@ def test_dual_transformer_simple_play_regression_test(self): DualTransformer()(axl.Cooperator) )() - self.versus_test(multiple_dual_transformers, - dual_transformer_not_first, - [D, D, D], - [D, D, D]) + self.versus_test( + multiple_dual_transformers, + dual_transformer_not_first, + [D, D, D], + [D, D, D], + ) def test_dual_transformer_multiple_interspersed_regression_test(self): """DualTransformer has failed when there were multiple DualTransformers. @@ -406,7 +435,9 @@ def test_final_transformer(self): p2 = FinalTransformer([D, D, D])(axl.Cooperator)() self.assertEqual(axl.Classifiers["makes_use_of"](p2), set(["length"])) self.assertEqual(axl.Classifiers["memory_depth"](p2), 3) - self.assertEqual(axl.Classifiers["makes_use_of"](axl.Cooperator()), set([])) + self.assertEqual( + axl.Classifiers["makes_use_of"](axl.Cooperator()), set([]) + ) self.versus_test(p1, p2, [C] * 8, [C, C, C, C, C, D, D, D], turns=8) def test_infinite_memory_depth_transformed(self): @@ -419,8 +450,9 @@ def test_final_transformer_unknown_length(self): """Tests the FinalTransformer when tournament length is not known.""" p1 = axl.Defector() p2 = FinalTransformer([D, D])(axl.Cooperator)() - self.versus_test(p1, p2, [D] * 6, [C] * 6, - match_attributes={"length": -1}) + self.versus_test( + p1, p2, [D] * 6, [C] * 6, match_attributes={"length": -1} + ) class TestFlipTransformer(TestMatch): @@ -449,8 +481,9 @@ def test_forgiving_transformer(self): p1 = ForgiverTransformer(0.5)(axl.Alternator)() p2 = axl.Defector() turns = 10 - self.versus_test(p1, p2, [C, D, C, C, D, C, C, D, C, D], [D] * turns, - seed=8) + self.versus_test( + p1, p2, [C, D, C, C, D, C, C, D, C, D], [D] * turns, seed=8 + ) def test_stochastic_values_classifier(self): p1 = ForgiverTransformer(0.5)(axl.Alternator)() @@ -473,7 +506,9 @@ def test_grudging1(self): def test_grudging2(self): p1 = InitialTransformer([C])(axl.Defector)() p2 = GrudgeTransformer(2)(axl.Cooperator)() - self.versus_test(p1, p2, [C, D, D, D, D, D, D, D], [C, C, C, C, D, D, D, D], seed=11) + self.versus_test( + p1, p2, [C, D, D, D, D, D, D, D], [C, C, C, C, D, D, D, D], seed=11 + ) class TestHistoryTrackingTransformer(TestMatch): @@ -510,8 +545,12 @@ def test_generic(self): Defector2 = transformer(axl.Defector) turns = 100 - self.versus_test(axl.Cooperator(), Cooperator2(), [C] * turns, [C] * turns) - self.versus_test(axl.Cooperator(), Defector2(), [C] * turns, [D] * turns) + self.versus_test( + axl.Cooperator(), Cooperator2(), [C] * turns, [C] * turns + ) + self.versus_test( + axl.Cooperator(), Defector2(), [C] * turns, [D] * turns + ) class TestInitialTransformer(TestMatch): @@ -684,7 +723,9 @@ def test_noisy_transformer(self): p1 = axl.Cooperator() p2 = NoisyTransformer(0.5)(axl.Cooperator)() self.assertTrue(axl.Classifiers["stochastic"](p2)) - self.versus_test(p1, p2, [C] * 10, [D, C, C, D, C, D, C, D, D, C], seed=1) + self.versus_test( + p1, p2, [C] * 10, [D, C, C, D, C, D, C, D, D, C], seed=1 + ) def test_noisy_transformation_stochastic(self): """Depending on the value of the noise parameter, the strategy may become stochastic @@ -722,7 +763,9 @@ def test_retailiating_cooperator_against_2TFT(self): TwoTitsForTat = RetaliationTransformer(2)(axl.Cooperator) p1 = TwoTitsForTat() p2 = axl.CyclerCCD() - self.versus_test(p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D]) + self.versus_test( + p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D] + ) class TestRetailiateUntilApologyTransformer(TestMatch): @@ -746,6 +789,7 @@ def test_retaliation_until_apology_stochastic(self): # Run the standard Player tests on some specifically transformed players + class TestNullInitialTransformedCooperator(TestPlayer): player = InitialTransformer([])(axl.Cooperator) name = "Initial Cooperator: []" @@ -786,7 +830,9 @@ class TestFinalTransformedCooperator(TestPlayer): class TestInitialFinalTransformedCooperator(TestPlayer): - player = InitialTransformer([D, D])(FinalTransformer([D, D, D])(axl.Cooperator)) + player = InitialTransformer([D, D])( + FinalTransformer([D, D, D])(axl.Cooperator) + ) name = "Initial Final Cooperator: [D, D, D]: [D, D]" expected_classifier = { "memory_depth": 3, @@ -799,7 +845,9 @@ class TestInitialFinalTransformedCooperator(TestPlayer): class TestFinalInitialTransformedCooperator(TestPlayer): - player = FinalTransformer([D, D])(InitialTransformer([D, D, D])(axl.Cooperator)) + player = FinalTransformer([D, D])( + InitialTransformer([D, D, D])(axl.Cooperator) + ) name = "Final Initial Cooperator: [D, D, D]: [D, D]" expected_classifier = { "memory_depth": 3, @@ -911,7 +959,9 @@ class TestMixed0(TestDefector): class TestMixed1(TestDefector): - name = "Mutated Cooperator: 1, " + name = ( + "Mutated Cooperator: 1, " + ) player = MixedTransformer(1, axl.Defector)(axl.Cooperator) expected_classifier = { "memory_depth": 0, @@ -996,7 +1046,9 @@ class TestJossAnnDual(TestPlayer): class TestJossAnnOverwriteClassifier(TestPlayer): name = "Joss-Ann Final Random: 0.5: [D, D]: (1.0, 0.0)" - player = JossAnnTransformer((1., 0.))(FinalTransformer([D, D])(axl.Random)) + player = JossAnnTransformer((1.0, 0.0))( + FinalTransformer([D, D])(axl.Random) + ) expected_classifier = { "memory_depth": 0, "stochastic": False, diff --git a/axelrod/tests/unit/test_strategy_utils.py b/axelrod/tests/unit/test_strategy_utils.py index b3a4f7ef0..5cade0dba 100644 --- a/axelrod/tests/unit/test_strategy_utils.py +++ b/axelrod/tests/unit/test_strategy_utils.py @@ -35,7 +35,9 @@ def test_no_cycle(self): history = [D, D, C, C, C] self.assertIsNone(detect_cycle(history)) - def test_regression_test_can_detect_cycle_that_is_repeated_exactly_once(self): + def test_regression_test_can_detect_cycle_that_is_repeated_exactly_once( + self, + ): self.assertEqual(detect_cycle([C, D, C, D]), (C, D)) self.assertEqual(detect_cycle([C, D, C, D, C]), (C, D)) @@ -53,11 +55,14 @@ def test_min_size_greater_than_two_times_history_tail_returns_none(self): def test_min_size_greater_than_two_times_max_size_has_no_effect(self): self.assertEqual( - detect_cycle([C, C, C, C, C, C, C, C], min_size=2, max_size=3), (C, C) + detect_cycle([C, C, C, C, C, C, C, C], min_size=2, max_size=3), + (C, C), ) def test_cycle_greater_than_max_size_returns_none(self): - self.assertEqual(detect_cycle([C, C, D] * 2, min_size=1, max_size=3), (C, C, D)) + self.assertEqual( + detect_cycle([C, C, D] * 2, min_size=1, max_size=3), (C, C, D) + ) self.assertIsNone(detect_cycle([C, C, D] * 2, min_size=1, max_size=2)) @@ -81,7 +86,9 @@ def test_strategies_with_countermeasures_return_their_countermeasures(self): inspector = axl.Cooperator() match = axl.Match((d_geller, inspector), turns=1) match.play() - self.assertEqual(inspect_strategy(inspector=inspector, opponent=d_geller), D) + self.assertEqual( + inspect_strategy(inspector=inspector, opponent=d_geller), D + ) self.assertEqual(d_geller.strategy(inspector), C) diff --git a/axelrod/tests/unit/test_tournament.py b/axelrod/tests/unit/test_tournament.py index f8cca4bb4..e73003398 100644 --- a/axelrod/tests/unit/test_tournament.py +++ b/axelrod/tests/unit/test_tournament.py @@ -41,7 +41,9 @@ test_edges = [(0, 1), (1, 2), (3, 4)] deterministic_strategies = [ - s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s()) + s + for s in axl.short_run_time_strategies + if not axl.Classifiers["stochastic"](s()) ] @@ -107,7 +109,9 @@ def test_init(self): noise=0.2, ) self.assertEqual(len(tournament.players), len(test_strategies)) - self.assertIsInstance(tournament.players[0].match_attributes["game"], axl.Game) + self.assertIsInstance( + tournament.players[0].match_attributes["game"], axl.Game + ) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertEqual(tournament.turns, self.test_turns) self.assertEqual(tournament.repetitions, 10) @@ -123,7 +127,9 @@ def test_init_with_match_attributes(self): ) mg = tournament.match_generator match_params = mg.build_single_match_params() - self.assertEqual(match_params["match_attributes"], {"length": float("inf")}) + self.assertEqual( + match_params["match_attributes"], {"length": float("inf")} + ) def test_warning(self): tournament = axl.Tournament( @@ -275,7 +281,11 @@ def test_serial_play_with_different_game(self): # Test that a non default game is passed to the result set game = axl.Game(p=-1, r=-1, s=-1, t=-1) tournament = axl.Tournament( - name=self.test_name, players=self.players, game=game, turns=1, repetitions=1 + name=self.test_name, + players=self.players, + game=game, + turns=1, + repetitions=1, ) results = tournament.play(progress_bar=False) self.assertLessEqual(np.max(results.scores), 0) @@ -409,7 +419,9 @@ def test_progress_bar_play_parallel(self): # these two examples were identified by hypothesis. @example( tournament=axl.Tournament( - players=[axl.BackStabber(), axl.MindReader()], turns=2, repetitions=1, + players=[axl.BackStabber(), axl.MindReader()], + turns=2, + repetitions=1, ) ) @example( @@ -541,7 +553,9 @@ def test_n_workers(self): tournament._n_workers(processes=max_processes + 2), max_processes ) - @unittest.skipIf(cpu_count() < 2, "not supported on single processor machines") + @unittest.skipIf( + cpu_count() < 2, "not supported on single processor machines" + ) def test_2_workers(self): # This is a separate test with a skip condition because we # cannot guarantee that the tests will always run on a machine @@ -725,9 +739,13 @@ def test_write_to_csv_without_results(self): turns=2, repetitions=2, ) - tournament.play(filename=self.filename, progress_bar=False, build_results=False) + tournament.play( + filename=self.filename, progress_bar=False, build_results=False + ) df = pd.read_csv(self.filename) - path = pathlib.Path("test_outputs/expected_test_tournament_no_results.csv") + path = pathlib.Path( + "test_outputs/expected_test_tournament_no_results.csv" + ) expected_df = pd.read_csv(axl_filename(path)) self.assertTrue(df.equals(expected_df)) @@ -747,7 +765,7 @@ def test_seeding_equality(self, seed): game=self.game, turns=10, repetitions=100, - seed=seed + seed=seed, ) tournament2 = axl.Tournament( name=self.test_name, @@ -755,7 +773,7 @@ def test_seeding_equality(self, seed): game=self.game, turns=10, repetitions=100, - seed=seed + seed=seed, ) for _ in range(4): results1 = tournament1.play(processes=2) @@ -770,7 +788,7 @@ def test_seeding_inequality(self): game=self.game, turns=2, repetitions=2, - seed=0 + seed=0, ) tournament2 = axl.Tournament( name=self.test_name, @@ -778,7 +796,7 @@ def test_seeding_inequality(self): game=self.game, turns=2, repetitions=2, - seed=10 + seed=10, ) results1 = tournament1.play() results2 = tournament2.play() @@ -802,7 +820,9 @@ def test_init(self): prob_end=self.test_prob_end, noise=0.2, ) - self.assertEqual(tournament.match_generator.prob_end, tournament.prob_end) + self.assertEqual( + tournament.match_generator.prob_end, tournament.prob_end + ) self.assertEqual(len(tournament.players), len(test_strategies)) self.assertEqual(tournament.game.score((C, C)), (3, 3)) self.assertIsNone(tournament.turns) @@ -821,7 +841,7 @@ def test_init(self): max_prob_end=0.9, min_repetitions=2, max_repetitions=4, - seed=100 + seed=100, ) ) @settings(max_examples=5, deadline=None) @@ -830,7 +850,7 @@ def test_init(self): players=[s() for s in test_strategies], prob_end=0.2, repetitions=test_repetitions, - seed=101 + seed=101, ) ) # These two examples are to make sure #465 is fixed. @@ -841,7 +861,7 @@ def test_init(self): players=[axl.BackStabber(), axl.MindReader()], prob_end=0.2, repetitions=1, - seed=102 + seed=102, ) ) @example( @@ -849,7 +869,7 @@ def test_init(self): players=[axl.ThueMorse(), axl.MindReader()], prob_end=0.2, repetitions=1, - seed=103 + seed=103, ) ) def test_property_serial_play(self, tournament): @@ -902,7 +922,9 @@ def test_init(self): seed=integers(min_value=0, max_value=4294967295), ) @settings(max_examples=5, deadline=None) - def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): + def test_complete_tournament( + self, strategies, turns, repetitions, noise, seed + ): """ A test to check that a spatial tournament on the complete multigraph gives the same results as the round robin. @@ -917,13 +939,20 @@ def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): # create a round robin tournament tournament = axl.Tournament( - players, repetitions=repetitions, turns=turns, noise=noise, - seed=seed + players, + repetitions=repetitions, + turns=turns, + noise=noise, + seed=seed, ) # create a complete spatial tournament spatial_tournament = axl.Tournament( - players, repetitions=repetitions, turns=turns, noise=noise, edges=edges, - seed=seed + players, + repetitions=repetitions, + turns=turns, + noise=noise, + edges=edges, + seed=seed, ) results = tournament.play(progress_bar=False) @@ -932,16 +961,23 @@ def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): self.assertEqual(results.ranked_names, spatial_results.ranked_names) self.assertEqual(results.num_players, spatial_results.num_players) self.assertEqual(results.repetitions, spatial_results.repetitions) - self.assertEqual(results.payoff_diffs_means, spatial_results.payoff_diffs_means) + self.assertEqual( + results.payoff_diffs_means, spatial_results.payoff_diffs_means + ) self.assertEqual(results.payoff_matrix, spatial_results.payoff_matrix) self.assertEqual(results.payoff_stddevs, spatial_results.payoff_stddevs) self.assertEqual(results.payoffs, spatial_results.payoffs) - self.assertEqual(results.cooperating_rating, spatial_results.cooperating_rating) + self.assertEqual( + results.cooperating_rating, spatial_results.cooperating_rating + ) self.assertEqual(results.cooperation, spatial_results.cooperation) self.assertEqual( - results.normalised_cooperation, spatial_results.normalised_cooperation + results.normalised_cooperation, + spatial_results.normalised_cooperation, + ) + self.assertEqual( + results.normalised_scores, spatial_results.normalised_scores ) - self.assertEqual(results.normalised_scores, spatial_results.normalised_scores) self.assertEqual( results.good_partner_matrix, spatial_results.good_partner_matrix ) @@ -961,7 +997,12 @@ def test_particular_tournament(self): edges = [(0, 2), (0, 3), (1, 2), (1, 3)] tournament = axl.Tournament(players, edges=edges) results = tournament.play(progress_bar=False) - expected_ranked_names = ["Cooperator", "Tit For Tat", "Grudger", "Defector"] + expected_ranked_names = [ + "Cooperator", + "Tit For Tat", + "Grudger", + "Defector", + ] self.assertEqual(results.ranked_names, expected_ranked_names) # Check that this tournament runs with noise @@ -1018,17 +1059,19 @@ def test_complete_tournament(self, strategies, prob_end, seed, reps): # create a prob end round robin tournament - tournament = axl.Tournament(players, prob_end=prob_end, repetitions=reps, - seed=seed) + tournament = axl.Tournament( + players, prob_end=prob_end, repetitions=reps, seed=seed + ) results = tournament.play(progress_bar=False) # create a complete spatial tournament # edges - edges = [(i, j) for i in range(len(players)) for j in range(i, len(players))] + edges = [ + (i, j) for i in range(len(players)) for j in range(i, len(players)) + ] spatial_tournament = axl.Tournament( - players, prob_end=prob_end, repetitions=reps, edges=edges, - seed=seed + players, prob_end=prob_end, repetitions=reps, edges=edges, seed=seed ) spatial_results = spatial_tournament.play(progress_bar=False) self.assertEqual(results.match_lengths, spatial_results.match_lengths) @@ -1063,7 +1106,9 @@ def test_one_turn_tournament(self, tournament, seed): one_turn_results = tournament.play(progress_bar=False) self.assertEqual(prob_end_results.scores, one_turn_results.scores) self.assertEqual(prob_end_results.wins, one_turn_results.wins) - self.assertEqual(prob_end_results.cooperation, one_turn_results.cooperation) + self.assertEqual( + prob_end_results.cooperation, one_turn_results.cooperation + ) class TestHelperFunctions(unittest.TestCase): diff --git a/axelrod/tournament.py b/axelrod/tournament.py index 925fe1477..41d7d45f0 100644 --- a/axelrod/tournament.py +++ b/axelrod/tournament.py @@ -33,7 +33,7 @@ def __init__( noise: float = 0, edges: List[Tuple] = None, match_attributes: dict = None, - seed: int = None + seed: int = None, ) -> None: """ Parameters @@ -87,7 +87,7 @@ def __init__( noise=self.noise, edges=edges, match_attributes=match_attributes, - seed=self.seed + seed=self.seed, ) self._logger = logging.getLogger(__name__) @@ -232,7 +232,9 @@ def _get_file_objects(self, build_results=True): def _get_progress_bar(self): if self.use_progress_bar: - return tqdm.tqdm(total=self.match_generator.size, desc="Playing matches") + return tqdm.tqdm( + total=self.match_generator.size, desc="Playing matches" + ) return None def _write_interactions_to_file(self, results, writer): @@ -256,8 +258,14 @@ def _write_interactions_to_file(self, results, writer): ) = results for index, player_index in enumerate(index_pair): opponent_index = index_pair[index - 1] - row = [self.num_interactions, player_index, opponent_index, repetition, - str(self.players[player_index]), str(self.players[opponent_index])] + row = [ + self.num_interactions, + player_index, + opponent_index, + repetition, + str(self.players[player_index]), + str(self.players[opponent_index]), + ] history = actions_to_str([i[index] for i in interaction]) row.append(history) @@ -277,16 +285,24 @@ def _write_interactions_to_file(self, results, writer): for state in states: row.append(state_distribution[state]) for state in states: - row.append(state_to_action_distributions[index][(state, C)]) - row.append(state_to_action_distributions[index][(state, D)]) + row.append( + state_to_action_distributions[index][(state, C)] + ) + row.append( + state_to_action_distributions[index][(state, D)] + ) - row.append(int(cooperations[index] >= cooperations[index - 1])) + row.append( + int(cooperations[index] >= cooperations[index - 1]) + ) writer.writerow(row) repetition += 1 self.num_interactions += 1 - def _run_parallel(self, processes: int = 2, build_results: bool = True) -> bool: + def _run_parallel( + self, processes: int = 2, build_results: bool = True + ) -> bool: """ Run all matches in parallel @@ -349,7 +365,8 @@ def _start_workers( """ for worker in range(workers): process = Process( - target=self._worker, args=(work_queue, done_queue, build_results) + target=self._worker, + args=(work_queue, done_queue, build_results), ) work_queue.put("STOP") process.start() @@ -387,7 +404,9 @@ def _process_done_queue( _close_objects(out_file, progress_bar) return True - def _worker(self, work_queue: Queue, done_queue: Queue, build_results: bool = True): + def _worker( + self, work_queue: Queue, done_queue: Queue, build_results: bool = True + ): """ The work for each parallel sub-process to execute. @@ -455,13 +474,17 @@ def _calculate_results(self, interactions): turns = len(interactions) results.append(turns) - score_per_turns = iu.compute_final_score_per_turn(interactions, self.game) + score_per_turns = iu.compute_final_score_per_turn( + interactions, self.game + ) results.append(score_per_turns) score_diffs_per_turns = score_diffs[0] / turns, score_diffs[1] / turns results.append(score_diffs_per_turns) - initial_coops = tuple(map(bool, iu.compute_cooperations(interactions[:1]))) + initial_coops = tuple( + map(bool, iu.compute_cooperations(interactions[:1])) + ) results.append(initial_coops) cooperations = iu.compute_cooperations(interactions) diff --git a/docs/conf.py b/docs/conf.py index 72d0a774c..0e33e21c3 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -231,7 +231,13 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ("index", "Axelrod.tex", "Axelrod Documentation", "Vincent Knight", "manual") + ( + "index", + "Axelrod.tex", + "Axelrod Documentation", + "Vincent Knight", + "manual", + ) ] # The name of an image file (relative to this directory) to place at the top of @@ -259,7 +265,9 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [("index", "axelrod", "Axelrod Documentation", ["Vincent Knight"], 1)] +man_pages = [ + ("index", "axelrod", "Axelrod Documentation", ["Vincent Knight"], 1) +] # If true, show URL addresses after external links. # man_show_urls = False diff --git a/doctests.py b/doctests.py index 8bc74f50f..3ac377571 100644 --- a/doctests.py +++ b/doctests.py @@ -27,7 +27,8 @@ def load_tests(loader, tests, ignore): tests.addTests( doctest.DocFileSuite( # ELLIPSIS option tells doctest to ignore portions of the verification value. - os.path.join(root, f), optionflags=doctest.ELLIPSIS + os.path.join(root, f), + optionflags=doctest.ELLIPSIS, ) ) From 0d0332d5d1a6757b81cc3ba02f1c72ac43aba9c7 Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 13:07:42 +0100 Subject: [PATCH 09/10] Run isort. --- axelrod/strategies/titfortat.py | 5 +---- axelrod/tests/property.py | 8 +------- axelrod/tests/strategies/test_evolvable_player.py | 6 +----- axelrod/tests/strategies/test_finite_state_machines.py | 4 +--- 4 files changed, 4 insertions(+), 19 deletions(-) diff --git a/axelrod/strategies/titfortat.py b/axelrod/strategies/titfortat.py index d3ab2d68d..fbd54f7cd 100644 --- a/axelrod/strategies/titfortat.py +++ b/axelrod/strategies/titfortat.py @@ -1,9 +1,6 @@ from axelrod.action import Action, actions_to_str from axelrod.player import Player -from axelrod.strategy_transformers import ( - FinalTransformer, - TrackHistoryTransformer, -) +from axelrod.strategy_transformers import FinalTransformer, TrackHistoryTransformer C, D = Action.C, Action.D diff --git a/axelrod/tests/property.py b/axelrod/tests/property.py index d5d1919d9..b1452f63d 100644 --- a/axelrod/tests/property.py +++ b/axelrod/tests/property.py @@ -4,13 +4,7 @@ import itertools import axelrod as axl -from hypothesis.strategies import ( - composite, - floats, - integers, - lists, - sampled_from, -) +from hypothesis.strategies import composite, floats, integers, lists, sampled_from @composite diff --git a/axelrod/tests/strategies/test_evolvable_player.py b/axelrod/tests/strategies/test_evolvable_player.py index ea0fb77fc..95888027a 100644 --- a/axelrod/tests/strategies/test_evolvable_player.py +++ b/axelrod/tests/strategies/test_evolvable_player.py @@ -3,11 +3,7 @@ import axelrod as axl from axelrod.action import Action -from axelrod.evolvable_player import ( - copy_lists, - crossover_dictionaries, - crossover_lists, -) +from axelrod.evolvable_player import copy_lists, crossover_dictionaries, crossover_lists from .test_player import TestPlayer diff --git a/axelrod/tests/strategies/test_finite_state_machines.py b/axelrod/tests/strategies/test_finite_state_machines.py index f7eab3029..1db116ffd 100644 --- a/axelrod/tests/strategies/test_finite_state_machines.py +++ b/axelrod/tests/strategies/test_finite_state_machines.py @@ -2,9 +2,7 @@ import unittest import axelrod as axl -from axelrod.compute_finite_state_machine_memory import ( - get_memory_from_transitions, -) +from axelrod.compute_finite_state_machine_memory import get_memory_from_transitions from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.finite_state_machines import ( EvolvableFSMPlayer, From ec7ca49c7109e788ef6ee965be3313f56381054f Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 12 Aug 2020 13:18:10 +0100 Subject: [PATCH 10/10] Make black and isort compatible. --- .isort.cfg | 2 +- axelrod/strategies/titfortat.py | 5 ++++- axelrod/tests/property.py | 8 +++++++- axelrod/tests/strategies/test_evolvable_player.py | 6 +++++- axelrod/tests/strategies/test_finite_state_machines.py | 4 +++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.isort.cfg b/.isort.cfg index 46cb98577..c28f73f7c 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -4,4 +4,4 @@ multi_line_output = 3 include_trailing_comma = True force_grid_wrap = 0 combine_as_imports = True -line_length = 88 +line_length = 80 diff --git a/axelrod/strategies/titfortat.py b/axelrod/strategies/titfortat.py index fbd54f7cd..d3ab2d68d 100644 --- a/axelrod/strategies/titfortat.py +++ b/axelrod/strategies/titfortat.py @@ -1,6 +1,9 @@ from axelrod.action import Action, actions_to_str from axelrod.player import Player -from axelrod.strategy_transformers import FinalTransformer, TrackHistoryTransformer +from axelrod.strategy_transformers import ( + FinalTransformer, + TrackHistoryTransformer, +) C, D = Action.C, Action.D diff --git a/axelrod/tests/property.py b/axelrod/tests/property.py index b1452f63d..d5d1919d9 100644 --- a/axelrod/tests/property.py +++ b/axelrod/tests/property.py @@ -4,7 +4,13 @@ import itertools import axelrod as axl -from hypothesis.strategies import composite, floats, integers, lists, sampled_from +from hypothesis.strategies import ( + composite, + floats, + integers, + lists, + sampled_from, +) @composite diff --git a/axelrod/tests/strategies/test_evolvable_player.py b/axelrod/tests/strategies/test_evolvable_player.py index 95888027a..ea0fb77fc 100644 --- a/axelrod/tests/strategies/test_evolvable_player.py +++ b/axelrod/tests/strategies/test_evolvable_player.py @@ -3,7 +3,11 @@ import axelrod as axl from axelrod.action import Action -from axelrod.evolvable_player import copy_lists, crossover_dictionaries, crossover_lists +from axelrod.evolvable_player import ( + copy_lists, + crossover_dictionaries, + crossover_lists, +) from .test_player import TestPlayer diff --git a/axelrod/tests/strategies/test_finite_state_machines.py b/axelrod/tests/strategies/test_finite_state_machines.py index 1db116ffd..f7eab3029 100644 --- a/axelrod/tests/strategies/test_finite_state_machines.py +++ b/axelrod/tests/strategies/test_finite_state_machines.py @@ -2,7 +2,9 @@ import unittest import axelrod as axl -from axelrod.compute_finite_state_machine_memory import get_memory_from_transitions +from axelrod.compute_finite_state_machine_memory import ( + get_memory_from_transitions, +) from axelrod.evolvable_player import InsufficientParametersError from axelrod.strategies.finite_state_machines import ( EvolvableFSMPlayer,