-
Notifications
You must be signed in to change notification settings - Fork 264
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
add a new strategy CAPRI #1393
add a new strategy CAPRI #1393
Conversation
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 this, I have made some initial stylistic comments but need to read the paper to offer a comprehensive review (planning on doing that soon as it looks interesting).
A comment: it does look like this strategy is a specific case of a axl.Lookerup
(https://github.com/Axelrod-Python/Axelrod/blob/dev/axelrod/strategies/lookerup.py#L236) so it would be possible to implement this as a particular case which could help clear things up.
axelrod/strategies/grudger.py
Outdated
C: Cooperate at mutual cooperation. | ||
This rule prescribes c at (ccc, ccc). | ||
A: Accept punishment when you mistakenly defected from mutual cooperation. | ||
This rule prescribes c at (ccd, ccc), (cdc, ccd), (dcc, cdc), and (ccc, dcc). | ||
P: Punish your co-player by defecting once when he defected from mutual cooperation. | ||
This rule prescribes d at (ccc, ccd), and then c at (ccd, cdc), (cdc, dcc), and (dcc, ccc). | ||
R: Recover cooperation when you or your co-player cooperated at mutual defection. | ||
This rule prescribes c at (ddd, ddc), (ddc, dcc), (dcc, ccc), (ddc, ddd), (dcc, ddc), (ccc, dcc), (ddc, ddc), and (dcc, dcc). | ||
I: In all the ohter cases, defect. |
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.
These rules are not immediately clear to me. If it is verbatim from the paper (which I have not read yet) then that should be made clear and some clarification should be added.
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 rules are from the paper)
axelrod/strategies/grudger.py
Outdated
elif hist == [[C,C],[C,C],[D,C]]: # Rule: A | ||
return C | ||
elif hist == [[C,C],[D,C],[C,D]]: | ||
return C | ||
elif hist == [[D,C],[C,D],[C,C]]: | ||
return C | ||
elif hist == [[C,D],[C,C],[C,C]]: | ||
return C | ||
elif hist == [[C,C],[C,C],[C,D]]: # Rule: P | ||
return D | ||
elif hist == [[C,C],[C,D],[D,C]]: | ||
return C | ||
elif hist == [[C,D],[D,C],[C,C]]: | ||
return C | ||
elif hist == [[D,C],[C,C],[C,C]]: | ||
return C | ||
elif hist == [[D,D],[D,D],[D,C]]: # Rule: R1 | ||
return C | ||
elif hist == [[D,D],[D,C],[C,C]]: | ||
return C | ||
elif hist == [[D,C],[C,C],[C,C]]: | ||
return C | ||
elif hist == [[D,D],[D,D],[C,D]]: # Rule: R2 | ||
return C | ||
elif hist == [[D,D],[C,D],[C,C]]: | ||
return C | ||
elif hist == [[C,D],[C,C],[C,C]]: | ||
return C | ||
elif hist == [[D,D],[D,D],[C,C]]: # Rule: R3 | ||
return C | ||
elif hist == [[D,D],[C,C],[C,C]]: | ||
return C |
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.
elif hist == [[C,C],[C,C],[D,C]]: # Rule: A | |
return C | |
elif hist == [[C,C],[D,C],[C,D]]: | |
return C | |
elif hist == [[D,C],[C,D],[C,C]]: | |
return C | |
elif hist == [[C,D],[C,C],[C,C]]: | |
return C | |
elif hist == [[C,C],[C,C],[C,D]]: # Rule: P | |
return D | |
elif hist == [[C,C],[C,D],[D,C]]: | |
return C | |
elif hist == [[C,D],[D,C],[C,C]]: | |
return C | |
elif hist == [[D,C],[C,C],[C,C]]: | |
return C | |
elif hist == [[D,D],[D,D],[D,C]]: # Rule: R1 | |
return C | |
elif hist == [[D,D],[D,C],[C,C]]: | |
return C | |
elif hist == [[D,C],[C,C],[C,C]]: | |
return C | |
elif hist == [[D,D],[D,D],[C,D]]: # Rule: R2 | |
return C | |
elif hist == [[D,D],[C,D],[C,C]]: | |
return C | |
elif hist == [[C,D],[C,C],[C,C]]: | |
return C | |
elif hist == [[D,D],[D,D],[C,C]]: # Rule: R3 | |
return C | |
elif hist == [[D,D],[C,C],[C,C]]: | |
return C | |
if hist == [[C, C], [C, C], [D, C]]: # Rule: A | |
return C | |
if hist == [[C, C], [D, C], [C, D]]: | |
return C | |
if hist == [[D, C], [C, D], [C, C]]: | |
return C | |
if hist == [[C, D], [C, C], [C, C]]: | |
return C | |
if hist == [[C, C], [C, C], [C, D]]: # Rule: P | |
return D | |
if hist == [[C, C], [C, D], [D, C]]: | |
return C | |
if hist == [[C, D], [D, C], [C, C]]: | |
return C | |
if hist == [[D, C], [C, C], [C, C]]: | |
return C | |
if hist == [[D, D], [D, D], [D, C]]: # Rule: R1 | |
return C | |
if hist == [[D, D], [D, C], [C, C]]: | |
return C | |
if hist == [[D, C], [C, C], [C, C]]: | |
return C | |
if hist == [[D, D], [D, D], [C, D]]: # Rule: R2 | |
return C | |
if hist == [[D, D], [C, D], [C, C]]: | |
return C | |
if hist == [[C, D], [C, C], [C, C]]: | |
return C | |
if hist == [[D, D], [D, D], [C, C]]: # Rule: R3 | |
return C | |
if hist == [[D, D], [C, C], [C, C]]: | |
return C |
The tests are currently failing as the documentation does not build because of the way you have formatted your doctstring:
|
Co-authored-by: Vince Knight <vince@vknight.org>
Co-authored-by: Vince Knight <vince@vknight.org>
Co-authored-by: Vince Knight <vince@vknight.org>
Co-authored-by: Vince Knight <vince@vknight.org>
Co-authored-by: Vince Knight <vince@vknight.org>
Since all the permutations must be specified to use |
Co-authored-by: Vince Knight <vince@vknight.org>
Thank you for pointing that out. Could you tell me how to test the docstring? |
axelrod/strategies/grudger.py
Outdated
CAPRI is a memory-3 strategy proposed in [Murase2020]_. Its behavior is defined by the following five rules. | ||
C: Cooperate at mutual cooperation. | ||
This rule prescribes c at (ccc, ccc). | ||
A: Accept punishment when you mistakenly defected from mutual cooperation. | ||
This rule prescribes c at (ccd, ccc), (cdc, ccd), (dcc, cdc), and (ccc, dcc). | ||
P: Punish your co-player by defecting once when he defected from mutual cooperation. | ||
This rule prescribes d at (ccc, ccd), and then c at (ccd, cdc), (cdc, dcc), and (dcc, ccc). | ||
R: Recover cooperation when you or your co-player cooperated at mutual defection. | ||
This rule prescribes c at (ddd, ddc), (ddc, dcc), (dcc, ccc), (ddc, ddd), (dcc, ddc), (ccc, dcc), (ddc, ddc), and (dcc, dcc). | ||
I: In all the ohter cases, defect. |
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.
Thank you for pointing that out. Could you tell me how to test the docstring?
Of course: top build the documentation, cd docs
(go to the docs
directory) and run:
make html
You might need to pip
install some dependencies.
I have checked locally and below fixes the sphinx build problems:
CAPRI is a memory-3 strategy proposed in [Murase2020]_. Its behavior is defined by the following five rules. | |
C: Cooperate at mutual cooperation. | |
This rule prescribes c at (ccc, ccc). | |
A: Accept punishment when you mistakenly defected from mutual cooperation. | |
This rule prescribes c at (ccd, ccc), (cdc, ccd), (dcc, cdc), and (ccc, dcc). | |
P: Punish your co-player by defecting once when he defected from mutual cooperation. | |
This rule prescribes d at (ccc, ccd), and then c at (ccd, cdc), (cdc, dcc), and (dcc, ccc). | |
R: Recover cooperation when you or your co-player cooperated at mutual defection. | |
This rule prescribes c at (ddd, ddc), (ddc, dcc), (dcc, ccc), (ddc, ddd), (dcc, ddc), (ccc, dcc), (ddc, ddc), and (dcc, dcc). | |
I: In all the ohter cases, defect. | |
CAPRI is a memory-3 strategy proposed in [Murase2020]_. Its behavior is | |
defined by the following five rules. | |
- C: Cooperate at mutual cooperation. This rule prescribes c at (ccc, ccc). | |
- A: Accept punishment when you mistakenly defected from mutual cooperation. | |
This rule prescribes c at (ccd, ccc), (cdc, ccd), (dcc, cdc), and (ccc, | |
dcc). | |
- P: Punish your co-player by defecting once when he defected from mutual | |
cooperation. This rule prescribes d at (ccc, ccd), and then c at (ccd, | |
cdc), (cdc, dcc), and (dcc, ccc). | |
- R: Recover cooperation when you or your co-player cooperated at mutual | |
defection. This rule prescribes c at (ddd, ddc), (ddc, dcc), (dcc, ccc), | |
(ddc, ddd), (dcc, ddc), (ccc, dcc), (ddc, ddc), and (dcc, dcc). | |
- I: In all the ohter cases, defect. | |
Names: | |
- CAPRI: Original Name by Y. Murase et al. [Murase2020]_ | |
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: ohter -> other on line 362
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.
Thank you for your help. I confirmed that the docstring is compiled without error in my environment.
Sure, I don't feel strongly, this could always be refactored at a later date. |
Everything is looking really good, I've left a couple of suggestions/comments, let me know if I can assist with anything. FYI, re my comment about implementing this as a |
[No action required] Based on Figure 2 in the paper, it looks like the strategy could also be implemented as a finite state machine. FYI the original implementation is here, perhaps that's worth documenting. (I don't see any tests though so I'm not sure if the code would be more helpful than the paper descriptions.) |
Co-authored-by: Vince Knight <vince@vknight.org>
Thank you for your help.
If you like FSM, you might find the following paper interesting :) |
I added a link to the repository in the docstring. Thank you for your suggestion. |
That looks very cool indeed, have added it to my reading list. @marcharper and @gaffney2010 designed an algorithm for finding the memory length of arbitrary FSMs (https://arxiv.org/abs/1912.04493) which is implemented in the library: https://axelrod.readthedocs.io/en/latest/discussion/strategy_archetypes.html?highlight=finite%20state%20machines#finite-state-machines implementing your algorithm to obtain the FSM from the LookerUp could be neat as well. |
if hist == [[D, D], [D, C], [C, C]]: | ||
return C | ||
if hist == [[D, C], [C, C], [C, C]]: | ||
return C |
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 CI is now failing due to coverage (which checks that all lines of code are hit by at least one test):
axelrod/strategies/grudger.py 160 2 99% 413, 419
This line (line 413) is not hit and also line 419.
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.
These lines are never used because the conditions are duplicates of the previous lines. I commented out these lines.
to make the coverage 100%, codes that never run are commented out
axelrod/strategies/grudger.py
Outdated
|
||
def strategy(self, opponent: Player) -> Action: | ||
# initial history profile is full cooperation | ||
hist = [[C, C], [C, C], [C, C]] |
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.
It might be more readable to do something like:
hist = zip(self.history[-3:], opponent.history[-3:])
while len(hist) < 3:
hist.push(0, (C, C))
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.
Thank you for your suggestion. I polished up the code with 2252c88
👋🏻 @yohm! The CI is failing due to coverage:
because you are not using the method |
Another comment I had was regarding 35d1890 I would delete these lines instead of commenting them out. Then you could add comments like,
or discuss in the docstring that in the implementation of the strategy we don’t need to check all the conditions for each rule. p.s. Thank you for sharing this https://www.nature.com/articles/s41598-020-70281-x! |
Almost there @yohm ! |
@Nikoleta-v3 @marcharper Thank you for your comments. The code was revised according to your advice. See 3ff428b |
axelrod/strategies/_strategies.py
Outdated
@@ -143,6 +143,7 @@ | |||
OppositeGrudger, | |||
SoftGrudger, | |||
SpitefulCC, | |||
Capri, |
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 you move this so that the tuple is in alphabetical order please and then Capri
needs to be added to the all_strategies
list in this file. (This is why the CI is failing).
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.
Fixed.
The CI is failing because one of the meta strategies' behaviours has now changed (as it is using actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] (Previously it was: actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] ) I have made the change locally and checked that the |
Hopefully, it is fixed now. |
A couple of doc tests are failing, just need to bump up the counts:
|
Looks like the last failing test is due to |
This looks like everything is fixed now! Thanks for going through it all @yohm 👍 |
I added a new strategy 'CAPRI', which is proposed in the following paper.
https://www.nature.com/articles/s41598-020-73855-x
Would you please review and let me know if you find something that needs to be revised? Thank you.