Validate option combinations

This commit is contained in:
RunasSudo 2021-06-22 14:34:26 +10:00
parent 2e034d06c8
commit 08821c3c18
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 35 additions and 10 deletions

View File

@ -378,8 +378,8 @@ function changePreset() {
document.getElementById('txtMeekSurplusTolerance').value = '0.001%'; document.getElementById('txtMeekSurplusTolerance').value = '0.001%';
//document.getElementById('selSurplus').value = 'by_size'; //document.getElementById('selSurplus').value = 'by_size';
document.getElementById('selTransfers').value = 'meek'; document.getElementById('selTransfers').value = 'meek';
//document.getElementById('selPapers').value = 'both'; document.getElementById('selPapers').value = 'both';
//document.getElementById('selExclusion').value = 'single_stage'; document.getElementById('selExclusion').value = 'single_stage';
document.getElementById('selTies').value = 'backwards,random'; document.getElementById('selTies').value = 'backwards,random';
} else if (document.getElementById('selPreset').value === 'meek06') { } else if (document.getElementById('selPreset').value === 'meek06') {
document.getElementById('selQuotaCriterion').value = 'geq'; document.getElementById('selQuotaCriterion').value = 'geq';
@ -406,8 +406,8 @@ function changePreset() {
document.getElementById('txtMeekSurplusTolerance').value = '0.0001'; document.getElementById('txtMeekSurplusTolerance').value = '0.0001';
//document.getElementById('selSurplus').value = 'by_size'; //document.getElementById('selSurplus').value = 'by_size';
document.getElementById('selTransfers').value = 'meek'; document.getElementById('selTransfers').value = 'meek';
//document.getElementById('selPapers').value = 'both'; document.getElementById('selPapers').value = 'both';
//document.getElementById('selExclusion').value = 'single_stage'; document.getElementById('selExclusion').value = 'single_stage';
document.getElementById('selTies').value = 'backwards,random'; document.getElementById('selTies').value = 'backwards,random';
} else if (document.getElementById('selPreset').value === 'meeknz') { } else if (document.getElementById('selPreset').value === 'meeknz') {
document.getElementById('selQuotaCriterion').value = 'geq'; document.getElementById('selQuotaCriterion').value = 'geq';
@ -434,8 +434,8 @@ function changePreset() {
document.getElementById('txtMeekSurplusTolerance').value = '0.0001'; document.getElementById('txtMeekSurplusTolerance').value = '0.0001';
//document.getElementById('selSurplus').value = 'by_size'; //document.getElementById('selSurplus').value = 'by_size';
document.getElementById('selTransfers').value = 'meek'; document.getElementById('selTransfers').value = 'meek';
//document.getElementById('selPapers').value = 'both'; document.getElementById('selPapers').value = 'both';
//document.getElementById('selExclusion').value = 'single_stage'; document.getElementById('selExclusion').value = 'single_stage';
document.getElementById('selTies').value = 'backwards,random'; document.getElementById('selTies').value = 'backwards,random';
} else if (document.getElementById('selPreset').value === 'senate') { } else if (document.getElementById('selPreset').value === 'senate') {
document.getElementById('selQuotaCriterion').value = 'geq'; document.getElementById('selQuotaCriterion').value = 'geq';

View File

@ -36,6 +36,9 @@ onmessage = function(evt) {
// Init STV options // Init STV options
opts = wasm.STVOptions.new.apply(null, evt.data.optsStr); opts = wasm.STVOptions.new.apply(null, evt.data.optsStr);
// Validate options
opts.validate();
// Describe count // Describe count
postMessage({'type': 'describeCount', 'content': wasm['describe_count_' + numbers](evt.data.filePath, election, opts)}); postMessage({'type': 'describeCount', 'content': wasm['describe_count_' + numbers](evt.data.filePath, election, opts)});

View File

@ -198,6 +198,14 @@ impl STVOptions {
if self.pp_decimals != 2 { flags.push(format!("--pp-decimals {}", self.pp_decimals)); } if self.pp_decimals != 2 { flags.push(format!("--pp-decimals {}", self.pp_decimals)); }
return flags.join(" "); return flags.join(" ");
} }
/// Validate the combination of [STVOptions] and panic if invalid
pub fn validate(&self) {
if self.surplus == SurplusMethod::Meek {
if self.transferable_only { panic!("--surplus meek is incompatible with --transferable-only"); }
if self.exclusion != ExclusionMethod::SingleStage { panic!("--surplus meek requires --exclusion single_stage"); }
}
}
} }
/// Enum of options for [STVOptions::sum_surplus_transfers] /// Enum of options for [STVOptions::sum_surplus_transfers]
@ -929,6 +937,8 @@ where
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>,
{ {
match opts.exclusion {
ExclusionMethod::SingleStage => {
match opts.surplus { match opts.surplus {
SurplusMethod::WIG | SurplusMethod::UIG | SurplusMethod::EG => { SurplusMethod::WIG | SurplusMethod::UIG | SurplusMethod::EG => {
gregory::exclude_candidates(state, opts, excluded_candidates); gregory::exclude_candidates(state, opts, excluded_candidates);
@ -937,6 +947,13 @@ where
meek::exclude_candidates(state, opts, excluded_candidates); meek::exclude_candidates(state, opts, excluded_candidates);
} }
} }
}
ExclusionMethod::ByValue | ExclusionMethod::ParcelsByOrder => {
// Exclusion in parts compatible only with Gregory method
gregory::exclude_candidates(state, opts, excluded_candidates);
}
}
}
} }
/// Determine if the count is complete because the number of elected candidates equals the number of vacancies /// Determine if the count is complete because the number of elected candidates equals the number of vacancies

View File

@ -229,6 +229,11 @@ impl STVOptions {
pp_decimals, pp_decimals,
)) ))
} }
/// Wrapper for [stv::STVOptions::validate]
pub fn validate(&self) {
self.0.validate();
}
} }
impl STVOptions { impl STVOptions {