File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 9
9
interactions.
10
10
"""
11
11
import csv
12
+ from collections import Counter
12
13
13
14
from .game import Game
14
15
from axelrod import Actions
@@ -87,6 +88,29 @@ def compute_normalised_cooperation(interactions):
87
88
return normalised_cooperation
88
89
89
90
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
+
90
114
def sparkline (actions , c_symbol = u'█' , d_symbol = u' ' ):
91
115
return u'' .join ([
92
116
c_symbol if play == 'C' else d_symbol for play in actions ])
Original file line number Diff line number Diff line change @@ -159,6 +159,12 @@ def normalised_cooperation(self):
159
159
"""Returns the count of cooperations by each player per turn"""
160
160
return iu .compute_normalised_cooperation (self .result )
161
161
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
+
162
168
def sparklines (self , c_symbol = u'█' , d_symbol = u' ' ):
163
169
return iu .compute_sparklines (self .result , c_symbol , d_symbol )
164
170
You can’t perform that action at this time.
0 commit comments