-
Notifications
You must be signed in to change notification settings - Fork 282
Implement an eq method for players #975
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
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9875d21
Implement an eq method for players
drvinceknight 819b618
Add missing type annotations for qlearner.
drvinceknight 77b29cc
Remove unneeded method overwrite.
drvinceknight caf2783
Fix spacing.
drvinceknight 308741e
Add inline comments and docstring.
drvinceknight c5e95d6
Ensure types of generator/cycle stay the same.
drvinceknight eb612f5
Add eq for very strange edge case.
drvinceknight 239a511
Fix typo.
drvinceknight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ Contents: | |
| parallel_processing.rst | ||
| using_the_cache.rst | ||
| setting_a_seed.rst | ||
| player_equality.rst | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Player equality | ||
| =============== | ||
|
|
||
| It is possible to test for player equality using :code:`==`:: | ||
|
|
||
| >>> import axelrod as axl | ||
| >>> p1, p2, p3 = axl.Alternator(), axl.Alternator(), axl.TitForTat() | ||
| >>> p1 == p2 | ||
| True | ||
| >>> p1 == p3 | ||
| False | ||
|
|
||
| Note that this checks all the attributes of an instance:: | ||
|
|
||
| >>> p1.name = "John Nash" | ||
| >>> p1 == p2 | ||
| False | ||
|
|
||
| This however does not check if the players will behave in the same way. For | ||
| example here are two equivalent players:: | ||
|
|
||
| >>> p1 = axl.Alternator() | ||
| >>> p2 = axl.Cycler((axl.Actions.C, axl.Actions.D)) | ||
| >>> p1 == p2 | ||
| False | ||
|
|
||
| To check if player strategies are equivalent you can use :ref:`fingerprinting`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could cause unintended behavior if used during a match or tournament.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see what you mean?
I've done this in such a way that the generator gets copied and there are tests that show that using
__eq__does not alter the generator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lines 199 and 215:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So when the generator is copied it's state is also reset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup that's right. What
itertools.teedoes is essentially fork a generator in to two versions of itself from the state it currently is in.That is also being tests:
Create two
Cooperatorsand give them two cycle generators and tests that they are equal (at this stage their generator are in the same state:Bump
p2s cycle and now the players are not the same:Check what the next values in the cycle are for each player, for
p1it should be0because the player has not iterated through any values and forp2it should be1because it has iterated through the first value:Let me know if you think I could clarify that in the code and/or add other tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI
test_equality_on_initis the test that fails in this case.This should clarify why:
So the second time around the generator/cycle is not compared in the correct way. I did have a special case for a cycle but found it more elegant to just treat them all the same way. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add
isinstance(value, collections.Iterable)to the first check but that would also catch lists etc which we don't want :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might actually be a better way as it ensures we don't change anything at all (even the type of the attributes). I've also realised something else that's not right in this test. Going to tweak and push.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In c5e95d6 I've added a third equality test (theoretically things could break on the second one). I've also added test that ensure the type of a generator/cycle stays the same (which implies I've changed the way I've dealt with the output of
itertools.tee). 👍 Let me know what you think :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanations!