-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_scores.py
110 lines (86 loc) · 3.61 KB
/
calculate_scores.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pandas as pd
from mock_results import single_preference_var, pair_preference_var, pair_preference_gpt_4o, pair_preference_gpt_35, triple_preference_gpt_35
import matplotlib.pyplot as plt
from constants import MORAL_VALUES
import numpy as np
import numpy as np
from scipy.optimize import linear_sum_assignment
import itertools
from itertools import permutations
import seaborn as sns
def get_single_prefs_means_var():
single_means_dict = {}
for moral_val in MORAL_VALUES:
vals = []
for preference_list in single_preference_var:
vals.append(preference_list[moral_val]["yes"])
single_means_dict[moral_val] = [np.mean(vals), np.std(vals)]
# print(single_means_dict)
return single_means_dict
def normalise_pair_prefs(pair_preference_var):
first_comb = pair_preference_var
num = sum(first_comb[list(first_comb.keys())[0]].values())
# print(num)
normalised_pair_prefs = []
# for i, preference_list in enumerate(pair_preference_var):
new_dict = {}
for comb, results in pair_preference_var.items():
new_dict[comb] = {k: int(v * 100 / num) for k, v in results.items()}
# normalised_pair_prefs.append(new_dict)
# print(normalised_pair_prefs)
return new_dict# normalised_pair_prefs[0]
def pair_preference_matrix(preference_dict):
# Define the items
items = ["authority", "care", "fairness", "liberty", "loyalty", "sanctity"]
# Create the preference matrix
preference_matrix = np.zeros((6, 6))
# Populate the preference matrix
for (item1, item2), preferences in preference_dict.items():
i = items.index(item1)
j = items.index(item2)
preference_matrix[i, j] = preferences[item1]
preference_matrix[j, i] = preferences[item2]
matrix = pd.DataFrame(data=preference_matrix, columns=items, index=items)
# print(matrix)
return matrix, preference_matrix
def kemeny_young(matrix):
n = len(matrix)
scores = np.zeros(n)
# Generate all permutations
perms = list(permutations(range(n)))
min_dist = float('inf')
optimal_perm = None
for perm in perms:
dist = 0
for i in range(n):
for j in range(i+1, n):
if matrix[perm[i]][perm[j]] < matrix[perm[j]][perm[i]]:
dist += matrix[perm[j]][perm[i]]
if dist < min_dist:
min_dist = dist
optimal_perm = perm
# Calculate ranking scores based on the optimal permutation
for i in range(n):
scores[optimal_perm[i]] = n - i - 1
return scores
def get_pair_pref_ranking(pair_preferences=pair_preference_var[0]):
matrix, preference_matrix = pair_preference_matrix(pair_preferences)
# best_ranking, min_distance = kemeny_young_method(MORAL_VALUES, pair_preference_var[0])
# print("Kemeny-Young ranking:", best_ranking, min_distance)
for col in matrix.columns:
matrix[col] = matrix[col].astype(int)
new_matrix = matrix
new_matrix["row_sums"] = new_matrix.apply(sum, axis=1)
new_matrix["row_sums"] = new_matrix["row_sums"].apply(int)
new_matrix = new_matrix.sort_values(by="row_sums", ascending=False)
matrix = new_matrix[MORAL_VALUES]
rankings = kemeny_young(preference_matrix)
# total_scores = sorted(zip(MORAL_VALUES, rankings, matrix["row_sums"].values), key=lambda x:x[1])
# print(new_matrix)
print("Kemeny-Young Ranking Scores:", rankings)
# print(matrix["row_sums"].values)
sns.heatmap(matrix, cmap="Blues",annot=True, fmt=".3g")
plt.show()
return matrix, rankings
if __name__ == "__main__":
get_pair_pref_ranking(normalise_pair_prefs(pair_preference_gpt_35))