Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Method of Equal Shares #599

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions packages/backend/src/Tabulators/MethodOfEqualShares.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { methodOfEqualShares } from "./MethodOfEqualShares";

describe("Method of Equal Shares Tests", () => {
test("Basic Example", () => {
const candidates = ["Allison", "Bill", "Carmen", "Doug"];
const votes = [
[0, 5, 0, 0],
[5, 5, 0, 0],
[5, 5, 0, 0],
[5, 5, 0, 0],
[5, 5, 5, 5],
[0, 0, 0, 5],
[0, 0, 5, 5],
[0, 0, 5, 5],
[0, 0, 5, 5],
[0, 0, 5, 5],
];
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(["Carmen", "Bill", "Doug"]).toEqual(expect.arrayContaining([results.elected[1].name]));
expect(["Doug", "Allison"]).toEqual(expect.arrayContaining([results.elected[1].name]));
});

test("Basic Example 2", () => {
const candidates = ["Allison", "Bill", "Carmen", "Doug"];
const votes = [
[0,1,1,1],
[1,1,1,1],
[1,0,1,1],
[1,1,0,0],
[1,1,1,1],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0],
[1,1,1,0],
[0,1,0,0],
[0,1,1,0],
[0,1,1,0],
[0,1,0,1],
[0,0,1,0],
[1,1,1,0],
[1,1,0,1],
[0,1,0,1],
[0,1,1,1],
];
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(results.elected[0].name).toBe("Bill");
expect(results.elected[1].name).toBe("Carmen");
});

test("Basic Example 3", () => {
const candidates = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
];

// 1000111100000010
// 0100000011000111
// 1000111101011010
// 1111011000000000
// 0001001001111100
// 1101100111011001
// 1111111000011110
// 0110100010001011
// 0110011111001100
// 1001011111110010

const votes = [
[1,0,0,0,0,1,0,1,0,1,1],
[1,1,0,1,0,0,1,1,1,0,1],
[1,0,0,0,0,0,0,1,0,1,0],
[0,0,1,0,0,1,1,0,0,1,0],
[1,1,1,1,1,1,0,1,0,1,1],
[1,1,0,0,0,1,0,0,1,1,1],
[1,0,0,1,0,0,1,0,1,0,0],
[0,1,1,0,0,1,0,0,1,0,1],
[1,1,1,0,1,0,1,0,1,1,0],
[1,1,1,0,1,1,1,1,0,1,0],
[0,0,1,0,0,1,1,0,0,0,0],
[1,1,0,0,1,0,0,1,1,1,0],
[1,0,0,0,1,0,1,1,1,1,1],
[0,1,0,1,1,1,0,1,0,0,1],
[0,0,1,0,0,1,1,1,0,0,0],
[0,0,1,0,1,0,1,1,1,1,1],
[1,0,1,0,1,1,1,0,0,1,1],
[1,0,0,1,1,0,0,0,1,1,0],
[1,1,1,1,1,1,0,0,0,1,1],
[1,0,0,1,0,0,0,0,0,1,1],
[0,0,1,1,0,1,1,0,0,0,1],
[1,1,0,0,0,1,0,0,0,0,1],
[1,1,0,1,0,1,1,0,0,1,1],
[1,0,0,1,0,1,0,1,0,0,1],
[0,1,0,0,1,1,1,1,0,1,0],
[1,1,1,0,1,0,0,1,1,0,1],
[0,1,0,0,0,0,0,1,1,1,1],
[1,0,0,0,0,0,1,0,0,0,0],
[1,0,1,0,0,0,1,0,0,1,1],
[1,0,1,1,1,1,1,0,0,0,0],
[1,0,0,1,1,0,1,1,1,1,1],
[1,1,0,1,1,1,0,1,0,0,0],
[0,0,1,0,1,0,0,1,1,0,1],
[1,0,1,0,1,0,0,0,0,0,0],
[0,1,0,1,0,0,1,0,0,0,0],

];
const results = methodOfEqualShares(candidates, votes, 4, []);
console.log(results.summaryData)
console.log(results.elected)
console.log(...results.roundResults)
expect(results.elected.length).toBe(4);
expect(results.elected[0].name).toBe("0");
expect(["6", "10", "5",]).toEqual(expect.arrayContaining([results.elected[1].name]));
expect(results.elected[2].name).toBe("9");
expect(["6", "10", "5","9"]).toEqual(expect.arrayContaining([results.elected[3].name]));
});

test("Influence Budget Redistribution", () => {
const candidates = ["Allison", "Bill", "Carmen", "Doug"];
const votes = [
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 4, 4, 0],
[0, 0, 0, 3],
[0, 0, 4, 5],
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(results.elected[0].name).toBe("Allison");
expect(results.elected[1].name).toBe("Bill");
});

test("Random Tiebreaker", () => {
const candidates = ["Allison", "Bill", "Carmen", "Doug"];
const votes = [
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 4, 0],
[0, 0, 0, 3],
[0, 0, 4, 5],
[0, 0, 4, 5],
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = methodOfEqualShares(candidates, votes, 2, []);
expect(results.elected.length).toBe(2);
expect(results.elected[0].name).toBe("Allison"); // random tiebreaker, second place lower index 1
expect(results.elected[1].name).toBe("Bill");
});

test("Random Tiebreaker with Defined Order", () => {
const candidates = ["Allison", "Bill", "Carmen", "Doug"];
const votes = [
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 1, 0],
[5, 5, 4, 0],
[0, 0, 0, 3],
[0, 0, 4, 5],
[0, 0, 4, 5],
[0, 0, 4, 5],
[0, 0, 4, 5],
];
const results = methodOfEqualShares(candidates, votes, 2, ["Doug", "Carmen", "Bill", "Allison"]); expect(results.elected.length).toBe(2);
expect(results.elected[0].name).toBe("Allison"); // random tiebreaker, second place lower index 1
expect(results.elected[1].name).toBe("Bill");
});

test("Valid/Invalid/Under/Bullet Vote Counts", () => {
const candidates = ["Allison", "Bill", "Carmen"];
const votes = [
[1, 3, 5],
[1, 3, 5],
[1, 3, 5],
[0, 0, 0],
[0, 0, 0],
[-1, 3, 5],
[0, 3, 6],
[5, 0, 0],
[0, 5, 0],
[0, 0, 5],
];
const results = methodOfEqualShares(candidates, votes, 1, []);
expect(results.summaryData.nValidVotes).toBe(8);
expect(results.summaryData.nInvalidVotes).toBe(2);
expect(results.summaryData.nUnderVotes).toBe(2);
expect(results.summaryData.nBulletVotes).toBe(3);
});
});
Loading