Skip to content

Commit 8a50a74

Browse files
committed
[#696] Add filterset dict doc
1 parent 3e053dc commit 8a50a74

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

axelrod/tests/integration/test_filtering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest
1+
2import unittest
22
from hypothesis import given, example
33
from hypothesis.strategies import integers
44
from axelrod import all_strategies, filtered_strategies

docs/tutorials/advanced/classification_of_strategies.rst

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ various dimensions.
1010
Here is the :code:`classifier` for the :code:`Cooperator` strategy::
1111

1212
>>> import axelrod as axl
13-
>>> expected_dictionary = {'manipulates_state': False, 'makes_use_of': set([]), 'long_run_time': False, 'stochastic': False, 'manipulates_source': False, 'inspects_source': False, 'memory_depth': 0} # Order of this dictionary might be different on your machine
13+
>>> expected_dictionary = {
14+
>>> 'manipulates_state': False,
15+
>>> 'makes_use_of': set([]),
16+
>>> 'long_run_time': False,
17+
>>> 'stochastic': False,
18+
>>> 'manipulates_source': False,
19+
>>> 'inspects_source': False,
20+
>>> 'memory_depth': 0
21+
>>> } # Order of this dictionary might be different on your machine
1422
>>> axl.Cooperator.classifier == expected_dictionary
1523
True
1624

@@ -20,37 +28,73 @@ Note that instances of the class also have this classifier::
2028
>>> s.classifier == expected_dictionary
2129
True
2230

23-
This allows us to, for example, quickly identify all the stochastic
31+
We can use this classification to generate sets of strategies according to
32+
filters which we define in a 'filterset' dictionary and then pass to the
33+
'filtered_strategies' function. For example, to identify all the stochastic
2434
strategies::
2535

26-
>>> len([s for s in axl.strategies if s().classifier['stochastic']])
36+
>>> filterset = {
37+
>>> 'stochastic': True
38+
>>> }
39+
>>> strategies = filtered_strategies(filterset)
40+
>>> len(strategies)
2741
43
2842

29-
Or indeed find out how many strategy only use 1 turn worth of memory to
43+
44+
Or, to find out how many strategy only use 1 turn worth of memory to
3045
make a decision::
3146

32-
>>> len([s for s in axl.strategies if s().classifier['memory_depth']==1])
47+
>>> filterset = {
48+
>>> 'memory_depth': 1
49+
>>> }
50+
>>> strategies = filtered_strategies(filterset)
51+
>>> len(strategies)
52+
24
53+
54+
Mutliple filter can be specified within the filterset dictionary. To specify a
55+
range of memory_depth values, we can use the 'min_memory_depth' and
56+
'max_memory_depth' filters::
57+
58+
>>> filterset = {
59+
>>> 'min_memory_depth': 1,
60+
>>> 'max_memory_depth': 4
61+
>>> }
62+
>>> strategies = filtered_strategies(filterset)
63+
>>> len(strategies)
3364
24
3465

3566
We can also identify strategies that make use of particular properties of the
3667
tournament. For example, here is the number of strategies that make use of the
3768
length of each match of the tournament::
3869

39-
>>> len([s() for s in axl.strategies if 'length' in s().classifier['makes_use_of']])
70+
>>> filterset = {
71+
>>> 'makes_use_of': ['length']
72+
>>> }
73+
>>> strategies = filtered_strategies(filterset)
74+
>>> len(strategies)
4075
10
4176

42-
Here are how many of the strategies that make use of the particular game being
43-
played (whether or not it's the default Prisoner's dilemma)::
77+
Note that in the filterset dictionary, the value for the 'makes_use_of' key
78+
must be a list. Here is how we might identify the number of strategies that use
79+
both the length of the tournament and the game being played::
4480

45-
>>> len([s() for s in axl.strategies if 'game' in s().classifier['makes_use_of']])
46-
22
81+
>>> filterset = {
82+
>>> 'makes_use_of': ['length', 'game']
83+
>>> }
84+
>>> strategies = filtered_strategies(filterset)
85+
>>> len(strategies)
86+
10
4787

4888
Some strategies have been classified as having a particularly long run time::
4989

50-
>>> len([s() for s in axl.strategies if s().classifier['long_run_time']])
90+
>>> filterset = {
91+
>>> 'long_run_time': True
92+
>>> }
93+
>>> strategies = filtered_strategies(filterset)
94+
>>> len(strategies)
5195
10
5296

53-
Similarly, strategies that :code:`manipulate_source`, :code:`manipulate_state`
97+
Strategies that :code:`manipulate_source`, :code:`manipulate_state`
5498
and/or :code:`inspect_source` return :code:`False` for the :code:`obey_axelrod`
5599
function::
56100

0 commit comments

Comments
 (0)