1 unstable release
0.1.0 | Jun 21, 2024 |
---|
#141 in Simulation
21KB
387 lines
ElectoSIM
lib.rs
:
ElectoSIM
ElectoSIM is a library that allows you to simulate simple elections using different methods.
Methods
The following methods are available:
- D'Hondt
- Webster/Sainte-Laguë
- Adams
- Imperiali
- Huntington-Hill
- Danish
- Hare-Niemeyer
- Hagenbach-Bischoff
- Imperiali - Quotient
- Droop
- Winner Takes All
Usage
use electosim::methods::Method;
use electosim::models::Candidacy;
use electosim::SimpleElection;
fn main() {
let mut election = SimpleElection {
results: vec![
Candidacy::new(2010, 9),
Candidacy::new(1018, 4),
Candidacy::new(86, 0),
Candidacy::new(77, 0),
],
seats: 13,
method: Method::HAGENBASCHBISCHOFF,
};
election.compute().expect("Can not compute method");
election.results.iter().for_each(|c| println!("{:?}", c));
}
The first statement in the main
function creates a new [SimpleElection] with the candidates, the number of seats available, and the method to be used. The compute
method is then called to compute the election results. Finally, the results are printed to the console.
compute_
functions
A method is a function with type fn(&mut Vec<T>, u16) -> Result<(), &str>
where T
is a type that implements the WithVotes
and WithSeats
traits.
You can use the compute_
functions directly if you want to compute the election results without using the [SimpleElection] struct. For example:
use electosim::methods::divisor::compute_dhondt;
use electosim::models::Candidacy;
fn main() {
let mut candidacies = vec![
Candidacy::new(2010, 0),
Candidacy::new(1018, 0),
Candidacy::new(86, 0),
Candidacy::new(77, 0),
];
compute_dhondt(&mut candidacies, 13).unwrap();
candidacies.iter().for_each(|c| println!("{:?}", c));
}
There are some implementations of the compute_
functions in the methods::divisor (ex: D'hondt) and methods::remainder (ex: Hare) modules.