UNPKG

ecclesia

Version:

Framework for political and electoral simulations

164 lines (160 loc) 5.39 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/voting.ts var voting_exports = {}; __export(voting_exports, { approvalVote: () => approvalVote, balancedCardinalVote: () => balancedCardinalVote, cardinalVote: () => cardinalVote, orderingVote: () => orderingVote, singleVote: () => singleVote, toShuffledVote: () => toShuffledVote }); module.exports = __toCommonJS(voting_exports); var import_python = require("@gouvernathor/python"); var import_collections = require("@gouvernathor/python/collections"); // src/utils.ts var import_rng = __toESM(require("@gouvernathor/rng"), 1); function createRandomObj({ randomObj, randomSeed } = {}) { if (randomObj === void 0) { randomObj = new import_rng.default(randomSeed); } return randomObj; } // 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/voting.ts function toShuffledVote({ voting, ...rest }) { return (voters, candidates) => { const randomObj = createRandomObj(rest); const partees = randomObj.shuffled(candidates); return voting(voters, partees); }; } function singleVote({ disagree }) { return (voters, candidates) => { const scores = import_collections.Counter.fromkeys(candidates, 0); for (const voter of voters) { scores.increment((0, import_python.min)(candidates, (party) => disagree(voter, party))); } return scores; }; } function orderingVote({ disagree }) { return (voters, candidates) => { const order = []; for (const voter of voters) { order.push([...candidates].sort((a, b) => disagree(voter, a) - disagree(voter, b))); } return order; }; } function cardinalVote({ nGrades, disagree }) { return (voters, candidates) => { const scores = Scores.fromGrades(nGrades); for (const voter of voters) { for (const party of candidates) { const grade = Math.min( nGrades - 1, Math.floor(nGrades) * (1 - disagree(voter, party)) ); scores.get(party)[grade]++; } } return scores; }; } function balancedCardinalVote({ nGrades, disagree }) { return (voters, candidates) => { const scores = Scores.fromGrades(nGrades); for (const voter of voters) { const prefs = new Map(Array.from(candidates, (party) => [party, 1 - disagree(voter, party)])); const minPref = Math.min(...prefs.values()); let maxPref = Math.max(...prefs.values()); if (minPref !== maxPref) { maxPref -= minPref; } for (const party of candidates) { const grade = Math.min( nGrades - 1, Math.floor(nGrades * (prefs.get(party) - minPref) / maxPref) ); scores.get(party)[grade]++; } } return scores; }; } function approvalVote({ disagree }) { const cardinal = balancedCardinalVote({ nGrades: 2, disagree }); return (voters, candidates) => { const scores = cardinal(voters, candidates); const approvals = new import_collections.Counter(); for (const [party, [_disapproval, approval]] of scores) { approvals.increment(party, approval); } return approvals; }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { approvalVote, balancedCardinalVote, cardinalVote, orderingVote, singleVote, toShuffledVote }); //# sourceMappingURL=voting.cjs.map