poi-plugin-brainoff 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/engine/rng.js CHANGED
@@ -1,169 +1,169 @@
1
- 'use strict';
2
-
3
- // Fast deterministic 32-bit PRNG (Mulberry32)
4
- function mulberry32(seed) {
5
- let a = seed >>> 0;
6
- return function next() {
7
- a |= 0;
8
- a = (a + 0x6d2b79f5) | 0;
9
- let t = Math.imul(a ^ (a >>> 15), 1 | a);
10
- t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
11
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
12
- };
13
- }
14
-
15
- // Synchronous scoped Math.random override (guaranteed restore via try/finally)
16
- function withRng(rngFn, fn) {
17
- if (!rngFn) return fn();
18
- const prev = Math.random;
19
- Math.random = rngFn;
20
- try {
21
- return fn();
22
- } finally {
23
- Math.random = prev;
24
- }
25
- }
26
-
27
- // Create independent per-trial Environment RNG and Combat RNG streams for 2-Stream CRN
28
- function createTrialRngs(baseSeed, trialIndex) {
29
- const s1 = (Math.imul(baseSeed ^ 0x9e3779b9, 2654435761) + Math.imul(trialIndex + 1, 2246822519)) >>> 0;
30
- const s2 = (Math.imul(baseSeed ^ 0x85ebca6b, 3266489917) + Math.imul(trialIndex + 1, 668265263)) >>> 0;
31
- return {
32
- envRng: mulberry32(s1),
33
- combatRng: mulberry32(s2),
34
- };
35
- }
36
-
37
- // Phase L (LBAS, design_lbas sec. 2.2): dedicated per-trial RNG stream for the land-based air squadrons (waves, jet assault,
38
- // base air raid). Created by sortie.js only when LBAS is configured (normalized options.lbas present), so runs without LBAS
39
- // never call it and their envRng / combatRng sequences are unchanged; with LBAS the fleet battle still draws from the same
40
- // combatRng sequence it would see if the LBAS hook consumed nothing (the hook runs inside withRng(lbasRng, ...)).
41
- // Mulberry32 seeded like createTrialRngs but with its own constants, so the stream is independent of envRng / combatRng.
42
- function createLbasRng(baseSeed, trialIndex) {
43
- const s3 = (Math.imul(baseSeed ^ 0x27d4eb2f, 0x165667b1) + Math.imul(trialIndex + 1, 0x2545f491)) >>> 0;
44
- return mulberry32(s3);
45
- }
46
- // Wave 59 (geminireview §39): dedicated per-trial stream of the player's battle detection roll (one draw per battle in which the
47
- // fleet launches planes), so envRng / combatRng sequences stay unchanged and a run only differs where detection fails.
48
- const DETECT_SEED_SALT = 0x5d0e7ec7;
49
- function createDetectRng(baseSeed, trialIndex) {
50
- const s = (Math.imul(baseSeed ^ DETECT_SEED_SALT, 0x2c1b3c6d) + Math.imul(trialIndex + 1, 0x297a2d39)) >>> 0;
51
- return mulberry32(s);
52
- }
53
-
54
- // Paired comparison between Candidate A (baseline) and Candidate B (challenger) on identical CRN trials.
55
- // valuesA / valuesB are arrays of per-trial numeric scores (e.g. 0/1 for win, or continuous cost).
56
- // Higher is better when maximize=true; lower is better when maximize=false.
57
- function pairedCompare(valuesA, valuesB, maximize = true) {
58
- const n = Math.min(valuesA.length, valuesB.length);
59
- if (n === 0) return { n: 0, meanDiff: 0, se: 0, z: 0, ci95: 0, improved: 0, regressed: 0, mcnemarZ: 0 };
60
- const sign = maximize ? 1 : -1;
61
- let sumD = 0;
62
- let sumD2 = 0;
63
- let improved = 0; // A=0, B=1 (or B better)
64
- let regressed = 0; // A=1, B=0 (or A better)
65
- for (let i = 0; i < n; i++) {
66
- const d = sign * (valuesB[i] - valuesA[i]);
67
- sumD += d;
68
- sumD2 += d * d;
69
- if (d > 0) improved++;
70
- else if (d < 0) regressed++;
71
- }
72
- const meanDiff = sumD / n;
73
- const variance = n > 1 ? Math.max(0, (sumD2 - (sumD * sumD) / n) / (n - 1)) : 0;
74
- const se = Math.sqrt(variance / n);
75
- const z = se > 1e-12 ? meanDiff / se : (meanDiff > 0 ? 99 : meanDiff < 0 ? -99 : 0);
76
- const ci95 = 1.96 * se;
77
- const disc = improved + regressed;
78
- const mcnemarZ = disc > 0 ? (improved - regressed) / Math.sqrt(disc) : 0;
79
- return { n, meanDiff, se, z, ci95, improved, regressed, mcnemarZ };
80
- }
81
-
82
- // Wilson 95% score interval [lowPct, highPct] for a binomial proportion (in percentage points, 0..100).
83
- // Unlike Wald (which collapses to [0,0]/[100,100]), Wilson stays asymmetric and well-calibrated at boundary rates.
84
- function binomialCI95BoundsPct(successes, trials) {
85
- if (!trials) return [0, 0];
86
- const z = 1.96;
87
- const z2 = z * z;
88
- const p = successes / trials;
89
- const denom = 1 + z2 / trials;
90
- const center = (p + z2 / (2 * trials)) / denom;
91
- const halfWidth = (z * Math.sqrt((p * (1 - p)) / trials + z2 / (4 * trials * trials))) / denom;
92
- const low = Math.max(0, center - halfWidth);
93
- const high = Math.min(1, center + halfWidth);
94
- return [Number((low * 100).toFixed(1)), Number((high * 100).toFixed(1))];
95
- }
96
-
97
- // Wilson 95% score interval half-width (kept for backward-compatible numeric fields; prefer binomialCI95BoundsPct for display).
98
- function binomialCI95Pct(successes, trials) {
99
- if (!trials) return 0;
100
- const z = 1.96;
101
- const z2 = z * z;
102
- const p = successes / trials;
103
- const denom = 1 + z2 / trials;
104
- const halfWidth = (z * Math.sqrt((p * (1 - p)) / trials + z2 / (4 * trials * trials))) / denom;
105
- return Number((halfWidth * 100).toFixed(1));
106
- }
107
-
108
- // Build the vsTop1 summary for candidate #k vs #1 from a pairedCompare(top1, candidate) result (d = candidate - top1).
109
- // tied: |z| < 1.96; better: candidate significantly ahead of #1 (z >= 1.96), surfaced to users because ranking keeps Pass A order.
110
- // Wave 28: kind 'count' (nodeS / kills) reports the difference in counts per sortie (delta / ci95; deltaPct / ci95Pct null);
111
- // the default 'rate' object is unchanged.
112
- function buildVsTop1(metric, cmp, kind = 'rate') {
113
- if (kind === 'count') {
114
- return {
115
- metric,
116
- kind: 'count',
117
- delta: Number(cmp.meanDiff.toFixed(3)),
118
- ci95: Number(cmp.ci95.toFixed(3)),
119
- deltaPct: null,
120
- ci95Pct: null,
121
- z: Number(cmp.z.toFixed(2)),
122
- tied: Math.abs(cmp.z) < 1.96,
123
- better: cmp.z >= 1.96 && cmp.meanDiff > 0,
124
- };
125
- }
126
- return {
127
- metric,
128
- deltaPct: Number((cmp.meanDiff * 100).toFixed(1)),
129
- ci95Pct: Number((cmp.ci95 * 100).toFixed(1)),
130
- z: Number(cmp.z.toFixed(2)),
131
- tied: Math.abs(cmp.z) < 1.96,
132
- better: cmp.z >= 1.96 && cmp.meanDiff > 0,
133
- };
134
- }
135
-
136
- // costPerS flavour of vsTop1 (Wave 19 P2-25): metric / delta / CI / z all describe cost per boss S.
137
- // costCmp = { delta: candidate - #1 cost per S (negative = cheaper), se, z } from the paired 1-DoF linearized test (z > 0 = candidate
138
- // cheaper = better), or null when cost per S is undefined (S = 0). sCmp = pairedCompare(#1 S, candidate S) -> deltaSPct / ci95SPct / zS.
139
- // deltaPct / ci95Pct are null here on purpose (they always mean "metric difference in pp" for the rate objectives).
140
- function buildVsTop1CostPerS(costCmp, sCmp) {
141
- const z = costCmp ? costCmp.z : 0;
142
- return {
143
- metric: 'costPerS',
144
- deltaPct: null,
145
- ci95Pct: null,
146
- deltaCostPerS: costCmp ? Math.round(costCmp.delta) : null,
147
- ci95CostPerS: costCmp ? Math.round(1.96 * costCmp.se) : null,
148
- z: costCmp ? Number(z.toFixed(2)) : null,
149
- tied: costCmp ? Math.abs(z) < 1.96 : false,
150
- better: costCmp ? z >= 1.96 : false,
151
- costPerSUndefined: !costCmp,
152
- deltaSPct: Number((sCmp.meanDiff * 100).toFixed(1)),
153
- ci95SPct: Number((sCmp.ci95 * 100).toFixed(1)),
154
- zS: Number(sCmp.z.toFixed(2)),
155
- };
156
- }
157
-
158
- module.exports = {
159
- buildVsTop1CostPerS,
160
- mulberry32,
161
- withRng,
162
- createTrialRngs,
163
- createLbasRng,
164
- createDetectRng, DETECT_SEED_SALT, // Wave 59
165
- pairedCompare,
166
- buildVsTop1,
167
- binomialCI95Pct,
168
- binomialCI95BoundsPct,
169
- };
1
+ 'use strict';
2
+
3
+ // Fast deterministic 32-bit PRNG (Mulberry32)
4
+ function mulberry32(seed) {
5
+ let a = seed >>> 0;
6
+ return function next() {
7
+ a |= 0;
8
+ a = (a + 0x6d2b79f5) | 0;
9
+ let t = Math.imul(a ^ (a >>> 15), 1 | a);
10
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
11
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
12
+ };
13
+ }
14
+
15
+ // Synchronous scoped Math.random override (guaranteed restore via try/finally)
16
+ function withRng(rngFn, fn) {
17
+ if (!rngFn) return fn();
18
+ const prev = Math.random;
19
+ Math.random = rngFn;
20
+ try {
21
+ return fn();
22
+ } finally {
23
+ Math.random = prev;
24
+ }
25
+ }
26
+
27
+ // Create independent per-trial Environment RNG and Combat RNG streams for 2-Stream CRN
28
+ function createTrialRngs(baseSeed, trialIndex) {
29
+ const s1 = (Math.imul(baseSeed ^ 0x9e3779b9, 2654435761) + Math.imul(trialIndex + 1, 2246822519)) >>> 0;
30
+ const s2 = (Math.imul(baseSeed ^ 0x85ebca6b, 3266489917) + Math.imul(trialIndex + 1, 668265263)) >>> 0;
31
+ return {
32
+ envRng: mulberry32(s1),
33
+ combatRng: mulberry32(s2),
34
+ };
35
+ }
36
+
37
+ // Phase L (LBAS, design_lbas sec. 2.2): dedicated per-trial RNG stream for the land-based air squadrons (waves, jet assault,
38
+ // base air raid). Created by sortie.js only when LBAS is configured (normalized options.lbas present), so runs without LBAS
39
+ // never call it and their envRng / combatRng sequences are unchanged; with LBAS the fleet battle still draws from the same
40
+ // combatRng sequence it would see if the LBAS hook consumed nothing (the hook runs inside withRng(lbasRng, ...)).
41
+ // Mulberry32 seeded like createTrialRngs but with its own constants, so the stream is independent of envRng / combatRng.
42
+ function createLbasRng(baseSeed, trialIndex) {
43
+ const s3 = (Math.imul(baseSeed ^ 0x27d4eb2f, 0x165667b1) + Math.imul(trialIndex + 1, 0x2545f491)) >>> 0;
44
+ return mulberry32(s3);
45
+ }
46
+ // Wave 59 (geminireview §39): dedicated per-trial stream of the player's battle detection roll (one draw per battle in which the
47
+ // fleet launches planes), so envRng / combatRng sequences stay unchanged and a run only differs where detection fails.
48
+ const DETECT_SEED_SALT = 0x5d0e7ec7;
49
+ function createDetectRng(baseSeed, trialIndex) {
50
+ const s = (Math.imul(baseSeed ^ DETECT_SEED_SALT, 0x2c1b3c6d) + Math.imul(trialIndex + 1, 0x297a2d39)) >>> 0;
51
+ return mulberry32(s);
52
+ }
53
+
54
+ // Paired comparison between Candidate A (baseline) and Candidate B (challenger) on identical CRN trials.
55
+ // valuesA / valuesB are arrays of per-trial numeric scores (e.g. 0/1 for win, or continuous cost).
56
+ // Higher is better when maximize=true; lower is better when maximize=false.
57
+ function pairedCompare(valuesA, valuesB, maximize = true) {
58
+ const n = Math.min(valuesA.length, valuesB.length);
59
+ if (n === 0) return { n: 0, meanDiff: 0, se: 0, z: 0, ci95: 0, improved: 0, regressed: 0, mcnemarZ: 0 };
60
+ const sign = maximize ? 1 : -1;
61
+ let sumD = 0;
62
+ let sumD2 = 0;
63
+ let improved = 0; // A=0, B=1 (or B better)
64
+ let regressed = 0; // A=1, B=0 (or A better)
65
+ for (let i = 0; i < n; i++) {
66
+ const d = sign * (valuesB[i] - valuesA[i]);
67
+ sumD += d;
68
+ sumD2 += d * d;
69
+ if (d > 0) improved++;
70
+ else if (d < 0) regressed++;
71
+ }
72
+ const meanDiff = sumD / n;
73
+ const variance = n > 1 ? Math.max(0, (sumD2 - (sumD * sumD) / n) / (n - 1)) : 0;
74
+ const se = Math.sqrt(variance / n);
75
+ const z = se > 1e-12 ? meanDiff / se : (meanDiff > 0 ? 99 : meanDiff < 0 ? -99 : 0);
76
+ const ci95 = 1.96 * se;
77
+ const disc = improved + regressed;
78
+ const mcnemarZ = disc > 0 ? (improved - regressed) / Math.sqrt(disc) : 0;
79
+ return { n, meanDiff, se, z, ci95, improved, regressed, mcnemarZ };
80
+ }
81
+
82
+ // Wilson 95% score interval [lowPct, highPct] for a binomial proportion (in percentage points, 0..100).
83
+ // Unlike Wald (which collapses to [0,0]/[100,100]), Wilson stays asymmetric and well-calibrated at boundary rates.
84
+ function binomialCI95BoundsPct(successes, trials) {
85
+ if (!trials) return [0, 0];
86
+ const z = 1.96;
87
+ const z2 = z * z;
88
+ const p = successes / trials;
89
+ const denom = 1 + z2 / trials;
90
+ const center = (p + z2 / (2 * trials)) / denom;
91
+ const halfWidth = (z * Math.sqrt((p * (1 - p)) / trials + z2 / (4 * trials * trials))) / denom;
92
+ const low = Math.max(0, center - halfWidth);
93
+ const high = Math.min(1, center + halfWidth);
94
+ return [Number((low * 100).toFixed(1)), Number((high * 100).toFixed(1))];
95
+ }
96
+
97
+ // Wilson 95% score interval half-width (kept for backward-compatible numeric fields; prefer binomialCI95BoundsPct for display).
98
+ function binomialCI95Pct(successes, trials) {
99
+ if (!trials) return 0;
100
+ const z = 1.96;
101
+ const z2 = z * z;
102
+ const p = successes / trials;
103
+ const denom = 1 + z2 / trials;
104
+ const halfWidth = (z * Math.sqrt((p * (1 - p)) / trials + z2 / (4 * trials * trials))) / denom;
105
+ return Number((halfWidth * 100).toFixed(1));
106
+ }
107
+
108
+ // Build the vsTop1 summary for candidate #k vs #1 from a pairedCompare(top1, candidate) result (d = candidate - top1).
109
+ // tied: |z| < 1.96; better: candidate significantly ahead of #1 (z >= 1.96), surfaced to users because ranking keeps Pass A order.
110
+ // Wave 28: kind 'count' (nodeS / kills) reports the difference in counts per sortie (delta / ci95; deltaPct / ci95Pct null);
111
+ // the default 'rate' object is unchanged.
112
+ function buildVsTop1(metric, cmp, kind = 'rate') {
113
+ if (kind === 'count') {
114
+ return {
115
+ metric,
116
+ kind: 'count',
117
+ delta: Number(cmp.meanDiff.toFixed(3)),
118
+ ci95: Number(cmp.ci95.toFixed(3)),
119
+ deltaPct: null,
120
+ ci95Pct: null,
121
+ z: Number(cmp.z.toFixed(2)),
122
+ tied: Math.abs(cmp.z) < 1.96,
123
+ better: cmp.z >= 1.96 && cmp.meanDiff > 0,
124
+ };
125
+ }
126
+ return {
127
+ metric,
128
+ deltaPct: Number((cmp.meanDiff * 100).toFixed(1)),
129
+ ci95Pct: Number((cmp.ci95 * 100).toFixed(1)),
130
+ z: Number(cmp.z.toFixed(2)),
131
+ tied: Math.abs(cmp.z) < 1.96,
132
+ better: cmp.z >= 1.96 && cmp.meanDiff > 0,
133
+ };
134
+ }
135
+
136
+ // costPerS flavour of vsTop1 (Wave 19 P2-25): metric / delta / CI / z all describe cost per boss S.
137
+ // costCmp = { delta: candidate - #1 cost per S (negative = cheaper), se, z } from the paired 1-DoF linearized test (z > 0 = candidate
138
+ // cheaper = better), or null when cost per S is undefined (S = 0). sCmp = pairedCompare(#1 S, candidate S) -> deltaSPct / ci95SPct / zS.
139
+ // deltaPct / ci95Pct are null here on purpose (they always mean "metric difference in pp" for the rate objectives).
140
+ function buildVsTop1CostPerS(costCmp, sCmp) {
141
+ const z = costCmp ? costCmp.z : 0;
142
+ return {
143
+ metric: 'costPerS',
144
+ deltaPct: null,
145
+ ci95Pct: null,
146
+ deltaCostPerS: costCmp ? Math.round(costCmp.delta) : null,
147
+ ci95CostPerS: costCmp ? Math.round(1.96 * costCmp.se) : null,
148
+ z: costCmp ? Number(z.toFixed(2)) : null,
149
+ tied: costCmp ? Math.abs(z) < 1.96 : false,
150
+ better: costCmp ? z >= 1.96 : false,
151
+ costPerSUndefined: !costCmp,
152
+ deltaSPct: Number((sCmp.meanDiff * 100).toFixed(1)),
153
+ ci95SPct: Number((sCmp.ci95 * 100).toFixed(1)),
154
+ zS: Number(sCmp.z.toFixed(2)),
155
+ };
156
+ }
157
+
158
+ module.exports = {
159
+ buildVsTop1CostPerS,
160
+ mulberry32,
161
+ withRng,
162
+ createTrialRngs,
163
+ createLbasRng,
164
+ createDetectRng, DETECT_SEED_SALT, // Wave 59
165
+ pairedCompare,
166
+ buildVsTop1,
167
+ binomialCI95Pct,
168
+ binomialCI95BoundsPct,
169
+ };