Skip to content

Commit bfb3cfa

Browse files
committed
Add short run time strategy list.
Closes #771 Have included a very basic test and have also done some minor tidying up.
1 parent 15ddd34 commit bfb3cfa

File tree

2 files changed

+132
-121
lines changed

2 files changed

+132
-121
lines changed

axelrod/strategies/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
long_run_time_strategies = [s for s in all_strategies if
4848
s().classifier['long_run_time']]
49+
short_run_time_strategies = [s for s in strategies
50+
if s not in long_run_time_strategies]
4951
cheating_strategies = [s for s in all_strategies if not obey_axelrod(s())]
5052

5153
ordinary_strategies = strategies # This is a legacy and will be removed
Lines changed: 130 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for the classification"""
22

33
import unittest
4-
import axelrod
4+
import axelrod as axl
55

66

77
class TestClassification(unittest.TestCase):
@@ -15,118 +15,118 @@ def test_known_classifiers(self):
1515
'manipulates_source',
1616
'manipulates_state']
1717

18-
for s in axelrod.all_strategies:
18+
for s in axl.all_strategies:
1919
s = s()
2020
self.assertTrue(
2121
None not in [s.classifier[key] for key in known_keys])
2222

2323
def test_multiple_instances(self):
2424
"""Certain instances of classes of strategies will have different
2525
classifiers based on the initialisation variables"""
26-
P1 = axelrod.MemoryOnePlayer((.5, .5, .5, .5))
27-
P2 = axelrod.MemoryOnePlayer((1, 0, 0, 1))
26+
P1 = axl.MemoryOnePlayer((.5, .5, .5, .5))
27+
P2 = axl.MemoryOnePlayer((1, 0, 0, 1))
2828
self.assertNotEqual(P1.classifier, P2.classifier)
2929

30-
P1 = axelrod.Joss()
31-
P2 = axelrod.Joss(0)
30+
P1 = axl.Joss()
31+
P2 = axl.Joss(0)
3232
self.assertNotEqual(P1.classifier, P2.classifier)
3333

34-
P1 = axelrod.GTFT(1)
35-
P2 = axelrod.GTFT(.5)
34+
P1 = axl.GTFT(1)
35+
P2 = axl.GTFT(.5)
3636
self.assertNotEqual(P1.classifier, P2.classifier)
3737

38-
P1 = axelrod.StochasticWSLS()
39-
P2 = axelrod.StochasticWSLS(0)
38+
P1 = axl.StochasticWSLS()
39+
P2 = axl.StochasticWSLS(0)
4040
self.assertNotEqual(P1.classifier, P2.classifier)
4141

42-
P1 = axelrod.GoByMajority(5)
43-
P2 = axelrod.StochasticWSLS(.1)
42+
P1 = axl.GoByMajority(5)
43+
P2 = axl.StochasticWSLS(.1)
4444
self.assertNotEqual(P1.classifier, P2.classifier)
4545

4646
def test_manipulation_of_classifier(self):
4747
"""Test that can change the classifier of an instance without changing
4848
the classifier of the class"""
49-
player = axelrod.Cooperator()
49+
player = axl.Cooperator()
5050
player.classifier['memory_depth'] += 1
51-
self.assertNotEqual(player.classifier, axelrod.Cooperator.classifier)
52-
player = axelrod.Defector()
51+
self.assertNotEqual(player.classifier, axl.Cooperator.classifier)
52+
player = axl.Defector()
5353
player.classifier['memory_depth'] += 1
54-
self.assertNotEqual(player.classifier, axelrod.Defector.classifier)
54+
self.assertNotEqual(player.classifier, axl.Defector.classifier)
5555

5656
def test_obey_axelrod(self):
57-
"""A test that verifies if the obey_axelrod function works correctly"""
58-
known_cheaters = [axelrod.Darwin,
59-
axelrod.Geller,
60-
axelrod.GellerCooperator,
61-
axelrod.GellerDefector,
62-
axelrod.MindBender,
63-
axelrod.MindController,
64-
axelrod.MindWarper,
65-
axelrod.MindReader]
66-
67-
known_basic = [axelrod.Alternator,
68-
axelrod.AntiTitForTat,
69-
axelrod.Bully,
70-
axelrod.Cooperator,
71-
axelrod.Defector,
72-
axelrod.GoByMajority,
73-
axelrod.SuspiciousTitForTat,
74-
axelrod.TitForTat,
75-
axelrod.WinStayLoseShift]
76-
77-
known_ordinary = [axelrod.AverageCopier,
78-
axelrod.ForgivingTitForTat,
79-
axelrod.GoByMajority20,
80-
axelrod.GTFT,
81-
axelrod.Grudger,
82-
axelrod.Inverse,
83-
axelrod.Random]
57+
"""A test that verifies if the obey_axl function works correctly"""
58+
known_cheaters = [axl.Darwin,
59+
axl.Geller,
60+
axl.GellerCooperator,
61+
axl.GellerDefector,
62+
axl.MindBender,
63+
axl.MindController,
64+
axl.MindWarper,
65+
axl.MindReader]
66+
67+
known_basic = [axl.Alternator,
68+
axl.AntiTitForTat,
69+
axl.Bully,
70+
axl.Cooperator,
71+
axl.Defector,
72+
axl.GoByMajority,
73+
axl.SuspiciousTitForTat,
74+
axl.TitForTat,
75+
axl.WinStayLoseShift]
76+
77+
known_ordinary = [axl.AverageCopier,
78+
axl.ForgivingTitForTat,
79+
axl.GoByMajority20,
80+
axl.GTFT,
81+
axl.Grudger,
82+
axl.Inverse,
83+
axl.Random]
8484

8585
for strategy in known_cheaters:
86-
self.assertFalse(axelrod.obey_axelrod(strategy()), msg=strategy)
86+
self.assertFalse(axl.obey_axelrod(strategy()), msg=strategy)
8787

8888
for strategy in known_basic:
89-
self.assertTrue(axelrod.obey_axelrod(strategy()), msg=strategy)
89+
self.assertTrue(axl.obey_axelrod(strategy()), msg=strategy)
9090

9191
for strategy in known_ordinary:
92-
self.assertTrue(axelrod.obey_axelrod(strategy()), msg=strategy)
92+
self.assertTrue(axl.obey_axelrod(strategy()), msg=strategy)
9393

9494
def test_is_basic(self):
9595
"""A test that verifies if the is_basic function works correctly"""
96-
known_cheaters = [axelrod.Darwin,
97-
axelrod.Geller,
98-
axelrod.GellerCooperator,
99-
axelrod.GellerDefector,
100-
axelrod.MindBender,
101-
axelrod.MindController,
102-
axelrod.MindWarper,
103-
axelrod.MindReader]
104-
105-
known_basic = [axelrod.Alternator,
106-
axelrod.AntiTitForTat,
107-
axelrod.Bully,
108-
axelrod.Cooperator,
109-
axelrod.Defector,
110-
axelrod.SuspiciousTitForTat,
111-
axelrod.TitForTat,
112-
axelrod.WinStayLoseShift]
113-
114-
known_ordinary = [axelrod.AverageCopier,
115-
axelrod.ForgivingTitForTat,
116-
axelrod.GoByMajority20,
117-
axelrod.GTFT,
118-
axelrod.Grudger,
119-
axelrod.Inverse,
120-
axelrod.Random]
96+
known_cheaters = [axl.Darwin,
97+
axl.Geller,
98+
axl.GellerCooperator,
99+
axl.GellerDefector,
100+
axl.MindBender,
101+
axl.MindController,
102+
axl.MindWarper,
103+
axl.MindReader]
104+
105+
known_basic = [axl.Alternator,
106+
axl.AntiTitForTat,
107+
axl.Bully,
108+
axl.Cooperator,
109+
axl.Defector,
110+
axl.SuspiciousTitForTat,
111+
axl.TitForTat,
112+
axl.WinStayLoseShift]
113+
114+
known_ordinary = [axl.AverageCopier,
115+
axl.ForgivingTitForTat,
116+
axl.GoByMajority20,
117+
axl.GTFT,
118+
axl.Grudger,
119+
axl.Inverse,
120+
axl.Random]
121121

122122
for strategy in known_cheaters:
123-
self.assertFalse(axelrod.is_basic(strategy()), msg=strategy)
123+
self.assertFalse(axl.is_basic(strategy()), msg=strategy)
124124

125125
for strategy in known_basic:
126-
self.assertTrue(axelrod.is_basic(strategy()), msg=strategy)
126+
self.assertTrue(axl.is_basic(strategy()), msg=strategy)
127127

128128
for strategy in known_ordinary:
129-
self.assertFalse(axelrod.is_basic(strategy()), msg=strategy)
129+
self.assertFalse(axl.is_basic(strategy()), msg=strategy)
130130

131131

132132
def str_reps(xs):
@@ -144,71 +144,80 @@ def test_strategy_list(self):
144144
"strategies",
145145
"ordinary_strategies",
146146
"cheating_strategies"]:
147-
self.assertTrue(hasattr(axelrod, strategy_list))
147+
self.assertTrue(hasattr(axl, strategy_list))
148148

149149
def test_lists_not_empty(self):
150-
for strategy_list in [axelrod.all_strategies,
151-
axelrod.demo_strategies,
152-
axelrod.basic_strategies,
153-
axelrod.long_run_time_strategies,
154-
axelrod.strategies,
155-
axelrod.ordinary_strategies,
156-
axelrod.cheating_strategies]:
150+
for strategy_list in [axl.all_strategies,
151+
axl.demo_strategies,
152+
axl.basic_strategies,
153+
axl.long_run_time_strategies,
154+
axl.strategies,
155+
axl.ordinary_strategies,
156+
axl.cheating_strategies]:
157157
self.assertTrue(len(strategy_list) > 0)
158158

159159
def test_inclusion_of_strategy_lists(self):
160-
all_strategies_set = set(axelrod.all_strategies)
161-
for strategy_list in [axelrod.demo_strategies,
162-
axelrod.basic_strategies,
163-
axelrod.long_run_time_strategies,
164-
axelrod.strategies,
165-
axelrod.ordinary_strategies,
166-
axelrod.cheating_strategies]:
160+
all_strategies_set = set(axl.all_strategies)
161+
for strategy_list in [axl.demo_strategies,
162+
axl.basic_strategies,
163+
axl.long_run_time_strategies,
164+
axl.strategies,
165+
axl.ordinary_strategies,
166+
axl.cheating_strategies]:
167167
self.assertTrue(str_reps(strategy_list).issubset(
168168
str_reps(all_strategies_set)))
169169

170-
strategies_set = set(axelrod.strategies)
171-
for strategy_list in [axelrod.demo_strategies,
172-
axelrod.basic_strategies,
173-
axelrod.long_run_time_strategies]:
170+
strategies_set = set(axl.strategies)
171+
for strategy_list in [axl.demo_strategies,
172+
axl.basic_strategies,
173+
axl.long_run_time_strategies]:
174174
self.assertTrue(str_reps(strategy_list).issubset(
175175
str_reps(strategies_set)))
176176

177177
def test_long_run_strategies(self):
178-
long_run_time_strategies = [axelrod.MetaMajority,
179-
axelrod.MetaMinority,
180-
axelrod.MetaWinner,
181-
axelrod.MetaWinnerEnsemble,
182-
axelrod.MetaMajorityFiniteMemory,
183-
axelrod.MetaWinnerFiniteMemory,
184-
axelrod.MetaMajorityLongMemory,
185-
axelrod.MetaWinnerLongMemory,
186-
axelrod.MetaMixer,
187-
axelrod.MWEFiniteMemory,
188-
axelrod.MetaWinnerDeterministic,
189-
axelrod.MWELongMemory,
190-
axelrod.MWEStochastic,
191-
axelrod.MWEDeterministic,
192-
axelrod.MetaWinnerStochastic
178+
long_run_time_strategies = [axl.MetaMajority,
179+
axl.MetaMinority,
180+
axl.MetaWinner,
181+
axl.MetaWinnerEnsemble,
182+
axl.MetaMajorityFiniteMemory,
183+
axl.MetaWinnerFiniteMemory,
184+
axl.MetaMajorityLongMemory,
185+
axl.MetaWinnerLongMemory,
186+
axl.MetaMixer,
187+
axl.MWEFiniteMemory,
188+
axl.MetaWinnerDeterministic,
189+
axl.MWELongMemory,
190+
axl.MWEStochastic,
191+
axl.MWEDeterministic,
192+
axl.MetaWinnerStochastic
193193
]
194194

195195
self.assertEqual(str_reps(long_run_time_strategies),
196-
str_reps(axelrod.long_run_time_strategies))
196+
str_reps(axl.long_run_time_strategies))
197+
self.assertTrue(all(s().classifier['long_run_time']
198+
for s in axl.long_run_time_strategies))
199+
200+
def test_short_run_strategies(self):
201+
short_run_time_strategies = [s for s in axl.strategies
202+
if s not in axl.long_run_time_strategies]
203+
204+
self.assertEqual(str_reps(short_run_time_strategies),
205+
str_reps(axl.short_run_time_strategies))
206+
self.assertFalse(any(s().classifier['long_run_time']
207+
for s in axl.short_run_time_strategies))
197208

198209
def test_meta_inclusion(self):
199-
self.assertTrue(str(axelrod.MetaMajority()) in
200-
str_reps(axelrod.strategies))
210+
self.assertTrue(str(axl.MetaMajority()) in str_reps(axl.strategies))
201211

202-
self.assertTrue(str(axelrod.MetaHunter()) in
203-
str_reps(axelrod.strategies))
204-
self.assertFalse(str(axelrod.MetaHunter()) in
205-
str_reps(axelrod.long_run_time_strategies))
212+
self.assertTrue(str(axl.MetaHunter()) in str_reps(axl.strategies))
213+
self.assertFalse(str(axl.MetaHunter()) in
214+
str_reps(axl.long_run_time_strategies))
206215

207216
def test_demo_strategies(self):
208-
demo_strategies = [axelrod.Cooperator,
209-
axelrod.Defector,
210-
axelrod.TitForTat,
211-
axelrod.Grudger,
212-
axelrod.Random]
217+
demo_strategies = [axl.Cooperator,
218+
axl.Defector,
219+
axl.TitForTat,
220+
axl.Grudger,
221+
axl.Random]
213222
self.assertTrue(str_reps(demo_strategies),
214-
str_reps(axelrod.demo_strategies))
223+
str_reps(axl.demo_strategies))

0 commit comments

Comments
 (0)