### Install Development Dependencies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/setting_up_the_environment.md Installs all development dependencies for the project. It is recommended to do this within a virtual environment. ```bash pip install .[development] ``` -------------------------------- ### Install Axelrod with Human Strategy Support Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/installation.md Install Axelrod along with the 'prompt_toolkit' library to enable the manual Human strategy for interactive gameplay. This command installs the core library and the extra dependency. ```bash pip install axelrod[Human] ``` -------------------------------- ### Access Demonstration Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_collections_of_strategies.md Get a list of 5 simple strategies suitable for demonstration purposes. ```python >>> axl.demo_strategies # 5 simple strategies useful for demonstration. [...] ``` -------------------------------- ### Install Axelrod from Source Source: https://github.com/axelrod-python/axelrod/blob/dev/README.rst Clone the Axelrod repository and install from source. This method is useful for developers or for obtaining the latest unreleased version. ```bash git clone https://github.com/Axelrod-Python/Axelrod.git cd Axelrod python setup.py install ``` -------------------------------- ### TitForTat Strategy Implementation Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/writing_the_new_strategy.md Example of a basic strategy implementation. It cooperates on the first move and then mimics the opponent's previous move. This serves as a clear example for new contributors. ```python class TitForTat(Player): """ A player starts by cooperating and then mimics previous move by opponent. Note that the code for this strategy is written in a fairly verbose way. This is done so that it can serve as an example strategy for those who might be new to Python. Names - Rapoport's strategy: [Axelrod1980]_ - TitForTat: [Axelrod1980]_ """ # These are various properties for the strategy name = 'Tit For Tat' classifier = { 'memory_depth': 1, # Four-Vector = (1.,0.,1.,0.) 'stochastic': False, 'inspects_source': False, 'manipulates_source': False, 'manipulates_state': False } def strategy(self, opponent): """This is the actual strategy""" # First move if len(self.history) == 0: return C # React to the opponent's last move if opponent.history[-1] == D: return D return C ``` -------------------------------- ### Install Axelrod using pip Source: https://github.com/axelrod-python/axelrod/blob/dev/README.rst Use pip to install the Axelrod library. This is the simplest method for installation. ```bash pip install axelrod ``` -------------------------------- ### InitialTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply InitialTransformer to make a player play a specific sequence of moves at the beginning of the tournament. ```python >>> InitiallyCooperatingDefector = InitialTransformer([C, C])(axl.Defector) >>> player = InitiallyCooperatingDefector() ``` -------------------------------- ### NiceTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply NiceTransformer to prevent a strategy from defecting if the opponent has not yet defected. ```python >>> NiceDefector = NiceTransformer()(axl.Defector) >>> player = NiceDefector() ``` -------------------------------- ### Build and Play a Tournament Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/index.md Create a tournament with a list of players and play the tournament to get ranked results. ```python >>> import axelrod as axl >>> players = (axl.Cooperator(), axl.Alternator(), axl.TitForTat()) >>> tournament = axl.Tournament(players) >>> results = tournament.play() >>> results.ranked_names ['Alternator', 'Tit For Tat', 'Cooperator'] ``` -------------------------------- ### Instantiating Players Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/reference/glossary.md Players are instances of strategy classes. This example shows how to create player instances for the Cooperator and Defector strategies and print their representations. ```python >>> p1, p2 = axl.Cooperator(), axl.Defector() >>> p1 Cooperator >>> p2 Defector ``` -------------------------------- ### Run a Basic Axelrod Tournament Source: https://github.com/axelrod-python/axelrod/blob/dev/README.rst This example demonstrates how to set up and play a basic tournament using Axelrod. It involves importing the library, creating players from demo strategies, initializing a tournament, and playing it. ```python import axelrod as axl players = [s() for s in axl.demo_strategies] # Create players tournament = axl.Tournament(players, seed=1) # Create a tournament results = tournament.play() # Play the tournament results.ranked_names ``` -------------------------------- ### Example CSV Data Structure Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/read_and_write_interactions.md This is an example of the data structure saved to the CSV file. It includes details about each interaction, player, and game statistics. ```text Interaction index,Player index,Opponent index,Repetition,Player name,Opponent name,Actions,Score,Score difference,Turns,Score per turn,Score difference per turn,Win,Initial cooperation,Cooperation count,CC count,CD count,DC count,DD count,CC to C count,CC to D count,CD to C count,CD to D count,DC to C count,DC to D count,DD to C count,DD to D count,Good partner 0,0,0,0,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 0,0,0,0,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 1,0,0,1,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 1,0,0,1,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 2,0,1,0,Alternator,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 2,1,0,0,Anti Tit For Tat,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 3,0,1,1,Alternator,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 3,1,0,1,Anti Tit For Tat,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 4,0,2,0,Alternator,Bully,CDCD,5,-5,4,1.25,-1.25,0,True,2,1,1,0,2,0,1,0,1,0,0,1,0,1 4,2,0,0,Bully,Alternator,DDCD,10,5,4,2.5,1.25,1,False,1,1,0,1,2,0,1,0,0,0,1,1,0,0 5,0,2,1,Alternator,Bully,CDCD,5,-5,4,1.25,-1.25,0,True,2,1,1,0,2,0,1,0,1,0,0,1,0,1 5,2,0,1,Bully,Alternator,DDCD,10,5,4,2.5,1.25,1,False,1,1,0,1,2,0,1,0,0,0,1,1,0,0 6,0,3,0,Alternator,Cooperator,CDCD,16,10,4,4.0,2.5,1,True,2,2,0,2,0,0,2,0,0,1,0,0,0,0 6,3,0,0,Cooperator,Alternator,CCCC,6,-10,4,1.5,-2.5,0,True,4,2,2,0,0,2,0,1,0,0,0,0,0,1 7,0,3,1,Alternator,Cooperator,CDCD,16,10,4,4.0,2.5,1,True,2,2,0,2,0,0,2,0,0,1,0,0,0,0 7,3,0,1,Cooperator,Alternator,CCCC,6,-10,4,1.5,-2.5,0,True,4,2,2,0,0,2,0,1,0,0,0,0,0,1 8,0,4,0,Alternator,Cycler DC,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1 8,4,0,0,Cycler DC,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1 9,0,4,1,Alternator,Cycler DC,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1 9,4,0,1,Cycler DC,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1 10,0,5,0,Alternator,Defector,CDCD,2,-10,4,0.5,-2.5,0,True,2,0,2,0,2,0,0,0,2,0,0,1,0,1 10,5,0,0,Defector,Alternator,DDDD,12,10,4,3.0,2.5,1,False,0,0,0,2,2,0,0,0,0,0,2,0,1,0 11,0,5,1,Alternator,Defector,CDCD,2,-10,4,0.5,-2.5,0,True,2,0,2,0,2,0,0,0,2,0,0,1,0,1 11,5,0,1,Defector,Alternator,DDDD,12,10,4,3.0,2.5,1,False,0,0,0,2,2,0,0,0,0,0,2,0,1,0 12,0,6,0,Alternator,Grudger,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1 12,6,0,0,Grudger,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1 13,0,6,1,Alternator,Grudger,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1 13,6,0,1,Grudger,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1 14,0,7,0,Alternator,Suspicious Tit For Tat,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1 14,7,0,0,Suspicious Tit For Tat,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1 15,0,7,1,Alternator,Suspicious Tit For Tat,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1 15,7,0,1,Suspicious Tit For Tat,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1 16,0,8,0,Alternator,Tit For Tat,CDCD,13,5,4,3.25,1.25,1,True,2,1,1,2,0,0,1,0,1,1,0,0,0,0 16,8,0,0,Tit For Tat,Alternator,CCDC,8,-5,4,2.0,-1.25,0,True,3,1,2,1,0,1,0,0,1,1,0,0,0,1 17,0,8,1,Alternator,Tit For Tat,CDCD,13,5,4,3.25,1.25,1,True,2,1,1,2,0,0,1,0,1,1,0,0,0,0 17,8,0,1,Tit For Tat,Alternator,CCDC,8,-5,4,2.0,-1.25,0,True,3,1,2,1,0,1,0,0,1,1,0,0,0,1 18,0,9,0,Alternator,Win-Shift Lose-Stay: D,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1 18,9,0,0,Win-Shift Lose-Stay: D,Alternator,DCCD,9,0,4,2.25,0.0,0,False,2,1,1,1,1,0,1,1,0,1,0,0,0,1 19,0,9,1,Alternator,Win-Shift Lose-Stay: D,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1 19,9,0,1,Win-Shift Lose-Stay: D,Alternator,DCCD,9,0,4,2.25,0.0,0,False,2,1,1,1,1,0,1,1,0,1,0,0,0,1 20,0,10,0,Alternator,Win-Stay Lose-Shift: C,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1 20,10,0,0,Win-Stay Lose-Shift: C,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1 21,0,10,1,Alternator,Win-Stay Lose-Shift: C,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1 21,10,0,1,Win-Stay Lose-Shift: C,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1 22,1,1,0,Anti Tit For Tat,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1 ``` -------------------------------- ### Strategy Docstring Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/writing_the_new_strategy.md A concise docstring explaining the strategy's behavior. It should clearly state the strategy's logic. ```python """A player starts by cooperating and then mimics previous move by opponent.""" ``` -------------------------------- ### FlipTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply FlipTransformer to invert all actions of a given strategy. ```python >>> FlippedCooperator = FlipTransformer()(axl.Cooperator) >>> player = FlippedCooperator() ``` -------------------------------- ### Run a Tournament and Get Results Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md This code snippet sets up a tournament with specified players and parameters, then runs the tournament and retrieves the results object. ```python import axelrod as axl players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()] tournament = axl.Tournament(players, turns=10, repetitions=3) results = tournament.play() ``` -------------------------------- ### Run All Doctests Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/running_tests.md Execute all doctests within the project's documentation. This ensures that examples in the documentation are accurate and functional. ```bash python doctests.py ``` -------------------------------- ### JossAnnTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use JossAnnTransformer to introduce probabilities for cooperating and defecting, falling back to the original strategy otherwise. ```python >>> JossAnnTFT = JossAnnTransformer((0.2, 0.3))(axl.TitForTat) >>> player = JossAnnTFT() ``` -------------------------------- ### NoisyTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use NoisyTransformer to introduce a probability of flipping actions. ```python >>> NoisyCooperator = NoisyTransformer(0.5)(axl.Cooperator) >>> player = NoisyCooperator() ``` -------------------------------- ### Example Test Class for TitForTat Strategy Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/writing_test_for_the_new_strategy.md A comprehensive example of a test class for the Tit-For-Tat strategy, demonstrating various testing scenarios including initial plays, playing against different opponents, and using mock players. ```python import axelrod from test_player import TestPlayer C, D = axelrod.Action.C, axelrod.Action.D class TestTitForTat(TestPlayer): """ Note that this test is referred to in the documentation as an example on writing tests. If you modify the tests here please also modify the documentation. """ name = "Tit For Tat" player = axelrod.TitForTat expected_classifier = { 'memory_depth': 1, 'stochastic': False, 'makes_use_of': set(), 'inspects_source': False, 'manipulates_source': False, 'manipulates_state': False } def test_strategy(self): self.first_play_test(C) self.second_play_test(rCC=C, rCD=D, rDC=C, rDD=D) # Play against opponents actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions) actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions) # This behaviour is independent of knowledge of the Match length actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, match_attributes={"length": float("inf")}) # We can also test against random strategies actions = [(C, D), (D, D), (D, C), (C, C)] self.versus_test(axelrod.Random(), expected_actions=actions, seed=0) actions = [(C, C), (C, D), (D, D), (D, C)] self.versus_test(axelrod.Random(), expected_actions=actions, seed=1) # If you would like to test against a sequence of moves you should use # a MockPlayer opponent = axelrod.MockPlayer(actions=[C, D]) actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions) opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D]) actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions) ``` -------------------------------- ### Play a Match with Default Information Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/set_player_information.md Create and play a match with default player information. This example shows a match between Alternator and FirstBySteinAndRapoport for 5 turns. ```python >>> import axelrod as axl >>> players = (axl.Alternator(), axl.FirstBySteinAndRapoport()) >>> axl.Match(players, turns=5).play() [(C, C), (D, C), (C, C), (D, D), (C, D)] ``` -------------------------------- ### ForgiverTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply ForgiverTransformer to introduce a probability of flipping defections. ```python >>> ForgivinDefector = ForgiverTransformer(0.1)(axl.Defector) >>> player = ForgivinDefector() ``` -------------------------------- ### DualTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use DualTransformer to create a strategy that plays the exact opposite moves of another strategy given the same history. ```python >>> DualWSLS = DualTransformer()(axl.WinStayLoseShift) >>> player = DualWSLS() ``` -------------------------------- ### RetaliationTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply RetaliationTransformer to make a player retaliate a specified number of times after a defection. ```python >>> TwoTitsForTat = RetaliationTransformer(2)(axl.Cooperator) >>> player = TwoTitsForTat() ``` -------------------------------- ### Access All Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_collections_of_strategies.md Get a comprehensive list containing all strategies available within the Axelrod library. ```python >>> axl.all_strategies [...] ``` -------------------------------- ### Create Transformer with StrategyTransformerFactory (with args) Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use `StrategyTransformerFactory` with a `strategy_wrapper` function to create a transformer. This example shows how to create a transformed class that can be instantiated with arguments. ```python TransformedClass = StrategyTransformerFactory(generic_strategy_wrapper) Cooperator2 = TransformedClass(*args, **kwargs)(axl.Cooperator) ``` -------------------------------- ### Create and Play a Match Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/index.md Instantiate two players, create a match between them for a specified number of rounds, and play the match to get interactions. ```python >>> import axelrod as axl >>> players = (axl.Alternator(), axl.TitForTat()) >>> match = axl.Match(players, 5) >>> interactions = match.play() >>> interactions [(C, C), (D, C), (C, D), (D, C), (C, D)] ``` -------------------------------- ### Simulating a Match Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/reference/glossary.md A match is a series of turns between two players. This example shows how to create a Match object, play it for a specified number of turns, and inspect the results and player histories. ```python >>> p1, p2 = axl.Cooperator(), axl.Defector() >>> match = axl.Match((p1, p2), turns=3) >>> result = match.play() >>> result [(C, D), (C, D), (C, D)] >>> p1.history, p2.history ([C, C, C], [D, D, D]) ``` -------------------------------- ### Run All Tests with Coverage Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/running_tests.md Execute all tests while measuring code coverage for the entire library. Requires the 'coverage' package to be installed. ```bash coverage run --source=axelrod -m pytest . ``` -------------------------------- ### Run Doctests on a Specific File Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/running_tests.md Execute doctests for a single specified file. This is useful for verifying documentation examples in a particular file. ```bash python -m doctest docs/tutorials/getting_started/match.rst ``` -------------------------------- ### Implement and Use Memory-One Strategies Source: https://context7.com/axelrod-python/axelrod/llms.txt This section covers memory-one strategies, defined by their response probabilities to the previous round's outcomes. It shows how to instantiate Win-Stay Lose-Shift (Pavlov), a generic memory-one player with a custom four-vector, and Zero-Determinant strategies like ZDExtort2 and GTFT. It also includes examples of comparing strategy performance in matches. ```python import axelrod as axl # Win-Stay Lose-Shift (Pavlov) -- a classic memory-one strategy wsls = axl.WinStayLoseShift() print(wsls.classifier["memory_depth"]) # 1 ``` ```python # Generic memory-one player with custom four-vector # Generous Tit-For-Tat: (1, 1/3, 1, 1/3) generous_tft = axl.MemoryOnePlayer(four_vector=(1, 1/3, 1, 1/3)) print(generous_tft) # Generic Memory One Player: (1, 0.333..., 1, 0.333...) ``` ```python # Zero-determinant strategies (extortionate) extort = axl.ZDExtort2() # ZD strategy that guarantees advantage godfather = axl.GTFT() # Generous Tit For Tat (a ZD strategy) ``` ```python # Compare performance in a match match = axl.Match((axl.WinStayLoseShift(), axl.TitForTat()), turns=100, seed=0) match.play() print(match.final_score_per_turn()) # (3.0, 3.0) — both score well cooperating print(match.normalised_cooperation()) # both ~1.0 ``` ```python match2 = axl.Match((axl.ZDExtort2(), axl.Cooperator()), turns=100, seed=0) match2.play() print(match2.final_score_per_turn()) # extorter outscores cooperator ``` -------------------------------- ### Demonstrate Stochastic Classifier Behavior Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/classifying_the_new_strategy.md This example illustrates how the 'stochastic' classifier can differ between strategy instances based on initialization parameters. It shows that a strategy with a deterministic setting (p=1) is not considered stochastic. ```default >>> joss = axelrod.FirstByJoss() >>> boring_joss = axelrod.FirstByJoss(p=1) >>> axelrod.Classifiers["stochastic"](joss) True >>> axelrod.Classifiers["stochastic"](boring_joss) False ``` -------------------------------- ### Inspect TitForTat Strategy Classifiers Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/classifying_the_new_strategy.md This example shows how to access and print the sorted keys of the classifier dictionary for the TitForTat strategy. This helps in understanding the available classification dimensions. ```default >>> import axelrod >>> classifier = axelrod.TitForTat.classifier >>> for key in sorted(classifier.keys()): ... print(key) inspects_source long_run_time manipulates_source manipulates_state memory_depth stochastic ``` -------------------------------- ### Instantiate Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_collections_of_strategies.md Import the library and instantiate specific strategies like TitForTat or Cooperator. ```python >>> import axelrod as axl >>> axl.TitForTat() Tit For Tat >>> axl.Cooperator() Cooperator ``` -------------------------------- ### Create Player List and Masses Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/creating_heterogenous_player_moran_process/index.md Initializes a list of demo strategies and creates a corresponding list of masses for heterogeneous player attributes. ```python import axelrod as axl players = [player() for player in axl.demo_strategies] masses = [i for i in range(len(players))] players ``` -------------------------------- ### Create and run a basic tournament Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/tournament.md Initializes a list of players with simple strategies, creates a tournament, plays it, and displays the ranked results. ```python import axelrod as axl players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()] players ``` ```python tournament = axl.Tournament(players) results = tournament.play() results.ranked_names ``` -------------------------------- ### Create and Play a Tournament Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/run_axelrods_ecological_variant.md Set up a list of players, create a tournament, play the tournament, and then create an ecosystem from the results. The ecosystem can then be evolved over a specified number of time steps. ```python import axelrod as axl players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger(), axl.Random()] tournament = axl.Tournament(players) results = tournament.play() eco = axl.Ecosystem(results) eco.reproduce(100) # Evolve the population over 100 time steps ``` -------------------------------- ### RetaliateUntilApologyTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use RetaliateUntilApologyTransformer to add TitForTat-style retaliation behavior. ```python >>> TFT = RetaliateUntilApologyTransformer()(axl.Cooperator) >>> player = TFT() ``` -------------------------------- ### Create and inspect an empty cache Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_a_cache.md Demonstrates how to initialize an empty DeterministicCache and check its current size. ```default >>> cache = axl.DeterministicCache() >>> len(cache) 0 ``` -------------------------------- ### Docstring with Bibliography/Source Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/docstrings.md Demonstrates how to document the origin of a strategy using the 'Names' section. Reference bibliographic items using the specified format. ```default class TitForTat(Player): """ A player starts by cooperating and then mimics the previous action of the opponent. Names: - Rapoport's strategy: [Axelrod1980]_ - TitForTat: [Axelrod1980]_ """ ``` -------------------------------- ### Instantiate FSMPlayer with Transitions Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/discussion/strategy_archetypes.md Creates an instance of the FSMPlayer strategy using a defined set of transitions, an initial state, and an initial action. This allows for the creation of complex strategies based on state. ```python >>> from axelrod.strategies.finite_state_machines import FSMPlayer >>> grudger_2 = FSMPlayer(transitions=grudger_2_transitions, ... initial_state=1, initial_action=C) ``` -------------------------------- ### DeadlockBreakingTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply DeadlockBreakingTransformer to a strategy to help break deadlocks by cooperating. ```python >>> DeadlockBreakingTFT = DeadlockBreakingTransformer()(axl.TitForTat) >>> player = DeadlockBreakingTFT() ``` -------------------------------- ### Create and Run a Tournament with Random Player Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/visualising_results.md Sets up a tournament with standard players and adds a random player. This is a prerequisite for visualising results. ```python import axelrod as axl players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()] players.append(axl.Random()) tournament = axl.Tournament(players) results = tournament.play() ``` -------------------------------- ### GrudgeTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use GrudgeTransformer to make a player defect unconditionally after a specified number of defections. ```python >>> GrudgingCooperator = GrudgeTransformer(2)(axl.Cooperator) >>> player = GrudgingCooperator() ``` -------------------------------- ### Get Eigenmoses Rating Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md Retrieves the Eigenmoses rating, a specific morality metric calculated for each player. ```python >>> results.eigenmoses_rating [0.37..., -0.37..., 0.59..., 0.59...] ``` -------------------------------- ### Count Available Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/index.md Import the axelrod library and get the total count of available strategies. ```python >>> import axelrod as axl >>> len(axl.strategies) 243 ``` -------------------------------- ### Create and Play a Basic Match Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/match.md Use the `Match` class to create a match between two players for a specified number of turns. Call `play()` to execute the match and view the results. ```python >>> import axelrod as axl >>> players = (axl.Cooperator(), axl.Alternator()) >>> match = axl.Match(players, 5) >>> match.play() [(C, C), (C, D), (C, C), (C, D), (C, C)] ``` -------------------------------- ### Access Cheating Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_collections_of_strategies.md Retrieve strategies that are designed to 'cheat', for example, by modifying opponents' source code. ```python >>> axl.cheating_strategies [...] ``` -------------------------------- ### Create Tournament with Main Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_collections_of_strategies.md Use the list of main strategies to create a tournament by instantiating each strategy and passing them to the Tournament class. ```python >>> players = [s() for s in axl.strategies] >>> tournament = axl.Tournament(players) ``` -------------------------------- ### TrackHistoryTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Apply TrackHistoryTransformer to enable a player to internally track its history, useful for detecting noise or other patterns. ```python >>> player = TrackHistoryTransformer()(axl.Random)()) ``` -------------------------------- ### Docstring with Strategy Working Description Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/docstrings.md Shows how to include a brief summary of the strategy's mechanics. This section should explain how the player operates. ```default class TitForTat(Player): """ A player starts by cooperating and then mimics the previous action of the opponent. """ ``` -------------------------------- ### FinalTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use FinalTransformer to specify a sequence of moves for the end of a tournament. This requires the tournament length to be known. ```python >>> FinallyDefectingCooperator = FinalTransformer([D, D])(axl.Cooperator) >>> player = FinallyDefectingCooperator() ``` -------------------------------- ### Get Individual Turn Scores Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/match.md After playing a match, use the `scores()` method to retrieve a list of scores for each individual turn. ```python >>> import axelrod as axl >>> players = (axl.Cooperator(), axl.Alternator()) >>> match = axl.Match(players, 25) >>> match.play() [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (C, C)] >>> match.scores() [(3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3), (0, 5), (3, 3)] ``` -------------------------------- ### Play Standard Moran Process for Comparison Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/create_approximate_moran_processes.md Play a standard Moran Process with the same players and seed for comparison with the Approximate Moran Process. This highlights potential differences in outcomes. ```python mp = axl.MoranProcess(players, seed=5) results = mp.play() mp.population_distribution() ``` -------------------------------- ### Use a pre-built cache in a Moran Process Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_a_cache.md Initialize a MoranProcess with a pre-loaded DeterministicCache from a file. The cache will be used for matches within the process and potentially augmented. ```default >>> cache = axl.DeterministicCache("cache.txt") >>> players = [axl.GoByMajority(), axl.Alternator(), ... axl.Cooperator(), axl.Grudger()] >>> mp = axl.MoranProcess(players, deterministic_cache=cache) >>> populations = mp.play() >>> mp.winning_strategy_name Defector ``` -------------------------------- ### Get Cooperating Rating Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md Retrieves the cooperation rating for each player. This metric is derived from the cooperation rate and provides a normalized score. ```python >>> results.cooperating_rating [1.0, 0.0, 0.7, 0.7] ``` -------------------------------- ### Create Transformer with StrategyTransformerFactory (no args) Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Instantiate a transformer using `StrategyTransformerFactory` when the wrapper requires no arguments. This allows direct application to a strategy class. ```python >>> TransformedClass = StrategyTransformerFactory(generic_strategy_wrapper)() >>> Cooperator2 = TransformedClass(axl.Cooperator) ``` -------------------------------- ### Get Eigenjesus Rating Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md Retrieves the Eigenjesus rating, another morality metric that assesses player behavior based on specific criteria. ```python >>> results.eigenjesus_rating [0.57..., 0.0, 0.57..., 0.57...] ``` -------------------------------- ### Run a basic Moran Process simulation Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/moran.md Initializes a Moran process with a given set of players and runs the simulation. Access the winning strategy name and the number of rounds after playing. ```python import axelrod as axl players = [axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger()] mp = axl.MoranProcess(players, seed=1) populations = mp.play() mp.winning_strategy_name ``` ```python len(mp) ``` -------------------------------- ### Initialize Graph with Edges Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/create_moran_processes_on_graphs.md Initialize a graph object by providing a list of edges. Nodes can be any hashable object. ```default edges = [(source_1, target1), (source2, target2), ...] ``` ```python >>> import axelrod as axl >>> from axelrod.graph import Graph >>> edges = [(0, 1), (1, 2), (2, 3), (3, 1)] >>> graph = Graph(edges) ``` -------------------------------- ### Access Long Run Time Strategies Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_collections_of_strategies.md Get a list of strategies that have a high computational cost and may take longer to run. ```python >>> axl.long_run_time_strategies # These have a high computational cost [...] ``` -------------------------------- ### ApologyTransformer Example Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/use_strategy_transformers.md Use ApologyTransformer to make a player apologize after specific sequences of opponent and player moves. The sequences can be passed as lists of actions. ```python >>> ApologizingDefector = ApologyTransformer([D], [C])(axl.Defector) >>> player = ApologizingDefector() ``` ```python >>> ApologizingDefector = ApologyTransformer([D, D], [C, C])(axl.Defector) >>> player = ApologizingDefector() ``` -------------------------------- ### Initialize Tournament with Probabilistic Endings Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/include_probabilistic_endings.md Create a tournament instance with a specified probability of ending each match. This allows for variable match lengths. ```default >>> import axelrod as axl >>> players = [axl.Cooperator(), axl.Defector(), ... axl.TitForTat(), axl.Grudger()] >>> tournament = axl.Tournament(players, prob_end=0.5) ``` -------------------------------- ### Testing Strategy With Initialization Keywords Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/writing_test_for_the_new_strategy.md Test how your strategy behaves when initialized with specific keyword arguments using the `init_kwargs` parameter in `versus_test`. This is crucial for strategies with configurable parameters. ```python actions = [(C, C), (C, D), (C, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, init_kwargs={"p": 1}) ``` -------------------------------- ### Filter Strategies by Stochastic Property Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/classify_strategies.md Identify all stochastic strategies by defining a filterset and passing it to the `filtered_strategies` function. This example returns the count of such strategies. ```python >>> filterset = { ... 'stochastic': True ... } >>> strategies = axl.filtered_strategies(filterset) >>> len(strategies) 88 ``` -------------------------------- ### Get Initial Cooperation Rates Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md Calculates the rate at which each strategy cooperates during the first turn of matches. A rate of 1.0 indicates consistent cooperation. ```python >>> results.initial_cooperation_rate [1.0, 0.0, 1.0, 1.0] ``` -------------------------------- ### Basic Docstring Structure Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/strategy/docstrings.md Illustrates the fundamental multi-line docstring format. Use this for general descriptions. ```default """This is a docstring. It can be written over multiple lines. """ ``` -------------------------------- ### Run a Match with Custom Game Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/implement_new_games/index.md This snippet demonstrates how to set up and play a match between two players using a custom game, like Rock-Paper-Scissors. It specifies the players, number of turns, and the game object. ```python match = axl.Match(players=(Copycat(starting_move=P), Rotator()), turns=5, game=rock_paper_scissors) match.play() ``` -------------------------------- ### Run Moran Process with custom fitness transformation Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/tutorials/new_to_game_theory_and_or_python/moran.md Initializes a Moran process with a custom fitness transformation function, scaling utility values based on the intensity of selection 'w'. The simulation runs for a specified number of turns. ```python players = (axl.Cooperator(), axl.Defector(), axl.Defector(), axl.Defector()) w = 0.95 fitness_transformation = lambda score: 1 - w + w * score mp = axl.MoranProcess(players, turns=10, fitness_transformation=fitness_transformation, seed=3) populations = mp.play() mp.winning_strategy_name ``` -------------------------------- ### Filter Strategies by Memory Depth Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/classify_strategies.md Find strategies that use a specific amount of memory for decision-making. This example counts strategies with a memory depth of 1. ```python >>> filterset = { ... 'memory_depth': 1 ... } >>> strategies = axl.filtered_strategies(filterset) >>> len(strategies) 33 ``` -------------------------------- ### Get Good Partner Rating Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md Retrieves the rating for being a 'good partner', normalized to reflect a player's tendency to cooperate and maintain positive interactions. ```python >>> pprint.pprint(results.good_partner_rating) [1.0, 0.0, 1.0, 1.0] ``` -------------------------------- ### Get Initial Cooperation Counts Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/access_tournament_results.md Retrieves the count of cooperations made by each player in the first turn of every match. This helps understand early-game strategy. ```python >>> results.initial_cooperation_count [9, 0, 9, 9] ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/contributing/setting_up_the_environment.md Creates a new virtual environment named 'axelrod_development' and activates it. This isolates project dependencies. ```bash python -m venv axelrod_development source axelrod_development/bin/activate ``` -------------------------------- ### Get Ranked Player Names Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/include_probabilistic_endings.md Obtain the ranked list of player names based on tournament performance. This is similar to standard tournament result retrieval. ```default >>> results.ranked_names ['Defector', 'Tit For Tat', 'Grudger', 'Cooperator'] ``` -------------------------------- ### Create and Play Graph-Based Moran Process Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/create_moran_processes_on_graphs.md Create a Moran process using a graph for interactions and play the process. The population distribution can then be inspected. ```python >>> from axelrod.graph import Graph >>> edges = [(0, 1), (1, 2), (2, 3), (3, 1)] >>> graph = Graph(edges) >>> players = [axl.Cooperator(), axl.Cooperator(), axl.Cooperator(), axl.Defector()] >>> mp = axl.MoranProcess(players, interaction_graph=graph, seed=40) >>> results = mp.play() >>> mp.population_distribution() Counter({'Defector': 4}) ``` -------------------------------- ### Get Classifier Dictionary for Cooperator Strategy Source: https://github.com/axelrod-python/axelrod/blob/dev/docs/how-to/classify_strategies.md Access the classifier dictionary for a strategy class to view its properties. The instance of the class also has a copy of this classifier. ```python >>> import axelrod as axl >>> expected_dictionary = { ... 'manipulates_state': False, ... '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 >>> axl.Cooperator.classifier == expected_dictionary True ``` ```python >>> s = axl.Cooperator() >>> s.classifier == expected_dictionary True ```