-
Notifications
You must be signed in to change notification settings - Fork 268
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
Implemented WmAdams strategy (k44r from Axelrod's second) #1142
Conversation
I accidentally edited/uploaded the version of test_axelrod_second that had the random.seed() calls. I'm trying to fix it, but failing I'm afraid. |
There's a merge conflict due to difference in Because you're working on the Let me know if you're comfortable sorting this out or if I can assist (ping me on gitter). |
No problem, messages crossed :) Going to gitter as it'll be easier to do this in sync. |
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.
Some minor stylistic requests (all looks great thanks).
To fix the merge conflict, assuming you're using a command line interface to git (let me know if not), let's first pull the changes from the upstream branch:
git pull /~https://github.com/Axelrod-Python/Axelrod.git master
Then there will be some errors in a file somewhere, these errors will just include things like <<<<<
, ========
and >>>>>>>>>
to denote the conflicts. Open up the files and fix those manually.
After that lets commit things:
git commit
Then you should be done :) Not to worry if there's any trouble, we will always be able to figure it out (despite appearances, with git, it's actually very difficult to get in to a mess that is not fixable :) 👍).
axelrod/strategies/axelrod_second.py
Outdated
|
||
class WmAdams(Player): | ||
""" | ||
Strategy submitted to Axelrod's second tournament by William Adams (K49R), |
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.
k44r
?
axelrod/strategies/axelrod_second.py
Outdated
Strategy submitted to Axelrod's second tournament by William Adams (K49R), | ||
and came in fifth in that tournament. | ||
|
||
Count the number of opponent defects after their first move, call |
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.
of opponent defections
axelrod/strategies/axelrod_second.py
Outdated
|
||
Count the number of opponent defects after their first move, call | ||
`c_defect`. Defect if c_defect equals 4, 7, or 9. If c_defect > 9, | ||
then defect immediately after opponent defect with probability = |
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.
then defect immediately after opponent defects
axelrod/strategies/axelrod_second.py
Outdated
|
||
if number_defects == 4 or number_defects == 7 or number_defects == 9: return D | ||
if number_defects > 9 and opponent.history[-1] == D: | ||
return random_choice((0.5)**(number_defects-9)) |
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.
PEP8: ((0.5) ** (number_defects - 9))
actions = [(C, D)] * 4 + [(C, C)] * 100 | ||
self.versus_test(defect_four, expected_actions=actions) | ||
|
||
actions = [(C, D), (C, D), (C, D), (C, D), (C, D), (D, D), (C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (D, D), (D, D), (D, D)] |
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.
Can we line break these before 80 characters please (PEP8).
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 contribution!
axelrod/strategies/axelrod_second.py
Outdated
} | ||
|
||
def strategy(self, opponent: Player) -> Action: | ||
if len(self.history) <=1: |
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.
Space after =
axelrod/strategies/axelrod_second.py
Outdated
def strategy(self, opponent: Player) -> Action: | ||
if len(self.history) <=1: | ||
return C | ||
did_d = np.vectorize(lambda action: float(action == D)) |
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 keep a running count of the number of defections in opponent.defections
. Please use that instead, and subtract one if the first move is a defection. That way is O(rounds)
, the current implementation is O(rounds^2)
.
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.
Wondered about this. I can fix in my earlier "Cave" implementation too.
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.
If you could that would be great (my bad for missing that in the review :)).
axelrod/strategies/axelrod_second.py
Outdated
did_d = np.vectorize(lambda action: float(action == D)) | ||
number_defects = np.sum(did_d(opponent.history[1:])) | ||
|
||
if number_defects == 4 or number_defects == 7 or number_defects == 9: return D |
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.
nit: if number_defects in [4, 7, 9]: return D
is cleaner
I may not get to fixing these until after the Holiday (say Friday). Let me know if I should close the PR and re-open then. Thanks. |
No rush at all, happy thanksgiving for tomorrow 👍
Whatever is easier for you, if you're able to fix the merge conflict (we can help) then it's fine to keep it on this PR but if you'd prefer to close and open another PR that's also cool. |
Conflicts: axelrod/tests/strategies/test_axelrod_second.py
I've made the changes you've requested. Thanks |
if opponent.history[0] == D: | ||
number_defects -= 1 | ||
|
||
if number_defects in [4, 7, 9]: return D |
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.
@gaffney2010 can you confirm which strategy this is? Looking at /~https://github.com/Axelrod-Python/TourExec/blob/master/src/strategies/k44r.f I don't immediately follow how this implementation corresponds to k44r
?
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 can confirm that it's k44r. I'll say more about the translation...
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 confirm I get the same fingerprints :)
I think you've just done a good job refactoring and re writing the code to be much more readable, just got me scratching my head to follow along, so one or two pointers for my own benefit just to confirm would be awesome (even if it's not in the PR itself but just in a comment here).
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.
The ones I'm particularly stumped by are the comparisons of number_defects
to 4, 7 and 9 and also where number_defects - 9
comes from...
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.
Basically every time a defect comes up, it increments this MC variable. While AM is still a whole number, then MC is either less than AM or equal to AM. [While less than, line 16 returns C.] While MC=AM it defects; this happens for the first time at 4 defects [line 11]. As soon as MC exceeds AM, then AM is halved, and MC is reset to zero. [The 5th defect resets MC and sets AM to 2. So the 7th defect gets MC=AM. Then the 8th defect resets MC and sets AM to 1. So the 9th defect gets MC=AM]
After the 9th defect, the function changes because the AM is no longer whole, so MC never again equal AM (and line 17 is out of play). So as soon as MC is incremented (any defect), lines 19-22 are triggered. [Line 22 was 100% until now.] And MC is immediately reset, so there's no sustained defecting, like there was earlier. [For example, there could be exactly 4 defects for many turns, and the strategy would defect for all those turns. But after the 100th defect, the strategy will defect (with some probability) ONLY once.]
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.
That's awesome and make sense. Thanks for explaining.
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.
number_defects - 9
just makes the AM follow the right halving pattern. AM starts at 4.0 and halves after the 4th and 7th, for AM=1.0. After the 9th defect EACH defect will trigger a halving.
I'm happy with all the changes and impressed with the refactor/translation of the original! Thanks @gaffney2010 :) 👍 |
Changes LGTM, just need to resolve the merge conflict(s) |
The conflicts were fixed in c20a106. Thanks for the contribution @gaffney2010 👍 |
Fingerprints below.