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

Algorithm for Memory Depth in FSM #1233

Merged
merged 40 commits into from
Jan 30, 2019
Merged

Conversation

gaffney2010
Copy link
Member

As discussed here:
#597

next_action,
last_opponent_action))

class ActionChain(object):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move these classes out of the function, it doesn't seem like they use any local data in their definitions.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right that they could be moved outside of the function without problem. My reasoning for leaving them in the function is because they're not used anywhere else, and my feeling is that it's similar to a local variable, which could be made global, but encapsulation is better. I know this is a contentious point though.

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to see these classes outside the function and with unit tests for their behaviour.

@marcharper
Copy link
Member

marcharper commented Jan 7, 2019

The travis error is as follows (docstring format):

Warning, treated as error:
/home/travis/build/Axelrod-Python/Axelrod/axelrod/strategies/finite_state_machines.py:docstring of axelrod.strategies.finite_state_machines.get_memory_from_transitions:36:Unexpected indentation.
make: *** [html] Error 2
The command "cd docs; make clean; make html" exited with 2.

The tests are also outputing all the branches as the algorithm proceeds, I'd make that optional (with a verbose kwarg):

If _/D, C/_, then D
If _/C, C/_, then C
If _/C, D/_, then D
If _/D, C/D, D/_, then D
If _/C, D/D, D/_, then D
If _/D, D/D, D/_, then C
...

@marcharper
Copy link
Member

marcharper commented Jan 7, 2019

Can you apply the algorithm to the existing FSM players in the library? The current memory depth values were best guesses at the time the strategies were added. In some cases we inherit from the TestPlayer class to add additional tests, in this case something like TestFSMPlayer where it applies your algorithm to verify, at test time, that the player is of the expected depth.

@marcharper
Copy link
Member

We could also consider adding a method to FSMPlayer's __init__ to compute the memory_depth if it is unspecified. This will come in handy for e.g. the dojo so we can evolve players of depth <= N.

@gaffney2010
Copy link
Member Author

The travis error is as follows (docstring format):

Fixed.

The tests are also outputing all the branches as the algorithm proceeds, I'd make that optional (with a verbose kwarg):

I added a print_output flag to go along with the (even more verbose) print_trace flag.

@gaffney2010
Copy link
Member Author

Can you apply the algorithm to the existing FSM players in the library?

I'll do that.

in this case something like TestFSMPlayer where it applies your algorithm to verify, at test time, that the player is of the expected depth.

Currently the runtime for large FSMs is too long to be tolerable. [Like many times longer than the rest of the tests put together.] I'll see if there are any easy speed-ups.

@drvinceknight
Copy link
Member

Currently the runtime for large FSMs is too long to be tolerable. [Like many times longer than the rest of the tests put together.] I'll see if there are any easy speed-ups.

A suggestion: one option could be to have a separate repo where we run the algorithm on the fsms perhaps?

@gaffney2010
Copy link
Member Author

These 16-state strategies take a very long time. I guess the number of branches is growing exponentially. I'm going have to get this thing to run faster. I think there's a memoization here that makes sense.

@gaffney2010
Copy link
Member Author

I may end up changing stuff for the caching logic. Closing for now.

@gaffney2010 gaffney2010 closed this Jan 8, 2019
@gaffney2010 gaffney2010 reopened this Jan 9, 2019
@gaffney2010
Copy link
Member Author

In thinking about how to memoize, I ended up changing significantly. It brought the runtime down from days to milliseconds.

The new approach is a little abstract, and it may be harder to see that it works, but it does match the other implementation on every FSM strategy, and it passes all of the tests.

@gaffney2010
Copy link
Member Author

in this case something like TestFSMPlayer where it applies your algorithm to verify, at test time, that the player is of the expected depth.

We could also consider adding a method to FSMPlayer's init to compute the memory_depth if it is unspecified. This will come in handy for e.g. the dojo so we can evolve players of depth <= N.

I can probably do these now that it runs fast.

@marcharper
Copy link
Member

marcharper commented Jan 12, 2019

The error is on the typing syntax, which isn't supported on Python 3.5. However we generally only support the last two trailing versions of Python, so it's time we updated the library to 3.6 and 3.7, which would eliminate the error.

@drvinceknight ok with you to drop 3.5 and add 3.7?

Edit: See #1235

@gaffney2010
Copy link
Member Author

I played around with adding a test of the coded memory depth against the output of the algorithm in TestFSMPlayer so I opened gaffney2010#1. As you said there is basically no time cost to doing that so perhaps nice to include? I went through your conversations with @marcharper and didn't see that being discussed so perhaps I've missed something (feel free to close the PR or implement your own way).

