Skip to content

Commit

Permalink
Tidying after PR comments.
Browse files Browse the repository at this point in the history
- 'C'/'D' -> C/D
- Adding type docstrings.
  • Loading branch information
drvinceknight committed Feb 22, 2017
1 parent 3ef8478 commit a01eebc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
33 changes: 16 additions & 17 deletions axelrod/interaction_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Functions to calculate results from interactions. Interactions are lists of the
form:
[('C', 'D'), ('D', 'C'),...]
[(C, D), (D, C),...]
This is used by both the Match class and the ResultSet class which analyse
interactions.
Expand Down Expand Up @@ -145,7 +145,7 @@ def compute_state_to_action_distribution(interactions):
Implying that from a state of (C, D) (the first player having played C and
the second playing D) the player in question then played C.
The following counter object, implies that the player in question was in
The following counter object implies that the player in question was in
state (C, D) for a total of 12 times, subsequently cooperating 4 times and
defecting 8 times.
Expand All @@ -167,8 +167,8 @@ def compute_state_to_action_distribution(interactions):
if not interactions:
return None

distributions = [Counter([(state, outcome[j]) for state, outcome in zip(interactions,
interactions[1:])])
distributions = [Counter([(state, outcome[j])
for state, outcome in zip(interactions, interactions[1:])])
for j in range(2)]
return distributions

Expand All @@ -180,13 +180,12 @@ def compute_normalised_state_to_action_distribution(interactions):
((C, D), C)
Implying that from a state of (C, D) (the first player having played C and
implying that from a state of (C, D) (the first player having played C and
the second playing D) the player in question then played C.
The following counter object, implies that the player in question was only
ever in
state (C, D), subsequently cooperating 1/3 of the time and
defecting 2/3 times.
ever in state (C, D), subsequently cooperating 1/3 of the time and defecting
2/3 times.
Counter({((C, D), C): 0.333333, ((C, D), D): 0.66666667})
Expand All @@ -197,11 +196,11 @@ def compute_normalised_state_to_action_distribution(interactions):
this file.
Returns
----------
-------
normalised_state_to_C_distributions : List of Counter Object
List of Counter objects where the keys are the states and actions and
the values the normalized counts.. The
first/second Counter corresponds to the first/second player.
the values the normalized counts. The first/second Counter corresponds
to the first/second player.
"""
if not interactions:
return None
Expand All @@ -210,22 +209,22 @@ def compute_normalised_state_to_action_distribution(interactions):
normalized_distribution = []
for player in range(2):
counter = {}
for state in [('C', 'C'), ('C', 'D'), ('D', 'C'), ('D', 'D')]:
C_count = distribution[player].get((state, 'C'), 0)
D_count = distribution[player].get((state, 'D'), 0)
for state in [(C, C), (C, D), (D, C), (D, D)]:
C_count = distribution[player].get((state, C), 0)
D_count = distribution[player].get((state, D), 0)
total = C_count + D_count
if total > 0:
if C_count > 0:
counter[(state, 'C')] = C_count / (C_count + D_count)
counter[(state, C)] = C_count / (C_count + D_count)
if D_count > 0:
counter[(state, 'D')] = D_count / (C_count + D_count)
counter[(state, D)] = D_count / (C_count + D_count)
normalized_distribution.append(Counter(counter))
return normalized_distribution


def sparkline(actions, c_symbol='█', d_symbol=' '):
return ''.join([
c_symbol if play == 'C' else d_symbol for play in actions])
c_symbol if play == C else d_symbol for play in actions])


def compute_sparklines(interactions, c_symbol='█', d_symbol=' '):
Expand Down
12 changes: 7 additions & 5 deletions axelrod/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ def _build_ranking(self):
@update_progress_bar
def _build_normalised_state_distribution(self):
"""
Returns
----------
Returns:
--------
norm : list
Normalised state distribution. A list of lists of counter objects:
Expand All @@ -361,10 +362,11 @@ def _build_normalised_state_distribution(self):
@update_progress_bar
def _build_normalised_state_to_action_distribution(self):
"""
Returns
----------
Returns:
--------
norm : list
Normalised state distribution. A list of lists of counter objects:
A list of lists of counter objects.
Dictionary where the keys are the states and the values are a
normalized counts of the number of times that state goes to a given
Expand Down

0 comments on commit a01eebc

Please sign in to comment.