-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_credentials.py
74 lines (63 loc) · 2.83 KB
/
three_credentials.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
from maximal_mechanisms import *
from utils import generate_all_binary_tuples
def get_all_majority_mechanisms() -> list[Mechanism]:
"""
Generates all majority profiles using a tie-breaking mechanism.
Doesn't exclude duplicates.
Returns:
list of tuples: A list where each tuple contains a label (str) and a profile (object).
"""
all_tie_breaks = generate_all_binary_tuples(6)
return [MajorityMechanism(3, lambda x, y: tie_breaker_function_3creds(x, y, tb), tb) for tb in all_tie_breaks]
def get_all_priority_mechanisms() -> list[Mechanism]:
"""
Generate all priority profiles based on all possible rules and exceptions.
Doesn't exclude duplicates.
Returns:
list of tuple: A list of tuples where each tuple contains a label (str)
describing the rule and exception, and the corresponding profile object.
"""
mechanisms = []
for rule in [[0, 1, 2], [1, 0, 2], [2, 0, 1], [0, 2, 1], [1, 2, 0], [2, 1, 0]]:
for exception in [True, False]:
m = PriorityMechanism(rule, exception)
mechanisms.append(m)
return mechanisms
def get_all_3cred_mechanisms():
return get_all_majority_mechanisms() + get_all_priority_mechanisms()
def get_complete_maximal_set() -> list[Mechanism]:
mechanisms = get_all_3cred_mechanisms()
unique_mechanisms = []
for m in mechanisms:
if m not in unique_mechanisms:
unique_mechanisms.append(m)
return unique_mechanisms
def find_best_mechanisms(probabilities: list[CredentialProbabilities]):
"""
Identifies the best mechanisms based on their success probabilities.
This function evaluates a list of mechanisms and determines which ones have the highest
success probability given a list of credential probabilities. It iterates through each
mechanism, calculates the total success probability for each scenario in the mechanism's
profile, and compares it to find the best mechanisms.
Args:
probabilities (list[CredentialProbabilities]): A list of credential probabilities
used to calculate the success probability of each mechanism.
Returns:
tuple: A tuple containing the best mechanisms and their success probability
"""
if len(probabilities) != 3:
raise ValueError("Number of probabilities must match number of credentials")
best_mechanisms = []
best_profile_value = 0
all_mechanisms = get_complete_maximal_set()
for M in all_mechanisms:
value = 0
for scenario in M.profile:
value += scenario.success_probability(probabilities)
print(value, M)
if value > best_profile_value:
best_profile_value = value
best_mechanisms = [M]
elif value == best_profile_value:
best_mechanisms.append(M)
return (best_mechanisms, best_profile_value)