Add tests for Meek STV

This commit is contained in:
RunasSudo 2021-06-16 20:28:03 +10:00
parent 151c9e484e
commit 2ea8b4b757
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
13 changed files with 123 additions and 122 deletions

View File

@ -38,7 +38,7 @@ fn get_factor() -> &'static IBig {
} }
/// Fixed-point number /// Fixed-point number
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Fixed(IBig); pub struct Fixed(IBig);
impl Fixed { impl Fixed {

View File

@ -44,7 +44,7 @@ fn get_factor_cmp() -> &'static IBig {
} }
/// Guarded fixed-point number /// Guarded fixed-point number
#[derive(Clone, Eq)] #[derive(Clone, Debug, Eq)]
pub struct GuardedFixed(IBig); pub struct GuardedFixed(IBig);
impl GuardedFixed { impl GuardedFixed {

View File

@ -45,7 +45,7 @@ pub trait Assign<Src=Self> {
/// Trait for OpenTally numeric representations /// Trait for OpenTally numeric representations
//pub trait Number: NumRef + NumAssignRef + PartialOrd + Assign + Clone + fmt::Display where for<'a> &'a Self: RefNum<&'a Self> { //pub trait Number: NumRef + NumAssignRef + PartialOrd + Assign + Clone + fmt::Display where for<'a> &'a Self: RefNum<&'a Self> {
pub trait Number: pub trait Number:
NumRef + NumAssignRef + ops::Neg<Output=Self> + Ord + Assign + From<usize> + From<f64> + Clone + fmt::Display NumRef + NumAssignRef + ops::Neg<Output=Self> + Ord + Assign + From<usize> + From<f64> + Clone + fmt::Debug + fmt::Display
where where
for<'a> Self: Assign<&'a Self> for<'a> Self: Assign<&'a Self>
{ {

View File

@ -27,7 +27,7 @@ use std::ops;
type ImplType = f64; type ImplType = f64;
/// Native 64-bit floating-point number /// Native 64-bit floating-point number
#[derive(Clone, Display, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, PartialEq, PartialOrd)]
pub struct NativeFloat64(ImplType); pub struct NativeFloat64(ImplType);
impl Number for NativeFloat64 { impl Number for NativeFloat64 {

View File

@ -26,7 +26,7 @@ use std::ops;
type RatioBase = num_bigint::BigInt; type RatioBase = num_bigint::BigInt;
type RatioType = num_rational::BigRational; type RatioType = num_rational::BigRational;
#[derive(Clone, PartialEq, PartialOrd)] #[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Rational(RatioType); pub struct Rational(RatioType);
impl Number for Rational { impl Number for Rational {

View File

@ -26,7 +26,7 @@ use std::fmt;
use std::ops; use std::ops;
/// Rational number /// Rational number
#[derive(Clone, PartialEq, PartialOrd)] #[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Rational(rug::Rational); pub struct Rational(rug::Rational);
impl Number for Rational { impl Number for Rational {

View File

@ -73,6 +73,5 @@ fn aec_tas19_rational() {
defer_surpluses: false, defer_surpluses: false,
pp_decimals: 2, pp_decimals: 2,
}; };
utils::validate_election(stages, records, election, stv_opts, None, &["exhausted", "lbf"]);
utils::validate_election(stages, records, election, stv_opts);
} }

15
tests/data/ers97_meek.csv Normal file
View File

@ -0,0 +1,15 @@
Stage:,1,,2,,4,,6,,9,,11,
Comment:,First preferences,,Surpluses distributed,,Surpluses distributed,,Surpluses distributed,,Surpluses distributed,,Surpluses distributed,
Smith,134,EL,107.26,EL,106.96,EL,104.42,EL,101.58,EL,73,EL
Carpenter,81,,87.98,,88.47,,97.71,,101.58,EL,73,EL
Wright,27,,31.99,,32.34,,34.97,,0,EX,0,EX
Glazier,24,,30.19,,30.62,,0,EX,0,EX,0,EX
Duke,105,,106.6,,106.96,EL,104.42,EL,101.58,EL,73,EL
Prince,91,,91,,91,,92.45,,97.07,,73,EL
Baron,64,,64,,64,,64.03,,67.24,,0,EX
Abbot,59,,59.8,,64.85,,66,,68.92,,70.83,
Vicar,55,,55,,69.21,,70.99,,73.27,,75.16,EL
Monk,23,,23.4,,0,EX,0,EX,0,EX,0,EX
Freeman,90,,93.59,,94.27,,95.97,,99.81,,73,EL
Exhausted,0,,2.2,,4.31,,22.05,,41.95,,242.01,
Quota,107.57,,107.26,,106.96,,104.42,,101.58,,73,
1 Stage: 1 2 4 6 9 11
2 Comment: First preferences Surpluses distributed Surpluses distributed Surpluses distributed Surpluses distributed Surpluses distributed
3 Smith 134 EL 107.26 EL 106.96 EL 104.42 EL 101.58 EL 73 EL
4 Carpenter 81 87.98 88.47 97.71 101.58 EL 73 EL
5 Wright 27 31.99 32.34 34.97 0 EX 0 EX
6 Glazier 24 30.19 30.62 0 EX 0 EX 0 EX
7 Duke 105 106.6 106.96 EL 104.42 EL 101.58 EL 73 EL
8 Prince 91 91 91 92.45 97.07 73 EL
9 Baron 64 64 64 64.03 67.24 0 EX
10 Abbot 59 59.8 64.85 66 68.92 70.83
11 Vicar 55 55 69.21 70.99 73.27 75.16 EL
12 Monk 23 23.4 0 EX 0 EX 0 EX 0 EX
13 Freeman 90 93.59 94.27 95.97 99.81 73 EL
14 Exhausted 0 2.2 4.31 22.05 41.95 242.01
15 Quota 107.57 107.26 106.96 104.42 101.58 73

BIN
tests/data/ers97_meek.ods Normal file

Binary file not shown.

View File

@ -17,15 +17,9 @@
mod utils; mod utils;
use opentally::election::{CandidateState, CountState, Election};
use opentally::numbers::Rational; use opentally::numbers::Rational;
use opentally::stv; use opentally::stv;
use csv::StringRecord;
use std::fs::File;
use std::io::{self, BufRead};
#[test] #[test]
fn ers97_rational() { fn ers97_rational() {
let stv_opts = stv::STVOptions { let stv_opts = stv::STVOptions {
@ -47,92 +41,5 @@ fn ers97_rational() {
defer_surpluses: true, defer_surpluses: true,
pp_decimals: 2, pp_decimals: 2,
}; };
utils::read_validate_election::<Rational>("tests/data/ers97.csv", "tests/data/ers97.blt", stv_opts, None, &["nt", "vre"]);
// ---------------------------------------------
// Custom implementation due to nontransferables
// and vote required for election
// Read CSV file
let reader = csv::ReaderBuilder::new()
.has_headers(false)
.from_path("tests/data/ers97.csv")
.expect("IO Error");
let records: Vec<StringRecord> = reader.into_records().map(|r| r.expect("Syntax Error")).collect();
let mut candidates: Vec<&str> = records.iter().skip(2).map(|r| &r[0]).collect();
// Remove NT/VRE rows
candidates.truncate(candidates.len() - 2);
// TODO: Validate candidate names
let stages: Vec<usize> = records.first().unwrap().iter().skip(1).step_by(2).map(|s| s.parse().unwrap()).collect();
// Read BLT
let file = File::open("tests/data/ers97.blt").expect("IO Error");
let file_reader = io::BufReader::new(file);
let lines = file_reader.lines();
let election: Election<Rational> = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
// Initialise count state
let mut state = CountState::new(&election);
// Distribute first preferences
stv::count_init(&mut state, &stv_opts);
let mut stage_num = 1;
for (idx, stage) in stages.into_iter().enumerate() {
while stage_num < stage {
// Step through stages
// Assert count not yet done
assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false);
stage_num += 1;
}
println!("Col at idx {}", idx);
let mut candidate_votes: Vec<Option<Rational>> = records.iter().skip(2)
.map(|r|
if r[idx*2 + 1].len() > 0 {
Some(Rational::from(r[idx*2 + 1].parse::<f64>().expect("Syntax Error")))
} else {
None
})
.collect();
// Validate NT/VRE
let vre_votes = candidate_votes.pop().unwrap();
let nt_votes = candidate_votes.pop().unwrap();
assert!(&state.exhausted.votes + &state.loss_fraction.votes == nt_votes.unwrap());
if let Some(v) = vre_votes {
assert!(state.vote_required_election.as_ref().unwrap() == &v);
}
// Remove NT/VRE rows
candidate_votes.truncate(candidate_votes.len() - 2);
// Validate candidate votes
for (candidate, votes) in state.election.candidates.iter().zip(candidate_votes) {
let count_card = state.candidates.get(candidate).unwrap();
assert!(&count_card.votes == votes.as_ref().unwrap(), "Failed to validate votes for candidate {}. Expected {:}, got {:}", candidate.name, votes.unwrap(), count_card.votes);
}
// Validate candidate states
let mut candidate_states: Vec<&str> = records.iter().skip(2).map(|r| &r[idx*2 + 2]).collect();
candidate_states.truncate(candidate_states.len() - 2);
for (candidate, candidate_state) in state.election.candidates.iter().zip(candidate_states) {
let count_card = state.candidates.get(candidate).unwrap();
if candidate_state == "" {
assert!(count_card.state == CandidateState::Hopeful);
} else if candidate_state == "EL" || candidate_state == "PEL" {
assert!(count_card.state == CandidateState::Elected);
} else if candidate_state == "EX" || candidate_state == "EXCLUDING" {
assert!(count_card.state == CandidateState::Excluded);
} else {
panic!("Unknown state descriptor {}", candidate_state);
}
}
}
} }

45
tests/meek.rs Normal file
View File

@ -0,0 +1,45 @@
/* 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/>.
*/
mod utils;
use opentally::numbers::NativeFloat64;
use opentally::stv;
#[test]
fn meek_ers97_float64() {
let stv_opts = stv::STVOptions {
round_tvs: None,
round_weights: None,
round_votes: None,
round_quota: None,
sum_surplus_transfers: stv::SumSurplusTransfersMode::SingleStep,
normalise_ballots: false,
quota: stv::QuotaType::DroopExact,
quota_criterion: stv::QuotaCriterion::GreaterOrEqual,
quota_mode: stv::QuotaMode::Static,
ties: vec![],
surplus: stv::SurplusMethod::Meek,
surplus_order: stv::SurplusOrder::BySize,
transferable_only: false,
exclusion: stv::ExclusionMethod::SingleStage,
bulk_exclude: false,
defer_surpluses: false,
pp_decimals: 2,
};
utils::read_validate_election::<NativeFloat64>("tests/data/ers97_meek.csv", "tests/data/ers97.blt", stv_opts, Some(2), &["exhausted", "quota"]);
}

View File

@ -41,5 +41,5 @@ fn prsa1_rational() {
defer_surpluses: false, defer_surpluses: false,
pp_decimals: 2, pp_decimals: 2,
}; };
utils::read_validate_election::<Rational>("tests/data/prsa1.csv", "tests/data/prsa1.blt", stv_opts); utils::read_validate_election::<Rational>("tests/data/prsa1.csv", "tests/data/prsa1.blt", stv_opts, None, &["exhausted", "lbf"]);
} }

View File

@ -16,7 +16,7 @@
*/ */
use opentally::election::{CandidateState, CountState, Election}; use opentally::election::{CandidateState, CountState, Election};
use opentally::numbers::{Number, Rational}; use opentally::numbers::Number;
use opentally::stv; use opentally::stv;
use csv::StringRecord; use csv::StringRecord;
@ -26,7 +26,14 @@ use std::io::{self, BufRead};
use std::ops; use std::ops;
#[allow(dead_code)] // Suppress false positive #[allow(dead_code)] // Suppress false positive
pub fn read_validate_election<N: Number>(csv_file: &str, blt_file: &str, stv_opts: stv::STVOptions) { pub fn read_validate_election<N: Number>(csv_file: &str, blt_file: &str, stv_opts: stv::STVOptions, cmp_dps: Option<usize>, sum_rows: &[&str])
where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
for<'r> &'r N: ops::Mul<&'r N, Output=N>,
for<'r> &'r N: ops::Div<&'r N, Output=N>,
for<'r> &'r N: ops::Neg<Output=N>,
{
// Read CSV file // Read CSV file
let reader = csv::ReaderBuilder::new() let reader = csv::ReaderBuilder::new()
.has_headers(false) .has_headers(false)
@ -47,18 +54,23 @@ pub fn read_validate_election<N: Number>(csv_file: &str, blt_file: &str, stv_opt
let file_reader = io::BufReader::new(file); let file_reader = io::BufReader::new(file);
let lines = file_reader.lines(); let lines = file_reader.lines();
let election: Election<Rational> = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter()); let election: Election<N> = Election::from_blt(lines.map(|r| r.expect("IO Error").to_string()).into_iter());
validate_election(stages, records, election, stv_opts); validate_election(stages, records, election, stv_opts, cmp_dps, sum_rows);
} }
pub fn validate_election<N: Number>(stages: Vec<usize>, records: Vec<StringRecord>, election: Election<N>, stv_opts: stv::STVOptions) pub fn validate_election<N: Number>(stages: Vec<usize>, records: Vec<StringRecord>, mut election: Election<N>, stv_opts: stv::STVOptions, cmp_dps: Option<usize>, sum_rows: &[&str])
where where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
for<'r> &'r N: ops::Sub<&'r N, Output=N>, for<'r> &'r N: ops::Sub<&'r N, Output=N>,
for<'r> &'r N: ops::Mul<&'r N, Output=N>, for<'r> &'r N: ops::Mul<&'r N, Output=N>,
for<'r> &'r N: ops::Div<&'r N, Output=N>, for<'r> &'r N: ops::Div<&'r N, Output=N>,
for<'r> &'r N: ops::Neg<Output=N>, for<'r> &'r N: ops::Neg<Output=N>,
{ {
if stv_opts.normalise_ballots {
election.normalise_ballots();
}
// Initialise count state // Initialise count state
let mut state = CountState::new(&election); let mut state = CountState::new(&election);
@ -73,29 +85,38 @@ where
assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false); assert_eq!(stv::count_one_stage(&mut state, &stv_opts).unwrap(), false);
stage_num += 1; stage_num += 1;
} }
validate_stage(idx, &state, &records); validate_stage(idx, &state, &records, cmp_dps, sum_rows);
} }
} }
fn validate_stage<N: Number>(idx: usize, state: &CountState<N>, records: &Vec<StringRecord>) { fn validate_stage<N: Number>(idx: usize, state: &CountState<N>, records: &Vec<StringRecord>, cmp_dps: Option<usize>, sum_rows: &[&str])
println!("Col at idx {}", idx); where
for<'r> &'r N: ops::Add<&'r N, Output=N>,
let mut candidate_votes: Vec<N> = records.iter().skip(2).map(|r| N::from(r[idx*2 + 1].parse::<f64>().expect("Syntax Error"))).collect(); {
let mut candidate_votes: Vec<Option<N>> = records.iter().skip(2)
.map(|r| if r[idx*2 + 1].len() > 0 { Some(N::from(r[idx*2 + 1].parse::<f64>().expect("Syntax Error"))) } else { None })
.collect();
// Validate exhausted/LBF // Validate exhausted/LBF
let lbf_votes = candidate_votes.pop().unwrap(); for kind in sum_rows.iter().rev() {
let exhausted_votes = candidate_votes.pop().unwrap(); let votes = candidate_votes.pop().unwrap_or(None);
if let Some(votes) = votes {
assert!(state.exhausted.votes == exhausted_votes); match kind {
assert!(state.loss_fraction.votes == lbf_votes); &"exhausted" => approx_eq(&state.exhausted.votes, &votes, cmp_dps, idx, "exhausted votes"),
&"lbf" => approx_eq(&state.loss_fraction.votes, &votes, cmp_dps, idx, "LBF"),
// Remove exhausted/LBF rows &"nt" => approx_eq(&(&state.exhausted.votes + &state.loss_fraction.votes), &votes, cmp_dps, idx, "NTs"),
candidate_votes.truncate(candidate_votes.len() - 2); &"quota" => approx_eq(state.quota.as_ref().unwrap(), &votes, cmp_dps, idx, "quota"),
&"vre" => approx_eq(state.vote_required_election.as_ref().unwrap(), &votes, cmp_dps, idx, "VRE"),
_ => panic!("Unknown sum_rows"),
}
}
}
// Validate candidate votes // Validate candidate votes
for (candidate, votes) in state.election.candidates.iter().zip(candidate_votes) { for (candidate, votes) in state.election.candidates.iter().zip(candidate_votes) {
let count_card = state.candidates.get(candidate).unwrap(); let count_card = state.candidates.get(candidate).unwrap();
assert!(count_card.votes == votes, "Failed to validate votes for candidate {}. Expected {:}, got {:}", candidate.name, votes, count_card.votes); let votes = votes.unwrap();
approx_eq(&count_card.votes, &votes, cmp_dps, idx, &format!("votes for candidate {}", candidate.name));
} }
// Validate candidate states // Validate candidate states
@ -115,3 +136,17 @@ fn validate_stage<N: Number>(idx: usize, state: &CountState<N>, records: &Vec<St
} }
} }
} }
fn approx_eq<N: Number>(n1: &N, n2: &N, cmp_dps: Option<usize>, idx: usize, description: &str) {
match cmp_dps {
Some(dps) => {
let s1 = format!("{:.dps$}", n1, dps=dps);
let s2 = format!("{:.dps$}", n2, dps=dps);
assert!(s1 == s2, "Failed to verify {} for idx {}. Expected {}, got {}", description, idx, s2, s1);
}
None => {
assert!(n1 == n2, "Failed to verify {} for idx {}. Expected {}, got {}", description, idx, n2, n1);
}
}
}