ecclesia
Version:
Framework for political and electoral simulations
164 lines (153 loc) • 5.96 kB
JavaScript
;
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/transform.ts
var transform_exports = {};
__export(transform_exports, {
addThresholdToSimpleAttribution: () => addThresholdToSimpleAttribution
});
module.exports = __toCommonJS(transform_exports);
var import_collections7 = require("@gouvernathor/python/collections");
// src/election/attribution/base.ts
var AttributionFailure = class extends Error {
};
// src/election/attribution/proportionalBase.ts
var import_collections = 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_collections2 = require("@gouvernathor/python/collections");
// src/election/attribution/orderingFactory.ts
var import_python2 = require("@gouvernathor/python");
var import_collections3 = require("@gouvernathor/python/collections");
function condorcet({ nSeats, contingency = null }) {
const attrib = (votes, rest = {}) => {
const counts = new import_collections3.DefaultMap(() => new import_collections3.Counter());
const majority = votes.length / 2;
for (const ballot of votes) {
for (const [i, party1] of (0, import_python2.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_collections3.Counter([[winner, nSeats]]);
};
attrib.nSeats = nSeats;
return attrib;
}
condorcet.Standoff = class CondorcetStandoff extends AttributionFailure {
};
// src/election/attribution/scoreFactory.ts
var import_python3 = 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_python4 = 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/transform.ts
function addThresholdToSimpleAttribution({ threshold, attribution, contingency = attribution }) {
const attrib = (votes, rest = {}) => {
if (threshold > 0) {
const original_votes = votes;
const votes_threshold = threshold * votes.total;
votes = new import_collections7.Counter([...votes.entries()].filter(([_, v]) => v >= votes_threshold));
if (votes.size === 0) {
if (contingency === null) {
throw new AttributionFailure("No party reached the threshold");
}
return contingency(original_votes, rest);
}
}
return attribution(votes, rest);
};
return attrib;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addThresholdToSimpleAttribution
});
//# sourceMappingURL=transform.cjs.map