Fix error/bug when excluding candidates in exclusive Gregory

This commit is contained in:
RunasSudo 2020-12-27 23:30:54 +11:00
parent 7ad142f19d
commit c9ff0a16fb
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 37 additions and 35 deletions

View File

@ -104,6 +104,7 @@ function stepElection() {
postMessage({'type': 'require_input', 'message': ex.message}); postMessage({'type': 'require_input', 'message': ex.message});
break; break;
} else { } else {
console.error(ex);
throw ex; throw ex;
} }
} }

View File

@ -127,50 +127,51 @@ class ParcelledEGSTVCounter(UIGSTVCounter):
next_preferences = SafeDict([(c, []) for c, cc in self.candidates.items()]) next_preferences = SafeDict([(c, []) for c, cc in self.candidates.items()])
next_exhausted = [] next_exhausted = []
parcel = count_card.parcels[0] if len(count_card.parcels) > 0:
count_card.parcels.remove(parcel) parcel = count_card.parcels[0]
count_card.parcels.remove(parcel)
# Count next preferences # Count next preferences
for ballot, ballot_value in parcel: 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 candidate is not None:
__pragma__('opov') __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') __pragma__('noopov')
else:
next_exhausted.append((ballot, ballot_value))
# Make transfers if candidate is not None:
for candidate, cand_ballots in next_preferences.items(): __pragma__('opov')
if len(cand_ballots) == 0: next_preferences[candidate].append((ballot, ballot_value))
continue __pragma__('noopov')
else:
next_exhausted.append((ballot, ballot_value))
new_parcel = [] # 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))
# Credit exhausted votes
num_votes = sum((bv for b, bv in next_exhausted), Rational('0'))
__pragma__('opov') __pragma__('opov')
self.candidates[candidate].parcels.append(new_parcel) self.exhausted.transfers += num_votes.to_num()
__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() count_card.transfers -= num_votes.to_num()
__pragma__('noopov') __pragma__('noopov')
for ballot, ballot_value in cand_ballots: for ballot, ballot_value in next_exhausted:
new_parcel.append((ballot, ballot_value)) self.exhausted.ballots.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))
if len(count_card.parcels) == 0: if len(count_card.parcels) == 0:
count_card.state = CandidateState.EXCLUDED count_card.state = CandidateState.EXCLUDED