-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (181 loc) · 6.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Fraction = require('fraction.js');
function WrightSTV(numVacancies, votes, candidates) {
const quota = calculateDroopQuota(numVacancies, votes.length);
return determineWinnersIteratively(numVacancies, quota, votes, candidates);
}
exports.WrightSTV = WrightSTV;
function calculateDroopQuota(numVacancies, numVotes) {
return Fraction(numVotes).div(1 + numVacancies).add(1).floor();
}
exports.calculateDroopQuota = calculateDroopQuota;
function calculateHareQuota(numVacancies, numVotes) {
return Fraction(numVotes).div(numVacancies).floor();
}
exports.calculateHareQuota = calculateHareQuota;
class Candidate {
constructor(name) {
this.name = name;
}
static filterCandidateFromList(candidates, candidateToRemove) {
return candidates.filter(c => c !== candidateToRemove);
}
}
exports.Candidate = Candidate;
class Vote {
constructor(prefs) {
this.prefs = Array.from(new Set(prefs));
}
}
exports.Vote = Vote;
class RoundVote {
constructor(vote) {
this.vote = vote;
this.value = Fraction(1);
}
mostPreferred(candidates) {
const prefCandidate = this.vote.prefs.find(c => candidates.includes(c));
if (!prefCandidate) {
throw new Error('exhausted vote, no mostPreferred amongst candidates');
}
return prefCandidate;
}
updateValueWithSurplusRatio(surplusRatio) {
this.value = this.value.mul(surplusRatio);
}
static fromVotes(votes) {
return votes.map(v => new RoundVote(v));
}
}
exports.RoundVote = RoundVote;
class CandidateResult {
constructor(candidate) {
this.candidate = candidate;
this.cachedRoundVotes = [];
this.cachedValue = Fraction(0);
}
saveRoundVote(roundVote) {
this.cachedValue = this.cachedValue.add(roundVote.value);
this.cachedRoundVotes.push(roundVote);
}
isWinner(quota) {
return this.cachedValue.compare(quota) >= 0;
}
isVoteSurplus(quota) {
return this.cachedValue.compare(quota) > 0;
}
declareVoteSurplus(quota) {
if (this.cachedValue.compare(quota) <= 0) {
throw new Error();
}
const surplusRatio = this.calculateSurplusRatio(quota);
this.cachedRoundVotes.forEach(rv => rv.updateValueWithSurplusRatio(surplusRatio));
this.cachedValue = quota;
return this.cachedRoundVotes;
}
calculateSurplusRatio(quota) {
if (this.cachedValue.compare(quota) <= 0) {
throw new Error();
}
return this.cachedValue.sub(quota).div(this.cachedValue);
}
static sortHighest(a, b) {
const byValue = b.cachedValue.sub(a.cachedValue);
if (byValue.compare(0) === 0) {
const byNumVotes = a.cachedRoundVotes.length - b.cachedRoundVotes.length;
if (byNumVotes === 0) {
// not sure how to handle this scenario...
return 0;
}
return byNumVotes;
}
return byValue;
}
}
exports.CandidateResult = CandidateResult;
class ResultMap extends Map {
static fromCandidates(candidates) {
const resultMap = new ResultMap();
candidates.forEach(c => resultMap.set(c, new CandidateResult(c)));
return resultMap;
}
saveRoundVotes(roundVotes, validCandidates) {
roundVotes.forEach(roundVote => {
try {
const prefCandidate = roundVote.mostPreferred(validCandidates);
const prefCandidateResult = this.get(prefCandidate);
prefCandidateResult.saveRoundVote(roundVote);
}
catch (e) {
// ignore exhausted votes
}
});
}
getSortedResults() {
return Array.from(this.values()).sort(CandidateResult.sortHighest);
}
}
exports.ResultMap = ResultMap;
function determineWinnersIteratively(numVacancies, quota, legalVotes, candidates) {
if (candidates.length < numVacancies) {
throw new Error();
}
else if (candidates.length === numVacancies) {
return candidates;
}
const roundVotes = RoundVote.fromVotes(legalVotes);
const stableResultMap = calculateStableVoteResult(quota, roundVotes, candidates);
const winningCandidates = determineWinners(quota, stableResultMap);
if (winningCandidates.length === numVacancies) {
return winningCandidates;
}
const remainingCandidates = filterExcludedCandidate(stableResultMap);
return determineWinnersIteratively(numVacancies, quota, legalVotes, remainingCandidates);
}
exports.determineWinnersIteratively = determineWinnersIteratively;
function calculateStableVoteResult(quota, roundVotes, candidates, resultMap) {
if (!resultMap) {
resultMap = calculateInitialVoteResult(roundVotes, candidates);
}
const surplusCandidate = determineSurplusCandidate(quota, resultMap);
if (surplusCandidate === false) {
return resultMap;
}
const remainingCandidates = Candidate.filterCandidateFromList(candidates, surplusCandidate);
updateResultWithSurplusVotes(quota, remainingCandidates, resultMap, surplusCandidate);
return calculateStableVoteResult(quota, roundVotes, remainingCandidates, resultMap);
}
exports.calculateStableVoteResult = calculateStableVoteResult;
function calculateInitialVoteResult(roundVotes, candidates) {
const resultMap = ResultMap.fromCandidates(candidates);
resultMap.saveRoundVotes(roundVotes, candidates);
return resultMap;
}
exports.calculateInitialVoteResult = calculateInitialVoteResult;
function determineSurplusCandidate(quota, resultMap) {
const sortedResults = resultMap.getSortedResults();
const highestSurplus = sortedResults.find(result => result.isVoteSurplus(quota));
return highestSurplus === undefined ? false : highestSurplus.candidate;
}
function updateResultWithSurplusVotes(quota, remainingCandidates, resultMap, surplusCandidate) {
const candidateResult = resultMap.get(surplusCandidate);
const surplusVotes = candidateResult.declareVoteSurplus(quota);
resultMap.saveRoundVotes(surplusVotes, remainingCandidates);
}
exports.updateResultWithSurplusVotes = updateResultWithSurplusVotes;
function determineWinners(quota, stableResultMap) {
return stableResultMap
.getSortedResults()
.filter(result => result.isWinner(quota))
.map(result => result.candidate);
}
exports.determineWinners = determineWinners;
function filterExcludedCandidate(stableResultMap) {
return stableResultMap
.getSortedResults()
.map(result => result.candidate)
.reverse()
.slice(1);
}
exports.filterExcludedCandidate = filterExcludedCandidate;