I was thinking to do this in a separate PR, but I hadn't realized that it would be such a minor change. I accepted your PR into this change; it looks good to me.

One other request I'd have would be to (briefly) document the usage of the algorithm in https://axelrod.readthedocs.io/en/stable/tutorials/advanced/index.html (just a super short use case example).

I think it makes sense to make a FSM section on that page, and mention it on there. It would feel out-of-place otherwise. I'd be happy to write such a section; let me know what you think.

Finally, is it perhaps worth moving the code to it's own module (python file) to keep finite_state_macines.py for players? I personally feel it would clear things up slightly as there is currently a lot of code at the top of finite_state_machines.py that's not relevant for someone "just" contributing a strategy. Perhaps axelrod/compute_finite_state_machine_memory.py (the specific tests would be moved to unit)? What do you think?

Makes sense to me. I moved it.

@drvinceknight
Copy link
Member

I think it makes sense to make a FSM section on that page, and mention it on there. It would feel out-of-place otherwise. I'd be happy to write such a section; let me know what you think.

Sounds good to me 👍

@drvinceknight
Copy link
Member

For a nice overview of FSMs (with the memory length use case) perhaps this section of the docs would be more appropriate: https://axelrod.readthedocs.io/en/stable/tutorials/further_topics/index.html

?

@gaffney2010
Copy link
Member Author

I decided to leave it on the further topics page, and focus on the way you execute the code. I think the research topics page would be better if I focused on open questions or some of the research around this, but this is sorta how dojo documentation approaches it already.

I put this under a broader "meta-strategies" heading, because I thought we could eventually add write-up for things like HMMPlayer, SequencePlayer, LookerUp, and Gambler. I wrote the section that it would be general enough to apply to games other than IPD, in anticipation of 5.0.

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.

This all looks really good to me @gaffney2010, I've left some minor comments to ensure the docs build, a style suggestion/request for the orderered memit and also a request to modularise some of the tests (sorry for being a pain).

Once these minor tweaks have been made I think this is good to go makes for an awesome contribution. 👍

docs/tutorials/further_topics/meta_strategies.rst Outdated Show resolved Hide resolved
docs/tutorials/further_topics/meta_strategies.rst Outdated Show resolved Hide resolved
docs/tutorials/further_topics/meta_strategies.rst Outdated Show resolved Hide resolved
docs/tutorials/further_topics/meta_strategies.rst Outdated Show resolved Hide resolved
docs/tutorials/further_topics/meta_strategies.rst Outdated Show resolved Hide resolved
docs/tutorials/further_topics/index.rst Show resolved Hide resolved
axelrod/compute_finite_state_machine_memory.py Outdated Show resolved Hide resolved
drvinceknight added a commit to drvinceknight/testing_for_ZD that referenced this pull request Jan 24, 2019
Closes #119

Since this research was originally started, an algorithm has been proposed to
accurately measure the memory of finite state machines:
Axelrod-Python/Axelrod#1233
This step manually corrects the known discrepancies but has no effect on the
results of the work.
drvinceknight added a commit to drvinceknight/testing_for_ZD that referenced this pull request Jan 25, 2019
Closes #119

Since this research was originally started, an algorithm has been proposed to
accurately measure the memory of finite state machines:
Axelrod-Python/Axelrod#1233
This step manually corrects the known discrepancies but has no effect on the
results of the work.
@marcharper
Copy link
Member

Here's the build error (it's in the docs):

Warning, treated as error:
/home/travis/build/Axelrod-Python/Axelrod/docs/tutorials/further_topics/meta_strategies.rst:20:citation not found: Harper2017
make: *** [html] Error 2
The command "cd docs; make clean; make html" exited with 2.

@drvinceknight
Copy link
Member

It's the bib item missing: #1233 (comment) 👍

@gaffney2010
Copy link
Member Author

Can't figure out why I can't commit that file. Ended up editing it on Github's GUI.

@gaffney2010
Copy link
Member Author

I was accidentally making changes to the wrong branch. Got it cleaned up now.

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.

Awesome, thank you for all the changes @gaffney2010 and thanks for the overall contribution: very very cool.

I'll let @marcharper merge just to be sure he's not unhappy with any of the changes made. 👍

@marcharper marcharper merged commit ee13777 into Axelrod-Python:master Jan 30, 2019
@drvinceknight
Copy link
Member

I'll aim to put a new release together tomorrow 👍

@langner
Copy link
Member

langner commented Feb 16, 2019

Hey guys, I just had the pleasure of reading through this thread and wanted to send a shout out. It's a very cool change. And it's encouraging to see the project being so vibrant with so much collaboration :) kudos!

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

Successfully merging this pull request may close these issues.

None yet

5 participants