-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstv.js
241 lines (217 loc) · 9.3 KB
/
stv.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const ACTION = {
COUNT_ROUND: "@ROUND",
TRANSFER: ">TRANSFER",
ELIMINATE: "-ELIMINATE",
QUOTA: "!QUOTA",
ELECT: "+ELECT",
COUNT: ".COUNT",
ZOMBIES: "~ZOMBIES",
RANDOM: "*RANDOM",
THRESHOLD: "^THRESHOLD"
};
const ballotSeparator = "\n";
const voteSeparator = "\t";
function runStv() {
// Transform input
const ballots = document.getElementById("csvInput").value.trim().split(ballotSeparator).map(ballotTxt => {
return new Ballot(ballotTxt.trim().split(voteSeparator).map(voteTxt => voteTxt.trim()).filter(vote => vote))
}).filter(ballot => ballot.candidates.length > 0);
const seats = parseInt(document.getElementById("seat").value);
document.getElementById("inputBox").innerHTML = "";
document.getElementById("output").style.display = "block";
const threshold = 1 + (ballots.length / (seats + 1)); // Droop quota
output("BALLOT COUNT", ballots.length);
output("SEATS", seats);
output(ACTION.THRESHOLD, threshold);
let allocated = {}; // The allocation of ballots to candidates
let voteCount = {} // A hash of ballot counts, indexed by candidates
let candidates = [] // All candidates
let elected = [] // The candidates that have been elected
let hopefuls; // The candidates that may be elected
let eliminated = [] // The candidates that have been eliminated because of low counts
// Initial count
for (const ballot of ballots) {
const selected = ballot.candidates[0];
for (const candidate of ballot.candidates) {
if (!candidates.includes(candidate)) {
candidates.push(candidate);
voteCount[candidate] = 0;
}
if (!allocated.hasOwnProperty(candidate)) {
allocated[candidate] = [];
}
}
allocated[selected].push(ballot);
voteCount[selected]++;
}
hopefuls = candidates; // In the beginning, all candidates are hopefuls
// Start rounds
let currentRound = 1;
let numElected = elected.length;
let numHopefuls = hopefuls.length;
while (numElected < seats && numHopefuls > 0) {
output(ACTION.COUNT_ROUND, currentRound);
hopefuls.sort((hopeful1, hopeful2) => voteCount[hopeful2] - voteCount[hopeful1]);
output(ACTION.COUNT, countDescription(voteCount, hopefuls));
// If there is a surplus record it so that we can try to redistribute
// the best candidate's votes according to their next preferences
const surplus = voteCount[hopefuls[0]] - threshold;
// If there is either a candidate with surplus votes, or
// there are hopeful candidates beneath the threshold.
if (surplus >= 0 || numHopefuls <= (seats - numElected)) {
const bestCandidate = randomlySelectFirst(hopefuls, voteCount, ACTION.ELECT);
if (!hopefuls.includes(bestCandidate)) {
alert("Not a valid candidate: " + bestCandidate);
}
hopefuls = hopefuls.filter(hopeful => hopeful !== bestCandidate); // Remove from hopefuls
// Elect
elected.push([bestCandidate, currentRound, voteCount[bestCandidate]]);
output(ACTION.ELECT, bestCandidate + " = " + voteCount[bestCandidate]);
if (surplus > 0) {
// Calculate the weight for this round
const weight = surplus / voteCount[bestCandidate];
// Find the next eligible preference for each one of the ballots
// cast for the candidate, and transfer the vote to that
// candidate with its value adjusted by the correct weight.
allocated = redistributeBallots(bestCandidate, weight, hopefuls, allocated, voteCount);
}
} else {
// If nobody can get elected, take the least hopeful candidate
// (i.e., the hopeful candidate with the less votes) and redistribute that candidate's votes.
hopefuls.reverse();
const worstCandidate = randomlySelectFirst(hopefuls, voteCount, ACTION.ELIMINATE);
hopefuls = hopefuls.filter(hopeful => hopeful !== worstCandidate);
eliminated.push(worstCandidate);
output(ACTION.ELIMINATE, worstCandidate + " = " + voteCount[worstCandidate]);
allocated = redistributeBallots(worstCandidate, 1, hopefuls, allocated, voteCount);
}
currentRound++;
numHopefuls = hopefuls.length;
numElected = elected.length;
}
while ((seats - numElected) > 0 && eliminated.length > 0) {
// If there is either a candidate with surplus votes,
// or there are hopeful candidates beneath the threshold.
output(ACTION.COUNT, currentRound);
output(ACTION.ZOMBIES, countDescription(voteCount, eliminated));
const bestCandidate = eliminated.pop();
// Elect
elected.push([bestCandidate, currentRound, voteCount[bestCandidate]]);
output(ACTION.ELECT, bestCandidate + " = " + voteCount[bestCandidate]);
currentRound++;
}
output('', '');
for (const e of elected) {
output("***ELECTED", e[0] + " at round " + e[1] + " with " + e[2] + " votes");
}
}
function output(tag, description) {
document.getElementById("output").innerHTML += "<div><b>" + tag + "</b> " + description + "</div>";
}
function countDescription(voteCount, hopefuls) {
return hopefuls.map(hopeful => {
return hopeful + " = " + voteCount[hopeful];
}).join(", ");
}
function randomlySelectFirst(sequence, key, action) {
/* Selects the first item of equals in a sorted sequence of items.
For the given sorted sequence, returns the first item if it
is different than the second; if there are ties so that there
are items with equal values, it randomly selects among those items.
The value of each item in the sequence is provided by applying the
function key to the item. The action parameter indicates the context
in which the random selection takes place (election or elimination).
random_generator, if given, is the function that produces the random
selection. */
const firstValue = key[sequence[0]];
const collected = [];
for (const candidate of sequence) {
if (key[candidate] === firstValue) {
collected.push(candidate);
} else {
break;
}
}
let selected = collected[0];
const numEligibles = collected.length;
if (numEligibles > 1) {
selected = randomArrayMember(collected);
output(ACTION.RANDOM, selected + " from " + collected + " to " + action)
}
return selected;
}
function randomArrayMember(items) {
return items[Math.floor(Math.random() * items.length)];
}
function redistributeBallots(selected, weight, hopefuls, allocated, voteCount) {
const transferred = [];
// Keep a hash of ballot moves for logging purposes.
// Keys are a tuple of the form (from_recipient, to_recipient, value)
// where value is the current value of the ballot. Each tuple points
// to the ballot being moved.
const moves = {};
for (const ballot of allocated[selected]) {
let reallocated = false;
let i = ballot.currentPreference + 1;
while (!reallocated && i < ballot.candidates.length) {
const recipient = ballot.candidates[i];
if (hopefuls.includes(recipient)) {
ballot.currentPreference = i;
ballot.addWeight(weight);
const currentValue = ballot.getValue();
if (allocated.hasOwnProperty(recipient)) {
allocated[recipient].push(ballot);
} else {
allocated[recipient] = [ballot];
}
if (voteCount.hasOwnProperty(recipient)) {
voteCount[recipient] += currentValue;
} else {
voteCount[recipient] = currentValue;
}
voteCount[selected] -= currentValue;
reallocated = true;
const move = [selected, recipient, currentValue].join(" #!# ");
if (moves.hasOwnProperty(move)) {
moves[move].push(ballot);
} else {
moves[move] = [ballot];
}
transferred.push(ballot);
} else {
i++;
}
}
}
for (const moveKey in moves) {
const ballots = moves[moveKey];
const times = ballots.length;
const move = moveKey.split(" #!# ");
output(ACTION.TRANSFER, "from " + move[0] + " to " + move[1] + " " + times + "*" + parseFloat(move[2]).toFixed(3) + "=" + (times * parseFloat(move[2])).toFixed(3));
}
allocated[selected] = allocated[selected].filter(ballot => !transferred.includes(ballot));
return allocated;
}
class Ballot {
candidates = [];
weights = [1.0];
currentPreference = 0;
_value = 1.0;
constructor(candidates = []) {
this.candidates = candidates;
}
addWeight(weight) {
this.weights.push(weight);
this._value *= weight;
}
getValue() {
return this._value;
}
}
document.getElementById("first-start").onclick = function () {
if (document.getElementById("seat").value && document.getElementById("csvInput").value) {
runStv();
} else {
M.toast({html: "Invalid input"});
}
}