Skip to content

Commit 1fd9dd6

Browse files
committed
Rework classifiers for transformers, rebuild expected classifiers to correctly capture Meta player makes_use_of
1 parent 761f887 commit 1fd9dd6

File tree

4 files changed

+78
-28
lines changed

4 files changed

+78
-28
lines changed

axelrod/data/all_classifiers.yml

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -961,39 +961,47 @@ Meta Hunter Aggressive:
961961
Meta Majority:
962962
inspects_source: false
963963
long_run_time: true
964-
makes_use_of: !!set {}
964+
makes_use_of: !!set
965+
game: null
966+
length: null
965967
manipulates_source: false
966968
manipulates_state: false
967969
memory_depth: .inf
968970
stochastic: true
969971
Meta Majority Finite Memory:
970972
inspects_source: false
971973
long_run_time: true
972-
makes_use_of: !!set {}
974+
makes_use_of: !!set
975+
game: null
973976
manipulates_source: false
974977
manipulates_state: false
975978
memory_depth: .inf
976979
stochastic: true
977980
Meta Majority Long Memory:
978981
inspects_source: false
979982
long_run_time: true
980-
makes_use_of: !!set {}
983+
makes_use_of: !!set
984+
game: null
985+
length: null
981986
manipulates_source: false
982987
manipulates_state: false
983988
memory_depth: .inf
984989
stochastic: true
985990
Meta Majority Memory One:
986991
inspects_source: false
987992
long_run_time: true
988-
makes_use_of: !!set {}
993+
makes_use_of: !!set
994+
game: null
989995
manipulates_source: false
990996
manipulates_state: false
991997
memory_depth: .inf
992998
stochastic: true
993999
Meta Minority:
9941000
inspects_source: false
9951001
long_run_time: true
996-
makes_use_of: !!set {}
1002+
makes_use_of: !!set
1003+
game: null
1004+
length: null
9971005
manipulates_source: false
9981006
manipulates_state: false
9991007
memory_depth: .inf
@@ -1011,6 +1019,7 @@ Meta Winner:
10111019
long_run_time: true
10121020
makes_use_of: !!set
10131021
game: null
1022+
length: null
10141023
manipulates_source: false
10151024
manipulates_state: false
10161025
memory_depth: .inf
@@ -1020,6 +1029,7 @@ Meta Winner Deterministic:
10201029
long_run_time: true
10211030
makes_use_of: !!set
10221031
game: null
1032+
length: null
10231033
manipulates_source: false
10241034
manipulates_state: false
10251035
memory_depth: .inf
@@ -1029,6 +1039,7 @@ Meta Winner Ensemble:
10291039
long_run_time: true
10301040
makes_use_of: !!set
10311041
game: null
1042+
length: null
10321043
manipulates_source: false
10331044
manipulates_state: false
10341045
memory_depth: .inf
@@ -1047,6 +1058,7 @@ Meta Winner Long Memory:
10471058
long_run_time: true
10481059
makes_use_of: !!set
10491060
game: null
1061+
length: null
10501062
manipulates_source: false
10511063
manipulates_state: false
10521064
memory_depth: .inf
@@ -1065,6 +1077,7 @@ Meta Winner Stochastic:
10651077
long_run_time: true
10661078
makes_use_of: !!set
10671079
game: null
1080+
length: null
10681081
manipulates_source: false
10691082
manipulates_state: false
10701083
memory_depth: .inf
@@ -1130,40 +1143,47 @@ N Tit(s) For M Tat(s):
11301143
NMWE Deterministic:
11311144
inspects_source: false
11321145
long_run_time: true
1133-
makes_use_of: &id001 !!set
1146+
makes_use_of: !!set
11341147
game: null
1148+
length: null
11351149
manipulates_source: false
11361150
manipulates_state: false
11371151
memory_depth: .inf
11381152
stochastic: true
11391153
NMWE Finite Memory:
11401154
inspects_source: false
11411155
long_run_time: true
1142-
makes_use_of: *id001
1156+
makes_use_of: !!set
1157+
game: null
11431158
manipulates_source: false
11441159
manipulates_state: false
11451160
memory_depth: .inf
11461161
stochastic: true
11471162
NMWE Long Memory:
11481163
inspects_source: false
11491164
long_run_time: true
1150-
makes_use_of: *id001
1165+
makes_use_of: !!set
1166+
game: null
1167+
length: null
11511168
manipulates_source: false
11521169
manipulates_state: false
11531170
memory_depth: .inf
11541171
stochastic: true
11551172
NMWE Memory One:
11561173
inspects_source: false
11571174
long_run_time: true
1158-
makes_use_of: *id001
1175+
makes_use_of: !!set
1176+
game: null
11591177
manipulates_source: false
11601178
manipulates_state: false
11611179
memory_depth: .inf
11621180
stochastic: true
11631181
NMWE Stochastic:
11641182
inspects_source: false
11651183
long_run_time: true
1166-
makes_use_of: *id001
1184+
makes_use_of: !!set
1185+
game: null
1186+
length: null
11671187
manipulates_source: false
11681188
manipulates_state: false
11691189
memory_depth: .inf
@@ -1197,14 +1217,17 @@ Nice Meta Winner:
11971217
long_run_time: true
11981218
makes_use_of: !!set
11991219
game: null
1220+
length: null
12001221
manipulates_source: false
12011222
manipulates_state: false
12021223
memory_depth: .inf
12031224
stochastic: true
12041225
Nice Meta Winner Ensemble:
12051226
inspects_source: false
12061227
long_run_time: true
1207-
makes_use_of: *id001
1228+
makes_use_of: !!set
1229+
game: null
1230+
length: null
12081231
manipulates_source: false
12091232
manipulates_state: false
12101233
memory_depth: .inf

