-
Notifications
You must be signed in to change notification settings - Fork 278
Strategy transformers #380
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
Strategy transformers #380
Conversation
This looks interesting and obviously fits the goal of the library of facilitating the study of IPDs. I need to get my head around it a bit more I think. I haven't looked at the code yet, so just thinking about the problem this solves: can you help me out a bit more than you already have. Are you thinking the (main) use case is for people using the library and wanting to create on the fly strategies? Or are these (mainly) to help create strategies? Very much in favour, for example the
Just my usual initial slowness at getting my head around it 👍 😄
This is a perfect example of a 'further_topics' tutorial I think? Also: anything we can do about 0.1% drop in coverage? (Not a disaster obviously) |
I'm sure that I can get the coverage up. This should prevent code duplication and expand the possible strategies the library can produce substantially. There are already many strategies that are e.g. TFT but defect on round one instead. Plus you can, for example, run a tournament of many strategies and apply a transform to all of them. What happens if everyone defects on the first round? If everyone retaliates like TFT? |
Have had more time to think about it: big big fan. Will look through the code itself as soon as possible. |
Glad to hear about it, as I'm fairly certain these transformers are the key to studying the IPD with category theory. I'll write something up eventually... |
Oh wow: that sounds awesome! Category theory is something I know very On Fri, Oct 23, 2015 at 5:42 PM Marc Harper, PhD notifications@github.com
|
I can only aspire to attain such a level of knowledge |
Be careful what you wish for :) On Fri, 23 Oct 2015, 18:35 Owen Campbell notifications@github.com wrote:
|
It's nbd really, you just have to waste years of your life in mathematics graduate school, postponing other life goals and the development of a viable career in the meantime. Or learn a bit of Haskell I suppose. |
Lol. Slightly related. I've seen a quote somewhere, something like: 'graduate school is reducing current income in order to reduce future On Fri, 23 Oct 2015, 18:52 Marc Harper, PhD notifications@github.com
|
axelrod/strategies/backstabber.py
Outdated
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.
Shouldn't this be two defections? The orginal condition was for opponent history length to be greater than 197, so 198 is the first occurrence. If the the history is 198 long, then we are on turn 199.
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 -- I fixed it, and updated the tests.
This is an excellent piece of work and opens up all sorts of possibilities. Other than my comment on backstabber, I'm more than happy to see this go in. |
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 comment isn't quite right? It's do seq on last len(seq) actions right? The default is default...
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.
Good catch.
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.
Thought as much :)
On Sat, 24 Oct 2015, 22:59 Marc Harper, PhD notifications@github.com
wrote:
In axelrod/strategies/strategy_transformers.py
#380 (comment):
- return transformer
+def final_sequence(player, opponent, action, seq):
- """Play the moves in
seq
first, ignoring the strategy's- moves until the list is exhausted."""
- length = player.tournament_attributes["length"]
- if length < 0: # default is -1
return action
- index = length - len(player.history)
- if index <= len(seq):
return seq[-index]
- return action
+# Defect on last N actions
Good catch -- that's what it was initially but then I generalized to an
arbitrary sequence.—
Reply to this email directly or view it on GitHub
https://github.com/Axelrod-Python/Axelrod/pull/380/files#r42938684.
Just some minor things I've commented on. I think the main thing this is missing is an advanced tutorial or further topics. I'd also suggest that a pointer gets put in the contribution docs to that tutorial but as you say I guess that is easiest if you wait for #372? I completely agree with @meatballs: this is great and leaves open the possibility of further transformations to be added :) (that could be explained in the tutorial perhaps?) |
I will certainly improve the documentation -- both the docstrings and an advanced tutorial, both of which are needed since we're metaprogramming now. But I do want #372 to hit first so I don't have merge conflicts on the docs (and I wanted to make sure that you all liked the idea). |
Love the idea. On Sat, 24 Oct 2015, 23:14 Marc Harper, PhD notifications@github.com
|
…, plus a test to catch this
fb3cf46
to
ece74d6
Compare
8953bca
to
a2221fc
Compare
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.
blank line needed here (before the code)
Yeah this is brilliant. Just some blank lines to tidy things a bit. I really like the tutorial. Brilliant contribution, very exciting. |
👍 |
This is very nice! |
…ormers Strategy transformers
This PR introduces strategy transformers, which are ways of transforming the behavior of a strategy without rewriting the strategy's class. There is a generic transformer factory that will take a user-defined function and wrap the Player's strategy method.
For example, using
FlipTransformer
, we can turnCooperator
intoDefector
:Similarly we could turn AntiCycler into DefectingAntiCycler, playing
Instead of
Another transform adds TFT style retaliation to any other strategy:
This actually does transform
Cooperator
exactly intoTitForTat
(it passes all the tests for TFT, which specify TFT completely). For other strategies, it only affects what follows an opponent's defection, otherwise the player's desired plays are simply passed through.There are additional transformers for the following modifications:
If you've been wondering if MetaHunter would be better with a little TFT style retaliation, there's no need to write a new strategy! Moreover the transformations can be chained or composed, so you can add as many transformations to any strategy. The transformations are preserved when the player is cloned.
These transforms can also be used as class decorators. As an example in the library, I modified BackStabber's implementation:
while removing these two lines from the strategy method:
Now it works even if the tournament length is not known, and defects as intended if the length is available.
Some ideas for additional transforms:
I only ran into one significant issue while testing -- the fact that the classifier dict is sometimes a class variable made it difficult to test, since it would overwrite the original class variable (in the super class). One workaround would be to set the classifiers in a method rather than have them as class variables.
Once #372 is merged I'll add a page of usage documentation.