UNPKG

ecclesia

Version:

Framework for political and electoral simulations

191 lines (180 loc) 6.75 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/election/attribution/orderingFactory.ts var orderingFactory_exports = {}; __export(orderingFactory_exports, { bordaCount: () => bordaCount, condorcet: () => condorcet, instantRunoff: () => instantRunoff }); module.exports = __toCommonJS(orderingFactory_exports); var import_python4 = require("@gouvernathor/python"); var import_collections7 = require("@gouvernathor/python/collections"); // src/election/attribution/base.ts var AttributionFailure = class extends Error { }; // src/election/attribution/transform.ts var import_collections = require("@gouvernathor/python/collections"); // src/election/attribution/proportionalBase.ts var import_collections2 = require("@gouvernathor/python/collections"); function stationaryDivisorFunction(r) { return (k) => k + r; } function rankIndexFunctionFromDivisorFunction(divisorFunction) { return (t, a) => t / divisorFunction(a); } // src/election/attribution/majorityFactory.ts var import_python = require("@gouvernathor/python"); var import_collections3 = require("@gouvernathor/python/collections"); // src/election/attribution/scoreFactory.ts var import_python2 = require("@gouvernathor/python"); var import_collections4 = require("@gouvernathor/python/collections"); var import_statistics = require("@gouvernathor/python/statistics"); // src/election/ballots.ts var Scores; ((Scores2) => { function get(key) { const value = this.get(key); if (value === void 0) { return Array(this.ngrades).fill(0); } return value; } function fromEntries(elements) { if (elements.length === 0) { throw new Error("Use the fromGrades method to create an empty Scores instance"); } const ths = new Map(elements); ths.ngrades = elements[0][1].length; ths.get = get.bind(ths); return ths; } Scores2.fromEntries = fromEntries; function fromGrades(ngrades) { const ths = /* @__PURE__ */ new Map(); ths.ngrades = ngrades; ths.get = get.bind(ths); return ths; } Scores2.fromGrades = fromGrades; })(Scores || (Scores = {})); // src/election/attribution/proportionalFactory.ts var import_python3 = require("@gouvernathor/python"); var import_collections5 = require("@gouvernathor/python/collections"); var divisor1 = stationaryDivisorFunction(1); var huntingtonHillBaseRankIndexFunction = rankIndexFunctionFromDivisorFunction((k) => Math.sqrt(k * (k + 1))); // src/election/attribution/randomFactory.ts var import_collections6 = require("@gouvernathor/python/collections"); // src/utils.ts var import_rng = __toESM(require("@gouvernathor/rng"), 1); // src/election/attribution/orderingFactory.ts function instantRunoff({ nSeats }) { const attrib = (votes, rest = {}) => { const blacklisted = /* @__PURE__ */ new Set(); const nParties = new Set(votes.flat()).size; for (let pn = 0; pn < nParties; pn++) { const firstPlaces = new import_collections7.Counter(); for (const ballot of votes) { for (const party of ballot) { if (!blacklisted.has(party)) { firstPlaces.increment(party); break; } } } const total = firstPlaces.total; for (const [party, score] of firstPlaces) { if (score / total > 0.5) { return new import_collections7.Counter([[party, nSeats]]); } } blacklisted.add((0, import_python4.min)(firstPlaces.keys(), (p) => firstPlaces.get(p))); } throw new Error("Should not happen"); }; attrib.nSeats = nSeats; return attrib; } function bordaCount({ nSeats }) { const attrib = (votes, rest = {}) => { const scores = new import_collections7.Counter(); for (const ballot of votes) { for (const [i, party] of (0, import_python4.enumerate)(ballot.slice().reverse(), 1)) { scores.increment(party, i); } } return new import_collections7.Counter([[(0, import_python4.max)(scores.keys(), (p) => scores.get(p)), nSeats]]); }; attrib.nSeats = nSeats; return attrib; } function condorcet({ nSeats, contingency = null }) { const attrib = (votes, rest = {}) => { const counts = new import_collections7.DefaultMap(() => new import_collections7.Counter()); const majority = votes.length / 2; for (const ballot of votes) { for (const [i, party1] of (0, import_python4.enumerate)(ballot)) { for (const party2 of ballot.slice(i + 1)) { counts.get(party1).increment(party2); } } } const win = new Set(counts.keys()); for (const [party, partyCounter] of counts) { for (const value of partyCounter.pos.values()) { if (value > majority) { win.delete(party); break; } } } if (win.size !== 1) { if (win.size !== 0) { throw new Error("Bad attribution"); } if (contingency === null) { throw new condorcet.Standoff("No Condorcet winner"); } return contingency(votes, rest); } const [winner] = win; return new import_collections7.Counter([[winner, nSeats]]); }; attrib.nSeats = nSeats; return attrib; } condorcet.Standoff = class CondorcetStandoff extends AttributionFailure { }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { bordaCount, condorcet, instantRunoff }); //# sourceMappingURL=orderingFactory.cjs.map