def simulate_play(player1: Player, player2: Player, action1: Action =None, action2: Action =None) -> Tuple[Action, Action]: """ Simulates play with or without forced history. If action1 and action2 are given, these actions are enforced in the players strategy. This generally should not be necessary, but various tests may force impossible or unlikely histories. """ if action1 and action2: mock_player1 = MockPlayer(actions=[action1], history=player1.history) mock_player2 = MockPlayer(actions=[action2], history=player2.history) # Force plays s1 = player1.strategy(mock_player2) s2 = player2.strategy(mock_player1) if (s1 != action1) or (s2 != action2): warnings.warn( "Simulated play mismatch with expected history: Round was " "({}, {}) but ({}, {}) was expected for player: {}".format( s1, s2, action1, action2, str(player1)) ) # Record intended history # Update Cooperation / Defection counts update_history(player1, action1) update_history(player2, action2) update_state_distribution(player1, action1, action2) update_state_distribution(player2, action2, action1) return (s1, s2) else: s1 = player1.strategy(player2) s2 = player2.strategy(player1) # Record history update_history(player1, s1) update_history(player2, s2) update_state_distribution(player1, s1, s2) update_state_distribution(player2, s2, s1) return (s1, s2)
def __init__( self, num_features: int, num_hidden: int, weights: List[float] = None ) -> None: Player.__init__(self) self.num_features = num_features self.num_hidden = num_hidden self._process_weights(weights, num_features, num_hidden)
def __init__(self, transitions: Tuple[Transition, ...] = ((1, C, 1, C), (1, D, 1, D)), initial_state: int = 1, initial_action: Action = C) -> None: Player.__init__(self) self.initial_state = initial_state self.initial_action = initial_action self.fsm = SimpleFSM(transitions, initial_state)
def __init__(self, cycle: str = "CCD") -> None: """This strategy will repeat the parameter `cycle` endlessly, e.g. C C D C C D C C D ... Special Cases ------------- Cooperator is equivalent to Cycler("C") Defector is equivalent to Cycler("D") Alternator is equivalent to Cycler("CD") """ Player.__init__(self) self.cycle = cycle self.set_cycle(cycle=cycle)
def hint(): details = request.get_json() p = r.pipeline() p.hget(f'user:{details["id"]}', 'strategy') p.hmget(f'match:{details["match"]}', 'proponent', 'rounds') strategy, (proponent, rounds) = p.execute() proponent_history, opponent_history = get_histories( details['id'], proponent, rounds) proponent_strategy = getattr(_strategies, strategy)() proponent_strategy.history = proponent_history opponent_strategy = Player() opponent_strategy.history = opponent_history return jsonify(status='OK', move=proponent_strategy.strategy(opponent_strategy))
def __init__( self, lookup_dict: dict = None, initial_actions: tuple = None, pattern: Any = None, # pattern is str or tuple of Action's. parameters: Plays = None) -> None: Player.__init__(self) self.parameters = parameters self.pattern = pattern self._lookup = self._get_lookup_table(lookup_dict, pattern, parameters) self._set_memory_depth() self.initial_actions = self._get_initial_actions(initial_actions) self._initial_actions_pool = list(self.initial_actions)
def strategy(opponent: Player) -> Action: """ Alters the opponents strategy method to be a lambda function which always returns C. This player will then always return D to take advantage of this """ opponent.strategy = lambda opponent: C return D
def __init__(self, transitions_C=None, transitions_D=None, emission_probabilities=None, initial_state=0, initial_action=C) -> None: Player.__init__(self) if not transitions_C: transitions_C = [[1]] transitions_D = [[1]] emission_probabilities = [0.5] # Not stochastic initial_state = 0 self.initial_state = initial_state self.initial_action = initial_action self.hmm = SimpleHMM(copy_lists(transitions_C), copy_lists(transitions_D), list(emission_probabilities), initial_state) assert self.hmm.is_well_formed() self.state = self.hmm.state self.classifier["stochastic"] = self.is_stochastic()
def strategy(self, opponent: Player) -> Action: """ Look at what the opponent will play in the next round and choose a strategy that gives the least jail time, which is is equivalent to playing the same strategy as that which the opponent will play. """ curframe = inspect.currentframe() calframe = inspect.getouterframes(curframe, 2) calname = calframe[1][3] if calname == 'strategy': return self.default() else: return opponent.strategy(self)
def strategy(self, opponent: Player) -> Action: """Will read the mind of the opponent and play the opponent's strategy. Also avoid infinite recursion when called by itself or another mind reader or bender by cooperating. """ curframe = inspect.currentframe() calframe = inspect.getouterframes(curframe, 2) calname = calframe[1][3] if calname in ('strategy', 'simulate_match'): return C return opponent.strategy(self)
def strategy(opponent: Player) -> Action: opponent.__dict__['strategy'] = lambda opponent: C return D
def strategy(opponent: Player) -> Action: opponent.strategy = lambda opponent: C return D
class TestModuleMethods(unittest.TestCase): def setUp(self): self.player = Player() def update_history(self, history_list): for move in history_list: update_history(self.player, move) def test_update_history(self): self.assertEqual(self.player.history, []) self.assertEqual(self.player.defections, 0) self.update_history([D, D, C]) self.assertEqual(self.player.history, [D, D, C]) self.assertEqual(self.player.defections, 2) self.player.reset() self.update_history([D]) self.assertEqual(self.player.history, [D]) self.assertEqual(self.player.defections, 1) def test_backstabber_strategy_no_history(self): self.assertEqual(C, backstabber._backstabber_strategy(self.player)) def test_backstabber_strategy_three_defections(self): self.update_history([D, D, D]) self.assertEqual(C, backstabber._backstabber_strategy(self.player)) def test_backstabber_strategy_four_defections(self): self.update_history([D, D, D, D]) self.assertEqual(D, backstabber._backstabber_strategy(self.player)) def test_alt_strategy_no_history_one_history_returns_C(self): self.assertEqual(C, backstabber._alt_strategy(self.player)) self.update_history([D]) self.assertEqual(C, backstabber._alt_strategy(self.player)) def test_alt_strategy_returns_D(self): self.update_history([C, C, D, D]) self.assertEqual(D, backstabber._alt_strategy(self.player)) self.player.reset() self.update_history([D, D, D]) self.assertEqual(D, backstabber._alt_strategy(self.player)) def test_alt_strategy_returns_C(self): self.update_history([D, D, D, C]) self.assertEqual(C, backstabber._alt_strategy(self.player)) def test_opponent_defected_in_first_n_rounds(self): self.update_history([C, C, C, C, D, C]) self.assertTrue( backstabber._opponent_defected_in_first_n_rounds(self.player, 10) ) self.assertTrue( backstabber._opponent_defected_in_first_n_rounds(self.player, 6) ) self.assertTrue( backstabber._opponent_defected_in_first_n_rounds(self.player, 5) ) self.assertFalse( backstabber._opponent_defected_in_first_n_rounds(self.player, 4) ) def test_opponent_triggers_alt_strategy_false_by_defected_in_first_n_rounds(self): last_of_first_n_rounds = 7 history = [C if rnd != last_of_first_n_rounds else D for rnd in range(1, 20)] self.update_history(history) self.assertFalse(backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_false_by_before_round_eight(self): current_round = 7 history = [C] * (current_round - 1) self.update_history(history) self.assertFalse(backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_false_by_after_round_one_eighty(self): current_round = 181 history = [C] * (current_round - 1) self.update_history(history) self.assertFalse(backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_true_edge_case_high(self): current_round = 180 history = [C] * (current_round - 1) self.update_history(history) self.assertTrue(backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_true_edge_case_low(self): current_round = 8 history = [C] * (current_round - 1) self.update_history(history) self.assertTrue(backstabber._opponent_triggers_alt_strategy(self.player))
def setUp(self): self.player = Player()
class TestModuleMethods(unittest.TestCase): def setUp(self): self.player = Player() def update_history(self, history_list): for move in history_list: update_history(self.player, move) def test_update_history(self): self.assertEqual(self.player.history, []) self.assertEqual(self.player.defections, 0) self.update_history([D, D, C]) self.assertEqual(self.player.history, [D, D, C]) self.assertEqual(self.player.defections, 2) self.player.reset() self.update_history([D]) self.assertEqual(self.player.history, [D]) self.assertEqual(self.player.defections, 1) def test_backstabber_strategy_no_history(self): self.assertEqual(C, backstabber._backstabber_strategy(self.player)) def test_backstabber_strategy_three_defections(self): self.update_history([D, D, D]) self.assertEqual(C, backstabber._backstabber_strategy(self.player)) def test_backstabber_strategy_four_defections(self): self.update_history([D, D, D, D]) self.assertEqual(D, backstabber._backstabber_strategy(self.player)) def test_alt_strategy_no_history_one_history_returns_C(self): self.assertEqual(C, backstabber._alt_strategy(self.player)) self.update_history([D]) self.assertEqual(C, backstabber._alt_strategy(self.player)) def test_alt_strategy_returns_D(self): self.update_history([C, C, D, D]) self.assertEqual(D, backstabber._alt_strategy(self.player)) self.player.reset() self.update_history([D, D, D]) self.assertEqual(D, backstabber._alt_strategy(self.player)) def test_alt_strategy_returns_C(self): self.update_history([D, D, D, C]) self.assertEqual(C, backstabber._alt_strategy(self.player)) def test_opponent_defected_in_first_n_rounds(self): self.update_history([C, C, C, C, D, C]) self.assertTrue( backstabber._opponent_defected_in_first_n_rounds(self.player, 10)) self.assertTrue( backstabber._opponent_defected_in_first_n_rounds(self.player, 6)) self.assertTrue( backstabber._opponent_defected_in_first_n_rounds(self.player, 5)) self.assertFalse( backstabber._opponent_defected_in_first_n_rounds(self.player, 4)) def test_opponent_triggers_alt_strategy_false_by_defected_in_first_n_rounds( self): last_of_first_n_rounds = 7 history = [ C if rnd != last_of_first_n_rounds else D for rnd in range(1, 20) ] self.update_history(history) self.assertFalse( backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_false_by_before_round_eight(self): current_round = 7 history = [C] * (current_round - 1) self.update_history(history) self.assertFalse( backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_false_by_after_round_one_eighty( self): current_round = 181 history = [C] * (current_round - 1) self.update_history(history) self.assertFalse( backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_true_edge_case_high(self): current_round = 180 history = [C] * (current_round - 1) self.update_history(history) self.assertTrue( backstabber._opponent_triggers_alt_strategy(self.player)) def test_opponent_triggers_alt_strategy_true_edge_case_low(self): current_round = 8 history = [C] * (current_round - 1) self.update_history(history) self.assertTrue( backstabber._opponent_triggers_alt_strategy(self.player))
def strategy(opponent: Player) -> Action: opponent.__dict__["strategy"] = lambda opponent: C return D
def __repr__(self): return Player.__repr__(self)