govote

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2019 License: MIT Imports: 7 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

govote

...is a Go library for conducting polls using various voting systems

Install

Grab the package with:

$ go get github.com/Sam-Izdat/govote

License MIT GoDoc

How do I even...

Behold.

package main

import(
    "fmt"
    "github.com/Sam-Izdat/govote"
)

func main() {
    candidates := []string{"Kang", "Kodos"}
    poll, _ := govote.Plurality.New(candidates)
    poll.AddBallot("Kang")
    poll.AddBallot("Kang")
    poll.AddBallot("Kodos")
    fmt.Println(poll.Evaluate())
    // => [Kang] [{Kang 2} {Kodos 1}] <nil>
}

There's a more interesting example used on Wikipedia for comparing voting systems, which shows how different methods can yield very different results.

// Tennessee voting on capital, suppose all voters want it close, blah, blah, blah...
candidates := []string{"Memphis", "Nashville", "Knoxville", "Chattanooga"}
schulze, _ := govote.Schulze.New(candidates)
plurality, _ := govote.Plurality.New(candidates)
runoff, _ := govote.InstantRunoff.New(candidates)

ballotMemphis := []string{"Memphis", "Nashville", "Chattanooga", "Knoxville"}
ballotNashville := []string{"Nashville", "Chattanooga", "Knoxville", "Memphis"}
ballotChattanooga := []string{"Chattanooga", "Knoxville", "Nashville", "Memphis"}
ballotKnoxville := []string{"Knoxville", "Chattanooga", "Nashville", "Memphis"}

for i := 0; i < 42; i++ {
    schulze.AddBallot(ballotMemphis)
    plurality.AddBallot(ballotMemphis[0])
    runoff.AddBallot(ballotMemphis)
}
for i := 0; i < 26; i++ {
    schulze.AddBallot(ballotNashville)
    plurality.AddBallot(ballotNashville[0])
    runoff.AddBallot(ballotNashville)
}
for i := 0; i < 15; i++ {
    schulze.AddBallot(ballotChattanooga)
    plurality.AddBallot(ballotChattanooga[0])
    runoff.AddBallot(ballotChattanooga)
}
for i := 0; i < 17; i++ {
    schulze.AddBallot(ballotKnoxville)
    plurality.AddBallot(ballotKnoxville[0])
    runoff.AddBallot(ballotKnoxville)
}

// Schulze scores are a tally of superior strongest-path comparisons for the candidate
fmt.Println(schulze.Evaluate())
// => [Nashville] [{Nashville 3} {Chattanooga 2} {Knoxville 1} {Memphis 0}] <nil>

// Plurality scores are the number of votes for the candidate
fmt.Println(plurality.Evaluate())
// => [Memphis] [{Memphis 42} {Nashville 26} {Knoxville 17} {Chattanooga 15}] <nil>

// Instant-runoff returns a slice of rounds, each an ordered slice of candidate scores
fmt.Println(runoff.Evaluate())
// => [Knoxville] [map[Memphis:42 Nashville:26 Chattanooga:15 Knoxville:17] \
// =>   [ [{Memphis 42} {Nashville 26} {Knoxville 17} {Chattanooga 15}] \
// =>     [{Memphis 42} {Knoxville 32} {Nashville 26}] \
// =>     [{Knoxville 58} {Memphis 42}] \
// =>     [{Knoxville 100}] ]
// =>   <nil>

Keep in mind that multiple winners are possible.

Condorcet polls may result in a tie and, in the event of a voting paradox, will return all candidates as winners.

Instant runoff polls, in the event of loser ties at the end of a round, will either eliminate the tied candidates if the sum of their scores is lower than the score of the leader or else elimate a loser at random. If, in the final round, two or more candidates are tied for victory they will all be returned as winners.

Plurality polls will return multiple winner in the event of a tie.

What still needs doin'

Voting systems implemented:

  • Approval Method
  • Instant Runoff Method
  • Minmax Method
  • Plurality Method
  • Range Method
  • Schulze Method (Condorcet)
  • Chain Method
  • Majority Choice Method

Also need to:

  • Write unit tests

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Schulze handler
	Schulze SchulzeCtrl

	// InstantRunoff handler
	InstantRunoff InstantRunoffCtrl

	// Plurality handler
	Plurality PluralityCtrl
)

Functions

This section is empty.

Types

type CPair

type CPair struct{ A, B int }

CPair represents two candidates' indices for pairwise comparison

type CScore

type CScore struct {
	Name  string
	Score int
}

CScore represents a candidate and the candidate's score

type InstantRunoffCtrl

type InstantRunoffCtrl struct{}

InstantRunoffCtrl is a struct exposed for reference -- use govote.InstantRunoff instance

func (InstantRunoffCtrl) New

func (_ InstantRunoffCtrl) New(candidates []string) (InstantRunoffPoll, error)

New creates a new Instant Runoff poll

type InstantRunoffPoll

type InstantRunoffPoll struct {
	// contains filtered or unexported fields
}

InstantRunoffPoll is a poll using the IRV method with specific rules for tie-breaking

func (*InstantRunoffPoll) AddBallot

func (p *InstantRunoffPoll) AddBallot(ballot []string) bool

AddBallot submits a ballot to the poll, returns true on success, false on failure

func (*InstantRunoffPoll) Evaluate

func (p *InstantRunoffPoll) Evaluate() ([]string, [][]CScore, error)

Evaluate poll; returns list of winners as slice of candidate names and a slice of consecutive round tallies, each an ordered slice of candidate scores

type PluralityCtrl

type PluralityCtrl struct{}

PluralityCtrl is a struct exposed for reference -- use govote.Plurality instance

func (PluralityCtrl) New

func (_ PluralityCtrl) New(candidates []string) (PluralityPoll, error)

New creates a new Plurality poll

type PluralityPoll

type PluralityPoll struct {
	// contains filtered or unexported fields
}

PluralityPoll is a poll using a simple plurality method (candidate with most votes wins)

func (*PluralityPoll) AddBallot

func (p *PluralityPoll) AddBallot(ballot string) bool

AddBallot submits a ballot to the poll, returns true on success, false on failure

func (*PluralityPoll) Evaluate

func (p *PluralityPoll) Evaluate() ([]string, []CScore, error)

Evaluate poll; returns list of winners as slice of candidate names and ranking as slice of CScores

type SchulzeCtrl

type SchulzeCtrl struct{}

SchulzeCtrl is a struct exposed for reference -- use govote.Schulze instance

func (SchulzeCtrl) New

func (_ SchulzeCtrl) New(candidates []string) (SchulzePoll, error)

New creates a new Schulze method poll

type SchulzePoll

type SchulzePoll struct {
	PP map[CPair]int // pairwise preferences
	SP map[CPair]int // strongest path
	// contains filtered or unexported fields
}

SchulzePoll is a Condorcet poll using the Schulze method

func (*SchulzePoll) AddBallot

func (p *SchulzePoll) AddBallot(ballot []string) (ok bool)

AddBallot submits a ballot to the poll, returns true on success, false on failure

func (*SchulzePoll) Evaluate

func (p *SchulzePoll) Evaluate() ([]string, []CScore, error)

Evaluate poll; returns list of winners as slice of candidate names and ranking as slice of CScores

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL