2020-10-18 18:58:24 +11:00
|
|
|
/*
|
|
|
|
pyRCV2: Preferential vote counting
|
|
|
|
Copyright © 2020 Lee Yingtong Li (RunasSudo)
|
2020-10-17 23:38:42 +11:00
|
|
|
|
2020-10-18 18:58:24 +11:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-12-31 16:44:17 +11:00
|
|
|
importScripts('vendor/BigInt_BigRat-a5f89e2.min.js', 'vendor/big-6.0.0.min.js', 'vendor/sjcl-1.0.8.min.js', 'bundle.js');
|
2020-10-18 02:55:15 +11:00
|
|
|
|
2021-01-07 03:45:12 +11:00
|
|
|
let stage, result, counter, ppDPs, tiesPrompt;
|
2020-12-27 23:28:16 +11:00
|
|
|
|
2020-10-18 18:58:24 +11:00
|
|
|
onmessage = function(evt) {
|
2020-12-27 23:28:16 +11:00
|
|
|
if (evt.data.type === 'init') {
|
|
|
|
// Set settings
|
|
|
|
if (evt.data.data.numbers === 'native') {
|
|
|
|
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Native);
|
|
|
|
}
|
|
|
|
if (evt.data.data.numbers === 'rational') {
|
|
|
|
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Rational);
|
|
|
|
}
|
|
|
|
if (evt.data.data.numbers === 'fixed') {
|
|
|
|
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Fixed);
|
|
|
|
py.pyRCV2.numbers.set_dps(evt.data.data.fixedDPs);
|
|
|
|
}
|
2021-01-14 22:31:24 +11:00
|
|
|
if (evt.data.data.numbers === 'gfixed') {
|
|
|
|
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.FixedGuarded);
|
|
|
|
py.pyRCV2.numbers.set_dps(evt.data.data.fixedDPs);
|
|
|
|
}
|
2020-12-27 23:28:16 +11:00
|
|
|
|
2021-01-07 03:45:12 +11:00
|
|
|
ppDPs = evt.data.data.ppDPs;
|
2020-12-27 23:28:16 +11:00
|
|
|
|
|
|
|
let election = py.pyRCV2.blt.readBLT(evt.data.data.data);
|
2021-01-09 23:58:25 +11:00
|
|
|
let total_ballots = py.pyRCV2.numbers.Num(0);
|
|
|
|
for (let b of election.ballots) {
|
|
|
|
total_ballots.__iadd__(b.value);
|
|
|
|
}
|
2020-12-27 23:28:16 +11:00
|
|
|
|
|
|
|
// Create counter
|
|
|
|
if (evt.data.data.transfers === 'uig') {
|
2021-01-08 19:21:33 +11:00
|
|
|
counter = py.pyRCV2.method.gregory.UIGSTVCounter(election, evt.data.data.options);
|
2020-12-29 02:43:37 +11:00
|
|
|
} else if (evt.data.data.transfers === 'eg') {
|
2021-01-08 19:21:33 +11:00
|
|
|
counter = py.pyRCV2.method.gregory.EGSTVCounter(election, evt.data.data.options);
|
2021-01-08 19:16:56 +11:00
|
|
|
} else if (evt.data.data.transfers === 'meek') {
|
|
|
|
counter = py.pyRCV2.method.meek.MeekSTVCounter(election, evt.data.data.options);
|
2020-12-27 23:28:16 +11:00
|
|
|
} else {
|
2021-01-08 19:21:33 +11:00
|
|
|
counter = py.pyRCV2.method.gregory.WIGSTVCounter(election, evt.data.data.options);
|
2020-12-27 23:28:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if (evt.data.data.options['ties'] === 'backwards_random') {
|
|
|
|
counter.options['ties'] = [py.pyRCV2.ties.TiesBackwards(counter), py.pyRCV2.ties.TiesRandom(evt.data.data.seed)];
|
2021-01-05 01:27:30 +11:00
|
|
|
} else if (evt.data.data.options['ties'] === 'forwards_random') {
|
|
|
|
counter.options['ties'] = [py.pyRCV2.ties.TiesForwards(counter), py.pyRCV2.ties.TiesRandom(evt.data.data.seed)];
|
2020-12-27 23:28:16 +11:00
|
|
|
} else if (evt.data.data.options['ties'] === 'random') {
|
|
|
|
counter.options['ties'] = [py.pyRCV2.ties.TiesRandom(evt.data.data.seed)];
|
|
|
|
} else if (evt.data.data.options['ties'] === 'prompt') {
|
|
|
|
tiesPrompt = py.pyRCV2.ties.TiesPrompt();
|
|
|
|
counter.options['ties'] = [tiesPrompt];
|
|
|
|
}
|
|
|
|
|
2021-01-09 23:58:25 +11:00
|
|
|
postMessage({
|
|
|
|
'type': 'init',
|
|
|
|
'election': {
|
|
|
|
'name': election.py_name,
|
2021-01-11 03:01:51 +11:00
|
|
|
'candidates': election.candidates.map(c => c.py_name),
|
|
|
|
'seats': election.seats,
|
2021-01-09 23:58:25 +11:00
|
|
|
},
|
|
|
|
'total_ballots': total_ballots.pp(0),
|
|
|
|
'options': counter.describe_options(),
|
|
|
|
});
|
|
|
|
|
2020-12-27 23:28:16 +11:00
|
|
|
// Reset
|
2021-01-03 01:23:53 +11:00
|
|
|
stage = 1;
|
2021-01-08 19:16:56 +11:00
|
|
|
try {
|
|
|
|
result = counter.reset();
|
|
|
|
} catch (ex) {
|
|
|
|
handleException(ex);
|
|
|
|
return;
|
|
|
|
}
|
2020-12-27 23:28:16 +11:00
|
|
|
|
2021-01-08 19:16:56 +11:00
|
|
|
if (py.isinstance(result, py.pyRCV2.model.CountCompleted)) {
|
|
|
|
postMessage({'type': 'done', 'result': resultToJS(result)});
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
postMessage({'type': 'result', 'result': resultToJS(result)});
|
|
|
|
}
|
2020-12-27 23:28:16 +11:00
|
|
|
|
|
|
|
stepElection();
|
|
|
|
} else if (evt.data.type === 'require_input') {
|
|
|
|
// Data received from the user
|
|
|
|
tiesPrompt.buffer = evt.data.input;
|
|
|
|
stepElection();
|
2020-12-24 00:04:30 +11:00
|
|
|
}
|
2020-12-27 23:28:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function stepElection() {
|
2020-10-17 23:38:42 +11:00
|
|
|
while (true) {
|
2020-12-27 23:28:16 +11:00
|
|
|
try {
|
|
|
|
result = counter.step();
|
|
|
|
} catch (ex) {
|
2021-01-08 19:16:56 +11:00
|
|
|
handleException(ex);
|
|
|
|
return;
|
2020-12-27 23:28:16 +11:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:16:56 +11:00
|
|
|
stage += 1;
|
|
|
|
|
2020-10-17 23:38:42 +11:00
|
|
|
if (py.isinstance(result, py.pyRCV2.model.CountCompleted)) {
|
2021-01-05 03:37:09 +11:00
|
|
|
postMessage({'type': 'done', 'result': resultToJS(result)});
|
2020-10-17 23:38:42 +11:00
|
|
|
break;
|
|
|
|
} else {
|
2021-01-05 03:37:09 +11:00
|
|
|
postMessage({'type': 'result', 'result': resultToJS(result)});
|
2020-10-17 23:38:42 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 03:37:09 +11:00
|
|
|
|
2021-01-08 19:16:56 +11:00
|
|
|
function handleException(ex) {
|
|
|
|
if (py.isinstance(ex, py.pyRCV2.ties.RequireInput)) {
|
|
|
|
// Signals we require input to break a tie
|
|
|
|
postMessage({'type': 'require_input', 'message': ex.message});
|
|
|
|
} else if (py.isinstance(ex, py.pyRCV2.method.base_stv.STVException)) {
|
|
|
|
console.error(ex);
|
|
|
|
postMessage({'type': 'stv_exception', 'message': ex.message});
|
|
|
|
} else {
|
|
|
|
console.error(ex);
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 03:37:09 +11:00
|
|
|
function resultToJS(result) {
|
|
|
|
return {
|
|
|
|
'stage': stage,
|
|
|
|
'comment': result.comment,
|
2021-01-09 04:33:13 +11:00
|
|
|
'logs': result.logs,
|
2021-01-05 03:37:09 +11:00
|
|
|
'candidates': result.candidates.py_items().map(([c, cc]) => [c.py_name, {
|
2021-01-07 03:45:12 +11:00
|
|
|
'transfers': cc.transfers.pp(ppDPs),
|
|
|
|
'votes': cc.votes.pp(ppDPs),
|
2021-01-05 03:37:09 +11:00
|
|
|
'state': cc.state,
|
|
|
|
'order_elected': cc.order_elected,
|
|
|
|
}]),
|
|
|
|
'exhausted': {
|
2021-01-07 03:45:12 +11:00
|
|
|
'transfers': result.exhausted.transfers.pp(ppDPs),
|
|
|
|
'votes': result.exhausted.votes.pp(ppDPs)
|
2021-01-05 03:37:09 +11:00
|
|
|
},
|
|
|
|
'loss_fraction': {
|
2021-01-07 03:45:12 +11:00
|
|
|
'transfers': result.loss_fraction.transfers.pp(ppDPs),
|
|
|
|
'votes': result.loss_fraction.votes.pp(ppDPs)
|
2021-01-05 03:37:09 +11:00
|
|
|
},
|
2021-01-07 03:45:12 +11:00
|
|
|
'total': result.total.pp(ppDPs),
|
|
|
|
'quota': result.quota.pp(ppDPs),
|
|
|
|
'vote_required_election': result.vote_required_election === null ? null : result.vote_required_election.pp(ppDPs),
|
2021-01-05 03:37:09 +11:00
|
|
|
};
|
|
|
|
}
|