Skip to content

Commit abec603

Browse files
committed
Merge pull request #292 from marcharper/display_names
Display names
2 parents 680bf71 + b016906 commit abec603

File tree

8 files changed

+64
-9
lines changed

8 files changed

+64
-9
lines changed

axelrod/strategies/gobymajority.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ def strategy(self, opponent):
4141
def __repr__(self):
4242
"""The string method for the strategy."""
4343
memory = self.memory_depth
44-
return 'Go By Majority' + (memory > 0) * (":%i" % memory)
44+
name = 'Go By Majority' + (memory > 0) * (": %i" % memory)
45+
if self.soft:
46+
name = "Soft " + name
47+
else:
48+
name = "Hard " + name
49+
return name
4550

4651

4752
class GoByMajority40(GoByMajority):

axelrod/strategies/memoryone.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,19 @@ def strategy(self, opponent):
6565
class GTFT(MemoryOnePlayer):
6666
"""Generous Tit-For-Tat Strategy."""
6767

68-
name = 'Generous Tit-For-Tat'
68+
name = 'GTFT'
6969

7070
def __init__(self, p=None):
7171
(R, P, S, T) = Game().RPST()
7272
if not p:
7373
p = min(1 - float(T - R) / (R - S), float(R - P) / (T - P))
74+
self.p = p
7475
four_vector = [1, p, 1, p]
7576
super(self.__class__, self).__init__(four_vector)
7677

78+
def __repr__(self):
79+
return "%s: %s" % (self.name, round(self.p, 2))
80+
7781

7882
class StochasticCooperator(MemoryOnePlayer):
7983
"""Stochastic Cooperator, http://www.nature.com/ncomms/2013/130801/ncomms3193/full/ncomms3193.html."""
@@ -171,8 +175,12 @@ class Joss(MemoryOnePlayer):
171175

172176
def __init__(self, p=0.9):
173177
four_vector = (p, 0, p, 0)
178+
self.p = p
174179
super(self.__class__, self).__init__(four_vector)
175180

181+
def __repr__(self):
182+
return "%s: %s" % (self.name, round(self.p, 2))
183+
176184

177185
class SoftJoss(MemoryOnePlayer):
178186
"""
@@ -185,3 +193,7 @@ class SoftJoss(MemoryOnePlayer):
185193
def __init__(self, q=0.9):
186194
four_vector = (1., 1 - q, 1, 1 - q)
187195
super(self.__class__, self).__init__(four_vector)
196+
self.q = q
197+
198+
def __repr__(self):
199+
return "%s: %s" % (self.name, round(self.q, 2))

axelrod/strategies/rand.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ def strategy(self, opponent):
1717
if r > self.p:
1818
return 'D'
1919
return 'C'
20+
21+
def __repr__(self):
22+
return "%s: %s" % (self.name, round(self.p, 2))

axelrod/tests/integration/test_tournament.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def setUpClass(cls):
2020
cls.expected_outcome = [
2121
('Cooperator', [1800, 1800, 1800, 1800, 1800]),
2222
('Defector', [1612, 1612, 1612, 1612, 1612]),
23-
('Go By Majority', [1999, 1999, 1999, 1999, 1999]),
2423
('Grudger', [1999, 1999, 1999, 1999, 1999]),
24+
('Soft Go By Majority', [1999, 1999, 1999, 1999, 1999]),
2525
('Tit For Tat', [1999, 1999, 1999, 1999, 1999])]
2626
cls.expected_outcome.sort()
2727

axelrod/tests/unit/test_gobymajority.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class TestGoByMajority(TestPlayer):
1111

12-
name = "Go By Majority"
12+
name = "Soft Go By Majority"
1313
player = axelrod.GoByMajority
1414
stochastic = False
1515

@@ -24,11 +24,19 @@ def test_strategy(self):
2424
self.responses_test([C, D, D, D], [D, D, C, C], [C])
2525
self.responses_test([C, C, D, D, C], [D, D, C, C, D], [D])
2626

27+
def test_repr(self):
28+
player = self.player(soft=True)
29+
name = str(player)
30+
self.assertEqual(name, "Soft Go By Majority")
31+
player = self.player(soft=False)
32+
name = str(player)
33+
self.assertEqual(name, "Hard Go By Majority")
34+
2735
def factory_TestGoByRecentMajority(L):
2836

2937
class TestGoByRecentMajority(TestPlayer):
3038

31-
name = "Go By Majority:%i" % L
39+
name = "Soft Go By Majority: %i" % L
3240
player = getattr(axelrod, 'GoByMajority%i' % L)
3341
stochastic = False
3442

axelrod/tests/unit/test_memoryone.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_effect_of_strategy(self):
2323

2424
class TestGTFT(TestPlayer):
2525

26-
name = "Generous Tit-For-Tat"
26+
name = "GTFT: 0.33"
2727
player = axelrod.GTFT
2828
stochastic = True
2929

@@ -146,7 +146,7 @@ def test_strategy(self):
146146

147147
class TestJoss(TestPlayer):
148148

149-
name = "Joss"
149+
name = "Joss: 0.9"
150150
player = axelrod.Joss
151151
stochastic = True
152152

@@ -161,7 +161,7 @@ def test_strategy(self):
161161

162162
class TestSoftJoss(TestPlayer):
163163

164-
name = "Soft Joss"
164+
name = "Soft Joss: 0.9"
165165
player = axelrod.SoftJoss
166166
stochastic = True
167167

axelrod/tests/unit/test_rand.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TestRandom(TestPlayer):
1010

11-
name = "Random"
11+
name = "Random: 0.5"
1212
player = axelrod.Random
1313
stochastic = True
1414

docs/contributing.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ Here is the :code:`reset` method which takes care of resetting this in between r
114114
self.grudged = False
115115
self.grudge_memory = 0
116116

117+
118+
You can also modify the name of the strategy with the `__repr__` method, which is invoked
119+
when `str` is applied to a player instance. For example, the player `Random` takes a
120+
parameter `p` for how often it cooperates, and the `__repr__` method adds the value
121+
of this parameter to the name::
122+
123+
def __repr__(self):
124+
return "%s: %s" % (self.name, round(self.p, 2))
125+
126+
Now we have separate names for different instantiations::
127+
128+
import axelrod
129+
player1 = axelrod.Random(p=0.5)
130+
player2 = axelrod.Random(p=0.1)
131+
print(str(player1))
132+
print(str(player2))
133+
134+
This produces the following output::
135+
136+
'Random: 0.5'
137+
'Random: 0.1'
138+
139+
This helps distinguish players in tournaments that have multiple instances of the
140+
same strategy. If you modify the `__repr__` method of player, be sure to add an
141+
appropriate test.
142+
143+
117144
Adding the strategy to the library
118145
''''''''''''''''''''''''''''''''''
119146

0 commit comments

Comments
 (0)