From c9ff0a16fb40343f688ed0524d2848a7d47e20ec Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 27 Dec 2020 23:30:54 +1100 Subject: [PATCH] Fix error/bug when excluding candidates in exclusive Gregory --- html/worker.js | 1 + pyRCV2/method/parcels.py | 71 ++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/html/worker.js b/html/worker.js index ef23dae..3959d0a 100644 --- a/html/worker.js +++ b/html/worker.js @@ -104,6 +104,7 @@ function stepElection() { postMessage({'type': 'require_input', 'message': ex.message}); break; } else { + console.error(ex); throw ex; } } diff --git a/pyRCV2/method/parcels.py b/pyRCV2/method/parcels.py index dc4f7e8..acab4c4 100644 --- a/pyRCV2/method/parcels.py +++ b/pyRCV2/method/parcels.py @@ -127,50 +127,51 @@ class ParcelledEGSTVCounter(UIGSTVCounter): next_preferences = SafeDict([(c, []) for c, cc in self.candidates.items()]) next_exhausted = [] - parcel = count_card.parcels[0] - count_card.parcels.remove(parcel) - - # Count next preferences - for ballot, ballot_value in parcel: - __pragma__('opov') - candidate = next((c for c in ballot.preferences if self.candidates[c].state == CandidateState.HOPEFUL), None) - __pragma__('noopov') + if len(count_card.parcels) > 0: + parcel = count_card.parcels[0] + count_card.parcels.remove(parcel) - if candidate is not None: + # Count next preferences + for ballot, ballot_value in parcel: __pragma__('opov') - next_preferences[candidate].append((ballot, ballot_value)) + candidate = next((c for c in ballot.preferences if self.candidates[c].state == CandidateState.HOPEFUL), None) __pragma__('noopov') - else: - next_exhausted.append((ballot, ballot_value)) - - # Make transfers - for candidate, cand_ballots in next_preferences.items(): - if len(cand_ballots) == 0: - continue + + if candidate is not None: + __pragma__('opov') + next_preferences[candidate].append((ballot, ballot_value)) + __pragma__('noopov') + else: + next_exhausted.append((ballot, ballot_value)) - new_parcel = [] - __pragma__('opov') - self.candidates[candidate].parcels.append(new_parcel) - __pragma__('noopov') + # Make transfers + for candidate, cand_ballots in next_preferences.items(): + if len(cand_ballots) == 0: + continue + + new_parcel = [] + __pragma__('opov') + self.candidates[candidate].parcels.append(new_parcel) + __pragma__('noopov') + + num_votes = sum((bv for b, bv in cand_ballots), Rational('0')) + __pragma__('opov') + self.candidates[candidate].transfers += num_votes.to_num() + count_card.transfers -= num_votes.to_num() + __pragma__('noopov') + + for ballot, ballot_value in cand_ballots: + new_parcel.append((ballot, ballot_value)) - num_votes = sum((bv for b, bv in cand_ballots), Rational('0')) + # Credit exhausted votes + num_votes = sum((bv for b, bv in next_exhausted), Rational('0')) __pragma__('opov') - self.candidates[candidate].transfers += num_votes.to_num() + self.exhausted.transfers += num_votes.to_num() count_card.transfers -= num_votes.to_num() __pragma__('noopov') - for ballot, ballot_value in cand_ballots: - new_parcel.append((ballot, ballot_value)) - - # Credit exhausted votes - num_votes = sum((bv for b, bv in next_exhausted), Rational('0')) - __pragma__('opov') - self.exhausted.transfers += num_votes.to_num() - count_card.transfers -= num_votes.to_num() - __pragma__('noopov') - - for ballot, ballot_value in next_exhausted: - self.exhausted.ballots.append((ballot, ballot_value)) + for ballot, ballot_value in next_exhausted: + self.exhausted.ballots.append((ballot, ballot_value)) if len(count_card.parcels) == 0: count_card.state = CandidateState.EXCLUDED