UNPKG

ecclesia

Version:

Framework for political and electoral simulations

1 lines 3.1 kB
{"version":3,"sources":["../../../src/election/attribution/randomFactory.ts","../../../src/utils.ts"],"sourcesContent":["import { Counter } from \"@gouvernathor/python/collections\";\nimport { createRandomObj, type RandomObjParam } from \"../../utils\";\nimport { type Simple } from \"../ballots\";\nimport { type Attribution, type HasNSeats } from \"../attribution\";\n\n/**\n * Randomized attribution.\n * Everyone votes, then one ballot is selected at random.\n * (One ballot per seat to fill, and assuming there's\n * enough ballots that the picking is with replacement.)\n *\n * The randomization is based on the given parameters. If a RNG object\n * is passed, it is used without reseeding across all calls of the attribution.\n * If a seed is passed, the random object is reseeded\n * at each call of the attribution.\n */\nexport function randomize<Party>(\n { nSeats, ...randomParam }: {\n nSeats: number,\n } & RandomObjParam\n): Attribution<Party, Simple<Party>> & HasNSeats {\n const attrib = (votes: Simple<Party>, rest = {}): Counter<Party> => {\n const randomObj = createRandomObj(randomParam);\n return new Counter(randomObj.choices([...votes.keys()], { weights: [...votes.values()], k: nSeats }));\n };\n attrib.nSeats = nSeats;\n return attrib;\n}\n","import RNG from \"@gouvernathor/rng\";\n\nexport type RandomObjParam = {randomObj: RNG} | {randomSeed?: number|string};\n\nexport function createRandomObj(param?: RandomObjParam): RNG;\nexport function createRandomObj({ randomObj, randomSeed }:\n { randomObj?: RNG, randomSeed?: number | string } = {},\n): RNG {\n if (randomObj === undefined) {\n randomObj = new RNG(randomSeed);\n }\n return randomObj;\n}\n\n\n// https://stackoverflow.com/a/71700658\n\n/**\n * Mutable tuple of a single element type and a given length.\n *\n * Warning: due to limitations in TypeScript, when N is unknown/generic,\n * the typing system does not recognize that the type extends array.\n * Using methods such as map will require a (double) cast first to any then to T[].\n */\nexport type Tuple<\n T,\n N extends number,\n R extends T[] = [],\n> = R['length'] extends N ? R : Tuple<T, N, [T, ...R]>;\n\n/**\n * Immutable version of Tuple.\n *\n * Warning: due to limitations in TypeScript, when N is unknown/generic,\n * the typing system does not recognize that the type extends readonly array.\n * Using methods such as map will require a (double) cast first to any then to readonly T[].\n */\nexport type ReadonlyTuple<T, N extends number> = Readonly<Tuple<T, N>>;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAwB;;;ACAxB,iBAAgB;AAKT,SAAS,gBAAgB,EAAE,WAAW,WAAW,IACA,CAAC,GAClD;AACH,MAAI,cAAc,QAAW;AACzB,gBAAY,IAAI,WAAAA,QAAI,UAAU;AAAA,EAClC;AACA,SAAO;AACX;;;ADIO,SAAS,UACZ,EAAE,QAAQ,GAAG,YAAY,GAGoB;AAC7C,QAAM,SAAS,CAAC,OAAsB,OAAO,CAAC,MAAsB;AAChE,UAAM,YAAY,gBAAgB,WAAW;AAC7C,WAAO,IAAI,2BAAQ,UAAU,QAAQ,CAAC,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC,CAAC;AAAA,EACxG;AACA,SAAO,SAAS;AAChB,SAAO;AACX;","names":["RNG"]}