Improve performance of Scottish STV

Remove reliance on normalising ballot papers
This commit is contained in:
RunasSudo 2021-08-19 18:08:24 +10:00
parent b9e66fde73
commit 85b695c133
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A
4 changed files with 10 additions and 6 deletions

View File

@ -196,7 +196,7 @@ In the BLT file format, each set of preferences can have a specified weight
When ballots are not normalised (default), a set of preferences with weight *n* > 1 is represented as a single ballot with value *n*. This is known as [list-packed ballots](http://www.votingmatters.org.uk/ISSUE21/I21P1.pdf). When ballots are not normalised (default), a set of preferences with weight *n* > 1 is represented as a single ballot with value *n*. This is known as [list-packed ballots](http://www.votingmatters.org.uk/ISSUE21/I21P1.pdf).
When ballots are normalised, a set of preferences with weight *n* > 1 is instead converted to *n* ballots each with value 1. This is generally required only when the rules directly deal with individual ballot values, such as when *Sum surplus transfers* is set to *Per ballot*. When ballots are normalised, a set of preferences with weight *n* > 1 is instead converted to *n* ballots each with value 1.
## Count optimisations ## Count optimisations

View File

@ -438,7 +438,7 @@ function changePreset() {
document.getElementById('selNumbers').value = 'fixed'; document.getElementById('selNumbers').value = 'fixed';
document.getElementById('txtDP').value = '5'; document.getElementById('txtDP').value = '5';
document.getElementById('txtPPDP').value = '5'; document.getElementById('txtPPDP').value = '5';
document.getElementById('chkNormaliseBallots').checked = true; document.getElementById('chkNormaliseBallots').checked = false;
document.getElementById('chkRoundQuota').checked = true; document.getElementById('chkRoundQuota').checked = true;
document.getElementById('txtRoundQuota').value = '0'; document.getElementById('txtRoundQuota').value = '0';
document.getElementById('chkRoundVotes').checked = false; document.getElementById('chkRoundVotes').checked = false;

View File

@ -226,15 +226,19 @@ where
SumSurplusTransfersMode::PerBallot => { SumSurplusTransfersMode::PerBallot => {
// Sum transfer per each individual ballot // Sum transfer per each individual ballot
// TODO: This could be moved to distribute_surplus to avoid looping over the votes and calculating transfer values twice // TODO: This could be moved to distribute_surplus to avoid looping over the votes and calculating transfer values twice
let mut new_value_fraction = reweight_value_fraction(orig_value_fraction, surplus, is_weighted, surplus_fraction, surplus_denom, opts.round_surplus_fractions);
if let Some(dps) = opts.round_votes {
new_value_fraction.floor_mut(dps);
}
let mut result = N::new(); let mut result = N::new();
for vote in entry.votes.iter() { for vote in entry.votes.iter() {
let mut vote_value = &vote.ballot.orig_value * &reweight_value_fraction(orig_value_fraction, surplus, is_weighted, surplus_fraction, surplus_denom, opts.round_surplus_fractions); let mut vote_value = &new_value_fraction * &vote.ballot.orig_value;
if let Some(dps) = opts.round_votes { if let Some(dps) = opts.round_votes {
vote_value.floor_mut(dps); vote_value.floor_mut(dps);
} }
result += vote_value; result += vote_value;
} }
//state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.dps$} votes.", entry.num_ballots, entry.num_votes, dps=opts.pp_decimals));
return result; return result;
} }
} }

View File

@ -32,7 +32,7 @@ fn scotland_linn07_fixed5() {
//.round_votes(Some(5)) //.round_votes(Some(5))
.round_quota(Some(0)) .round_quota(Some(0))
.sum_surplus_transfers(stv::SumSurplusTransfersMode::PerBallot) .sum_surplus_transfers(stv::SumSurplusTransfersMode::PerBallot)
.normalise_ballots(true) //.normalise_ballots(true)
.quota_criterion(stv::QuotaCriterion::GreaterOrEqual) .quota_criterion(stv::QuotaCriterion::GreaterOrEqual)
.early_bulk_elect(false) .early_bulk_elect(false)
.pp_decimals(5) .pp_decimals(5)
@ -50,7 +50,7 @@ fn scotland_linn07_gfixed5() {
.round_votes(Some(5)) .round_votes(Some(5))
.round_quota(Some(0)) .round_quota(Some(0))
.sum_surplus_transfers(stv::SumSurplusTransfersMode::PerBallot) .sum_surplus_transfers(stv::SumSurplusTransfersMode::PerBallot)
.normalise_ballots(true) //.normalise_ballots(true)
.quota_criterion(stv::QuotaCriterion::GreaterOrEqual) .quota_criterion(stv::QuotaCriterion::GreaterOrEqual)
.early_bulk_elect(false) .early_bulk_elect(false)
.pp_decimals(5) .pp_decimals(5)