Skip to content

Commit 4c89284

Browse files
committed
Some attempts to fix.
1 parent cc60038 commit 4c89284

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

axelrod/strategies/titfortat.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from axelrod.player import Player
33
from axelrod.strategy_transformers import TrackHistoryTransformer, FinalTransformer
44
from axelrod.random_ import random_choice
5-
import random
65

76
C, D = Actions.C, Actions.D
87

@@ -95,16 +94,21 @@ def strategy(opponent: Player) -> Action:
9594
return D if D in opponent.history[-2:] else C
9695

9796
class DynamicTwoTitsForTat(Player):
98-
"""A player starts by cooperating and then mimics previous move by
99-
opponent with a dynamic bias based off of the opponents ratio of
100-
cooperations to total moves (so their current probability of
101-
cooperating towards cooporating regardless of the move
102-
(aka: forgiveness)."""
97+
"""
98+
A player starts by cooperating and then punishes its opponent's
99+
defectiions by opponent with a dynamic bias based off of the
100+
opponents ratio of cooperations to total moves (so their current
101+
probability of cooperating towards cooporating regardless of the
102+
move (aka: forgiveness)).
103+
Names:
104+
105+
- Dynamic Two Tits For Tat: Original name by Grant Garrett-Grossman.
106+
"""
103107

104108
name = 'Dynamic Two Tits For Tat'
105109
classifier = {
106110
'memory_depth': 2, # Long memory, memory-2
107-
'stochastic': False,
111+
'stochastic': True,
108112
'makes_use_of': set(),
109113
'long_run_time': False,
110114
'inspects_source': False,
@@ -119,9 +123,8 @@ def strategy(opponent):
119123
# Make sure we cooporate first turn
120124
return C
121125
if D in opponent.history[-2:]:
122-
# Probability of turning the other cheek based off of
123-
# opponent's probability of defection
124-
if random.random() < (opponent.cooperations / len(opponent.history)):
126+
# Probability of cooperating regardless
127+
if random_choice(opponent.cooperations / len(opponent.history)):
125128
return C
126129
else:
127130
return D

axelrod/tests/strategies/test_titfortat.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,36 @@ class TestDynamicTwoTitsForTat(TestPlayer):
120120
player = axelrod.DynamicTwoTitsForTat
121121
expected_classifier = {
122122
'memory_depth': 2,
123-
'stochastic': False,
123+
'stochastic': True,
124124
'makes_use_of': set(),
125125
'inspects_source': False,
126126
'manipulates_source': False,
127127
'manipulates_state': False
128128
}
129129

130130
def test_strategy(self):
131+
# First move is to cooperate
131132
self.first_play_test(C)
132-
self.second_play_test(rCC=C, rCD=D, rDC=C, rDD=D)
133-
134-
# Will cooperate if opponent cooperates/.
135-
actions = [(C, C), (C, C), (C, C), (C, C), (C, C)]
136-
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
137-
# Will defect twice when last turn of opponent was defection.
138-
opponent = axelrod.MockPlayer(actions=[D, C, C, D, C])
139-
actions = [(C, D), (D, C), (D, C), (C, D), (D, C)]
140-
self.versus_test(opponent, expected_actions=actions)
141-
# Test against defector
142-
actions = [(C, D), (D, D), (D, D), (D, D), (D, D)]
143-
self.versus_test(axelrod.Defector(), expected_actions=actions)
133+
# Test that it is stochastic
134+
135+
opponent = axelrod.MockPlayer(actions=[D, C, D, D, C])
136+
actions = [(C, D), (C,C), (C,D), (C,D), (C,C)]
137+
self.versus_test(opponent, expected_actions=actions, seed=2)
138+
139+
opponent = axelrod.MockPlayer(actions=[D, C, D, D, C])
140+
actions = [(C, D), (C,C), (C,D), (C,D), (D,C)]
141+
self.versus_test(opponent, expected_actions=actions, seed=13)
142+
143+
## Will cooperate if opponent cooperates.
144+
#actions = [(C, C), (C, C), (C, C), (C, C), (C, C)]
145+
#self.versus_test(axelrod.Cooperator(), expected_actions=actions)
146+
## Will defect twice when last turn of opponent was defection.
147+
#opponent = axelrod.MockPlayer(actions=[D, C, C, D, C])
148+
#actions = [(C, D), (D, C), (D, C), (C, D), (D, C)]
149+
#self.versus_test(opponent, expected_actions=actions)
150+
## Test against defector
151+
#actions = [(C, D), (D, D), (D, D), (D, D), (D, D)]
152+
#self.versus_test(axelrod.Defector(), expected_actions=actions)
144153

145154
class TestBully(TestPlayer):
146155

docs/tutorials/advanced/classification_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ strategies::
4747
... }
4848
>>> strategies = axl.filtered_strategies(filterset)
4949
>>> len(strategies)
50-
66
50+
67
5151

5252
Or, to find out how many strategies only use 1 turn worth of memory to
5353
make a decision::

0 commit comments

Comments
 (0)