Skip to content

Commit 6e4daa5

Browse files
authored
Merge pull request #1099 from Axelrod-Python/mem2_zd
Adds a memory two generalization of a zero determinant strategy
2 parents 715f67c + b8de0e5 commit 6e4daa5

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

axelrod/strategies/_strategies.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .geller import Geller, GellerCooperator, GellerDefector
2828
from .gambler import (
2929
Gambler, PSOGambler1_1_1, PSOGambler2_2_2, PSOGambler2_2_2_Noise05,
30-
PSOGamblerMem1)
30+
PSOGamblerMem1, ZDMem2)
3131
from .gobymajority import (GoByMajority,
3232
GoByMajority10, GoByMajority20, GoByMajority40,
3333
GoByMajority5,
@@ -87,6 +87,7 @@
8787

8888
# Note: Meta* strategies are handled in .__init__.py
8989

90+
9091
all_strategies = [
9192
Adaptive,
9293
AdaptiveTitForTat,
@@ -270,6 +271,7 @@
270271
ZDExtort4,
271272
ZDGTFT2,
272273
ZDGen2,
274+
ZDMem2,
273275
ZDMischief,
274276
ZDSet2,
275277
e,

axelrod/strategies/gambler.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,36 @@ def __init__(self) -> None:
117117
parameters = Plays(self_plays=2, op_plays=2, op_openings=2)
118118

119119
super().__init__(parameters=parameters, pattern=pattern)
120+
121+
122+
class ZDMem2(Gambler):
123+
"""
124+
A memory two generalization of a zero determinant player.
125+
126+
Names:
127+
128+
- ZDMem2: Original name by Marc Harper
129+
- Unnamed [LiS2014]_
130+
131+
"""
132+
133+
name = "ZD-Mem2"
134+
135+
classifier = {
136+
'memory_depth': 2,
137+
'stochastic': True,
138+
'makes_use_of': set(),
139+
'long_run_time': False,
140+
'inspects_source': False,
141+
'manipulates_source': False,
142+
'manipulates_state': False
143+
}
144+
145+
def __init__(self) -> None:
146+
pattern = [11 / 12, 4 / 11, 7 / 9, 1 / 10,
147+
5 / 6, 3 / 11, 7 / 9, 1 / 10,
148+
2 / 3, 1 / 11, 7 / 9, 1 / 10,
149+
3 / 4, 2 / 11, 7 / 9, 1 / 10]
150+
parameters = Plays(self_plays=2, op_plays=2, op_openings=0)
151+
152+
super().__init__(parameters=parameters, pattern=pattern)

axelrod/strategies/lookerup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ def make_keys_into_plays(lookup_table: dict) -> dict:
176176
return new_table
177177

178178

179-
def create_lookup_table_keys(player_depth: int,
180-
op_depth: int,
179+
def create_lookup_table_keys(player_depth: int, op_depth: int,
181180
op_openings_depth: int) -> list:
182181
"""Returns a list of Plays that has all possible permutations of C's and
183182
D's for each specified depth. the list is in order,

axelrod/tests/strategies/test_gambler.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,67 @@ def test_vs_DCDDC(self):
375375
self.versus_test(axelrod.MockPlayer(opponent_actions),
376376
expected_actions=new_expected,
377377
seed=new_seed)
378+
379+
380+
class TestZDMem2(TestPlayer):
381+
name = "ZD-Mem2"
382+
player = axelrod.ZDMem2
383+
384+
expected_classifier = {
385+
'memory_depth': 2,
386+
'stochastic': True,
387+
'makes_use_of': set(),
388+
'long_run_time': False,
389+
'inspects_source': False,
390+
'manipulates_source': False,
391+
'manipulates_state': False
392+
}
393+
394+
def test_new_data(self):
395+
original_data = {
396+
('', 'CC', 'CC'): 11 /12,
397+
('', 'CC', 'CD'): 4 / 11,
398+
('', 'CC', 'DC'): 7 / 9,
399+
('', 'CC', 'DD'): 1 / 10,
400+
('', 'CD', 'CC'): 5 / 6,
401+
('', 'CD', 'CD'): 3 / 11,
402+
('', 'CD', 'DC'): 7 / 9,
403+
('', 'CD', 'DD'): 1 / 10,
404+
('', 'DC', 'CC'): 2 / 3,
405+
('', 'DC', 'CD'): 1 / 11,
406+
('', 'DC', 'DC'): 7 / 9,
407+
('', 'DC', 'DD'): 1 / 10 ,
408+
('', 'DD', 'CC'): 3 / 4,
409+
('', 'DD', 'CD'): 2 / 11,
410+
('', 'DD', 'DC'): 7 / 9,
411+
('', 'DD', 'DD'): 1 / 10,
412+
}
413+
converted_original = convert_original_to_current(original_data)
414+
self.assertEqual(self.player().lookup_dict, converted_original)
415+
416+
def test_vs_defector(self):
417+
seed = 5
418+
expected = [(C, D), (C, D), (D, D), (D, D), (D, D), (D, D), (D, D),
419+
(D, D), (C, D), (D, D)]
420+
421+
self.versus_test(axelrod.Defector(), expected_actions=expected,
422+
seed=seed)
423+
424+
def test_vs_cooperator(self):
425+
seed = 5
426+
expected = [(C, C), (C, C), (C, C), (C, C), (C, C), (D, C), (C, C),
427+
(D, C), (C, C), (C, C)]
428+
429+
self.versus_test(axelrod.Cooperator(), expected_actions=expected,
430+
seed=seed)
431+
432+
def test_vs_alternator(self):
433+
seed = 2
434+
expected = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D), (D, C)]
435+
self.versus_test(axelrod.Alternator(), expected_actions=expected,
436+
seed=seed)
437+
438+
new_seed = 1
439+
expected = [(C, C), (C, D), (C, C), (D, D), (D, C), (C, D), (D, C)]
440+
self.versus_test(axelrod.Alternator(), expected_actions=expected,
441+
seed=new_seed)

docs/reference/bibliography.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ documentation.
3535
.. [Li2009] Li, J. & Kendall, G. (2009). A Strategy with Novel Evolutionary Features for the Iterated Prisoner’s Dilemma. Evolutionary Computation 17(2): 257–274.
3636
.. [Li2011] Li, J., Hingston, P., Member, S., & Kendall, G. (2011). Engineering Design of Strategies for Winning Iterated Prisoner ’ s Dilemma Competitions, 3(4), 348–360.
3737
.. [Li2014] Li, J. and Kendall, G. (2014). The Effect of Memory Size on the Evolutionary Stability of Strategies in Iterated Prisoner's Dilemma. IEEE Transactions on Evolutionary Computation, 18(6) 819-826
38+
.. [LiS2014] Li, Siwei. (2014). Strategies in the Stochastic Iterated Prisoner's Dilemma. Available at: http://math.uchicago.edu/~may/REU2014/REUPapers/Li,Siwei.pdf
3839
.. [Mathieu2015] Mathieu, P. and Delahaye, J. (2015). New Winning Strategies
3940
for the Iterated Prisoner's Dilemma. Proceedings of the 2015
4041
International Conference on Autonomous Agents and Multiagent Systems.

docs/tutorials/advanced/classification_of_strategies.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ strategies::
4747
... }
4848
>>> strategies = axl.filtered_strategies(filterset)
4949
>>> len(strategies)
50-
70
50+
71
5151

5252
Or, to find out how many strategies only use 1 turn worth of memory to
5353
make a decision::
@@ -69,7 +69,7 @@ range of memory_depth values, we can use the 'min_memory_depth' and
6969
... }
7070
>>> strategies = axl.filtered_strategies(filterset)
7171
>>> len(strategies)
72-
54
72+
55
7373

7474
We can also identify strategies that make use of particular properties of the
7575
tournament. For example, here is the number of strategies that make use of the

0 commit comments

Comments
 (0)