def _add_noise(noise, s1, s2): r = random.random() if r < noise: s1 = flip_action(s1) r = random.random() if r < noise: s2 = flip_action(s2) return s1, s2
def test_dual_tft_transformer(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ p1 = axelrod.TitForTat() p2 = DualTransformer()(axelrod.TitForTat)() p3 = axelrod.CyclerCCD() # Cycles 'CCD' for _ in range(10): p1.play(p3) p3.reset() for _ in range(10): p2.play(p3) self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
def test_dual_majority_transformer(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ p1 = axelrod.GoByMajority() p2 = DualTransformer()(axelrod.GoByMajority)() p3 = axelrod.Cycler(cycle='CDD') # Cycles 'CDD' for _ in range(10): p1.play(p3) p3.reset() for _ in range(10): p2.play(p3) self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
def strategy(self, opponent: Player) -> Action: # Defect on the first move if not opponent.history: return D # Am I TFT? if self.is_TFT: return D if opponent.history[-1:] == [D] else C else: # Did opponent defect? if opponent.history[-1] == D: self.is_TFT = True return C if len(self.history) in [1, 2]: return C # Alternate C and D return flip_action(self.history[-1])
def get_joss_strategy_actions(opponent_moves: list, indices_to_flip: list) -> list: """ Takes a list of opponent moves and returns a tuple list of [(Joss moves, opponent moves)]. "indices_to_flip" are the indices where Joss differs from it's expected TitForTat. Joss is from axelrod.strategies.axelrod_first. """ out = [] for index, action in enumerate(opponent_moves): previous_action = opponent_moves[index - 1] if index == 0: out.append((C, action)) elif index in indices_to_flip: out.append((flip_action(previous_action), action)) else: out.append((previous_action, action)) return out
def strategy(self, opponent: Player) -> Action: round_number = len(self.history) + 1 # According to internet sources, the original implementation defected # on the first two moves. Otherwise it wins (if this code is removed # and the comment restored. # http://www.sci.brooklyn.cuny.edu/~sklar/teaching/f05/alife/notes/azhar-ipd-Oct19th.pdf if self.revised: if round_number == 1: self.move = C return self.move elif not self.revised: if round_number <= 2: self.move = D return self.move # Update various counts if round_number > 2: if self.history[-1] == D: if opponent.history[-1] == C: self.nice2 += 1 self.total_D += 1 self.bad = self.nice2 / self.total_D else: if opponent.history[-1] == C: self.nice1 += 1 self.total_C += 1 self.good = self.nice1 / self.total_C # Make a decision based on the accrued counts c = 6.0 * self.good - 8.0 * self.bad - 2 alt = 4.0 * self.good - 5.0 * self.bad - 1 if (c >= 0 and c >= alt): self.move = C elif (c >= 0 and c < alt) or (alt >= 0): self.move = flip_action(self.move) else: self.move = D return self.move
def strategy(self, opponent: Player) -> Action: # Random first move if not self.history: return random_choice() # Act opposite of opponent otherwise return flip_action(opponent.history[-1])