Skip to content

Commit bf9e9b9

Browse files
committed
Have added actual Worse and worse strategy now.
1 parent 008656c commit bf9e9b9

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
6060
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
6161
OmegaTFT, Gradual, ContriteTitForTat, SlowTitForTwoTats, AdaptiveTitForTat)
62-
from .worse_and_worse import WorseAndWorse
62+
from .worse_and_worse import (WorseAndWorse, WorseAndWorseRandom)
6363

6464
# Note: Meta* strategies are handled in .__init__.py
6565

@@ -196,6 +196,7 @@
196196
WinShiftLoseStay,
197197
WinStayLoseShift,
198198
WorseAndWorse,
199+
WorseAndWorseRandom,
199200
ZDExtort2,
200201
ZDExtort2v2,
201202
ZDExtort4,

axelrod/strategies/worse_and_worse.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import random
2+
13
from axelrod import Actions, Player, random_choice
24

35
C, D = Actions.C, Actions.D
46

57
class WorseAndWorse (Player):
68
"""
7-
Defects with probability of 'current turn / total no. of turns'. Therefore
9+
Defects with probability of 'current turn / 1000'. Therefore
810
it is more and more likely to defect as the round goes on.
911
1012
Names:
@@ -13,6 +15,31 @@ class WorseAndWorse (Player):
1315
"""
1416

1517
name = 'Worse and worse'
18+
classifier = {
19+
'memory_depth': float('inf'),
20+
'stochastic': True,
21+
'makes_use_of': set(),
22+
'long_run_time': False,
23+
'inspects_source': False,
24+
'manipulates_source': False,
25+
'manipulates_state': False
26+
}
27+
28+
def strategy(self, opponent):
29+
current_round = len(self.history) + 1
30+
if random.randint(0, 1000) < current_round:
31+
return D
32+
return C
33+
34+
35+
class WorseAndWorseRandom (Player):
36+
"""
37+
This strategy is based of 'Worse And Worse' but will defect with probability
38+
of 'current turn / total no. of turns'.
39+
40+
"""
41+
42+
name = 'Worse and worse random'
1643
classifier = {
1744
'memory_depth': float('inf'),
1845
'stochastic': True,

axelrod/tests/unit/test_worse_and_worse.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
"""Test for the Worse and Worse strategy."""
1+
"""Test for the Worse and Worse strategies."""
22

3-
import sys
43
import axelrod
54

65
from .test_player import TestPlayer
76

87
C, D = axelrod.Actions.C, axelrod.Actions.D
98

10-
119
class TestWorseAndWorse(TestPlayer):
1210

1311
name = "Worse and worse"
1412
player = axelrod.WorseAndWorse
1513
expected_classifier = {
1614
'memory_depth': float('inf'),
1715
'stochastic': True,
18-
'makes_use_of': set(['length']),
16+
'makes_use_of': set(),
1917
'long_run_time': False,
2018
'inspects_source': False,
2119
'manipulates_source': False,
@@ -26,10 +24,51 @@ def test_strategy(self):
2624
"""
2725
Test that the strategy gives expected behaviour
2826
"""
27+
2928
axelrod.seed(1)
3029
opponent = axelrod.Cooperator()
3130
player = axelrod.WorseAndWorse()
3231
match = axelrod.Match((opponent, player), turns=5)
32+
self.assertEqual(match.play(), [('C', 'C'),
33+
('C', 'C'),
34+
('C', 'C'),
35+
('C', 'C'),
36+
('C', 'C')])
37+
38+
# Test that behaviour does not depend on opponent
39+
opponent = axelrod.Defector()
40+
player = axelrod.WorseAndWorse()
41+
axelrod.seed(1)
42+
match = axelrod.Match((opponent, player), turns=5)
43+
self.assertEqual(match.play(), [('D', 'C'),
44+
('D', 'C'),
45+
('D', 'C'),
46+
('D', 'C'),
47+
('D', 'C')])
48+
49+
50+
class TestWorseAndWorseRandom(TestPlayer):
51+
52+
name = "Worse and worse random"
53+
player = axelrod.WorseAndWorseRandom
54+
expected_classifier = {
55+
'memory_depth': float('inf'),
56+
'stochastic': True,
57+
'makes_use_of': set(['length']),
58+
'long_run_time': False,
59+
'inspects_source': False,
60+
'manipulates_source': False,
61+
'manipulates_state': False
62+
}
63+
64+
def test_strategy(self):
65+
"""
66+
Test that the strategy gives expected behaviour
67+
"""
68+
axelrod.seed(1)
69+
opponent = axelrod.Cooperator()
70+
player = axelrod.WorseAndWorseRandom()
71+
match = axelrod.Match((opponent, player), turns=5)
3372
self.assertEqual(match.play(), [('C', 'C'),
3473
('C', 'D'),
3574
('C', 'D'),
@@ -38,7 +77,7 @@ def test_strategy(self):
3877

3978
# Test that behaviour does not depend on opponent
4079
opponent = axelrod.Defector()
41-
player = axelrod.WorseAndWorse()
80+
player = axelrod.WorseAndWorseRandom()
4281
axelrod.seed(1)
4382
match = axelrod.Match((opponent, player), turns=5)
4483
self.assertEqual(match.play(), [('D', 'C'),

0 commit comments

Comments
 (0)