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

Meta Winner Ensemble #757

Merged
merged 3 commits into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions axelrod/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
MetaPlayer, MetaMajority, MetaMinority, MetaWinner, MetaHunter,
MetaMajorityMemoryOne, MetaWinnerMemoryOne, MetaMajorityFiniteMemory,
MetaWinnerFiniteMemory, MetaMajorityLongMemory, MetaWinnerLongMemory,
MetaMixer
MetaMixer, MetaWinnerEnsemble
)

all_strategies.extend([MetaHunter, MetaMajority, MetaMinority, MetaWinner,
MetaMajorityMemoryOne, MetaWinnerMemoryOne,
MetaMajorityFiniteMemory, MetaWinnerFiniteMemory,
MetaMajorityLongMemory, MetaWinnerLongMemory, MetaMixer])
MetaMajorityLongMemory, MetaWinnerLongMemory, MetaMixer,
MetaWinnerEnsemble])


# Distinguished strategy collections in addition to
Expand Down
28 changes: 27 additions & 1 deletion axelrod/strategies/meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod import Actions, Player, obey_axelrod
from axelrod import Actions, Player, obey_axelrod, random_choice
from ._strategies import all_strategies
from .hunter import (
DefectorHunter, AlternatorHunter, RandomHunter, MathConstantHunter,
Expand Down Expand Up @@ -165,6 +165,32 @@ def meta_strategy(self, results, opponent):

return bestresult

class MetaWinnerEnsemble(MetaWinner):
"""A variant of MetaWinner that chooses one of the top scoring strategies at random against each opponent."""
Copy link
Member

Choose a reason for hiding this comment

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

Could you add something like:

Names:

Meta Winner Ensemble: Original name by Marc Harper

Copy link
Member Author

Choose a reason for hiding this comment

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

done


name = "Meta Winner Ensemble"

def meta_strategy(self, results, opponent):
# Sort by score
scores = [(pl.score, i) for (i, pl) in enumerate(self.team)]
# Choose one of the best scorers at random
scores.sort(reverse=True)
prop = max(1, int(len(scores) * 0.08))
index = choice([i for (s, i) in scores[:prop]])

# Update each player's proposed history with his proposed result, but
# always after the new result has been settled based on scores
# accumulated until now.
for r, t in zip(results, self.team):
t.proposed_history.append(r)

if opponent.defections == 0:
# Don't poke the bear
Copy link
Member

Choose a reason for hiding this comment

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

😄

return C

# return result
return results[index]


class MetaHunter(MetaPlayer):
"""A player who uses a selection of hunters."""
Expand Down
21 changes: 21 additions & 0 deletions axelrod/tests/unit/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ def test_strategy(self):
self.assertEqual(player.history[-1], D)


class TestMetaWinnerEnsemble(TestMetaPlayer):
name = "Meta Winner Ensemble"
player = axelrod.MetaWinnerEnsemble
expected_classifier = {
'memory_depth': float('inf'), # Long memory
'stochastic': True,
'makes_use_of': set(['game']),
'long_run_time': True,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

expected_class_classifier = copy.copy(expected_classifier)
expected_class_classifier['stochastic'] = False
expected_class_classifier['makes_use_of'] = set([])

def test_strategy(self):
self.first_play_test(C)

Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test (for a given random seed) just for a few plays please.

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 added two deterministic test cases.


class TestMetaHunter(TestMetaPlayer):

name = "Meta Hunter"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/advanced/classification_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Some strategies have been classified as having a particularly long run time::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
10
11

Strategies that :code:`manipulate_source`, :code:`manipulate_state`
and/or :code:`inspect_source` return :code:`False` for the :code:`obey_axelrod`
Expand Down