Subversion Repositories Local Hare Voting

Rev

Rev 46 | Rev 52 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 46 Rev 51
Line 17... Line 17...
17
## Note that plurals are generally used to indicate lists of other
17
## Note that plurals are generally used to indicate lists of other
18
## items, e.g., ballots is a list of ballot items.
18
## items, e.g., ballots is a list of ballot items.
19
##
19
##
20
## To download the complete source distribution, use Subversion:
20
## To download the complete source distribution, use Subversion:
21
##
21
##
22
##	% svn co http://www.bikmort.com/hare
22
##      % svn co http://www.bikmort.com/hare
23
##
23
##
24
 
24
 
25
import sys
25
import sys
26
import math 
26
import math 
27
import random
27
import random
28
 
28
 
29
fdOut = None
29
fdOut = None
30
fTrace = 1
30
fTrace = 1
31
randcalls = 0
31
randcalls = 0
32
 
32
 
-
 
33
class Universe:
-
 
34
    p = 1               # probability of this universe existing
-
 
35
    winners = None
-
 
36
    losers = None
-
 
37
    tally = None
-
 
38
 
-
 
39
    def getP(self):
-
 
40
        print "instance p =", self.p
-
 
41
        print "class p =", Universe.p
-
 
42
 
-
 
43
print Universe.p
-
 
44
x = Universe()
-
 
45
print x.p
-
 
46
x.p = 5
-
 
47
print Universe.p
-
 
48
print x.p
-
 
49
x.getP()
-
 
50
 
33
def trace(s):
51
def trace(s):
34
    global fTrace, fdOut
52
    global fTrace, fdOut
35
    if fTrace: print >>fdOut, s
53
    if fTrace: print >>fdOut, s
36
    return
54
    return
37
 
55
 
38
def findWinner(winners, losers, tally, quota, nWinners):
56
def findWinner(winners, losers, tally, quota, nWinners):
39
    global randcalls
57
    global randcalls
40
    cWin = quota       # number of votes for highest winner
58
    cWin = quota        # number of votes for highest winner
41
    lWin = []          # list of candidates with highest votes
59
    lWin = []           # list of candidates with highest votes
42
    for c in tally.keys():
60
    for c in tally.keys():
43
        if c not in losers and c not in winners and len(tally[c]) >= cWin:
61
        if c not in losers and c not in winners and len(tally[c]) >= cWin:
44
            if len(tally[c]) == cWin:
62
            if len(tally[c]) == cWin:
45
                lWin.append(c)
63
                lWin.append(c)
46
            else:
64
            else:
Line 72... Line 90...
72
    global randcalls
90
    global randcalls
73
    excess = len(tally[winner]) - quota
91
    excess = len(tally[winner]) - quota
74
    if excess <= 0:
92
    if excess <= 0:
75
        trace("\tno excess ballots to redistribute")
93
        trace("\tno excess ballots to redistribute")
76
    else:
94
    else:
77
	lEffective = gatherEffectiveBallots(winner, winners, losers, tally)
95
        lEffective = gatherEffectiveBallots(winner, winners, losers, tally)
78
        nRedistribute = min(excess, len(lEffective))
96
        nRedistribute = min(excess, len(lEffective))
79
        trace("\tredistributing %d effective of %d excess ballot(s) at random from %s" %
97
        trace("\tredistributing %d effective of %d excess ballot(s) at random from %s" %
80
              (nRedistribute, excess, winner))
98
              (nRedistribute, excess, winner))
81
        for ballot in random.sample(lEffective, nRedistribute):
99
        for ballot in random.sample(lEffective, nRedistribute):
82
            randcalls += 1
100
            randcalls += 1