Created
January 16, 2018 08:48
-
-
Save OlavHN/3ed89d9d81b50d2a389f860614794322 to your computer and use it in GitHub Desktop.
Propagate votes example
This file contains 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
from scipy import sparse | |
class Proposition: | |
def __init__(self, title): | |
self.title = title | |
self.votes = {} | |
self.delegations = {} | |
def vote(self, user, vote): | |
# TODO: Remove possible delegation | |
self.votes[user] = vote | |
def delegate(self, delegator, delegatee): | |
# TODO: Check delegator hasn't voted | |
self.delegations[delegator] = delegatee | |
def get_explicit_votes(self, num_users): | |
users, votes = zip(*self.votes.items()) | |
explicit_votes = sparse.csr_matrix( | |
(votes, ([0] * len(users), users)), shape=(1, num_users)) | |
delegators, delegatees = zip(*self.delegations.items()) | |
delegations = sparse.csc_matrix( | |
([1] * len(self.delegations), (delegatees, delegators)), | |
shape=(num_users, num_users)) | |
return self._propagate(explicit_votes, delegations) | |
def _propagate(self, votes, delegations): | |
voted = votes | |
while len(voted.data): | |
voted @= delegations | |
votes += voted | |
return votes | |
def main(): | |
p = Proposition("More cookies") | |
p.vote(0, 1) | |
p.vote(2, 0) | |
p.vote(1, -1) | |
p.delegate(4, 0) | |
p.delegate(5, 4) | |
votes = p.get_explicit_votes(6) | |
print(votes.todense()) | |
if __name__ == '__main__': | |
main() |
Ja, vil si at bruker 0 stemte "1", bruker 1: "-1" og e.g. bruker 4 stemte samme som bruker 0 og bruker 5 stemte samme som bruker 4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. Fikk kjørt det. Output:
[[ 1 -1 0 0 1 1]]