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

Implemented WmAdams strategy (k44r from Axelrod's second) #1142

Merged
merged 5 commits into from
Nov 27, 2017

Conversation

gaffney2010
Copy link
Member

Fingerprints below.
basic_fortran
basic_python
random_fortran
random_python

@gaffney2010
Copy link
Member Author

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.

@drvinceknight
Copy link
Member

There's a merge conflict due to difference in test_axelrod_second.

Because you're working on the master branch, I'd suggest you git pull upstream (where you set upstream to be the remote corresponding to this repo) and fix thing locally.

Let me know if you're comfortable sorting this out or if I can assist (ping me on gitter).

@drvinceknight
Copy link
Member

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.

No problem, messages crossed :) Going to gitter as it'll be easier to do this in sync.

Copy link
Member

@drvinceknight drvinceknight left a 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 :) 👍).


class WmAdams(Player):
"""
Strategy submitted to Axelrod's second tournament by William Adams (K49R),
Copy link
Member

Choose a reason for hiding this comment

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

k44r?

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
Copy link
Member

Choose a reason for hiding this comment

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

of opponent defections


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 =
Copy link
Member

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


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))
Copy link
Member

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)]
Copy link
Member

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).

Copy link
Member

@marcharper marcharper left a 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!

}

def strategy(self, opponent: Player) -> Action:
if len(self.history) <=1:
Copy link
Member

Choose a reason for hiding this comment

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

Space after =

def strategy(self, opponent: Player) -> Action:
if len(self.history) <=1:
return C
did_d = np.vectorize(lambda action: float(action == D))
Copy link
Member

@marcharper marcharper Nov 21, 2017

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).

Copy link
Member Author

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.

Copy link
Member

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 :)).

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
Copy link
Member

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

@gaffney2010
Copy link
Member Author

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.

@drvinceknight
Copy link
Member

I may not get to fixing these until after the Holiday (say Friday).

No rush at all, happy thanksgiving for tomorrow 👍

Let me know if I should close the PR and re-open then. Thanks.

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.

@gaffney2010
Copy link
Member Author

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
Copy link
Member

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?

Copy link
Member Author

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...

Copy link
Member

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).

Copy link
Member

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...

Copy link
Member Author

@gaffney2010 gaffney2010 Nov 26, 2017

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.]

Copy link
Member

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.

Copy link
Member Author

@gaffney2010 gaffney2010 Nov 26, 2017

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.

@drvinceknight
Copy link
Member

I'm happy with all the changes and impressed with the refactor/translation of the original! Thanks @gaffney2010 :) 👍

@marcharper
Copy link
Member

Changes LGTM, just need to resolve the merge conflict(s)

@drvinceknight
Copy link
Member

The conflicts were fixed in c20a106. Thanks for the contribution @gaffney2010 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants