Exemple #1
0
    def build_scores(self):
        """
        Returns:
        --------
            The total scores per player for each repetition lengths.
            List of the form:

            [ML1, ML2, ML3..., MLn]

            Where n is the number of players and MLi is a list of the form:

            [pi1, pi2, pi3, ..., pim]

            Where m is the number of repetitions and pij is the total score
            obtained by each player in repetition j.

            In Axelrod's original tournament, there were no self-interactions
            (e.g. player 1 versus player 1) and so these are also ignored.
        """
        scores = [[0 for rep in range(self.nrepetitions)] for _ in
                  range(self.nplayers)]

        for index_pair, repetitions in self.interactions.items():
            if index_pair[0] != index_pair[1]:  # Ignoring self interactions
                for repetition, interaction in enumerate(repetitions):
                    final_scores = iu.compute_final_score(interaction)
                    for player in range(2):
                        player_index = index_pair[player]
                        player_score = final_scores[player]
                        scores[player_index][repetition] += player_score

        return scores
Exemple #2
0
    def strategy(self, opponent: Player) -> Action:

        if not self.history:
            return C

        self.update_state(opponent)
        if self.num_turns_after_good_defection in [1, 2]:
            return C

        current_score = compute_final_score(zip(self.history,
                                                opponent.history))

        if (current_score[0] / ((len(self.history)) + 1)) >= 2.25:
            probability = ((.95 - (
                ((self.one_turn_after_good_defection_ratio) +
                 (self.two_turns_after_good_defection_ratio) - 5) / 15)) +
                           (1 / (((len(self.history)) + 1)**2)) -
                           (self.dict[opponent.history[-1]] / 4))
            if random.random() <= probability:
                return C
            self.num_turns_after_good_defection = 1
            return D
        if (current_score[0] / ((len(self.history)) + 1)) >= 1.75:
            probability = ((.25 + ((opponent.cooperations + 1) /
                                   ((len(self.history)) + 1))) -
                           (self.opponent_consecutive_defections * .25) +
                           ((current_score[0] - current_score[1]) / 100) +
                           (4 / ((len(self.history)) + 1)))
            if random.random() <= probability:
                return C
            return D
        return opponent.history[-1]
Exemple #3
0
    def build_scores(self):
        """
        Returns:
        --------
            The total scores per player for each repetition lengths.
            List of the form:

            [ML1, ML2, ML3..., MLn]

            Where n is the number of players and MLi is a list of the form:

            [pi1, pi2, pi3, ..., pim]

            Where m is the number of repetitions and pij is the total score
            obtained by each player in repetition j.

            In Axelrod's original tournament, there were no self-interactions
            (e.g. player 1 versus player 1) and so these are also ignored.
        """
        scores = [[0 for rep in range(self.nrepetitions)]
                  for _ in range(self.nplayers)]

        for index_pair, repetitions in self.interactions.items():
            if index_pair[0] != index_pair[1]:  # Ignoring self interactions
                for repetition, interaction in enumerate(repetitions):
                    final_scores = iu.compute_final_score(
                        interaction, self.game)
                    for player in range(2):
                        player_index = index_pair[player]
                        player_score = final_scores[player]
                        scores[player_index][repetition] += player_score

        return scores
Exemple #4
0
    def _update_scores(self, repetition, p1, p2, interaction):
        """
        During a read of the data, update the scores attribute

        Parameters
        ----------

            repetition : int
                The index of a repetition
            p1, p2 : int
                The indices of the first and second player
            interaction : list of tuples
                A list of interactions
        """
        final_scores = iu.compute_final_score(interaction, game=self.game)
        for index, player in enumerate([p1, p2]):
            player_score = final_scores[index]
            self.scores[player][repetition] += player_score
Exemple #5
0
    def _update_scores(self, repetition, p1, p2, interaction):
        """
        During a read of the data, update the scores attribute

        Parameters
        ----------

            repetition : int
                The index of a repetition
            p1, p2 : int
                The indices of the first and second player
            interaction : list of tuples
                A list of interactions
        """
        final_scores = iu.compute_final_score(interaction, game=self.game)
        for index, player in enumerate([p1, p2]):
            player_score = final_scores[index]
            self.scores[player][repetition] += player_score
