Implement TransferTable for exclusions (WIP)

This commit is contained in:
RunasSudo 2021-09-11 01:19:38 +10:00
parent 99dbbcd5d5
commit fbdc32ba30
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 186 additions and 31 deletions

View File

@ -189,6 +189,10 @@ pub struct SubcmdOptions {
#[clap(help_heading=Some("OUTPUT"), long)] #[clap(help_heading=Some("OUTPUT"), long)]
sort_votes: bool, sort_votes: bool,
/// Show details of transfers to candidates during surplus distributions/candidate exclusions
#[clap(help_heading=Some("OUTPUT"), long)]
transfers_detail: bool,
/// Print votes to specified decimal places in results report /// Print votes to specified decimal places in results report
#[clap(help_heading=Some("OUTPUT"), long, default_value="2", value_name="dps")] #[clap(help_heading=Some("OUTPUT"), long, default_value="2", value_name="dps")]
pp_decimals: usize, pp_decimals: usize,
@ -285,6 +289,7 @@ where
cmd_opts.constraint_mode.into(), cmd_opts.constraint_mode.into(),
cmd_opts.hide_excluded, cmd_opts.hide_excluded,
cmd_opts.sort_votes, cmd_opts.sort_votes,
cmd_opts.transfers_detail,
cmd_opts.pp_decimals, cmd_opts.pp_decimals,
); );

View File

