Example #1
0
    def build_good_partner_matrix(self):
        """
        Returns:
        --------

            An n by n matrix of good partner ratings for n players i.e. an n by
            n matrix where n is the number of players. Each row (i) and column
            (j) represents an individual player and the value Pij is the sum of
            the number of repetitions where player i cooperated as often or
            more than opponent j.
        """

        plist = list(range(self.nplayers))
        good_partner_matrix = [[0 for opponent in plist] for player in plist]

        for player in plist:
            for opponent in plist:
                if player != opponent:
                    for index_pair, repetitions in self.interactions.items():

                        if (player, opponent) == index_pair:
                            for interaction in repetitions:
                                coops = iu.compute_cooperations(interaction)
                                if coops[0] >= coops[1]:
                                    good_partner_matrix[player][opponent] += 1

                        if (opponent, player) == index_pair:
                            for interaction in repetitions:
                                coops = iu.compute_cooperations(interaction)
                                if coops[0] <= coops[1]:
                                    good_partner_matrix[player][opponent] += 1

        return good_partner_matrix
Example #2
0
    def build_good_partner_matrix(self):
        """
        Returns:
        --------

            An n by n matrix of good partner ratings for n players i.e. an n by
            n matrix where n is the number of players. Each row (i) and column
            (j) represents an individual player and the value Pij is the sum of
            the number of repetitions where player i cooperated as often or
            more than opponent j.
        """

        plist = list(range(self.nplayers))
        good_partner_matrix = [[0 for opponent in plist] for player in plist]

        for player in plist:
            for opponent in plist:
                if player != opponent:
                    for index_pair, repetitions in self.interactions.items():

                        if (player, opponent) == index_pair:
                            for interaction in repetitions:
                                coops = iu.compute_cooperations(interaction)
                                if coops[0] >= coops[1]:
                                    good_partner_matrix[player][opponent] += 1

                        elif (opponent, player) == index_pair:
                            for interaction in repetitions:
                                coops = iu.compute_cooperations(interaction)
                                if coops[0] <= coops[1]:
                                    good_partner_matrix[player][opponent] += 1

        return good_partner_matrix
Example #3
0
    def build_cooperation(self):
        """
        Returns:
        --------

            The list of cooperation counts.
            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 pij is the total number of cooperations over all repetitions
            played by player i against player j.
        """
        plist = list(range(self.nplayers))
        cooperations = [[0 for opponent in plist] for player in plist]

        for player in plist:
            for opponent in plist:
                if player != opponent:
                    for index_pair, repetitions in self.interactions.items():
                        coop_count = 0

                        if (player, opponent) == index_pair:
                            for interaction in repetitions:
                                coop_count += iu.compute_cooperations(
                                    interaction)[0]
                        elif (opponent, player) == index_pair:
                            for interaction in repetitions:
                                coop_count += iu.compute_cooperations(
                                    interaction)[1]

                        cooperations[player][opponent] += coop_count

        return cooperations
Example #4
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
Example #5
0
    def build_cooperation(self):
        """
        Returns:
        --------

            The list of cooperation counts.
            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 pij is the total number of cooperations over all repetitions
            played by player i against player j.
        """
        plist = list(range(self.nplayers))
        cooperations = [[0 for opponent in plist] for player in plist]

        for player in plist:
            for opponent in plist:
                if player != opponent:
                    for index_pair, repetitions in self.interactions.items():
                        coop_count = 0

                        if (player, opponent) == index_pair:
                            for interaction in repetitions:
                                coop_count += iu.compute_cooperations(interaction)[0]
                        if (opponent, player) == index_pair:
                            for interaction in repetitions:
                                coop_count += iu.compute_cooperations(interaction)[1]

                        cooperations[player][opponent] += coop_count

        return cooperations
Example #6
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_cooperations(self):
     for inter, coop in zip(self.interactions, self.cooperations):
         self.assertEqual(coop, iu.compute_cooperations(inter))
Example #8
0
 def cooperation(self):
     """Returns the count of cooperations by each player"""
     return iu.compute_cooperations(self.result)
 def test_compute_cooperations(self):
     for inter, coop in zip(self.interactions, self.cooperations):
         self.assertEqual(coop, iu.compute_cooperations(inter))