axelrod/makes_use_of.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def method_makes_use_of(method: Callable) -> Set[Text]:
1616

1717

1818
def class_makes_use_of(cls) -> Set[Text]:
19-
result = set()
19+
try:
20+
result = cls.classifier["makes_use_of"]
21+
except (AttributeError, KeyError):
22+
result = set()
23+
2024
for method in inspect.getmembers(cls, inspect.ismethod):
2125
if method[0] == "__init__":
2226
continue
@@ -32,10 +36,13 @@ def makes_use_of(player: Type[Player]) -> Set[Text]:
3236

3337

3438
def makes_use_of_variant(
35-
player_or_method: Union[Callable, Type[Player]], verbose=False
39+
player_or_method: Union[Callable, Type[Player]]
3640
) -> Set[Text]:
3741
"""A version of makes_use_of that works on functions or player classes."""
3842
try:
3943
return method_makes_use_of(player_or_method)
40-
except:
44+
# OSError catches the case of a transformed player, which has a dynamically
45+
# created class.
46+
# TypeError is the case in which we have a class rather than a method
47+
except (OSError, TypeError):
4148
return class_makes_use_of(player_or_method)

axelrod/strategy_transformers.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
See the various Meta strategies for another type of transformation.
66
"""
77

8+
import inspect
89
from importlib import import_module
910
from typing import Any
1011

1112
from axelrod.strategies.sequence_player import SequencePlayer
1213

1314
from .action import Action
14-
from .makes_use_of import *
15+
from .makes_use_of import makes_use_of_variant
1516
from .player import Player
1617

1718
C, D = Action.C, Action.D
@@ -25,8 +26,11 @@
2526
def makes_use_of_reclassifier(original_classifier, player_class, wrapper):
2627
"""Reclassifier for post-transformation determination of whether
2728
strategy makes_use_of anything differently."""
28-
classifier_makes_use_of = makes_use_of(player_class)
29+
classifier_makes_use_of = makes_use_of_variant(player_class)
30+
# Wrapper is usually a function, but can be a class, e.g. in the case of the
31+
# RetaliationUntilApologyWrapper
2932
classifier_makes_use_of.update(makes_use_of_variant(wrapper))
33+
3034
try:
3135
original_classifier["makes_use_of"].update(classifier_makes_use_of)
3236
except KeyError:
@@ -156,16 +160,6 @@ def strategy(self, opponent):
156160
# Modify the Player name (class variable inherited from Player)
157161
name = " ".join([name_prefix, PlayerClass.name])
158162

159-
# original_classifier = copy.deepcopy(PlayerClass.classifier) # Copy
160-
# if reclassifier is not None:
161-
# classifier = reclassifier(original_classifier, *args, **kwargs)
162-
# else:
163-
# classifier = original_classifier
164-
# classifier_makes_use_of = makes_use_of(PlayerClass)
165-
# classifier_makes_use_of.update(
166-
# makes_use_of_variant(strategy_wrapper))
167-
# classifier["makes_use_of"] = classifier_makes_use_of
168-
169163
# Define the new __repr__ method to add the wrapper arguments
170164
# at the end of the name
171165
def __repr__(self):
@@ -494,10 +488,14 @@ def final_sequence(player, opponent, action, seq):
494488

495489

496490
def final_reclassifier(original_classifier, seq):
497-
"""Reclassify the strategy"""
491+
"""Reclassify the strategy."""
498492
original_classifier["memory_depth"] = max(
499493
len(seq), original_classifier["memory_depth"]
500494
)
495+
# This should also be picked up by the makes_use_of inspection,
496+
# but we list it here to be explicit.
497+
if len(seq) > 0:
498+
original_classifier["makes_use_of"].add("length")
501499
return original_classifier
502500

503501

axelrod/tests/unit/test_makes_use_of.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import unittest
44

55
import axelrod as axl
6-
from axelrod.makes_use_of import makes_use_of
6+
from axelrod.makes_use_of import (
7+
class_makes_use_of,
8+
makes_use_of,
9+
makes_use_of_variant,
10+
method_makes_use_of
11+
)
12+
from axelrod.strategy_transformers import final_sequence
713

814

915
class TestMakesUseOfLengthAndGamePlayer(axl.Player):
@@ -45,3 +51,19 @@ def test_makes_use_of_length_and_game(self):
4551

4652
def test_makes_use_of_empty(self):
4753
self.assertEqual(makes_use_of(TestMakesUseOfNothingPlayer()), set())
54+
55+
def test_untransformed_class(self):
56+
for player in [axl.Cooperator(), axl.Random()]:
57+
self.assertEqual(class_makes_use_of(player), set())
58+
self.assertEqual(makes_use_of_variant(player), set())
59+
self.assertEqual(method_makes_use_of(player.strategy), set())
60+
61+
def test_transformer_wrapper(self):
62+
# Test that the final transformer wrapper makes use of length
63+
self.assertEqual(method_makes_use_of(final_sequence), {"length"})
64+
65+
def test_makes_use_of_transformed(self):
66+
# These players use match length via Final transformer
67+
for player in [axl.BackStabber(), axl.FirstBySteinAndRapoport()]:
68+
self.assertEqual(makes_use_of(player), {"length"})
69+
self.assertEqual(makes_use_of_variant(player), {"length"})

0 commit comments

Comments
 (0)