@ -20,6 +20,8 @@ use crate::logger::Logger;
use crate::numbers::Number; use crate::numbers::Number;
use crate::sharandom::SHARandom; use crate::sharandom::SHARandom;
use crate::stv::{self, STVOptions}; use crate::stv::{self, STVOptions};
use crate::stv::meek::BallotTree;
use crate::stv::transfers::TransferTable;
use itertools::Itertools; use itertools::Itertools;
@ -110,8 +112,8 @@ pub struct CountState<'a, N: Number> {
/// [CountCard] representing loss by fraction /// [CountCard] representing loss by fraction
pub loss_fraction: CountCard<'a, N>, pub loss_fraction: CountCard<'a, N>,
/// [crate::stv::meek::BallotTree] for Meek STV /// [BallotTree] for Meek STV
pub ballot_tree: Option<crate::stv::meek::BallotTree<'a, N>>, pub ballot_tree: Option<BallotTree<'a, N>>,
/// Values used to break ties, based on forwards tie-breaking /// Values used to break ties, based on forwards tie-breaking
pub forwards_tiebreak: Option<HashMap<&'a Candidate, usize>>, pub forwards_tiebreak: Option<HashMap<&'a Candidate, usize>>,
@ -135,6 +137,9 @@ pub struct CountState<'a, N: Number> {
/// [ConstraintMatrix] for constrained elections /// [ConstraintMatrix] for constrained elections
pub constraint_matrix: Option<ConstraintMatrix>, pub constraint_matrix: Option<ConstraintMatrix>,
/// Transfer table for this surplus/exclusion
pub transfer_table: Option<TransferTable<'a, N>>,
/// The type of stage being counted, etc. /// The type of stage being counted, etc.
pub title: StageKind<'a>, pub title: StageKind<'a>,
/// [Logger] for this stage of the count /// [Logger] for this stage of the count
@ -158,6 +163,7 @@ impl<'a, N: Number> CountState<'a, N> {
num_elected: 0, num_elected: 0,
num_excluded: 0, num_excluded: 0,
constraint_matrix: None, constraint_matrix: None,
transfer_table: None,
title: StageKind::FirstPreferences, title: StageKind::FirstPreferences,
logger: Logger { entries: Vec::new() }, logger: Logger { entries: Vec::new() },
}; };
@ -282,7 +288,9 @@ impl<'a, N: Number> CountState<'a, N> {
result.push_str(&format!("Quota: {:.dps$}\n", self.quota.as_ref().unwrap(), dps=opts.pp_decimals)); result.push_str(&format!("Quota: {:.dps$}\n", self.quota.as_ref().unwrap(), dps=opts.pp_decimals));
if stv::should_show_vre(opts) { if stv::should_show_vre(opts) {
result.push_str(&format!("Vote required for election: {:.dps$}\n", self.vote_required_election.as_ref().unwrap(), dps=opts.pp_decimals)); if let Some(vre) = &self.vote_required_election {
result.push_str(&format!("Vote required for election: {:.dps$}\n", vre, dps=opts.pp_decimals));
}
} }
return result; return result;

View File

@ -21,6 +21,7 @@ use super::sample;
use crate::constraints; use crate::constraints;
use crate::election::{Candidate, CandidateState, CountState, Parcel, StageKind, Vote}; use crate::election::{Candidate, CandidateState, CountState, Parcel, StageKind, Vote};
use crate::numbers::Number; use crate::numbers::Number;
use crate::stv::transfers::TransferTable;
use crate::ties; use crate::ties;
use std::cmp::max; use std::cmp::max;
@ -635,11 +636,7 @@ where
let value = match parcels.first() { Some(p) => Some(p.value_fraction.clone()), _ => None }; let value = match parcels.first() { Some(p) => Some(p.value_fraction.clone()), _ => None };
let mut candidate_transfers: HashMap<&Candidate, N> = HashMap::new(); let mut transfer_table = TransferTable::new();
for candidate in state.election.candidates.iter() {
candidate_transfers.insert(candidate, N::new());
}
let mut exhausted_transfers = N::new();
for parcel in parcels { for parcel in parcels {
// Count next preferences // Count next preferences
@ -657,11 +654,9 @@ where
}; };
// Record transfers // Record transfers
let transfers_orig = candidate_transfers.remove(candidate).unwrap(); transfer_table.add_transfers(&parcel.value_fraction, candidate, &entry.num_ballots);
candidate_transfers.insert(candidate, transfers_orig + &entry.num_ballots * &parcel.value_fraction);
let count_card = state.candidates.get_mut(candidate).unwrap(); let count_card = state.candidates.get_mut(candidate).unwrap();
count_card.ballot_transfers += parcel.num_ballots();
count_card.parcels.push(parcel); count_card.parcels.push(parcel);
} }
@ -673,11 +668,9 @@ where
}; };
// Record transfers // Record transfers
state.exhausted.ballot_transfers += parcel.num_ballots(); transfer_table.add_exhausted(&parcel.value_fraction, &result.exhausted.num_ballots);
exhausted_transfers += &result.exhausted.num_ballots * &parcel.value_fraction;
state.exhausted.parcels.push(parcel);
// TODO: Detailed transfers logs state.exhausted.parcels.push(parcel);
} }
if let ExclusionMethod::SingleStage = opts.exclusion { if let ExclusionMethod::SingleStage = opts.exclusion {
@ -697,22 +690,7 @@ where
} }
// Credit transferred votes // Credit transferred votes
// ballot_transfers updated above checksum += transfer_table.apply_to(state, opts);
for (candidate, mut votes) in candidate_transfers {
if let Some(dps) = opts.round_votes {
votes.floor_mut(dps);
}
let count_card = state.candidates.get_mut(candidate).unwrap();
count_card.transfer(&votes);
checksum += votes;
}
// Credit exhausted votes
if let Some(dps) = opts.round_votes {
exhausted_transfers.floor_mut(dps);
}
state.exhausted.transfer(&exhausted_transfers);
checksum += exhausted_transfers;
if !votes_remain { if !votes_remain {
// Finalise candidate votes // Finalise candidate votes

View File

@ -23,6 +23,8 @@ pub mod gregory;
pub mod meek; pub mod meek;
/// Random sample methods of surplus distributions /// Random sample methods of surplus distributions
pub mod sample; pub mod sample;
/// Transfer tables
pub mod transfers;
/// WebAssembly wrappers /// WebAssembly wrappers
//#[cfg(target_arch = "wasm32")] //#[cfg(target_arch = "wasm32")]
@ -155,6 +157,10 @@ pub struct STVOptions {
#[builder(default="false")] #[builder(default="false")]
pub sort_votes: bool, pub sort_votes: bool,
/// Show details of transfers to candidates during surplus distributions/candidate exclusions
#[builder(default="false")]
pub transfers_detail: bool,
/// Print votes to specified decimal places in results report /// Print votes to specified decimal places in results report
#[builder(default="2")] #[builder(default="2")]
pub pp_decimals: usize, pub pp_decimals: usize,

156
src/stv/transfers.rs Normal file
View File

@ -0,0 +1,156 @@
/* OpenTally: Open-source election vote counting
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use super::STVOptions;
use crate::election::{Candidate, CountState};
use crate::numbers::Number;
use std::collections::HashMap;
/// Table describing vote transfers during a surplus distribution or exclusion
pub struct TransferTable<'e, N: Number> {
/// Columns in the table
pub columns: Vec<TransferTableColumn<'e, N>>,
}
impl<'e, N: Number> TransferTable<'e, N> {
/// Return a new [TransferTable]
pub fn new() -> Self {
TransferTable {
columns: Vec::new(),
}
}
/// Record the specified transfer
pub fn add_transfers(&mut self, value_fraction: &N, candidate: &'e Candidate, ballots: &N) {
for col in self.columns.iter_mut() {
if &col.value_fraction == value_fraction {
col.add_transfers(candidate, ballots);
return;
}
}
let mut col = TransferTableColumn {
value_fraction: value_fraction.clone(),
cells: HashMap::new(),
exhausted: TransferTableCell { ballots: N::new() },
};
col.add_transfers(candidate, ballots);
self.columns.push(col);
}
/// Record the specified exhaustion
pub fn add_exhausted(&mut self, value_fraction: &N, ballots: &N) {
for col in self.columns.iter_mut() {
if &col.value_fraction == value_fraction {
col.exhausted.ballots += ballots;
return;
}
}
let col = TransferTableColumn {
value_fraction: value_fraction.clone(),
cells: HashMap::new(),
exhausted: TransferTableCell { ballots: ballots.clone() },
};
self.columns.push(col);
}
/// Apply the transfers described in the table to the count sheet
///
/// Credit continuing candidates and exhausted pile with the appropriate number of ballot papers and votes.
pub fn apply_to(&self, state: &mut CountState<N>, opts: &STVOptions) -> N {
// TODO: SumSurplusTransfers
let mut checksum = N::new();
// Credit transferred votes
for (candidate, count_card) in state.candidates.iter_mut() {
let mut votes_transferred = N::new();
let mut ballots_transferred = N::new();
for column in self.columns.iter() {
if let Some(cell) = column.cells.get(*candidate) {
votes_transferred += cell.ballots.clone() * &column.value_fraction;
ballots_transferred += &cell.ballots;
}
}
if let Some(dps) = opts.round_votes {
votes_transferred.floor_mut(dps);
}
count_card.transfer(&votes_transferred);
count_card.ballot_transfers += ballots_transferred;
checksum += votes_transferred;
}
// Credit exhausted votes
let mut votes_transferred = N::new();
let mut ballots_transferred = N::new();
for column in self.columns.iter() {
votes_transferred += column.exhausted.ballots.clone() * &column.value_fraction;
ballots_transferred += &column.exhausted.ballots;
}
if let Some(dps) = opts.round_votes {
votes_transferred.floor_mut(dps);
}
state.exhausted.transfer(&votes_transferred);
state.exhausted.ballot_transfers += ballots_transferred;
checksum += votes_transferred;
return checksum;
}
}
/// Column in a [TransferTable]
pub struct TransferTableColumn<'e, N: Number> {
/// Value fraction of ballots counted in this column
pub value_fraction: N,
/// Cells in this column
pub cells: HashMap<&'e Candidate, TransferTableCell<N>>,
/// Exhausted cell
pub exhausted: TransferTableCell<N>,
}
impl<'e, N: Number> TransferTableColumn<'e, N> {
/// Record the specified transfer
pub fn add_transfers(&mut self, candidate: &'e Candidate, ballots: &N) {
if let Some(cell) = self.cells.get_mut(candidate) {
cell.ballots += ballots;
} else {
let cell = TransferTableCell {
ballots: ballots.clone(),
};
self.cells.insert(candidate, cell);
}
}
}
/// Cell in a [TransferTable], representing transfers to one candidate at a particular value
pub struct TransferTableCell<N: Number> {
/// Ballots transferred to this candidate
pub ballots: N,
}

View File

@ -240,6 +240,7 @@ impl STVOptions {
min_threshold: String, min_threshold: String,
constraints_path: Option<String>, constraints_path: Option<String>,
constraint_mode: &str, constraint_mode: &str,
transfers_detail: bool,
pp_decimals: usize, pp_decimals: usize,
) -> Self { ) -> Self {
Self(stv::STVOptions::new( Self(stv::STVOptions::new(
@ -270,6 +271,7 @@ impl STVOptions {
constraint_mode.into(), constraint_mode.into(),
false, false,
false, false,
transfers_detail,
pp_decimals, pp_decimals,
)) ))
} }