Minor optimisations

This commit is contained in:
RunasSudo 2021-01-19 02:36:36 +11:00
parent 6f5ed93657
commit 4205d9b262
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 15 additions and 10 deletions

View File

@ -624,7 +624,9 @@ class BaseSTVCounter:
count_card = self.candidates[candidate]
if count_card.state == CandidateState.HOPEFUL:
next_preferences[candidate][0].append(BallotInCount(bc.ballot, bc.ballot_value, i))
#next_preferences[candidate][0].append(BallotInCount(bc.ballot, bc.ballot_value, i))
bc.last_preference = i
next_preferences[candidate][0].append(bc)
next_preferences[candidate][1] += bc.ballot.value
next_preferences[candidate][2] += bc.ballot_value
break

View File

@ -183,21 +183,24 @@ class BaseGregorySTVCounter(BaseSTVCounter):
c, cc = candidates_excluded[0]
self._exclusion = (candidates_excluded, [[(c, p)] for p in cc.parcels])
elif self.options['exclusion'] == 'by_value':
ballots = [(c, bc) for c, cc in candidates_excluded for p in cc.parcels for bc in p]
# Sort ballots by value
__pragma__('opov')
ballots.sort(key=lambda x: x[1].ballot_value / x[1].ballot.value, reverse=True)
# Precompute ballot transfer values
if self.options['round_tvs']:
ballots_by_value = groupby(ballots, lambda x: self.round_tv(x[1].ballot_value / x[1].ballot.value))
__pragma__('opov')
ballots = [(c, bc, self.round_tv(bc.ballot_value / bc.ballot.value)) for c, cc in candidates_excluded for p in cc.parcels for bc in p]
__pragma__('noopov')
else:
# Round to 8 decimal places to consider equality
# FIXME: Work out a better way of doing this
ballots_by_value = groupby(ballots, lambda x: (x[1].ballot_value / x[1].ballot.value).round(8, x[1].ballot_value.ROUND_DOWN))
__pragma__('noopov')
__pragma__('opov')
ballots = [(c, bc, (bc.ballot_value / bc.ballot.value).round(8, bc.ballot_value.ROUND_DOWN)) for c, cc in candidates_excluded for p in cc.parcels for bc in p]
__pragma__('noopov')
# Sort ballots by value
ballots.sort(key=lambda x: x[2], reverse=True)
ballots_by_value = groupby(ballots, lambda x: x[2])
# TODO: Can we combine ballots for each candidate within each stage?
self._exclusion = (candidates_excluded, [[(c, [bc]) for c, bc in x] for x in ballots_by_value])
self._exclusion = (candidates_excluded, [[(c, [bc]) for c, bc, tv in x] for x in ballots_by_value])
else:
raise STVException('Invalid exclusion mode') # pragma: no cover