ecclesia
Version:
Framework for political and electoral simulations
1 lines • 3.33 kB
Source Map (JSON)
{"version":3,"sources":["../../src/actors/disagreement.ts"],"sourcesContent":["import { sum } from \"@gouvernathor/python\";\nimport { DisagreementFunction } from \"../election/voting\";\nimport { OpinionsArray, OpinionsArrayManager } from \"./opinionsArray\";\n\n// shorthand type aliases\ntype DiffFunction<N extends number> = DisagreementFunction<OpinionsArray<N>, OpinionsArray<N>>;\ntype OpinParams<N extends number> = Pick<OpinionsArrayManager<N>, \"nOpinions\" | \"opinMax\">;\n\n/**\n * Typically used for comparison between instances of the same kind.\n */\nexport function symmetricDiff<N extends number>(\n { nOpinions, opinMax }: OpinParams<N>,\n): DiffFunction<N> {\n return (a, b) =>\n sum((<number[]><any>a).map((oa, i) => Math.abs(oa - (<number[]><any>b)[i]))) / (nOpinions * 2 * opinMax);\n}\n\n/**\n * The second operand is the one ponderating the difference.\n * It's intended as the one whose point of view is taken, the \"most human\" of the two.\n */\nexport function ponderedDiff<N extends number>(\n { nOpinions, opinMax }: OpinParams<N>,\n): DiffFunction<N> {\n return (a, b) =>\n sum((<number[]><any>a).map((oa, i) => Math.abs(oa - (<number[]><any>b)[i]) * (<number[]><any>b)[i])) / (nOpinions * 2 * (opinMax ** 2));\n}\n\n/**\n * The second operand is again the \"most human\", the one whose point of view is taken.\n * This simulates agreeing plainly with laws that aren't going far enough,\n * but disagreeing with laws that go too far or that go the wrong way.\n *\n * For each opinion:\n * - if A's opinion a is of the opposite sign of B's opinion b, it \"goes the wrong way\".\n * - the difference is Math.abs(a * b)\n * - if A's opinion a is of the same sign as B's opinion b but closer to 0, it \"doesn't go far enough\".\n * - the difference is 0\n * - otherwise, it \"goes too far\".\n * - the difference is Math.abs(a - b)\n */\nexport function fromMaxDiff<N extends number>(\n { nOpinions, opinMax }: OpinParams<N>,\n): DiffFunction<N> {\n return (a, b) => {\n let diffSum = 0;\n for (let i = 0; i < nOpinions; i++) {\n const ao = (<number[]><any>a)[i];\n const bo = (<number[]><any>b)[i];\n if (ao * bo < 0) {\n diffSum += Math.abs(ao * bo);\n } else if (Math.abs(ao) < Math.abs(bo)) {\n ;\n } else {\n diffSum += Math.abs(ao - bo);\n }\n }\n return diffSum / (nOpinions * (opinMax ** 2));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoB;AAWb,SAAS,cACZ,EAAE,WAAW,QAAQ,GACN;AACf,SAAO,CAAC,GAAG,UACP,mBAAoB,EAAG,IAAI,CAAC,IAAI,MAAM,KAAK,IAAI,KAAqB,EAAG,CAAC,CAAC,CAAC,CAAC,KAAK,YAAY,IAAI;AACxG;AAMO,SAAS,aACZ,EAAE,WAAW,QAAQ,GACN;AACf,SAAO,CAAC,GAAG,UACP,mBAAoB,EAAG,IAAI,CAAC,IAAI,MAAM,KAAK,IAAI,KAAqB,EAAG,CAAC,CAAC,IAAoB,EAAG,CAAC,CAAC,CAAC,KAAK,YAAY,IAAK,WAAW;AAC5I;AAeO,SAAS,YACZ,EAAE,WAAW,QAAQ,GACN;AACf,SAAO,CAAC,GAAG,MAAM;AACb,QAAI,UAAU;AACd,aAAS,IAAI,GAAG,IAAI,WAAW,KAAK;AAChC,YAAM,KAAqB,EAAG,CAAC;AAC/B,YAAM,KAAqB,EAAG,CAAC;AAC/B,UAAI,KAAK,KAAK,GAAG;AACb,mBAAW,KAAK,IAAI,KAAK,EAAE;AAAA,MAC/B,WAAW,KAAK,IAAI,EAAE,IAAI,KAAK,IAAI,EAAE,GAAG;AACpC;AAAA,MACJ,OAAO;AACH,mBAAW,KAAK,IAAI,KAAK,EAAE;AAAA,MAC/B;AAAA,IACJ;AACA,WAAO,WAAW,YAAa,WAAW;AAAA,EAC9C;AACJ;","names":[]}