Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track CC, CD, DC, DD in Player class #752

Merged
merged 6 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions axelrod/player.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from functools import wraps
import random
import copy
Expand Down Expand Up @@ -52,6 +53,12 @@ def update_history(player, move):
player.defections += 1


def update_play_counts(player, move, reply):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the glossary http://axelrod.readthedocs.io/en/latest/reference/glossary.html#a-play we have defined a play as "a single player choosing an action."

In the results set and the interaction utils we have called these counters "state distributions". Could we call them the same thing here (for consistency).

  • update_state_distribution
  • player_state_distribution

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.
Maybe, because the class is already called Player, the property could be called just state_distribution. What do you think would be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup: that's better than my suggestion :) 👍

"""Updates play_counts following play. """
last_round = (move, reply)
player.play_counts[last_round] += 1


def init_args(func):
"""Decorator to simplify the handling of init_args. Use whenever overriding
Player.__init__ in subclasses of Player that require arguments as follows:
Expand Down Expand Up @@ -97,6 +104,7 @@ def __init__(self):
self.classifier[dimension] = self.default_classifier[dimension]
self.cooperations = 0
self.defections = 0
self.play_counts = defaultdict(int)
self.init_args = ()
self.set_match_attributes()

Expand Down Expand Up @@ -141,6 +149,8 @@ def play(self, opponent, noise=0):
s1, s2 = self._add_noise(noise, s1, s2)
update_history(self, s1)
update_history(opponent, s2)
update_play_counts(self, s1, s2)
update_play_counts(opponent, s2, s1)

def clone(self):
"""Clones the player without history, reapplying configuration
Expand All @@ -164,3 +174,4 @@ def reset(self):
self.history = []
self.cooperations = 0
self.defections = 0
self.play_counts = defaultdict(int)
9 changes: 9 additions & 0 deletions axelrod/tests/unit/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def test_play(self):
self.assertEqual(p1.defections, 0)
self.assertEqual(p2.cooperations, 0)
self.assertEqual(p2.defections, 1)
# Test play counts
self.assertEqual(p1.play_counts, {(C, D): 1})
self.assertEqual(p2.play_counts, {(D, C): 1})

p1.play(p2)
self.assertEqual(p1.history[-1], C)
self.assertEqual(p2.history[-1], D)
Expand All @@ -60,6 +64,9 @@ def test_play(self):
self.assertEqual(p1.defections, 0)
self.assertEqual(p2.cooperations, 0)
self.assertEqual(p2.defections, 2)
# Test play counts
self.assertEqual(p1.play_counts, {(C, D): 2})
self.assertEqual(p2.play_counts, {(D, C): 2})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a couple more tests leading to a 'fuller' dictionary please.


def test_noisy_play(self):
random.seed(1)
Expand Down Expand Up @@ -172,6 +179,7 @@ def test_reset(self):
self.assertEqual(p.history, [])
self.assertEqual(self.player().cooperations, 0)
self.assertEqual(self.player().defections, 0)
self.assertEqual(self.player().play_counts, {})

def test_clone(self):
# Make sure that self.init_args has the right number of arguments
Expand All @@ -190,6 +198,7 @@ def test_clone(self):
self.assertEqual(len(p2.history), 0)
self.assertEqual(p2.cooperations, 0)
self.assertEqual(p2.defections, 0)
self.assertEqual(p2.play_counts, {})
self.assertEqual(p2.classifier, p1.classifier)
self.assertEqual(p2.match_attributes, p1.match_attributes)

Expand Down