-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightedVotingSystemsCalculator.py
185 lines (124 loc) · 5.07 KB
/
WeightedVotingSystemsCalculator.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'''Anish Reddy - Weighted Voting Systems Calculator'''
import itertools
class player:
totalVotes = 0
def __init__(self, name, weight):
self.name = name
self.weight = weight
player.totalVotes+=self.weight
def BPIIndex(self):
if hasattr(self, 'timesCritical'):
return self.timesCritical/player.totalCritical
else:
return None
def BPIIndexDetails(self):
if hasattr(self, 'timesCritical'):
return f'{self.timesCritical}/{player.totalCritical}', round(self.timesCritical/player.totalCritical,5)
else:
return 'n/a', 0
def SSPIIndexDetails(self):
if hasattr(self, 'timesPivotal'):
return f'{self.timesPivotal}/{player.totalPivotal}', round(self.timesPivotal/player.totalPivotal,5)
else:
return 'n/a', 0
def __repr__(self):
return self.name
def __str__(self):
return f'{self.name}; Weight: {self.weight}; PercentVote: {round(self.weight*100/player.totalVotes,2)}%; BPI: {self.BPIIndexDetails()}; SSPI: {self.SSPIIndexDetails()};'
def findAllCoalitions(playerList):
allCoalitions = []
for i in range(len(playerList) + 1):
for subset in itertools.combinations(playerList, i):
votes = 0
for voter in subset:
votes += voter.weight
allCoalitions.append([list(subset),votes])
return allCoalitions
# i = index of each list within wC --> [coalition, total votes]
# j = index of each player object within i
def BanzhafPowerIndex(playerList, quota):
player.totalCritical = 0
for voter in playerList:
voter.timesCritical = 0
wCoalitions = []
for i in range(len(playerList) + 1):
for subset in itertools.combinations(playerList, i):
votes = 0
for voter in subset:
votes += voter.weight
if votes>=quota:
wCoalitions.append([list(subset),votes])
bpiList = wCoalitions.copy()
for i in range(len(bpiList)):
for j in range(len(bpiList[i][0])):
if bpiList[i][1] - bpiList[i][0][j].weight < quota:
# Updates objects first
bpiList[i][0][j].timesCritical +=1
player.totalCritical+=1
# Replaces object with string indicating criticality
bpiList[i][0][j] = bpiList[i][0][j].name + "*"
return wCoalitions, bpiList
def ShapleyShubikPowerIndex(playerList, quota):
player.totalPivotal = 0
for voter in playerList:
voter.timesPivotal = 0
coalitionPermutations = []
for subset in itertools.permutations(playerList):
coalitionPermutations.append(list(subset))
sspiList = coalitionPermutations.copy()
for permutationIndex in range(len(sspiList)):
votes = 0
index = 0
while votes<quota and index<len(playerList):
votes+=sspiList[permutationIndex][index].weight
index+=1
index-=1
sspiList[permutationIndex][index].timesPivotal+=1
player.totalPivotal+=1
sspiList[permutationIndex][index] = sspiList[permutationIndex][index].name+'*'
return coalitionPermutations, sspiList
def printList(list):
for i in list:
print(i)
print(f'Total: {len(list)}')
def getBPI(weightsList):
names = ["a","b","c","d","e","f"]
player.totalVotes = 0
pList = []
bpiList = []
for i in range(len(weightsList)):
pList.append(player(names[i],weightsList[i]))
BanzhafPowerIndex(pList,int(player.totalVotes/2)+1)
for i in pList:
bpiList.append(round(i.BPIIndex(),4))
return bpiList
def runProgram():
'''Voting System (Edit these)'''
quota = 133
playerList = [
player("R", 91),
player("D", 118),
player("I", 55),
# player("D", 15),
# player("E", 7),
# player("F", 6),
]
'''Running Stuff'''
# Returns all winning coalitions (as objects --> usable)
allCoalitions = findAllCoalitions(playerList)
# Returns winning coalitions with and without critical players starred respectively (2 outputs)
# Adds BPI to objects
allWinningCoalitions, BPIMarkedList = BanzhafPowerIndex(playerList, quota)
# Returns all permutations with and without pivotal players starred respectively (2 outputs)
# Adds SSPI to objects
allPermutations, SSPIMarkedList = ShapleyShubikPowerIndex(playerList, quota)
#Prints one of the lists above
print('---------------------------------------\nBPI List:\n---------------------------------------')
printList(BPIMarkedList)
print('\n---------------------------------------\nSSPI List:\n---------------------------------------')
printList(SSPIMarkedList)
print('\n---------------------------------------\nPlayer Details:\n---------------------------------------')
printList(playerList)
print('\n------------------------------------------------------------------------------\n\n')
# Enable this during use
# runProgram()