Example #10
0
    def _build_score_related_metrics(self,
                                     progress_bar=False,
                                     keep_interactions=False):
        """
        Read the data and carry out all relevant calculations.

        Parameters
        ----------
            progress_bar : bool
                Whether or not to display a progress bar
            keep_interactions : bool
                Whether or not to lad the interactions in to memory
        """
        match_chunks = self.read_match_chunks(progress_bar)

        for match in match_chunks:
            p1, p2 = int(match[0][0]), int(match[0][1])

            for repetition, record in enumerate(match):
                interaction = record[4:]

                if keep_interactions:
                    try:
                        self.interactions[(p1, p2)].append(interaction)
                    except KeyError:
                        self.interactions[(p1, p2)] = [interaction]

                scores_per_turn = iu.compute_final_score_per_turn(
                    interaction, game=self.game)
                cooperations = iu.compute_cooperations(interaction)
                state_counter = iu.compute_state_distribution(interaction)

                self._update_match_lengths(repetition, p1, p2, interaction)
                self._update_payoffs(p1, p2, scores_per_turn)
                self._update_score_diffs(repetition, p1, p2, scores_per_turn)
                self._update_normalised_cooperation(p1, p2, interaction)

                if p1 != p2:  # Anything that ignores self interactions

                    for player in [p1, p2]:
                        self.total_interactions[player] += 1

                    self._update_match_lengths(repetition, p2, p1, interaction)
                    self._update_wins(repetition, p1, p2, interaction)
                    self._update_scores(repetition, p1, p2, interaction)
                    self._update_normalised_scores(repetition, p1, p2,
                                                   scores_per_turn)
                    self._update_cooperation(p1, p2, cooperations)
                    initial_coops = iu.compute_cooperations(interaction[:1])
                    self._update_initial_cooperation_count(
                        p1, p2, initial_coops)
                    self._update_state_distribution(p1, p2, state_counter)
                    self._update_good_partner_matrix(p1, p2, cooperations)

        if progress_bar:
            self.progress_bar = tqdm.tqdm(total=12 + 2 * self.nplayers,
                                          desc="Finishing")
        self._summarise_normalised_scores()
        self._summarise_normalised_cooperation()

        self.ranking = self._build_ranking()
        self.normalised_state_distribution = self._build_normalised_state_distribution(
        )
        self.ranked_names = self._build_ranked_names()
        self.payoff_matrix = self._build_payoff_matrix()
        self.payoff_stddevs = self._build_payoff_stddevs()
        self.payoff_diffs_means = self._build_payoff_diffs_means()
        self.vengeful_cooperation = self._build_vengeful_cooperation()
        self.cooperating_rating = self._build_cooperating_rating()
        self.initial_cooperation_rate = self._build_initial_cooperation_rate()
        self.good_partner_rating = self._build_good_partner_rating()
        self.eigenjesus_rating = self._build_eigenjesus_rating()
        self.eigenmoses_rating = self._build_eigenmoses_rating()

        if progress_bar:
            self.progress_bar.close()
Example #11
0
    def _build_score_related_metrics(self, progress_bar=False,
                                     keep_interactions=False):
        """
        Read the data and carry out all relevant calculations.

        Parameters
        ----------
            progress_bar : bool
                Whether or not to display a progress bar
            keep_interactions : bool
                Whether or not to lad the interactions in to memory
        """
        match_chunks = self.read_match_chunks(progress_bar)

        for match in match_chunks:
            p1, p2 = int(match[0][0]), int(match[0][1])

            for repetition, record in enumerate(match):
                interaction = record[4:]

                if keep_interactions:
                    try:
                        self.interactions[(p1, p2)].append(interaction)
                    except KeyError:
                        self.interactions[(p1, p2)] = [interaction]

                scores_per_turn = iu.compute_final_score_per_turn(interaction,
                                                                 game=self.game)
                cooperations = iu.compute_cooperations(interaction)
                state_counter = iu.compute_state_distribution(interaction)

                self._update_match_lengths(repetition, p1, p2, interaction)
                self._update_payoffs(p1, p2, scores_per_turn)
                self._update_score_diffs(repetition, p1, p2, scores_per_turn)
                self._update_normalised_cooperation(p1, p2, interaction)

                if p1 != p2:  # Anything that ignores self interactions

                    for player in [p1, p2]:
                        self.total_interactions[player] += 1

                    self._update_match_lengths(repetition, p2, p1, interaction)
                    self._update_wins(repetition, p1, p2, interaction)
                    self._update_scores(repetition, p1, p2, interaction)
                    self._update_normalised_scores(repetition, p1, p2,
                                                   scores_per_turn)
                    self._update_cooperation(p1, p2, cooperations)
                    self._update_state_distribution(p1, p2, state_counter)
                    self._update_good_partner_matrix(p1, p2, cooperations)

        if progress_bar:
            self.progress_bar = tqdm.tqdm(total=11 + 2 * self.nplayers,
                                          desc="Finishing")
        self._summarise_normalised_scores()
        self._summarise_normalised_cooperation()

        self.ranking = self._build_ranking()
        self.normalised_state_distribution = self._build_normalised_state_distribution()
        self.ranked_names = self._build_ranked_names()
        self.payoff_matrix = self._build_payoff_matrix()
        self.payoff_stddevs = self._build_payoff_stddevs()
        self.payoff_diffs_means = self._build_payoff_diffs_means()
        self.vengeful_cooperation = self._build_vengeful_cooperation()
        self.cooperating_rating = self._build_cooperating_rating()
        self.good_partner_rating = self._build_good_partner_rating()
        self.eigenjesus_rating = self._build_eigenjesus_rating()
        self.eigenmoses_rating = self._build_eigenmoses_rating()

        if progress_bar:
            self.progress_bar.close()
Example #12
0
 def cooperation(self):
     """Returns the count of cooperations by each player"""
     return iu.compute_cooperations(self.result)