Skip to content

Commit e1fc7e2

Browse files
committed
Add fuctions to compute the state distribution for a match
1 parent e7491d7 commit e1fc7e2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

axelrod/interaction_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
interactions.
1010
"""
1111
import csv
12+
from collections import Counter
1213

1314
from .game import Game
1415
from axelrod import Actions
@@ -87,6 +88,29 @@ def compute_normalised_cooperation(interactions):
8788
return normalised_cooperation
8889

8990

91+
def compute_state_distribution(interactions):
92+
"""
93+
Returns the count of each state for a set of interactions.
94+
"""
95+
if len(interactions) == 0:
96+
return None
97+
return Counter(interactions)
98+
99+
100+
def compute_normalised_state_distribution(interactions):
101+
"""
102+
Returns the normalized count of each state for a set of interactions.
103+
"""
104+
normalized_count = Counter(interactions)
105+
total = sum(normalized_count.values(), 0.0)
106+
# By starting the sum with 0.0 we make sure total is a floating point value,
107+
# avoiding the Python 2 floor division behaviour of / with integer operands (Stack Overflow)
108+
109+
for key in normalized_count:
110+
normalized_count[key] /= total
111+
return normalized_count
112+
113+
90114
def sparkline(actions, c_symbol=u'█', d_symbol=u' '):
91115
return u''.join([
92116
c_symbol if play == 'C' else d_symbol for play in actions])

axelrod/match.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ def normalised_cooperation(self):
159159
"""Returns the count of cooperations by each player per turn"""
160160
return iu.compute_normalised_cooperation(self.result)
161161

162+
def state_distribution(self):
163+
return iu.compute_state_distribution(self.result)
164+
165+
def normalised_state_distribution(self):
166+
return iu.compute_normalised_state_distribution(self.result)
167+
162168
def sparklines(self, c_symbol=u'█', d_symbol=u' '):
163169
return iu.compute_sparklines(self.result, c_symbol, d_symbol)
164170

0 commit comments

Comments
 (0)