-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from lzear/next
Next
- Loading branch information
Showing
34 changed files
with
581 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import difference from 'lodash/difference' | ||
import { | ||
SystemUsingRankings, | ||
ScoreObject, | ||
VotingSystem, | ||
Ballot, | ||
} from '../../types' | ||
import { borda } from '../borda' | ||
import { scoresToRanking } from '../../utils/scores' | ||
|
||
export const baldwin: SystemUsingRankings = { | ||
type: VotingSystem.Baldwin, | ||
computeFromBallots(ballots: Ballot[], candidates: string[]): ScoreObject { | ||
const score: ScoreObject = {} | ||
let remainingCandidates = candidates | ||
let points = 0 | ||
while (remainingCandidates.length > 0) { | ||
const bordaScores = borda.computeFromBallots(ballots, remainingCandidates) | ||
const ranking = scoresToRanking(bordaScores) | ||
const losers = ranking[ranking.length - 1] | ||
for (const loser of losers) score[loser] = points | ||
remainingCandidates = difference(remainingCandidates, losers) | ||
points++ | ||
} | ||
return score | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import difference from 'lodash/difference' | ||
import { | ||
SystemUsingRankings, | ||
ScoreObject, | ||
VotingSystem, | ||
Ballot, | ||
} from '../../types' | ||
import { firstPastThePost } from '../first-past-the-post' | ||
import { scoresToRanking } from '../../utils/scores' | ||
|
||
export const coombs: SystemUsingRankings = { | ||
type: VotingSystem.Coombs, | ||
computeFromBallots(ballots: Ballot[], candidates: string[]): ScoreObject { | ||
const score: ScoreObject = {} | ||
const reversedBallots = ballots.map((ballot) => ({ | ||
ranking: [...ballot.ranking].reverse(), | ||
weight: ballot.weight, | ||
})) | ||
let remainingCandidates = candidates | ||
let points = 0 | ||
while (remainingCandidates.length > 0) { | ||
const fptpScore = firstPastThePost.computeFromBallots( | ||
reversedBallots, | ||
remainingCandidates, | ||
) | ||
const ranking = scoresToRanking(fptpScore) | ||
for (const winner of ranking[0]) score[winner] = points | ||
remainingCandidates = difference(remainingCandidates, ranking[0]) | ||
points++ | ||
} | ||
return score | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Ballot, ScoreObject } from '../../types' | ||
import { normalizeBallots } from '../../utils/normalize' | ||
import { scoresZero } from '../../utils/scoresZero' | ||
|
||
export const iterateFirstChoices = ( | ||
ballots: Ballot[], | ||
candidates: string[], | ||
compute: (rank: string[]) => number, | ||
): ScoreObject => { | ||
const result: ScoreObject = scoresZero(candidates) | ||
normalizeBallots(ballots, candidates).forEach((ballot) => { | ||
if (ballot.ranking.length) { | ||
const votes = ballot.ranking[0].filter((c) => candidates.includes(c)) | ||
votes.forEach( | ||
(candidate, idx, rank) => | ||
(result[candidate] += compute(rank) * ballot.weight), | ||
) | ||
} | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import difference from 'lodash/difference' | ||
import sum from 'lodash/sum' | ||
import { | ||
SystemUsingRankings, | ||
ScoreObject, | ||
VotingSystem, | ||
Ballot, | ||
} from '../../types' | ||
import { borda } from '../borda' | ||
|
||
export const nanson: SystemUsingRankings = { | ||
type: VotingSystem.Nanson, | ||
computeFromBallots(ballots: Ballot[], candidates: string[]): ScoreObject { | ||
const score: ScoreObject = {} | ||
let remainingCandidates = candidates | ||
let points = 0 | ||
while (remainingCandidates.length > 0) { | ||
const bordaScores = borda.computeFromBallots(ballots, remainingCandidates) | ||
const scores = Object.values(bordaScores) | ||
const avg = sum(scores) / scores.length | ||
const losers = remainingCandidates.filter((c) => bordaScores[c] <= avg) | ||
let maxPoints = points + 1 | ||
for (const loser of losers) { | ||
const p = points + bordaScores[loser] + 1 | ||
score[loser] = p | ||
if (p > maxPoints) maxPoints = p | ||
} | ||
remainingCandidates = difference(remainingCandidates, losers) | ||
points = maxPoints | ||
} | ||
return score | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.