def test_strategy(self): actions = [(C, C)] * 8 self.versus_test(axelrod.Cooperator(), expected_actions=actions, attrs={"opponent_class": "Cooperative"}) actions = [(C, D)] + [(D, D)] * 9 self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"opponent_class": "ALLD"}) opponent = axelrod.MockPlayer(actions=[C, D, D, D, D, D, D]) actions = [(C, C), (C, D)] + [(D, D)] * 5 + [(D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"}) opponent = axelrod.MockPlayer(actions=[C, C, D, D, D, D, D]) actions = [(C, C), (C, C), (C, D)] + [(D, D)] * 4 + [(D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"}) opponent = axelrod.MockPlayer(actions=[C, D, D, C, D, D, D]) actions = [(C, C), (C, D), (D, D), (D, C), (C, D), (D, D), (D, D), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "ALLD"}) opponent = axelrod.MockPlayer(actions=[C, D, D, C, C, D, D]) actions = [(C, C), (C, D), (D, D), (D, C), (C, C), (C, D), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "STFT"}) opponent = axelrod.MockPlayer(actions=[C, D, C, D, C, D, D]) actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "STFT"}) opponent = axelrod.MockPlayer(actions=[D, D, D, C, C, C, C]) actions = [(C, D), (D, D), (D, D), (D, C), (C, C), (C, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "STFT"}) opponent = axelrod.MockPlayer(actions=[C, C, C, C, D, D]) actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, D), (D, C), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "Random"}) opponent = axelrod.MockPlayer(actions=[D, D, C, C, C, C]) actions = [(C, D), (D, D), (D, C), (C, C), (C, C), (C, C), (D, D), (D, D)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_class": "Random"})
def test_strategy(self): # Tests that if opponent has played all C then player chooses C. actions = [(C, C)] * 10 self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) actions = [(D, C)] + [(C, C)] * 9 self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=2) # Tests that if opponent has played all D then player chooses D. actions = [(C, D)] + [(D, D)] * 9 self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) actions = [(D, D)] + [(D, D)] * 9 self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2) # Variable behaviour based on the history and stochastic actions = [ (C, C), (C, D), (D, C), (D, D), (C, C), (C, D), (C, C), (D, D), (D, C), (C, D), ] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) actions = [ (D, C), (C, D), (D, C), (C, D), (C, C), (D, D), (D, C), (D, D), (C, C), (D, D), ] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) opponent = axelrod.MockPlayer(actions=[C, C, D, D, D, D]) actions = [ (C, C), (C, C), (C, D), (D, D), (D, D), (C, D), (D, C), (D, C), (D, D), (D, D), ] self.versus_test(opponent, expected_actions=actions, seed=1) opponent = axelrod.MockPlayer(actions=[C, C, C, D, D, D]) actions = [ (D, C), (C, C), (C, C), (C, D), (D, D), (C, D), (C, C), (D, C), (D, C), (D, D), ] self.versus_test(opponent, expected_actions=actions, seed=2)
def test_strategy(self): # Cooperator Test opponent = axelrod.Cooperator() actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions) # Defector Test opponent = axelrod.Defector() actions = [(C, D), (D, D), (D, D), (D, D)] self.versus_test(opponent, expected_actions=actions) # Test increasing retaliation opponent = axelrod.MockPlayer([D, C]) actions = [(C, D), (D, C), (C, D), (D, C), (D, D), (D, C), (D, D), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={'is_retaliating': True, 'retaliation_length': 4, 'retaliation_remaining': 3}) opponent = axelrod.Cycler('DDCDD') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D)] self.versus_test(opponent, expected_actions=actions, attrs={'current_score': 34, 'opponent_score': 19, 'last_fresh_start': 0, 'retaliation_length': 6, 'retaliation_remaining': 2}) # When the length is given this strategy will not give a fresh start opponent = axelrod.Cycler('DDCDD') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, match_attributes={'length': 50}) # When the length is not given this strategy will give a fresh start. opponent = axelrod.Cycler('DDCDD') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, match_attributes={'length': float('inf')}) # Check standard deviation conditions. # The opponent is similar to the one above except the stddev condition # is not met, therefore no fresh start will be given. opponent = axelrod.Cycler('DDCDDDDCDDCDCCC') actions = [(C, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (D, D), (D, D), (D, C), (C, D), (D, C), (D, C), (D, C), (D, D), (D, D), (D, C), (D, D), (D, D), (D, D), (D, D), (D, C), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, attrs={'last_fresh_start': 0}) # Check the fresh start condition opponent = axelrod.TitForTat() actions = [(C, C), (C, C), (C, C), (C, C)] self.versus_test(opponent, expected_actions=actions, attrs={'fresh_start': False}) # check the fresh start condition: least 20 rounds since the last ‘fresh start’ opponent = axelrod.Cycler('CCCCD') actions = [(C, C), (C, C), (C, C), (C, C), (C, D), (D, C), (C, C), (C, C), (C, C), (C, D), (D, C), (D, C), (C, C), (C, C), (C, D), (D, C), (D, C), (D, C), (C, C), (C, D), (D, C), (D, C), (D, C), (C, C), (C, D), (D, C), (C, C), (C, C), (C, C), (C, D), (D, C), (D, C), (C, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions, match_attributes={'length': 35}, attrs={'current_score': 108, 'opponent_score': 78, 'last_fresh_start': 24, 'retaliation_length': 2, 'retaliation_remaining': 0})
def test_iter(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess((p1, p2)) self.assertEqual(mp.__iter__(), mp)
def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions)
def test_vs_defector(self): p1 = axelrod.MirrorMindReader() p2 = axelrod.Defector() self.assertEqual(p1.strategy(p2), D)
def test_next(self): players = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess(players) self.assertIsInstance(next(mp), MoranProcess)
import numpy as np from IPD_fixed import IPDEnv import axelrod import time import argparse import json import pandas as pd from pandas import DataFrame as df env = IPDEnv({}) # remove empty values config = {} agent= SMA2CAgent(env, config) encodings_before_c = df(np.array(agent.encode_run(axelrod.Cooperator()))) encodings_before_d = df(np.array(agent.encode_run(axl.Defector()))) encodings_before_tft = df(np.array(agent.encode_run(axl.TitForTat()))) print(encodings_before_c.shape) agent.load('outputs/1592391802-sma2c-4000-smaller-mid') encodings_after_c = df(np.array(agent.encode_run(axelrod.Cooperator()))) encodings_after_d = df(np.array(agent.encode_run(axelrod.Defector()))) encodings_after_tft = df(np.array(agent.encode_run(axl.TitForTat()))) encodings = [encodings_before_c, encodings_before_d, encodings_before_tft, encodings_after_c, encodings_after_d, encodings_after_tft] for i, df in enumerate(encodings): df.to_csv(f'{i}.csv') # json.dump([x.tolist() for x in [encodings_before, encodings_after_c, encodings_after_d]], open('encodings.json', 'w'))
def test_strategy3(self): actions = [(C, D), (D, D), (C, D), (D, D), (D, D)] self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=1)
def test_nice1(self): """Tests the NiceTransformer.""" p1 = NiceTransformer()(axl.Defector)() p2 = axl.Defector() self.versus_test(p1, p2, [C, D, D, D, D], [D, D, D, D, D])
def test_retailiating_cooperator_against_defector(self): """Tests the RetaliateTransformer.""" p1 = RetaliationTransformer(1)(axl.Cooperator)() p2 = axl.Defector() self.versus_test(p1, p2, [C, D, D, D, D], [D, D, D, D, D])
def test_grudging1(self): p1 = axl.Defector() p2 = GrudgeTransformer(1)(axl.Cooperator)() self.versus_test(p1, p2, [D, D, D, D], [C, C, D, D], seed=11)
# After third round, if previous 2 times of opponent are D then # return D if previous 3 times of opponent are C then return C if len(self.history) > 2: if list(opponent.history)[-2] == D and list(opponent.history)[-1] == D: return D elif list(opponent.history)[-3:len(self.history)] ==[C, C, C]: return C return C return C #============================================================================== # Tournament Running #============================================================================== # Store strategies that we need to test for the tornament into players players = [IDoWhatIWant(), axl.Random(), axl.TitForTat(), axl.Defector(), axl.BackStabber(), axl.FirstByDavis(), axl.Grudger(), axl.Gambler(), axl.EvolvedLookerUp2_2_2(), axl.Cooperator()] # Setting tournament with 200 turns and repetitions with 20 tournament = axl.Tournament(players, turns = 200, repetitions = 20) # Run the tournament and save the result into results results = tournament.play() # Output the rank of choosen strategies results.ranked_names # Save results into csv file results.write_summary('Milestone_3_result.csv') # Save all results plots plot = axl.Plot(results)
def test_reset_genome(self): self.versus_test(axelrod.Defector(), expected_actions=[(C, D)] + [(D, D)] * 4) self.player.reset_genome() self.assertEqual(self.player().genome, [C])
def test_strategy_with_noise1(self): self.versus_test(axl.Defector(), [(C, D)], turns=1, seed=9, attrs={"_recorded_history": [C]})
def test_vs_defector(self): expected = [(C, D), (C, D)] + [(D, D)] * 10 self.versus_test(axl.Defector(), expected_actions=expected)
def test_strategy_with_noise2(self): self.versus_test(axl.Defector(), [(D, C)], turns=1, noise=0.5, seed=11, attrs={"_recorded_history": [C]})
def test_vs(self): self.versus_test(axelrod.PSOGambler(), axelrod.Defector(), [C, C, D, D], [D, D, D, D])
def test_strategy(self): # Always retaliate a defection actions = [(C, D)] + [(D, D)] * 10 self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, attrs={"probing": False})
selfScore = [] oppScore = [] selfAvgList = [] oppAvgList = [] avgScore = [] oppAvgScore = [] evolveCode = [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,\ 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0] # For final experiments singEvolveCode = [0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1,\ 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0] # strategies = [axelrod.Cooperator(), axelrod.Defector()] # strategies = [axelrod.TitFor2Tats(), axelrod.SuspiciousTitForTat(), axelrod.TitForTat(), axelrod.Prober()] strategies = [axelrod.Cooperator(), axelrod.Defector(), axelrod.CyclerCCD(),axelrod.HardTitForTat(),\ axelrod.TitFor2Tats(), axelrod.SuspiciousTitForTat(), axelrod.Random(), axelrod.TitForTat(), axelrod.Prober()] learner = axelrod.LearnerAxel(memory_depth=2, exploreProb=0.1, learnerType=2) multAxel = axelrod.EvolveAxel(3, evolveCode, 'MULT') singAxel = axelrod.EvolveAxel(3, evolveCode, 'SING') ply = learner # print ply for p in strategies: print "Currently playing against strategy:", p for turn in range(numTurns): ply.play(p) selfList = map(ScoreMatrix, zip(ply.history, p.history))
def test_play_exception(self): p1, p2 = axelrod.Cooperator(), axelrod.Defector() mp = MoranProcess((p1, p2), mutation_rate=0.2) with self.assertRaises(ValueError): mp.play()
def test_strategy(self): opponent = axelrod.Bully() actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, D), (D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) # Tests whether TitForTat is played given score is below 1.75 opponent = axelrod.Defector() actions = [(C, D)] + [(D, D)] * 20 expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) opponent = axelrod.MockPlayer([C] * 2 + [D] * 8 + [C] * 4) actions = [(C, C), (C, C)] + [(C, D)] + [(D, D)] * 7 + [(D, C)] + [(C, C)] * 3 expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs) # If score is between 1.75 and 2.25, may cooperate or defect opponent = axelrod.MockPlayer(actions=[D] * 3 + [C] * 4 + [D] * 2) actions = [(C, D)] + [(D, D)] * 2 + [(D, C)] + [(C, C)] * 3 + [(C, D)] actions += ([(C, D)]) # <-- Random expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=0, attrs=expected_attrs) opponent = axelrod.MockPlayer(actions=[D] * 3 + [C] * 4 + [D] * 2) actions = [(C, D)] + [(D, D)] * 2 + [(D, C)] + [(C, C)] * 3 + [(C, D)] actions += ([(D, D)]) # <-- Random expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=17, attrs=expected_attrs) """If score is greater than 2.25 either cooperate or defect, if turn number <= 5; cooperate""" opponent = axelrod.MockPlayer(actions=[C] * 5) actions = [(C, C)] * 5 expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=1, attrs=expected_attrs) opponent = axelrod.MockPlayer(actions=[C] * 5) actions = [(C, C)] * 4 + [(D, C)] expected_attrs = { "num_turns_after_good_defection": 1, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=89, attrs=expected_attrs) """ Given score per turn is greater than 2.25, Tranquilizer will never defect twice in a row""" opponent = axelrod.MockPlayer(actions=[C] * 6) actions = [(C, C)] * 4 + [(D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 2, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 2, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=89, attrs=expected_attrs) # Tests cooperation after update_state opponent = axelrod.MockPlayer(actions=[C] * 5) actions = [(C, C)] * 4 + [(D, C)] + [(C, C)] expected_attrs = { "num_turns_after_good_defection": 2, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 2, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=89, attrs=expected_attrs) # Ensures FD1 values are calculated opponent = axelrod.MockPlayer(actions=[C] * 6) actions = [(C, C)] * 4 + [(D, C), (C, C)] expected_attrs = { "num_turns_after_good_defection": 2, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 2, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, seed=89, attrs=expected_attrs) # Ensures FD2 values are calculated opponent = axelrod.MockPlayer(actions=[C] * 6) actions = [(C, C)] * 4 + [(D, C)] + [(C, C)] * 2 expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 1.5, "one_turn_after_good_defection_ratio_count": 2, "two_turns_after_good_defection_ratio_count": 2 } self.versus_test(opponent, expected_actions=actions, seed=89, attrs=expected_attrs) # Ensures scores are being counted opponent = axelrod.Defector() actions = [(C, D)] + [(D, D)] * 19 expected_attrs = { "num_turns_after_good_defection": 0, "one_turn_after_good_defection_ratio": 5, "two_turns_after_good_defection_ratio": 0, "one_turn_after_good_defection_ratio_count": 1, "two_turns_after_good_defection_ratio_count": 1 } self.versus_test(opponent, expected_actions=actions, attrs=expected_attrs)
def test_birth_in_db(self): players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat() mp = MoranProcess(players, mode="db") axelrod.seed(1) self.assertEqual(mp.death(), 0) self.assertEqual(mp.birth(0), 2)
def test_strategy(self): # TitFor2Tats test_strategy init_kwargs = {"N": 1, "M": 2} opponent = axl.MockPlayer(actions=[D, D, D, C, C]) actions = [(C, D), (C, D), (D, D), (D, C), (C, C), (C, D)] self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) # TwoTitsForTat test_strategy init_kwargs = {"N": 2, "M": 1} opponent = axl.MockPlayer(actions=[D, C, C, D, C]) actions = [(C, D), (D, C), (D, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs) actions = [(C, C), (C, C)] self.versus_test( opponent=axl.Cooperator(), expected_actions=actions, init_kwargs=init_kwargs, ) actions = [(C, D), (D, D), (D, D)] self.versus_test( opponent=axl.Defector(), expected_actions=actions, init_kwargs=init_kwargs, ) # Cooperator test_strategy actions = [(C, C)] + [(C, D), (C, C)] * 9 self.versus_test( opponent=axl.Alternator(), expected_actions=actions, init_kwargs={ "N": 0, "M": 1 }, ) self.versus_test( opponent=axl.Alternator(), expected_actions=actions, init_kwargs={ "N": 0, "M": 5 }, ) self.versus_test( opponent=axl.Alternator(), expected_actions=actions, init_kwargs={ "N": 0, "M": 0 }, ) # Defector test_strategy actions = [(D, C)] + [(D, D), (D, C)] * 9 self.versus_test( opponent=axl.Alternator(), expected_actions=actions, init_kwargs={ "N": 1, "M": 0 }, ) self.versus_test( opponent=axl.Alternator(), expected_actions=actions, init_kwargs={ "N": 5, "M": 0 }, ) # Default init args actions = [(C, C), (C, D), (C, D), (D, C), (D, C), (D, D), (C, C)] opponent = axl.MockPlayer(actions=[acts[1] for acts in actions]) self.versus_test(opponent=opponent, expected_actions=actions)
def test_strategy(self): # Test TitForTat behavior in first 15 turns opponent = axelrod.Cooperator() actions = list([(C, C)]) * 15 self.versus_test(opponent, expected_actions=actions) opponent = axelrod.Defector() actions = [(C, D)] + list([(D, D)]) * 14 self.versus_test(opponent, expected_actions=actions) opponent = axelrod.Alternator() actions = [(C, C)] + [(C, D), (D, C)] * 7 self.versus_test(opponent, expected_actions=actions) opponent_actions = [C, D, D, C, D, C, C, D, C, D, D, C, C, D, D] opponent = axelrod.MockPlayer(actions=opponent_actions) mem_actions = [C, C, D, D, C, D, C, C, D, C, D, D, C, C, D] actions = list(zip(mem_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions) opponent = axelrod.Random() actions = [(C, D), (D, D), (D, C), (C, C), (C, D), (D, C)] self.versus_test(opponent, expected_actions=actions, seed=0) # Test net-cooperation-score (NCS) based decisions in subsequent turns opponent = axelrod.Cooperator() actions = [(C, C)] * 15 + [(C, C)] self.versus_test( opponent, expected_actions=actions, seed=1, init_kwargs={"memory": [D] * 5 + [C] * 10}, ) opponent = axelrod.Cooperator() actions = [(C, C)] * 15 + [(C, C)] self.versus_test( opponent, expected_actions=actions, seed=1, init_kwargs={"memory": [D] * 4 + [C] * 11}, ) # Test alternative starting strategies opponent = axelrod.Cooperator() actions = list([(D, C)]) * 15 self.versus_test( opponent, expected_actions=actions, init_kwargs={"start_strategy": axelrod.Defector}, ) opponent = axelrod.Cooperator() actions = list([(C, C)]) * 15 self.versus_test( opponent, expected_actions=actions, init_kwargs={"start_strategy": axelrod.Cooperator}, ) opponent = axelrod.Cooperator() actions = [(C, C)] + list([(D, C), (C, C)]) * 7 self.versus_test( opponent, expected_actions=actions, init_kwargs={"start_strategy": axelrod.Alternator}, ) opponent = axelrod.Defector() actions = [(C, D)] * 7 + [(D, D)] self.versus_test( opponent, expected_actions=actions, seed=4, init_kwargs={ "memory": [C] * 12, "start_strategy": axelrod.Defector, "start_strategy_duration": 0, }, )
def test_strategy(self): # Punishes defection with a growing number of defections and calms # the opponent with two cooperations in a row. opponent = axl.MockPlayer(actions=[C]) actions = [(C, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 0, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D]) actions = [(C, D)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 0, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D, C]) actions = [(C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 2, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D, C, C]) actions = [(C, D), (D, C), (C, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 1, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D, C, D, C]) actions = [(C, D), (D, C), (C, D), (C, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 0, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D, C, D, C, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 0, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 0, "punish_count": 0, }, ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, C, D, C]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, C), (C, D), (D, C)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 2, "punish_count": 2, }, ) opponent = axl.MockPlayer(actions=[D, C, D, C, C, D, D, D]) actions = [(C, D), (D, C), (C, D), (C, C), (C, C), (C, D), (D, D), (D, D)] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 2, "punish_count": 1, }, ) opponent = axl.Defector() actions = [ (C, D), (D, D), # 1 defection as a response to the 1 defection by opponent (C, D), (C, D), (D, D), # starts defecting after a total of 4 defections by the opponent (D, D), (D, D), (D, D), # 4 defections (C, D), (C, D), (D, D), # Start defecting after a total of 10 defections by the opponent (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), # 10 defections (C, D), (C, D), (D, D), # starts defecting after 22 defections by the opponent (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), # 22 defections (C, D), (C, D), (D, D), (D, D), (D, D), (D, D), ] self.versus_test( opponent, expected_actions=actions, attrs={ "calm_count": 2, "punish_count": 42, }, )
def test_vs(self): self.versus_test(axelrod.EvolvedLookerUp2_2_2(), axelrod.Defector(), [C, C, D], [D, D, D])
def test_vs_defector(self): actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test(axl.Defector(), expected_actions=actions)
#Random value for Dr Jykle j = random.random() #Let Mr Hyde and Dr Jykle compete #If Dr Jykle beats Mr Hyde cooperate else defect if h - j >= 0: return C else: return D hj = DrJekyllMrHyde() #Create instance of class #players = [s() for s in axl.short_run_time_strategies] # Create players players = [ hj, axl.Defector(), axl.TitForTat(), axl.Thumper(), axl.Punisher(), axl.AdaptiveTitForTat(), axl.FirstByDavis(), axl.Appeaser() ] #players.append(hj) tournament = Tournament(players) # Create a tournament results = tournament.play() # Play the tournament results.write_summary('SummaryMileStone3.csv') # Write results to csv file plot = axl.Plot(results) p = plot.boxplot() #Plot results
def test_rounds(self): self.versus_test(axl.EvolvedHMM5(), axl.Defector(), [C, C, D], [D, D, D])