Minor optimisations
This commit is contained in:
parent
6f5ed93657
commit
4205d9b262
@ -624,7 +624,9 @@ class BaseSTVCounter:
|
|||||||
count_card = self.candidates[candidate]
|
count_card = self.candidates[candidate]
|
||||||
|
|
||||||
if count_card.state == CandidateState.HOPEFUL:
|
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][1] += bc.ballot.value
|
||||||
next_preferences[candidate][2] += bc.ballot_value
|
next_preferences[candidate][2] += bc.ballot_value
|
||||||
break
|
break
|
||||||
|
@ -183,21 +183,24 @@ class BaseGregorySTVCounter(BaseSTVCounter):
|
|||||||
c, cc = candidates_excluded[0]
|
c, cc = candidates_excluded[0]
|
||||||
self._exclusion = (candidates_excluded, [[(c, p)] for p in cc.parcels])
|
self._exclusion = (candidates_excluded, [[(c, p)] for p in cc.parcels])
|
||||||
elif self.options['exclusion'] == 'by_value':
|
elif self.options['exclusion'] == 'by_value':
|
||||||
ballots = [(c, bc) for c, cc in candidates_excluded for p in cc.parcels for bc in p]
|
# Precompute ballot transfer values
|
||||||
|
|
||||||
# Sort ballots by value
|
|
||||||
__pragma__('opov')
|
|
||||||
ballots.sort(key=lambda x: x[1].ballot_value / x[1].ballot.value, reverse=True)
|
|
||||||
if self.options['round_tvs']:
|
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:
|
else:
|
||||||
# Round to 8 decimal places to consider equality
|
# Round to 8 decimal places to consider equality
|
||||||
# FIXME: Work out a better way of doing this
|
# 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__('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')
|
__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?
|
# 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:
|
else:
|
||||||
raise STVException('Invalid exclusion mode') # pragma: no cover
|
raise STVException('Invalid exclusion mode') # pragma: no cover
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user