-
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 #26 from lzear/maximal-lotteries
feat: add a new voting system: maximal lotteries
- Loading branch information
Showing
21 changed files
with
622 additions
and
123 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
exclude_paths: | ||
- '**/*.test.ts' | ||
- 'src/test/' | ||
- 'tools/gh-pages-publish.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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exclude_patterns: | ||
- '**/*.test.ts' | ||
- 'src/test/' |
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,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 was deleted.
Oops, something went wrong.
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,38 @@ | ||
import zipObject from 'lodash/zipObject' | ||
import { solve } from '../../simplex' | ||
import { | ||
SystemUsingMatrix, | ||
VotingSystem, | ||
Matrix, | ||
ScoreObject, | ||
} from '../../types' | ||
import { makeAntisymetric } from '../../utils' | ||
import { findCondorcet } from '../../utils/condorcet' | ||
|
||
export const computeLottery = ( | ||
matrix: Matrix, | ||
): { [candidate: string]: number } => { | ||
const condorset = findCondorcet(matrix) | ||
|
||
const subVector = solve(condorset.array).map((v) => Math.max(0, v)) | ||
|
||
// Fixups because I can't implement the simplex algorithm correctly | ||
// ---------- Begin ---------- | ||
const sum = subVector.reduce((acc, cur) => acc + cur, 0) | ||
const normalizedVector = subVector.map((v) => v / sum) | ||
// ---------- End ---------- | ||
|
||
return matrix.candidates | ||
.filter((candidate) => !condorset.candidates.includes(candidate)) | ||
.reduce( | ||
(acc, cur) => ({ [cur]: 0, ...acc }), | ||
zipObject(condorset.candidates, normalizedVector), | ||
) | ||
} | ||
|
||
export const maximalLotteries: SystemUsingMatrix = { | ||
type: VotingSystem.MaximalLotteries, | ||
computeFromMatrix(matrix: Matrix): ScoreObject { | ||
return computeLottery(makeAntisymetric(matrix)) | ||
}, | ||
} |
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,89 @@ | ||
import { performPivots, simplexTableau } from '../../simplex' | ||
import { matrixString } from '../../simplex/utils' | ||
import { maximalLotteries } 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('maximal lotteries', () => { | ||
it('works with "simple" example', () => { | ||
expect( | ||
maximalLotteries.computeFromMatrix({ | ||
array: example1, | ||
candidates: ['a', 'b', 'c'], | ||
}), | ||
).toEqual({ | ||
a: 0.2857142857142857, | ||
b: 0.5714285714285714, | ||
c: 0.14285714285714285, | ||
}) | ||
}) | ||
it('works with "complexer" example', () => { | ||
expect( | ||
maximalLotteries.computeFromMatrix({ array: example2, candidates }), | ||
).toEqual({ | ||
a: 0, | ||
b: 0.2857142857142857, | ||
c: 0.5714285714285714, | ||
d: 0.14285714285714285, | ||
e: 0, | ||
}) | ||
}) | ||
it('works with "example3" example', () => { | ||
expect( | ||
maximalLotteries.computeFromMatrix({ | ||
array: example3, | ||
candidates: ['a', 'b', 'c', 'd', 'e'], | ||
}), | ||
).toEqual({ | ||
a: 0.6428571428571428, | ||
b: 0, | ||
c: 0, | ||
d: 0, | ||
e: 0.3571428571428572, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('pivot', () => { | ||
it('matrixString', () => { | ||
expect(matrixString(example1)).toEqual(` | ||
0 -2 8 | ||
2 0 -4 | ||
-8 4 0`) | ||
}) | ||
it('tableau', () => { | ||
expect(matrixString(simplexTableau(example1))).toEqual(` | ||
0 -1 -1 -1 1 0 0 0 0 1 | ||
0 0 -2 8 0 1 0 0 0 0 | ||
0 2 0 -4 0 0 1 0 0 0 | ||
0 -8 4 0 0 0 0 1 0 0 | ||
1 1 1 1 0 0 0 0 1 0`) | ||
}) | ||
it('no row', () => { | ||
expect(() => performPivots(simplexTableau(example1), [1, 2, 3, 4])).toThrow( | ||
'no row no pivot', | ||
) | ||
}) | ||
}) |
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,20 @@ | ||
import { | ||
SystemUsingMatrix, | ||
VotingSystem, | ||
Matrix, | ||
ScoreObject, | ||
} from '../../types' | ||
import { makeAntisymetric } from '../../utils' | ||
import { computeLottery } from '../maximal-lotteries' | ||
|
||
export const randomizedCondorcet: SystemUsingMatrix = { | ||
type: VotingSystem.RandomizedCondorcet, | ||
computeFromMatrix(matrix: Matrix): ScoreObject { | ||
const antisymetric = makeAntisymetric(matrix) | ||
const antisymetricUnit = { | ||
...antisymetric, | ||
array: antisymetric.array.map((r) => r.map(Math.sign)), | ||
} | ||
return computeLottery(antisymetricUnit) | ||
}, | ||
} |
89 changes: 89 additions & 0 deletions
89
src/methods/randomized-condorcet/randomized-condorcet.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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { performPivots, simplexTableau } from '../../simplex' | ||
import { matrixString } from '../../simplex/utils' | ||
import { randomizedCondorcet } 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('randomized condorcet', () => { | ||
it('works with "simple" example', () => { | ||
expect( | ||
randomizedCondorcet.computeFromMatrix({ | ||
array: example1, | ||
candidates: ['a', 'b', 'c'], | ||
}), | ||
).toEqual({ | ||
a: 0.3333333333333333, | ||
b: 0.3333333333333333, | ||
c: 0.3333333333333333, | ||
}) | ||
}) | ||
it('works with "complexer" example', () => { | ||
expect( | ||
randomizedCondorcet.computeFromMatrix({ array: example2, candidates }), | ||
).toEqual({ | ||
a: 0, | ||
b: 0.3333333333333333, | ||
c: 0.3333333333333333, | ||
d: 0.3333333333333333, | ||
e: 0, | ||
}) | ||
}) | ||
it('works with "example3" example', () => { | ||
expect( | ||
randomizedCondorcet.computeFromMatrix({ | ||
array: example3, | ||
candidates: ['a', 'b', 'c', 'd', 'e'], | ||
}), | ||
).toEqual({ | ||
a: 0.3333333333333333, | ||
b: 0, | ||
c: 0.3333333333333333, | ||
d: 0, | ||
e: 0.3333333333333333, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('pivot', () => { | ||
it('matrixString', () => { | ||
expect(matrixString(example1)).toEqual(` | ||
0 -2 8 | ||
2 0 -4 | ||
-8 4 0`) | ||
}) | ||
it('tableau', () => { | ||
expect(matrixString(simplexTableau(example1))).toEqual(` | ||
0 -1 -1 -1 1 0 0 0 0 1 | ||
0 0 -2 8 0 1 0 0 0 0 | ||
0 2 0 -4 0 0 1 0 0 0 | ||
0 -8 4 0 0 0 0 1 0 0 | ||
1 1 1 1 0 0 0 0 1 0`) | ||
}) | ||
it('no row', () => { | ||
expect(() => performPivots(simplexTableau(example1), [1, 2, 3, 4])).toThrow( | ||
'no row no pivot', | ||
) | ||
}) | ||
}) |
Oops, something went wrong.