gorankvote

package module
v0.0.0-...-db394b4 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

README

gorankvote

Go implementation of different ranked-choice voting system, directly based on pyrankvote.

Note: Only Single Transferable Vote (STV) has been implemented for now.

Documentation

Index

Constants

View Source
const (
	Elected  = "Elected"
	Hopeful  = "Hopeful"
	Rejected = "Rejected"
)
View Source
const (
	CompareMethodMostSecondChoice = "MostSecondChoice"
	CompareMethodRandom           = "Random"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Ballot

type Ballot struct {
	RankedCandidates []*Candidate
}

func NewBallot

func NewBallot(candidates []*Candidate) *Ballot

func (*Ballot) String

func (b *Ballot) String() string

type Candidate

type Candidate struct {
	Name string
}

func NewCandidate

func NewCandidate(name string) *Candidate

func (*Candidate) Equals

func (c *Candidate) Equals(a *Candidate) bool

func (*Candidate) Hash

func (c *Candidate) Hash() string

func (*Candidate) String

func (c *Candidate) String() string

type CandidateResult

type CandidateResult struct {
	Candidate     *Candidate
	NumberOfVotes float64
	Status        CandidateStatus
}

type CandidateStatus

type CandidateStatus string

type CandidateVoteCount

type CandidateVoteCount struct {
	Candidate     *Candidate
	Status        CandidateStatus
	NumberOfVotes float64
	Votes         []*Ballot
}

func NewCandidateVoteCount

func NewCandidateVoteCount(candidate *Candidate) *CandidateVoteCount

func (*CandidateVoteCount) GetCandidateResult

func (cvc *CandidateVoteCount) GetCandidateResult() *CandidateResult

func (*CandidateVoteCount) IsInRace

func (cvc *CandidateVoteCount) IsInRace() bool

func (*CandidateVoteCount) String

func (cvc *CandidateVoteCount) String() string

type CompareMethod

type CompareMethod string

type ElectionManager

type ElectionManager struct {
	Candidates []*Candidate
	Ballots    []*Ballot
	ElectionManagerOptions

	CandidateVoteCounts map[*Candidate]*CandidateVoteCount
	CandidatesInRace    []*CandidateVoteCount
	CandidatesElected   []*CandidateVoteCount
	CandidatesRejected  []*CandidateVoteCount
	ExhaustedBallots    []*Ballot
	NumberOfBlankVotes  float64
	NumberOfCandidates  int
}

func NewElectionManager

func NewElectionManager(candidates []*Candidate, ballots []*Ballot, options ElectionManagerOptions) *ElectionManager

func (*ElectionManager) Candidate1HasMostSecondChoices

func (em *ElectionManager) Candidate1HasMostSecondChoices(c1vc, c2vc *CandidateVoteCount, x int) bool

func (*ElectionManager) ElectCandidate

func (em *ElectionManager) ElectCandidate(candidate *Candidate) error

func (*ElectionManager) GetBallotCandidateNrXInRaceOrNone

func (em *ElectionManager) GetBallotCandidateNrXInRaceOrNone(ballot *Ballot, x int) *Candidate

func (*ElectionManager) GetCandidateWithLeastVotesInRace

func (em *ElectionManager) GetCandidateWithLeastVotesInRace() (*Candidate, error)

func (*ElectionManager) GetCandidatesInRace

func (em *ElectionManager) GetCandidatesInRace() []*Candidate

func (*ElectionManager) GetCandidatesWithMoreThanXVotes

func (em *ElectionManager) GetCandidatesWithMoreThanXVotes(x int) []*Candidate

func (*ElectionManager) GetNumberOfCandidatesInRace

func (em *ElectionManager) GetNumberOfCandidatesInRace() int

func (*ElectionManager) GetNumberOfElectedCandidates

func (em *ElectionManager) GetNumberOfElectedCandidates() int

func (*ElectionManager) GetNumberOfNonExhaustedBallots

func (em *ElectionManager) GetNumberOfNonExhaustedBallots() float64

func (*ElectionManager) GetNumberOfNonExhaustedVotes

func (em *ElectionManager) GetNumberOfNonExhaustedVotes() float64

func (*ElectionManager) GetNumberOfVotes

func (em *ElectionManager) GetNumberOfVotes(candidate *Candidate) (float64, error)

func (*ElectionManager) GetResults

func (em *ElectionManager) GetResults() *RoundResult

func (*ElectionManager) IsValidCandidate

func (em *ElectionManager) IsValidCandidate(candidate *Candidate) bool

func (*ElectionManager) RejectCandidate

func (em *ElectionManager) RejectCandidate(candidate *Candidate) error

func (*ElectionManager) SortCandidatesInRace

func (em *ElectionManager) SortCandidatesInRace()

func (*ElectionManager) TransferVotes

func (em *ElectionManager) TransferVotes(candidate *Candidate, numberOfTransferVotes float64) error

type ElectionManagerOptions

type ElectionManagerOptions struct {
	NumberOfVotesPerVoter int
	CompareMethodIfEqual  CompareMethod
	PickRandomIfBlank     bool
}

func DefaultElectionManagerOptions

func DefaultElectionManagerOptions() ElectionManagerOptions

type ElectionResults

type ElectionResults struct {
	Rounds []*RoundResult
}

func NewElectionResults

func NewElectionResults() *ElectionResults

func SingleTransferableVote

func SingleTransferableVote(candidates []*Candidate, ballots []*Ballot, numberOfSeats int, options SingleTransferableVoteOptions) (*ElectionResults, error)

func (*ElectionResults) GetWinners

func (er *ElectionResults) GetWinners() []*Candidate

func (*ElectionResults) RegisterResults

func (er *ElectionResults) RegisterResults(round *RoundResult)

type RoundResult

type RoundResult struct {
	CandidateResults   []*CandidateResult
	NumberOfBlankVotes float64
}

func NewRoundResult

func NewRoundResult(candidateResults []*CandidateResult, nbBlankVotes float64) *RoundResult

func (*RoundResult) String

func (rr *RoundResult) String() string

type SingleTransferableVoteOptions

type SingleTransferableVoteOptions struct {
	CompareMethodIfEquals CompareMethod
	PickRandomIfBlank     bool
}

func DefaultSingleTransferableVoteOptions

func DefaultSingleTransferableVoteOptions() SingleTransferableVoteOptions

Jump to

Keyboard shortcuts

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