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

Gladstein strategy #1110

Merged
merged 10 commits into from
Aug 5, 2017
Merged

Gladstein strategy #1110

merged 10 commits into from
Aug 5, 2017

Conversation

dmanc
Copy link
Contributor

@dmanc dmanc commented Aug 4, 2017

This is the implementation of the K76R Fortran strategy mentioned in #1102. The strategy was named Gladstein after the author's last name.


This strategy is a TFT variant that defects on the first round in order to
test the opponent's response. If the opponent ever defects, the strategy
'apologies' by cooperating and then plays TFT for the rest of the game.
Copy link
Member

Choose a reason for hiding this comment

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

typo - should be 'apologizes'

"""
Submitted to Axelrod's second tournament by David Gladstein.

This strategy is also known as Tester and is based of the reverse
Copy link
Member

Choose a reason for hiding this comment

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

typo - should be 'is based on'


Names:

- Gladstein: [Axelrod1980b]_
Copy link
Member

Choose a reason for hiding this comment

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

Could you add the name Tester here, please?

super().__init__()
# This strategy assumes the opponent is a patsy
self.patsy = True
self.cooperation_ratio = 0
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to maintain this ratio between calls to the play method. The original fortran strategy had to do that, but we pass the entire history to the player at each turn, so it can always be calculated within the play method.

self.patsy = False
return C
# Cooperate as long as the cooperation ratio is below 0.5
self.cooperation_ratio = self.cooperations / len(self.history)
Copy link
Member

@meatballs meatballs Aug 4, 2017

Choose a reason for hiding this comment

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

As above, this can be calculated directly from the history without the need for an instance variable. For example:

from collections import Counter

cooperation_ratio = Counter(self.history)[C] / len(self.history)

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I was forgetting we have that count already built into the Player class, so it becomes even simpler:

cooperation_ratio = self.cooperations / len(self.history)

Copy link
Member

Choose a reason for hiding this comment

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

Using self.cooperations is better, recomputing each time is O(n^2).

actions = [(D, C), (C, C), (D, C), (C, D), (C, D)]
self.versus_test(opponent, expected_actions=actions,
attrs={'patsy': False,
'cooperation_ratio': 0.3333333333333333})
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 this as 'cooperation_ratio': 1/3.


name = "Gladstein"
classifier = {
'memory_depth': 1,
Copy link
Member

Choose a reason for hiding this comment

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

This strategy has infinite memory depth : float('inf') as it keeps track of it's cooperations.

(You'll need to adjust the corresponding doctest.)

@dmanc
Copy link
Contributor Author

dmanc commented Aug 5, 2017

I added the requested changes. I had to change a test in test_meta.py for TestMetaMajorityLongMemory since it was giving me errors after changing the memory depth of the strategy.

Copy link
Member

@meatballs meatballs left a comment

Choose a reason for hiding this comment

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

Could you also amend the overview of strategies doc to show that K76R has been implemented by this new class?

@drvinceknight
Copy link
Member

Thanks for this @dmanc!

@drvinceknight drvinceknight merged commit bb951aa into Axelrod-Python:master Aug 5, 2017
@dmanc dmanc deleted the Gladstein branch August 7, 2017 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants