Skip to content

Commit

Permalink
fix stv, check-stv for better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gdtsouk committed May 26, 2014
1 parent dd523a8 commit 098de1c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
26 changes: 24 additions & 2 deletions check_stv/check-stv.py
Expand Up @@ -70,9 +70,31 @@
item[0] = str(item[0])

if ecounting_results != local_results:
print 'Results NOT exactly the same'
if len(ecounting_results) != len(local_results):
print "Different number of elected candidates!"
else:
comp = ''.join([str(int(a == b)) for a, b in
zip(local_results, ecounting_results)])
print "Round comparison: ", comp
for c, a, b in zip(comp, local_results, ecounting_results):
if c == '1':
continue
if a[0] != b[0]:
print "Different Candidates Elected!"
print a
print b
elif a[1] != b[1]:
print "Different Round of Election!"
print a
print b
else:
for i, aa, bb in zip(itertools.count(), a[2], b[2]):
if aa == bb:
continue
print i, aa, bb
else:
pass # print 'Results are exactlu the same'
pass # print 'Results are exactly the same'

same_candidates_elected = True
same_rounds_of_election = True
same_votes_on_round = True
Expand Down
37 changes: 23 additions & 14 deletions stv/stv.py
Expand Up @@ -227,6 +227,17 @@ def count_description(vote_count, candidates):
candidates))


def update_candidate_counts(full_data, current_round, vote_count, hopefuls):
hopeful_set = set(hopefuls)
for candidate, candidate_rounds in full_data:
if candidate not in hopeful_set:
continue

vote_no_decimal = float(vote_count[candidate]) * 10000
vote_rounded = int(round(vote_no_decimal))
candidate_rounds.append([current_round, vote_rounded])


def count_stv(ballots, seats, droop = True, constituencies = None,
quota_limit = 0, rnd_gen=None, logger=logger):
"""Performs a STV vote for the given ballots and number of seats.
Expand Down Expand Up @@ -300,16 +311,7 @@ def count_stv(ballots, seats, droop = True, constituencies = None,
description = count_description(vote_count, hopefuls)

# using this for testing
data = description.split(';')
for item in data:
item = item.split('=')
item[0] = item[0].strip()
item[1] = item[1].strip()
for candidate in full_data:
if candidate[0] == item[0]:
vote_no_decimal = float(item[1]) * 10000
vote_rounded = int(round(vote_no_decimal))
candidate[1].append([current_round, vote_rounded])
update_candidate_counts(full_data, current_round, vote_count, hopefuls)

logger.info(LOG_MESSAGE.format(action=Action.COUNT,
desc=description))
Expand Down Expand Up @@ -379,11 +381,18 @@ def count_stv(ballots, seats, droop = True, constituencies = None,
logger.info(LOG_MESSAGE.format(action=Action.ZOMBIES,
desc=description))
best_candidate = eliminated.pop()
elect_reject(best_candidate, vote_count, constituencies,
quota_limit, current_round,
elected, rejected, constituencies_elected,
logger=logger)
was_elected = elect_reject(best_candidate, vote_count, constituencies,
quota_limit, current_round,
elected, rejected, constituencies_elected,
logger=logger)
if was_elected:
update_candidate_counts(full_data, current_round, vote_count,
[best_candidate])
redistribute_ballots(best_candidate, 1.0, hopefuls, allocated,
vote_count, logger=logger)

current_round += 1
num_elected = len(elected)

return elected, vote_count, full_data

Expand Down

0 comments on commit 098de1c

Please sign in to comment.