added new voting system

This commit is contained in:
Jay Robson 2025-05-03 23:58:31 +10:00
parent ae8b1c8989
commit ba091c6803
2 changed files with 25 additions and 4 deletions

View File

@ -40,6 +40,27 @@ impl Counter {
scores scores
} }
pub fn count_all_with_powers_of_2(&self, ty: BallotType, running: &[bool]) -> Vec<f64> {
let mut scores = vec![0.0; running.len()];
for ballot in self.ballots.iter() {
if ballot.ty != ty {
continue;
}
let mut m = 0.5;
for &id in ballot.votes.iter() {
if !running[id] {
continue;
}
scores[id] += m;
m *= 0.5;
}
}
scores
}
} }

View File

@ -65,15 +65,15 @@ fn main() {
println!(); println!();
eprintln!("Running election rounds:"); eprintln!("Running election rounds:");
let mut scores: Vec<u32> = Vec::new(); let mut scores: Vec<f64> = Vec::new();
for index in 0..(running.len() - winner_count) { for index in 0..(running.len() - winner_count) {
scores = counter.count_primaries(ty, &running); scores = counter.count_all_with_powers_of_2(ty, &running);
let (id_worst, score_worst) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| u32::cmp(&a.1, &b.1)) { let (id_worst, score_worst) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| f64::total_cmp(&a.1, &b.1)) {
Some(v) => v, Some(v) => v,
None => break None => break
}; };
let (id_best, score_best) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| u32::cmp(&b.1, &a.1)) { let (id_best, score_best) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| f64::total_cmp(&b.1, &a.1)) {
Some(v) => v, Some(v) => v,
None => break, None => break,
}; };