Better error messages when insufficient candidates to fill vacancies
This commit is contained in:
parent
0a7189e54f
commit
09c4a375a7
|
@ -553,12 +553,14 @@ impl<S: AsRef<str>> From<S> for ConstraintMode {
|
|||
}
|
||||
|
||||
/// An error during the STV count
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum STVError {
|
||||
/// Options for the count are invalid
|
||||
InvalidOptions(&'static str),
|
||||
/// Tie could not be resolved
|
||||
UnresolvedTie,
|
||||
/// Unrecoverable error during the count
|
||||
CannotCompleteCount(&'static str),
|
||||
}
|
||||
|
||||
impl STVError {
|
||||
|
@ -567,6 +569,7 @@ impl STVError {
|
|||
match self {
|
||||
STVError::InvalidOptions(s) => s,
|
||||
STVError::UnresolvedTie => "Unable to resolve tie",
|
||||
STVError::CannotCompleteCount(s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -667,6 +670,14 @@ where
|
|||
return Ok(false);
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
let num_hopefuls = state.candidates.values()
|
||||
.filter(|cc| cc.state == CandidateState::Hopeful)
|
||||
.count();
|
||||
if num_hopefuls == 0 {
|
||||
return Err(STVError::CannotCompleteCount("Insufficient continuing candidates to complete count"));
|
||||
}
|
||||
|
||||
// Exclude lowest hopeful
|
||||
exclude_hopefuls(state, &opts)?; // Cannot fail
|
||||
calculate_quota(state, opts);
|
||||
|
@ -960,9 +971,11 @@ fn elect_sure_winners<'a, N: Number>(state: &mut CountState<'a, N>, opts: &STVOp
|
|||
}
|
||||
});
|
||||
|
||||
let last_winner = hopefuls[num_vacancies - 1].1;
|
||||
if last_winner.votes <= total_trailing {
|
||||
return Ok(false);
|
||||
if num_vacancies - 1 < hopefuls.len() {
|
||||
let last_winner = hopefuls[num_vacancies - 1].1;
|
||||
if last_winner.votes <= total_trailing {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
let mut leading_hopefuls: Vec<&Candidate> = hopefuls.iter().take(num_vacancies).map(|(c, _)| *c).collect();
|
||||
|
@ -973,7 +986,7 @@ fn elect_sure_winners<'a, N: Number>(state: &mut CountState<'a, N>, opts: &STVOp
|
|||
}
|
||||
|
||||
// Bulk elect all leading candidates
|
||||
while state.num_elected < state.election.seats {
|
||||
while !leading_hopefuls.is_empty() && state.num_elected < state.election.seats {
|
||||
let max_cands = ties::multiple_max_by(&leading_hopefuls, |c| &state.candidates[c].votes);
|
||||
let candidate = if max_cands.len() > 1 {
|
||||
choose_highest(state, opts, max_cands, "Which candidate to elect?")?
|
||||
|
@ -1151,7 +1164,7 @@ fn can_bulk_elect<N: Number>(state: &CountState<N>, num_to_exclude: usize) -> bo
|
|||
})
|
||||
.count();
|
||||
|
||||
if state.num_elected + num_hopefuls - num_to_exclude <= state.election.seats {
|
||||
if num_hopefuls > 0 && state.num_elected + num_hopefuls - num_to_exclude <= state.election.seats {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
# Comment: Constructed example - insufficient candidates to fill vacancies
|
||||
5 7
|
||||
1 1 0
|
||||
1 2 0
|
||||
1 3 0
|
||||
1 4 0
|
||||
1 5 0
|
||||
0
|
||||
"Candidate 1"
|
||||
"Candidate 2"
|
||||
"Candidate 3"
|
||||
"Candidate 4"
|
||||
"Candidate 5"
|
||||
"Insufficient candidates example 1"
|
|
@ -0,0 +1,17 @@
|
|||
# Comment: Constructed example - insufficient candidates after the exclusion of those with no votes
|
||||
8 7
|
||||
1 1 0
|
||||
1 2 0
|
||||
1 3 0
|
||||
1 4 0
|
||||
1 5 0
|
||||
0
|
||||
"Candidate 1"
|
||||
"Candidate 2"
|
||||
"Candidate 3"
|
||||
"Candidate 4"
|
||||
"Candidate 5"
|
||||
"Candidate 6"
|
||||
"Candidate 7"
|
||||
"Candidate 8"
|
||||
"Insufficient candidates example 2"
|
|
@ -0,0 +1,74 @@
|
|||
/* 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::election::{CountState, Election};
|
||||
use opentally::numbers::Rational;
|
||||
use opentally::parser::blt;
|
||||
use opentally::stv;
|
||||
use opentally::ties::TieStrategy;
|
||||
|
||||
#[test]
|
||||
fn insufficient_candidates1() {
|
||||
let stv_opts = stv::STVOptionsBuilder::default()
|
||||
.ties(vec![TieStrategy::Random(String::new())])
|
||||
.build().unwrap();
|
||||
|
||||
let mut election: Election<Rational> = blt::parse_path("tests/data/insufficient_candidates1.blt").expect("Syntax Error");
|
||||
stv::preprocess_election(&mut election, &stv_opts);
|
||||
|
||||
let mut state = CountState::new(&election);
|
||||
|
||||
stv::count_init(&mut state, &stv_opts).unwrap();
|
||||
|
||||
loop {
|
||||
let result = stv::count_one_stage::<Rational>(&mut state, &stv_opts);
|
||||
match result {
|
||||
Ok(done) => { assert_eq!(done, false); }
|
||||
Err(err) => {
|
||||
assert_eq!(err, stv::STVError::CannotCompleteCount("Insufficient continuing candidates to complete count"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insufficient_candidates2() {
|
||||
let stv_opts = stv::STVOptionsBuilder::default()
|
||||
.ties(vec![TieStrategy::Random(String::new())])
|
||||
.build().unwrap();
|
||||
|
||||
let mut election: Election<Rational> = blt::parse_path("tests/data/insufficient_candidates2.blt").expect("Syntax Error");
|
||||
stv::preprocess_election(&mut election, &stv_opts);
|
||||
|
||||
let mut state = CountState::new(&election);
|
||||
|
||||
stv::count_init(&mut state, &stv_opts).unwrap();
|
||||
|
||||
loop {
|
||||
let result = stv::count_one_stage::<Rational>(&mut state, &stv_opts);
|
||||
match result {
|
||||
Ok(done) => { assert_eq!(done, false); }
|
||||
Err(err) => {
|
||||
assert_eq!(err, stv::STVError::CannotCompleteCount("Insufficient continuing candidates to complete count"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -65,9 +65,7 @@ where
|
|||
for<'r> &'r N: ops::Div<&'r N, Output=N>,
|
||||
for<'r> &'r N: ops::Neg<Output=N>,
|
||||
{
|
||||
if stv_opts.normalise_ballots {
|
||||
election.normalise_ballots();
|
||||
}
|
||||
stv::preprocess_election(&mut election, &stv_opts);
|
||||
|
||||
// Initialise count state
|
||||
let mut state = CountState::new(&election);
|
||||
|
|
Loading…
Reference in New Issue