poker-flopsolve 0.4.1 → 0.5.0-beta.0
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/CHANGELOG.md +73 -0
- package/README.md +936 -86
- package/dist/browser-threads.js +238 -0
- package/dist/browser-threads.js.map +1 -0
- package/dist/browser.js +2195 -396
- package/dist/browser.js.map +1 -1
- package/dist/engine-worker.js +81 -50
- package/dist/engine-worker.js.map +1 -1
- package/dist/index.d.ts +1251 -30
- package/dist/index.js +2178 -392
- package/dist/index.js.map +1 -1
- package/dist/threads.d.ts +41 -0
- package/dist/threads.js +28 -0
- package/dist/threads.js.map +1 -0
- package/dist/wasm-storage.d.ts +84 -0
- package/dist/wasm-storage.js +532 -0
- package/dist/wasm-storage.js.map +1 -0
- package/dist/worker.js +1037 -240
- package/dist/worker.js.map +1 -1
- package/docs/APPROXIMATIONS.md +588 -0
- package/package.json +19 -4
package/dist/index.js
CHANGED
|
@@ -135,6 +135,10 @@ function mergeBetSizes(sizes, distance) {
|
|
|
135
135
|
flush();
|
|
136
136
|
return [...merged, ...others];
|
|
137
137
|
}
|
|
138
|
+
function actionLabel(action, target, chipScale) {
|
|
139
|
+
return action >= ActionKind.Bet ? `${ACTION_LABELS[action]} ${target / chipScale}` : ACTION_LABELS[action];
|
|
140
|
+
}
|
|
141
|
+
var THIN_BASE = 0.75;
|
|
138
142
|
function buildTree(config, runouts) {
|
|
139
143
|
const kind = [];
|
|
140
144
|
const player = [];
|
|
@@ -169,6 +173,27 @@ function buildTree(config, runouts) {
|
|
|
169
173
|
});
|
|
170
174
|
const noDonk = perRound(config.noDonk ?? false, Array.isArray(config.noDonk), rounds, "noDonk");
|
|
171
175
|
const half = Math.round(potUnits / 2);
|
|
176
|
+
const remove = config.removeActions && config.removeActions.length > 0 ? new Set(config.removeActions) : null;
|
|
177
|
+
const removed = /* @__PURE__ */ new Set();
|
|
178
|
+
const extend = (path, step) => remove ? path ? `${path} ${step}` : step : "";
|
|
179
|
+
const thin = config.thinSizes === true;
|
|
180
|
+
const thinned = (sized, state) => {
|
|
181
|
+
if (state.round === 0 && state.aggressions === 0 && state.acted === 0) return sized;
|
|
182
|
+
const me = state.toAct;
|
|
183
|
+
const mine = state.invested[me];
|
|
184
|
+
const owed = Math.max(0, state.invested[1 - me] - mine);
|
|
185
|
+
const potAfterCall = potUnits + state.invested[0] + state.invested[1] + owed;
|
|
186
|
+
const fraction2 = (target) => (target - mine - owed) / potAfterCall;
|
|
187
|
+
let nearest2 = -1;
|
|
188
|
+
sized.forEach((entry, i) => {
|
|
189
|
+
const distance = Math.abs(fraction2(entry.target) - THIN_BASE);
|
|
190
|
+
if (nearest2 < 0 || distance < Math.abs(fraction2(sized[nearest2].target) - THIN_BASE)) nearest2 = i;
|
|
191
|
+
});
|
|
192
|
+
const big = rounds > 1 && state.round === rounds - 1 && owed === 0 && state.streetsBet > 0;
|
|
193
|
+
return sized.filter(
|
|
194
|
+
(entry, i) => i === nearest2 || big && (entry.action === ActionKind.AllIn || fraction2(entry.target) > 1)
|
|
195
|
+
);
|
|
196
|
+
};
|
|
172
197
|
const addNode = (nodeKind, nodePlayer, nodeRound, nodePot, nodePayoff) => {
|
|
173
198
|
kind.push(nodeKind);
|
|
174
199
|
player.push(nodePlayer);
|
|
@@ -179,7 +204,7 @@ function buildTree(config, runouts) {
|
|
|
179
204
|
payoff.push(nodePayoff);
|
|
180
205
|
return kind.length - 1;
|
|
181
206
|
};
|
|
182
|
-
const closeRound = (state) => {
|
|
207
|
+
const closeRound = (state, path) => {
|
|
183
208
|
const total = potUnits + state.invested[0] + state.invested[1];
|
|
184
209
|
const allIn = state.invested[0] >= stackUnits || state.invested[1] >= stackUnits;
|
|
185
210
|
if (state.round + 1 >= runouts.rounds) {
|
|
@@ -195,9 +220,10 @@ function buildTree(config, runouts) {
|
|
|
195
220
|
acted: 0,
|
|
196
221
|
base: state.invested[0],
|
|
197
222
|
aggressor: -1,
|
|
198
|
-
previousAggressor: state.aggressor
|
|
223
|
+
previousAggressor: state.aggressor,
|
|
224
|
+
streetsBet: state.streetsBet + (state.aggressions > 0 ? 1 : 0)
|
|
199
225
|
};
|
|
200
|
-
const child = allIn ? closeRound(next) : buildDecision(next);
|
|
226
|
+
const child = allIn ? closeRound(next, extend(path, "/")) : buildDecision(next, extend(path, "/"));
|
|
201
227
|
firstEdge[node] = edgeChild.length;
|
|
202
228
|
numEdges[node] = 1;
|
|
203
229
|
edgeAction.push(ActionKind.Check);
|
|
@@ -224,7 +250,7 @@ function buildTree(config, runouts) {
|
|
|
224
250
|
if (raiseSize < minimum) target = Math.min(theirs + minimum, cap);
|
|
225
251
|
return target;
|
|
226
252
|
};
|
|
227
|
-
const buildDecision = (state) => {
|
|
253
|
+
const buildDecision = (state, path) => {
|
|
228
254
|
const me = state.toAct;
|
|
229
255
|
const opp = 1 - me;
|
|
230
256
|
const mine = state.invested[me];
|
|
@@ -243,6 +269,7 @@ function buildTree(config, runouts) {
|
|
|
243
269
|
const donk = noDonk[state.round] && me === 0 && owed === 0 && state.aggressions === 0 && state.previousAggressor === 1;
|
|
244
270
|
if (state.aggressions < maxRaises[me][state.round] && myRemaining > owed && !donk) {
|
|
245
271
|
const seen = /* @__PURE__ */ new Set();
|
|
272
|
+
const sized = [];
|
|
246
273
|
const myRaises = raiseSizes[me];
|
|
247
274
|
const sizes = owed > 0 && myRaises ? [...myRaises[state.round]] : [...betSizes[me][state.round] ?? []].filter((size) => !(typeof size === "object" && "times" in size));
|
|
248
275
|
if (allowAllIn[me][state.round]) sizes.push("allin");
|
|
@@ -254,10 +281,17 @@ function buildTree(config, runouts) {
|
|
|
254
281
|
if (target === null || seen.has(target)) continue;
|
|
255
282
|
seen.add(target);
|
|
256
283
|
const isShove = target >= stackUnits;
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
284
|
+
sized.push({ action: isShove ? ActionKind.AllIn : owed > 0 ? ActionKind.Raise : ActionKind.Bet, target });
|
|
285
|
+
}
|
|
286
|
+
for (const entry of thin ? thinned(sized, state) : sized) {
|
|
287
|
+
if (remove) {
|
|
288
|
+
const line = extend(path, actionLabel(entry.action, entry.target, chipScale));
|
|
289
|
+
if (remove.has(line)) {
|
|
290
|
+
removed.add(line);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
actions.push(entry);
|
|
261
295
|
}
|
|
262
296
|
}
|
|
263
297
|
const base = edgeChild.length;
|
|
@@ -269,6 +303,7 @@ function buildTree(config, runouts) {
|
|
|
269
303
|
edgeChild.push(-1);
|
|
270
304
|
}
|
|
271
305
|
actions.forEach((entry, i) => {
|
|
306
|
+
const line = remove ? extend(path, actionLabel(entry.action, entry.target, chipScale)) : "";
|
|
272
307
|
const invested = me === 0 ? [entry.target, theirs] : [theirs, entry.target];
|
|
273
308
|
let child;
|
|
274
309
|
if (entry.action === ActionKind.Fold) {
|
|
@@ -276,7 +311,7 @@ function buildTree(config, runouts) {
|
|
|
276
311
|
child = addNode(NodeKind.TerminalFold, -1, state.round, total, value);
|
|
277
312
|
} else if (entry.action === ActionKind.Check || entry.action === ActionKind.Call) {
|
|
278
313
|
const closed = invested[0] === invested[1] && state.acted + 1 >= 2;
|
|
279
|
-
child = closed ? closeRound({ ...state, invested, acted: state.acted + 1 }) : buildDecision({ ...state, invested, toAct: opp, acted: state.acted + 1 });
|
|
314
|
+
child = closed ? closeRound({ ...state, invested, acted: state.acted + 1 }, line) : buildDecision({ ...state, invested, toAct: opp, acted: state.acted + 1 }, line);
|
|
280
315
|
} else {
|
|
281
316
|
child = buildDecision({
|
|
282
317
|
round: state.round,
|
|
@@ -287,8 +322,9 @@ function buildTree(config, runouts) {
|
|
|
287
322
|
acted: state.acted + 1,
|
|
288
323
|
base: state.base,
|
|
289
324
|
aggressor: me,
|
|
290
|
-
previousAggressor: state.previousAggressor
|
|
291
|
-
|
|
325
|
+
previousAggressor: state.previousAggressor,
|
|
326
|
+
streetsBet: state.streetsBet
|
|
327
|
+
}, line);
|
|
292
328
|
}
|
|
293
329
|
edgeChild[base + i] = child;
|
|
294
330
|
});
|
|
@@ -303,8 +339,16 @@ function buildTree(config, runouts) {
|
|
|
303
339
|
acted: 0,
|
|
304
340
|
base: 0,
|
|
305
341
|
aggressor: -1,
|
|
306
|
-
previousAggressor: -1
|
|
307
|
-
|
|
342
|
+
previousAggressor: -1,
|
|
343
|
+
streetsBet: 0
|
|
344
|
+
}, "");
|
|
345
|
+
if (remove && removed.size < remove.size) {
|
|
346
|
+
const under = (line) => [...removed].some((cut) => line.startsWith(`${cut} `));
|
|
347
|
+
const missing = [...remove].filter((line) => !removed.has(line) && !under(line));
|
|
348
|
+
if (missing.length > 0) throw new Error(
|
|
349
|
+
`removeActions: no bet, raise or shove in this tree ends the line ${missing.map((m) => `"${m}"`).join(", ")}. Give each as Solution.path writes the node the action leads to, e.g. "check bet 18.15"; checks, calls and folds cannot be removed`
|
|
350
|
+
);
|
|
351
|
+
}
|
|
308
352
|
const decisionNodes = [];
|
|
309
353
|
for (let i = 0; i < kind.length; i++) if (kind[i] === NodeKind.Decision) decisionNodes.push(i);
|
|
310
354
|
return {
|
|
@@ -443,8 +487,6 @@ function enumerateRunouts(board, symmetries, merge) {
|
|
|
443
487
|
stabiliserMap.push(Int32Array.from(flat));
|
|
444
488
|
};
|
|
445
489
|
recordStabilisers(stabilisers);
|
|
446
|
-
const used = new Uint8Array(DECK);
|
|
447
|
-
const seen = new Uint8Array(DECK);
|
|
448
490
|
for (let round = 1; round < rounds; round++) {
|
|
449
491
|
const previousWidth = width[round - 1];
|
|
450
492
|
const previousCount = counts[round - 1];
|
|
@@ -459,49 +501,9 @@ function enumerateRunouts(board, symmetries, merge) {
|
|
|
459
501
|
const nextStabilisers = [];
|
|
460
502
|
for (let p = 0; p < previousCount; p++) {
|
|
461
503
|
starts[p] = parents.length;
|
|
462
|
-
used.fill(0);
|
|
463
|
-
seen.fill(0);
|
|
464
504
|
const base = p * previousWidth;
|
|
465
|
-
|
|
466
|
-
const
|
|
467
|
-
const children = [];
|
|
468
|
-
for (let card = 0; card < DECK; card++) {
|
|
469
|
-
if (used[card] || seen[card]) continue;
|
|
470
|
-
const child = { card, maps: [], cards: [], fixing: [], merged: false };
|
|
471
|
-
for (const sigma of stabiliser) {
|
|
472
|
-
const image = maps.card[sigma][card];
|
|
473
|
-
if (image === card) child.fixing.push(sigma);
|
|
474
|
-
if (seen[image]) continue;
|
|
475
|
-
seen[image] = 1;
|
|
476
|
-
child.maps.push(image === card ? 0 : maps.inverse(sigma));
|
|
477
|
-
child.cards.push(image);
|
|
478
|
-
}
|
|
479
|
-
children.push(child);
|
|
480
|
-
}
|
|
481
|
-
if (merge) {
|
|
482
|
-
const boardRanks = /* @__PURE__ */ new Set();
|
|
483
|
-
for (let i = 0; i < previousWidth; i++) boardRanks.add(previousCards[base + i] >> 2);
|
|
484
|
-
for (let j = 0; j < children.length; j++) {
|
|
485
|
-
const later = children[j];
|
|
486
|
-
const rankJ = later.card >> 2;
|
|
487
|
-
if (boardRanks.has(rankJ)) continue;
|
|
488
|
-
for (let i = 0; i < j; i++) {
|
|
489
|
-
const earlier = children[i];
|
|
490
|
-
if (earlier.merged || (earlier.card & 3) !== (later.card & 3)) continue;
|
|
491
|
-
const rankI = earlier.card >> 2;
|
|
492
|
-
if (boardRanks.has(rankI) || rankJ - rankI > merge.maxGap || !merge.ranks(rankI, rankJ)) continue;
|
|
493
|
-
if (connectivity(boardRanks, rankI) !== connectivity(boardRanks, rankJ)) continue;
|
|
494
|
-
const swap = maps.add(rankSwap(rankI, rankJ));
|
|
495
|
-
for (let m = 0; m < later.maps.length; m++) {
|
|
496
|
-
earlier.maps.push(maps.then(later.maps[m], swap));
|
|
497
|
-
earlier.cards.push(later.cards[m]);
|
|
498
|
-
}
|
|
499
|
-
later.merged = true;
|
|
500
|
-
approximate = true;
|
|
501
|
-
break;
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
+
const children = dealChildren(maps, previousCards, base, previousWidth, stabilisers[p], merge);
|
|
506
|
+
for (const child of children) if (child.merged) approximate = true;
|
|
505
507
|
for (const child of children) {
|
|
506
508
|
if (child.merged) continue;
|
|
507
509
|
for (let i = 0; i < previousWidth; i++) level.push(previousCards[base + i]);
|
|
@@ -544,6 +546,53 @@ function enumerateRunouts(board, symmetries, merge) {
|
|
|
544
546
|
initial: [...board]
|
|
545
547
|
};
|
|
546
548
|
}
|
|
549
|
+
var usedScratch = new Uint8Array(DECK);
|
|
550
|
+
var seenScratch = new Uint8Array(DECK);
|
|
551
|
+
function dealChildren(maps, cards, base, width, stabiliser, merge) {
|
|
552
|
+
const used = usedScratch;
|
|
553
|
+
const seen = seenScratch;
|
|
554
|
+
used.fill(0);
|
|
555
|
+
seen.fill(0);
|
|
556
|
+
for (let i = 0; i < width; i++) used[cards[base + i]] = 1;
|
|
557
|
+
const children = [];
|
|
558
|
+
for (let card = 0; card < DECK; card++) {
|
|
559
|
+
if (used[card] || seen[card]) continue;
|
|
560
|
+
const child = { card, maps: [], cards: [], fixing: [], merged: false };
|
|
561
|
+
for (const sigma of stabiliser) {
|
|
562
|
+
const image = maps.card[sigma][card];
|
|
563
|
+
if (image === card) child.fixing.push(sigma);
|
|
564
|
+
if (seen[image]) continue;
|
|
565
|
+
seen[image] = 1;
|
|
566
|
+
child.maps.push(image === card ? 0 : maps.inverse(sigma));
|
|
567
|
+
child.cards.push(image);
|
|
568
|
+
}
|
|
569
|
+
children.push(child);
|
|
570
|
+
}
|
|
571
|
+
if (merge) {
|
|
572
|
+
const boardRanks = /* @__PURE__ */ new Set();
|
|
573
|
+
for (let i = 0; i < width; i++) boardRanks.add(cards[base + i] >> 2);
|
|
574
|
+
for (let j = 0; j < children.length; j++) {
|
|
575
|
+
const later = children[j];
|
|
576
|
+
const rankJ = later.card >> 2;
|
|
577
|
+
if (boardRanks.has(rankJ)) continue;
|
|
578
|
+
for (let i = 0; i < j; i++) {
|
|
579
|
+
const earlier = children[i];
|
|
580
|
+
if (earlier.merged || (earlier.card & 3) !== (later.card & 3)) continue;
|
|
581
|
+
const rankI = earlier.card >> 2;
|
|
582
|
+
if (boardRanks.has(rankI) || rankJ - rankI > merge.maxGap || !merge.ranks(rankI, rankJ)) continue;
|
|
583
|
+
if (connectivity(boardRanks, rankI) !== connectivity(boardRanks, rankJ)) continue;
|
|
584
|
+
const swap = maps.add(rankSwap(rankI, rankJ));
|
|
585
|
+
for (let m = 0; m < later.maps.length; m++) {
|
|
586
|
+
earlier.maps.push(maps.then(later.maps[m], swap));
|
|
587
|
+
earlier.cards.push(later.cards[m]);
|
|
588
|
+
}
|
|
589
|
+
later.merged = true;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return children;
|
|
595
|
+
}
|
|
547
596
|
function rankSwap(a, b) {
|
|
548
597
|
const out = new Uint8Array(DECK);
|
|
549
598
|
for (let card = 0; card < DECK; card++) {
|
|
@@ -586,11 +635,13 @@ function resolveBoard(runouts, board) {
|
|
|
586
635
|
const round = i - start + 1;
|
|
587
636
|
const dealt = runouts.maps.card[map][board[i]];
|
|
588
637
|
let found = -1;
|
|
589
|
-
|
|
638
|
+
const children = runouts.childStart[round];
|
|
639
|
+
const relabel = runouts.orbitMap[round];
|
|
640
|
+
for (let child = children[index]; child < children[index + 1] && found < 0; child++) {
|
|
590
641
|
for (let m = runouts.orbitStart[round][child]; m < runouts.orbitStart[round][child + 1]; m++) {
|
|
591
642
|
if (runouts.orbitCard[round][m] !== dealt) continue;
|
|
592
643
|
found = child;
|
|
593
|
-
map = runouts.maps.then(map,
|
|
644
|
+
map = runouts.maps.then(map, relabel[m]);
|
|
594
645
|
break;
|
|
595
646
|
}
|
|
596
647
|
}
|
|
@@ -973,6 +1024,13 @@ function buildShowdownTable(runouts, relevant) {
|
|
|
973
1024
|
}
|
|
974
1025
|
|
|
975
1026
|
// src/hands.ts
|
|
1027
|
+
function markMembers(lists, from, count, indexOf, clear) {
|
|
1028
|
+
for (let i = 0; i < count; i++) {
|
|
1029
|
+
for (let m = lists.memberStart[from + i]; m < lists.memberStart[from + i + 1]; m++) {
|
|
1030
|
+
indexOf[lists.memberCombo[m]] = clear ? -1 : i;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
976
1034
|
function span(lists, round, board, player) {
|
|
977
1035
|
const slot = (lists.roundBase[round] + board) * 2 + player;
|
|
978
1036
|
return [lists.start[slot], lists.count[slot]];
|
|
@@ -989,7 +1047,7 @@ function buildHandSpace(runouts, players, lists) {
|
|
|
989
1047
|
const parent = runouts.parent[round][child];
|
|
990
1048
|
for (let player = 0; player < 2; player++) {
|
|
991
1049
|
const [parentFrom, parentCount] = span(lists, round - 1, parent, player);
|
|
992
|
-
|
|
1050
|
+
markMembers(lists, parentFrom, parentCount, indexOf, false);
|
|
993
1051
|
const [childFrom, childCount] = span(lists, round, child, player);
|
|
994
1052
|
for (let m = orbitStart[child]; m < orbitStart[child + 1]; m++) {
|
|
995
1053
|
const back = runouts.maps.combo[runouts.maps.inverse(runouts.orbitMap[round][m])];
|
|
@@ -1001,7 +1059,7 @@ function buildHandSpace(runouts, players, lists) {
|
|
|
1001
1059
|
up.push(at);
|
|
1002
1060
|
}
|
|
1003
1061
|
}
|
|
1004
|
-
|
|
1062
|
+
markMembers(lists, parentFrom, parentCount, indexOf, true);
|
|
1005
1063
|
}
|
|
1006
1064
|
}
|
|
1007
1065
|
upStart.push(starts);
|
|
@@ -1014,11 +1072,11 @@ function buildHandSpace(runouts, players, lists) {
|
|
|
1014
1072
|
for (let player = 0; player < 2; player++) {
|
|
1015
1073
|
const slot = (lists.roundBase[round] + board) * 2 + player;
|
|
1016
1074
|
const [otherFrom, otherCount] = span(lists, round, board, 1 - player);
|
|
1017
|
-
|
|
1075
|
+
markMembers(lists, otherFrom, otherCount, indexOf, false);
|
|
1018
1076
|
const [from, count] = span(lists, round, board, player);
|
|
1019
1077
|
sameStart[slot] = same.length;
|
|
1020
1078
|
for (let i = 0; i < count; i++) same.push(indexOf[lists.list[from + i]]);
|
|
1021
|
-
|
|
1079
|
+
markMembers(lists, otherFrom, otherCount, indexOf, true);
|
|
1022
1080
|
}
|
|
1023
1081
|
}
|
|
1024
1082
|
}
|
|
@@ -1070,6 +1128,48 @@ function showdownValuesHands(oppReach, cardA, cardB, strength, oppFrom, oppCount
|
|
|
1070
1128
|
}
|
|
1071
1129
|
sums.fill(0, 0, DECK);
|
|
1072
1130
|
}
|
|
1131
|
+
function addMembers(reach, entry, memberStart, memberA, memberB, sums) {
|
|
1132
|
+
const to = memberStart[entry + 1];
|
|
1133
|
+
for (let m = memberStart[entry]; m < to; m++) {
|
|
1134
|
+
sums[memberA[m]] += reach;
|
|
1135
|
+
sums[memberB[m]] += reach;
|
|
1136
|
+
}
|
|
1137
|
+
return reach * (to - memberStart[entry]);
|
|
1138
|
+
}
|
|
1139
|
+
function foldValuesPacked(oppReach, cardA, cardB, memberStart, memberA, memberB, oppFrom, oppCount, myFrom, myCount, same, sameFrom, payoff, sums, out) {
|
|
1140
|
+
let total = 0;
|
|
1141
|
+
for (let j = 0; j < oppCount; j++) total += addMembers(oppReach[j], oppFrom + j, memberStart, memberA, memberB, sums);
|
|
1142
|
+
for (let i = 0; i < myCount; i++) {
|
|
1143
|
+
const twin = same[sameFrom + i];
|
|
1144
|
+
const both = twin >= 0 ? oppReach[twin] : 0;
|
|
1145
|
+
out[i] = payoff * (total - (sums[cardA[myFrom + i]] + sums[cardB[myFrom + i]] - both));
|
|
1146
|
+
}
|
|
1147
|
+
sums.fill(0, 0, DECK);
|
|
1148
|
+
}
|
|
1149
|
+
function showdownValuesPacked(oppReach, cardA, cardB, memberStart, memberA, memberB, strength, oppFrom, oppCount, myFrom, myCount, payoff, sums, out) {
|
|
1150
|
+
let running = 0;
|
|
1151
|
+
let j = 0;
|
|
1152
|
+
for (let i = 0; i < myCount; i++) {
|
|
1153
|
+
const level = strength[myFrom + i];
|
|
1154
|
+
while (j < oppCount && strength[oppFrom + j] < level) {
|
|
1155
|
+
running += addMembers(oppReach[j], oppFrom + j, memberStart, memberA, memberB, sums);
|
|
1156
|
+
j++;
|
|
1157
|
+
}
|
|
1158
|
+
out[i] = running - (sums[cardA[myFrom + i]] + sums[cardB[myFrom + i]]);
|
|
1159
|
+
}
|
|
1160
|
+
sums.fill(0, 0, DECK);
|
|
1161
|
+
running = 0;
|
|
1162
|
+
j = oppCount - 1;
|
|
1163
|
+
for (let i = myCount - 1; i >= 0; i--) {
|
|
1164
|
+
const level = strength[myFrom + i];
|
|
1165
|
+
while (j >= 0 && strength[oppFrom + j] > level) {
|
|
1166
|
+
running += addMembers(oppReach[j], oppFrom + j, memberStart, memberA, memberB, sums);
|
|
1167
|
+
j--;
|
|
1168
|
+
}
|
|
1169
|
+
out[i] = payoff * (out[i] - (running - (sums[cardA[myFrom + i]] + sums[cardB[myFrom + i]])));
|
|
1170
|
+
}
|
|
1171
|
+
sums.fill(0, 0, DECK);
|
|
1172
|
+
}
|
|
1073
1173
|
|
|
1074
1174
|
// src/pairs.ts
|
|
1075
1175
|
var CHUNK = 64;
|
|
@@ -1101,7 +1201,8 @@ function makeBuffers(ctx, width) {
|
|
|
1101
1201
|
hands: make(() => new Uint16Array(width)),
|
|
1102
1202
|
strategy: make(() => new Float64Array(actions * COMBOS)),
|
|
1103
1203
|
rank: new Int32Array(COMBOS),
|
|
1104
|
-
position: new Int32Array(COMBOS)
|
|
1204
|
+
position: new Int32Array(COMBOS).fill(-1),
|
|
1205
|
+
at: make(() => new Int32Array(width))
|
|
1105
1206
|
};
|
|
1106
1207
|
}
|
|
1107
1208
|
function pairValues(ctx, node, board, traverser, hands, handReach, live, liveCount) {
|
|
@@ -1198,20 +1299,23 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1198
1299
|
const childLive = buffers.live[depth];
|
|
1199
1300
|
const childCols2 = buffers.cols[depth + 1];
|
|
1200
1301
|
const mappedHands = buffers.hands[depth];
|
|
1201
|
-
|
|
1302
|
+
const starts = runouts.childStart[nextRound];
|
|
1303
|
+
for (let childBoard = starts[board]; childBoard < starts[board + 1]; childBoard++) {
|
|
1304
|
+
const stored = dealtCards[childBoard * width + width - 1];
|
|
1202
1305
|
for (let m = orbitStart[childBoard]; m < orbitStart[childBoard + 1]; m++) {
|
|
1203
|
-
const dealt = m === orbitStart[childBoard] ?
|
|
1306
|
+
const dealt = m === orbitStart[childBoard] ? stored : runouts.orbitCard[nextRound][m];
|
|
1204
1307
|
const map = runouts.maps.combo[runouts.orbitMap[nextRound][m]];
|
|
1308
|
+
const blocked = (combo) => cards[combo * 2] === dealt || cards[combo * 2 + 1] === dealt;
|
|
1205
1309
|
let childCount = 0;
|
|
1206
1310
|
for (let i = 0; i < liveCount; i++) {
|
|
1207
1311
|
const combo = live[i];
|
|
1208
|
-
if (
|
|
1312
|
+
if (blocked(combo)) continue;
|
|
1209
1313
|
childLive[childCount++] = map[combo];
|
|
1210
1314
|
}
|
|
1211
1315
|
let childColCount = 0;
|
|
1212
1316
|
for (let c = 0; c < colCount; c++) {
|
|
1213
1317
|
const k = cols[c];
|
|
1214
|
-
if (
|
|
1318
|
+
if (!blocked(hands[k])) childCols2[childColCount++] = k;
|
|
1215
1319
|
}
|
|
1216
1320
|
if (childColCount === 0) continue;
|
|
1217
1321
|
for (let k = 0; k < hands.length; k++) mappedHands[k] = map[hands[k]];
|
|
@@ -1233,7 +1337,7 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1233
1337
|
const row = childCols2[c] * COMBOS;
|
|
1234
1338
|
for (let i = 0; i < liveCount; i++) {
|
|
1235
1339
|
const combo = live[i];
|
|
1236
|
-
if (
|
|
1340
|
+
if (blocked(combo)) continue;
|
|
1237
1341
|
out[row + combo] += value[row + map[combo]];
|
|
1238
1342
|
}
|
|
1239
1343
|
}
|
|
@@ -1246,16 +1350,14 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1246
1350
|
const owner = tree.player[node];
|
|
1247
1351
|
const lists = ctx.active;
|
|
1248
1352
|
const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
|
|
1249
|
-
const active = lists.list;
|
|
1250
1353
|
const activeFrom = lists.start[listSlot];
|
|
1251
1354
|
const activeCount = lists.count[listSlot];
|
|
1252
1355
|
const stride = activeCount;
|
|
1253
1356
|
const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1254
1357
|
const strategy = buffers.strategy[depth];
|
|
1255
|
-
|
|
1358
|
+
storedStrategy(ctx, node, slotBase, actions, stride, strategy);
|
|
1256
1359
|
if (owner === traverser) {
|
|
1257
1360
|
for (let a = 0; a < actions; a++) {
|
|
1258
|
-
const offset = a * COMBOS;
|
|
1259
1361
|
const value = walk(
|
|
1260
1362
|
ctx,
|
|
1261
1363
|
buffers,
|
|
@@ -1273,8 +1375,11 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1273
1375
|
for (let c = 0; c < colCount; c++) {
|
|
1274
1376
|
const row = cols[c] * COMBOS;
|
|
1275
1377
|
for (let i = 0; i < activeCount; i++) {
|
|
1276
|
-
const
|
|
1277
|
-
|
|
1378
|
+
const share = strategy[a * stride + i];
|
|
1379
|
+
for (let m = lists.memberStart[activeFrom + i]; m < lists.memberStart[activeFrom + i + 1]; m++) {
|
|
1380
|
+
const combo = lists.memberCombo[m];
|
|
1381
|
+
out[row + combo] += share * value[row + combo];
|
|
1382
|
+
}
|
|
1278
1383
|
}
|
|
1279
1384
|
}
|
|
1280
1385
|
}
|
|
@@ -1283,13 +1388,15 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1283
1388
|
const childReach = buffers.colReach[depth + 1];
|
|
1284
1389
|
const childCols = buffers.cols[depth + 1];
|
|
1285
1390
|
const position = buffers.position;
|
|
1286
|
-
|
|
1391
|
+
markMembers(lists, activeFrom, activeCount, position, false);
|
|
1392
|
+
const at = buffers.at[depth];
|
|
1393
|
+
for (let c = 0; c < colCount; c++) at[cols[c]] = position[hands[cols[c]]];
|
|
1394
|
+
markMembers(lists, activeFrom, activeCount, position, true);
|
|
1287
1395
|
for (let a = 0; a < actions; a++) {
|
|
1288
|
-
const offset = a * COMBOS;
|
|
1289
1396
|
let childCount = 0;
|
|
1290
1397
|
for (let c = 0; c < colCount; c++) {
|
|
1291
1398
|
const k = cols[c];
|
|
1292
|
-
const r = colReach[k] * strategy[a * stride +
|
|
1399
|
+
const r = at[k] < 0 ? 0 : colReach[k] * strategy[a * stride + at[k]];
|
|
1293
1400
|
childReach[k] = r;
|
|
1294
1401
|
if (r !== 0) childCols[childCount++] = k;
|
|
1295
1402
|
}
|
|
@@ -1381,58 +1488,243 @@ function buildPlayerIndex(weights) {
|
|
|
1381
1488
|
}
|
|
1382
1489
|
return { combos: Uint16Array.from(combos), count: combos.length, slot, weights };
|
|
1383
1490
|
}
|
|
1384
|
-
function
|
|
1491
|
+
function orbitLeader(runouts, round, board, combo) {
|
|
1492
|
+
const from = runouts.stabiliserStart[round][board];
|
|
1493
|
+
const to = runouts.stabiliserStart[round][board + 1];
|
|
1494
|
+
for (let k = from + 1; k < to; k++) {
|
|
1495
|
+
if (runouts.maps.combo[runouts.stabiliserMap[round][k]][combo] < combo) return false;
|
|
1496
|
+
}
|
|
1497
|
+
return true;
|
|
1498
|
+
}
|
|
1499
|
+
var INT32_MAX = 2 ** 31 - 1;
|
|
1500
|
+
function buildLayout(tree, runouts, players, bytesPerElement, alias, compact) {
|
|
1385
1501
|
const roundBase = new Int32Array(runouts.rounds + 1);
|
|
1386
1502
|
for (let round = 0; round < runouts.rounds; round++) roundBase[round + 1] = roundBase[round] + runouts.counts[round];
|
|
1387
1503
|
const rowStart = new Int32Array(roundBase[runouts.rounds] * 2);
|
|
1388
1504
|
const totals = new Int32Array(runouts.rounds * 2);
|
|
1389
1505
|
const onBoard = new Uint8Array(DECK);
|
|
1390
1506
|
for (let round = 0; round < runouts.rounds; round++) {
|
|
1391
|
-
const
|
|
1507
|
+
const width2 = runouts.width[round];
|
|
1392
1508
|
const cards = runouts.cards[round];
|
|
1393
1509
|
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1394
1510
|
onBoard.fill(0);
|
|
1395
|
-
for (let i = 0; i <
|
|
1511
|
+
for (let i = 0; i < width2; i++) onBoard[cards[board * width2 + i]] = 1;
|
|
1396
1512
|
for (let player = 0; player < 2; player++) {
|
|
1397
1513
|
const index = players[player];
|
|
1398
1514
|
let live = 0;
|
|
1399
1515
|
for (let i = 0; i < index.count; i++) {
|
|
1400
1516
|
const combo = index.combos[i];
|
|
1401
|
-
if (
|
|
1517
|
+
if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
|
|
1518
|
+
if (!orbitLeader(runouts, round, board, combo)) continue;
|
|
1519
|
+
live++;
|
|
1402
1520
|
}
|
|
1403
1521
|
rowStart[(roundBase[round] + board) * 2 + player] = totals[round * 2 + player];
|
|
1404
1522
|
totals[round * 2 + player] += live;
|
|
1405
1523
|
}
|
|
1406
1524
|
}
|
|
1407
1525
|
}
|
|
1408
|
-
const offset = new
|
|
1526
|
+
const offset = new Float64Array(tree.nodeCount).fill(-1);
|
|
1409
1527
|
const blockOffset = new Int32Array(tree.nodeCount).fill(-1);
|
|
1528
|
+
const compactFrom = compact ? compact.from : runouts.rounds;
|
|
1529
|
+
const handOffset = compact ? new Float64Array(tree.nodeCount).fill(-1) : null;
|
|
1530
|
+
const compactChunk = compact ? new Int32Array(tree.nodeCount).fill(-1) : null;
|
|
1531
|
+
const chunkSlots = [];
|
|
1532
|
+
const chunkHands = [];
|
|
1533
|
+
const chunkStart = [];
|
|
1534
|
+
const width = compact?.float ? 4 : 1;
|
|
1410
1535
|
let total = 0;
|
|
1536
|
+
let compactSlots = 0;
|
|
1537
|
+
let compactHands = 0;
|
|
1411
1538
|
let blocks = 0;
|
|
1412
1539
|
let maxActions = 0;
|
|
1540
|
+
let shared = false;
|
|
1541
|
+
let walked = 0;
|
|
1413
1542
|
for (let i = 0; i < tree.decisionNodes.length; i++) {
|
|
1414
1543
|
const node = tree.decisionNodes[i];
|
|
1415
1544
|
const actions = tree.numEdges[node];
|
|
1416
1545
|
if (actions > maxActions) maxActions = actions;
|
|
1417
|
-
|
|
1546
|
+
const hands = totals[tree.round[node] * 2 + tree.player[node]];
|
|
1547
|
+
walked += actions * hands;
|
|
1548
|
+
if (alias !== void 0 && alias[node] !== node) {
|
|
1549
|
+
const owner = alias[node];
|
|
1550
|
+
if (owner > node) throw new Error("buildLayout: a storage owner must come before the nodes it owns");
|
|
1551
|
+
offset[node] = offset[owner];
|
|
1552
|
+
blockOffset[node] = blockOffset[owner];
|
|
1553
|
+
if (compact) {
|
|
1554
|
+
handOffset[node] = handOffset[owner];
|
|
1555
|
+
compactChunk[node] = compactChunk[owner];
|
|
1556
|
+
}
|
|
1557
|
+
shared = true;
|
|
1558
|
+
continue;
|
|
1559
|
+
}
|
|
1418
1560
|
blockOffset[node] = blocks;
|
|
1419
|
-
total += actions * totals[tree.round[node] * 2 + tree.player[node]];
|
|
1420
1561
|
blocks += runouts.counts[tree.round[node]];
|
|
1562
|
+
if (tree.round[node] >= compactFrom) {
|
|
1563
|
+
const slots = actions * hands;
|
|
1564
|
+
let chunk = chunkSlots.length - 1;
|
|
1565
|
+
if (slots * width > compact.chunkBytes) {
|
|
1566
|
+
throw new Error(
|
|
1567
|
+
`compactStorage.chunkBytes is ${compact.chunkBytes}, but one decision needs ${slots * width} bytes`
|
|
1568
|
+
);
|
|
1569
|
+
}
|
|
1570
|
+
if (chunk < 0 || (chunkSlots[chunk] + slots) * width > compact.chunkBytes || chunkHands[chunk] + hands > compact.chunkBytes) {
|
|
1571
|
+
chunk++;
|
|
1572
|
+
chunkSlots.push(0);
|
|
1573
|
+
chunkHands.push(0);
|
|
1574
|
+
chunkStart.push(compactSlots);
|
|
1575
|
+
}
|
|
1576
|
+
compactChunk[node] = chunk;
|
|
1577
|
+
offset[node] = chunkSlots[chunk];
|
|
1578
|
+
handOffset[node] = chunkHands[chunk];
|
|
1579
|
+
chunkSlots[chunk] += slots;
|
|
1580
|
+
chunkHands[chunk] += hands;
|
|
1581
|
+
compactSlots += slots;
|
|
1582
|
+
compactHands += hands;
|
|
1583
|
+
} else {
|
|
1584
|
+
offset[node] = total;
|
|
1585
|
+
total += actions * hands;
|
|
1586
|
+
}
|
|
1421
1587
|
}
|
|
1422
1588
|
const scales = bytesPerElement === 2 ? blocks * 8 * 2 : 0;
|
|
1589
|
+
const compactBytes = !compact ? 0 : compact.float ? compactSlots * 4 : (compactSlots + (compact.perHand ? compactHands : blocks * 8)) * (compact.sums ? 2 : 1);
|
|
1423
1590
|
return {
|
|
1424
|
-
offset,
|
|
1591
|
+
offset: Math.max(total, ...chunkSlots) <= INT32_MAX ? Int32Array.from(offset) : offset,
|
|
1425
1592
|
blockOffset,
|
|
1426
1593
|
blocks,
|
|
1427
1594
|
totalSlots: total,
|
|
1595
|
+
walkedSlots: walked,
|
|
1428
1596
|
maxActions,
|
|
1429
1597
|
rowStart,
|
|
1430
1598
|
roundBase,
|
|
1431
|
-
bytes: total * bytesPerElement * 2 + scales
|
|
1599
|
+
bytes: total * bytesPerElement * 2 + scales + compactBytes,
|
|
1600
|
+
shared,
|
|
1601
|
+
reachWeighted: shared ? sharedNodes(tree, alias) : null,
|
|
1602
|
+
compactFrom,
|
|
1603
|
+
compactSlots,
|
|
1604
|
+
handOffset,
|
|
1605
|
+
compactHands,
|
|
1606
|
+
compactChunk,
|
|
1607
|
+
chunkSlots,
|
|
1608
|
+
chunkHands,
|
|
1609
|
+
chunkStart
|
|
1432
1610
|
};
|
|
1433
1611
|
}
|
|
1612
|
+
function sharedNodes(tree, alias) {
|
|
1613
|
+
const marks = new Uint8Array(tree.nodeCount);
|
|
1614
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
1615
|
+
if (alias[node] !== node) marks[node] = marks[alias[node]] = 1;
|
|
1616
|
+
}
|
|
1617
|
+
return marks;
|
|
1618
|
+
}
|
|
1434
1619
|
var REGRET_STEPS = 32767;
|
|
1435
1620
|
var SUM_STEPS = 65535;
|
|
1621
|
+
function sampledRounds(options, rounds) {
|
|
1622
|
+
const asked = options.rounds ?? Array.from({ length: rounds }, (_, r) => r);
|
|
1623
|
+
return asked.filter((r) => r > 0 && r < rounds);
|
|
1624
|
+
}
|
|
1625
|
+
function planSampling(tree, runouts, active, layout, options) {
|
|
1626
|
+
const rounds = new Uint8Array(runouts.rounds);
|
|
1627
|
+
for (const r of sampledRounds(options, runouts.rounds)) rounds[r] = 1;
|
|
1628
|
+
const boardOffset = new Int32Array(active.roundBase[runouts.rounds] * 2);
|
|
1629
|
+
const perNode = new Float64Array(runouts.rounds);
|
|
1630
|
+
for (let r = 1; r < runouts.rounds; r++) {
|
|
1631
|
+
if (!rounds[r]) continue;
|
|
1632
|
+
let total = 0;
|
|
1633
|
+
for (let b = 0; b < runouts.counts[r]; b++) {
|
|
1634
|
+
for (let p2 = 0; p2 < 2; p2++) {
|
|
1635
|
+
const slot = (active.roundBase[r] + b) * 2 + p2;
|
|
1636
|
+
boardOffset[slot] = total;
|
|
1637
|
+
total += active.count[slot];
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
perNode[r] = total;
|
|
1641
|
+
}
|
|
1642
|
+
const baseOffset = new Int32Array(tree.nodeCount).fill(-1);
|
|
1643
|
+
const walkOffset = new Int32Array(tree.nodeCount).fill(-1);
|
|
1644
|
+
let values = 0;
|
|
1645
|
+
let walks = 0;
|
|
1646
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
1647
|
+
if (tree.kind[node] !== NodeKind.Chance) continue;
|
|
1648
|
+
const next = tree.round[node] + 1;
|
|
1649
|
+
if (next >= runouts.rounds || !rounds[next]) continue;
|
|
1650
|
+
baseOffset[node] = values;
|
|
1651
|
+
walkOffset[node] = walks;
|
|
1652
|
+
values += perNode[next];
|
|
1653
|
+
walks += runouts.counts[next] * 2;
|
|
1654
|
+
}
|
|
1655
|
+
if (values > 1073741823) throw new Error("chanceSampling: too many baseline values for this spot");
|
|
1656
|
+
const every = options.every ?? 0;
|
|
1657
|
+
const p = every > 0 ? 1 / every : options.p ?? 0.35;
|
|
1658
|
+
if (!(p > 0 && p <= 1)) throw new Error(`chanceSampling: p must be in (0, 1], got ${p}`);
|
|
1659
|
+
return {
|
|
1660
|
+
rounds,
|
|
1661
|
+
p,
|
|
1662
|
+
every,
|
|
1663
|
+
unbiased: (options.estimator ?? "unbiased") === "unbiased",
|
|
1664
|
+
catchUp: options.catchUp ?? true,
|
|
1665
|
+
baseOffset,
|
|
1666
|
+
boardOffset,
|
|
1667
|
+
walkOffset,
|
|
1668
|
+
values,
|
|
1669
|
+
walks,
|
|
1670
|
+
blocks: layout.blocks
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
function samplingBytes(plan) {
|
|
1674
|
+
return (plan.values + plan.walks + plan.blocks) * 4;
|
|
1675
|
+
}
|
|
1676
|
+
function makeSampling(plan, storage, allocate = (bytes) => new ArrayBuffer(bytes)) {
|
|
1677
|
+
return {
|
|
1678
|
+
...plan,
|
|
1679
|
+
baseline: storage?.baseline ?? new Float32Array(allocate(plan.values * 4)),
|
|
1680
|
+
lastWalk: storage?.lastWalk ?? new Int32Array(allocate(plan.walks * 4)),
|
|
1681
|
+
lastUpdate: storage?.lastUpdate ?? new Int32Array(allocate(plan.blocks * 4)),
|
|
1682
|
+
bytes: samplingBytes(plan)
|
|
1683
|
+
};
|
|
1684
|
+
}
|
|
1685
|
+
function sampled(s, node, board, t) {
|
|
1686
|
+
let h = Math.imul(node ^ Math.imul(board, 2654435761), 2246822507);
|
|
1687
|
+
if (s.every > 0) {
|
|
1688
|
+
h ^= h >>> 13;
|
|
1689
|
+
h = Math.imul(h, 3266489909);
|
|
1690
|
+
h ^= h >>> 16;
|
|
1691
|
+
return ((h >>> 0) + t) % s.every === 0;
|
|
1692
|
+
}
|
|
1693
|
+
h ^= Math.imul(t, 668265263);
|
|
1694
|
+
h ^= h >>> 15;
|
|
1695
|
+
h = Math.imul(h, 3266489909);
|
|
1696
|
+
h ^= h >>> 13;
|
|
1697
|
+
h = Math.imul(h, 374761393);
|
|
1698
|
+
h ^= h >>> 16;
|
|
1699
|
+
return (h >>> 0) / 4294967296 < s.p;
|
|
1700
|
+
}
|
|
1701
|
+
var factorCache = [];
|
|
1702
|
+
function cachedFactors(t) {
|
|
1703
|
+
let f = factorCache[t];
|
|
1704
|
+
if (f === void 0) factorCache[t] = f = dcfrFactors(t);
|
|
1705
|
+
return f;
|
|
1706
|
+
}
|
|
1707
|
+
function blockFactors(ctx, block, iterWeight) {
|
|
1708
|
+
let { keepPositive, keepNegative, keepSum } = cachedFactors(iterWeight);
|
|
1709
|
+
const discounted = ctx.discounted;
|
|
1710
|
+
if (discounted !== void 0 && discounted !== null) {
|
|
1711
|
+
if (discounted[block] === iterWeight) keepPositive = keepNegative = keepSum = 1;
|
|
1712
|
+
discounted[block] = iterWeight;
|
|
1713
|
+
}
|
|
1714
|
+
const sampling = ctx.sampling;
|
|
1715
|
+
if (sampling !== void 0 && sampling !== null) {
|
|
1716
|
+
if (sampling.catchUp) {
|
|
1717
|
+
for (let s = sampling.lastUpdate[block] + 1; s < iterWeight; s++) {
|
|
1718
|
+
const f = cachedFactors(s);
|
|
1719
|
+
keepPositive *= f.keepPositive;
|
|
1720
|
+
keepNegative *= f.keepNegative;
|
|
1721
|
+
keepSum *= f.keepSum;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
sampling.lastUpdate[block] = iterWeight;
|
|
1725
|
+
}
|
|
1726
|
+
return { keepPositive, keepNegative, keepSum };
|
|
1727
|
+
}
|
|
1436
1728
|
function runoutWeights(runouts) {
|
|
1437
1729
|
const weights = new Float64Array(runouts.rounds).fill(1);
|
|
1438
1730
|
for (let round = 0; round < runouts.rounds; round++) {
|
|
@@ -1471,15 +1763,22 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1471
1763
|
const slots = roundBase[runouts.rounds] * 2;
|
|
1472
1764
|
const start = new Int32Array(slots);
|
|
1473
1765
|
const count = new Uint16Array(slots);
|
|
1766
|
+
const packedSlot = new Uint8Array(slots);
|
|
1474
1767
|
const entries = [];
|
|
1475
1768
|
const entrySlots = [];
|
|
1769
|
+
const memberStart = [];
|
|
1770
|
+
const members = [];
|
|
1476
1771
|
const onBoard = new Uint8Array(DECK);
|
|
1772
|
+
const seen = new Uint8Array(COMBOS);
|
|
1477
1773
|
for (let round = 0; round < runouts.rounds; round++) {
|
|
1478
1774
|
const width = runouts.width[round];
|
|
1479
1775
|
const cards = runouts.cards[round];
|
|
1776
|
+
const stabStart = runouts.stabiliserStart[round];
|
|
1777
|
+
const stabMap = runouts.stabiliserMap[round];
|
|
1480
1778
|
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1481
1779
|
onBoard.fill(0);
|
|
1482
1780
|
for (let i = 0; i < width; i++) onBoard[cards[board * width + i]] = 1;
|
|
1781
|
+
const symmetric = stabStart[board + 1] - stabStart[board] > 1;
|
|
1483
1782
|
for (let player = 0; player < 2; player++) {
|
|
1484
1783
|
const index = players[player];
|
|
1485
1784
|
const slot = (roundBase[round] + board) * 2 + player;
|
|
@@ -1488,6 +1787,7 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1488
1787
|
for (let i = 0; i < index.count; i++) {
|
|
1489
1788
|
const combo = index.combos[i];
|
|
1490
1789
|
if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
|
|
1790
|
+
if (symmetric && !orbitLeader(runouts, round, board, combo)) continue;
|
|
1491
1791
|
here.push(i);
|
|
1492
1792
|
}
|
|
1493
1793
|
if (showdown !== null && round === runouts.rounds - 1) {
|
|
@@ -1495,47 +1795,42 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1495
1795
|
here.sort((x, y) => group[index.combos[x]] - group[index.combos[y]] || x - y);
|
|
1496
1796
|
}
|
|
1497
1797
|
for (const i of here) {
|
|
1498
|
-
|
|
1798
|
+
const combo = index.combos[i];
|
|
1799
|
+
entries.push(combo);
|
|
1499
1800
|
entrySlots.push(i);
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
for (let round = 0; round < runouts.rounds; round++) {
|
|
1508
|
-
const stabStart = runouts.stabiliserStart[round];
|
|
1509
|
-
const stabMap = runouts.stabiliserMap[round];
|
|
1510
|
-
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1511
|
-
for (let player = 0; player < 2; player++) {
|
|
1512
|
-
const slot = (roundBase[round] + board) * 2 + player;
|
|
1513
|
-
tieStart[slot] = ties.length;
|
|
1514
|
-
if (stabStart[board + 1] - stabStart[board] < 2) continue;
|
|
1515
|
-
const position = /* @__PURE__ */ new Map();
|
|
1516
|
-
for (let e = start[slot]; e < start[slot] + count[slot]; e++) position.set(entries[e], e - start[slot]);
|
|
1517
|
-
const seen = new Uint8Array(count[slot]);
|
|
1518
|
-
for (let e = start[slot]; e < start[slot] + count[slot]; e++) {
|
|
1519
|
-
if (seen[e - start[slot]]) continue;
|
|
1520
|
-
const members = [];
|
|
1521
|
-
for (let k = stabStart[board]; k < stabStart[board + 1]; k++) {
|
|
1522
|
-
const image = position.get(runouts.maps.combo[stabMap[k]][entries[e]]);
|
|
1523
|
-
if (image === void 0 || seen[image]) continue;
|
|
1801
|
+
memberStart.push(members.length);
|
|
1802
|
+
members.push(combo);
|
|
1803
|
+
if (!symmetric) continue;
|
|
1804
|
+
seen[combo] = 1;
|
|
1805
|
+
for (let k = stabStart[board] + 1; k < stabStart[board + 1]; k++) {
|
|
1806
|
+
const image = runouts.maps.combo[stabMap[k]][combo];
|
|
1807
|
+
if (seen[image]) continue;
|
|
1524
1808
|
seen[image] = 1;
|
|
1525
1809
|
members.push(image);
|
|
1810
|
+
packedSlot[slot] = 1;
|
|
1526
1811
|
}
|
|
1527
|
-
|
|
1812
|
+
for (let m = memberStart[memberStart.length - 1]; m < members.length; m++) seen[members[m]] = 0;
|
|
1528
1813
|
}
|
|
1814
|
+
count[slot] = entries.length - start[slot];
|
|
1529
1815
|
}
|
|
1530
1816
|
}
|
|
1531
1817
|
}
|
|
1532
|
-
|
|
1818
|
+
memberStart.push(members.length);
|
|
1533
1819
|
const list = Uint16Array.from(entries);
|
|
1534
1820
|
const cardA = new Uint8Array(list.length);
|
|
1535
1821
|
const cardB = new Uint8Array(list.length);
|
|
1822
|
+
const multiplicity = new Float64Array(list.length);
|
|
1536
1823
|
for (let e = 0; e < list.length; e++) {
|
|
1537
1824
|
cardA[e] = COMBO_CARDS[list[e] * 2];
|
|
1538
1825
|
cardB[e] = COMBO_CARDS[list[e] * 2 + 1];
|
|
1826
|
+
multiplicity[e] = memberStart[e + 1] - memberStart[e];
|
|
1827
|
+
}
|
|
1828
|
+
const memberCombo = Uint16Array.from(members);
|
|
1829
|
+
const memberA = new Uint8Array(members.length);
|
|
1830
|
+
const memberB = new Uint8Array(members.length);
|
|
1831
|
+
for (let m = 0; m < members.length; m++) {
|
|
1832
|
+
memberA[m] = COMBO_CARDS[members[m] * 2];
|
|
1833
|
+
memberB[m] = COMBO_CARDS[members[m] * 2 + 1];
|
|
1539
1834
|
}
|
|
1540
1835
|
const strength = new Int32Array(list.length);
|
|
1541
1836
|
if (showdown !== null) {
|
|
@@ -1554,11 +1849,15 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1554
1849
|
list,
|
|
1555
1850
|
slot: Uint16Array.from(entrySlots),
|
|
1556
1851
|
roundBase,
|
|
1557
|
-
tieStart,
|
|
1558
|
-
ties: Int32Array.from(ties),
|
|
1559
1852
|
cardA,
|
|
1560
1853
|
cardB,
|
|
1561
|
-
strength
|
|
1854
|
+
strength,
|
|
1855
|
+
packed: packedSlot,
|
|
1856
|
+
multiplicity,
|
|
1857
|
+
memberStart: Int32Array.from(memberStart),
|
|
1858
|
+
memberCombo,
|
|
1859
|
+
memberA,
|
|
1860
|
+
memberB
|
|
1562
1861
|
};
|
|
1563
1862
|
}
|
|
1564
1863
|
var strengthScratch = new Int32Array(COMBOS);
|
|
@@ -1572,35 +1871,6 @@ function strengthOn(table, board) {
|
|
|
1572
1871
|
}
|
|
1573
1872
|
return strengthScratch;
|
|
1574
1873
|
}
|
|
1575
|
-
function tieMirrors(store, slotBase, actions, stride, ties, from, to, integer) {
|
|
1576
|
-
for (let a = 0; a < actions; a++) {
|
|
1577
|
-
const row = slotBase + a * stride;
|
|
1578
|
-
for (let k = from; k < to; ) {
|
|
1579
|
-
const size = ties[k];
|
|
1580
|
-
let sum = 0;
|
|
1581
|
-
for (let j = 1; j <= size; j++) sum += store[row + ties[k + j]];
|
|
1582
|
-
let mean = sum / size;
|
|
1583
|
-
if (integer) mean = Math.round(mean);
|
|
1584
|
-
for (let j = 1; j <= size; j++) store[row + ties[k + j]] = mean;
|
|
1585
|
-
k += size + 1;
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
}
|
|
1589
|
-
function tieBlock(ctx, node, board) {
|
|
1590
|
-
const { tree } = ctx;
|
|
1591
|
-
const owner = tree.player[node];
|
|
1592
|
-
const lists = ctx.active;
|
|
1593
|
-
const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
|
|
1594
|
-
const from = lists.tieStart[listSlot];
|
|
1595
|
-
const to = lists.tieStart[listSlot + 1];
|
|
1596
|
-
if (from === to) return;
|
|
1597
|
-
const actions = tree.numEdges[node];
|
|
1598
|
-
const count = lists.count[listSlot];
|
|
1599
|
-
const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1600
|
-
const integer = ctx.regretScale !== null;
|
|
1601
|
-
if (ctx.regret.length > 0) tieMirrors(ctx.regret, base, actions, count, lists.ties, from, to, integer);
|
|
1602
|
-
tieMirrors(ctx.strategySum, base, actions, count, lists.ties, from, to, integer);
|
|
1603
|
-
}
|
|
1604
1874
|
function makeBuffers2(tree, maxActions, simd) {
|
|
1605
1875
|
const depth = treeDepth2(tree) + 2;
|
|
1606
1876
|
const make = (factory) => Array.from({ length: depth }, factory);
|
|
@@ -1646,7 +1916,7 @@ function dcfrFactors(t) {
|
|
|
1646
1916
|
keepSum: ((t - 1) / t) ** DCFR_GAMMA
|
|
1647
1917
|
};
|
|
1648
1918
|
}
|
|
1649
|
-
var Mode = { Train: 0, BestResponse: 1, Average: 2 };
|
|
1919
|
+
var Mode = { Train: 0, BestResponse: 1, Average: 2, Current: 3 };
|
|
1650
1920
|
function normalise(source, base, actions, count, strategy) {
|
|
1651
1921
|
const uniform = 1 / actions;
|
|
1652
1922
|
for (let i = 0; i < count; i++) {
|
|
@@ -1665,6 +1935,20 @@ function normalise(source, base, actions, count, strategy) {
|
|
|
1665
1935
|
}
|
|
1666
1936
|
}
|
|
1667
1937
|
}
|
|
1938
|
+
var TIE_TOLERANCE = 2 ** -20;
|
|
1939
|
+
function snapTies(actionValues, out, actions, count) {
|
|
1940
|
+
for (let a = 0; a < actions; a++) {
|
|
1941
|
+
const offset = a * count;
|
|
1942
|
+
for (let i = 0; i < count; i++) {
|
|
1943
|
+
const v = actionValues[offset + i];
|
|
1944
|
+
const value = out[i];
|
|
1945
|
+
const gap = v < value ? value - v : v - value;
|
|
1946
|
+
const size = v < 0 ? -v : v;
|
|
1947
|
+
const valueSize = value < 0 ? -value : value;
|
|
1948
|
+
if (gap <= TIE_TOLERANCE * (size > valueSize ? size : valueSize)) actionValues[offset + i] = value;
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1668
1952
|
function regretMatch(regret, base, actions, count, strategy) {
|
|
1669
1953
|
normalise(regret, base, actions, count, strategy);
|
|
1670
1954
|
}
|
|
@@ -1715,6 +1999,26 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1715
1999
|
const signed = traverser === 0 ? tree.payoff[node] : -tree.payoff[node];
|
|
1716
2000
|
const weight = ctx.runoutWeight[round] / ctx.chipScale;
|
|
1717
2001
|
const hands = ctx.hands;
|
|
2002
|
+
if (lists.packed[oppSlot]) {
|
|
2003
|
+
foldValuesPacked(
|
|
2004
|
+
reachOpp,
|
|
2005
|
+
lists.cardA,
|
|
2006
|
+
lists.cardB,
|
|
2007
|
+
lists.memberStart,
|
|
2008
|
+
lists.memberA,
|
|
2009
|
+
lists.memberB,
|
|
2010
|
+
oppFrom,
|
|
2011
|
+
oppCount,
|
|
2012
|
+
myFrom,
|
|
2013
|
+
myCount,
|
|
2014
|
+
hands.same,
|
|
2015
|
+
hands.sameStart[mySlot],
|
|
2016
|
+
signed * weight,
|
|
2017
|
+
buffers.cardSums,
|
|
2018
|
+
out
|
|
2019
|
+
);
|
|
2020
|
+
return out;
|
|
2021
|
+
}
|
|
1718
2022
|
foldValuesHands(
|
|
1719
2023
|
reachOpp,
|
|
1720
2024
|
lists.cardA,
|
|
@@ -1733,6 +2037,25 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1733
2037
|
}
|
|
1734
2038
|
if (kind === NodeKind.TerminalShowdown) {
|
|
1735
2039
|
const weight = ctx.runoutWeight[round] / ctx.chipScale;
|
|
2040
|
+
if (lists.packed[oppSlot]) {
|
|
2041
|
+
showdownValuesPacked(
|
|
2042
|
+
reachOpp,
|
|
2043
|
+
lists.cardA,
|
|
2044
|
+
lists.cardB,
|
|
2045
|
+
lists.memberStart,
|
|
2046
|
+
lists.memberA,
|
|
2047
|
+
lists.memberB,
|
|
2048
|
+
lists.strength,
|
|
2049
|
+
oppFrom,
|
|
2050
|
+
oppCount,
|
|
2051
|
+
myFrom,
|
|
2052
|
+
myCount,
|
|
2053
|
+
tree.payoff[node] * weight,
|
|
2054
|
+
buffers.cardSums,
|
|
2055
|
+
out
|
|
2056
|
+
);
|
|
2057
|
+
return out;
|
|
2058
|
+
}
|
|
1736
2059
|
showdownValuesHands(
|
|
1737
2060
|
reachOpp,
|
|
1738
2061
|
lists.cardA,
|
|
@@ -1772,10 +2095,13 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1772
2095
|
const childMe = buffers.reachFor[traverser][depth];
|
|
1773
2096
|
const childOpp = buffers.reachFor[opponent][depth];
|
|
1774
2097
|
const sharded = nextRound === ctx.shardRound;
|
|
1775
|
-
const
|
|
1776
|
-
const
|
|
2098
|
+
const starts = runouts.childStart[nextRound];
|
|
2099
|
+
const first = starts[board];
|
|
2100
|
+
const last = starts[board + 1];
|
|
1777
2101
|
const from = sharded && ctx.shardStart > first ? ctx.shardStart : first;
|
|
1778
2102
|
const to = sharded && ctx.shardEnd < last ? ctx.shardEnd : last;
|
|
2103
|
+
const sampling = ctx.sampling ?? null;
|
|
2104
|
+
const sampleHere = sampling !== null && mode === Mode.Train && ctx.applyUpdates && !ctx.expandOrbits && sampling.rounds[nextRound] === 1;
|
|
1779
2105
|
for (let childBoard = from; childBoard < to; childBoard++) {
|
|
1780
2106
|
const childMySlot = (lists.roundBase[nextRound] + childBoard) * 2 + traverser;
|
|
1781
2107
|
const childOppSlot = (lists.roundBase[nextRound] + childBoard) * 2 + opponent;
|
|
@@ -1786,15 +2112,40 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1786
2112
|
let value = null;
|
|
1787
2113
|
for (let m = firstMember; m < membersEnd; m++) {
|
|
1788
2114
|
const upMe = upStart[m * 2 + traverser];
|
|
1789
|
-
if (value === null
|
|
2115
|
+
if (sampleHere && value === null) {
|
|
2116
|
+
value = sampledValue(
|
|
2117
|
+
ctx,
|
|
2118
|
+
sampling,
|
|
2119
|
+
buffers,
|
|
2120
|
+
traverser,
|
|
2121
|
+
iterWeight,
|
|
2122
|
+
node,
|
|
2123
|
+
child,
|
|
2124
|
+
nextRound,
|
|
2125
|
+
childBoard,
|
|
2126
|
+
reachMe,
|
|
2127
|
+
reachOpp,
|
|
2128
|
+
childMe,
|
|
2129
|
+
childOpp,
|
|
2130
|
+
depth
|
|
2131
|
+
);
|
|
2132
|
+
} else if (value === null || ctx.expandOrbits) {
|
|
1790
2133
|
const upOpp = upStart[m * 2 + opponent];
|
|
1791
2134
|
for (let k = 0; k < nMe; k++) childMe[k] = reachMe[up[upMe + k]];
|
|
1792
2135
|
for (let k = 0; k < nOpp; k++) childOpp[k] = reachOpp[up[upOpp + k]];
|
|
1793
2136
|
value = traverse(ctx, buffers, mode, traverser, iterWeight, child, childBoard, childMe, childOpp, depth + 1);
|
|
1794
2137
|
}
|
|
1795
|
-
|
|
2138
|
+
if (lists.packed[childMySlot]) {
|
|
2139
|
+
const childFrom = lists.start[childMySlot];
|
|
2140
|
+
for (let k = 0; k < nMe; k++) out[up[upMe + k]] += value[k] * lists.multiplicity[childFrom + k];
|
|
2141
|
+
} else {
|
|
2142
|
+
for (let k = 0; k < nMe; k++) out[up[upMe + k]] += value[k];
|
|
2143
|
+
}
|
|
1796
2144
|
}
|
|
1797
2145
|
}
|
|
2146
|
+
if (lists.packed[mySlot]) {
|
|
2147
|
+
for (let i = 0; i < myCount; i++) out[i] /= lists.multiplicity[myFrom + i];
|
|
2148
|
+
}
|
|
1798
2149
|
return out;
|
|
1799
2150
|
}
|
|
1800
2151
|
const actions = tree.numEdges[node];
|
|
@@ -1807,12 +2158,19 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1807
2158
|
const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1808
2159
|
const strategy = buffers.strategy[depth];
|
|
1809
2160
|
const length = actions * stride;
|
|
1810
|
-
|
|
2161
|
+
const compactNode = round >= ctx.layout.compactFrom;
|
|
2162
|
+
const chunk = compactNode ? ctx.layout.compactChunk[node] : 0;
|
|
2163
|
+
const regrets = compactNode ? ctx.compactRegret[chunk] : ctx.regret;
|
|
2164
|
+
const compactSums = compactNode && ctx.compact.sums;
|
|
2165
|
+
const current = mode === Mode.Train || mode === Mode.Current || compactNode && !compactSums;
|
|
2166
|
+
if (current && buffers.kernels !== null) {
|
|
1811
2167
|
const kernels = buffers.kernels;
|
|
1812
|
-
kernels.input.set(
|
|
2168
|
+
kernels.input.set(regrets.subarray(slotBase, slotBase + length));
|
|
1813
2169
|
kernels.regretMatch(strategy, actions, stride);
|
|
1814
|
-
} else if (
|
|
1815
|
-
regretMatch(
|
|
2170
|
+
} else if (current) {
|
|
2171
|
+
regretMatch(regrets, slotBase, actions, stride, strategy);
|
|
2172
|
+
} else if (compactSums) {
|
|
2173
|
+
averageStrategy(ctx.compactSum[chunk], slotBase, actions, stride, strategy, regrets);
|
|
1816
2174
|
} else if (buffers.kernels !== null) {
|
|
1817
2175
|
const kernels = buffers.kernels;
|
|
1818
2176
|
kernels.input.set(ctx.strategySum.subarray(slotBase, slotBase + length));
|
|
@@ -1873,9 +2231,36 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1873
2231
|
for (let i = 0; i < myCount; i++) out[i] += value[i];
|
|
1874
2232
|
}
|
|
1875
2233
|
}
|
|
1876
|
-
if (isTraverser && mode === Mode.Train && ctx.applyUpdates
|
|
2234
|
+
if (isTraverser && mode === Mode.Train && ctx.applyUpdates) snapTies(actionValues, out, actions, myCount);
|
|
2235
|
+
const weighted = ctx.layout.reachWeighted;
|
|
2236
|
+
if (weighted !== null && weighted[node] && isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
2237
|
+
for (let a = 0; a < actions; a++) {
|
|
2238
|
+
const offset = a * myCount;
|
|
2239
|
+
for (let i = 0; i < myCount; i++) {
|
|
2240
|
+
actionValues[offset + i] = out[i] + reachMe[i] * (actionValues[offset + i] - out[i]);
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
}
|
|
2244
|
+
if (isTraverser && mode === Mode.Train && ctx.applyUpdates && compactNode) {
|
|
2245
|
+
updateCompact(
|
|
2246
|
+
ctx,
|
|
2247
|
+
node,
|
|
2248
|
+
chunk,
|
|
2249
|
+
ctx.layout.blockOffset[node] + board,
|
|
2250
|
+
slotBase,
|
|
2251
|
+
ctx.layout.handOffset[node] + ctx.layout.rowStart[listSlot],
|
|
2252
|
+
actions,
|
|
2253
|
+
stride,
|
|
2254
|
+
iterWeight,
|
|
2255
|
+
reachMe,
|
|
2256
|
+
strategy,
|
|
2257
|
+
actionValues,
|
|
2258
|
+
out
|
|
2259
|
+
);
|
|
2260
|
+
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.regretScale !== null) {
|
|
1877
2261
|
updateQuantised(
|
|
1878
2262
|
ctx,
|
|
2263
|
+
node,
|
|
1879
2264
|
ctx.layout.blockOffset[node] + board,
|
|
1880
2265
|
slotBase,
|
|
1881
2266
|
actions,
|
|
@@ -1887,7 +2272,7 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1887
2272
|
out
|
|
1888
2273
|
);
|
|
1889
2274
|
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.dcfr) {
|
|
1890
|
-
const { keepPositive, keepNegative, keepSum } =
|
|
2275
|
+
const { keepPositive, keepNegative, keepSum } = blockFactors(ctx, ctx.layout.blockOffset[node] + board, iterWeight);
|
|
1891
2276
|
const regret = ctx.regret;
|
|
1892
2277
|
const strategySum = ctx.strategySum;
|
|
1893
2278
|
for (let a = 0; a < actions; a++) {
|
|
@@ -1898,7 +2283,7 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1898
2283
|
const at = slotRow + i;
|
|
1899
2284
|
const accumulated = regret[at];
|
|
1900
2285
|
regret[at] = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + i] - out[i];
|
|
1901
|
-
strategySum[at] = strategySum[at] * keepSum +
|
|
2286
|
+
strategySum[at] = strategySum[at] * keepSum + strategy[row + i];
|
|
1902
2287
|
}
|
|
1903
2288
|
}
|
|
1904
2289
|
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
@@ -1916,18 +2301,39 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1916
2301
|
}
|
|
1917
2302
|
}
|
|
1918
2303
|
}
|
|
1919
|
-
if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
1920
|
-
const tieFrom = lists.tieStart[listSlot];
|
|
1921
|
-
const tieTo = lists.tieStart[listSlot + 1];
|
|
1922
|
-
if (tieFrom < tieTo) {
|
|
1923
|
-
const integer = ctx.regretScale !== null;
|
|
1924
|
-
tieMirrors(ctx.regret, slotBase, actions, stride, lists.ties, tieFrom, tieTo, integer);
|
|
1925
|
-
tieMirrors(ctx.strategySum, slotBase, actions, stride, lists.ties, tieFrom, tieTo, integer);
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
2304
|
return out;
|
|
1929
2305
|
}
|
|
1930
|
-
function
|
|
2306
|
+
function sampledValue(ctx, s, buffers, traverser, t, node, child, nextRound, childBoard, reachMe, reachOpp, childMe, childOpp, depth) {
|
|
2307
|
+
const lists = ctx.active;
|
|
2308
|
+
const opponent = 1 - traverser;
|
|
2309
|
+
const childMySlot = (lists.roundBase[nextRound] + childBoard) * 2 + traverser;
|
|
2310
|
+
const nMe = lists.count[childMySlot];
|
|
2311
|
+
const at = s.baseOffset[node] + s.boardOffset[childMySlot];
|
|
2312
|
+
const walkAt = s.walkOffset[node] + childBoard * 2 + traverser;
|
|
2313
|
+
const fresh = s.lastWalk[walkAt] === 0;
|
|
2314
|
+
if (!fresh && !sampled(s, node, childBoard, t)) return s.baseline.subarray(at, at + nMe);
|
|
2315
|
+
const childOppSlot = (lists.roundBase[nextRound] + childBoard) * 2 + opponent;
|
|
2316
|
+
const nOpp = lists.count[childOppSlot];
|
|
2317
|
+
const member = ctx.runouts.orbitStart[nextRound][childBoard];
|
|
2318
|
+
const upStart = ctx.hands.upStart[nextRound];
|
|
2319
|
+
const up = ctx.hands.up;
|
|
2320
|
+
const upMe = upStart[member * 2 + traverser];
|
|
2321
|
+
const upOpp = upStart[member * 2 + opponent];
|
|
2322
|
+
for (let k = 0; k < nMe; k++) childMe[k] = reachMe[up[upMe + k]];
|
|
2323
|
+
for (let k = 0; k < nOpp; k++) childOpp[k] = reachOpp[up[upOpp + k]];
|
|
2324
|
+
const value = traverse(ctx, buffers, Mode.Train, traverser, t, child, childBoard, childMe, childOpp, depth + 1);
|
|
2325
|
+
const weight = s.unbiased ? 1 / s.p : 1;
|
|
2326
|
+
const baseline = s.baseline;
|
|
2327
|
+
for (let k = 0; k < nMe; k++) {
|
|
2328
|
+
const v = value[k];
|
|
2329
|
+
const b = baseline[at + k];
|
|
2330
|
+
baseline[at + k] = v;
|
|
2331
|
+
if (!fresh) value[k] = v + (weight - 1) * (v - b);
|
|
2332
|
+
}
|
|
2333
|
+
s.lastWalk[walkAt] = t;
|
|
2334
|
+
return value;
|
|
2335
|
+
}
|
|
2336
|
+
function updateQuantised(ctx, node, block, slotBase, actions, count, iterWeight, reachMe, strategy, actionValues, out) {
|
|
1931
2337
|
const regret = ctx.regret;
|
|
1932
2338
|
const strategySum = ctx.strategySum;
|
|
1933
2339
|
const regretScale = ctx.regretScale;
|
|
@@ -1940,7 +2346,7 @@ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reach
|
|
|
1940
2346
|
let weight = iterWeight;
|
|
1941
2347
|
const clamp = !ctx.dcfr;
|
|
1942
2348
|
if (ctx.dcfr) {
|
|
1943
|
-
({ keepPositive, keepNegative, keepSum } =
|
|
2349
|
+
({ keepPositive, keepNegative, keepSum } = blockFactors(ctx, block, iterWeight));
|
|
1944
2350
|
weight = 1;
|
|
1945
2351
|
}
|
|
1946
2352
|
let largestRegret = 0;
|
|
@@ -1956,13 +2362,15 @@ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reach
|
|
|
1956
2362
|
const magnitude = updated < 0 ? -updated : updated;
|
|
1957
2363
|
if (magnitude > largestRegret) largestRegret = magnitude;
|
|
1958
2364
|
actionValues[offset + i] = updated;
|
|
1959
|
-
const
|
|
2365
|
+
const added = clamp ? weight * reachMe[i] * strategy[a * count + i] : weight * strategy[a * count + i];
|
|
2366
|
+
const sum = strategySum[at] * oldSum * keepSum + added;
|
|
1960
2367
|
if (sum > largestSum) largestSum = sum;
|
|
1961
2368
|
strategy[a * count + i] = sum;
|
|
1962
2369
|
}
|
|
1963
2370
|
}
|
|
1964
2371
|
const regretStep = largestRegret > 0 ? REGRET_STEPS / largestRegret : 0;
|
|
1965
2372
|
const sumStep = largestSum > 0 ? SUM_STEPS / largestSum : 0;
|
|
2373
|
+
const salt = ctx.layout.shared ? node : 0;
|
|
1966
2374
|
for (let a = 0; a < actions; a++) {
|
|
1967
2375
|
const offset = a * count;
|
|
1968
2376
|
const slotRow = slotBase + a * count;
|
|
@@ -1970,15 +2378,157 @@ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reach
|
|
|
1970
2378
|
const at = slotRow + i;
|
|
1971
2379
|
const r = actionValues[offset + i] * regretStep;
|
|
1972
2380
|
regret[at] = r < 0 ? r - 0.5 : r + 0.5;
|
|
1973
|
-
const s = strategy[a * count + i] * sumStep + dither(at, iterWeight);
|
|
2381
|
+
const s = strategy[a * count + i] * sumStep + dither(at, iterWeight, salt);
|
|
1974
2382
|
strategySum[at] = s < SUM_STEPS ? s : SUM_STEPS;
|
|
1975
2383
|
}
|
|
1976
2384
|
}
|
|
1977
2385
|
regretScale[block] = largestRegret / REGRET_STEPS;
|
|
1978
2386
|
sumScale[block] = largestSum / SUM_STEPS;
|
|
1979
2387
|
}
|
|
1980
|
-
|
|
1981
|
-
|
|
2388
|
+
var SHIFT_BIAS = 160;
|
|
2389
|
+
var SHIFT_SCALE = Float64Array.from({ length: 256 }, (_, e) => e === 0 ? 0 : 2 ** (e - SHIFT_BIAS));
|
|
2390
|
+
function updateCompact(ctx, node, chunk, block, slotBase, handBase, actions, count, iterWeight, reachMe, strategy, actionValues, out) {
|
|
2391
|
+
const plan = ctx.compact;
|
|
2392
|
+
const q = ctx.compactRegret[chunk];
|
|
2393
|
+
const hashBase = ctx.layout.chunkStart[chunk] + slotBase;
|
|
2394
|
+
const salt = ctx.layout.shared ? node : 0;
|
|
2395
|
+
const steps = plan.steps;
|
|
2396
|
+
let keepPositive = 1;
|
|
2397
|
+
let keepNegative = 0;
|
|
2398
|
+
let keepSum = 1;
|
|
2399
|
+
let weight = iterWeight;
|
|
2400
|
+
const clamp = !ctx.dcfr;
|
|
2401
|
+
if (ctx.dcfr) {
|
|
2402
|
+
({ keepPositive, keepNegative, keepSum } = blockFactors(ctx, block, iterWeight));
|
|
2403
|
+
weight = 1;
|
|
2404
|
+
}
|
|
2405
|
+
if (plan.float) {
|
|
2406
|
+
for (let k = 0; k < actions * count; k++) {
|
|
2407
|
+
const accumulated = q[slotBase + k];
|
|
2408
|
+
const updated = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[k] - out[k % count];
|
|
2409
|
+
q[slotBase + k] = clamp && updated < 0 ? 0 : updated;
|
|
2410
|
+
}
|
|
2411
|
+
return;
|
|
2412
|
+
}
|
|
2413
|
+
if (!plan.perHand) {
|
|
2414
|
+
const scales = ctx.compactScale;
|
|
2415
|
+
const old = scales[block];
|
|
2416
|
+
let largest = 0;
|
|
2417
|
+
for (let a = 0; a < actions; a++) {
|
|
2418
|
+
const row = a * count;
|
|
2419
|
+
for (let i = 0; i < count; i++) {
|
|
2420
|
+
const accumulated = q[slotBase + row + i] * old;
|
|
2421
|
+
let updated = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[row + i] - out[i];
|
|
2422
|
+
if (clamp && updated < 0) updated = 0;
|
|
2423
|
+
actionValues[row + i] = updated;
|
|
2424
|
+
const magnitude = updated < 0 ? -updated : updated;
|
|
2425
|
+
if (magnitude > largest) largest = magnitude;
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
const step = largest > 0 ? steps / largest : 0;
|
|
2429
|
+
const stochastic2 = plan.stochastic;
|
|
2430
|
+
for (let a = 0; a < actions; a++) {
|
|
2431
|
+
const row = a * count;
|
|
2432
|
+
let h = stochastic2 ? streamSeed(hashBase + row, iterWeight, salt) : 0;
|
|
2433
|
+
for (let i = 0; i < count; i++) {
|
|
2434
|
+
const value = actionValues[row + i] * step;
|
|
2435
|
+
let rounded;
|
|
2436
|
+
if (stochastic2) {
|
|
2437
|
+
h = nextState(h);
|
|
2438
|
+
rounded = Math.floor(value + (h >>> 0) / 4294967296);
|
|
2439
|
+
} else {
|
|
2440
|
+
rounded = Math.round(value);
|
|
2441
|
+
}
|
|
2442
|
+
q[slotBase + row + i] = rounded > steps ? steps : rounded < -steps ? -steps : rounded;
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
scales[block] = largest / steps;
|
|
2446
|
+
return;
|
|
2447
|
+
}
|
|
2448
|
+
const shifts = ctx.handShift[chunk];
|
|
2449
|
+
const stochastic = plan.stochastic;
|
|
2450
|
+
const sums = plan.sums ? ctx.compactSum[chunk] : null;
|
|
2451
|
+
const sumShifts = plan.sums ? ctx.sumShift[chunk] : null;
|
|
2452
|
+
for (let i = 0; i < count; i++) {
|
|
2453
|
+
const scale = SHIFT_SCALE[shifts[handBase + i]];
|
|
2454
|
+
let largest = 0;
|
|
2455
|
+
for (let a = 0; a < actions; a++) {
|
|
2456
|
+
const k = a * count + i;
|
|
2457
|
+
const accumulated = q[slotBase + k] * scale;
|
|
2458
|
+
let updated = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[k] - out[i];
|
|
2459
|
+
if (clamp && updated < 0) updated = 0;
|
|
2460
|
+
actionValues[k] = updated;
|
|
2461
|
+
const magnitude = updated < 0 ? -updated : updated;
|
|
2462
|
+
if (magnitude > largest) largest = magnitude;
|
|
2463
|
+
}
|
|
2464
|
+
const exponent = scaleExponent(largest, steps);
|
|
2465
|
+
shifts[handBase + i] = exponent;
|
|
2466
|
+
const inverse = exponent === 0 ? 0 : 1 / SHIFT_SCALE[exponent];
|
|
2467
|
+
let h = stochastic || sums !== null ? streamSeed(hashBase + i, iterWeight, salt) : 0;
|
|
2468
|
+
for (let a = 0; a < actions; a++) {
|
|
2469
|
+
const k = a * count + i;
|
|
2470
|
+
const value = actionValues[k] * inverse;
|
|
2471
|
+
let rounded;
|
|
2472
|
+
if (stochastic) {
|
|
2473
|
+
h = nextState(h);
|
|
2474
|
+
rounded = Math.floor(value + (h >>> 0) / 4294967296);
|
|
2475
|
+
} else {
|
|
2476
|
+
rounded = Math.round(value);
|
|
2477
|
+
}
|
|
2478
|
+
q[slotBase + k] = rounded > steps ? steps : rounded < -steps ? -steps : rounded;
|
|
2479
|
+
}
|
|
2480
|
+
if (sums === null) continue;
|
|
2481
|
+
const sumScale = SHIFT_SCALE[sumShifts[handBase + i]];
|
|
2482
|
+
let largestSum = 0;
|
|
2483
|
+
for (let a = 0; a < actions; a++) {
|
|
2484
|
+
const k = a * count + i;
|
|
2485
|
+
const added = clamp ? weight * reachMe[i] * strategy[k] : weight * strategy[k];
|
|
2486
|
+
const sum = sums[slotBase + k] * sumScale * keepSum + added;
|
|
2487
|
+
strategy[k] = sum;
|
|
2488
|
+
if (sum > largestSum) largestSum = sum;
|
|
2489
|
+
}
|
|
2490
|
+
const sumExponent = scaleExponent(largestSum, COMPACT_SUM_STEPS);
|
|
2491
|
+
sumShifts[handBase + i] = sumExponent;
|
|
2492
|
+
const sumInverse = sumExponent === 0 ? 0 : 1 / SHIFT_SCALE[sumExponent];
|
|
2493
|
+
for (let a = 0; a < actions; a++) {
|
|
2494
|
+
const k = a * count + i;
|
|
2495
|
+
h = nextState(h);
|
|
2496
|
+
const rounded = Math.floor(strategy[k] * sumInverse + (h >>> 0) / 4294967296);
|
|
2497
|
+
sums[slotBase + k] = rounded < COMPACT_SUM_STEPS ? rounded : COMPACT_SUM_STEPS;
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
var COMPACT_SUM_STEPS = 255;
|
|
2502
|
+
function scaleExponent(largest, steps) {
|
|
2503
|
+
if (!(largest > 0)) return 0;
|
|
2504
|
+
EXPONENT_FLOAT[0] = largest / steps;
|
|
2505
|
+
const high2 = EXPONENT_BITS[1];
|
|
2506
|
+
let exponent = (high2 >>> 20 & 2047) - 1023 + SHIFT_BIAS;
|
|
2507
|
+
if ((high2 & 1048575) !== 0 || EXPONENT_BITS[0] !== 0) exponent++;
|
|
2508
|
+
return exponent < 1 ? 0 : exponent > 255 ? 255 : exponent;
|
|
2509
|
+
}
|
|
2510
|
+
function streamSeed(slot, iteration, salt) {
|
|
2511
|
+
return Math.imul(slot ^ Math.imul(iteration, 2654435761) ^ Math.imul(salt, 2146121005) ^ 1540483477, 2246822507) | 1;
|
|
2512
|
+
}
|
|
2513
|
+
function nextState(h) {
|
|
2514
|
+
h ^= h >>> 15;
|
|
2515
|
+
h = Math.imul(h, 739982445);
|
|
2516
|
+
return h ^ h >>> 12;
|
|
2517
|
+
}
|
|
2518
|
+
var EXPONENT_FLOAT = new Float64Array(1);
|
|
2519
|
+
var EXPONENT_BITS = new Uint32Array(EXPONENT_FLOAT.buffer);
|
|
2520
|
+
function storedStrategy(ctx, node, base, actions, count, strategy) {
|
|
2521
|
+
if (ctx.tree.round[node] >= ctx.layout.compactFrom && ctx.compact.sums) {
|
|
2522
|
+
const chunk = ctx.layout.compactChunk[node];
|
|
2523
|
+
averageStrategy(ctx.compactSum[chunk], base, actions, count, strategy, ctx.compactRegret[chunk]);
|
|
2524
|
+
} else if (ctx.tree.round[node] >= ctx.layout.compactFrom) {
|
|
2525
|
+
regretMatch(ctx.compactRegret[ctx.layout.compactChunk[node]], base, actions, count, strategy);
|
|
2526
|
+
} else {
|
|
2527
|
+
averageStrategy(ctx.strategySum, base, actions, count, strategy, ctx.regret);
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
function dither(slot, iteration, salt = 0) {
|
|
2531
|
+
let h = Math.imul(slot ^ Math.imul(iteration, 2654435761) ^ Math.imul(salt, 668265263), 2246822507);
|
|
1982
2532
|
h ^= h >>> 13;
|
|
1983
2533
|
h = Math.imul(h, 3266489909);
|
|
1984
2534
|
h ^= h >>> 16;
|
|
@@ -2020,7 +2570,7 @@ function makeSolver(ctx) {
|
|
|
2020
2570
|
const weights = ctx.players[traverser].weights;
|
|
2021
2571
|
const [from, count] = listOf(0, 0, traverser);
|
|
2022
2572
|
let sum = 0;
|
|
2023
|
-
for (let i = 0; i < count; i++) sum += weights[lists.list[from + i]] * values[i];
|
|
2573
|
+
for (let i = 0; i < count; i++) sum += weights[lists.list[from + i]] * lists.multiplicity[from + i] * values[i];
|
|
2024
2574
|
return sum / ctx.totalMass;
|
|
2025
2575
|
};
|
|
2026
2576
|
return {
|
|
@@ -2030,6 +2580,9 @@ function makeSolver(ctx) {
|
|
|
2030
2580
|
return rootCount;
|
|
2031
2581
|
},
|
|
2032
2582
|
traverseRoot: run,
|
|
2583
|
+
get root() {
|
|
2584
|
+
return root;
|
|
2585
|
+
},
|
|
2033
2586
|
useRoot(next) {
|
|
2034
2587
|
root = next ?? run;
|
|
2035
2588
|
},
|
|
@@ -2046,17 +2599,79 @@ function makeSolver(ctx) {
|
|
|
2046
2599
|
expectedValue() {
|
|
2047
2600
|
return [total(root(Mode.Average, 0, 0), 0), total(root(Mode.Average, 1, 0), 1)];
|
|
2048
2601
|
},
|
|
2602
|
+
rootValues(mode) {
|
|
2603
|
+
if (mode === Mode.Current && ctx.regret.length === 0 && !ctx.compactRegret) {
|
|
2604
|
+
throw new Error("The current profile needs regrets, and an analysis-only run has none");
|
|
2605
|
+
}
|
|
2606
|
+
const one = (player) => {
|
|
2607
|
+
const values = root(mode, player, 0);
|
|
2608
|
+
return [total(values, player), values.slice(0, listOf(0, 0, player)[1])];
|
|
2609
|
+
};
|
|
2610
|
+
const [total0, values0] = one(0);
|
|
2611
|
+
const [total1, values1] = one(1);
|
|
2612
|
+
return { totals: [total0, total1], values: [values0, values1] };
|
|
2613
|
+
},
|
|
2614
|
+
handEvs(player, values) {
|
|
2615
|
+
const opponent = ctx.players[1 - player];
|
|
2616
|
+
const evs = new Float64Array(COMBOS);
|
|
2617
|
+
const [from, count] = listOf(0, 0, player);
|
|
2618
|
+
if (values.length !== count) {
|
|
2619
|
+
throw new Error(`Player ${player} has ${count} root hands, but ${values.length} values were given`);
|
|
2620
|
+
}
|
|
2621
|
+
const scale = ctx.runoutWeight[0];
|
|
2622
|
+
for (let i = 0; i < count; i++) {
|
|
2623
|
+
for (let m = lists.memberStart[from + i]; m < lists.memberStart[from + i + 1]; m++) {
|
|
2624
|
+
const combo = lists.memberCombo[m];
|
|
2625
|
+
const a = COMBO_CARDS[combo * 2];
|
|
2626
|
+
const b = COMBO_CARDS[combo * 2 + 1];
|
|
2627
|
+
let mass = 0;
|
|
2628
|
+
for (let j = 0; j < opponent.count; j++) {
|
|
2629
|
+
const other = opponent.combos[j];
|
|
2630
|
+
const c = COMBO_CARDS[other * 2];
|
|
2631
|
+
const d = COMBO_CARDS[other * 2 + 1];
|
|
2632
|
+
if (c !== a && c !== b && d !== a && d !== b) mass += opponent.weights[other];
|
|
2633
|
+
}
|
|
2634
|
+
evs[combo] = mass > 0 ? values[i] / (mass * scale) : 0;
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
return evs;
|
|
2638
|
+
},
|
|
2049
2639
|
exploitability() {
|
|
2050
2640
|
const br0 = total(root(Mode.BestResponse, 0, 0), 0);
|
|
2051
2641
|
const br1 = total(root(Mode.BestResponse, 1, 0), 1);
|
|
2052
2642
|
return (br0 + br1) / 2;
|
|
2053
2643
|
},
|
|
2054
2644
|
valuesAt(node, board, traverser, reachMe, reachOpp, live, liveCount) {
|
|
2645
|
+
void live;
|
|
2646
|
+
void liveCount;
|
|
2647
|
+
const round = ctx.tree.round[node];
|
|
2648
|
+
const opponent = 1 - traverser;
|
|
2649
|
+
const [oppFrom, oppCount] = listOf(round, board, opponent);
|
|
2650
|
+
let symmetric = true;
|
|
2651
|
+
for (let j = 0; j < oppCount && symmetric; j++) {
|
|
2652
|
+
const own = reachOpp[lists.list[oppFrom + j]];
|
|
2653
|
+
for (let m = lists.memberStart[oppFrom + j] + 1; m < lists.memberStart[oppFrom + j + 1]; m++) {
|
|
2654
|
+
if (reachOpp[lists.memberCombo[m]] !== own) symmetric = false;
|
|
2655
|
+
}
|
|
2656
|
+
}
|
|
2657
|
+
const members = (player) => {
|
|
2658
|
+
const [from, count] = listOf(round, board, player);
|
|
2659
|
+
return lists.memberCombo.slice(lists.memberStart[from], lists.memberStart[from + count]);
|
|
2660
|
+
};
|
|
2661
|
+
if (!symmetric) {
|
|
2662
|
+
const theirs = members(opponent).filter((combo) => reachOpp[combo] !== 0);
|
|
2663
|
+
const mine = members(traverser);
|
|
2664
|
+
const handReach = Float64Array.from(theirs, (combo) => reachOpp[combo]);
|
|
2665
|
+
const rows = pairValues(ctx, node, board, traverser, theirs, handReach, mine, mine.length);
|
|
2666
|
+
const result = new Float64Array(COMBOS);
|
|
2667
|
+
for (let k = 0; k < theirs.length; k++) {
|
|
2668
|
+
for (const combo of mine) result[combo] += rows[k * COMBOS + combo];
|
|
2669
|
+
}
|
|
2670
|
+
return result;
|
|
2671
|
+
}
|
|
2055
2672
|
const expanded = ctx.expandOrbits;
|
|
2056
2673
|
ctx.expandOrbits = true;
|
|
2057
2674
|
try {
|
|
2058
|
-
const round = ctx.tree.round[node];
|
|
2059
|
-
const opponent = 1 - traverser;
|
|
2060
2675
|
const me = new Float64Array(COMBOS);
|
|
2061
2676
|
const opp = new Float64Array(COMBOS);
|
|
2062
2677
|
gather(reachMe, round, board, traverser, me);
|
|
@@ -2064,9 +2679,11 @@ function makeSolver(ctx) {
|
|
|
2064
2679
|
const value = traverse(ctx, buffers, Mode.Average, traverser, 0, node, board, me, opp, 0);
|
|
2065
2680
|
const result = new Float64Array(COMBOS);
|
|
2066
2681
|
const [from, count] = listOf(round, board, traverser);
|
|
2067
|
-
for (let i = 0; i < count; i++)
|
|
2068
|
-
|
|
2069
|
-
|
|
2682
|
+
for (let i = 0; i < count; i++) {
|
|
2683
|
+
for (let m = lists.memberStart[from + i]; m < lists.memberStart[from + i + 1]; m++) {
|
|
2684
|
+
result[lists.memberCombo[m]] = value[i];
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2070
2687
|
return result;
|
|
2071
2688
|
} finally {
|
|
2072
2689
|
ctx.expandOrbits = expanded;
|
|
@@ -2084,11 +2701,13 @@ function makeSolver(ctx) {
|
|
|
2084
2701
|
const count = lists.count[slot];
|
|
2085
2702
|
const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[slot];
|
|
2086
2703
|
const strategy = new Float64Array(actions * count);
|
|
2087
|
-
|
|
2704
|
+
storedStrategy(ctx, node, base, actions, count, strategy);
|
|
2088
2705
|
const probs = new Float64Array(index.count * actions).fill(1 / actions);
|
|
2089
2706
|
for (let i = 0; i < count; i++) {
|
|
2090
|
-
|
|
2091
|
-
|
|
2707
|
+
for (let m = lists.memberStart[from + i]; m < lists.memberStart[from + i + 1]; m++) {
|
|
2708
|
+
const at = index.slot[lists.memberCombo[m]];
|
|
2709
|
+
for (let a = 0; a < actions; a++) probs[at * actions + a] = strategy[a * count + i];
|
|
2710
|
+
}
|
|
2092
2711
|
}
|
|
2093
2712
|
return { actions, probs };
|
|
2094
2713
|
}
|
|
@@ -2096,17 +2715,19 @@ function makeSolver(ctx) {
|
|
|
2096
2715
|
}
|
|
2097
2716
|
|
|
2098
2717
|
// src/wasm/engine.ts
|
|
2099
|
-
var ENGINE_WASM = "
|
|
2718
|
+
var ENGINE_WASM = "AGFzbQEAAAABcAtgBH9/f38AYAd/f39/f3x/AGADf39/AGACf38AYAF/AGAGf39/f39/AX9gCH9/f39/f39/AGAMf3x/f39/f39/f39/AGAMf39/f39/f39/f39/AX9gCn9/f39/fX19fX8Bf2AJf39/f319fX1/AX8CDwEDZW52Bm1lbW9yeQIAAAMREAUGAAEBAgIHAwQDAAgJCgQGmwNIfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEA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IA1KBEAgCiANQQJ0aiAIIAYgDUEBdGovAQBBAnRqKgIAOAIAIA1BAWohDQwBCwsgACACIAQgCSAKIAtBAWoQACEAA0AgDiAPSARAIA5BAnQiASAAaiICKgIAIRQgASAQaiIBKgIAIRUgASAUOAIAIBJFBEAgAiAUIzFDAACAv5IgFCAVk5SSOAIACyAOQQFqIQ4MAQsLIBEjOjYCACAACysAIAAkQSAFJEIgBiRDIAckRCAIJEUgCUEARyRGIAEgAkEAIAMgBEEAEAALKwAgACRBIAQkQiAFJEMgBiREIAckRSAIQQBHJEYgAUEAQQAgAiADQQAQAAuOAgAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBJCEgACgCiAEkIiAAKAKMASQjIAAoApABuCQkCw==";
|
|
2100
2719
|
|
|
2101
2720
|
// src/wasm/engine-shared.ts
|
|
2102
|
-
var ENGINE_SHARED_WASM = "
|
|
2721
|
+
var ENGINE_SHARED_WASM = "AGFzbQEAAAABcAtgBH9/f38AYAd/f39/f3x/AGADf39/AGACf38AYAF/AGAGf39/f39/AX9gCH9/f39/f39/AGAMf3x/f39/f39/f39/AGAMf39/f39/f39/f39/AX9gCn9/f39/fX19fX8Bf2AJf39/f319fX1/AX8CEgEDZW52Bm1lbW9yeQIDAICABAMREAUGAAEBAgIHAwQDAAgJCgQGmwNIfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt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J0aiAIIAYgDUEBdGovAQBBAnRqKgIAOAIAIA1BAWohDQwBCwsgACACIAQgCSAKIAtBAWoQACEAA0AgDiAPSARAIA5BAnQiASAAaiICKgIAIRQgASAQaiIBKgIAIRUgASAUOAIAIBJFBEAgAiAUIzFDAACAv5IgFCAVk5SSOAIACyAOQQFqIQ4MAQsLIBEjOjYCACAACysAIAAkQSAFJEIgBiRDIAckRCAIJEUgCUEARyRGIAEgAkEAIAMgBEEAEAALKwAgACRBIAQkQiAFJEMgBiREIAckRSAIQQBHJEYgAUEAQQAgAiADQQAQAAuOAgAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBJCEgACgCiAEkIiAAKAKMASQjIAAoApABuCQkCw==";
|
|
2103
2722
|
|
|
2104
2723
|
// src/platform/node.ts
|
|
2105
2724
|
import { availableParallelism } from "os";
|
|
2106
2725
|
import { fileURLToPath } from "url";
|
|
2107
2726
|
import { Worker } from "worker_threads";
|
|
2108
2727
|
var PLATFORM = "node";
|
|
2109
|
-
|
|
2728
|
+
function threadLimit(_engine) {
|
|
2729
|
+
return Infinity;
|
|
2730
|
+
}
|
|
2110
2731
|
var DEFAULT_MAX_BYTES = 6 * 1024 * 1024 * 1024;
|
|
2111
2732
|
function workerFlags() {
|
|
2112
2733
|
return process.execArgv.filter((flag) => !flag.startsWith("--input-type"));
|
|
@@ -2137,6 +2758,9 @@ function spawnEngineWorker(setup, onError) {
|
|
|
2137
2758
|
}
|
|
2138
2759
|
};
|
|
2139
2760
|
}
|
|
2761
|
+
function threadAdvice(_engine) {
|
|
2762
|
+
return "";
|
|
2763
|
+
}
|
|
2140
2764
|
|
|
2141
2765
|
// src/parallel.ts
|
|
2142
2766
|
var CONTROL = {
|
|
@@ -2147,8 +2771,31 @@ var CONTROL = {
|
|
|
2147
2771
|
iterWeight: 4,
|
|
2148
2772
|
ready: 5,
|
|
2149
2773
|
mode: 6,
|
|
2774
|
+
/** Set, never cleared, once any thread's part of a step threw; see src/worker.ts. */
|
|
2775
|
+
failed: 7,
|
|
2150
2776
|
size: 8
|
|
2151
2777
|
};
|
|
2778
|
+
var THREAD_FAILED_ERROR = "A solver thread failed mid-iteration; this run cannot continue. Close it and solve again, or use workers: 1.";
|
|
2779
|
+
var THREAD_LOST_ERROR = "A solver thread stopped answering mid-iteration; it may have run out of memory or been killed. This run cannot continue. Close it and solve again, or use workers: 1.";
|
|
2780
|
+
var THREAD_BACKSTOP_FLOOR_MS = 6e4;
|
|
2781
|
+
var THREAD_BACKSTOP_FACTOR = 20;
|
|
2782
|
+
function backstopMs(ownMs, floorMs = THREAD_BACKSTOP_FLOOR_MS) {
|
|
2783
|
+
return Math.max(floorMs, THREAD_BACKSTOP_FACTOR * ownMs);
|
|
2784
|
+
}
|
|
2785
|
+
var WAIT_SLICE_MS = 50;
|
|
2786
|
+
var STOPPED_MS = 1e3;
|
|
2787
|
+
function awaitThreads(control, slot, target, dispatched, floorMs, stopped) {
|
|
2788
|
+
let giveUp = dispatched + backstopMs(performance.now() - dispatched, floorMs);
|
|
2789
|
+
for (; ; ) {
|
|
2790
|
+
const done = Atomics.load(control, slot);
|
|
2791
|
+
if (done >= target || stopped()) return null;
|
|
2792
|
+
const before = performance.now();
|
|
2793
|
+
if (before > giveUp) return new Error(THREAD_LOST_ERROR);
|
|
2794
|
+
Atomics.wait(control, slot, done, WAIT_SLICE_MS);
|
|
2795
|
+
const overrun = performance.now() - before - WAIT_SLICE_MS;
|
|
2796
|
+
if (overrun > STOPPED_MS) giveUp += overrun;
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2152
2799
|
var COMMAND = { work: 0, exit: 1 };
|
|
2153
2800
|
var WORKER_START_MS = 3e4;
|
|
2154
2801
|
var WORKER_START_ERROR = "Worker threads did not start within 30 s. Run with workers: 1, or check that worker threads can load this package.";
|
|
@@ -2177,7 +2824,7 @@ function planShards(tree, runouts, workers) {
|
|
|
2177
2824
|
for (let w = 0; w <= workers; w++) bounds[w] = Math.round(boards * w / workers);
|
|
2178
2825
|
return { round, boards, entryNodes: Int32Array.from(entryNodes), entryIndexOf, bounds };
|
|
2179
2826
|
}
|
|
2180
|
-
function startPool(ctx, solver, plan, payload, workers) {
|
|
2827
|
+
function startPool(ctx, solver, plan, payload, workers, spawn = spawnWorker, backstopFloorMs = THREAD_BACKSTOP_FLOOR_MS) {
|
|
2181
2828
|
const entryCount = plan.entryNodes.length;
|
|
2182
2829
|
const spawned = workers - 1;
|
|
2183
2830
|
const reachFloats = entryCount * 2 * COMBOS;
|
|
@@ -2188,6 +2835,12 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2188
2835
|
const control = new Int32Array(controlBuffer);
|
|
2189
2836
|
const threads = [];
|
|
2190
2837
|
let failure = null;
|
|
2838
|
+
const samplingBuffers = ctx.sampling ? [ctx.sampling.baseline, ctx.sampling.lastWalk, ctx.sampling.lastUpdate].map((view) => {
|
|
2839
|
+
if (!(view.buffer instanceof SharedArrayBuffer) || view.byteOffset !== 0 || view.byteLength !== view.buffer.byteLength) {
|
|
2840
|
+
throw new Error("chanceSampling on worker threads needs its state in shared buffers of its own");
|
|
2841
|
+
}
|
|
2842
|
+
return view.buffer;
|
|
2843
|
+
}) : null;
|
|
2191
2844
|
for (let w = 0; w < spawned; w++) {
|
|
2192
2845
|
const setup = {
|
|
2193
2846
|
workerId: w,
|
|
@@ -2199,11 +2852,17 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2199
2852
|
strategySum: ctx.strategySum.buffer,
|
|
2200
2853
|
regretScale: ctx.regretScale?.buffer ?? null,
|
|
2201
2854
|
sumScale: ctx.sumScale?.buffer ?? null,
|
|
2855
|
+
compactRegret: ctx.compactRegret?.map((chunk) => chunk.buffer) ?? null,
|
|
2856
|
+
handShift: ctx.handShift?.map((chunk) => chunk.buffer) ?? null,
|
|
2857
|
+
compactSum: ctx.compactSum?.map((chunk) => chunk.buffer) ?? null,
|
|
2858
|
+
sumShift: ctx.sumShift?.map((chunk) => chunk.buffer) ?? null,
|
|
2859
|
+
compactScale: ctx.compactScale?.buffer ?? null,
|
|
2860
|
+
sampling: samplingBuffers ? [samplingBuffers[0], samplingBuffers[1], samplingBuffers[2]] : null,
|
|
2202
2861
|
control: controlBuffer,
|
|
2203
2862
|
exchange: exchangeBuffer,
|
|
2204
2863
|
entryCount
|
|
2205
2864
|
};
|
|
2206
|
-
const thread =
|
|
2865
|
+
const thread = spawn(setup, (error) => {
|
|
2207
2866
|
failure ??= error;
|
|
2208
2867
|
Atomics.store(control, CONTROL.ready, spawned);
|
|
2209
2868
|
Atomics.add(control, CONTROL.done, spawned);
|
|
@@ -2227,6 +2886,7 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2227
2886
|
const chanceValues = new Float64Array(entryCount * COMBOS);
|
|
2228
2887
|
const mainReachMe = new Float64Array(COMBOS);
|
|
2229
2888
|
const mainReachOpp = new Float64Array(COMBOS);
|
|
2889
|
+
const failed = () => failure ?? (Atomics.load(control, CONTROL.failed) ? new Error(THREAD_FAILED_ERROR) : null);
|
|
2230
2890
|
const dispatch = (mode, traverser, iterWeight) => {
|
|
2231
2891
|
Atomics.store(control, CONTROL.done, 0);
|
|
2232
2892
|
Atomics.store(control, CONTROL.mode, mode);
|
|
@@ -2235,6 +2895,7 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2235
2895
|
Atomics.store(control, CONTROL.command, COMMAND.work);
|
|
2236
2896
|
Atomics.add(control, CONTROL.generation, 1);
|
|
2237
2897
|
Atomics.notify(control, CONTROL.generation);
|
|
2898
|
+
const dispatched = performance.now();
|
|
2238
2899
|
const fullStart = ctx.shardStart;
|
|
2239
2900
|
const fullEnd = ctx.shardEnd;
|
|
2240
2901
|
ctx.shardStart = plan.bounds[0];
|
|
@@ -2260,43 +2921,49 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2260
2921
|
ctx.shardStart = fullStart;
|
|
2261
2922
|
ctx.shardEnd = fullEnd;
|
|
2262
2923
|
}
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2924
|
+
failure ??= awaitThreads(control, CONTROL.done, spawned, dispatched, backstopFloorMs, () => failure !== null);
|
|
2925
|
+
const dead = failed();
|
|
2926
|
+
if (dead) throw dead;
|
|
2927
|
+
};
|
|
2928
|
+
const traverseAll = (mode, traverser, iterWeight) => {
|
|
2929
|
+
ctx.applyUpdates = false;
|
|
2930
|
+
ctx.chanceOverride = null;
|
|
2931
|
+
ctx.chanceCollect = (node, reachMe, reachOpp) => {
|
|
2932
|
+
const entry = plan.entryIndexOf[node];
|
|
2933
|
+
exchange.set(reachMe.subarray(0, COMBOS), entry * 2 * COMBOS);
|
|
2934
|
+
exchange.set(reachOpp.subarray(0, COMBOS), entry * 2 * COMBOS + COMBOS);
|
|
2935
|
+
};
|
|
2936
|
+
try {
|
|
2937
|
+
solver.traverseRoot(mode, traverser, iterWeight);
|
|
2938
|
+
} finally {
|
|
2939
|
+
ctx.chanceCollect = null;
|
|
2268
2940
|
}
|
|
2269
|
-
|
|
2941
|
+
dispatch(mode, traverser, iterWeight);
|
|
2942
|
+
for (let w = 0; w < spawned; w++) {
|
|
2943
|
+
const base = reachFloats + w * entryCount * COMBOS;
|
|
2944
|
+
for (let i = 0; i < entryCount * COMBOS; i++) chanceValues[i] += exchange[base + i];
|
|
2945
|
+
}
|
|
2946
|
+
ctx.chanceOverride = chanceValues;
|
|
2947
|
+
ctx.applyUpdates = true;
|
|
2948
|
+
try {
|
|
2949
|
+
const value = solver.traverseRoot(mode, traverser, iterWeight);
|
|
2950
|
+
total.set(value.subarray(0, COMBOS));
|
|
2951
|
+
} finally {
|
|
2952
|
+
ctx.chanceOverride = null;
|
|
2953
|
+
}
|
|
2954
|
+
return total;
|
|
2270
2955
|
};
|
|
2271
2956
|
return {
|
|
2272
2957
|
workers,
|
|
2273
2958
|
traverse(mode, traverser, iterWeight) {
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
ctx.chanceCollect = (node, reachMe, reachOpp) => {
|
|
2277
|
-
const entry = plan.entryIndexOf[node];
|
|
2278
|
-
exchange.set(reachMe.subarray(0, COMBOS), entry * 2 * COMBOS);
|
|
2279
|
-
exchange.set(reachOpp.subarray(0, COMBOS), entry * 2 * COMBOS + COMBOS);
|
|
2280
|
-
};
|
|
2959
|
+
const dead = failed();
|
|
2960
|
+
if (dead) throw dead;
|
|
2281
2961
|
try {
|
|
2282
|
-
|
|
2283
|
-
}
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
dispatch(mode, traverser, iterWeight);
|
|
2287
|
-
for (let w = 0; w < spawned; w++) {
|
|
2288
|
-
const base = reachFloats + w * entryCount * COMBOS;
|
|
2289
|
-
for (let i = 0; i < entryCount * COMBOS; i++) chanceValues[i] += exchange[base + i];
|
|
2290
|
-
}
|
|
2291
|
-
ctx.chanceOverride = chanceValues;
|
|
2292
|
-
ctx.applyUpdates = true;
|
|
2293
|
-
try {
|
|
2294
|
-
const value = solver.traverseRoot(mode, traverser, iterWeight);
|
|
2295
|
-
total.set(value.subarray(0, COMBOS));
|
|
2296
|
-
} finally {
|
|
2297
|
-
ctx.chanceOverride = null;
|
|
2962
|
+
return traverseAll(mode, traverser, iterWeight);
|
|
2963
|
+
} catch (error) {
|
|
2964
|
+
Atomics.store(control, CONTROL.failed, 1);
|
|
2965
|
+
throw error;
|
|
2298
2966
|
}
|
|
2299
|
-
return total;
|
|
2300
2967
|
},
|
|
2301
2968
|
close() {
|
|
2302
2969
|
Atomics.store(control, CONTROL.command, COMMAND.exit);
|
|
@@ -2311,25 +2978,40 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2311
2978
|
var wasm2 = globalThis.WebAssembly;
|
|
2312
2979
|
var modules = { plain: void 0, shared: void 0 };
|
|
2313
2980
|
function compiledEngine(kind) {
|
|
2314
|
-
|
|
2315
|
-
|
|
2981
|
+
return compileInto(modules, kind, kind === "plain" ? ENGINE_WASM : ENGINE_SHARED_WASM);
|
|
2982
|
+
}
|
|
2983
|
+
function compileInto(cache, kind, text) {
|
|
2984
|
+
if (cache[kind] !== void 0) return cache[kind];
|
|
2985
|
+
cache[kind] = null;
|
|
2316
2986
|
try {
|
|
2317
2987
|
if (!wasm2) return null;
|
|
2318
|
-
const text = kind === "plain" ? ENGINE_WASM : ENGINE_SHARED_WASM;
|
|
2319
2988
|
const bytes = Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
|
|
2320
2989
|
if (!wasm2.validate(bytes)) return null;
|
|
2321
|
-
|
|
2990
|
+
cache[kind] = new wasm2.Module(bytes);
|
|
2322
2991
|
} catch {
|
|
2323
|
-
|
|
2992
|
+
cache[kind] = null;
|
|
2324
2993
|
}
|
|
2325
|
-
return
|
|
2994
|
+
return cache[kind];
|
|
2995
|
+
}
|
|
2996
|
+
var storageModules = /* @__PURE__ */ new WeakMap();
|
|
2997
|
+
function compiledStorage(source, kind) {
|
|
2998
|
+
let cache = storageModules.get(source);
|
|
2999
|
+
if (!cache) storageModules.set(source, cache = { plain: void 0, shared: void 0 });
|
|
3000
|
+
return compileInto(cache, kind, source[kind]);
|
|
2326
3001
|
}
|
|
2327
|
-
var
|
|
3002
|
+
var MAX_MEMORY = 4294901760;
|
|
3003
|
+
var ENGINE_CONTROL = { generation: 0, done: 1, command: 2, mode: 3, traverser: 4, ready: 5, failed: 6, size: 8 };
|
|
2328
3004
|
function createEngine(input, pool) {
|
|
2329
3005
|
const shared = pool !== void 0;
|
|
2330
|
-
const
|
|
3006
|
+
const storage = input.storage ?? null;
|
|
3007
|
+
const compiledModule = storage ? compiledStorage(storage.source, shared ? "shared" : "plain") : compiledEngine(shared ? "shared" : "plain");
|
|
2331
3008
|
if (!compiledModule || !wasm2) return null;
|
|
2332
3009
|
const { tree, runouts, layout, active, hands } = input;
|
|
3010
|
+
const compact = storage?.compact ?? null;
|
|
3011
|
+
const quantised = storage?.quantised ?? false;
|
|
3012
|
+
const width = quantised ? 2 : 4;
|
|
3013
|
+
const compactBytes = !compact ? 0 : compact.float ? layout.compactSlots * 4 : (layout.compactSlots + (compact.perHand ? layout.compactHands : 0)) * (compact.sums ? 2 : 1);
|
|
3014
|
+
if (layout.totalSlots * width * 2 + compactBytes > MAX_MEMORY || !(layout.offset instanceof Int32Array)) return null;
|
|
2333
3015
|
const threads = pool ? pool.threads : 1;
|
|
2334
3016
|
const flatten = (parts) => {
|
|
2335
3017
|
const bases = new Int32Array(parts.length + 1);
|
|
@@ -2368,8 +3050,11 @@ function createEngine(input, pool) {
|
|
|
2368
3050
|
add(active.cardA),
|
|
2369
3051
|
add(active.cardB),
|
|
2370
3052
|
add(active.strength),
|
|
2371
|
-
add(active.
|
|
2372
|
-
add(active.
|
|
3053
|
+
add(active.packed),
|
|
3054
|
+
add(Float32Array.from(active.multiplicity)),
|
|
3055
|
+
add(active.memberStart),
|
|
3056
|
+
add(active.memberA),
|
|
3057
|
+
add(active.memberB),
|
|
2373
3058
|
add(upRound),
|
|
2374
3059
|
add(upStart),
|
|
2375
3060
|
add(hands.up),
|
|
@@ -2380,13 +3065,13 @@ function createEngine(input, pool) {
|
|
|
2380
3065
|
add(orbitRound),
|
|
2381
3066
|
add(orbitStart),
|
|
2382
3067
|
add(input.runoutWeight),
|
|
2383
|
-
add(null, layout.totalSlots *
|
|
3068
|
+
add(null, layout.totalSlots * width),
|
|
2384
3069
|
// regret
|
|
2385
|
-
add(null, layout.totalSlots *
|
|
3070
|
+
add(null, layout.totalSlots * width)
|
|
2386
3071
|
// strategy sums
|
|
2387
3072
|
];
|
|
2388
|
-
const REGRET = common[
|
|
2389
|
-
const SUMS = common[
|
|
3073
|
+
const REGRET = common[common.length - 2];
|
|
3074
|
+
const SUMS = common[common.length - 1];
|
|
2390
3075
|
const own = Array.from({ length: threads }, () => ({
|
|
2391
3076
|
buffers: add(null, depths * depthBytes),
|
|
2392
3077
|
cardSums: add(null, 52 * 8 + stride * 8),
|
|
@@ -2399,13 +3084,43 @@ function createEngine(input, pool) {
|
|
|
2399
3084
|
const reach = pool ? add(null, entries * 2 * rowBytes) : -1;
|
|
2400
3085
|
const values = pool ? add(null, entries * rowBytes) : -1;
|
|
2401
3086
|
const partials = pool ? add(null, (threads - 1) * entries * rowBytes) : -1;
|
|
3087
|
+
const plan = input.sampling;
|
|
3088
|
+
const sample = plan ? {
|
|
3089
|
+
baseOffset: add(plan.baseOffset),
|
|
3090
|
+
boardOffset: add(plan.boardOffset),
|
|
3091
|
+
walkOffset: add(plan.walkOffset),
|
|
3092
|
+
blockOffset: add(layout.blockOffset),
|
|
3093
|
+
baseline: add(null, plan.values * 4),
|
|
3094
|
+
lastWalk: add(null, plan.walks * 4),
|
|
3095
|
+
lastUpdate: add(null, plan.blocks * 4)
|
|
3096
|
+
} : null;
|
|
3097
|
+
const share = layout.shared ? {
|
|
3098
|
+
marks: add(null, layout.blocks * 4),
|
|
3099
|
+
blockOffset: sample ? sample.blockOffset : add(layout.blockOffset),
|
|
3100
|
+
weighted: layout.reachWeighted ? add(layout.reachWeighted) : -1
|
|
3101
|
+
} : null;
|
|
3102
|
+
const chunks = compact ? layout.chunkSlots.length : 0;
|
|
3103
|
+
const stored = storage ? {
|
|
3104
|
+
block: add(null, 128),
|
|
3105
|
+
blockOffset: sample ? sample.blockOffset : share ? share.blockOffset : add(layout.blockOffset),
|
|
3106
|
+
regretScale: quantised ? add(null, layout.blocks * 8) : -1,
|
|
3107
|
+
sumScale: quantised ? add(null, layout.blocks * 8) : -1,
|
|
3108
|
+
compactChunk: compact ? add(layout.compactChunk) : -1,
|
|
3109
|
+
handOffset: compact ? add(Int32Array.from(layout.handOffset)) : -1,
|
|
3110
|
+
tables: compact ? add(null, chunks * 4 * 5) : -1,
|
|
3111
|
+
regrets: layout.chunkSlots.map((slots) => add(null, slots * (compact.float ? 4 : 1))),
|
|
3112
|
+
shifts: compact?.perHand ? layout.chunkHands.map((count) => add(null, count)) : [],
|
|
3113
|
+
sums: compact?.sums ? layout.chunkSlots.map((slots) => add(null, slots)) : [],
|
|
3114
|
+
sumShifts: compact?.sums ? layout.chunkHands.map((count) => add(null, count)) : [],
|
|
3115
|
+
compactScale: compact && !compact.perHand && !compact.float ? add(null, layout.blocks * 8) : -1
|
|
3116
|
+
} : null;
|
|
2402
3117
|
const offsets = [];
|
|
2403
|
-
let total = 64;
|
|
3118
|
+
let total = 64 + Math.ceil(Math.max(input.lowBytes ?? 0, 0) / 16) * 16;
|
|
2404
3119
|
for (const piece of pieces) {
|
|
2405
3120
|
offsets.push(total);
|
|
2406
3121
|
total += piece.bytes;
|
|
2407
3122
|
}
|
|
2408
|
-
if (total >
|
|
3123
|
+
if (total > MAX_MEMORY) return null;
|
|
2409
3124
|
const pages = Math.ceil(total / 65536) + 1;
|
|
2410
3125
|
const memory = shared ? new wasm2.Memory({ initial: pages, maximum: pages, shared: true }) : new wasm2.Memory({ initial: pages });
|
|
2411
3126
|
const bytes = new Uint8Array(memory.buffer);
|
|
@@ -2415,20 +3130,91 @@ function createEngine(input, pool) {
|
|
|
2415
3130
|
for (const mine of own) {
|
|
2416
3131
|
const head = new Uint32Array(memory.buffer, offsets[mine.header], 40);
|
|
2417
3132
|
common.forEach((slot, i) => head[i] = offsets[slot]);
|
|
2418
|
-
head[
|
|
2419
|
-
head[
|
|
2420
|
-
head[
|
|
2421
|
-
head[
|
|
2422
|
-
head[
|
|
3133
|
+
head[common.length] = offsets[mine.buffers];
|
|
3134
|
+
head[common.length + 1] = depthBytes;
|
|
3135
|
+
head[common.length + 2] = rowBytes;
|
|
3136
|
+
head[common.length + 3] = offsets[mine.cardSums];
|
|
3137
|
+
head[common.length + 4] = input.chipScale;
|
|
3138
|
+
if (stored) head[common.length + 5] = offsets[stored.block];
|
|
3139
|
+
}
|
|
3140
|
+
if (stored) {
|
|
3141
|
+
const at = (slot) => slot >= 0 ? offsets[slot] : 0;
|
|
3142
|
+
const block = new Uint32Array(memory.buffer, offsets[stored.block], 20);
|
|
3143
|
+
block.set([
|
|
3144
|
+
quantised ? 1 : 0,
|
|
3145
|
+
compact ? compact.from : 255,
|
|
3146
|
+
!compact ? 0 : compact.float ? 3 : compact.perHand ? 1 : 2,
|
|
3147
|
+
compact?.sums ? 1 : 0,
|
|
3148
|
+
compact?.stochastic ? 1 : 0,
|
|
3149
|
+
compact?.steps ?? 127,
|
|
3150
|
+
at(stored.regretScale),
|
|
3151
|
+
at(stored.sumScale),
|
|
3152
|
+
offsets[stored.blockOffset],
|
|
3153
|
+
at(stored.compactChunk),
|
|
3154
|
+
at(stored.handOffset)
|
|
3155
|
+
]);
|
|
3156
|
+
if (compact) {
|
|
3157
|
+
const tables = offsets[stored.tables];
|
|
3158
|
+
const table = new Uint32Array(memory.buffer, tables, chunks * 5);
|
|
3159
|
+
for (let c = 0; c < chunks; c++) {
|
|
3160
|
+
table[c] = offsets[stored.regrets[c]];
|
|
3161
|
+
table[chunks + c] = stored.shifts.length ? offsets[stored.shifts[c]] : 0;
|
|
3162
|
+
table[2 * chunks + c] = stored.sums.length ? offsets[stored.sums[c]] : 0;
|
|
3163
|
+
table[3 * chunks + c] = stored.sumShifts.length ? offsets[stored.sumShifts[c]] : 0;
|
|
3164
|
+
table[4 * chunks + c] = layout.chunkStart[c] >>> 0;
|
|
3165
|
+
}
|
|
3166
|
+
block.set([tables, tables + chunks * 4, tables + chunks * 8, tables + chunks * 12, tables + chunks * 16, at(stored.compactScale)], 11);
|
|
3167
|
+
}
|
|
2423
3168
|
}
|
|
3169
|
+
const factors = stored ? new Float64Array(memory.buffer, offsets[stored.block] + 80, 4) : null;
|
|
2424
3170
|
const instance = new wasm2.Instance(compiledModule, { env: { memory, abort: () => {
|
|
2425
3171
|
throw new Error("engine abort");
|
|
2426
3172
|
} } });
|
|
2427
3173
|
const exports = instance.exports;
|
|
2428
3174
|
exports.init(offsets[own[0].header]);
|
|
2429
3175
|
exports.setMaxActions(maxActions);
|
|
2430
|
-
|
|
2431
|
-
|
|
3176
|
+
let samplingViews = null;
|
|
3177
|
+
let samplingArgs = null;
|
|
3178
|
+
if (plan && sample) {
|
|
3179
|
+
let mask = 0;
|
|
3180
|
+
for (let r = 0; r < plan.rounds.length; r++) if (plan.rounds[r]) mask |= 1 << r;
|
|
3181
|
+
samplingArgs = [
|
|
3182
|
+
mask,
|
|
3183
|
+
plan.p,
|
|
3184
|
+
plan.every,
|
|
3185
|
+
plan.unbiased ? 1 : 0,
|
|
3186
|
+
plan.catchUp ? 1 : 0,
|
|
3187
|
+
offsets[sample.baseOffset],
|
|
3188
|
+
offsets[sample.boardOffset],
|
|
3189
|
+
offsets[sample.walkOffset],
|
|
3190
|
+
offsets[sample.baseline],
|
|
3191
|
+
offsets[sample.lastWalk],
|
|
3192
|
+
offsets[sample.lastUpdate],
|
|
3193
|
+
offsets[sample.blockOffset]
|
|
3194
|
+
];
|
|
3195
|
+
const a = samplingArgs;
|
|
3196
|
+
exports.setSampling(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11]);
|
|
3197
|
+
samplingViews = {
|
|
3198
|
+
baseline: new Float32Array(memory.buffer, offsets[sample.baseline], plan.values),
|
|
3199
|
+
lastWalk: new Int32Array(memory.buffer, offsets[sample.lastWalk], plan.walks),
|
|
3200
|
+
lastUpdate: new Int32Array(memory.buffer, offsets[sample.lastUpdate], plan.blocks)
|
|
3201
|
+
};
|
|
3202
|
+
}
|
|
3203
|
+
const weightedAt = share && share.weighted >= 0 ? offsets[share.weighted] : 0;
|
|
3204
|
+
if (share) exports.setShared(offsets[share.marks], offsets[share.blockOffset], weightedAt);
|
|
3205
|
+
const discounted = share ? new Int32Array(memory.buffer, offsets[share.marks], layout.blocks) : null;
|
|
3206
|
+
const regret = quantised ? new Int16Array(memory.buffer, offsets[REGRET], layout.totalSlots) : new Float32Array(memory.buffer, offsets[REGRET], layout.totalSlots);
|
|
3207
|
+
const strategySum = quantised ? new Uint16Array(memory.buffer, offsets[SUMS], layout.totalSlots) : new Float32Array(memory.buffer, offsets[SUMS], layout.totalSlots);
|
|
3208
|
+
const scaleView = (slot) => slot >= 0 ? new Float64Array(memory.buffer, offsets[slot], layout.blocks) : null;
|
|
3209
|
+
const compactViews = stored && compact ? {
|
|
3210
|
+
regret: stored.regrets.map(
|
|
3211
|
+
(slot, c) => compact.float ? new Float32Array(memory.buffer, offsets[slot], layout.chunkSlots[c]) : new Int8Array(memory.buffer, offsets[slot], layout.chunkSlots[c])
|
|
3212
|
+
),
|
|
3213
|
+
handShift: compact.perHand ? stored.shifts.map((slot, c) => new Uint8Array(memory.buffer, offsets[slot], layout.chunkHands[c])) : null,
|
|
3214
|
+
sum: compact.sums ? stored.sums.map((slot, c) => new Uint8Array(memory.buffer, offsets[slot], layout.chunkSlots[c])) : null,
|
|
3215
|
+
sumShift: compact.sums ? stored.sumShifts.map((slot, c) => new Uint8Array(memory.buffer, offsets[slot], layout.chunkHands[c])) : null,
|
|
3216
|
+
scale: scaleView(stored.compactScale)
|
|
3217
|
+
} : null;
|
|
2432
3218
|
const me = new Float32Array(memory.buffer, offsets[own[0].rootMe], stride);
|
|
2433
3219
|
const opp = new Float32Array(memory.buffer, offsets[own[0].rootOpp], stride);
|
|
2434
3220
|
const result = new Float64Array(COMBOS);
|
|
@@ -2448,7 +3234,7 @@ function createEngine(input, pool) {
|
|
|
2448
3234
|
const workers = [];
|
|
2449
3235
|
let failure = null;
|
|
2450
3236
|
if (pool) {
|
|
2451
|
-
const
|
|
3237
|
+
const plan2 = pool.plan;
|
|
2452
3238
|
const controlBuffer = new SharedArrayBuffer(ENGINE_CONTROL.size * 4);
|
|
2453
3239
|
const settingsBuffer = new SharedArrayBuffer(8 * 8);
|
|
2454
3240
|
control = new Int32Array(controlBuffer);
|
|
@@ -2461,14 +3247,17 @@ function createEngine(input, pool) {
|
|
|
2461
3247
|
header: offsets[own[w].header],
|
|
2462
3248
|
maxActions,
|
|
2463
3249
|
exchange: [offsets[entryIndexOf], offsets[reach], offsets[values], stride],
|
|
2464
|
-
shard: [
|
|
2465
|
-
entryNodes:
|
|
3250
|
+
shard: [plan2.round, plan2.bounds[w], plan2.bounds[w + 1]],
|
|
3251
|
+
entryNodes: plan2.entryNodes,
|
|
2466
3252
|
rootMe: offsets[own[w].rootMe],
|
|
2467
3253
|
rootOpp: offsets[own[w].rootOpp],
|
|
2468
3254
|
partial: offsets[partials] + (w - 1) * entries * rowBytes,
|
|
2469
3255
|
stride,
|
|
2470
3256
|
control: controlBuffer,
|
|
2471
|
-
settings: settingsBuffer
|
|
3257
|
+
settings: settingsBuffer,
|
|
3258
|
+
...share ? { shared: [offsets[share.marks], offsets[share.blockOffset], weightedAt] } : {},
|
|
3259
|
+
...stored ? { storage: true } : {},
|
|
3260
|
+
...samplingArgs ? { sampling: samplingArgs } : {}
|
|
2472
3261
|
};
|
|
2473
3262
|
workers.push(pool.spawn(setup, (error) => {
|
|
2474
3263
|
failure ??= error;
|
|
@@ -2490,7 +3279,14 @@ function createEngine(input, pool) {
|
|
|
2490
3279
|
throw failure;
|
|
2491
3280
|
}
|
|
2492
3281
|
}
|
|
2493
|
-
const
|
|
3282
|
+
const failed = () => failure ?? (control && Atomics.load(control, ENGINE_CONTROL.failed) ? new Error(THREAD_FAILED_ERROR) : null);
|
|
3283
|
+
let iteration = 0;
|
|
3284
|
+
let dcfrRule = 0;
|
|
3285
|
+
const run = (asked, traverser, iterWeight, dcfr) => {
|
|
3286
|
+
const dead = failed();
|
|
3287
|
+
if (dead) throw dead;
|
|
3288
|
+
const apply = asked === Mode.Current ? 0 : 1;
|
|
3289
|
+
const mode = asked === Mode.Current ? Mode.Train : asked;
|
|
2494
3290
|
for (const player of [0, 1]) {
|
|
2495
3291
|
const [from, count2] = rootList(player);
|
|
2496
3292
|
const into = player === traverser ? me : opp;
|
|
@@ -2498,53 +3294,81 @@ function createEngine(input, pool) {
|
|
|
2498
3294
|
for (let i = 0; i < count2; i++) into[i] = weights[active.list[from + i]];
|
|
2499
3295
|
}
|
|
2500
3296
|
const [positive, negative, sum, weight, clamp] = settingsFor(mode, iterWeight, dcfr);
|
|
3297
|
+
iteration = mode === Mode.Train ? iterWeight : 0;
|
|
3298
|
+
dcfrRule = dcfr ? 1 : 0;
|
|
3299
|
+
if (plan || share || stored) exports.setIteration(iteration, dcfrRule);
|
|
3300
|
+
factors?.set([positive, mode === Mode.Train && !dcfr ? 0 : negative, sum, weight]);
|
|
2501
3301
|
let at;
|
|
2502
3302
|
if (!pool) {
|
|
2503
|
-
|
|
3303
|
+
if (apply === 0) exports.setPhase(0, 0);
|
|
3304
|
+
try {
|
|
3305
|
+
at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp) >>> 0;
|
|
3306
|
+
} finally {
|
|
3307
|
+
if (apply === 0) exports.setPhase(0, 1);
|
|
3308
|
+
}
|
|
2504
3309
|
} else {
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
Atomics.store(control, ENGINE_CONTROL.mode, mode);
|
|
2512
|
-
Atomics.store(control, ENGINE_CONTROL.traverser, traverser);
|
|
2513
|
-
Atomics.store(control, ENGINE_CONTROL.command, 0);
|
|
2514
|
-
Atomics.add(control, ENGINE_CONTROL.generation, 1);
|
|
2515
|
-
Atomics.notify(control, ENGINE_CONTROL.generation);
|
|
2516
|
-
exports.setShard(plan.round, plan.bounds[0], plan.bounds[1]);
|
|
2517
|
-
exports.setPhase(0, 1);
|
|
2518
|
-
const valueView = new Float32Array(memory.buffer, offsets[values], entries * stride);
|
|
2519
|
-
for (let entry = 0; entry < entries; entry++) {
|
|
2520
|
-
const slot = offsets[reach] + entry * 2 * rowBytes;
|
|
2521
|
-
const out = exports.runChance(mode, traverser, plan.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp);
|
|
2522
|
-
valueView.set(new Float32Array(memory.buffer, out, stride), entry * stride);
|
|
2523
|
-
}
|
|
2524
|
-
for (; ; ) {
|
|
2525
|
-
const done = Atomics.load(control, ENGINE_CONTROL.done);
|
|
2526
|
-
if (done >= threads - 1 || failure) break;
|
|
2527
|
-
Atomics.wait(control, ENGINE_CONTROL.done, done, 50);
|
|
2528
|
-
}
|
|
2529
|
-
if (failure) throw failure;
|
|
2530
|
-
const partialView = new Float32Array(memory.buffer, offsets[partials], (threads - 1) * entries * stride);
|
|
2531
|
-
for (let w = 0; w < threads - 1; w++) {
|
|
2532
|
-
for (let i = 0; i < entries * stride; i++) valueView[i] += partialView[w * entries * stride + i];
|
|
2533
|
-
}
|
|
2534
|
-
exports.setShard(plan.round, 0, 0);
|
|
2535
|
-
exports.setPhase(2, 1);
|
|
2536
|
-
at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
|
|
2537
|
-
exports.setPhase(0, 1);
|
|
2538
|
-
exports.setShard(-1, 0, 0);
|
|
3310
|
+
try {
|
|
3311
|
+
at = runThreaded(mode, traverser, apply, positive, negative, sum, weight, clamp);
|
|
3312
|
+
} catch (error) {
|
|
3313
|
+
Atomics.store(control, ENGINE_CONTROL.failed, 1);
|
|
3314
|
+
throw error;
|
|
3315
|
+
}
|
|
2539
3316
|
}
|
|
2540
3317
|
const [, count] = rootList(traverser);
|
|
2541
3318
|
const values32 = new Float32Array(memory.buffer, at, count);
|
|
2542
3319
|
for (let i = 0; i < count; i++) result[i] = values32[i];
|
|
2543
3320
|
return result;
|
|
2544
3321
|
};
|
|
3322
|
+
const runThreaded = (mode, traverser, apply, positive, negative, sum, weight, clamp) => {
|
|
3323
|
+
const plan2 = pool.plan;
|
|
3324
|
+
exports.setShard(plan2.round, 0, 0);
|
|
3325
|
+
exports.setPhase(1, 0);
|
|
3326
|
+
exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
|
|
3327
|
+
settings.set([positive, negative, sum, weight, clamp, apply, iteration, dcfrRule]);
|
|
3328
|
+
Atomics.store(control, ENGINE_CONTROL.done, 0);
|
|
3329
|
+
Atomics.store(control, ENGINE_CONTROL.mode, mode);
|
|
3330
|
+
Atomics.store(control, ENGINE_CONTROL.traverser, traverser);
|
|
3331
|
+
Atomics.store(control, ENGINE_CONTROL.command, 0);
|
|
3332
|
+
Atomics.add(control, ENGINE_CONTROL.generation, 1);
|
|
3333
|
+
Atomics.notify(control, ENGINE_CONTROL.generation);
|
|
3334
|
+
const dispatched = performance.now();
|
|
3335
|
+
exports.setShard(plan2.round, plan2.bounds[0], plan2.bounds[1]);
|
|
3336
|
+
exports.setPhase(0, apply);
|
|
3337
|
+
const valueView = new Float32Array(memory.buffer, offsets[values], entries * stride);
|
|
3338
|
+
for (let entry = 0; entry < entries; entry++) {
|
|
3339
|
+
const slot = offsets[reach] + entry * 2 * rowBytes;
|
|
3340
|
+
const out = exports.runChance(mode, traverser, plan2.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp) >>> 0;
|
|
3341
|
+
valueView.set(new Float32Array(memory.buffer, out, stride), entry * stride);
|
|
3342
|
+
}
|
|
3343
|
+
failure ??= awaitThreads(
|
|
3344
|
+
control,
|
|
3345
|
+
ENGINE_CONTROL.done,
|
|
3346
|
+
threads - 1,
|
|
3347
|
+
dispatched,
|
|
3348
|
+
pool.backstopFloorMs,
|
|
3349
|
+
() => failure !== null
|
|
3350
|
+
);
|
|
3351
|
+
const dead = failed();
|
|
3352
|
+
if (dead) throw dead;
|
|
3353
|
+
const partialView = new Float32Array(memory.buffer, offsets[partials], (threads - 1) * entries * stride);
|
|
3354
|
+
for (let w = 0; w < threads - 1; w++) {
|
|
3355
|
+
for (let i = 0; i < entries * stride; i++) valueView[i] += partialView[w * entries * stride + i];
|
|
3356
|
+
}
|
|
3357
|
+
exports.setShard(plan2.round, 0, 0);
|
|
3358
|
+
exports.setPhase(2, apply);
|
|
3359
|
+
const at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp) >>> 0;
|
|
3360
|
+
exports.setPhase(0, 1);
|
|
3361
|
+
exports.setShard(-1, 0, 0);
|
|
3362
|
+
return at;
|
|
3363
|
+
};
|
|
2545
3364
|
return {
|
|
2546
3365
|
regret,
|
|
2547
3366
|
strategySum,
|
|
3367
|
+
regretScale: stored ? scaleView(stored.regretScale) : null,
|
|
3368
|
+
sumScale: stored ? scaleView(stored.sumScale) : null,
|
|
3369
|
+
compact: compactViews,
|
|
3370
|
+
sampling: samplingViews,
|
|
3371
|
+
discounted,
|
|
2548
3372
|
threads,
|
|
2549
3373
|
root(dcfr) {
|
|
2550
3374
|
return (mode, traverser, iterWeight) => run(mode, traverser, iterWeight, dcfr);
|
|
@@ -2563,7 +3387,7 @@ function createEngine(input, pool) {
|
|
|
2563
3387
|
var SIZE_TOLERANCE = 1.35;
|
|
2564
3388
|
var REGRET_STEPS2 = 32767;
|
|
2565
3389
|
var SUM_STEPS2 = 65535;
|
|
2566
|
-
function matchNodes(to, from) {
|
|
3390
|
+
function matchNodes(to, from, tolerance = SIZE_TOLERANCE) {
|
|
2567
3391
|
const match = new Int32Array(to.nodeCount).fill(-1);
|
|
2568
3392
|
const walk2 = (a, b) => {
|
|
2569
3393
|
if (to.kind[a] !== from.kind[b] || to.round[a] !== from.round[b]) return;
|
|
@@ -2575,14 +3399,14 @@ function matchNodes(to, from) {
|
|
|
2575
3399
|
match[a] = b;
|
|
2576
3400
|
for (let e = 0; e < to.numEdges[a]; e++) {
|
|
2577
3401
|
const edge = to.firstEdge[a] + e;
|
|
2578
|
-
const other = matchEdge(to, edge, a, from, b);
|
|
3402
|
+
const other = matchEdge(to, edge, a, from, b, tolerance);
|
|
2579
3403
|
if (other >= 0) walk2(to.edgeChild[edge], from.edgeChild[other]);
|
|
2580
3404
|
}
|
|
2581
3405
|
};
|
|
2582
3406
|
walk2(0, 0);
|
|
2583
3407
|
return match;
|
|
2584
3408
|
}
|
|
2585
|
-
function matchEdge(to, edge, node, from, other) {
|
|
3409
|
+
function matchEdge(to, edge, node, from, other, tolerance) {
|
|
2586
3410
|
const kind = to.edgeAction[edge];
|
|
2587
3411
|
const sized = kind === ActionKind.Bet || kind === ActionKind.Raise;
|
|
2588
3412
|
const size = to.edgeTarget[edge] / to.pot[node];
|
|
@@ -2598,7 +3422,7 @@ function matchEdge(to, edge, node, from, other) {
|
|
|
2598
3422
|
best = candidate;
|
|
2599
3423
|
}
|
|
2600
3424
|
}
|
|
2601
|
-
return bestGap <= Math.log(
|
|
3425
|
+
return bestGap <= Math.log(tolerance) ? best : -1;
|
|
2602
3426
|
}
|
|
2603
3427
|
function readBlock(store, scale, block, base, length, out) {
|
|
2604
3428
|
const factor = scale ? scale[block] : 1;
|
|
@@ -2618,24 +3442,93 @@ function writeBlock(store, scale, steps, block, base, length, values) {
|
|
|
2618
3442
|
for (let i = 0; i < length; i++) store[base + i] = Math.round(values[i] * step);
|
|
2619
3443
|
scale[block] = largest / steps;
|
|
2620
3444
|
}
|
|
3445
|
+
function readCompactBlock(ctx, node, listSlot, block, base, actions, count, regrets, sums) {
|
|
3446
|
+
const plan = ctx.compact;
|
|
3447
|
+
const chunk = ctx.layout.compactChunk[node];
|
|
3448
|
+
const q = ctx.compactRegret[chunk];
|
|
3449
|
+
const handBase = ctx.layout.handOffset[node] + ctx.layout.rowStart[listSlot];
|
|
3450
|
+
const shifts = plan.perHand ? ctx.handShift[chunk] : null;
|
|
3451
|
+
const blockScale = !plan.perHand && !plan.float ? ctx.compactScale[block] : 1;
|
|
3452
|
+
for (let i = 0; i < count; i++) {
|
|
3453
|
+
const scale = shifts ? SHIFT_SCALE[shifts[handBase + i]] : blockScale;
|
|
3454
|
+
if (regrets) for (let a = 0; a < actions; a++) regrets[a * count + i] = q[base + a * count + i] * scale;
|
|
3455
|
+
if (!plan.sums) continue;
|
|
3456
|
+
const sumScale = SHIFT_SCALE[ctx.sumShift[chunk][handBase + i]];
|
|
3457
|
+
const stored = ctx.compactSum[chunk];
|
|
3458
|
+
for (let a = 0; a < actions; a++) sums[a * count + i] = stored[base + a * count + i] * sumScale;
|
|
3459
|
+
}
|
|
3460
|
+
if (!plan.sums) regretMatch(q, base, actions, count, sums);
|
|
3461
|
+
}
|
|
3462
|
+
function writeCompactBlock(ctx, node, listSlot, block, base, actions, count, regrets, sums) {
|
|
3463
|
+
const plan = ctx.compact;
|
|
3464
|
+
const chunk = ctx.layout.compactChunk[node];
|
|
3465
|
+
const q = ctx.compactRegret[chunk];
|
|
3466
|
+
const handBase = ctx.layout.handOffset[node] + ctx.layout.rowStart[listSlot];
|
|
3467
|
+
const steps = plan.steps;
|
|
3468
|
+
const fit = (value, top, bottom) => {
|
|
3469
|
+
const rounded = Math.round(value);
|
|
3470
|
+
return rounded > top ? top : rounded < bottom ? bottom : rounded;
|
|
3471
|
+
};
|
|
3472
|
+
if (regrets && plan.float) {
|
|
3473
|
+
for (let k = 0; k < actions * count; k++) q[base + k] = regrets[k];
|
|
3474
|
+
} else if (regrets && !plan.perHand) {
|
|
3475
|
+
let largest = 0;
|
|
3476
|
+
for (let k = 0; k < actions * count; k++) largest = Math.max(largest, Math.abs(regrets[k]));
|
|
3477
|
+
const step = largest > 0 ? steps / largest : 0;
|
|
3478
|
+
for (let k = 0; k < actions * count; k++) q[base + k] = fit(regrets[k] * step, steps, -steps);
|
|
3479
|
+
ctx.compactScale[block] = largest / steps;
|
|
3480
|
+
} else if (regrets) {
|
|
3481
|
+
const shifts2 = ctx.handShift[chunk];
|
|
3482
|
+
for (let i = 0; i < count; i++) {
|
|
3483
|
+
let largest = 0;
|
|
3484
|
+
for (let a = 0; a < actions; a++) largest = Math.max(largest, Math.abs(regrets[a * count + i]));
|
|
3485
|
+
const exponent = scaleExponent(largest, steps);
|
|
3486
|
+
shifts2[handBase + i] = exponent;
|
|
3487
|
+
const inverse = exponent === 0 ? 0 : 1 / SHIFT_SCALE[exponent];
|
|
3488
|
+
for (let a = 0; a < actions; a++) q[base + a * count + i] = fit(regrets[a * count + i] * inverse, steps, -steps);
|
|
3489
|
+
}
|
|
3490
|
+
}
|
|
3491
|
+
if (!sums || !plan.sums) return;
|
|
3492
|
+
const stored = ctx.compactSum[chunk];
|
|
3493
|
+
const shifts = ctx.sumShift[chunk];
|
|
3494
|
+
for (let i = 0; i < count; i++) {
|
|
3495
|
+
let largest = 0;
|
|
3496
|
+
for (let a = 0; a < actions; a++) largest = Math.max(largest, sums[a * count + i]);
|
|
3497
|
+
const exponent = scaleExponent(largest, COMPACT_SUM_STEPS);
|
|
3498
|
+
shifts[handBase + i] = exponent;
|
|
3499
|
+
const inverse = exponent === 0 ? 0 : 1 / SHIFT_SCALE[exponent];
|
|
3500
|
+
for (let a = 0; a < actions; a++) stored[base + a * count + i] = fit(sums[a * count + i] * inverse, COMPACT_SUM_STEPS, 0);
|
|
3501
|
+
}
|
|
3502
|
+
}
|
|
2621
3503
|
function realCards(runouts, round, board) {
|
|
2622
3504
|
const width = runouts.width[round];
|
|
2623
3505
|
return Array.from(runouts.cards[round].subarray(board * width, (board + 1) * width));
|
|
2624
3506
|
}
|
|
2625
|
-
function warmStart(to, from, averages) {
|
|
3507
|
+
function warmStart(to, from, averages, cards, tolerance = SIZE_TOLERANCE) {
|
|
3508
|
+
if (!(tolerance >= 1)) throw new Error(`warmStart: the size tolerance is a ratio of at least 1, not ${tolerance}`);
|
|
3509
|
+
if (to.layout.shared && !from.layout.shared) {
|
|
3510
|
+
throw new Error("A spot with shareHistories can only warm-start from a solve that shares histories too");
|
|
3511
|
+
}
|
|
2626
3512
|
const a = to.runouts.initial;
|
|
2627
3513
|
const b = from.runouts.initial;
|
|
2628
|
-
if (
|
|
2629
|
-
|
|
3514
|
+
if (cards) checkRelabelling(cards, a, b);
|
|
3515
|
+
else if (a.length !== b.length || a.some((card, i) => card !== b[i])) {
|
|
3516
|
+
throw new Error("A warm start must come from a solve of the same board, unless `warmCards` relabels one onto the other");
|
|
3517
|
+
}
|
|
3518
|
+
const asSource = (real) => cards ? [...b, ...real.slice(a.length).map((card) => cards[card])] : real;
|
|
3519
|
+
const comboOf = new Uint16Array(COMBOS);
|
|
3520
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
3521
|
+
comboOf[combo] = cards ? comboIndex(cards[COMBO_CARDS[combo * 2]], cards[COMBO_CARDS[combo * 2 + 1]]) : combo;
|
|
2630
3522
|
}
|
|
2631
|
-
const match = matchNodes(to.tree, from.tree);
|
|
3523
|
+
const match = matchNodes(to.tree, from.tree, tolerance);
|
|
2632
3524
|
const { tree } = to;
|
|
2633
3525
|
const width = to.layout.maxActions * COMBOS;
|
|
2634
3526
|
const oldRegret = new Float64Array(width);
|
|
2635
3527
|
const oldSum = new Float64Array(width);
|
|
2636
3528
|
const newRegret = new Float64Array(width);
|
|
2637
3529
|
const newSum = new Float64Array(width);
|
|
2638
|
-
const
|
|
3530
|
+
const keepsRegrets = (ctx) => ctx.regret.length > 0 || (ctx.compactRegret ?? null) !== null;
|
|
3531
|
+
const hasRegret = keepsRegrets(to) && keepsRegrets(from);
|
|
2639
3532
|
const position = new Int32Array(COMBOS).fill(-1);
|
|
2640
3533
|
let matched = 0;
|
|
2641
3534
|
for (const node of tree.decisionNodes) {
|
|
@@ -2648,20 +3541,24 @@ function warmStart(to, from, averages) {
|
|
|
2648
3541
|
const oldActions = from.tree.numEdges[old];
|
|
2649
3542
|
const actionOf = new Int32Array(actions).fill(-1);
|
|
2650
3543
|
for (let e = 0; e < actions; e++) {
|
|
2651
|
-
const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old);
|
|
3544
|
+
const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old, tolerance);
|
|
2652
3545
|
if (other >= 0) actionOf[e] = other - from.tree.firstEdge[old];
|
|
2653
3546
|
}
|
|
2654
3547
|
for (let board = 0; board < to.runouts.counts[round]; board++) {
|
|
2655
|
-
const resolved = resolveBoard(from.runouts, realCards(to.runouts, round, board));
|
|
3548
|
+
const resolved = resolveBoard(from.runouts, asSource(realCards(to.runouts, round, board)));
|
|
2656
3549
|
const oldSlot = (from.active.roundBase[round] + resolved.board) * 2 + owner;
|
|
2657
3550
|
const oldFrom = from.active.start[oldSlot];
|
|
2658
3551
|
const oldCount = from.active.count[oldSlot];
|
|
2659
3552
|
const oldBlock = from.layout.blockOffset[old] + resolved.board;
|
|
2660
3553
|
const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
|
|
2661
3554
|
const oldLength = oldActions * oldCount;
|
|
2662
|
-
if (
|
|
2663
|
-
|
|
2664
|
-
|
|
3555
|
+
if (round >= from.layout.compactFrom) {
|
|
3556
|
+
readCompactBlock(from, old, oldSlot, oldBlock, oldBase, oldActions, oldCount, hasRegret ? oldRegret : null, oldSum);
|
|
3557
|
+
} else {
|
|
3558
|
+
if (hasRegret) readBlock(from.regret, from.regretScale, oldBlock, oldBase, oldLength, oldRegret);
|
|
3559
|
+
readBlock(from.strategySum, from.sumScale, oldBlock, oldBase, oldLength, oldSum);
|
|
3560
|
+
}
|
|
3561
|
+
markMembers(from.active, oldFrom, oldCount, position, false);
|
|
2665
3562
|
const newSlot = (to.active.roundBase[round] + board) * 2 + owner;
|
|
2666
3563
|
const newFrom = to.active.start[newSlot];
|
|
2667
3564
|
const count = to.active.count[newSlot];
|
|
@@ -2669,34 +3566,659 @@ function warmStart(to, from, averages) {
|
|
|
2669
3566
|
newRegret.fill(0, 0, length);
|
|
2670
3567
|
newSum.fill(0, 0, length);
|
|
2671
3568
|
for (let i = 0; i < count; i++) {
|
|
2672
|
-
const
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
3569
|
+
const firstMember = to.active.memberStart[newFrom + i];
|
|
3570
|
+
const membersEnd = to.active.memberStart[newFrom + i + 1];
|
|
3571
|
+
const share = 1 / (membersEnd - firstMember);
|
|
3572
|
+
for (let m = firstMember; m < membersEnd; m++) {
|
|
3573
|
+
const j = position[resolved.combos[comboOf[to.active.memberCombo[m]]]];
|
|
3574
|
+
if (j < 0) continue;
|
|
3575
|
+
for (let e = 0; e < actions; e++) {
|
|
3576
|
+
const action = actionOf[e];
|
|
3577
|
+
if (action < 0) continue;
|
|
3578
|
+
newRegret[e * count + i] += share * oldRegret[action * oldCount + j];
|
|
3579
|
+
newSum[e * count + i] += share * oldSum[action * oldCount + j];
|
|
3580
|
+
}
|
|
2679
3581
|
}
|
|
2680
3582
|
}
|
|
2681
|
-
|
|
3583
|
+
markMembers(from.active, oldFrom, oldCount, position, true);
|
|
2682
3584
|
const block = to.layout.blockOffset[node] + board;
|
|
2683
3585
|
const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
|
|
2684
|
-
if (
|
|
2685
|
-
|
|
2686
|
-
|
|
3586
|
+
if (round >= to.layout.compactFrom) {
|
|
3587
|
+
writeCompactBlock(to, node, newSlot, block, base, actions, count, hasRegret ? newRegret : null, averages ? newSum : null);
|
|
3588
|
+
} else {
|
|
3589
|
+
if (hasRegret) writeBlock(to.regret, to.regretScale, REGRET_STEPS2, block, base, length, newRegret);
|
|
3590
|
+
if (averages) writeBlock(to.strategySum, to.sumScale, SUM_STEPS2, block, base, length, newSum);
|
|
3591
|
+
}
|
|
2687
3592
|
}
|
|
2688
3593
|
}
|
|
2689
3594
|
return { matchedNodes: matched, decisionNodes: tree.decisionNodes.length, averages };
|
|
2690
3595
|
}
|
|
3596
|
+
function checkRelabelling(cards, a, b) {
|
|
3597
|
+
const seen = new Uint8Array(DECK);
|
|
3598
|
+
if (cards.length !== DECK) throw new Error(`A card relabelling maps all ${DECK} cards; got ${cards.length}`);
|
|
3599
|
+
for (const card of cards) {
|
|
3600
|
+
if (card >= DECK || seen[card]) throw new Error("A card relabelling must be a permutation of the deck");
|
|
3601
|
+
seen[card] = 1;
|
|
3602
|
+
}
|
|
3603
|
+
const target = new Set(b);
|
|
3604
|
+
if (a.length !== b.length || a.some((card) => !target.has(cards[card]))) {
|
|
3605
|
+
throw new Error("The card relabelling must take the board onto the warm start's board");
|
|
3606
|
+
}
|
|
3607
|
+
}
|
|
2691
3608
|
|
|
2692
|
-
// src/
|
|
3609
|
+
// src/settle.ts
|
|
3610
|
+
function rootPlayNodes(solution) {
|
|
3611
|
+
const { tree } = solution.spot;
|
|
3612
|
+
if (tree.kind[0] !== NodeKind.Decision) return [];
|
|
3613
|
+
const nodes = [0];
|
|
3614
|
+
for (let e = 0; e < tree.numEdges[0]; e++) {
|
|
3615
|
+
const edge = tree.firstEdge[0] + e;
|
|
3616
|
+
const child = tree.edgeChild[edge];
|
|
3617
|
+
if (tree.edgeAction[edge] === ActionKind.Check && tree.kind[child] === NodeKind.Decision) nodes.push(child);
|
|
3618
|
+
}
|
|
3619
|
+
return nodes;
|
|
3620
|
+
}
|
|
3621
|
+
function rootPlay(solution) {
|
|
3622
|
+
const views = rootPlayNodes(solution).map((node) => solution.strategyAt(node, 0));
|
|
3623
|
+
return { views, weights: views.map((view) => solution.spot.players[view.player].weights) };
|
|
3624
|
+
}
|
|
3625
|
+
function rootPlayMovement(before, after) {
|
|
3626
|
+
if (before.views.length !== after.views.length) throw new Error("Root plays of different spots cannot be compared");
|
|
3627
|
+
let worst = 0;
|
|
3628
|
+
for (let d = 0; d < after.views.length; d++) {
|
|
3629
|
+
const a = before.views[d];
|
|
3630
|
+
const b = after.views[d];
|
|
3631
|
+
const actions = b.actions.length;
|
|
3632
|
+
if (a.node !== b.node || a.actions.length !== actions || a.combos.length !== b.combos.length) {
|
|
3633
|
+
throw new Error("Root plays of different spots cannot be compared");
|
|
3634
|
+
}
|
|
3635
|
+
const weights = after.weights[d];
|
|
3636
|
+
let moved = 0;
|
|
3637
|
+
let mass = 0;
|
|
3638
|
+
for (let i = 0; i < b.combos.length; i++) {
|
|
3639
|
+
const w = weights[b.combos[i]];
|
|
3640
|
+
if (!(w > 0)) continue;
|
|
3641
|
+
let tv = 0;
|
|
3642
|
+
for (let k = 0; k < actions; k++) tv += Math.abs(b.probs[i * actions + k] - a.probs[i * actions + k]);
|
|
3643
|
+
moved += w * tv / 2;
|
|
3644
|
+
mass += w;
|
|
3645
|
+
}
|
|
3646
|
+
if (mass > 0) worst = Math.max(worst, moved / mass);
|
|
3647
|
+
}
|
|
3648
|
+
return worst;
|
|
3649
|
+
}
|
|
3650
|
+
function settlePlan(options) {
|
|
3651
|
+
const plan = {
|
|
3652
|
+
every: options.every ?? 20,
|
|
3653
|
+
tolerance: options.tolerance ?? 0.01,
|
|
3654
|
+
patience: options.patience ?? 2,
|
|
3655
|
+
minIterations: options.minIterations ?? 0
|
|
3656
|
+
};
|
|
3657
|
+
if (!(Number.isInteger(plan.every) && plan.every > 0)) throw new Error(`settle.every must be a positive whole number; got ${plan.every}`);
|
|
3658
|
+
if (!(plan.tolerance >= 0)) throw new Error(`settle.tolerance must be 0 or more; got ${plan.tolerance}`);
|
|
3659
|
+
if (!(Number.isInteger(plan.patience) && plan.patience > 0)) {
|
|
3660
|
+
throw new Error(`settle.patience must be a positive whole number; got ${plan.patience}`);
|
|
3661
|
+
}
|
|
3662
|
+
if (!(plan.minIterations >= 0)) throw new Error(`settle.minIterations must be 0 or more; got ${plan.minIterations}`);
|
|
3663
|
+
return plan;
|
|
3664
|
+
}
|
|
3665
|
+
function trackSettle(options = {}) {
|
|
3666
|
+
const plan = settlePlan(options);
|
|
3667
|
+
let previous = null;
|
|
3668
|
+
let still = 0;
|
|
3669
|
+
let movement = Number.NaN;
|
|
3670
|
+
let settled = false;
|
|
3671
|
+
return {
|
|
3672
|
+
check(solution, iterations) {
|
|
3673
|
+
const play = rootPlay(solution);
|
|
3674
|
+
movement = previous ? rootPlayMovement(previous, play) : Number.NaN;
|
|
3675
|
+
previous = play;
|
|
3676
|
+
still = iterations >= plan.minIterations && movement <= plan.tolerance ? still + 1 : 0;
|
|
3677
|
+
settled = still >= plan.patience;
|
|
3678
|
+
return settled;
|
|
3679
|
+
},
|
|
3680
|
+
get movement() {
|
|
3681
|
+
return movement;
|
|
3682
|
+
},
|
|
3683
|
+
get settled() {
|
|
3684
|
+
return settled;
|
|
3685
|
+
}
|
|
3686
|
+
};
|
|
3687
|
+
}
|
|
3688
|
+
|
|
3689
|
+
// src/history-merge.ts
|
|
3690
|
+
function interner() {
|
|
3691
|
+
const ids = /* @__PURE__ */ new Map();
|
|
3692
|
+
return (key) => {
|
|
3693
|
+
let id = ids.get(key);
|
|
3694
|
+
if (id === void 0) ids.set(key, id = ids.size);
|
|
3695
|
+
return id;
|
|
3696
|
+
};
|
|
3697
|
+
}
|
|
3698
|
+
function signatures(tree) {
|
|
3699
|
+
const exact = new Int32Array(tree.nodeCount);
|
|
3700
|
+
const exactId = interner();
|
|
3701
|
+
for (let node = tree.nodeCount - 1; node >= 0; node--) {
|
|
3702
|
+
let e = `${tree.kind[node]},${tree.player[node]},${tree.round[node]},${tree.pot[node]},${tree.payoff[node]}`;
|
|
3703
|
+
const first = tree.firstEdge[node];
|
|
3704
|
+
for (let i = 0; i < tree.numEdges[node]; i++) {
|
|
3705
|
+
const edge = first + i;
|
|
3706
|
+
e += `|${tree.edgeAction[edge]}:${tree.edgeTarget[edge]}:${exact[tree.edgeChild[edge]]}`;
|
|
3707
|
+
}
|
|
3708
|
+
exact[node] = exactId(e);
|
|
3709
|
+
}
|
|
3710
|
+
return exact;
|
|
3711
|
+
}
|
|
3712
|
+
function aliasHistories(tree, runouts, options = {}) {
|
|
3713
|
+
const exact = signatures(tree);
|
|
3714
|
+
const alias = new Int32Array(tree.nodeCount);
|
|
3715
|
+
for (let node = 0; node < tree.nodeCount; node++) alias[node] = node;
|
|
3716
|
+
const rootsByRound = Array.from({ length: runouts.rounds }, () => []);
|
|
3717
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
3718
|
+
if (tree.kind[node] !== NodeKind.Chance) continue;
|
|
3719
|
+
const child = tree.edgeChild[tree.firstEdge[node]];
|
|
3720
|
+
if (tree.kind[child] === NodeKind.Decision) rootsByRound[tree.round[child]].push(child);
|
|
3721
|
+
}
|
|
3722
|
+
const walk2 = (rep, member) => {
|
|
3723
|
+
alias[member] = rep;
|
|
3724
|
+
const a = tree.firstEdge[rep];
|
|
3725
|
+
const b = tree.firstEdge[member];
|
|
3726
|
+
for (let i = 0; i < tree.numEdges[rep]; i++) walk2(tree.edgeChild[a + i], tree.edgeChild[b + i]);
|
|
3727
|
+
};
|
|
3728
|
+
const roots = new Int32Array(runouts.rounds);
|
|
3729
|
+
const groups = new Int32Array(runouts.rounds);
|
|
3730
|
+
const last = runouts.rounds - 1;
|
|
3731
|
+
if (last >= 1) {
|
|
3732
|
+
const reps = /* @__PURE__ */ new Map();
|
|
3733
|
+
for (const root of rootsByRound[last]) {
|
|
3734
|
+
if (options.keep?.[root]) continue;
|
|
3735
|
+
const rep = reps.get(exact[root]);
|
|
3736
|
+
if (rep === void 0) reps.set(exact[root], root);
|
|
3737
|
+
else walk2(rep, root);
|
|
3738
|
+
}
|
|
3739
|
+
groups[last] = reps.size;
|
|
3740
|
+
}
|
|
3741
|
+
for (let round = 1; round < runouts.rounds; round++) roots[round] = rootsByRound[round].length;
|
|
3742
|
+
return { alias, roots, groups };
|
|
3743
|
+
}
|
|
3744
|
+
|
|
3745
|
+
// src/betting.ts
|
|
2693
3746
|
var DEFAULT_CHIP_SCALE = 100;
|
|
3747
|
+
function betRows(config, rounds) {
|
|
3748
|
+
const rowsFor = (sizes, who) => {
|
|
3749
|
+
const expanded = sizes.length === 1 ? Array.from({ length: rounds }, () => sizes[0]) : sizes;
|
|
3750
|
+
if (expanded.length !== rounds) {
|
|
3751
|
+
throw new Error(
|
|
3752
|
+
`${who}betSizes has ${sizes.length} rows but this board has ${rounds} betting rounds; give one row per round, or a single row for all of them`
|
|
3753
|
+
);
|
|
3754
|
+
}
|
|
3755
|
+
return expanded;
|
|
3756
|
+
};
|
|
3757
|
+
return isPerPlayer(config.betSizes) ? { oop: rowsFor(config.betSizes.oop, "oop "), ip: rowsFor(config.betSizes.ip, "ip ") } : rowsFor(config.betSizes, "");
|
|
3758
|
+
}
|
|
3759
|
+
function treeConfig(config, rows) {
|
|
3760
|
+
const chipScale = config.chipScale ?? DEFAULT_CHIP_SCALE;
|
|
3761
|
+
return {
|
|
3762
|
+
potUnits: Math.round(config.pot * chipScale),
|
|
3763
|
+
stackUnits: Math.round(config.stack * chipScale),
|
|
3764
|
+
betSizes: rows,
|
|
3765
|
+
raiseSizes: config.raiseSizes,
|
|
3766
|
+
maxRaises: config.maxRaises ?? 1,
|
|
3767
|
+
allowAllIn: config.allowAllIn ?? false,
|
|
3768
|
+
allInThreshold: config.allInThreshold,
|
|
3769
|
+
noDonk: config.noDonk,
|
|
3770
|
+
mergeSizes: config.mergeSizes,
|
|
3771
|
+
removeActions: config.removeActions,
|
|
3772
|
+
thinSizes: config.thinSizes,
|
|
3773
|
+
chipScale,
|
|
3774
|
+
minRaiseUnits: chipScale
|
|
3775
|
+
};
|
|
3776
|
+
}
|
|
3777
|
+
function bettingTree(config) {
|
|
3778
|
+
const rounds = 6 - parseCards(config.board).length;
|
|
3779
|
+
if (rounds < 1 || rounds > 3) throw new Error(`A postflop board has 3, 4 or 5 cards; got ${config.board}`);
|
|
3780
|
+
return buildTree(treeConfig(config, betRows(config, rounds)), { rounds });
|
|
3781
|
+
}
|
|
3782
|
+
|
|
3783
|
+
// src/resolve.ts
|
|
3784
|
+
function resolveSubgame(blueprint, node, board, options) {
|
|
3785
|
+
const { player } = options;
|
|
3786
|
+
const opponent = 1 - player;
|
|
3787
|
+
const iterations = options.iterations ?? 200;
|
|
3788
|
+
if (!(Number.isInteger(iterations) && iterations > 0)) {
|
|
3789
|
+
throw new Error(`iterations must be a positive whole number; got ${iterations}`);
|
|
3790
|
+
}
|
|
3791
|
+
const spot = defineSpot(blueprint.subgameAt(node, board, options.betting));
|
|
3792
|
+
const base = spot.players[opponent].weights;
|
|
3793
|
+
const mine = spot.players[player].weights;
|
|
3794
|
+
const open = options.measure === true ? openMass(mine) : null;
|
|
3795
|
+
const live = liveCombos(spot.players);
|
|
3796
|
+
const blueprintValues = open ? bestResponseAt(blueprint.solver.ctx, node, board, opponent, base, mine) : null;
|
|
3797
|
+
const run = createRun(spot, { ...options.solve });
|
|
3798
|
+
try {
|
|
3799
|
+
run.step(iterations);
|
|
3800
|
+
let alternative = null;
|
|
3801
|
+
let entered = null;
|
|
3802
|
+
if (open && blueprintValues) {
|
|
3803
|
+
const { ctx } = run.solver;
|
|
3804
|
+
const deals = ctx.runoutWeight[0];
|
|
3805
|
+
alternative = new Float64Array(COMBOS).fill(Number.NaN);
|
|
3806
|
+
for (const combo of live) {
|
|
3807
|
+
if (base[combo] > 0 && open[combo] > 0) alternative[combo] = blueprintValues[combo] / (open[combo] * deals);
|
|
3808
|
+
}
|
|
3809
|
+
const lists = ctx.active;
|
|
3810
|
+
const slot = lists.roundBase[0] * 2 + opponent;
|
|
3811
|
+
const from = lists.start[slot];
|
|
3812
|
+
const response = run.solver.root(Mode.BestResponse, opponent, 0);
|
|
3813
|
+
entered = new Float64Array(COMBOS).fill(Number.NaN);
|
|
3814
|
+
for (let i = 0; i < lists.count[slot]; i++) {
|
|
3815
|
+
const combo = lists.list[from + i];
|
|
3816
|
+
if (!Number.isFinite(alternative[combo])) continue;
|
|
3817
|
+
const chips = response[i] / (open[combo] * deals);
|
|
3818
|
+
for (let m = lists.memberStart[from + i]; m < lists.memberStart[from + i + 1]; m++) entered[lists.memberCombo[m]] = chips;
|
|
3819
|
+
}
|
|
3820
|
+
}
|
|
3821
|
+
return { solution: run.snapshot(), player, alternative, entered, iterations };
|
|
3822
|
+
} finally {
|
|
3823
|
+
run.close();
|
|
3824
|
+
}
|
|
3825
|
+
}
|
|
3826
|
+
function bestResponseAt(ctx, node, board, traverser, reachMe, reachOpp) {
|
|
3827
|
+
const lists = ctx.active;
|
|
3828
|
+
const round = ctx.tree.round[node];
|
|
3829
|
+
const listOf = (player) => {
|
|
3830
|
+
const slot = (lists.roundBase[round] + board) * 2 + player;
|
|
3831
|
+
return [lists.start[slot], lists.count[slot]];
|
|
3832
|
+
};
|
|
3833
|
+
const gather = (source, player) => {
|
|
3834
|
+
const [from, count] = listOf(player);
|
|
3835
|
+
const into = new Float64Array(COMBOS);
|
|
3836
|
+
for (let i = 0; i < count; i++) into[i] = source[lists.list[from + i]];
|
|
3837
|
+
return into;
|
|
3838
|
+
};
|
|
3839
|
+
const opponent = 1 - traverser;
|
|
3840
|
+
const [oppFrom, oppCount] = listOf(opponent);
|
|
3841
|
+
for (let j = 0; j < oppCount; j++) {
|
|
3842
|
+
const own = reachOpp[lists.list[oppFrom + j]];
|
|
3843
|
+
for (let m = lists.memberStart[oppFrom + j] + 1; m < lists.memberStart[oppFrom + j + 1]; m++) {
|
|
3844
|
+
if (reachOpp[lists.memberCombo[m]] !== own) throw new Error("Best-response values need reach that mirrored hands share");
|
|
3845
|
+
}
|
|
3846
|
+
}
|
|
3847
|
+
const expanded = ctx.expandOrbits;
|
|
3848
|
+
ctx.expandOrbits = true;
|
|
3849
|
+
try {
|
|
3850
|
+
const buffers = makeBuffers2(ctx.tree, ctx.layout.maxActions, ctx.simd);
|
|
3851
|
+
const value = traverse(ctx, buffers, Mode.BestResponse, traverser, 0, node, board, gather(reachMe, traverser), gather(reachOpp, opponent), 0);
|
|
3852
|
+
const result = new Float64Array(COMBOS);
|
|
3853
|
+
const [from, count] = listOf(traverser);
|
|
3854
|
+
for (let i = 0; i < count; i++) {
|
|
3855
|
+
for (let m = lists.memberStart[from + i]; m < lists.memberStart[from + i + 1]; m++) result[lists.memberCombo[m]] = value[i];
|
|
3856
|
+
}
|
|
3857
|
+
return result;
|
|
3858
|
+
} finally {
|
|
3859
|
+
ctx.expandOrbits = expanded;
|
|
3860
|
+
}
|
|
3861
|
+
}
|
|
3862
|
+
function liveCombos(players) {
|
|
3863
|
+
const live = [];
|
|
3864
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
3865
|
+
if (players[0].weights[combo] > 0 || players[1].weights[combo] > 0) live.push(combo);
|
|
3866
|
+
}
|
|
3867
|
+
return Uint16Array.from(live);
|
|
3868
|
+
}
|
|
3869
|
+
function openMass(range) {
|
|
3870
|
+
const held = [];
|
|
3871
|
+
for (let h = 0; h < COMBOS; h++) if (range[h] > 0) held.push(h);
|
|
3872
|
+
const open = new Float64Array(COMBOS);
|
|
3873
|
+
for (let h = 0; h < COMBOS; h++) {
|
|
3874
|
+
const a = COMBO_CARDS[h * 2];
|
|
3875
|
+
const b = COMBO_CARDS[h * 2 + 1];
|
|
3876
|
+
let mass = 0;
|
|
3877
|
+
for (const other of held) {
|
|
3878
|
+
const c = COMBO_CARDS[other * 2];
|
|
3879
|
+
const d = COMBO_CARDS[other * 2 + 1];
|
|
3880
|
+
if (c !== a && c !== b && d !== a && d !== b) mass += range[other];
|
|
3881
|
+
}
|
|
3882
|
+
open[h] = mass;
|
|
3883
|
+
}
|
|
3884
|
+
return open;
|
|
3885
|
+
}
|
|
3886
|
+
|
|
3887
|
+
// src/hybrid.ts
|
|
3888
|
+
function turnLineReach(solution) {
|
|
3889
|
+
const { tree, players } = solution.spot;
|
|
3890
|
+
if (solution.spot.runouts.rounds < 2) return [];
|
|
3891
|
+
const rootMass = matchupMass(players[0].weights, players[1].weights);
|
|
3892
|
+
const lines = [];
|
|
3893
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
3894
|
+
if (tree.kind[node] !== NodeKind.Chance || tree.round[node] !== 0) continue;
|
|
3895
|
+
const child = tree.edgeChild[tree.firstEdge[node]];
|
|
3896
|
+
if (tree.kind[child] !== NodeKind.Decision) continue;
|
|
3897
|
+
const share = matchupMass(solution.reachAt(child, 0, 0), solution.reachAt(child, 0, 1)) / rootMass;
|
|
3898
|
+
lines.push({ line: solution.path(child), node: child, share });
|
|
3899
|
+
}
|
|
3900
|
+
return lines.sort((a, b) => b.share - a.share);
|
|
3901
|
+
}
|
|
3902
|
+
function linesWithin(solution, maxBytes, options = {}) {
|
|
3903
|
+
const config = options.config ?? solution.spot.source;
|
|
3904
|
+
const precision = options.precision ?? "f32";
|
|
3905
|
+
const asked = config.shareHistories;
|
|
3906
|
+
if (!asked) throw new Error("linesWithin picks lines to keep out of shareHistories; this spot shares nothing");
|
|
3907
|
+
const share = asked === true ? {} : asked;
|
|
3908
|
+
const ranked = turnLineReach(solution).map((l) => l.line);
|
|
3909
|
+
const lines = (count) => [.../* @__PURE__ */ new Set([...share.keepLines ?? [], ...ranked.slice(0, count)])];
|
|
3910
|
+
const bytes = (count) => storageCost(defineSpot({ ...config, maxBytes: Number.MAX_SAFE_INTEGER, shareHistories: { ...share, keepLines: lines(count) } }), precision);
|
|
3911
|
+
const least = bytes(0);
|
|
3912
|
+
if (least > maxBytes) {
|
|
3913
|
+
throw new Error(`linesWithin: the spot needs ${least} bytes keeping no more lines, over the budget of ${maxBytes}`);
|
|
3914
|
+
}
|
|
3915
|
+
let low = 0;
|
|
3916
|
+
let high2 = ranked.length;
|
|
3917
|
+
while (low < high2) {
|
|
3918
|
+
const mid = Math.ceil((low + high2) / 2);
|
|
3919
|
+
if (bytes(mid) <= maxBytes) low = mid;
|
|
3920
|
+
else high2 = mid - 1;
|
|
3921
|
+
}
|
|
3922
|
+
return lines(low);
|
|
3923
|
+
}
|
|
3924
|
+
|
|
3925
|
+
// src/presets.ts
|
|
3926
|
+
function sizesCut(caller, preset) {
|
|
3927
|
+
const from = bettingTree(caller);
|
|
3928
|
+
const to = bettingTree(preset);
|
|
3929
|
+
const rounds = 6 - parseCards(caller.board).length;
|
|
3930
|
+
const streets = ["flop", "turn", "river"].slice(3 - rounds);
|
|
3931
|
+
const groups = /* @__PURE__ */ new Map();
|
|
3932
|
+
const walk2 = (a, b, invested) => {
|
|
3933
|
+
const kind = from.kind[a];
|
|
3934
|
+
if (kind === NodeKind.Chance) {
|
|
3935
|
+
walk2(from.edgeChild[from.firstEdge[a]], to.edgeChild[to.firstEdge[b]], invested);
|
|
3936
|
+
return;
|
|
3937
|
+
}
|
|
3938
|
+
if (kind !== NodeKind.Decision) return;
|
|
3939
|
+
const me = from.player[a];
|
|
3940
|
+
const owed = Math.max(0, invested[1 - me] - invested[me]);
|
|
3941
|
+
const facing = owed > 0 ? "raise" : "bet";
|
|
3942
|
+
const key = `${from.round[a]} ${me} ${facing}`;
|
|
3943
|
+
const group = groups.get(key) ?? { cut: /* @__PURE__ */ new Map(), decisions: 0, of: 0 };
|
|
3944
|
+
groups.set(key, group);
|
|
3945
|
+
const theirs = edgesOf(to, b);
|
|
3946
|
+
let sized = false;
|
|
3947
|
+
let lost = false;
|
|
3948
|
+
for (let e = from.firstEdge[a]; e < from.firstEdge[a] + from.numEdges[a]; e++) {
|
|
3949
|
+
const action = from.edgeAction[e];
|
|
3950
|
+
const target = from.edgeTarget[e];
|
|
3951
|
+
const aggressive = action === ActionKind.Bet || action === ActionKind.Raise || action === ActionKind.AllIn;
|
|
3952
|
+
if (aggressive) sized = true;
|
|
3953
|
+
const match = theirs.get(`${action} ${target}`);
|
|
3954
|
+
if (match === void 0) {
|
|
3955
|
+
if (!aggressive) throw new Error(`sizesCut: the preset's tree lost a fold, check or call at node ${a}`);
|
|
3956
|
+
lost = true;
|
|
3957
|
+
const size = action === ActionKind.AllIn ? "allin" : fraction(from, a, target, invested, me, owed);
|
|
3958
|
+
group.cut.set(String(size), size);
|
|
3959
|
+
continue;
|
|
3960
|
+
}
|
|
3961
|
+
const next = [invested[0], invested[1]];
|
|
3962
|
+
next[me] = target;
|
|
3963
|
+
walk2(from.edgeChild[e], to.edgeChild[match], next);
|
|
3964
|
+
}
|
|
3965
|
+
if (sized) group.of++;
|
|
3966
|
+
if (lost) group.decisions++;
|
|
3967
|
+
};
|
|
3968
|
+
walk2(0, 0, [0, 0]);
|
|
3969
|
+
const out = [];
|
|
3970
|
+
for (const [key, group] of groups) {
|
|
3971
|
+
if (group.cut.size === 0) continue;
|
|
3972
|
+
const [round, player, facing] = key.split(" ");
|
|
3973
|
+
const sizes = [...group.cut.values()].sort((x, y) => x === "allin" ? 1 : y === "allin" ? -1 : x - y);
|
|
3974
|
+
out.push({
|
|
3975
|
+
street: streets[Number(round)],
|
|
3976
|
+
player: player === "0" ? "oop" : "ip",
|
|
3977
|
+
facing,
|
|
3978
|
+
sizes,
|
|
3979
|
+
decisions: group.decisions,
|
|
3980
|
+
of: group.of
|
|
3981
|
+
});
|
|
3982
|
+
}
|
|
3983
|
+
const order = (c) => streets.indexOf(c.street) * 4 + (c.player === "oop" ? 0 : 2) + (c.facing === "bet" ? 0 : 1);
|
|
3984
|
+
return out.sort((x, y) => order(x) - order(y));
|
|
3985
|
+
}
|
|
3986
|
+
function edgesOf(tree, node) {
|
|
3987
|
+
const out = /* @__PURE__ */ new Map();
|
|
3988
|
+
for (let e = tree.firstEdge[node]; e < tree.firstEdge[node] + tree.numEdges[node]; e++) {
|
|
3989
|
+
out.set(`${tree.edgeAction[e]} ${tree.edgeTarget[e]}`, e);
|
|
3990
|
+
}
|
|
3991
|
+
return out;
|
|
3992
|
+
}
|
|
3993
|
+
function fraction(tree, node, target, invested, me, owed) {
|
|
3994
|
+
const potAfterCall = tree.pot[node] + owed;
|
|
3995
|
+
return Math.round((target - invested[me] - owed) / potAfterCall * 100) / 100;
|
|
3996
|
+
}
|
|
3997
|
+
function presetChanges(caller, preset, settings, notes) {
|
|
3998
|
+
const cuts = sizesCut(caller, preset);
|
|
3999
|
+
const roundsSimplified = ["flop", "turn", "river"].filter((s) => cuts.some((c) => c.street === s));
|
|
4000
|
+
const sizeNotes = cuts.map((c) => {
|
|
4001
|
+
const sizes = c.sizes.map((s) => s === "allin" ? "all-in" : String(s)).join(", ");
|
|
4002
|
+
const who = c.player === "oop" ? "OOP" : "IP";
|
|
4003
|
+
const what = c.facing === "bet" ? "bet" : "raise";
|
|
4004
|
+
return `${c.street}: ${who} loses the ${what} size${c.sizes.length > 1 ? "s" : ""} ${sizes} at ${c.decisions} of the ${c.of} such decisions the preset keeps`;
|
|
4005
|
+
});
|
|
4006
|
+
return { sizesCut: cuts, roundsSimplified, settings, notes: [...sizeNotes, ...notes] };
|
|
4007
|
+
}
|
|
4008
|
+
function samplingNote(sampling) {
|
|
4009
|
+
const which = sampling.rounds ? `the deals of rounds ${sampling.rounds.join(", ")}` : "every later round's deals";
|
|
4010
|
+
return sampling.every ? `${which} walked once every ${sampling.every} iterations, in rotation` : `${which} sampled at p ${sampling.p ?? 0.35}`;
|
|
4011
|
+
}
|
|
4012
|
+
|
|
4013
|
+
// src/lite.ts
|
|
4014
|
+
function liteConfig(config, options = {}) {
|
|
4015
|
+
const target = options.laterSize ?? 0.75;
|
|
4016
|
+
if (!(target > 0)) throw new Error(`laterSize must be above 0; got ${target}`);
|
|
4017
|
+
const rounds = 6 - parseCards(config.board).length;
|
|
4018
|
+
if (!(rounds >= 1 && rounds <= 3)) throw new Error(`A board has 3, 4 or 5 cards; got ${config.board}`);
|
|
4019
|
+
const flopCap = options.flopMaxRaises;
|
|
4020
|
+
if (flopCap !== void 0 && !(Number.isInteger(flopCap) && flopCap >= 1)) {
|
|
4021
|
+
throw new Error(`flopMaxRaises must be a whole number of at least 1; got ${flopCap}`);
|
|
4022
|
+
}
|
|
4023
|
+
const betSizes = mapPlayers(
|
|
4024
|
+
config.betSizes,
|
|
4025
|
+
(rows) => perRound(rows, true, rounds, "betSizes").map((row, r) => r === 0 ? row : nearest(row, target))
|
|
4026
|
+
);
|
|
4027
|
+
const raiseSizes = config.raiseSizes === void 0 ? void 0 : mapPlayers(
|
|
4028
|
+
config.raiseSizes,
|
|
4029
|
+
(side) => side === void 0 ? void 0 : raiseRows(side, rounds).map((row, r) => r === 0 ? row : nearest(row, target))
|
|
4030
|
+
);
|
|
4031
|
+
const maxRaises = mapPlayers(
|
|
4032
|
+
config.maxRaises ?? 1,
|
|
4033
|
+
(side) => perRound(side, Array.isArray(side), rounds, "maxRaises").map(
|
|
4034
|
+
(n, r) => r === 0 ? flopCap === void 0 ? n : Math.min(n, flopCap) : Math.min(n, options.laterRaise ? 2 : 1)
|
|
4035
|
+
)
|
|
4036
|
+
);
|
|
4037
|
+
const allowAllIn = mapPlayers(
|
|
4038
|
+
config.allowAllIn ?? false,
|
|
4039
|
+
(side) => perRound(side, Array.isArray(side), rounds, "allowAllIn").map((on, r) => r === 0 ? on : false)
|
|
4040
|
+
);
|
|
4041
|
+
const aggressions = (line) => line.split(" ").filter((w) => w === "bet" || w === "raise" || w === "allin").length;
|
|
4042
|
+
const removeActions = config.removeActions?.filter(
|
|
4043
|
+
(line) => !line.includes("/") && (flopCap === void 0 || aggressions(line) <= flopCap)
|
|
4044
|
+
);
|
|
4045
|
+
const buckets = options.runoutBuckets ?? 3;
|
|
4046
|
+
const given = typeof config.runoutBuckets === "number" ? config.runoutBuckets : config.runoutBuckets ? 1 : 0;
|
|
4047
|
+
return {
|
|
4048
|
+
...config,
|
|
4049
|
+
betSizes,
|
|
4050
|
+
...raiseSizes === void 0 ? {} : { raiseSizes },
|
|
4051
|
+
maxRaises,
|
|
4052
|
+
allowAllIn,
|
|
4053
|
+
...options.noDonk === void 0 ? {} : { noDonk: options.noDonk },
|
|
4054
|
+
...removeActions === void 0 ? {} : { removeActions },
|
|
4055
|
+
runoutBuckets: Math.max(buckets, given),
|
|
4056
|
+
shareHistories: options.shareHistories ?? config.shareHistories ?? true
|
|
4057
|
+
};
|
|
4058
|
+
}
|
|
4059
|
+
function raiseRows(side, rounds) {
|
|
4060
|
+
return raisesPerRound(side) ? perRound(side, true, rounds, "raiseSizes") : Array.from({ length: rounds }, () => side);
|
|
4061
|
+
}
|
|
4062
|
+
function nearest(row, target) {
|
|
4063
|
+
let best;
|
|
4064
|
+
let gap = Infinity;
|
|
4065
|
+
for (const size of row) {
|
|
4066
|
+
if (typeof size !== "number") continue;
|
|
4067
|
+
const g = Math.abs(Math.log(size / target));
|
|
4068
|
+
if (g < gap) {
|
|
4069
|
+
gap = g;
|
|
4070
|
+
best = size;
|
|
4071
|
+
}
|
|
4072
|
+
}
|
|
4073
|
+
if (best === void 0 && row.length > 0) best = row[0];
|
|
4074
|
+
return best === void 0 ? [] : [best];
|
|
4075
|
+
}
|
|
4076
|
+
function solveLite(config, options = {}) {
|
|
4077
|
+
const preset = litePreset(config, options);
|
|
4078
|
+
const blueprint = preset.config;
|
|
4079
|
+
const share = blueprint.shareHistories;
|
|
4080
|
+
const kept = typeof share === "object" ? [...share.keepLines ?? []] : [];
|
|
4081
|
+
const solution = solve(defineSpot(blueprint), preset.options);
|
|
4082
|
+
const subgameSolve = pick(options, ["workers", "precision", "wasmStorage"]);
|
|
4083
|
+
const { tree } = solution.spot;
|
|
4084
|
+
const scale = solution.spot.chipScale;
|
|
4085
|
+
return {
|
|
4086
|
+
solution,
|
|
4087
|
+
blueprint,
|
|
4088
|
+
kept,
|
|
4089
|
+
source: config,
|
|
4090
|
+
changes: preset.changes,
|
|
4091
|
+
resolve(node, board, resolveOptions) {
|
|
4092
|
+
const round = tree.round[node];
|
|
4093
|
+
const prefix = `${solution.path(node)} `;
|
|
4094
|
+
const invested = (tree.pot[node] - solution.spot.potUnits) / 2;
|
|
4095
|
+
const removeActions = (config.removeActions ?? []).filter((line) => line.startsWith(prefix)).map(
|
|
4096
|
+
(line) => line.slice(prefix.length).split(" ").map((word) => /^-?[\d.]+$/.test(word) ? String((Math.round(Number(word) * scale) - invested) / scale) : word).join(" ")
|
|
4097
|
+
);
|
|
4098
|
+
return resolveSubgame(solution, node, board, {
|
|
4099
|
+
...resolveOptions,
|
|
4100
|
+
betting: { ...bettingFrom(config, round), removeActions, ...resolveOptions.betting },
|
|
4101
|
+
solve: { ...subgameSolve, ...resolveOptions.solve }
|
|
4102
|
+
});
|
|
4103
|
+
}
|
|
4104
|
+
};
|
|
4105
|
+
}
|
|
4106
|
+
function litePreset(config, options = {}) {
|
|
4107
|
+
const blueprint = keepWithin(liteConfig(config, options), options);
|
|
4108
|
+
const sampling = options.sampling === void 0 ? {} : options.sampling;
|
|
4109
|
+
const settle = options.settle === void 0 ? {} : options.settle;
|
|
4110
|
+
const solveOptions = {
|
|
4111
|
+
iterations: options.iterations ?? 2e3,
|
|
4112
|
+
...settle ? { settle } : {},
|
|
4113
|
+
...sampling ? { approximations: { chanceSampling: sampling } } : {},
|
|
4114
|
+
...pick(options, ["workers", "precision", "wasmStorage", "algorithm", "onProgress", "targetPercent"])
|
|
4115
|
+
};
|
|
4116
|
+
const settings = [];
|
|
4117
|
+
const notes = [];
|
|
4118
|
+
const change = (setting, from, to, note) => {
|
|
4119
|
+
settings.push({ setting, from, to });
|
|
4120
|
+
notes.push(`${setting}: ${note}`);
|
|
4121
|
+
};
|
|
4122
|
+
const rounds = 6 - parseCards(config.board).length;
|
|
4123
|
+
const streets = ["flop", "turn", "river"].slice(3 - rounds);
|
|
4124
|
+
const donks = (value) => perRound(value ?? false, Array.isArray(value), rounds, "noDonk");
|
|
4125
|
+
const [donkFrom, donkTo] = [donks(config.noDonk), donks(blueprint.noDonk)];
|
|
4126
|
+
if (donkFrom.join() !== donkTo.join()) {
|
|
4127
|
+
const on = streets.filter((_, r) => donkTo[r]);
|
|
4128
|
+
change("noDonk", config.noDonk, blueprint.noDonk, on.length ? `donk bets forbidden on the ${on.join(" and ")}, a speed trim that changes the game` : "donk bets allowed on every street");
|
|
4129
|
+
}
|
|
4130
|
+
const gap = (value) => value === true ? 1 : typeof value === "number" ? value : 0;
|
|
4131
|
+
if (gap(config.runoutBuckets) !== gap(blueprint.runoutBuckets)) {
|
|
4132
|
+
const to = gap(blueprint.runoutBuckets);
|
|
4133
|
+
change("runoutBuckets", config.runoutBuckets, blueprint.runoutBuckets, to > 0 ? `gap ${to}: turn and river cards that play alike are solved once (approximate; folds nothing on ranges with no rank symmetry)` : "off: every turn and river card solved");
|
|
4134
|
+
}
|
|
4135
|
+
const sharing = (value) => JSON.stringify(value === true ? {} : value || null);
|
|
4136
|
+
if (sharing(config.shareHistories) !== sharing(blueprint.shareHistories)) {
|
|
4137
|
+
const kept = typeof blueprint.shareHistories === "object" ? blueprint.shareHistories.keepLines?.length ?? 0 : 0;
|
|
4138
|
+
change("shareHistories", config.shareHistories, blueprint.shareHistories, blueprint.shareHistories ? `lines that meet share river storage${kept ? `, but for ${kept} line${kept > 1 ? "s" : ""} kept apart` : ""} (approximate: exploitability plateaus a few % of pot from equilibrium; read the root play)` : "off: every line stores its own river");
|
|
4139
|
+
}
|
|
4140
|
+
const dropped = (config.removeActions?.length ?? 0) - (blueprint.removeActions?.length ?? 0);
|
|
4141
|
+
if (dropped > 0) {
|
|
4142
|
+
change("removeActions", config.removeActions, blueprint.removeActions, `${dropped} line${dropped > 1 ? "s" : ""} below a deal or past the flop cap left out of the blueprint; resolve puts those below a deal back`);
|
|
4143
|
+
}
|
|
4144
|
+
if (sampling) change("approximations.chanceSampling", void 0, sampling, samplingNote(sampling));
|
|
4145
|
+
if (settle) change("settle", void 0, settle, "stops once the root play stops moving, not at an exploitability target");
|
|
4146
|
+
return { config: blueprint, options: solveOptions, changes: presetChanges(config, blueprint, settings, notes) };
|
|
4147
|
+
}
|
|
4148
|
+
function keepWithin(blueprint, options = {}) {
|
|
4149
|
+
const budget = options.keepBudget;
|
|
4150
|
+
if (budget === void 0) return blueprint;
|
|
4151
|
+
if (!(budget > 0)) throw new Error(`keepBudget must be a number of bytes above 0; got ${budget}`);
|
|
4152
|
+
const its = options.pickIterations ?? 80;
|
|
4153
|
+
if (!(Number.isInteger(its) && its >= 1)) throw new Error(`pickIterations must be a whole number of at least 1; got ${its}`);
|
|
4154
|
+
const asked = blueprint.shareHistories;
|
|
4155
|
+
if (!asked) throw new Error("keepBudget keeps lines out of shareHistories; this blueprint shares nothing");
|
|
4156
|
+
const cheap = defineSpot(checkedDown(blueprint));
|
|
4157
|
+
const picked = solve(cheap, {
|
|
4158
|
+
iterations: its,
|
|
4159
|
+
...pickSampling(options.sampling === void 0 ? {} : options.sampling, cheap.runouts.rounds),
|
|
4160
|
+
...pick(options, ["workers", "precision", "wasmStorage", "algorithm"])
|
|
4161
|
+
});
|
|
4162
|
+
const keepLines = linesWithin(picked, budget, { config: blueprint, precision: options.precision ?? "f32" });
|
|
4163
|
+
return { ...blueprint, shareHistories: { ...asked === true ? {} : asked, keepLines } };
|
|
4164
|
+
}
|
|
4165
|
+
function pickSampling(sampling, rounds) {
|
|
4166
|
+
if (!sampling) return {};
|
|
4167
|
+
const asked = sampling.rounds ?? Array.from({ length: rounds - 1 }, (_, i) => i + 1);
|
|
4168
|
+
const kept = asked.filter((r) => r < rounds - 1);
|
|
4169
|
+
return kept.length > 0 ? { approximations: { chanceSampling: { ...sampling, rounds: kept } } } : {};
|
|
4170
|
+
}
|
|
4171
|
+
function checkedDown(config) {
|
|
4172
|
+
const rounds = 6 - parseCards(config.board).length;
|
|
4173
|
+
if (!(rounds >= 1 && rounds <= 3)) throw new Error(`A board has 3, 4 or 5 cards; got ${config.board}`);
|
|
4174
|
+
const last = rounds - 1;
|
|
4175
|
+
const betSizes = mapPlayers(
|
|
4176
|
+
config.betSizes,
|
|
4177
|
+
(rows) => perRound(rows, true, rounds, "betSizes").map((row, r) => r === last ? [] : row)
|
|
4178
|
+
);
|
|
4179
|
+
const allowAllIn = mapPlayers(
|
|
4180
|
+
config.allowAllIn ?? false,
|
|
4181
|
+
(side) => perRound(side, Array.isArray(side), rounds, "allowAllIn").map((on, r) => r === last ? false : on)
|
|
4182
|
+
);
|
|
4183
|
+
const removeActions = config.removeActions?.filter((line) => line.split("/").length - 1 < last);
|
|
4184
|
+
const asked = config.shareHistories;
|
|
4185
|
+
const shareHistories2 = typeof asked === "object" ? (({ keepLines: _kept, ...rest }) => rest)(asked) : asked;
|
|
4186
|
+
return {
|
|
4187
|
+
...config,
|
|
4188
|
+
betSizes,
|
|
4189
|
+
allowAllIn,
|
|
4190
|
+
...removeActions === void 0 ? {} : { removeActions },
|
|
4191
|
+
...shareHistories2 === void 0 ? {} : { shareHistories: shareHistories2 }
|
|
4192
|
+
};
|
|
4193
|
+
}
|
|
4194
|
+
function pick(from, keys) {
|
|
4195
|
+
const out = {};
|
|
4196
|
+
for (const key of keys) if (from[key] !== void 0) out[key] = from[key];
|
|
4197
|
+
return out;
|
|
4198
|
+
}
|
|
4199
|
+
function bettingFrom(config, round) {
|
|
4200
|
+
const rounds = 6 - parseCards(config.board).length;
|
|
4201
|
+
const from = (value, listed, name) => listed ? perRound(value, true, rounds, name).slice(round) : value;
|
|
4202
|
+
const each = (value, listed, name) => value === void 0 ? void 0 : mapPlayers(value, (side) => from(side, listed(side), name));
|
|
4203
|
+
const out = {
|
|
4204
|
+
betSizes: each(config.betSizes, () => true, "betSizes"),
|
|
4205
|
+
raiseSizes: each(config.raiseSizes, (side) => side !== void 0 && raisesPerRound(side), "raiseSizes"),
|
|
4206
|
+
maxRaises: each(config.maxRaises ?? 1, Array.isArray, "maxRaises"),
|
|
4207
|
+
allowAllIn: each(config.allowAllIn ?? false, Array.isArray, "allowAllIn"),
|
|
4208
|
+
allInThreshold: each(config.allInThreshold, Array.isArray, "allInThreshold"),
|
|
4209
|
+
// The blueprint may forbid donk bets where the caller did not.
|
|
4210
|
+
noDonk: from(config.noDonk ?? false, Array.isArray(config.noDonk), "noDonk")
|
|
4211
|
+
};
|
|
4212
|
+
return out;
|
|
4213
|
+
}
|
|
4214
|
+
|
|
4215
|
+
// src/index.ts
|
|
2694
4216
|
var OPS_PER_SECOND = 2e8;
|
|
2695
4217
|
var SpotTooLargeError = class extends Error {
|
|
2696
4218
|
estimate;
|
|
2697
4219
|
constructor(estimate, maxBytes) {
|
|
2698
4220
|
super(
|
|
2699
|
-
`This spot needs ~${formatBytes(estimate.compressedBytes)} even at 16-bit precision, but the limit is ${formatBytes(maxBytes)}.
|
|
4221
|
+
(estimate.compactBytes !== null ? `This spot needs ~${formatBytes(estimate.compactBytes)} even with compact storage, ` : `This spot needs ~${formatBytes(estimate.compressedBytes)} even at 16-bit precision, `) + `but the limit is ${formatBytes(maxBytes)}.
|
|
2700
4222
|
${estimate.decisionNodes} decision nodes x up to ${estimate.runouts.toLocaleString()} runouts x ${Math.max(...estimate.combos)} combos.
|
|
2701
4223
|
` + (estimate.suggestions.length ? `Try:
|
|
2702
4224
|
${estimate.suggestions.map((s) => ` - ${s}`).join("\n")}
|
|
@@ -2726,16 +4248,7 @@ function defineSpot(config) {
|
|
|
2726
4248
|
const rounds = 6 - board.length;
|
|
2727
4249
|
if (config.pot <= 0) throw new Error("pot must be positive");
|
|
2728
4250
|
if (config.stack <= 0) throw new Error("stack must be positive");
|
|
2729
|
-
const
|
|
2730
|
-
const expanded = sizes.length === 1 ? Array.from({ length: rounds }, () => sizes[0]) : sizes;
|
|
2731
|
-
if (expanded.length !== rounds) {
|
|
2732
|
-
throw new Error(
|
|
2733
|
-
`${who}betSizes has ${sizes.length} rows but this board has ${rounds} betting rounds; give one row per round, or a single row for all of them`
|
|
2734
|
-
);
|
|
2735
|
-
}
|
|
2736
|
-
return expanded;
|
|
2737
|
-
};
|
|
2738
|
-
const rows = isPerPlayer(config.betSizes) ? { oop: rowsFor(config.betSizes.oop, "oop "), ip: rowsFor(config.betSizes.ip, "ip ") } : rowsFor(config.betSizes, "");
|
|
4251
|
+
const rows = betRows(config, rounds);
|
|
2739
4252
|
const onBoard = new Set(board);
|
|
2740
4253
|
const trim = (range, who) => {
|
|
2741
4254
|
const weights = typeof range === "string" ? parseRange(range) : Float64Array.from(range);
|
|
@@ -2765,34 +4278,21 @@ function defineSpot(config) {
|
|
|
2765
4278
|
buildPlayerIndex(trim(config.ip, "in-position"))
|
|
2766
4279
|
];
|
|
2767
4280
|
const symmetries = config.isomorphism === false || board.length === 5 ? void 0 : suitSymmetries(board, [players[0].weights, players[1].weights]);
|
|
2768
|
-
const
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
);
|
|
2773
|
-
const tree = buildTree(
|
|
2774
|
-
{
|
|
2775
|
-
potUnits: Math.round(config.pot * chipScale),
|
|
2776
|
-
stackUnits: Math.round(config.stack * chipScale),
|
|
2777
|
-
betSizes: rows,
|
|
2778
|
-
raiseSizes: config.raiseSizes,
|
|
2779
|
-
maxRaises: config.maxRaises ?? 1,
|
|
2780
|
-
allowAllIn: config.allowAllIn ?? false,
|
|
2781
|
-
allInThreshold: config.allInThreshold,
|
|
2782
|
-
noDonk: config.noDonk,
|
|
2783
|
-
mergeSizes: config.mergeSizes,
|
|
2784
|
-
chipScale,
|
|
2785
|
-
minRaiseUnits: chipScale
|
|
2786
|
-
},
|
|
2787
|
-
runouts
|
|
2788
|
-
);
|
|
2789
|
-
const layout = buildLayout(tree, runouts, players, 4);
|
|
4281
|
+
const merge = config.runoutBuckets && board.length < 5 ? rangeRankMerge(players, config.runoutBuckets === true ? DEFAULT_BUCKET_GAP : config.runoutBuckets) : void 0;
|
|
4282
|
+
const runouts = enumerateRunouts(board, symmetries, merge);
|
|
4283
|
+
const tree = buildTree(treeConfig(config, rows), runouts);
|
|
4284
|
+
const historyAlias = config.shareHistories ? shareHistories(tree, runouts, config, chipScale) : void 0;
|
|
4285
|
+
const layout = buildLayout(tree, runouts, players, 4, historyAlias);
|
|
2790
4286
|
const last = runouts.rounds - 1;
|
|
2791
4287
|
const showdownBytes = runouts.counts[last] * (2 * COMBOS + COMBOS + 1) * 2;
|
|
2792
4288
|
const bytes = layout.bytes + showdownBytes;
|
|
2793
|
-
const compressedBytes = buildLayout(tree, runouts, players, 2).bytes + showdownBytes;
|
|
4289
|
+
const compressedBytes = buildLayout(tree, runouts, players, 2, historyAlias).bytes + showdownBytes;
|
|
4290
|
+
const walkedSlots = layout.walkedSlots;
|
|
4291
|
+
const compact = config.compactStorage ? compactPlan(config.compactStorage, runouts.rounds) : null;
|
|
4292
|
+
const compactBytes = compact ? buildLayout(tree, runouts, players, 4, historyAlias, compact).bytes + showdownBytes : null;
|
|
4293
|
+
const needed = compactBytes ?? compressedBytes;
|
|
2794
4294
|
const suggestions = [];
|
|
2795
|
-
if (
|
|
4295
|
+
if (needed > maxBytes) {
|
|
2796
4296
|
const widest = Math.max(...[0, 1].flatMap((p) => forPlayer(rows, p).map((r) => r.length)));
|
|
2797
4297
|
if (widest > 1) suggestions.push(`use fewer bet sizes (currently up to ${widest} per round)`);
|
|
2798
4298
|
const raises = [0, 1].flatMap((p) => [forPlayer(config.maxRaises ?? 1, p)].flat());
|
|
@@ -2808,8 +4308,9 @@ function defineSpot(config) {
|
|
|
2808
4308
|
totalSlots: layout.totalSlots,
|
|
2809
4309
|
bytes,
|
|
2810
4310
|
compressedBytes,
|
|
2811
|
-
|
|
2812
|
-
|
|
4311
|
+
compactBytes,
|
|
4312
|
+
secondsPerThousandIterations: walkedSlots * 4 * 1e3 / OPS_PER_SECOND,
|
|
4313
|
+
fits: needed <= maxBytes,
|
|
2813
4314
|
suggestions
|
|
2814
4315
|
};
|
|
2815
4316
|
if (!estimate.fits) throw new SpotTooLargeError(estimate, maxBytes);
|
|
@@ -2823,30 +4324,107 @@ function defineSpot(config) {
|
|
|
2823
4324
|
potUnits: Math.round(config.pot * chipScale),
|
|
2824
4325
|
stackUnits: Math.round(config.stack * chipScale),
|
|
2825
4326
|
source: config,
|
|
4327
|
+
...historyAlias ? { historyAlias } : {},
|
|
2826
4328
|
resolveBoard(cards) {
|
|
2827
4329
|
return resolveBoard(runouts, typeof cards === "string" ? parseCards(cards) : cards);
|
|
2828
4330
|
}
|
|
2829
4331
|
};
|
|
2830
4332
|
}
|
|
4333
|
+
function shareHistories(tree, runouts, config, chipScale) {
|
|
4334
|
+
const asked = config.shareHistories === true ? {} : config.shareHistories || {};
|
|
4335
|
+
const keep = asked.keepLines && asked.keepLines.length > 0 ? keptNodes(tree, chipScale, asked.keepLines) : void 0;
|
|
4336
|
+
return aliasHistories(tree, runouts, { keep }).alias;
|
|
4337
|
+
}
|
|
4338
|
+
function keptNodes(tree, chipScale, lines) {
|
|
4339
|
+
const parent = new Int32Array(tree.nodeCount).fill(-1);
|
|
4340
|
+
const paths = new Array(tree.nodeCount);
|
|
4341
|
+
paths[0] = "(root)";
|
|
4342
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
4343
|
+
for (let e = 0; e < tree.numEdges[node]; e++) {
|
|
4344
|
+
const edge = tree.firstEdge[node] + e;
|
|
4345
|
+
const child = tree.edgeChild[edge];
|
|
4346
|
+
parent[child] = node;
|
|
4347
|
+
const step = tree.kind[node] === NodeKind.Decision ? actionLabel(tree.edgeAction[edge], tree.edgeTarget[edge], chipScale) : "/";
|
|
4348
|
+
paths[child] = node === 0 ? step : `${paths[node]} ${step}`;
|
|
4349
|
+
}
|
|
4350
|
+
}
|
|
4351
|
+
const listed = new Set(lines);
|
|
4352
|
+
const missing = new Set(lines);
|
|
4353
|
+
const marks = new Uint8Array(tree.nodeCount);
|
|
4354
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
4355
|
+
if (listed.has(paths[node])) {
|
|
4356
|
+
missing.delete(paths[node]);
|
|
4357
|
+
marks[node] = 1;
|
|
4358
|
+
} else if (parent[node] >= 0 && marks[parent[node]]) {
|
|
4359
|
+
marks[node] = 1;
|
|
4360
|
+
}
|
|
4361
|
+
}
|
|
4362
|
+
if (missing.size > 0) {
|
|
4363
|
+
throw new Error(`shareHistories.keepLines: no node of this tree has the path ${[...missing].map((l) => `"${l}"`).join(", ")}`);
|
|
4364
|
+
}
|
|
4365
|
+
for (let node = tree.nodeCount - 1; node > 0; node--) if (marks[node]) marks[parent[node]] = 1;
|
|
4366
|
+
return marks;
|
|
4367
|
+
}
|
|
2831
4368
|
var PRECISION_BYTES = { f64: 8, f32: 4, i16: 2 };
|
|
4369
|
+
var TREE_SETTINGS = [
|
|
4370
|
+
"betSizes",
|
|
4371
|
+
"raiseSizes",
|
|
4372
|
+
"maxRaises",
|
|
4373
|
+
"allowAllIn",
|
|
4374
|
+
"allInThreshold",
|
|
4375
|
+
"noDonk",
|
|
4376
|
+
"mergeSizes",
|
|
4377
|
+
"thinSizes"
|
|
4378
|
+
];
|
|
2832
4379
|
function createRun(spot, options = {}) {
|
|
4380
|
+
if (options.analysisOnly && options.approximations?.currentProfileEv) {
|
|
4381
|
+
throw new Error("approximations.currentProfileEv needs regrets, and an analysisOnly run has none");
|
|
4382
|
+
}
|
|
2833
4383
|
const maxBytes = spot.source.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
2834
|
-
const {
|
|
2835
|
-
const
|
|
4384
|
+
const { bytes } = spot.estimate;
|
|
4385
|
+
const compact = spot.source.compactStorage ? compactPlan(spot.source.compactStorage, spot.runouts.rounds) : null;
|
|
4386
|
+
if (compact) {
|
|
4387
|
+
const clashes = [
|
|
4388
|
+
// An analysis-only run loads float32 sums, which compact nodes do not have.
|
|
4389
|
+
[options.analysisOnly, "analysisOnly"]
|
|
4390
|
+
];
|
|
4391
|
+
const clash = clashes.find(([set]) => set)?.[1];
|
|
4392
|
+
if (clash) throw new Error(`compactStorage does not combine with ${clash}`);
|
|
4393
|
+
}
|
|
4394
|
+
const precision = compact ? options.precision ?? "f32" : options.analysisOnly ? "f32" : options.precision ?? (bytes <= maxBytes ? "f32" : "i16");
|
|
4395
|
+
const coarsePlan = planCoarseStart(spot, options, precision, maxBytes);
|
|
2836
4396
|
const relevant = new Uint8Array(COMBOS);
|
|
2837
4397
|
for (const combo of spot.players[0].combos) relevant[combo] = 1;
|
|
2838
4398
|
for (const combo of spot.players[1].combos) relevant[combo] = 1;
|
|
2839
4399
|
const showdown = buildShowdownTable(spot.runouts, relevant);
|
|
2840
|
-
const layout = buildLayout(
|
|
4400
|
+
const layout = buildLayout(
|
|
4401
|
+
spot.tree,
|
|
4402
|
+
spot.runouts,
|
|
4403
|
+
spot.players,
|
|
4404
|
+
PRECISION_BYTES[precision],
|
|
4405
|
+
spot.historyAlias,
|
|
4406
|
+
compact ?? void 0
|
|
4407
|
+
);
|
|
2841
4408
|
const weights = runoutWeights(spot.runouts);
|
|
2842
|
-
const
|
|
2843
|
-
const
|
|
4409
|
+
const storageEngine = options.wasmStorage !== void 0 && precision !== "f64" && (precision === "i16" || compact !== null);
|
|
4410
|
+
const wantsEngine = options.engine !== "js" && !options.analysisOnly && options.simd !== false && (precision === "f32" && !compact || storageEngine);
|
|
4411
|
+
const samplingOptions = options.approximations?.chanceSampling;
|
|
4412
|
+
const requested = resolveWorkers(options, wantsEngine);
|
|
4413
|
+
const shards = requested > 1 ? planShards(spot.tree, spot.runouts, requested) : null;
|
|
4414
|
+
const plan = shards && samplingOptions && sampledRounds(samplingOptions, spot.runouts.rounds).some((r) => r < shards.round) ? null : shards;
|
|
2844
4415
|
const allocate = (bytes2) => plan ? new SharedArrayBuffer(bytes2) : new ArrayBuffer(bytes2);
|
|
2845
4416
|
const width = PRECISION_BYTES[precision];
|
|
2846
4417
|
const quantised = precision === "i16";
|
|
2847
4418
|
const active = buildActiveLists(spot.runouts, spot.players, showdown);
|
|
2848
4419
|
const hands = buildHandSpace(spot.runouts, spot.players, active);
|
|
2849
|
-
const
|
|
4420
|
+
const samplingPlan = samplingOptions ? planSampling(spot.tree, spot.runouts, active, layout, samplingOptions) : void 0;
|
|
4421
|
+
const storageBytes = compact ? layout.bytes : Math.max(quantised ? spot.estimate.compressedBytes : bytes, layout.bytes);
|
|
4422
|
+
if (samplingPlan && storageBytes + samplingBytes(samplingPlan) > maxBytes) {
|
|
4423
|
+
throw new Error(
|
|
4424
|
+
`chanceSampling needs ${Math.ceil(samplingBytes(samplingPlan) / 2 ** 20)} MiB of baselines beside ${Math.ceil(storageBytes / 2 ** 20)} MiB of storage, over maxBytes; sample fewer rounds or raise maxBytes`
|
|
4425
|
+
);
|
|
4426
|
+
}
|
|
4427
|
+
const engine = wantsEngine ? createEngine(
|
|
2850
4428
|
{
|
|
2851
4429
|
tree: spot.tree,
|
|
2852
4430
|
runouts: spot.runouts,
|
|
@@ -2855,10 +4433,13 @@ function createRun(spot, options = {}) {
|
|
|
2855
4433
|
active,
|
|
2856
4434
|
hands,
|
|
2857
4435
|
runoutWeight: weights,
|
|
2858
|
-
chipScale: spot.chipScale
|
|
4436
|
+
chipScale: spot.chipScale,
|
|
4437
|
+
sampling: samplingPlan,
|
|
4438
|
+
...storageEngine ? { storage: { source: options.wasmStorage, quantised, compact } } : {}
|
|
2859
4439
|
},
|
|
2860
4440
|
plan ? { plan, threads: requested, spawn: spawnEngineWorker } : void 0
|
|
2861
4441
|
) : null;
|
|
4442
|
+
const engineCompact = engine?.compact ?? null;
|
|
2862
4443
|
const ctx = {
|
|
2863
4444
|
tree: spot.tree,
|
|
2864
4445
|
runouts: spot.runouts,
|
|
@@ -2867,8 +4448,16 @@ function createRun(spot, options = {}) {
|
|
|
2867
4448
|
showdown,
|
|
2868
4449
|
regret: engine ? engine.regret : options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
|
|
2869
4450
|
strategySum: engine ? engine.strategySum : makeSumStore(precision, allocate(layout.totalSlots * width)),
|
|
2870
|
-
regretScale: quantised ? new Float64Array(allocate(layout.blocks * 8))
|
|
2871
|
-
sumScale: quantised ? new Float64Array(allocate(layout.blocks * 8))
|
|
4451
|
+
regretScale: !quantised ? null : engine ? engine.regretScale : new Float64Array(allocate(layout.blocks * 8)),
|
|
4452
|
+
sumScale: !quantised ? null : engine ? engine.sumScale : new Float64Array(allocate(layout.blocks * 8)),
|
|
4453
|
+
compact,
|
|
4454
|
+
compactRegret: !compact ? null : engineCompact ? engineCompact.regret : layout.chunkSlots.map(
|
|
4455
|
+
(slots) => compact.float ? new Float32Array(allocate(slots * 4)) : new Int8Array(allocate(slots))
|
|
4456
|
+
),
|
|
4457
|
+
handShift: !compact?.perHand ? null : engineCompact ? engineCompact.handShift : layout.chunkHands.map((hands2) => new Uint8Array(allocate(hands2))),
|
|
4458
|
+
compactSum: !compact?.sums ? null : engineCompact ? engineCompact.sum : layout.chunkSlots.map((slots) => new Uint8Array(allocate(slots))),
|
|
4459
|
+
sumShift: !compact?.sums ? null : engineCompact ? engineCompact.sumShift : layout.chunkHands.map((hands2) => new Uint8Array(allocate(hands2))),
|
|
4460
|
+
compactScale: !compact || compact.perHand || compact.float ? null : engineCompact ? engineCompact.scale : new Float64Array(allocate(layout.blocks * 8)),
|
|
2872
4461
|
scratch: makeTerminalScratch(),
|
|
2873
4462
|
chipScale: spot.chipScale,
|
|
2874
4463
|
active,
|
|
@@ -2890,15 +4479,37 @@ function createRun(spot, options = {}) {
|
|
|
2890
4479
|
totalMass: matchupMass(spot.players[0].weights, spot.players[1].weights) * weights[0],
|
|
2891
4480
|
dcfr: (options.algorithm ?? "dcfr") === "dcfr"
|
|
2892
4481
|
};
|
|
4482
|
+
if (samplingPlan) ctx.sampling = makeSampling(samplingPlan, engine?.sampling ?? void 0, allocate);
|
|
4483
|
+
if (layout.shared) ctx.discounted = engine?.discounted ?? new Int32Array(layout.blocks);
|
|
2893
4484
|
let warm = null;
|
|
2894
4485
|
let offset = 0;
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
4486
|
+
let solver;
|
|
4487
|
+
let pool;
|
|
4488
|
+
try {
|
|
4489
|
+
if (options.warmCards && !options.warmStart) {
|
|
4490
|
+
throw new Error("`warmCards` relabels a warm start, so it needs `warmStart` too");
|
|
4491
|
+
}
|
|
4492
|
+
if (options.warmStart) {
|
|
4493
|
+
const source = options.warmStart;
|
|
4494
|
+
warm = options.warmCards ? warmStart(ctx, source.solver.ctx, false, options.warmCards) : warmStart(ctx, source.solver.ctx, sameSpot(spot.source, source.spot.source));
|
|
4495
|
+
offset = options.warmIteration ?? source.totalIterations;
|
|
4496
|
+
} else if (coarsePlan && coarsePlan.iterations > 0) {
|
|
4497
|
+
const coarse = solveCoarse(coarsePlan, options);
|
|
4498
|
+
try {
|
|
4499
|
+
warm = warmStart(ctx, coarse.solver.ctx, false);
|
|
4500
|
+
} finally {
|
|
4501
|
+
coarse.close();
|
|
4502
|
+
}
|
|
4503
|
+
offset = options.warmIteration ?? coarse.totalIterations;
|
|
4504
|
+
}
|
|
4505
|
+
if (warm) ctx.sampling?.lastUpdate.fill(offset);
|
|
4506
|
+
solver = makeSolver(ctx);
|
|
4507
|
+
if (warm && coarsePlan) coarseMs.set(solver, coarsePlan.elapsed);
|
|
4508
|
+
pool = plan && !engine ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd, compact }, requested) : null;
|
|
4509
|
+
} catch (error) {
|
|
4510
|
+
engine?.close();
|
|
4511
|
+
throw error;
|
|
2899
4512
|
}
|
|
2900
|
-
const solver = makeSolver(ctx);
|
|
2901
|
-
const pool = plan && !engine ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
|
|
2902
4513
|
if (pool) solver.useRoot(pool.traverse);
|
|
2903
4514
|
if (engine) solver.useRoot(engine.root(ctx.dcfr));
|
|
2904
4515
|
const threadCount = pool ? pool.workers : engine ? engine.threads : 1;
|
|
@@ -2932,7 +4543,8 @@ function createRun(spot, options = {}) {
|
|
|
2932
4543
|
if (!spot.runouts.approximate) return this.exploitabilityPercent();
|
|
2933
4544
|
const real = createRun(defineSpot({ ...spot.source, runoutBuckets: false }), {
|
|
2934
4545
|
workers: threadCount,
|
|
2935
|
-
precision
|
|
4546
|
+
precision,
|
|
4547
|
+
wasmStorage: options.wasmStorage
|
|
2936
4548
|
});
|
|
2937
4549
|
try {
|
|
2938
4550
|
warmStart(real.solver.ctx, ctx, true);
|
|
@@ -2942,13 +4554,118 @@ function createRun(spot, options = {}) {
|
|
|
2942
4554
|
}
|
|
2943
4555
|
},
|
|
2944
4556
|
snapshot(history = [], stoppedAt = "iterations") {
|
|
2945
|
-
return makeSolution(
|
|
4557
|
+
return makeSolution(
|
|
4558
|
+
spot,
|
|
4559
|
+
solver,
|
|
4560
|
+
completed,
|
|
4561
|
+
offset + completed,
|
|
4562
|
+
[...history],
|
|
4563
|
+
stoppedAt,
|
|
4564
|
+
threadCount,
|
|
4565
|
+
options.approximations?.currentProfileEv === true
|
|
4566
|
+
);
|
|
2946
4567
|
}
|
|
2947
4568
|
};
|
|
2948
4569
|
}
|
|
4570
|
+
function compactPlan(settings, rounds) {
|
|
4571
|
+
const from = settings.from ?? 0;
|
|
4572
|
+
if (!Number.isInteger(from) || from < 0 || from >= rounds) {
|
|
4573
|
+
throw new Error(`compactStorage.from must be a round of this spot, 0 to ${rounds - 1}; got ${from}`);
|
|
4574
|
+
}
|
|
4575
|
+
const chunkBytes = settings.chunkBytes ?? 2 ** 30;
|
|
4576
|
+
if (!Number.isInteger(chunkBytes) || chunkBytes < 1) {
|
|
4577
|
+
throw new Error(`compactStorage.chunkBytes must be a positive whole number of bytes; got ${chunkBytes}`);
|
|
4578
|
+
}
|
|
4579
|
+
const steps = settings.steps ?? 127;
|
|
4580
|
+
if (!Number.isInteger(steps) || steps < 1 || steps > 127) {
|
|
4581
|
+
throw new Error(`compactStorage.steps must be an integer from 1 to 127; got ${steps}`);
|
|
4582
|
+
}
|
|
4583
|
+
if (settings.sums !== false && (settings.blockScale || settings.float)) {
|
|
4584
|
+
throw new Error("compactStorage.sums keeps a scale per hand, so blockScale and float need sums: false");
|
|
4585
|
+
}
|
|
4586
|
+
return {
|
|
4587
|
+
from,
|
|
4588
|
+
steps,
|
|
4589
|
+
perHand: !settings.blockScale && !settings.float,
|
|
4590
|
+
stochastic: !settings.nearest,
|
|
4591
|
+
float: settings.float === true,
|
|
4592
|
+
chunkBytes,
|
|
4593
|
+
sums: settings.sums ?? true
|
|
4594
|
+
};
|
|
4595
|
+
}
|
|
4596
|
+
var DEFAULT_COARSE_GAP = 3;
|
|
4597
|
+
var coarseMs = /* @__PURE__ */ new WeakMap();
|
|
4598
|
+
function planCoarseStart(spot, options, precision, maxBytes) {
|
|
4599
|
+
const settings = options.approximations?.coarseStart;
|
|
4600
|
+
if (!settings) return null;
|
|
4601
|
+
const { iterations, runoutBuckets = DEFAULT_COARSE_GAP } = settings;
|
|
4602
|
+
if (!(Number.isInteger(iterations) && iterations >= 0)) {
|
|
4603
|
+
throw new Error(`coarseStart.iterations must be a whole number of iterations; got ${iterations}`);
|
|
4604
|
+
}
|
|
4605
|
+
if (!(Number.isInteger(runoutBuckets) && runoutBuckets >= 1)) {
|
|
4606
|
+
throw new Error(`coarseStart.runoutBuckets must be a rank gap of 1 or more; got ${runoutBuckets}`);
|
|
4607
|
+
}
|
|
4608
|
+
if (options.warmStart) throw new Error("coarseStart is a warm start of its own; give one or the other, not both");
|
|
4609
|
+
if (options.analysisOnly) throw new Error("coarseStart solves; an analysis-only run must never be stepped");
|
|
4610
|
+
const own = spot.source.runoutBuckets;
|
|
4611
|
+
const ownGap = own === true ? DEFAULT_BUCKET_GAP : own || 0;
|
|
4612
|
+
if (iterations === 0 || spot.board.length === 5 || runoutBuckets <= ownGap) return null;
|
|
4613
|
+
const coarseSpot = defineSpot({ ...spot.source, runoutBuckets });
|
|
4614
|
+
if (coarseSpot.runouts.counts.at(-1) >= spot.runouts.counts.at(-1)) return null;
|
|
4615
|
+
const used = storageCost(spot, precision);
|
|
4616
|
+
const left = maxBytes - used;
|
|
4617
|
+
const coarsePrecision = options.precision ?? (storageCost(coarseSpot, "f32") <= left ? "f32" : "i16");
|
|
4618
|
+
const needs = storageCost(coarseSpot, coarsePrecision);
|
|
4619
|
+
if (needs > left) {
|
|
4620
|
+
throw new Error(
|
|
4621
|
+
`coarseStart needs ~${formatBytes(needs)} beside this spot's ~${formatBytes(used)}, over the ${formatBytes(maxBytes)} limit; raise maxBytes or leave coarseStart off`
|
|
4622
|
+
);
|
|
4623
|
+
}
|
|
4624
|
+
return { spot: coarseSpot, iterations, precision: coarsePrecision, elapsed: 0 };
|
|
4625
|
+
}
|
|
4626
|
+
function solveCoarse(plan, options) {
|
|
4627
|
+
const started = Date.now();
|
|
4628
|
+
const { coarseStart: _, ...rest } = options.approximations;
|
|
4629
|
+
const run = createRun(plan.spot, {
|
|
4630
|
+
workers: options.workers,
|
|
4631
|
+
precision: plan.precision,
|
|
4632
|
+
algorithm: options.algorithm,
|
|
4633
|
+
simd: options.simd,
|
|
4634
|
+
engine: options.engine,
|
|
4635
|
+
wasmStorage: options.wasmStorage,
|
|
4636
|
+
approximations: rest
|
|
4637
|
+
});
|
|
4638
|
+
try {
|
|
4639
|
+
run.step(plan.iterations);
|
|
4640
|
+
} catch (error) {
|
|
4641
|
+
run.close();
|
|
4642
|
+
throw error;
|
|
4643
|
+
}
|
|
4644
|
+
plan.elapsed = Date.now() - started;
|
|
4645
|
+
return run;
|
|
4646
|
+
}
|
|
4647
|
+
function storageCost(of, at) {
|
|
4648
|
+
if (of.estimate.compactBytes === null) {
|
|
4649
|
+
return at === "i16" ? of.estimate.compressedBytes : of.estimate.bytes * (at === "f64" ? 2 : 1);
|
|
4650
|
+
}
|
|
4651
|
+
const plan = compactPlan(of.source.compactStorage, of.runouts.rounds);
|
|
4652
|
+
const bytesAt = (width) => buildLayout(of.tree, of.runouts, of.players, width, of.historyAlias, plan).bytes;
|
|
4653
|
+
return of.estimate.compactBytes + bytesAt(PRECISION_BYTES[at]) - bytesAt(4);
|
|
4654
|
+
}
|
|
2949
4655
|
function sameSpot(a, b) {
|
|
2950
4656
|
const plain = ({ maxBytes: _bytes, maxCombos: _combos, isomorphism: _iso, ...game }) => JSON.stringify(game, (_, value) => value instanceof Float64Array ? Array.from(value) : value);
|
|
2951
|
-
|
|
4657
|
+
const kept = (config) => typeof config.shareHistories === "object" ? config.shareHistories.keepLines : void 0;
|
|
4658
|
+
if (kept(a) === void 0 && kept(b) === void 0) return plain(a) === plain(b);
|
|
4659
|
+
const mine = new Set(kept(a) ?? []);
|
|
4660
|
+
if (!(kept(b) ?? []).every((line) => mine.has(line))) return false;
|
|
4661
|
+
return plain(withoutKeptLines(a)) === plain(withoutKeptLines(b));
|
|
4662
|
+
}
|
|
4663
|
+
function withoutKeptLines(config) {
|
|
4664
|
+
const asked = config.shareHistories;
|
|
4665
|
+
if (!asked) return config;
|
|
4666
|
+
const { keepLines: _kept, ...rest } = asked === true ? {} : asked;
|
|
4667
|
+
const set = Object.entries(rest).filter(([, value]) => value !== void 0);
|
|
4668
|
+
return { ...config, shareHistories: set.length > 0 ? Object.fromEntries(set) : true };
|
|
2952
4669
|
}
|
|
2953
4670
|
var DEFAULT_BUCKET_GAP = 1;
|
|
2954
4671
|
function rangeRankMerge(players, maxGap) {
|
|
@@ -2969,16 +4686,18 @@ function rangeRankMerge(players, maxGap) {
|
|
|
2969
4686
|
}
|
|
2970
4687
|
return { ranks: (a, b) => allowed[a * 13 + b] === 1, maxGap };
|
|
2971
4688
|
}
|
|
2972
|
-
var
|
|
2973
|
-
function resolveWorkers(options) {
|
|
4689
|
+
var threadWarnings = /* @__PURE__ */ new Set();
|
|
4690
|
+
function resolveWorkers(options, engine) {
|
|
2974
4691
|
if (options.analysisOnly) return 1;
|
|
2975
4692
|
const asked = options.workers === "auto" ? defaultWorkerCount() : options.workers ?? 1;
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
4693
|
+
const limit = threadLimit(engine);
|
|
4694
|
+
if (asked > limit) {
|
|
4695
|
+
const advice = threadAdvice(engine);
|
|
4696
|
+
if (!threadWarnings.has(advice)) {
|
|
4697
|
+
threadWarnings.add(advice);
|
|
4698
|
+
console.warn(`poker-flopsolve: ${asked} threads asked for, solving on ${limit} (${PLATFORM} build): ${advice}`);
|
|
2980
4699
|
}
|
|
2981
|
-
return
|
|
4700
|
+
return limit;
|
|
2982
4701
|
}
|
|
2983
4702
|
return asked;
|
|
2984
4703
|
}
|
|
@@ -2994,15 +4713,18 @@ function solve(spot, options = {}) {
|
|
|
2994
4713
|
const iterations = options.iterations ?? 500;
|
|
2995
4714
|
const target = options.targetPercent ?? 0;
|
|
2996
4715
|
const fixed = options.exploitEvery ?? 0;
|
|
4716
|
+
const settle = options.settle ? settlePlan(options.settle) : null;
|
|
2997
4717
|
const run = createRun(spot, options);
|
|
2998
4718
|
const history = [];
|
|
2999
|
-
const started = Date.now();
|
|
4719
|
+
const started = Date.now() - (coarseMs.get(run.solver) ?? 0);
|
|
4720
|
+
const before = coarseMs.has(run.solver) ? run.totalIterations : 0;
|
|
3000
4721
|
try {
|
|
3001
4722
|
return runAll();
|
|
3002
4723
|
} finally {
|
|
3003
4724
|
run.close();
|
|
3004
4725
|
}
|
|
3005
4726
|
function runAll() {
|
|
4727
|
+
if (settle) return runSettled(settle);
|
|
3006
4728
|
if (fixed <= 0 && target <= 0) {
|
|
3007
4729
|
run.step(iterations);
|
|
3008
4730
|
return run.snapshot(history, "iterations");
|
|
@@ -3032,13 +4754,13 @@ function solve(spot, options = {}) {
|
|
|
3032
4754
|
next = done + fixed;
|
|
3033
4755
|
} else {
|
|
3034
4756
|
const predicted = predictCrossing(
|
|
3035
|
-
prevIteration,
|
|
4757
|
+
before + prevIteration,
|
|
3036
4758
|
prevExploitability,
|
|
3037
|
-
done,
|
|
4759
|
+
before + done,
|
|
3038
4760
|
info.exploitability,
|
|
3039
4761
|
target * spot.potUnits / (100 * spot.chipScale)
|
|
3040
4762
|
);
|
|
3041
|
-
const aim = predicted > 0 ? predicted * 0.95 : done * MAX_GROWTH;
|
|
4763
|
+
const aim = predicted > 0 ? predicted * 0.95 - before : done * MAX_GROWTH;
|
|
3042
4764
|
const bounded = Math.min(Math.max(aim, done * MIN_GROWTH), done * MAX_GROWTH);
|
|
3043
4765
|
next = Math.max(done + 1, Math.ceil(bounded));
|
|
3044
4766
|
prevIteration = done;
|
|
@@ -3047,6 +4769,36 @@ function solve(spot, options = {}) {
|
|
|
3047
4769
|
}
|
|
3048
4770
|
return run.snapshot(history, stoppedAt);
|
|
3049
4771
|
}
|
|
4772
|
+
function runSettled(plan) {
|
|
4773
|
+
const tracker = trackSettle(plan);
|
|
4774
|
+
let stoppedAt = "iterations";
|
|
4775
|
+
let done = 0;
|
|
4776
|
+
while (done < iterations) {
|
|
4777
|
+
const until = Math.min(done + plan.every, iterations);
|
|
4778
|
+
const full = until - done === plan.every;
|
|
4779
|
+
run.step(until - done);
|
|
4780
|
+
done = until;
|
|
4781
|
+
const measured = run.snapshot();
|
|
4782
|
+
const settled = full && tracker.check(measured, done);
|
|
4783
|
+
const info = {
|
|
4784
|
+
iteration: done,
|
|
4785
|
+
exploitability: target > 0 ? measured.exploitability() : Number.NaN,
|
|
4786
|
+
elapsedMs: Date.now() - started,
|
|
4787
|
+
movement: full ? tracker.movement : Number.NaN
|
|
4788
|
+
};
|
|
4789
|
+
history.push(info);
|
|
4790
|
+
options.onProgress?.(info);
|
|
4791
|
+
if (target > 0 && measured.exploitabilityPercent() <= target) {
|
|
4792
|
+
stoppedAt = "target";
|
|
4793
|
+
break;
|
|
4794
|
+
}
|
|
4795
|
+
if (settled) {
|
|
4796
|
+
stoppedAt = "settled";
|
|
4797
|
+
break;
|
|
4798
|
+
}
|
|
4799
|
+
}
|
|
4800
|
+
return run.snapshot(history, stoppedAt);
|
|
4801
|
+
}
|
|
3050
4802
|
}
|
|
3051
4803
|
var FIRST_CHECK = 10;
|
|
3052
4804
|
var MIN_GROWTH = 1.1;
|
|
@@ -3058,7 +4810,7 @@ function predictCrossing(t1, e1, t2, e2, target) {
|
|
|
3058
4810
|
const predicted = t2 * (e2 / target) ** (1 / p);
|
|
3059
4811
|
return Number.isFinite(predicted) ? predicted : 0;
|
|
3060
4812
|
}
|
|
3061
|
-
function makeSolution(spot,
|
|
4813
|
+
function makeSolution(spot, solver, iterations, totalIterations, history, stoppedAt, workers, currentProfileEv) {
|
|
3062
4814
|
const { tree } = spot;
|
|
3063
4815
|
const parent = new Int32Array(tree.nodeCount).fill(-1);
|
|
3064
4816
|
const viaEdge = new Int32Array(tree.nodeCount).fill(-1);
|
|
@@ -3069,10 +4821,16 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3069
4821
|
viaEdge[tree.edgeChild[edge]] = edge;
|
|
3070
4822
|
}
|
|
3071
4823
|
}
|
|
3072
|
-
const label = (edge) =>
|
|
3073
|
-
|
|
3074
|
-
const
|
|
3075
|
-
|
|
4824
|
+
const label = (edge) => actionLabel(tree.edgeAction[edge], tree.edgeTarget[edge], spot.chipScale);
|
|
4825
|
+
const path = (node) => {
|
|
4826
|
+
const steps = [];
|
|
4827
|
+
let current = node;
|
|
4828
|
+
while (parent[current] >= 0) {
|
|
4829
|
+
const owner = parent[current];
|
|
4830
|
+
steps.push(tree.kind[owner] === NodeKind.Decision ? label(viaEdge[current]) : "/");
|
|
4831
|
+
current = owner;
|
|
4832
|
+
}
|
|
4833
|
+
return steps.reverse().join(" ") || "(root)";
|
|
3076
4834
|
};
|
|
3077
4835
|
const reachAt = (node, board, player) => {
|
|
3078
4836
|
const index = spot.players[player];
|
|
@@ -3090,6 +4848,8 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3090
4848
|
return reach;
|
|
3091
4849
|
};
|
|
3092
4850
|
let cachedEv = null;
|
|
4851
|
+
const rootValues = () => cachedEv ??= solver.rootValues(currentProfileEv ? Mode.Current : Mode.Average);
|
|
4852
|
+
const cachedHands = [null, null];
|
|
3093
4853
|
let cachedExploit = null;
|
|
3094
4854
|
return {
|
|
3095
4855
|
spot,
|
|
@@ -3100,8 +4860,11 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3100
4860
|
solver,
|
|
3101
4861
|
history,
|
|
3102
4862
|
ev(player) {
|
|
3103
|
-
|
|
3104
|
-
|
|
4863
|
+
return rootValues().totals[player];
|
|
4864
|
+
},
|
|
4865
|
+
handEvs(player) {
|
|
4866
|
+
cachedHands[player] ??= solver.handEvs(player, rootValues().values[player]);
|
|
4867
|
+
return cachedHands[player].slice();
|
|
3105
4868
|
},
|
|
3106
4869
|
exploitability() {
|
|
3107
4870
|
cachedExploit ??= solver.exploitability();
|
|
@@ -3119,16 +4882,7 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3119
4882
|
for (let a = 0; a < actions; a++) names.push(label(tree.firstEdge[node] + a));
|
|
3120
4883
|
return { node, board, player: owner, actions: names, combos: spot.players[owner].combos, probs };
|
|
3121
4884
|
},
|
|
3122
|
-
path
|
|
3123
|
-
const steps = [];
|
|
3124
|
-
let current = node;
|
|
3125
|
-
while (parent[current] >= 0) {
|
|
3126
|
-
const owner = parent[current];
|
|
3127
|
-
steps.push(tree.kind[owner] === NodeKind.Decision ? label(viaEdge[current]) : "/");
|
|
3128
|
-
current = owner;
|
|
3129
|
-
}
|
|
3130
|
-
return steps.reverse().join(" ") || "(root)";
|
|
3131
|
-
},
|
|
4885
|
+
path,
|
|
3132
4886
|
reachAt,
|
|
3133
4887
|
subgameAt(node, board, betting = {}) {
|
|
3134
4888
|
const opensRound = node === 0 || parent[node] >= 0 && tree.kind[parent[node]] === NodeKind.Chance;
|
|
@@ -3146,7 +4900,20 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3146
4900
|
const later = (value, listed) => listed && value.length > 1 ? value.slice(round) : value;
|
|
3147
4901
|
const laterEach = (value, listed) => value === void 0 ? void 0 : mapPlayers(value, (side) => later(side, listed(side)));
|
|
3148
4902
|
const rows = laterEach(source.betSizes, () => true);
|
|
3149
|
-
const
|
|
4903
|
+
const raiseRows2 = laterEach(source.raiseSizes, (side) => side !== void 0 && raisesPerRound(side));
|
|
4904
|
+
const keeps = (key) => key === "thinSizes" ? !betting.thinSizes : betting[key] === void 0 && ["betSizes", "maxRaises", "allowAllIn"].includes(key);
|
|
4905
|
+
const reshaped = TREE_SETTINGS.some((key) => key in betting && !keeps(key));
|
|
4906
|
+
const prefix = node === 0 ? "" : `${path(node)} `;
|
|
4907
|
+
const rebase = (step) => {
|
|
4908
|
+
const [label2, chips] = step.split(" ");
|
|
4909
|
+
return chips === void 0 ? step : `${label2} ${(Math.round(Number(chips) * spot.chipScale) - invested) / spot.chipScale}`;
|
|
4910
|
+
};
|
|
4911
|
+
const cuts = source.removeActions?.filter((line) => line.startsWith(prefix)).map((line) => line.slice(prefix.length).split(" ").reduce((steps, word, i, words) => {
|
|
4912
|
+
if (/^-?[\d.]+$/.test(word)) steps[steps.length - 1] = rebase(`${words[i - 1]} ${word}`);
|
|
4913
|
+
else steps.push(word);
|
|
4914
|
+
return steps;
|
|
4915
|
+
}, []).join(" "));
|
|
4916
|
+
const removeActions = "removeActions" in betting ? betting.removeActions : reshaped || !cuts || cuts.length === 0 ? void 0 : cuts;
|
|
3150
4917
|
return {
|
|
3151
4918
|
board: formatCards(cards),
|
|
3152
4919
|
pot: tree.pot[node] / spot.chipScale,
|
|
@@ -3154,17 +4921,21 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3154
4921
|
oop: reachAt(node, board, 0),
|
|
3155
4922
|
ip: reachAt(node, board, 1),
|
|
3156
4923
|
betSizes: betting.betSizes ?? rows,
|
|
3157
|
-
raiseSizes: "raiseSizes" in betting ? betting.raiseSizes :
|
|
4924
|
+
raiseSizes: "raiseSizes" in betting ? betting.raiseSizes : raiseRows2,
|
|
3158
4925
|
maxRaises: betting.maxRaises ?? laterEach(source.maxRaises, Array.isArray),
|
|
3159
4926
|
allowAllIn: betting.allowAllIn ?? laterEach(source.allowAllIn, Array.isArray),
|
|
3160
4927
|
allInThreshold: "allInThreshold" in betting ? betting.allInThreshold : laterEach(source.allInThreshold, Array.isArray),
|
|
3161
4928
|
noDonk: "noDonk" in betting ? betting.noDonk : source.noDonk === void 0 ? void 0 : later(source.noDonk, Array.isArray(source.noDonk)),
|
|
3162
4929
|
mergeSizes: "mergeSizes" in betting ? betting.mergeSizes : source.mergeSizes,
|
|
4930
|
+
...removeActions ? { removeActions } : {},
|
|
4931
|
+
// The thinned shape is the whole tree's, so a subgame starts full.
|
|
4932
|
+
...betting.thinSizes ? { thinSizes: true } : {},
|
|
3163
4933
|
minWeight: source.minWeight,
|
|
3164
4934
|
isomorphism: source.isomorphism,
|
|
3165
4935
|
maxCombos: "maxCombos" in betting ? betting.maxCombos : source.maxCombos,
|
|
3166
4936
|
maxBytes: "maxBytes" in betting ? betting.maxBytes : source.maxBytes,
|
|
3167
|
-
chipScale: spot.chipScale
|
|
4937
|
+
chipScale: spot.chipScale,
|
|
4938
|
+
compactStorage: source.compactStorage
|
|
3168
4939
|
};
|
|
3169
4940
|
}
|
|
3170
4941
|
};
|
|
@@ -3185,8 +4956,10 @@ export {
|
|
|
3185
4956
|
SUITS,
|
|
3186
4957
|
SpotTooLargeError,
|
|
3187
4958
|
activeCombos,
|
|
4959
|
+
bettingFrom,
|
|
3188
4960
|
cardOf,
|
|
3189
4961
|
categoryOf,
|
|
4962
|
+
checkedDown,
|
|
3190
4963
|
comboIndex,
|
|
3191
4964
|
createRun,
|
|
3192
4965
|
defineSpot,
|
|
@@ -3195,6 +4968,10 @@ export {
|
|
|
3195
4968
|
formatCard,
|
|
3196
4969
|
formatCards,
|
|
3197
4970
|
formatCombo,
|
|
4971
|
+
keepWithin,
|
|
4972
|
+
linesWithin,
|
|
4973
|
+
liteConfig,
|
|
4974
|
+
litePreset,
|
|
3198
4975
|
makeRegretStore,
|
|
3199
4976
|
makeSumStore,
|
|
3200
4977
|
parseCard,
|
|
@@ -3203,9 +4980,18 @@ export {
|
|
|
3203
4980
|
predictCrossing,
|
|
3204
4981
|
rangeWeight,
|
|
3205
4982
|
rankOf,
|
|
4983
|
+
rankSwap,
|
|
4984
|
+
resolveSubgame,
|
|
4985
|
+
rootPlay,
|
|
4986
|
+
rootPlayMovement,
|
|
4987
|
+
rootPlayNodes,
|
|
3206
4988
|
simdAvailable,
|
|
3207
4989
|
solve,
|
|
4990
|
+
solveLite,
|
|
4991
|
+
storageCost,
|
|
3208
4992
|
suitLanes,
|
|
3209
|
-
suitOf
|
|
4993
|
+
suitOf,
|
|
4994
|
+
trackSettle,
|
|
4995
|
+
turnLineReach
|
|
3210
4996
|
};
|
|
3211
4997
|
//# sourceMappingURL=index.js.map
|