Created
September 28, 2018 08:39
-
-
Save ashwinreddy/7b5e17b829d171db1d708c1b8a15358b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import numpy as np | |
class BanzhafPower(object): | |
def __init__(self, voting_system): | |
self.voting_system = voting_system | |
self.generate_coalitions() | |
self.compute_critical_count() | |
def generate_coalitions(self): | |
"""Finds the power set of the set of players | |
""" | |
s = list(range(self.voting_system.num_players)) | |
self.coalitions = itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(2,len(s)+1)) | |
def compute_critical_count(self): | |
self.critical_counts = np.zeros(self.voting_system.num_players) | |
for coalition in self.coalitions: | |
W = sum([self.voting_system.weights[player] for player in coalition]) | |
if W >= self.voting_system.quota: | |
threshold = W - self.voting_system.quota | |
for player in coalition: | |
if threshold < self.voting_system.weights[player]: | |
self.critical_counts[player] += 1 | |
@property | |
def index(self): | |
return self.critical_counts / sum(self.critical_counts) | |
class WeightedVoting(object): | |
def __init__(self, quota, weights): | |
self._quota = quota | |
self._weights = np.sort(weights)[::-1] | |
self.check_problems() | |
def check_problems(self): | |
if self._quota <= self.votes / 2: | |
print("Potential for Anarchy") | |
if self._quota > self.votes: | |
print("Potential for Gridlock") | |
if self._weights[0] >= self._quota: | |
print("Dictator") | |
@property | |
def votes(self): | |
return sum(self._weights) | |
@property | |
def num_players(self): | |
return len(self.weights) | |
@property | |
def weights(self): | |
return self._weights | |
@property | |
def quota(self): | |
return self._quota | |
print(BanzhafPower(WeightedVoting(4, [3,2,1])).index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment