### 8 unstable releases (3 breaking)

0.4.0 | Jul 3, 2024 |
---|---|

0.3.0 | Oct 28, 2023 |

0.2.0 | Aug 4, 2023 |

0.1.4 | Jun 12, 2023 |

0.1.1 | Mar 26, 2023 |

#**146** in Math

**Apache-2.0**

350KB

9K
SLoC

# Single Transferable Vote implementation in Rust

This program is an implementation of Single Transferable Vote in Rust. The goal is to provide vote-counting transcripts that are reproducible with other vote-counting software, such as Droop.py.

For now, only Meek's counting method is implemented.

You can find more details in the following blog article: STV-rs: Single Transferable Vote implementation in Rust.

## Usage

With Cargo:

`$`` RUST_LOG=``$``LOG_LEVEL` cargo run \
`--release`` --`` \`
`--arithmetic`` ``$``ARITHMETIC` \
`--input`` ballots.txt \`
`meek`` \`
`--parallel``=``<`true`|``false``>`

`$`` RUST_LOG=``$``LOG_LEVEL` cargo run \
`--release`` --`` \`
`--arithmetic`` ``$``ARITHMETIC` \
`--input`` ballots.txt \`
`meek`` \`
`--parallel``=``<`true`|``false``>` \
` --`equalize

### Arithmetic implementations

You can control the arithmetic used to count votes via the

command-line flag. The following implementations are available.`--arithmetic`

: Each arithmetic operation is rounded to 9 decimal places. Rounding is downwards except for explicitly-marked operations (computing keep factors). This is backed by Rust's`fixed9`

and therefore might overflow. Compiling with the`i64`

feature (enabled by default) will trap integer overflows and make the program panic, rather than continuing with incorrect numbers.`checked_i64`

: Same as`bigfixed9`

, but this is backed by a big integer type (from the`fixed9`

crate) and therefore won't overflow. On the flip side, this will be slower than`num`

.`fixed9`

: Use 64-bit floating-point arithmetic (Rust's`float64`

). Generally fast but more brittle to reproduce, because the rounding introduced by floating-point arithmetic means that basic properties such as associativity and distributivity don't hold.`f64`

: Use exact rational numbers without rounding. The computational complexity is generally too high to complete more than a few rounds.`exact`

: Use exact rational numbers within each STV round, but then round the Meek keep factors after each round, to avoid computational complexity explosion.`approx`

### Equalized counting

In this mode, ballots where candidates are ranked equally are counted as fairly as possible, by simulating a superposition of all possible permutations of equally-ranked candidates.

For example, the ballot

becomes a superposition of `a b =c`

`a b c`

(with weight
1/2) and `a c b`

(with weight 1/2). Likewise, the ballot `a b``=`c`=`d

is counted as
a superposition of 6 ballots, each with weight 1/6: `a b c d`

, `a b d c`

, `a c b d`

, `a c d b`

, `a d b c`

, `a d c b`

.### Log levels

Besides the election transcript written to the standard output (which aims to be
consistent with Droop.py for
reproducibility), you can get more details via Rust's logging capabilities,
controlled by setting the

environment variable.`$``RUST_LOG`

The log levels will provide the following information.

: Print high-level results: election setup, elected/defeated candidates.`info`

:`debug`

+ print debug information about each STV round.`info`

:`trace`

+ print how each ballot is counted in each round.`debug`

For more advanced logging control, please check the

crate documentation.`env_logger`

### Parallelism

To speed up the computation, you can enable parallelism via the

command-line flag.`--parallel`

The vote-counting process involves accumulating votes across all ballots, summing the outcomes of counting each ballot. Without parallelism, this is done by a simple serial loop over the ballots. With parallelism enabled, a parallel loop is used instead, where each ballot is counted independently on any thread, and the sum is computed in any order.

Because the sum is computed in an arbitrary order, it is important to use an
arithmetic where addition is
commutative and
associative, otherwise
results won't be reproducible. This excludes

, as addition is not
associative.`float64`

Under the hood, the

crate is used to
automatically schedule and spread the work across available CPU cores
(map-reduce architecture).`rayon`

## Other STV implementations

Here is a non-exhaustive list of STV implementations.

## Contributing

See

for details.`CONTRIBUTING.md`

## License

Apache 2.0; see

for details.`LICENSE`

## Disclaimer

This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.

#### Dependencies

~5.5–8MB

~138K SLoC