Skip to content

Tool for calculating the winner of an election by letting voters cast their vote in form of a ranked list of candidates.

License

Notifications You must be signed in to change notification settings

uberbruns/SwiftSchulze

Repository files navigation

SwiftSchulze

The Schulze method is a ranked-choice voting algorithm. It guarantees that a majority-preferred candidate wins when one exists. Preference cycles are resolved using transitive beatpaths: if A defeats B and B defeats C, then A indirectly defeats C.

https://en.wikipedia.org/wiki/Schulze_method

Installation

Requirements

You need Swift 6.2 or later installed on your system. To install Swift, visit:

https://www.swift.org/install

Clone the Repository

git clone https://github.com/uberbruns/SwiftSchulze.git
cd SwiftSchulze

Using the Command Line Tool

Run Directly

You can run the tool directly without building using:

swift run schulze --help

Build for Release

To build an optimized release binary:

swift build -c release

The compiled binary will be located at:

.build/release/schulze

You can then copy it to a location in your PATH:

cp .build/release/schulze /usr/local/bin/

Using the Library in Your App

If you want to use the Schulze algorithm in your Swift application, add this package as a dependency in your Package.swift:

dependencies: [
  .package(url: "https://github.com/uberbruns/SwiftSchulze.git", from: "0.1.0")
]

Then add SchulzeLibrary to your target dependencies:

.target(
  name: "YourTarget",
  dependencies: [
    .product(name: "SchulzeLibrary", package: "SwiftSchulze")
  ]
)

Import and use in your code:

import SchulzeLibrary

let rankings = [
  ["Alice", "Bob", "Charlie"],
  ["Bob", "Charlie", "Alice"],
  ["Charlie", "Alice", "Bob"]
]

let winner = Schulze.ranking(rankings: rankings)
print(winner)

Command Line Usage

OVERVIEW: Tool for calculating the winner of an election by letting voters cast
their vote in form of a ranked list of candidates.

USAGE: schulze [--ranking <ranking> ...] [--format <format>] [--stdin] [--path <path>] [--directory <directory>]

OPTIONS:
  -r, --ranking <ranking> A comma separated ranking of candidates casted by one
                          voter. Repeat this option for every participant.
  -f, --format <format>   The output format ('plain' or 'json'). (default:
                          plain)
  -s, --stdin             Takes a list of votes from standard input. Every line
                          should contain a comma separated ranking.
  -p, --path <path>       Use this option to provide a path to a file
                          containing the casted rankings. Every line should
                          contain the candidates ranked by one voter seperated
                          via comma.
  -d, --directory <directory>
                          Use this option to provide a directory containing
                          .txt files. Every file should contain the candidates
                          ranked by one voter. One candidate per line.
  -h, --help              Show help information.

License

This project is released under the MIT license. See LICENSE for details.

About

Tool for calculating the winner of an election by letting voters cast their vote in form of a ranked list of candidates.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages