diff --git a/check_stv/check-stv.py b/check_stv/check-stv.py index 82432b0d1..4c8bb5c4f 100644 --- a/check_stv/check-stv.py +++ b/check_stv/check-stv.py @@ -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 diff --git a/stv/stv.py b/stv/stv.py index 1a25e7228..6ecd25518 100644 --- a/stv/stv.py +++ b/stv/stv.py @@ -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. @@ -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)) @@ -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