Exemple #6
0
    def strategy(self, opponent):
        if self is opponent:
            warnings.warn(message=self_interaction_message)

        if not self.history:
            their_last_move = 0
            scores = (0, 0)
            my_last_move = 0
        else:
            their_last_move = original_actions[opponent.history[-1]]
            scores = compute_final_score(zip(self.history, opponent.history),
                                         game=self.match_attributes["game"])
            my_last_move = original_actions[self.history[-1]]
        move_number = len(self.history) + 1
        if self.classifier["stochastic"]:
            random_value = self._random.random()
        else:
            random_value = 0
        original_action = self.original_strategy(
            their_last_move, move_number, scores[0], scores[1], random_value,
            my_last_move)
        return actions[original_action]
Exemple #7
0
    def _calculate_results(self, interactions):
        results = []

        scores = iu.compute_final_score(interactions, self.game)
        results.append(scores)

        score_diffs = scores[0] - scores[1], scores[1] - scores[0]
        results.append(score_diffs)

        turns = len(interactions)
        results.append(turns)

        score_per_turns = iu.compute_final_score_per_turn(
            interactions, self.game)
        results.append(score_per_turns)

        score_diffs_per_turns = score_diffs[0] / turns, score_diffs[1] / turns
        results.append(score_diffs_per_turns)

        initial_coops = tuple(
            map(bool, iu.compute_cooperations(interactions[:1])))
        results.append(initial_coops)

        cooperations = iu.compute_cooperations(interactions)
        results.append(cooperations)

        state_distribution = iu.compute_state_distribution(interactions)
        results.append(state_distribution)

        state_to_action_distributions = iu.compute_state_to_action_distribution(
            interactions)
        results.append(state_to_action_distributions)

        winner_index = iu.compute_winner_index(interactions, self.game)
        results.append(winner_index)

        return results
Exemple #8
0
    def _calculate_results(self, interactions):
        results = []

        scores = iu.compute_final_score(interactions, self.game)
        results.append(scores)

        score_diffs = scores[0] - scores[1], scores[1] - scores[0]
        results.append(score_diffs)

        turns = len(interactions)
        results.append(turns)

        score_per_turns = iu.compute_final_score_per_turn(interactions, self.game)
        results.append(score_per_turns)

        score_diffs_per_turns = score_diffs[0] / turns, score_diffs[1] / turns
        results.append(score_diffs_per_turns)

        initial_coops = tuple(map(bool, iu.compute_cooperations(interactions[:1])))
        results.append(initial_coops)

        cooperations = iu.compute_cooperations(interactions)
        results.append(cooperations)

        state_distribution = iu.compute_state_distribution(interactions)
        results.append(state_distribution)

        state_to_action_distributions = iu.compute_state_to_action_distribution(
            interactions
        )
        results.append(state_to_action_distributions)

        winner_index = iu.compute_winner_index(interactions, self.game)
        results.append(winner_index)

        return results
 def test_compute_final_score(self):
     for inter, final_score in zip(self.interactions, self.final_scores):
         self.assertEqual(final_score, iu.compute_final_score(inter))
Exemple #10
0
 def final_score(self):
     """Returns the final score for a Match"""
     return iu.compute_final_score(self.result, self.game)
 def test_compute_final_score(self):
     for inter, final_score in zip(self.interactions, self.final_scores):
         self.assertEqual(final_score, iu.compute_final_score(inter))
Exemple #12
0
 def final_score(self):
     """Returns the final score for a Match"""
     return iu.compute_final_score(self.result, self.game)
Exemple #13
0
 def _update_scores(self, repetition, p1, p2, interaction):
     final_scores = iu.compute_final_score(interaction, game=self.game)
     for index, player in enumerate([p1, p2]):
         player_score = final_scores[index]
         self.scores[player][repetition] += player_score