-
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 #27 from lzear/ranked-pairs
Ranked pairs
- Loading branch information
Showing
25 changed files
with
912 additions
and
774 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exclude_paths: | ||
- '**/*.test.ts' |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
coverage: | ||
range: "0...100" | ||
range: 0..100 |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import approbation from '.' | ||
import { approbation } from '.' | ||
|
||
it('skips empty votes', () => { | ||
expect( | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import firstPastThePost from '.' | ||
import { firstPastThePost } from '.' | ||
|
||
it('skips empty votes', () => { | ||
expect( | ||
|
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,65 @@ | ||
import * as _ from 'lodash' | ||
import { | ||
SystemUsingMatrix, | ||
VotingSystem, | ||
Matrix, | ||
ScoreObject, | ||
} from '../../types' | ||
|
||
type Edge = { from: number; to: number; value: number } | ||
|
||
export const rankedPairs: SystemUsingMatrix = { | ||
type: VotingSystem.RankedPairs, | ||
computeFromMatrix(matrix: Matrix): ScoreObject { | ||
const allEdges: Edge[] = matrix.array | ||
.flatMap( | ||
(row, from) => | ||
row | ||
.map((value, to) => | ||
value > 0 && to !== from ? { from, to, value } : null, | ||
) | ||
.filter(Boolean) as Edge[], | ||
) | ||
.sort((a, b) => b.value - a.value) | ||
const acyclicGraph = allEdges.reduce((graph, edgeToAdd) => { | ||
const active: number[] = [] | ||
const visited: number[] = [] | ||
let cur: number | undefined = edgeToAdd.to | ||
while (cur !== undefined) { | ||
visited.push(cur) | ||
for (const { to } of graph.filter(({ from }) => from === cur)) { | ||
if (to === edgeToAdd.from) return graph | ||
if (!visited.includes(to) && !active.includes(to)) active.push(to) | ||
} | ||
cur = active.pop() | ||
} | ||
return [...graph, edgeToAdd] | ||
}, [] as Edge[]) | ||
|
||
const winnersIdx = _.range(matrix.candidates.length).filter( | ||
(candidate, key) => !acyclicGraph.some(({ to }) => to === key), | ||
) | ||
|
||
if (winnersIdx.length === matrix.candidates.length) | ||
return _.zipObject( | ||
matrix.candidates, | ||
Array(matrix.candidates.length).fill(1), | ||
) | ||
const nextRound = { | ||
array: matrix.array | ||
.filter((c, k) => !winnersIdx.includes(k)) | ||
.map((row) => row.filter((c, k) => !winnersIdx.includes(k))), | ||
candidates: matrix.candidates.filter((c, k) => !winnersIdx.includes(k)), | ||
} | ||
|
||
const nextResults = rankedPairs.computeFromMatrix(nextRound) | ||
const maxScore = Math.max(...Object.values(nextResults)) | ||
return winnersIdx.reduce( | ||
(scoreObject, winnerIdx) => ({ | ||
...scoreObject, | ||
[matrix.candidates[winnerIdx]]: maxScore + 1, | ||
}), | ||
nextResults, | ||
) | ||
}, | ||
} |
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,65 @@ | ||
import { rankedPairs } from './index' | ||
|
||
const example1 = [ | ||
[0, -2, 8], | ||
[2, 0, -4], | ||
[-8, 4, 0], | ||
] | ||
|
||
const example2 = [ | ||
[0, -4, -4, -4, -4], | ||
[4, 0, -2, 8, 6], | ||
[4, 2, 0, -4, 2], | ||
[4, -8, 4, 0, 6], | ||
[4, -6, -2, -6, 0], | ||
] | ||
|
||
const example3 = [ | ||
[0, -5, 7, 15, -1], | ||
[5, 0, -13, 21, -9], | ||
[-7, 13, 0, -11, 3], | ||
[-15, -21, 11, 0, -22], | ||
[1, 9, -3, 22, 0], | ||
] | ||
|
||
const candidates = ['a', 'b', 'c', 'd', 'e'] | ||
|
||
describe('ranked pairs', () => { | ||
it('works with "simple" example', () => { | ||
expect( | ||
rankedPairs.computeFromMatrix({ | ||
array: example1, | ||
candidates: ['a', 'b', 'c'], | ||
}), | ||
).toEqual({ | ||
a: 3, | ||
b: 1, | ||
c: 2, | ||
}) | ||
}) | ||
it('works with "complexer" example', () => { | ||
expect( | ||
rankedPairs.computeFromMatrix({ array: example2, candidates }), | ||
).toEqual({ | ||
a: 1, | ||
b: 5, | ||
c: 3, | ||
d: 4, | ||
e: 2, | ||
}) | ||
}) | ||
it('works with "example3" example', () => { | ||
expect( | ||
rankedPairs.computeFromMatrix({ | ||
array: example3, | ||
candidates: ['a', 'b', 'c', 'd', 'e'], | ||
}), | ||
).toEqual({ | ||
a: 5, | ||
b: 2, | ||
c: 4, | ||
d: 1, | ||
e: 3, | ||
}) | ||
}) | ||
}) |
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
2 changes: 1 addition & 1 deletion
2
src/methods/two-round-runoff/trr.test.ts → ...two-round-runoff/two-round-runoff.test.ts
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import twoRoundRunoff from '.' | ||
import { twoRoundRunoff } from '.' | ||
|
||
it('skips empty votes', () => { | ||
expect( | ||
|
Oops, something went wrong.