Skip to content

Commit a8db7da

Browse files
committed
Added tests for Defect mode.
1 parent 8231f07 commit a8db7da

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

axelrod/strategies/axelrod_second.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ def __init__(self):
10941094
self.prob = 0.25
10951095

10961096
self.move_history = np.zeros([4, 2])
1097+
self.chi_squared = None
10971098
self.history_row = 0
10981099

10991100
self.more_coop = 0
@@ -1155,6 +1156,8 @@ def detect_random(self, turn):
11551156
if expct > 1.0:
11561157
chi_squared += (expct - self.move_history[i, j]) ** 2 / expct
11571158

1159+
self.chi_squared = round(chi_squared, 3) # For testing
1160+
11581161
if chi_squared > 3:
11591162
return False
11601163
return True

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for the Second Axelrod strategies."""
22

33
import random
4+
import numpy as np
45

56
import axelrod
67
from .test_player import TestPlayer
@@ -819,3 +820,32 @@ def test_strategy(self):
819820
# doesn't reset. This logic comes before parity streaks or the turn-
820821
# based logic.
821822
self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"recorded_defects": 119})
823+
824+
# Detect random
825+
actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, D),
826+
(D, C), (C, D), (D, C), (C, C), (C, D), (D, D), (D, C),
827+
(C, D), (D, D), (D, C), (C, C), (C, D), (D, C), (C, D),
828+
(D, D), (D, C), (C, D), (D, D), (D, D), (C, D), (D, C),
829+
(C, C)]
830+
# Enter defect mode.
831+
actions += [(D, C)]
832+
self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=10, attrs={"chi_squared": 2.395})
833+
# The history matrix will be [[0, 2], [5, 6], [3, 6], [4, 2]]
834+
835+
# Come back out of defect mode
836+
opponent_actions = [D, C, D, C, D, D, D, C, D, C, C, D, D, C, D, D, C,
837+
C, D, C, D, D, C, D, D, D, D, C, C, C]
838+
opponent_actions += [D] * 16
839+
Rand_Then_Def = axelrod.MockPlayer(actions=opponent_actions)
840+
actions = [(C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, D),
841+
(D, C), (C, D), (D, C), (C, C), (C, D), (D, D), (D, C),
842+
(C, D), (D, D), (D, C), (C, C), (C, D), (D, C), (C, D),
843+
(D, D), (D, C), (C, D), (D, D), (D, D), (C, D), (D, C),
844+
(C, C)]
845+
actions += [(D, C)]
846+
# Enter defect mode.
847+
actions += [(D, D)] * 14
848+
# Mutual defect for a while, then exit Defect mode with two coops
849+
actions += [(C, D)] * 2
850+
self.versus_test(Rand_Then_Def, expected_actions=actions, seed=10, \
851+
attrs={"mode": "Normal", "was_defective": True})

0 commit comments

Comments
 (0)