Subversion Repositories Local Hare Voting

Rev

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

Rev 40 Rev 44
Line 71... Line 71...
71
    global randcalls
71
    global randcalls
72
    excess = len(tally[winner]) - quota
72
    excess = len(tally[winner]) - quota
73
    if excess <= 0:
73
    if excess <= 0:
74
        trace("\tno excess ballots to redistribute")
74
        trace("\tno excess ballots to redistribute")
75
    else:
75
    else:
-
 
76
	lEffective = gatherEffectiveBallots(winner, winners, losers, tally)
-
 
77
        nRedistribute = min(excess, len(lEffective))
76
        trace("\tredistributing %d excess ballot(s) at random from %s" %
78
        trace("\tredistributing %d effective of %d excess ballot(s) at random from %s" %
77
              (excess, winner))
79
              (nRedistribute, excess, winner))
78
        while len(tally[winner]) > quota:
-
 
79
            i = int(random.uniform(0, len(tally[winner])))
80
        for ballot in random.sample(lEffective, nRedistribute):
80
            randcalls += 1
81
            randcalls += 1
81
            trace("\trandom choice = ballot %d" % (i+1))
82
            trace("\trandom choice = %s" % ballot)
82
            ballot = tally[winner][i]
83
            tally[winner].remove(ballot)
83
            tally[winner] = tally[winner][0:i] + tally[winner][i+1:]
84
            redistributeBallot(ballot, winners, losers, tally)
84
            redistributeBallot(ballot, winner, winners, losers, tally)
85
            nRedistribute -= 1
85
    traceTally(quota, tally)
86
    traceTally(quota, tally)
86
 
87
 
-
 
88
def gatherEffectiveBallots(winner, winners, losers, tally):
-
 
89
    lEffective = []
-
 
90
    for ballot in tally[winner]:
-
 
91
        for candidateTo in ballot:
-
 
92
           if candidateTo not in winners and candidateTo not in losers:
-
 
93
                lEffective.append(ballot)
-
 
94
                break
-
 
95
    return lEffective
-
 
96
 
87
def redistributeBallot(ballot, candidateFrom, winners, losers, tally):
97
def redistributeBallot(ballot, winners, losers, tally):
88
    for candidateTo in ballot:
98
    for candidateTo in ballot:
89
        if candidateTo not in winners and candidateTo not in losers:
99
        if candidateTo not in winners and candidateTo not in losers:
90
            trace("\tto %s: %s" % (candidateTo, ballot))
100
            trace("\tto %s: %s" % (candidateTo, ballot))
91
            if not tally.has_key(candidateTo): tally[candidateTo] = []
101
            if not tally.has_key(candidateTo): tally[candidateTo] = []
92
            tally[candidateTo].append(ballot)
102
            tally[candidateTo].append(ballot)
Line 122... Line 132...
122
    else:
132
    else:
123
        trace("\tredistributing %d ballot(s) from %s" % (excess, loser))
133
        trace("\tredistributing %d ballot(s) from %s" % (excess, loser))
124
        while len(tally[loser]) > 0:
134
        while len(tally[loser]) > 0:
125
            ballot = tally[loser][0]
135
            ballot = tally[loser][0]
126
            tally[loser] = tally[loser][1:]
136
            tally[loser] = tally[loser][1:]
127
            redistributeBallot(ballot, loser, winners, losers, tally)
137
            redistributeBallot(ballot, winners, losers, tally)
128
    traceTally(quota, tally)
138
    traceTally(quota, tally)
129
    return
139
    return
130
 
140
 
131
def traceTally(quota, tally):
141
def traceTally(quota, tally):
132
    global fTrace
142
    global fTrace