-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdigest_result.py
More file actions
77 lines (63 loc) · 3.03 KB
/
digest_result.py
File metadata and controls
77 lines (63 loc) · 3.03 KB
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
def digest_plurality_vote_honest_result(dict_result, nr_candidates, nr_voters):
# TODO: complete there the code to digest the output of the honest version
print(
"Update 'digest_plurality_vote_honest_result()' functions to digest the result."
)
return None
def digest_plurality_vote_dishonest_with_abort_result(
dict_result, nr_candidates, nr_voters
):
# TODO: complete there the code to digest the output of the dishonest with abort version
print(
"Update 'digest_plurality_vote_dishonest_with_abort_result()' functions to digest the result."
)
return None
def digest_plurality_vote_robust_result(dict_result, nr_candidates, nr_voters):
"""
Digests the robust result of a plurality voting process, identifying the winner,
the votes per candidate, and any potential cheaters.
Parameters:
- dict_result (dict): A dictionary containing the robust result of the plurality
voting process. It should contain keys for final vote counts
for each candidate, checks for sum and product rules per voter,
and actions if cheating is detected.
- nr_candidates (int): The total number of candidates in the voting process.
- nr_voters (int): The total number of voters in the voting process.
Returns:
- winner (int): The index of the winning candidate.
- votes_per_candidate (list): A list containing the number of votes received by
each candidate.
- cheaters (list): A list containing the IDs of any potential cheaters.
"""
# check cheaters
set_of_cheaters = set()
for v in range(nr_voters):
if dict_result["check_sum_v" + str(v)] != nr_candidates + 1:
# add the voter id to set_of_cheaters
set_of_cheaters.add(v)
for c in range(nr_candidates):
if dict_result["check_prod_v" + str(v) + "_c" + str(c)] != 0:
# add the voter id to set_of_cheaters
set_of_cheaters.add(v)
# collect final votes
votes_per_candidate = []
for c in range(nr_candidates):
# read nr of votes for candidate 'c'
votes_per_candidate.append(dict_result["final_vote_count_c" + str(c)].value)
# revert action of cheaters for all candidates
for cheater in set_of_cheaters:
# if sum rule was broken
if dict_result["check_sum_v" + str(cheater)] != nr_candidates + 1:
for c in range(nr_candidates):
votes_per_candidate[c] -= dict_result[
"if_sum_cheat_open_v" + str(cheater) + "_c" + str(c)
].value
# if product rule was broken
else:
for c in range(nr_candidates):
votes_per_candidate[c] -= dict_result[
"if_prod_cheat_open_v" + str(cheater) + "_c" + str(c)
].value
# define winner
winner = max(range(len(votes_per_candidate)), key=lambda i: votes_per_candidate[i])
return winner, votes_per_candidate, list(set_of_cheaters)