|
| 1 | +from collections import namedtuple |
| 2 | +import operator |
| 3 | + |
| 4 | + |
| 5 | +def passes_operator_filter(strategy, classifier_key, value, operator): |
| 6 | + """ |
| 7 | + Tests whether a given strategy passes a filter for a |
| 8 | + given key in its classifier dict using a given (in)equality operator. |
| 9 | +
|
| 10 | + e.g. |
| 11 | +
|
| 12 | + For the following strategy: |
| 13 | +
|
| 14 | + class ExampleStrategy(Player): |
| 15 | + classifier = { |
| 16 | + 'stochastic': True, |
| 17 | + 'inspects_source': False, |
| 18 | + 'memory_depth': 10, |
| 19 | + 'makes_use_of': ['game', 'length'] |
| 20 | + } |
| 21 | +
|
| 22 | + passes_operator_filter(ExampleStrategy, 'memory_depth', 10, operator.eq) |
| 23 | +
|
| 24 | + would test whether the 'memory_depth' entry equals 10 and return True |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + strategy : a descendant class of axelrod.Player |
| 29 | + classifier_key: string |
| 30 | + Defining which entry from the strategy's classifier dict is to be |
| 31 | + tested (e.g. 'memory_depth'). |
| 32 | + value: int |
| 33 | + The value against which the strategy's classifier dict entry is to |
| 34 | + be tested. |
| 35 | + operator: operator.le, operator.ge or operator.eq |
| 36 | + Indicating whether a 'less than or equal to' or 'greater than or |
| 37 | + equal to' test should be applied. |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + boolean |
| 42 | +
|
| 43 | + True if the value from the strategy's classifier dictionary matches |
| 44 | + the value and operator passed to the function. |
| 45 | + """ |
| 46 | + classifier_value = strategy.classifier[classifier_key] |
| 47 | + if (isinstance(classifier_value, str) and |
| 48 | + classifier_value.lower() == 'infinity'): |
| 49 | + classifier_value = float('inf') |
| 50 | + |
| 51 | + return operator(classifier_value, value) |
| 52 | + |
| 53 | + |
| 54 | +def passes_in_list_filter(strategy, classifier_key, value): |
| 55 | + """ |
| 56 | + Tests whether a given list of values exist in the list returned from the |
| 57 | + given strategy's classifier dict for the given classifier_key. |
| 58 | +
|
| 59 | + e.g. |
| 60 | +
|
| 61 | + For the following strategy: |
| 62 | +
|
| 63 | + class ExampleStrategy(Player): |
| 64 | + classifier = { |
| 65 | + 'stochastic': True, |
| 66 | + 'inspects_source': False, |
| 67 | + 'memory_depth': 10, |
| 68 | + 'makes_use_of': ['game', 'length'] |
| 69 | + } |
| 70 | +
|
| 71 | + passes_in_list_filter(ExampleStrategy, 'makes_use_of', 'game', operator.eq) |
| 72 | +
|
| 73 | + would test whether 'game' exists in the strategy's' 'makes_use_of' entry |
| 74 | + and return True. |
| 75 | +
|
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + strategy : a descendant class of axelrod.Player |
| 79 | + classifier_key: string |
| 80 | + Defining which entry from the strategy's classifier dict is to be |
| 81 | + tested (e.g. 'makes_use_of'). |
| 82 | + value: list |
| 83 | + The values against which the strategy's classifier dict entry is to |
| 84 | + be tested. |
| 85 | +
|
| 86 | + Returns |
| 87 | + ------- |
| 88 | + boolean |
| 89 | + """ |
| 90 | + result = True |
| 91 | + for entry in value: |
| 92 | + if entry not in strategy.classifier[classifier_key]: |
| 93 | + result = False |
| 94 | + return result |
| 95 | + |
| 96 | + |
| 97 | +def passes_filterset(strategy, filterset): |
| 98 | + """ |
| 99 | + Determines whether a given strategy meets the criteria defined in a |
| 100 | + dictionary of filters. |
| 101 | +
|
| 102 | + e.g. |
| 103 | +
|
| 104 | + For the following strategy: |
| 105 | +
|
| 106 | + class ExampleStrategy(Player): |
| 107 | + classifier = { |
| 108 | + 'stochastic': True, |
| 109 | + 'inspects_source': False, |
| 110 | + 'memory_depth': 10, |
| 111 | + 'makes_use_of': ['game', 'length'] |
| 112 | + } |
| 113 | +
|
| 114 | + and this filterset dict: |
| 115 | +
|
| 116 | + example_filterset = { |
| 117 | + 'stochastic': True, |
| 118 | + 'memory_depth': 10 |
| 119 | + } |
| 120 | +
|
| 121 | + passes_filterset(ExampleStrategy, example_filterset) |
| 122 | +
|
| 123 | + would test whether both the strategy's 'stochastic' entry is True AND |
| 124 | + that its 'memory_depth' equals 10 and return True. |
| 125 | +
|
| 126 | + Parameters |
| 127 | + ---------- |
| 128 | + strategy : a descendant class of axelrod.Player |
| 129 | + filterset : dict |
| 130 | + mapping filter name to criterion. |
| 131 | + e.g. |
| 132 | + { |
| 133 | + 'stochastic': True, |
| 134 | + 'min_memory_depth': 2 |
| 135 | + } |
| 136 | +
|
| 137 | + Returns |
| 138 | + ------- |
| 139 | + boolean |
| 140 | +
|
| 141 | + True if the given strategy meets all the supplied criteria in the |
| 142 | + filterset, otherwise false. |
| 143 | +
|
| 144 | + """ |
| 145 | + FilterFunction = namedtuple('FilterFunction', 'function kwargs') |
| 146 | + |
| 147 | + # A dictionary mapping filter name (from the supplied filterset) to |
| 148 | + # the relevant function and arguments for that filter. |
| 149 | + filter_functions = { |
| 150 | + 'stochastic': FilterFunction( |
| 151 | + function=passes_operator_filter, |
| 152 | + kwargs={ |
| 153 | + 'classifier_key': 'stochastic', |
| 154 | + 'operator': operator.eq |
| 155 | + }), |
| 156 | + 'long_run_time': FilterFunction( |
| 157 | + function=passes_operator_filter, |
| 158 | + kwargs={ |
| 159 | + 'classifier_key': 'long_run_time', |
| 160 | + 'operator': operator.eq |
| 161 | + }), |
| 162 | + 'manipulates_state': FilterFunction( |
| 163 | + function=passes_operator_filter, |
| 164 | + kwargs={ |
| 165 | + 'classifier_key': 'manipulates_state', |
| 166 | + 'operator': operator.eq |
| 167 | + }), |
| 168 | + 'manipulates_source': FilterFunction( |
| 169 | + function=passes_operator_filter, |
| 170 | + kwargs={ |
| 171 | + 'classifier_key': 'manipulates_source', |
| 172 | + 'operator': operator.eq |
| 173 | + }), |
| 174 | + 'inspects_source': FilterFunction( |
| 175 | + function=passes_operator_filter, |
| 176 | + kwargs={ |
| 177 | + 'classifier_key': 'inspects_source', |
| 178 | + 'operator': operator.eq |
| 179 | + }), |
| 180 | + 'memory_depth': FilterFunction( |
| 181 | + function=passes_operator_filter, |
| 182 | + kwargs={ |
| 183 | + 'classifier_key': 'memory_depth', |
| 184 | + 'operator': operator.eq |
| 185 | + }), |
| 186 | + 'min_memory_depth': FilterFunction( |
| 187 | + function=passes_operator_filter, |
| 188 | + kwargs={ |
| 189 | + 'classifier_key': 'memory_depth', |
| 190 | + 'operator': operator.ge |
| 191 | + }), |
| 192 | + 'max_memory_depth': FilterFunction( |
| 193 | + function=passes_operator_filter, |
| 194 | + kwargs={ |
| 195 | + 'classifier_key': 'memory_depth', |
| 196 | + 'operator': operator.le |
| 197 | + }), |
| 198 | + 'makes_use_of': FilterFunction( |
| 199 | + function=passes_in_list_filter, |
| 200 | + kwargs={'classifier_key': 'makes_use_of'}) |
| 201 | + } |
| 202 | + |
| 203 | + # A list of boolean values to record whether the strategy passed or failed |
| 204 | + # each of the filters in the supplied filterset. |
| 205 | + passes_filters = [] |
| 206 | + |
| 207 | + # Loop through each of the entries in the filter_functions dict and, if |
| 208 | + # that filter is defined in the supplied filterset, call the relevant |
| 209 | + # function and record its result in the passes_filters list. |
| 210 | + for _filter, filter_function in filter_functions.items(): |
| 211 | + |
| 212 | + if filterset.get(_filter, None) is not None: |
| 213 | + kwargs = filter_function.kwargs |
| 214 | + kwargs['strategy'] = strategy |
| 215 | + kwargs['value'] = filterset[_filter] |
| 216 | + passes_filters.append(filter_function.function(**kwargs)) |
| 217 | + |
| 218 | + # Return True if the strategy passed all the supplied filters |
| 219 | + return all(passes_filters) |
0 commit comments