def test_memory_depth_filtering(self, min_memory_depth, max_memory_depth, memory_depth): min_comprehension = set([ s for s in all_strategies if s.classifier['memory_depth'] >= min_memory_depth ]) min_filterset = {'min_memory_depth': min_memory_depth} min_filtered = set(filtered_strategies(min_filterset)) self.assertEqual(min_comprehension, min_filtered) max_comprehension = set([ s for s in all_strategies if s.classifier['memory_depth'] <= max_memory_depth ]) max_filterset = {'max_memory_depth': max_memory_depth} max_filtered = set(filtered_strategies(max_filterset)) self.assertEqual(max_comprehension, max_filtered) comprehension = set([ s for s in all_strategies if s.classifier['memory_depth'] == memory_depth ]) filterset = {'memory_depth': memory_depth} filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered)
def test_memory_depth_filtering(self, min_memory_depth, max_memory_depth, memory_depth, strategies): min_comprehension = set([ s for s in strategies if s().classifier["memory_depth"] >= min_memory_depth ]) min_filterset = {"min_memory_depth": min_memory_depth} min_filtered = set( filtered_strategies(min_filterset, strategies=strategies)) self.assertEqual(min_comprehension, min_filtered) max_comprehension = set([ s for s in strategies if s().classifier["memory_depth"] <= max_memory_depth ]) max_filterset = {"max_memory_depth": max_memory_depth} max_filtered = set( filtered_strategies(max_filterset, strategies=strategies)) self.assertEqual(max_comprehension, max_filtered) comprehension = set([ s for s in strategies if s().classifier["memory_depth"] == memory_depth ]) filterset = {"memory_depth": memory_depth} filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered)
def test_memory_depth_filtering(self, min_memory_depth, max_memory_depth, memory_depth): min_comprehension = set([ s for s in all_strategies if s.classifier['memory_depth'] >= min_memory_depth]) min_filterset = { 'min_memory_depth': min_memory_depth } min_filtered = set(filtered_strategies(min_filterset)) self.assertEqual(min_comprehension, min_filtered) max_comprehension = set([ s for s in all_strategies if s.classifier['memory_depth'] <= max_memory_depth]) max_filterset = { 'max_memory_depth': max_memory_depth } max_filtered = set(filtered_strategies(max_filterset)) self.assertEqual(max_comprehension, max_filtered) comprehension = set([ s for s in all_strategies if s.classifier['memory_depth'] == memory_depth]) filterset = { 'memory_depth': memory_depth } filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered)
def test_filtered_strategies(self): class StochasticTestStrategy(object): classifier = { 'stochastic': True, 'memory_depth': float('inf'), 'makes_use_of': [] } class MemoryDepth2TestStrategy(object): classifier = { 'stochastic': False, 'memory_depth': 2, 'makes_use_of': [] } class UsesLengthTestStrategy(object): classifier = { 'stochastic': True, 'memory_depth': float('inf'), 'makes_use_of': ['length'] } strategies = [ StochasticTestStrategy, MemoryDepth2TestStrategy, UsesLengthTestStrategy ] stochastic_filterset = { 'stochastic': True } deterministic_filterset = { 'stochastic': False } uses_length_filterset = { 'stochastic': True, 'makes_use_of': ['length'] } self.assertEqual( filtered_strategies(stochastic_filterset, strategies), [StochasticTestStrategy, UsesLengthTestStrategy]) self.assertEqual( filtered_strategies(deterministic_filterset, strategies), [MemoryDepth2TestStrategy]) self.assertEqual( filtered_strategies(uses_length_filterset, strategies), [UsesLengthTestStrategy])
def test_filtered_strategies(self): class StochasticTestStrategy(object): classifier = { "stochastic": True, "memory_depth": float("inf"), "makes_use_of": [], } class MemoryDepth2TestStrategy(object): classifier = { "stochastic": False, "memory_depth": 2, "makes_use_of": [] } class UsesLengthTestStrategy(object): classifier = { "stochastic": True, "memory_depth": float("inf"), "makes_use_of": ["length"], } strategies = [ StochasticTestStrategy, MemoryDepth2TestStrategy, UsesLengthTestStrategy, ] stochastic_filterset = {"stochastic": True} deterministic_filterset = {"stochastic": False} uses_length_filterset = { "stochastic": True, "makes_use_of": ["length"] } self.assertEqual( filtered_strategies(stochastic_filterset, strategies), [StochasticTestStrategy, UsesLengthTestStrategy], ) self.assertEqual( filtered_strategies(deterministic_filterset, strategies), [MemoryDepth2TestStrategy], ) self.assertEqual( filtered_strategies(uses_length_filterset, strategies), [UsesLengthTestStrategy], )
def filter_strategies(request): """ Take the incoming request object, convert the strings in its query_params dictionary into the types required by the axelrod filtering function and pass the resulting dictionary into that filtering function. """ params = request.query_params filter_types = { strtobool: [ 'stochastic', 'long_run_time', 'manipulates_state', 'manipulates_source', 'inpsects_source' ], int: ['memory_depth', 'min_memory_depth', 'max_memory_depth'], } filterset = { _filter: convert_type(params[_filter]) for convert_type, filters in filter_types.items() for _filter in filters if _filter in params } if 'makes_use_of' in params: filterset['makes_use_of'] = params.getlist('makes_use_of') return axl.filtered_strategies(filterset)
def test_makes_use_of_filtering(self, seed_): """ Test equivalent filtering using two approaches. This needs to be seeded as some players classification is random. """ classifiers = [ ['game'], ['length'], ['game', 'length'] ] for classifier in classifiers: seed(seed_) comprehension = set([ s for s in all_strategies if set(classifier).issubset(set(s().classifier['makes_use_of'])) ]) seed(seed_) filterset = { 'makes_use_of': classifier } filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered, msg="classifier: {}".format(classifier))
def test_makes_use_of_filtering(self, seed_, strategies): """ Test equivalent filtering using two approaches. This needs to be seeded as some players classification is random. """ classifiers = [["game"], ["length"], ["game", "length"]] for classifier in classifiers: axl.seed(seed_) comprehension = set( [ s for s in strategies if set(classifier).issubset(set(axl.Classifiers["makes_use_of"](s))) ] ) axl.seed(seed_) filterset = {"makes_use_of": classifier} filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) )
def test_makes_use_of_filtering(self, seed_, strategies): """ Test equivalent filtering using two approaches. This needs to be seeded as some players classification is random. """ classifiers = [["game"], ["length"], ["game", "length"]] for classifier in classifiers: seed(seed_) comprehension = set( [ s for s in strategies if set(classifier).issubset(set(s().classifier["makes_use_of"])) ] ) seed(seed_) filterset = {"makes_use_of": classifier} filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) )
def filter_strategies(request): """ Take the incoming request object, convert the strings in its query_params dictionary into the types required by the axelrod filtering function and pass the resulting dictionary into that filtering function. """ params = request.query_params filter_types = { strtobool: [ 'stochastic', 'long_run_time', 'manipulates_state', 'manipulates_source', 'inpsects_source' ], int: [ 'memory_depth', 'min_memory_depth', 'max_memory_depth' ], } filterset = { _filter: convert_type(params[_filter]) for convert_type, filters in filter_types.items() for _filter in filters if _filter in params } if 'makes_use_of' in params: filterset['makes_use_of'] = params.getlist('makes_use_of') return axl.filtered_strategies(filterset)
def test_filtered_strategies(self): class StochasticTestStrategy(object): classifier = { "stochastic": True, "memory_depth": float("inf"), "makes_use_of": [], } class MemoryDepth2TestStrategy(object): classifier = {"stochastic": False, "memory_depth": 2, "makes_use_of": []} class UsesLengthTestStrategy(object): classifier = { "stochastic": True, "memory_depth": float("inf"), "makes_use_of": ["length"], } strategies = [ StochasticTestStrategy, MemoryDepth2TestStrategy, UsesLengthTestStrategy, ] stochastic_filterset = {"stochastic": True} deterministic_filterset = {"stochastic": False} uses_length_filterset = {"stochastic": True, "makes_use_of": ["length"]} self.assertEqual( filtered_strategies(stochastic_filterset, strategies), [StochasticTestStrategy, UsesLengthTestStrategy], ) self.assertEqual( filtered_strategies(deterministic_filterset, strategies), [MemoryDepth2TestStrategy], ) self.assertEqual( filtered_strategies(uses_length_filterset, strategies), [UsesLengthTestStrategy], )
def test_makes_use_of_filtering(self): classifiers = [['game'], ['length'], ['game', 'length']] for classifier in classifiers: comprehension = set([ s for s in all_strategies if set(classifier).issubset(set(s.classifier['makes_use_of'])) ]) filterset = {'makes_use_of': classifier} filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered)
def test_boolean_filtering(self): classifiers = [ 'stochastic', 'long_run_time', 'manipulates_state', 'manipulates_source', 'inspects_source' ] for classifier in classifiers: comprehension = set( [s for s in all_strategies if s.classifier[classifier]]) filterset = {classifier: True} filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered)
def test_memory_depth_filtering( self, min_memory_depth, max_memory_depth, memory_depth, strategies ): min_comprehension = set( [ s for s in strategies if s().classifier["memory_depth"] >= min_memory_depth ] ) min_filterset = {"min_memory_depth": min_memory_depth} min_filtered = set(filtered_strategies(min_filterset, strategies=strategies)) self.assertEqual(min_comprehension, min_filtered) max_comprehension = set( [ s for s in strategies if s().classifier["memory_depth"] <= max_memory_depth ] ) max_filterset = {"max_memory_depth": max_memory_depth} max_filtered = set(filtered_strategies(max_filterset, strategies=strategies)) self.assertEqual(max_comprehension, max_filtered) comprehension = set( [ s for s in strategies if s().classifier["memory_depth"] == memory_depth ] ) filterset = {"memory_depth": memory_depth} filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered)
def selected_players(extra_players=fsm_players): """ Return a list of all players used in this paper """ filterset = {"long_run_time": False, "manipulates_source": False, "manipulates_state": False, "inspects_source": False} players = [s() for s in axl.filtered_strategies(filterset)] if extra_players is not None: players += extra_players return players
def test_boolean_filtering(self, strategies): classifiers = [ "stochastic", "long_run_time", "manipulates_state", "manipulates_source", "inspects_source", ] for classifier in classifiers: comprehension = set([s for s in strategies if s.classifier[classifier]]) filterset = {classifier: True} filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered)
def test_boolean_filtering(self, strategies): classifiers = [ "stochastic", "long_run_time", "manipulates_state", "manipulates_source", "inspects_source", ] for classifier in classifiers: comprehension = set(filter(axl.Classifiers[classifier], strategies)) filterset = {classifier: True} filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered)
def selected_players(extra_players=fsm_players): """ Return a list of all players used in this paper """ filterset = { "long_run_time": False, "manipulates_source": False, "manipulates_state": False, "inspects_source": False } players = [s() for s in axl.filtered_strategies(filterset)] if extra_players is not None: players += extra_players return players
def test_makes_use_of_filtering(self): classifiers = [ ['game'], ['length'], ['game', 'length'] ] for classifier in classifiers: comprehension = set([ s for s in all_strategies if set(classifier).issubset(set(s.classifier['makes_use_of'])) ]) filterset = { 'makes_use_of': classifier } filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered)
def test_boolean_filtering(self): classifiers = [ 'stochastic', 'long_run_time', 'manipulates_state', 'manipulates_source', 'inspects_source'] for classifier in classifiers: comprehension = set([ s for s in all_strategies if s.classifier[classifier]]) filterset = { classifier: True } filtered = set(filtered_strategies(filterset)) self.assertEqual(comprehension, filtered)
def test_makes_use_of_filtering(self, strategies): """ Test equivalent filtering using two approaches. """ classifiers = [["game"], ["length"], ["game", "length"]] for classifier in classifiers: comprehension = set( [ s for s in strategies if set(classifier).issubset(set(axl.Classifiers["makes_use_of"](s))) ] ) filterset = {"makes_use_of": classifier} filtered = set(axl.filtered_strategies(filterset, strategies=strategies)) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) )
def who_is_playing(num_of_opponents, long_run_strategies=False): """ A function to choose which strategies will be playing against the Defector, where: 'num_of_opponents' is a numeric variable which states how many players will be competing (EXCLUDING the Defector); and 'long_run_strategies' is a Boolean variable which states whether strategies which have a long running time should be included in the competitors or not. It has a default value of False. A list containing the selected strategies is returned. """ if long_run_strategies == True: filterstrategies = { "manipulates_state": False, "manipulates_source": False, "inspects_source": False, } else: filterstrategies = { "long_run_time": False, "manipulates_state": False, "manipulates_source": False, "inspects_source": False, } filtered_strategies = axl.filtered_strategies(filterstrategies) filtered_strategies.remove(axl.Defector) opponent_strategies = random.sample(filtered_strategies, num_of_opponents) list_of_players = [opponent() for opponent in opponent_strategies] list_of_players.append(axl.Defector()) return list_of_players