Skip to content

Commit

Permalink
Meta Winner Ensemble strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed Nov 4, 2016
1 parent 7367a32 commit 0f5b0af
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
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."""

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


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

0 comments on commit 0f5b0af

Please sign in to comment.