poker-flopsolve 0.4.0 → 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 +941 -86
- package/dist/browser-threads.js +238 -0
- package/dist/browser-threads.js.map +1 -0
- package/dist/browser.js +2200 -405
- 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 +2179 -397
- 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 +1038 -245
- 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);
|
|
@@ -221,14 +247,10 @@ function buildTree(config, runouts) {
|
|
|
221
247
|
if (target <= theirs) return null;
|
|
222
248
|
const raiseSize = target - theirs;
|
|
223
249
|
const minimum = Math.max(state.lastRaise, minRaiseUnits);
|
|
224
|
-
if (raiseSize < minimum)
|
|
225
|
-
const bumped = theirs + minimum;
|
|
226
|
-
if (bumped > cap) return null;
|
|
227
|
-
target = bumped;
|
|
228
|
-
}
|
|
250
|
+
if (raiseSize < minimum) target = Math.min(theirs + minimum, cap);
|
|
229
251
|
return target;
|
|
230
252
|
};
|
|
231
|
-
const buildDecision = (state) => {
|
|
253
|
+
const buildDecision = (state, path) => {
|
|
232
254
|
const me = state.toAct;
|
|
233
255
|
const opp = 1 - me;
|
|
234
256
|
const mine = state.invested[me];
|
|
@@ -247,6 +269,7 @@ function buildTree(config, runouts) {
|
|
|
247
269
|
const donk = noDonk[state.round] && me === 0 && owed === 0 && state.aggressions === 0 && state.previousAggressor === 1;
|
|
248
270
|
if (state.aggressions < maxRaises[me][state.round] && myRemaining > owed && !donk) {
|
|
249
271
|
const seen = /* @__PURE__ */ new Set();
|
|
272
|
+
const sized = [];
|
|
250
273
|
const myRaises = raiseSizes[me];
|
|
251
274
|
const sizes = owed > 0 && myRaises ? [...myRaises[state.round]] : [...betSizes[me][state.round] ?? []].filter((size) => !(typeof size === "object" && "times" in size));
|
|
252
275
|
if (allowAllIn[me][state.round]) sizes.push("allin");
|
|
@@ -258,10 +281,17 @@ function buildTree(config, runouts) {
|
|
|
258
281
|
if (target === null || seen.has(target)) continue;
|
|
259
282
|
seen.add(target);
|
|
260
283
|
const isShove = target >= stackUnits;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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);
|
|
265
295
|
}
|
|
266
296
|
}
|
|
267
297
|
const base = edgeChild.length;
|
|
@@ -273,6 +303,7 @@ function buildTree(config, runouts) {
|
|
|
273
303
|
edgeChild.push(-1);
|
|
274
304
|
}
|
|
275
305
|
actions.forEach((entry, i) => {
|
|
306
|
+
const line = remove ? extend(path, actionLabel(entry.action, entry.target, chipScale)) : "";
|
|
276
307
|
const invested = me === 0 ? [entry.target, theirs] : [theirs, entry.target];
|
|
277
308
|
let child;
|
|
278
309
|
if (entry.action === ActionKind.Fold) {
|
|
@@ -280,7 +311,7 @@ function buildTree(config, runouts) {
|
|
|
280
311
|
child = addNode(NodeKind.TerminalFold, -1, state.round, total, value);
|
|
281
312
|
} else if (entry.action === ActionKind.Check || entry.action === ActionKind.Call) {
|
|
282
313
|
const closed = invested[0] === invested[1] && state.acted + 1 >= 2;
|
|
283
|
-
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);
|
|
284
315
|
} else {
|
|
285
316
|
child = buildDecision({
|
|
286
317
|
round: state.round,
|
|
@@ -291,8 +322,9 @@ function buildTree(config, runouts) {
|
|
|
291
322
|
acted: state.acted + 1,
|
|
292
323
|
base: state.base,
|
|
293
324
|
aggressor: me,
|
|
294
|
-
previousAggressor: state.previousAggressor
|
|
295
|
-
|
|
325
|
+
previousAggressor: state.previousAggressor,
|
|
326
|
+
streetsBet: state.streetsBet
|
|
327
|
+
}, line);
|
|
296
328
|
}
|
|
297
329
|
edgeChild[base + i] = child;
|
|
298
330
|
});
|
|
@@ -307,8 +339,16 @@ function buildTree(config, runouts) {
|
|
|
307
339
|
acted: 0,
|
|
308
340
|
base: 0,
|
|
309
341
|
aggressor: -1,
|
|
310
|
-
previousAggressor: -1
|
|
311
|
-
|
|
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
|
+
}
|
|
312
352
|
const decisionNodes = [];
|
|
313
353
|
for (let i = 0; i < kind.length; i++) if (kind[i] === NodeKind.Decision) decisionNodes.push(i);
|
|
314
354
|
return {
|
|
@@ -447,8 +487,6 @@ function enumerateRunouts(board, symmetries, merge) {
|
|
|
447
487
|
stabiliserMap.push(Int32Array.from(flat));
|
|
448
488
|
};
|
|
449
489
|
recordStabilisers(stabilisers);
|
|
450
|
-
const used = new Uint8Array(DECK);
|
|
451
|
-
const seen = new Uint8Array(DECK);
|
|
452
490
|
for (let round = 1; round < rounds; round++) {
|
|
453
491
|
const previousWidth = width[round - 1];
|
|
454
492
|
const previousCount = counts[round - 1];
|
|
@@ -463,49 +501,9 @@ function enumerateRunouts(board, symmetries, merge) {
|
|
|
463
501
|
const nextStabilisers = [];
|
|
464
502
|
for (let p = 0; p < previousCount; p++) {
|
|
465
503
|
starts[p] = parents.length;
|
|
466
|
-
used.fill(0);
|
|
467
|
-
seen.fill(0);
|
|
468
504
|
const base = p * previousWidth;
|
|
469
|
-
|
|
470
|
-
const
|
|
471
|
-
const children = [];
|
|
472
|
-
for (let card = 0; card < DECK; card++) {
|
|
473
|
-
if (used[card] || seen[card]) continue;
|
|
474
|
-
const child = { card, maps: [], cards: [], fixing: [], merged: false };
|
|
475
|
-
for (const sigma of stabiliser) {
|
|
476
|
-
const image = maps.card[sigma][card];
|
|
477
|
-
if (image === card) child.fixing.push(sigma);
|
|
478
|
-
if (seen[image]) continue;
|
|
479
|
-
seen[image] = 1;
|
|
480
|
-
child.maps.push(image === card ? 0 : maps.inverse(sigma));
|
|
481
|
-
child.cards.push(image);
|
|
482
|
-
}
|
|
483
|
-
children.push(child);
|
|
484
|
-
}
|
|
485
|
-
if (merge) {
|
|
486
|
-
const boardRanks = /* @__PURE__ */ new Set();
|
|
487
|
-
for (let i = 0; i < previousWidth; i++) boardRanks.add(previousCards[base + i] >> 2);
|
|
488
|
-
for (let j = 0; j < children.length; j++) {
|
|
489
|
-
const later = children[j];
|
|
490
|
-
const rankJ = later.card >> 2;
|
|
491
|
-
if (boardRanks.has(rankJ)) continue;
|
|
492
|
-
for (let i = 0; i < j; i++) {
|
|
493
|
-
const earlier = children[i];
|
|
494
|
-
if (earlier.merged || (earlier.card & 3) !== (later.card & 3)) continue;
|
|
495
|
-
const rankI = earlier.card >> 2;
|
|
496
|
-
if (boardRanks.has(rankI) || rankJ - rankI > merge.maxGap || !merge.ranks(rankI, rankJ)) continue;
|
|
497
|
-
if (connectivity(boardRanks, rankI) !== connectivity(boardRanks, rankJ)) continue;
|
|
498
|
-
const swap = maps.add(rankSwap(rankI, rankJ));
|
|
499
|
-
for (let m = 0; m < later.maps.length; m++) {
|
|
500
|
-
earlier.maps.push(maps.then(later.maps[m], swap));
|
|
501
|
-
earlier.cards.push(later.cards[m]);
|
|
502
|
-
}
|
|
503
|
-
later.merged = true;
|
|
504
|
-
approximate = true;
|
|
505
|
-
break;
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
}
|
|
505
|
+
const children = dealChildren(maps, previousCards, base, previousWidth, stabilisers[p], merge);
|
|
506
|
+
for (const child of children) if (child.merged) approximate = true;
|
|
509
507
|
for (const child of children) {
|
|
510
508
|
if (child.merged) continue;
|
|
511
509
|
for (let i = 0; i < previousWidth; i++) level.push(previousCards[base + i]);
|
|
@@ -548,6 +546,53 @@ function enumerateRunouts(board, symmetries, merge) {
|
|
|
548
546
|
initial: [...board]
|
|
549
547
|
};
|
|
550
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
|
+
}
|
|
551
596
|
function rankSwap(a, b) {
|
|
552
597
|
const out = new Uint8Array(DECK);
|
|
553
598
|
for (let card = 0; card < DECK; card++) {
|
|
@@ -590,11 +635,13 @@ function resolveBoard(runouts, board) {
|
|
|
590
635
|
const round = i - start + 1;
|
|
591
636
|
const dealt = runouts.maps.card[map][board[i]];
|
|
592
637
|
let found = -1;
|
|
593
|
-
|
|
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++) {
|
|
594
641
|
for (let m = runouts.orbitStart[round][child]; m < runouts.orbitStart[round][child + 1]; m++) {
|
|
595
642
|
if (runouts.orbitCard[round][m] !== dealt) continue;
|
|
596
643
|
found = child;
|
|
597
|
-
map = runouts.maps.then(map,
|
|
644
|
+
map = runouts.maps.then(map, relabel[m]);
|
|
598
645
|
break;
|
|
599
646
|
}
|
|
600
647
|
}
|
|
@@ -977,6 +1024,13 @@ function buildShowdownTable(runouts, relevant) {
|
|
|
977
1024
|
}
|
|
978
1025
|
|
|
979
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
|
+
}
|
|
980
1034
|
function span(lists, round, board, player) {
|
|
981
1035
|
const slot = (lists.roundBase[round] + board) * 2 + player;
|
|
982
1036
|
return [lists.start[slot], lists.count[slot]];
|
|
@@ -993,7 +1047,7 @@ function buildHandSpace(runouts, players, lists) {
|
|
|
993
1047
|
const parent = runouts.parent[round][child];
|
|
994
1048
|
for (let player = 0; player < 2; player++) {
|
|
995
1049
|
const [parentFrom, parentCount] = span(lists, round - 1, parent, player);
|
|
996
|
-
|
|
1050
|
+
markMembers(lists, parentFrom, parentCount, indexOf, false);
|
|
997
1051
|
const [childFrom, childCount] = span(lists, round, child, player);
|
|
998
1052
|
for (let m = orbitStart[child]; m < orbitStart[child + 1]; m++) {
|
|
999
1053
|
const back = runouts.maps.combo[runouts.maps.inverse(runouts.orbitMap[round][m])];
|
|
@@ -1005,7 +1059,7 @@ function buildHandSpace(runouts, players, lists) {
|
|
|
1005
1059
|
up.push(at);
|
|
1006
1060
|
}
|
|
1007
1061
|
}
|
|
1008
|
-
|
|
1062
|
+
markMembers(lists, parentFrom, parentCount, indexOf, true);
|
|
1009
1063
|
}
|
|
1010
1064
|
}
|
|
1011
1065
|
upStart.push(starts);
|
|
@@ -1018,11 +1072,11 @@ function buildHandSpace(runouts, players, lists) {
|
|
|
1018
1072
|
for (let player = 0; player < 2; player++) {
|
|
1019
1073
|
const slot = (lists.roundBase[round] + board) * 2 + player;
|
|
1020
1074
|
const [otherFrom, otherCount] = span(lists, round, board, 1 - player);
|
|
1021
|
-
|
|
1075
|
+
markMembers(lists, otherFrom, otherCount, indexOf, false);
|
|
1022
1076
|
const [from, count] = span(lists, round, board, player);
|
|
1023
1077
|
sameStart[slot] = same.length;
|
|
1024
1078
|
for (let i = 0; i < count; i++) same.push(indexOf[lists.list[from + i]]);
|
|
1025
|
-
|
|
1079
|
+
markMembers(lists, otherFrom, otherCount, indexOf, true);
|
|
1026
1080
|
}
|
|
1027
1081
|
}
|
|
1028
1082
|
}
|
|
@@ -1074,6 +1128,48 @@ function showdownValuesHands(oppReach, cardA, cardB, strength, oppFrom, oppCount
|
|
|
1074
1128
|
}
|
|
1075
1129
|
sums.fill(0, 0, DECK);
|
|
1076
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
|
+
}
|
|
1077
1173
|
|
|
1078
1174
|
// src/pairs.ts
|
|
1079
1175
|
var CHUNK = 64;
|
|
@@ -1105,7 +1201,8 @@ function makeBuffers(ctx, width) {
|
|
|
1105
1201
|
hands: make(() => new Uint16Array(width)),
|
|
1106
1202
|
strategy: make(() => new Float64Array(actions * COMBOS)),
|
|
1107
1203
|
rank: new Int32Array(COMBOS),
|
|
1108
|
-
position: new Int32Array(COMBOS)
|
|
1204
|
+
position: new Int32Array(COMBOS).fill(-1),
|
|
1205
|
+
at: make(() => new Int32Array(width))
|
|
1109
1206
|
};
|
|
1110
1207
|
}
|
|
1111
1208
|
function pairValues(ctx, node, board, traverser, hands, handReach, live, liveCount) {
|
|
@@ -1202,20 +1299,23 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1202
1299
|
const childLive = buffers.live[depth];
|
|
1203
1300
|
const childCols2 = buffers.cols[depth + 1];
|
|
1204
1301
|
const mappedHands = buffers.hands[depth];
|
|
1205
|
-
|
|
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];
|
|
1206
1305
|
for (let m = orbitStart[childBoard]; m < orbitStart[childBoard + 1]; m++) {
|
|
1207
|
-
const dealt = m === orbitStart[childBoard] ?
|
|
1306
|
+
const dealt = m === orbitStart[childBoard] ? stored : runouts.orbitCard[nextRound][m];
|
|
1208
1307
|
const map = runouts.maps.combo[runouts.orbitMap[nextRound][m]];
|
|
1308
|
+
const blocked = (combo) => cards[combo * 2] === dealt || cards[combo * 2 + 1] === dealt;
|
|
1209
1309
|
let childCount = 0;
|
|
1210
1310
|
for (let i = 0; i < liveCount; i++) {
|
|
1211
1311
|
const combo = live[i];
|
|
1212
|
-
if (
|
|
1312
|
+
if (blocked(combo)) continue;
|
|
1213
1313
|
childLive[childCount++] = map[combo];
|
|
1214
1314
|
}
|
|
1215
1315
|
let childColCount = 0;
|
|
1216
1316
|
for (let c = 0; c < colCount; c++) {
|
|
1217
1317
|
const k = cols[c];
|
|
1218
|
-
if (
|
|
1318
|
+
if (!blocked(hands[k])) childCols2[childColCount++] = k;
|
|
1219
1319
|
}
|
|
1220
1320
|
if (childColCount === 0) continue;
|
|
1221
1321
|
for (let k = 0; k < hands.length; k++) mappedHands[k] = map[hands[k]];
|
|
@@ -1237,7 +1337,7 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1237
1337
|
const row = childCols2[c] * COMBOS;
|
|
1238
1338
|
for (let i = 0; i < liveCount; i++) {
|
|
1239
1339
|
const combo = live[i];
|
|
1240
|
-
if (
|
|
1340
|
+
if (blocked(combo)) continue;
|
|
1241
1341
|
out[row + combo] += value[row + map[combo]];
|
|
1242
1342
|
}
|
|
1243
1343
|
}
|
|
@@ -1250,16 +1350,14 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1250
1350
|
const owner = tree.player[node];
|
|
1251
1351
|
const lists = ctx.active;
|
|
1252
1352
|
const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
|
|
1253
|
-
const active = lists.list;
|
|
1254
1353
|
const activeFrom = lists.start[listSlot];
|
|
1255
1354
|
const activeCount = lists.count[listSlot];
|
|
1256
1355
|
const stride = activeCount;
|
|
1257
1356
|
const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1258
1357
|
const strategy = buffers.strategy[depth];
|
|
1259
|
-
|
|
1358
|
+
storedStrategy(ctx, node, slotBase, actions, stride, strategy);
|
|
1260
1359
|
if (owner === traverser) {
|
|
1261
1360
|
for (let a = 0; a < actions; a++) {
|
|
1262
|
-
const offset = a * COMBOS;
|
|
1263
1361
|
const value = walk(
|
|
1264
1362
|
ctx,
|
|
1265
1363
|
buffers,
|
|
@@ -1277,8 +1375,11 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1277
1375
|
for (let c = 0; c < colCount; c++) {
|
|
1278
1376
|
const row = cols[c] * COMBOS;
|
|
1279
1377
|
for (let i = 0; i < activeCount; i++) {
|
|
1280
|
-
const
|
|
1281
|
-
|
|
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
|
+
}
|
|
1282
1383
|
}
|
|
1283
1384
|
}
|
|
1284
1385
|
}
|
|
@@ -1287,13 +1388,15 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
|
|
|
1287
1388
|
const childReach = buffers.colReach[depth + 1];
|
|
1288
1389
|
const childCols = buffers.cols[depth + 1];
|
|
1289
1390
|
const position = buffers.position;
|
|
1290
|
-
|
|
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);
|
|
1291
1395
|
for (let a = 0; a < actions; a++) {
|
|
1292
|
-
const offset = a * COMBOS;
|
|
1293
1396
|
let childCount = 0;
|
|
1294
1397
|
for (let c = 0; c < colCount; c++) {
|
|
1295
1398
|
const k = cols[c];
|
|
1296
|
-
const r = colReach[k] * strategy[a * stride +
|
|
1399
|
+
const r = at[k] < 0 ? 0 : colReach[k] * strategy[a * stride + at[k]];
|
|
1297
1400
|
childReach[k] = r;
|
|
1298
1401
|
if (r !== 0) childCols[childCount++] = k;
|
|
1299
1402
|
}
|
|
@@ -1385,58 +1488,243 @@ function buildPlayerIndex(weights) {
|
|
|
1385
1488
|
}
|
|
1386
1489
|
return { combos: Uint16Array.from(combos), count: combos.length, slot, weights };
|
|
1387
1490
|
}
|
|
1388
|
-
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) {
|
|
1389
1501
|
const roundBase = new Int32Array(runouts.rounds + 1);
|
|
1390
1502
|
for (let round = 0; round < runouts.rounds; round++) roundBase[round + 1] = roundBase[round] + runouts.counts[round];
|
|
1391
1503
|
const rowStart = new Int32Array(roundBase[runouts.rounds] * 2);
|
|
1392
1504
|
const totals = new Int32Array(runouts.rounds * 2);
|
|
1393
1505
|
const onBoard = new Uint8Array(DECK);
|
|
1394
1506
|
for (let round = 0; round < runouts.rounds; round++) {
|
|
1395
|
-
const
|
|
1507
|
+
const width2 = runouts.width[round];
|
|
1396
1508
|
const cards = runouts.cards[round];
|
|
1397
1509
|
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1398
1510
|
onBoard.fill(0);
|
|
1399
|
-
for (let i = 0; i <
|
|
1511
|
+
for (let i = 0; i < width2; i++) onBoard[cards[board * width2 + i]] = 1;
|
|
1400
1512
|
for (let player = 0; player < 2; player++) {
|
|
1401
1513
|
const index = players[player];
|
|
1402
1514
|
let live = 0;
|
|
1403
1515
|
for (let i = 0; i < index.count; i++) {
|
|
1404
1516
|
const combo = index.combos[i];
|
|
1405
|
-
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++;
|
|
1406
1520
|
}
|
|
1407
1521
|
rowStart[(roundBase[round] + board) * 2 + player] = totals[round * 2 + player];
|
|
1408
1522
|
totals[round * 2 + player] += live;
|
|
1409
1523
|
}
|
|
1410
1524
|
}
|
|
1411
1525
|
}
|
|
1412
|
-
const offset = new
|
|
1526
|
+
const offset = new Float64Array(tree.nodeCount).fill(-1);
|
|
1413
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;
|
|
1414
1535
|
let total = 0;
|
|
1536
|
+
let compactSlots = 0;
|
|
1537
|
+
let compactHands = 0;
|
|
1415
1538
|
let blocks = 0;
|
|
1416
1539
|
let maxActions = 0;
|
|
1540
|
+
let shared = false;
|
|
1541
|
+
let walked = 0;
|
|
1417
1542
|
for (let i = 0; i < tree.decisionNodes.length; i++) {
|
|
1418
1543
|
const node = tree.decisionNodes[i];
|
|
1419
1544
|
const actions = tree.numEdges[node];
|
|
1420
1545
|
if (actions > maxActions) maxActions = actions;
|
|
1421
|
-
|
|
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
|
+
}
|
|
1422
1560
|
blockOffset[node] = blocks;
|
|
1423
|
-
total += actions * totals[tree.round[node] * 2 + tree.player[node]];
|
|
1424
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
|
+
}
|
|
1425
1587
|
}
|
|
1426
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);
|
|
1427
1590
|
return {
|
|
1428
|
-
offset,
|
|
1591
|
+
offset: Math.max(total, ...chunkSlots) <= INT32_MAX ? Int32Array.from(offset) : offset,
|
|
1429
1592
|
blockOffset,
|
|
1430
1593
|
blocks,
|
|
1431
1594
|
totalSlots: total,
|
|
1595
|
+
walkedSlots: walked,
|
|
1432
1596
|
maxActions,
|
|
1433
1597
|
rowStart,
|
|
1434
1598
|
roundBase,
|
|
1435
|
-
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
|
|
1436
1610
|
};
|
|
1437
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
|
+
}
|
|
1438
1619
|
var REGRET_STEPS = 32767;
|
|
1439
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
|
+
}
|
|
1440
1728
|
function runoutWeights(runouts) {
|
|
1441
1729
|
const weights = new Float64Array(runouts.rounds).fill(1);
|
|
1442
1730
|
for (let round = 0; round < runouts.rounds; round++) {
|
|
@@ -1475,15 +1763,22 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1475
1763
|
const slots = roundBase[runouts.rounds] * 2;
|
|
1476
1764
|
const start = new Int32Array(slots);
|
|
1477
1765
|
const count = new Uint16Array(slots);
|
|
1766
|
+
const packedSlot = new Uint8Array(slots);
|
|
1478
1767
|
const entries = [];
|
|
1479
1768
|
const entrySlots = [];
|
|
1769
|
+
const memberStart = [];
|
|
1770
|
+
const members = [];
|
|
1480
1771
|
const onBoard = new Uint8Array(DECK);
|
|
1772
|
+
const seen = new Uint8Array(COMBOS);
|
|
1481
1773
|
for (let round = 0; round < runouts.rounds; round++) {
|
|
1482
1774
|
const width = runouts.width[round];
|
|
1483
1775
|
const cards = runouts.cards[round];
|
|
1776
|
+
const stabStart = runouts.stabiliserStart[round];
|
|
1777
|
+
const stabMap = runouts.stabiliserMap[round];
|
|
1484
1778
|
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1485
1779
|
onBoard.fill(0);
|
|
1486
1780
|
for (let i = 0; i < width; i++) onBoard[cards[board * width + i]] = 1;
|
|
1781
|
+
const symmetric = stabStart[board + 1] - stabStart[board] > 1;
|
|
1487
1782
|
for (let player = 0; player < 2; player++) {
|
|
1488
1783
|
const index = players[player];
|
|
1489
1784
|
const slot = (roundBase[round] + board) * 2 + player;
|
|
@@ -1492,6 +1787,7 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1492
1787
|
for (let i = 0; i < index.count; i++) {
|
|
1493
1788
|
const combo = index.combos[i];
|
|
1494
1789
|
if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
|
|
1790
|
+
if (symmetric && !orbitLeader(runouts, round, board, combo)) continue;
|
|
1495
1791
|
here.push(i);
|
|
1496
1792
|
}
|
|
1497
1793
|
if (showdown !== null && round === runouts.rounds - 1) {
|
|
@@ -1499,47 +1795,42 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1499
1795
|
here.sort((x, y) => group[index.combos[x]] - group[index.combos[y]] || x - y);
|
|
1500
1796
|
}
|
|
1501
1797
|
for (const i of here) {
|
|
1502
|
-
|
|
1798
|
+
const combo = index.combos[i];
|
|
1799
|
+
entries.push(combo);
|
|
1503
1800
|
entrySlots.push(i);
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
for (let round = 0; round < runouts.rounds; round++) {
|
|
1512
|
-
const stabStart = runouts.stabiliserStart[round];
|
|
1513
|
-
const stabMap = runouts.stabiliserMap[round];
|
|
1514
|
-
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1515
|
-
for (let player = 0; player < 2; player++) {
|
|
1516
|
-
const slot = (roundBase[round] + board) * 2 + player;
|
|
1517
|
-
tieStart[slot] = ties.length;
|
|
1518
|
-
if (stabStart[board + 1] - stabStart[board] < 2) continue;
|
|
1519
|
-
const position = /* @__PURE__ */ new Map();
|
|
1520
|
-
for (let e = start[slot]; e < start[slot] + count[slot]; e++) position.set(entries[e], e - start[slot]);
|
|
1521
|
-
const seen = new Uint8Array(count[slot]);
|
|
1522
|
-
for (let e = start[slot]; e < start[slot] + count[slot]; e++) {
|
|
1523
|
-
if (seen[e - start[slot]]) continue;
|
|
1524
|
-
const members = [];
|
|
1525
|
-
for (let k = stabStart[board]; k < stabStart[board + 1]; k++) {
|
|
1526
|
-
const image = position.get(runouts.maps.combo[stabMap[k]][entries[e]]);
|
|
1527
|
-
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;
|
|
1528
1808
|
seen[image] = 1;
|
|
1529
1809
|
members.push(image);
|
|
1810
|
+
packedSlot[slot] = 1;
|
|
1530
1811
|
}
|
|
1531
|
-
|
|
1812
|
+
for (let m = memberStart[memberStart.length - 1]; m < members.length; m++) seen[members[m]] = 0;
|
|
1532
1813
|
}
|
|
1814
|
+
count[slot] = entries.length - start[slot];
|
|
1533
1815
|
}
|
|
1534
1816
|
}
|
|
1535
1817
|
}
|
|
1536
|
-
|
|
1818
|
+
memberStart.push(members.length);
|
|
1537
1819
|
const list = Uint16Array.from(entries);
|
|
1538
1820
|
const cardA = new Uint8Array(list.length);
|
|
1539
1821
|
const cardB = new Uint8Array(list.length);
|
|
1822
|
+
const multiplicity = new Float64Array(list.length);
|
|
1540
1823
|
for (let e = 0; e < list.length; e++) {
|
|
1541
1824
|
cardA[e] = COMBO_CARDS[list[e] * 2];
|
|
1542
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];
|
|
1543
1834
|
}
|
|
1544
1835
|
const strength = new Int32Array(list.length);
|
|
1545
1836
|
if (showdown !== null) {
|
|
@@ -1558,11 +1849,15 @@ function buildActiveLists(runouts, players, showdown = null) {
|
|
|
1558
1849
|
list,
|
|
1559
1850
|
slot: Uint16Array.from(entrySlots),
|
|
1560
1851
|
roundBase,
|
|
1561
|
-
tieStart,
|
|
1562
|
-
ties: Int32Array.from(ties),
|
|
1563
1852
|
cardA,
|
|
1564
1853
|
cardB,
|
|
1565
|
-
strength
|
|
1854
|
+
strength,
|
|
1855
|
+
packed: packedSlot,
|
|
1856
|
+
multiplicity,
|
|
1857
|
+
memberStart: Int32Array.from(memberStart),
|
|
1858
|
+
memberCombo,
|
|
1859
|
+
memberA,
|
|
1860
|
+
memberB
|
|
1566
1861
|
};
|
|
1567
1862
|
}
|
|
1568
1863
|
var strengthScratch = new Int32Array(COMBOS);
|
|
@@ -1576,35 +1871,6 @@ function strengthOn(table, board) {
|
|
|
1576
1871
|
}
|
|
1577
1872
|
return strengthScratch;
|
|
1578
1873
|
}
|
|
1579
|
-
function tieMirrors(store, slotBase, actions, stride, ties, from, to, integer) {
|
|
1580
|
-
for (let a = 0; a < actions; a++) {
|
|
1581
|
-
const row = slotBase + a * stride;
|
|
1582
|
-
for (let k = from; k < to; ) {
|
|
1583
|
-
const size = ties[k];
|
|
1584
|
-
let sum = 0;
|
|
1585
|
-
for (let j = 1; j <= size; j++) sum += store[row + ties[k + j]];
|
|
1586
|
-
let mean = sum / size;
|
|
1587
|
-
if (integer) mean = Math.round(mean);
|
|
1588
|
-
for (let j = 1; j <= size; j++) store[row + ties[k + j]] = mean;
|
|
1589
|
-
k += size + 1;
|
|
1590
|
-
}
|
|
1591
|
-
}
|
|
1592
|
-
}
|
|
1593
|
-
function tieBlock(ctx, node, board) {
|
|
1594
|
-
const { tree } = ctx;
|
|
1595
|
-
const owner = tree.player[node];
|
|
1596
|
-
const lists = ctx.active;
|
|
1597
|
-
const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
|
|
1598
|
-
const from = lists.tieStart[listSlot];
|
|
1599
|
-
const to = lists.tieStart[listSlot + 1];
|
|
1600
|
-
if (from === to) return;
|
|
1601
|
-
const actions = tree.numEdges[node];
|
|
1602
|
-
const count = lists.count[listSlot];
|
|
1603
|
-
const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1604
|
-
const integer = ctx.regretScale !== null;
|
|
1605
|
-
if (ctx.regret.length > 0) tieMirrors(ctx.regret, base, actions, count, lists.ties, from, to, integer);
|
|
1606
|
-
tieMirrors(ctx.strategySum, base, actions, count, lists.ties, from, to, integer);
|
|
1607
|
-
}
|
|
1608
1874
|
function makeBuffers2(tree, maxActions, simd) {
|
|
1609
1875
|
const depth = treeDepth2(tree) + 2;
|
|
1610
1876
|
const make = (factory) => Array.from({ length: depth }, factory);
|
|
@@ -1650,7 +1916,7 @@ function dcfrFactors(t) {
|
|
|
1650
1916
|
keepSum: ((t - 1) / t) ** DCFR_GAMMA
|
|
1651
1917
|
};
|
|
1652
1918
|
}
|
|
1653
|
-
var Mode = { Train: 0, BestResponse: 1, Average: 2 };
|
|
1919
|
+
var Mode = { Train: 0, BestResponse: 1, Average: 2, Current: 3 };
|
|
1654
1920
|
function normalise(source, base, actions, count, strategy) {
|
|
1655
1921
|
const uniform = 1 / actions;
|
|
1656
1922
|
for (let i = 0; i < count; i++) {
|
|
@@ -1669,6 +1935,20 @@ function normalise(source, base, actions, count, strategy) {
|
|
|
1669
1935
|
}
|
|
1670
1936
|
}
|
|
1671
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
|
+
}
|
|
1672
1952
|
function regretMatch(regret, base, actions, count, strategy) {
|
|
1673
1953
|
normalise(regret, base, actions, count, strategy);
|
|
1674
1954
|
}
|
|
@@ -1719,6 +1999,26 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1719
1999
|
const signed = traverser === 0 ? tree.payoff[node] : -tree.payoff[node];
|
|
1720
2000
|
const weight = ctx.runoutWeight[round] / ctx.chipScale;
|
|
1721
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
|
+
}
|
|
1722
2022
|
foldValuesHands(
|
|
1723
2023
|
reachOpp,
|
|
1724
2024
|
lists.cardA,
|
|
@@ -1737,6 +2037,25 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1737
2037
|
}
|
|
1738
2038
|
if (kind === NodeKind.TerminalShowdown) {
|
|
1739
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
|
+
}
|
|
1740
2059
|
showdownValuesHands(
|
|
1741
2060
|
reachOpp,
|
|
1742
2061
|
lists.cardA,
|
|
@@ -1776,10 +2095,13 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1776
2095
|
const childMe = buffers.reachFor[traverser][depth];
|
|
1777
2096
|
const childOpp = buffers.reachFor[opponent][depth];
|
|
1778
2097
|
const sharded = nextRound === ctx.shardRound;
|
|
1779
|
-
const
|
|
1780
|
-
const
|
|
2098
|
+
const starts = runouts.childStart[nextRound];
|
|
2099
|
+
const first = starts[board];
|
|
2100
|
+
const last = starts[board + 1];
|
|
1781
2101
|
const from = sharded && ctx.shardStart > first ? ctx.shardStart : first;
|
|
1782
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;
|
|
1783
2105
|
for (let childBoard = from; childBoard < to; childBoard++) {
|
|
1784
2106
|
const childMySlot = (lists.roundBase[nextRound] + childBoard) * 2 + traverser;
|
|
1785
2107
|
const childOppSlot = (lists.roundBase[nextRound] + childBoard) * 2 + opponent;
|
|
@@ -1790,15 +2112,40 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1790
2112
|
let value = null;
|
|
1791
2113
|
for (let m = firstMember; m < membersEnd; m++) {
|
|
1792
2114
|
const upMe = upStart[m * 2 + traverser];
|
|
1793
|
-
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) {
|
|
1794
2133
|
const upOpp = upStart[m * 2 + opponent];
|
|
1795
2134
|
for (let k = 0; k < nMe; k++) childMe[k] = reachMe[up[upMe + k]];
|
|
1796
2135
|
for (let k = 0; k < nOpp; k++) childOpp[k] = reachOpp[up[upOpp + k]];
|
|
1797
2136
|
value = traverse(ctx, buffers, mode, traverser, iterWeight, child, childBoard, childMe, childOpp, depth + 1);
|
|
1798
2137
|
}
|
|
1799
|
-
|
|
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
|
+
}
|
|
1800
2144
|
}
|
|
1801
2145
|
}
|
|
2146
|
+
if (lists.packed[mySlot]) {
|
|
2147
|
+
for (let i = 0; i < myCount; i++) out[i] /= lists.multiplicity[myFrom + i];
|
|
2148
|
+
}
|
|
1802
2149
|
return out;
|
|
1803
2150
|
}
|
|
1804
2151
|
const actions = tree.numEdges[node];
|
|
@@ -1811,12 +2158,19 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1811
2158
|
const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1812
2159
|
const strategy = buffers.strategy[depth];
|
|
1813
2160
|
const length = actions * stride;
|
|
1814
|
-
|
|
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) {
|
|
1815
2167
|
const kernels = buffers.kernels;
|
|
1816
|
-
kernels.input.set(
|
|
2168
|
+
kernels.input.set(regrets.subarray(slotBase, slotBase + length));
|
|
1817
2169
|
kernels.regretMatch(strategy, actions, stride);
|
|
1818
|
-
} else if (
|
|
1819
|
-
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);
|
|
1820
2174
|
} else if (buffers.kernels !== null) {
|
|
1821
2175
|
const kernels = buffers.kernels;
|
|
1822
2176
|
kernels.input.set(ctx.strategySum.subarray(slotBase, slotBase + length));
|
|
@@ -1877,9 +2231,36 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1877
2231
|
for (let i = 0; i < myCount; i++) out[i] += value[i];
|
|
1878
2232
|
}
|
|
1879
2233
|
}
|
|
1880
|
-
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) {
|
|
1881
2261
|
updateQuantised(
|
|
1882
2262
|
ctx,
|
|
2263
|
+
node,
|
|
1883
2264
|
ctx.layout.blockOffset[node] + board,
|
|
1884
2265
|
slotBase,
|
|
1885
2266
|
actions,
|
|
@@ -1891,7 +2272,7 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1891
2272
|
out
|
|
1892
2273
|
);
|
|
1893
2274
|
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.dcfr) {
|
|
1894
|
-
const { keepPositive, keepNegative, keepSum } =
|
|
2275
|
+
const { keepPositive, keepNegative, keepSum } = blockFactors(ctx, ctx.layout.blockOffset[node] + board, iterWeight);
|
|
1895
2276
|
const regret = ctx.regret;
|
|
1896
2277
|
const strategySum = ctx.strategySum;
|
|
1897
2278
|
for (let a = 0; a < actions; a++) {
|
|
@@ -1902,7 +2283,7 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1902
2283
|
const at = slotRow + i;
|
|
1903
2284
|
const accumulated = regret[at];
|
|
1904
2285
|
regret[at] = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + i] - out[i];
|
|
1905
|
-
strategySum[at] = strategySum[at] * keepSum +
|
|
2286
|
+
strategySum[at] = strategySum[at] * keepSum + strategy[row + i];
|
|
1906
2287
|
}
|
|
1907
2288
|
}
|
|
1908
2289
|
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
@@ -1920,18 +2301,39 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
|
|
|
1920
2301
|
}
|
|
1921
2302
|
}
|
|
1922
2303
|
}
|
|
1923
|
-
if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
1924
|
-
const tieFrom = lists.tieStart[listSlot];
|
|
1925
|
-
const tieTo = lists.tieStart[listSlot + 1];
|
|
1926
|
-
if (tieFrom < tieTo) {
|
|
1927
|
-
const integer = ctx.regretScale !== null;
|
|
1928
|
-
tieMirrors(ctx.regret, slotBase, actions, stride, lists.ties, tieFrom, tieTo, integer);
|
|
1929
|
-
tieMirrors(ctx.strategySum, slotBase, actions, stride, lists.ties, tieFrom, tieTo, integer);
|
|
1930
|
-
}
|
|
1931
|
-
}
|
|
1932
2304
|
return out;
|
|
1933
2305
|
}
|
|
1934
|
-
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) {
|
|
1935
2337
|
const regret = ctx.regret;
|
|
1936
2338
|
const strategySum = ctx.strategySum;
|
|
1937
2339
|
const regretScale = ctx.regretScale;
|
|
@@ -1944,7 +2346,7 @@ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reach
|
|
|
1944
2346
|
let weight = iterWeight;
|
|
1945
2347
|
const clamp = !ctx.dcfr;
|
|
1946
2348
|
if (ctx.dcfr) {
|
|
1947
|
-
({ keepPositive, keepNegative, keepSum } =
|
|
2349
|
+
({ keepPositive, keepNegative, keepSum } = blockFactors(ctx, block, iterWeight));
|
|
1948
2350
|
weight = 1;
|
|
1949
2351
|
}
|
|
1950
2352
|
let largestRegret = 0;
|
|
@@ -1960,13 +2362,15 @@ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reach
|
|
|
1960
2362
|
const magnitude = updated < 0 ? -updated : updated;
|
|
1961
2363
|
if (magnitude > largestRegret) largestRegret = magnitude;
|
|
1962
2364
|
actionValues[offset + i] = updated;
|
|
1963
|
-
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;
|
|
1964
2367
|
if (sum > largestSum) largestSum = sum;
|
|
1965
2368
|
strategy[a * count + i] = sum;
|
|
1966
2369
|
}
|
|
1967
2370
|
}
|
|
1968
2371
|
const regretStep = largestRegret > 0 ? REGRET_STEPS / largestRegret : 0;
|
|
1969
2372
|
const sumStep = largestSum > 0 ? SUM_STEPS / largestSum : 0;
|
|
2373
|
+
const salt = ctx.layout.shared ? node : 0;
|
|
1970
2374
|
for (let a = 0; a < actions; a++) {
|
|
1971
2375
|
const offset = a * count;
|
|
1972
2376
|
const slotRow = slotBase + a * count;
|
|
@@ -1974,15 +2378,157 @@ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reach
|
|
|
1974
2378
|
const at = slotRow + i;
|
|
1975
2379
|
const r = actionValues[offset + i] * regretStep;
|
|
1976
2380
|
regret[at] = r < 0 ? r - 0.5 : r + 0.5;
|
|
1977
|
-
const s = strategy[a * count + i] * sumStep + dither(at, iterWeight);
|
|
2381
|
+
const s = strategy[a * count + i] * sumStep + dither(at, iterWeight, salt);
|
|
1978
2382
|
strategySum[at] = s < SUM_STEPS ? s : SUM_STEPS;
|
|
1979
2383
|
}
|
|
1980
2384
|
}
|
|
1981
2385
|
regretScale[block] = largestRegret / REGRET_STEPS;
|
|
1982
2386
|
sumScale[block] = largestSum / SUM_STEPS;
|
|
1983
2387
|
}
|
|
1984
|
-
|
|
1985
|
-
|
|
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);
|
|
1986
2532
|
h ^= h >>> 13;
|
|
1987
2533
|
h = Math.imul(h, 3266489909);
|
|
1988
2534
|
h ^= h >>> 16;
|
|
@@ -2024,7 +2570,7 @@ function makeSolver(ctx) {
|
|
|
2024
2570
|
const weights = ctx.players[traverser].weights;
|
|
2025
2571
|
const [from, count] = listOf(0, 0, traverser);
|
|
2026
2572
|
let sum = 0;
|
|
2027
|
-
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];
|
|
2028
2574
|
return sum / ctx.totalMass;
|
|
2029
2575
|
};
|
|
2030
2576
|
return {
|
|
@@ -2034,6 +2580,9 @@ function makeSolver(ctx) {
|
|
|
2034
2580
|
return rootCount;
|
|
2035
2581
|
},
|
|
2036
2582
|
traverseRoot: run,
|
|
2583
|
+
get root() {
|
|
2584
|
+
return root;
|
|
2585
|
+
},
|
|
2037
2586
|
useRoot(next) {
|
|
2038
2587
|
root = next ?? run;
|
|
2039
2588
|
},
|
|
@@ -2050,17 +2599,79 @@ function makeSolver(ctx) {
|
|
|
2050
2599
|
expectedValue() {
|
|
2051
2600
|
return [total(root(Mode.Average, 0, 0), 0), total(root(Mode.Average, 1, 0), 1)];
|
|
2052
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
|
+
},
|
|
2053
2639
|
exploitability() {
|
|
2054
2640
|
const br0 = total(root(Mode.BestResponse, 0, 0), 0);
|
|
2055
2641
|
const br1 = total(root(Mode.BestResponse, 1, 0), 1);
|
|
2056
2642
|
return (br0 + br1) / 2;
|
|
2057
2643
|
},
|
|
2058
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
|
+
}
|
|
2059
2672
|
const expanded = ctx.expandOrbits;
|
|
2060
2673
|
ctx.expandOrbits = true;
|
|
2061
2674
|
try {
|
|
2062
|
-
const round = ctx.tree.round[node];
|
|
2063
|
-
const opponent = 1 - traverser;
|
|
2064
2675
|
const me = new Float64Array(COMBOS);
|
|
2065
2676
|
const opp = new Float64Array(COMBOS);
|
|
2066
2677
|
gather(reachMe, round, board, traverser, me);
|
|
@@ -2068,9 +2679,11 @@ function makeSolver(ctx) {
|
|
|
2068
2679
|
const value = traverse(ctx, buffers, Mode.Average, traverser, 0, node, board, me, opp, 0);
|
|
2069
2680
|
const result = new Float64Array(COMBOS);
|
|
2070
2681
|
const [from, count] = listOf(round, board, traverser);
|
|
2071
|
-
for (let i = 0; i < count; i++)
|
|
2072
|
-
|
|
2073
|
-
|
|
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
|
+
}
|
|
2074
2687
|
return result;
|
|
2075
2688
|
} finally {
|
|
2076
2689
|
ctx.expandOrbits = expanded;
|
|
@@ -2088,11 +2701,13 @@ function makeSolver(ctx) {
|
|
|
2088
2701
|
const count = lists.count[slot];
|
|
2089
2702
|
const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[slot];
|
|
2090
2703
|
const strategy = new Float64Array(actions * count);
|
|
2091
|
-
|
|
2704
|
+
storedStrategy(ctx, node, base, actions, count, strategy);
|
|
2092
2705
|
const probs = new Float64Array(index.count * actions).fill(1 / actions);
|
|
2093
2706
|
for (let i = 0; i < count; i++) {
|
|
2094
|
-
|
|
2095
|
-
|
|
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
|
+
}
|
|
2096
2711
|
}
|
|
2097
2712
|
return { actions, probs };
|
|
2098
2713
|
}
|
|
@@ -2100,17 +2715,19 @@ function makeSolver(ctx) {
|
|
|
2100
2715
|
}
|
|
2101
2716
|
|
|
2102
2717
|
// src/wasm/engine.ts
|
|
2103
|
-
var ENGINE_WASM = "
|
|
2718
|
+
var ENGINE_WASM = "AGFzbQEAAAABcAtgBH9/f38AYAd/f39/f3x/AGADf39/AGACf38AYAF/AGAGf39/f39/AX9gCH9/f39/f39/AG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ELIy8gASATQa/W074CbHMiAUEPdiABc0G13MqVfGwiAUENdiABc0Gxz9myAWwiAUEQdiABc7hEAAAAAAAA8D2iZAtFCwRAIBAPCyMLIANBASAAa2pBAnRqKAIAIQEDQCAMIA9IBEAgCSAMQQJ0aiAHIAUgDEEBdGovAQBBAnRqKgIAOAIAIAxBAWohDAwBCwsDQCABIA1KBEAgCiANQQJ0aiAIIAYgDUEBdGovAQBBAnRqKgIAOAIAIA1BAWohDQwBCwsgACACIAQgCSAKIAtBAWoQACEAA0AgDiAPSARAIA5BAnQiASAAaiICKgIAIRQgASAQaiIBKgIAIRUgASAUOAIAIBJFBEAgAiAUIzFDAACAv5IgFCAVk5SSOAIACyAOQQFqIQ4MAQsLIBEjOjYCACAACysAIAAkQSAFJEIgBiRDIAckRCAIJEUgCUEARyRGIAEgAkEAIAMgBEEAEAALKwAgACRBIAQkQiAFJEMgBiREIAckRSAIQQBHJEYgAUEAQQAgAiADQQAQAAuOAgAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBJCEgACgCiAEkIiAAKAKMASQjIAAoApABuCQkCw==";
|
|
2104
2719
|
|
|
2105
2720
|
// src/wasm/engine-shared.ts
|
|
2106
|
-
var ENGINE_SHARED_WASM = "
|
|
2721
|
+
var ENGINE_SHARED_WASM = "AGFzbQEAAAABcAtgBH9/f38AYAd/f39/f3x/AGADf39/AGACf38AYAF/AGAGf39/f39/AX9gCH9/f39/f39/AGAMf3x/f39/f39/f39/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bHMiAUEPdiABc0G13MqVfGwiAUENdiABc0Gxz9myAWwiAUEQdiABc7hEAAAAAAAA8D2iZAtFCwRAIBAPCyMLIANBASAAa2pBAnRqKAIAIQEDQCAMIA9IBEAgCSAMQQJ0aiAHIAUgDEEBdGovAQBBAnRqKgIAOAIAIAxBAWohDAwBCwsDQCABIA1KBEAgCiANQQJ0aiAIIAYgDUEBdGovAQBBAnRqKgIAOAIAIA1BAWohDQwBCwsgACACIAQgCSAKIAtBAWoQACEAA0AgDiAPSARAIA5BAnQiASAAaiICKgIAIRQgASAQaiIBKgIAIRUgASAUOAIAIBJFBEAgAiAUIzFDAACAv5IgFCAVk5SSOAIACyAOQQFqIQ4MAQsLIBEjOjYCACAACysAIAAkQSAFJEIgBiRDIAckRCAIJEUgCUEARyRGIAEgAkEAIAMgBEEAEAALKwAgACRBIAQkQiAFJEMgBiREIAckRSAIQQBHJEYgAUEAQQAgAiADQQAQAAuOAgAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBJCEgACgCiAEkIiAAKAKMASQjIAAoApABuCQkCw==";
|
|
2107
2722
|
|
|
2108
2723
|
// src/platform/node.ts
|
|
2109
2724
|
import { availableParallelism } from "os";
|
|
2110
2725
|
import { fileURLToPath } from "url";
|
|
2111
2726
|
import { Worker } from "worker_threads";
|
|
2112
2727
|
var PLATFORM = "node";
|
|
2113
|
-
|
|
2728
|
+
function threadLimit(_engine) {
|
|
2729
|
+
return Infinity;
|
|
2730
|
+
}
|
|
2114
2731
|
var DEFAULT_MAX_BYTES = 6 * 1024 * 1024 * 1024;
|
|
2115
2732
|
function workerFlags() {
|
|
2116
2733
|
return process.execArgv.filter((flag) => !flag.startsWith("--input-type"));
|
|
@@ -2141,6 +2758,9 @@ function spawnEngineWorker(setup, onError) {
|
|
|
2141
2758
|
}
|
|
2142
2759
|
};
|
|
2143
2760
|
}
|
|
2761
|
+
function threadAdvice(_engine) {
|
|
2762
|
+
return "";
|
|
2763
|
+
}
|
|
2144
2764
|
|
|
2145
2765
|
// src/parallel.ts
|
|
2146
2766
|
var CONTROL = {
|
|
@@ -2151,8 +2771,31 @@ var CONTROL = {
|
|
|
2151
2771
|
iterWeight: 4,
|
|
2152
2772
|
ready: 5,
|
|
2153
2773
|
mode: 6,
|
|
2774
|
+
/** Set, never cleared, once any thread's part of a step threw; see src/worker.ts. */
|
|
2775
|
+
failed: 7,
|
|
2154
2776
|
size: 8
|
|
2155
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
|
+
}
|
|
2156
2799
|
var COMMAND = { work: 0, exit: 1 };
|
|
2157
2800
|
var WORKER_START_MS = 3e4;
|
|
2158
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.";
|
|
@@ -2181,7 +2824,7 @@ function planShards(tree, runouts, workers) {
|
|
|
2181
2824
|
for (let w = 0; w <= workers; w++) bounds[w] = Math.round(boards * w / workers);
|
|
2182
2825
|
return { round, boards, entryNodes: Int32Array.from(entryNodes), entryIndexOf, bounds };
|
|
2183
2826
|
}
|
|
2184
|
-
function startPool(ctx, solver, plan, payload, workers) {
|
|
2827
|
+
function startPool(ctx, solver, plan, payload, workers, spawn = spawnWorker, backstopFloorMs = THREAD_BACKSTOP_FLOOR_MS) {
|
|
2185
2828
|
const entryCount = plan.entryNodes.length;
|
|
2186
2829
|
const spawned = workers - 1;
|
|
2187
2830
|
const reachFloats = entryCount * 2 * COMBOS;
|
|
@@ -2192,6 +2835,12 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2192
2835
|
const control = new Int32Array(controlBuffer);
|
|
2193
2836
|
const threads = [];
|
|
2194
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;
|
|
2195
2844
|
for (let w = 0; w < spawned; w++) {
|
|
2196
2845
|
const setup = {
|
|
2197
2846
|
workerId: w,
|
|
@@ -2203,11 +2852,17 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2203
2852
|
strategySum: ctx.strategySum.buffer,
|
|
2204
2853
|
regretScale: ctx.regretScale?.buffer ?? null,
|
|
2205
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,
|
|
2206
2861
|
control: controlBuffer,
|
|
2207
2862
|
exchange: exchangeBuffer,
|
|
2208
2863
|
entryCount
|
|
2209
2864
|
};
|
|
2210
|
-
const thread =
|
|
2865
|
+
const thread = spawn(setup, (error) => {
|
|
2211
2866
|
failure ??= error;
|
|
2212
2867
|
Atomics.store(control, CONTROL.ready, spawned);
|
|
2213
2868
|
Atomics.add(control, CONTROL.done, spawned);
|
|
@@ -2231,6 +2886,7 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2231
2886
|
const chanceValues = new Float64Array(entryCount * COMBOS);
|
|
2232
2887
|
const mainReachMe = new Float64Array(COMBOS);
|
|
2233
2888
|
const mainReachOpp = new Float64Array(COMBOS);
|
|
2889
|
+
const failed = () => failure ?? (Atomics.load(control, CONTROL.failed) ? new Error(THREAD_FAILED_ERROR) : null);
|
|
2234
2890
|
const dispatch = (mode, traverser, iterWeight) => {
|
|
2235
2891
|
Atomics.store(control, CONTROL.done, 0);
|
|
2236
2892
|
Atomics.store(control, CONTROL.mode, mode);
|
|
@@ -2239,6 +2895,7 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2239
2895
|
Atomics.store(control, CONTROL.command, COMMAND.work);
|
|
2240
2896
|
Atomics.add(control, CONTROL.generation, 1);
|
|
2241
2897
|
Atomics.notify(control, CONTROL.generation);
|
|
2898
|
+
const dispatched = performance.now();
|
|
2242
2899
|
const fullStart = ctx.shardStart;
|
|
2243
2900
|
const fullEnd = ctx.shardEnd;
|
|
2244
2901
|
ctx.shardStart = plan.bounds[0];
|
|
@@ -2264,43 +2921,49 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2264
2921
|
ctx.shardStart = fullStart;
|
|
2265
2922
|
ctx.shardEnd = fullEnd;
|
|
2266
2923
|
}
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
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;
|
|
2940
|
+
}
|
|
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;
|
|
2272
2953
|
}
|
|
2273
|
-
|
|
2954
|
+
return total;
|
|
2274
2955
|
};
|
|
2275
2956
|
return {
|
|
2276
2957
|
workers,
|
|
2277
2958
|
traverse(mode, traverser, iterWeight) {
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
ctx.chanceCollect = (node, reachMe, reachOpp) => {
|
|
2281
|
-
const entry = plan.entryIndexOf[node];
|
|
2282
|
-
exchange.set(reachMe.subarray(0, COMBOS), entry * 2 * COMBOS);
|
|
2283
|
-
exchange.set(reachOpp.subarray(0, COMBOS), entry * 2 * COMBOS + COMBOS);
|
|
2284
|
-
};
|
|
2285
|
-
try {
|
|
2286
|
-
solver.traverseRoot(mode, traverser, iterWeight);
|
|
2287
|
-
} finally {
|
|
2288
|
-
ctx.chanceCollect = null;
|
|
2289
|
-
}
|
|
2290
|
-
dispatch(mode, traverser, iterWeight);
|
|
2291
|
-
for (let w = 0; w < spawned; w++) {
|
|
2292
|
-
const base = reachFloats + w * entryCount * COMBOS;
|
|
2293
|
-
for (let i = 0; i < entryCount * COMBOS; i++) chanceValues[i] += exchange[base + i];
|
|
2294
|
-
}
|
|
2295
|
-
ctx.chanceOverride = chanceValues;
|
|
2296
|
-
ctx.applyUpdates = true;
|
|
2959
|
+
const dead = failed();
|
|
2960
|
+
if (dead) throw dead;
|
|
2297
2961
|
try {
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2962
|
+
return traverseAll(mode, traverser, iterWeight);
|
|
2963
|
+
} catch (error) {
|
|
2964
|
+
Atomics.store(control, CONTROL.failed, 1);
|
|
2965
|
+
throw error;
|
|
2302
2966
|
}
|
|
2303
|
-
return total;
|
|
2304
2967
|
},
|
|
2305
2968
|
close() {
|
|
2306
2969
|
Atomics.store(control, CONTROL.command, COMMAND.exit);
|
|
@@ -2315,25 +2978,40 @@ function startPool(ctx, solver, plan, payload, workers) {
|
|
|
2315
2978
|
var wasm2 = globalThis.WebAssembly;
|
|
2316
2979
|
var modules = { plain: void 0, shared: void 0 };
|
|
2317
2980
|
function compiledEngine(kind) {
|
|
2318
|
-
|
|
2319
|
-
|
|
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;
|
|
2320
2986
|
try {
|
|
2321
2987
|
if (!wasm2) return null;
|
|
2322
|
-
const text = kind === "plain" ? ENGINE_WASM : ENGINE_SHARED_WASM;
|
|
2323
2988
|
const bytes = Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
|
|
2324
2989
|
if (!wasm2.validate(bytes)) return null;
|
|
2325
|
-
|
|
2990
|
+
cache[kind] = new wasm2.Module(bytes);
|
|
2326
2991
|
} catch {
|
|
2327
|
-
|
|
2992
|
+
cache[kind] = null;
|
|
2328
2993
|
}
|
|
2329
|
-
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]);
|
|
2330
3001
|
}
|
|
2331
|
-
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 };
|
|
2332
3004
|
function createEngine(input, pool) {
|
|
2333
3005
|
const shared = pool !== void 0;
|
|
2334
|
-
const
|
|
3006
|
+
const storage = input.storage ?? null;
|
|
3007
|
+
const compiledModule = storage ? compiledStorage(storage.source, shared ? "shared" : "plain") : compiledEngine(shared ? "shared" : "plain");
|
|
2335
3008
|
if (!compiledModule || !wasm2) return null;
|
|
2336
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;
|
|
2337
3015
|
const threads = pool ? pool.threads : 1;
|
|
2338
3016
|
const flatten = (parts) => {
|
|
2339
3017
|
const bases = new Int32Array(parts.length + 1);
|
|
@@ -2372,8 +3050,11 @@ function createEngine(input, pool) {
|
|
|
2372
3050
|
add(active.cardA),
|
|
2373
3051
|
add(active.cardB),
|
|
2374
3052
|
add(active.strength),
|
|
2375
|
-
add(active.
|
|
2376
|
-
add(active.
|
|
3053
|
+
add(active.packed),
|
|
3054
|
+
add(Float32Array.from(active.multiplicity)),
|
|
3055
|
+
add(active.memberStart),
|
|
3056
|
+
add(active.memberA),
|
|
3057
|
+
add(active.memberB),
|
|
2377
3058
|
add(upRound),
|
|
2378
3059
|
add(upStart),
|
|
2379
3060
|
add(hands.up),
|
|
@@ -2384,13 +3065,13 @@ function createEngine(input, pool) {
|
|
|
2384
3065
|
add(orbitRound),
|
|
2385
3066
|
add(orbitStart),
|
|
2386
3067
|
add(input.runoutWeight),
|
|
2387
|
-
add(null, layout.totalSlots *
|
|
3068
|
+
add(null, layout.totalSlots * width),
|
|
2388
3069
|
// regret
|
|
2389
|
-
add(null, layout.totalSlots *
|
|
3070
|
+
add(null, layout.totalSlots * width)
|
|
2390
3071
|
// strategy sums
|
|
2391
3072
|
];
|
|
2392
|
-
const REGRET = common[
|
|
2393
|
-
const SUMS = common[
|
|
3073
|
+
const REGRET = common[common.length - 2];
|
|
3074
|
+
const SUMS = common[common.length - 1];
|
|
2394
3075
|
const own = Array.from({ length: threads }, () => ({
|
|
2395
3076
|
buffers: add(null, depths * depthBytes),
|
|
2396
3077
|
cardSums: add(null, 52 * 8 + stride * 8),
|
|
@@ -2403,13 +3084,43 @@ function createEngine(input, pool) {
|
|
|
2403
3084
|
const reach = pool ? add(null, entries * 2 * rowBytes) : -1;
|
|
2404
3085
|
const values = pool ? add(null, entries * rowBytes) : -1;
|
|
2405
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;
|
|
2406
3117
|
const offsets = [];
|
|
2407
|
-
let total = 64;
|
|
3118
|
+
let total = 64 + Math.ceil(Math.max(input.lowBytes ?? 0, 0) / 16) * 16;
|
|
2408
3119
|
for (const piece of pieces) {
|
|
2409
3120
|
offsets.push(total);
|
|
2410
3121
|
total += piece.bytes;
|
|
2411
3122
|
}
|
|
2412
|
-
if (total >
|
|
3123
|
+
if (total > MAX_MEMORY) return null;
|
|
2413
3124
|
const pages = Math.ceil(total / 65536) + 1;
|
|
2414
3125
|
const memory = shared ? new wasm2.Memory({ initial: pages, maximum: pages, shared: true }) : new wasm2.Memory({ initial: pages });
|
|
2415
3126
|
const bytes = new Uint8Array(memory.buffer);
|
|
@@ -2419,20 +3130,91 @@ function createEngine(input, pool) {
|
|
|
2419
3130
|
for (const mine of own) {
|
|
2420
3131
|
const head = new Uint32Array(memory.buffer, offsets[mine.header], 40);
|
|
2421
3132
|
common.forEach((slot, i) => head[i] = offsets[slot]);
|
|
2422
|
-
head[
|
|
2423
|
-
head[
|
|
2424
|
-
head[
|
|
2425
|
-
head[
|
|
2426
|
-
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];
|
|
2427
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
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
const factors = stored ? new Float64Array(memory.buffer, offsets[stored.block] + 80, 4) : null;
|
|
2428
3170
|
const instance = new wasm2.Instance(compiledModule, { env: { memory, abort: () => {
|
|
2429
3171
|
throw new Error("engine abort");
|
|
2430
3172
|
} } });
|
|
2431
3173
|
const exports = instance.exports;
|
|
2432
3174
|
exports.init(offsets[own[0].header]);
|
|
2433
3175
|
exports.setMaxActions(maxActions);
|
|
2434
|
-
|
|
2435
|
-
|
|
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;
|
|
2436
3218
|
const me = new Float32Array(memory.buffer, offsets[own[0].rootMe], stride);
|
|
2437
3219
|
const opp = new Float32Array(memory.buffer, offsets[own[0].rootOpp], stride);
|
|
2438
3220
|
const result = new Float64Array(COMBOS);
|
|
@@ -2452,7 +3234,7 @@ function createEngine(input, pool) {
|
|
|
2452
3234
|
const workers = [];
|
|
2453
3235
|
let failure = null;
|
|
2454
3236
|
if (pool) {
|
|
2455
|
-
const
|
|
3237
|
+
const plan2 = pool.plan;
|
|
2456
3238
|
const controlBuffer = new SharedArrayBuffer(ENGINE_CONTROL.size * 4);
|
|
2457
3239
|
const settingsBuffer = new SharedArrayBuffer(8 * 8);
|
|
2458
3240
|
control = new Int32Array(controlBuffer);
|
|
@@ -2465,14 +3247,17 @@ function createEngine(input, pool) {
|
|
|
2465
3247
|
header: offsets[own[w].header],
|
|
2466
3248
|
maxActions,
|
|
2467
3249
|
exchange: [offsets[entryIndexOf], offsets[reach], offsets[values], stride],
|
|
2468
|
-
shard: [
|
|
2469
|
-
entryNodes:
|
|
3250
|
+
shard: [plan2.round, plan2.bounds[w], plan2.bounds[w + 1]],
|
|
3251
|
+
entryNodes: plan2.entryNodes,
|
|
2470
3252
|
rootMe: offsets[own[w].rootMe],
|
|
2471
3253
|
rootOpp: offsets[own[w].rootOpp],
|
|
2472
3254
|
partial: offsets[partials] + (w - 1) * entries * rowBytes,
|
|
2473
3255
|
stride,
|
|
2474
3256
|
control: controlBuffer,
|
|
2475
|
-
settings: settingsBuffer
|
|
3257
|
+
settings: settingsBuffer,
|
|
3258
|
+
...share ? { shared: [offsets[share.marks], offsets[share.blockOffset], weightedAt] } : {},
|
|
3259
|
+
...stored ? { storage: true } : {},
|
|
3260
|
+
...samplingArgs ? { sampling: samplingArgs } : {}
|
|
2476
3261
|
};
|
|
2477
3262
|
workers.push(pool.spawn(setup, (error) => {
|
|
2478
3263
|
failure ??= error;
|
|
@@ -2494,7 +3279,14 @@ function createEngine(input, pool) {
|
|
|
2494
3279
|
throw failure;
|
|
2495
3280
|
}
|
|
2496
3281
|
}
|
|
2497
|
-
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;
|
|
2498
3290
|
for (const player of [0, 1]) {
|
|
2499
3291
|
const [from, count2] = rootList(player);
|
|
2500
3292
|
const into = player === traverser ? me : opp;
|
|
@@ -2502,53 +3294,81 @@ function createEngine(input, pool) {
|
|
|
2502
3294
|
for (let i = 0; i < count2; i++) into[i] = weights[active.list[from + i]];
|
|
2503
3295
|
}
|
|
2504
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]);
|
|
2505
3301
|
let at;
|
|
2506
3302
|
if (!pool) {
|
|
2507
|
-
|
|
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
|
+
}
|
|
2508
3309
|
} else {
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
Atomics.store(control, ENGINE_CONTROL.mode, mode);
|
|
2516
|
-
Atomics.store(control, ENGINE_CONTROL.traverser, traverser);
|
|
2517
|
-
Atomics.store(control, ENGINE_CONTROL.command, 0);
|
|
2518
|
-
Atomics.add(control, ENGINE_CONTROL.generation, 1);
|
|
2519
|
-
Atomics.notify(control, ENGINE_CONTROL.generation);
|
|
2520
|
-
exports.setShard(plan.round, plan.bounds[0], plan.bounds[1]);
|
|
2521
|
-
exports.setPhase(0, 1);
|
|
2522
|
-
const valueView = new Float32Array(memory.buffer, offsets[values], entries * stride);
|
|
2523
|
-
for (let entry = 0; entry < entries; entry++) {
|
|
2524
|
-
const slot = offsets[reach] + entry * 2 * rowBytes;
|
|
2525
|
-
const out = exports.runChance(mode, traverser, plan.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp);
|
|
2526
|
-
valueView.set(new Float32Array(memory.buffer, out, stride), entry * stride);
|
|
2527
|
-
}
|
|
2528
|
-
for (; ; ) {
|
|
2529
|
-
const done = Atomics.load(control, ENGINE_CONTROL.done);
|
|
2530
|
-
if (done >= threads - 1 || failure) break;
|
|
2531
|
-
Atomics.wait(control, ENGINE_CONTROL.done, done, 50);
|
|
2532
|
-
}
|
|
2533
|
-
if (failure) throw failure;
|
|
2534
|
-
const partialView = new Float32Array(memory.buffer, offsets[partials], (threads - 1) * entries * stride);
|
|
2535
|
-
for (let w = 0; w < threads - 1; w++) {
|
|
2536
|
-
for (let i = 0; i < entries * stride; i++) valueView[i] += partialView[w * entries * stride + i];
|
|
2537
|
-
}
|
|
2538
|
-
exports.setShard(plan.round, 0, 0);
|
|
2539
|
-
exports.setPhase(2, 1);
|
|
2540
|
-
at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
|
|
2541
|
-
exports.setPhase(0, 1);
|
|
2542
|
-
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
|
+
}
|
|
2543
3316
|
}
|
|
2544
3317
|
const [, count] = rootList(traverser);
|
|
2545
3318
|
const values32 = new Float32Array(memory.buffer, at, count);
|
|
2546
3319
|
for (let i = 0; i < count; i++) result[i] = values32[i];
|
|
2547
3320
|
return result;
|
|
2548
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
|
+
};
|
|
2549
3364
|
return {
|
|
2550
3365
|
regret,
|
|
2551
3366
|
strategySum,
|
|
3367
|
+
regretScale: stored ? scaleView(stored.regretScale) : null,
|
|
3368
|
+
sumScale: stored ? scaleView(stored.sumScale) : null,
|
|
3369
|
+
compact: compactViews,
|
|
3370
|
+
sampling: samplingViews,
|
|
3371
|
+
discounted,
|
|
2552
3372
|
threads,
|
|
2553
3373
|
root(dcfr) {
|
|
2554
3374
|
return (mode, traverser, iterWeight) => run(mode, traverser, iterWeight, dcfr);
|
|
@@ -2567,7 +3387,7 @@ function createEngine(input, pool) {
|
|
|
2567
3387
|
var SIZE_TOLERANCE = 1.35;
|
|
2568
3388
|
var REGRET_STEPS2 = 32767;
|
|
2569
3389
|
var SUM_STEPS2 = 65535;
|
|
2570
|
-
function matchNodes(to, from) {
|
|
3390
|
+
function matchNodes(to, from, tolerance = SIZE_TOLERANCE) {
|
|
2571
3391
|
const match = new Int32Array(to.nodeCount).fill(-1);
|
|
2572
3392
|
const walk2 = (a, b) => {
|
|
2573
3393
|
if (to.kind[a] !== from.kind[b] || to.round[a] !== from.round[b]) return;
|
|
@@ -2579,14 +3399,14 @@ function matchNodes(to, from) {
|
|
|
2579
3399
|
match[a] = b;
|
|
2580
3400
|
for (let e = 0; e < to.numEdges[a]; e++) {
|
|
2581
3401
|
const edge = to.firstEdge[a] + e;
|
|
2582
|
-
const other = matchEdge(to, edge, a, from, b);
|
|
3402
|
+
const other = matchEdge(to, edge, a, from, b, tolerance);
|
|
2583
3403
|
if (other >= 0) walk2(to.edgeChild[edge], from.edgeChild[other]);
|
|
2584
3404
|
}
|
|
2585
3405
|
};
|
|
2586
3406
|
walk2(0, 0);
|
|
2587
3407
|
return match;
|
|
2588
3408
|
}
|
|
2589
|
-
function matchEdge(to, edge, node, from, other) {
|
|
3409
|
+
function matchEdge(to, edge, node, from, other, tolerance) {
|
|
2590
3410
|
const kind = to.edgeAction[edge];
|
|
2591
3411
|
const sized = kind === ActionKind.Bet || kind === ActionKind.Raise;
|
|
2592
3412
|
const size = to.edgeTarget[edge] / to.pot[node];
|
|
@@ -2602,7 +3422,7 @@ function matchEdge(to, edge, node, from, other) {
|
|
|
2602
3422
|
best = candidate;
|
|
2603
3423
|
}
|
|
2604
3424
|
}
|
|
2605
|
-
return bestGap <= Math.log(
|
|
3425
|
+
return bestGap <= Math.log(tolerance) ? best : -1;
|
|
2606
3426
|
}
|
|
2607
3427
|
function readBlock(store, scale, block, base, length, out) {
|
|
2608
3428
|
const factor = scale ? scale[block] : 1;
|
|
@@ -2622,24 +3442,93 @@ function writeBlock(store, scale, steps, block, base, length, values) {
|
|
|
2622
3442
|
for (let i = 0; i < length; i++) store[base + i] = Math.round(values[i] * step);
|
|
2623
3443
|
scale[block] = largest / steps;
|
|
2624
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
|
+
}
|
|
2625
3503
|
function realCards(runouts, round, board) {
|
|
2626
3504
|
const width = runouts.width[round];
|
|
2627
3505
|
return Array.from(runouts.cards[round].subarray(board * width, (board + 1) * width));
|
|
2628
3506
|
}
|
|
2629
|
-
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
|
+
}
|
|
2630
3512
|
const a = to.runouts.initial;
|
|
2631
3513
|
const b = from.runouts.initial;
|
|
2632
|
-
if (
|
|
2633
|
-
|
|
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;
|
|
2634
3522
|
}
|
|
2635
|
-
const match = matchNodes(to.tree, from.tree);
|
|
3523
|
+
const match = matchNodes(to.tree, from.tree, tolerance);
|
|
2636
3524
|
const { tree } = to;
|
|
2637
3525
|
const width = to.layout.maxActions * COMBOS;
|
|
2638
3526
|
const oldRegret = new Float64Array(width);
|
|
2639
3527
|
const oldSum = new Float64Array(width);
|
|
2640
3528
|
const newRegret = new Float64Array(width);
|
|
2641
3529
|
const newSum = new Float64Array(width);
|
|
2642
|
-
const
|
|
3530
|
+
const keepsRegrets = (ctx) => ctx.regret.length > 0 || (ctx.compactRegret ?? null) !== null;
|
|
3531
|
+
const hasRegret = keepsRegrets(to) && keepsRegrets(from);
|
|
2643
3532
|
const position = new Int32Array(COMBOS).fill(-1);
|
|
2644
3533
|
let matched = 0;
|
|
2645
3534
|
for (const node of tree.decisionNodes) {
|
|
@@ -2652,20 +3541,24 @@ function warmStart(to, from, averages) {
|
|
|
2652
3541
|
const oldActions = from.tree.numEdges[old];
|
|
2653
3542
|
const actionOf = new Int32Array(actions).fill(-1);
|
|
2654
3543
|
for (let e = 0; e < actions; e++) {
|
|
2655
|
-
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);
|
|
2656
3545
|
if (other >= 0) actionOf[e] = other - from.tree.firstEdge[old];
|
|
2657
3546
|
}
|
|
2658
3547
|
for (let board = 0; board < to.runouts.counts[round]; board++) {
|
|
2659
|
-
const resolved = resolveBoard(from.runouts, realCards(to.runouts, round, board));
|
|
3548
|
+
const resolved = resolveBoard(from.runouts, asSource(realCards(to.runouts, round, board)));
|
|
2660
3549
|
const oldSlot = (from.active.roundBase[round] + resolved.board) * 2 + owner;
|
|
2661
3550
|
const oldFrom = from.active.start[oldSlot];
|
|
2662
3551
|
const oldCount = from.active.count[oldSlot];
|
|
2663
3552
|
const oldBlock = from.layout.blockOffset[old] + resolved.board;
|
|
2664
3553
|
const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
|
|
2665
3554
|
const oldLength = oldActions * oldCount;
|
|
2666
|
-
if (
|
|
2667
|
-
|
|
2668
|
-
|
|
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);
|
|
2669
3562
|
const newSlot = (to.active.roundBase[round] + board) * 2 + owner;
|
|
2670
3563
|
const newFrom = to.active.start[newSlot];
|
|
2671
3564
|
const count = to.active.count[newSlot];
|
|
@@ -2673,34 +3566,659 @@ function warmStart(to, from, averages) {
|
|
|
2673
3566
|
newRegret.fill(0, 0, length);
|
|
2674
3567
|
newSum.fill(0, 0, length);
|
|
2675
3568
|
for (let i = 0; i < count; i++) {
|
|
2676
|
-
const
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
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
|
+
}
|
|
2683
3581
|
}
|
|
2684
3582
|
}
|
|
2685
|
-
|
|
3583
|
+
markMembers(from.active, oldFrom, oldCount, position, true);
|
|
2686
3584
|
const block = to.layout.blockOffset[node] + board;
|
|
2687
3585
|
const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
|
|
2688
|
-
if (
|
|
2689
|
-
|
|
2690
|
-
|
|
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
|
+
}
|
|
2691
3592
|
}
|
|
2692
3593
|
}
|
|
2693
3594
|
return { matchedNodes: matched, decisionNodes: tree.decisionNodes.length, averages };
|
|
2694
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
|
+
}
|
|
2695
3608
|
|
|
2696
|
-
// 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
|
|
2697
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
|
|
2698
4216
|
var OPS_PER_SECOND = 2e8;
|
|
2699
4217
|
var SpotTooLargeError = class extends Error {
|
|
2700
4218
|
estimate;
|
|
2701
4219
|
constructor(estimate, maxBytes) {
|
|
2702
4220
|
super(
|
|
2703
|
-
`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)}.
|
|
2704
4222
|
${estimate.decisionNodes} decision nodes x up to ${estimate.runouts.toLocaleString()} runouts x ${Math.max(...estimate.combos)} combos.
|
|
2705
4223
|
` + (estimate.suggestions.length ? `Try:
|
|
2706
4224
|
${estimate.suggestions.map((s) => ` - ${s}`).join("\n")}
|
|
@@ -2730,16 +4248,7 @@ function defineSpot(config) {
|
|
|
2730
4248
|
const rounds = 6 - board.length;
|
|
2731
4249
|
if (config.pot <= 0) throw new Error("pot must be positive");
|
|
2732
4250
|
if (config.stack <= 0) throw new Error("stack must be positive");
|
|
2733
|
-
const
|
|
2734
|
-
const expanded = sizes.length === 1 ? Array.from({ length: rounds }, () => sizes[0]) : sizes;
|
|
2735
|
-
if (expanded.length !== rounds) {
|
|
2736
|
-
throw new Error(
|
|
2737
|
-
`${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`
|
|
2738
|
-
);
|
|
2739
|
-
}
|
|
2740
|
-
return expanded;
|
|
2741
|
-
};
|
|
2742
|
-
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);
|
|
2743
4252
|
const onBoard = new Set(board);
|
|
2744
4253
|
const trim = (range, who) => {
|
|
2745
4254
|
const weights = typeof range === "string" ? parseRange(range) : Float64Array.from(range);
|
|
@@ -2769,34 +4278,21 @@ function defineSpot(config) {
|
|
|
2769
4278
|
buildPlayerIndex(trim(config.ip, "in-position"))
|
|
2770
4279
|
];
|
|
2771
4280
|
const symmetries = config.isomorphism === false || board.length === 5 ? void 0 : suitSymmetries(board, [players[0].weights, players[1].weights]);
|
|
2772
|
-
const
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
);
|
|
2777
|
-
const tree = buildTree(
|
|
2778
|
-
{
|
|
2779
|
-
potUnits: Math.round(config.pot * chipScale),
|
|
2780
|
-
stackUnits: Math.round(config.stack * chipScale),
|
|
2781
|
-
betSizes: rows,
|
|
2782
|
-
raiseSizes: config.raiseSizes,
|
|
2783
|
-
maxRaises: config.maxRaises ?? 1,
|
|
2784
|
-
allowAllIn: config.allowAllIn ?? false,
|
|
2785
|
-
allInThreshold: config.allInThreshold,
|
|
2786
|
-
noDonk: config.noDonk,
|
|
2787
|
-
mergeSizes: config.mergeSizes,
|
|
2788
|
-
chipScale,
|
|
2789
|
-
minRaiseUnits: chipScale
|
|
2790
|
-
},
|
|
2791
|
-
runouts
|
|
2792
|
-
);
|
|
2793
|
-
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);
|
|
2794
4286
|
const last = runouts.rounds - 1;
|
|
2795
4287
|
const showdownBytes = runouts.counts[last] * (2 * COMBOS + COMBOS + 1) * 2;
|
|
2796
4288
|
const bytes = layout.bytes + showdownBytes;
|
|
2797
|
-
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;
|
|
2798
4294
|
const suggestions = [];
|
|
2799
|
-
if (
|
|
4295
|
+
if (needed > maxBytes) {
|
|
2800
4296
|
const widest = Math.max(...[0, 1].flatMap((p) => forPlayer(rows, p).map((r) => r.length)));
|
|
2801
4297
|
if (widest > 1) suggestions.push(`use fewer bet sizes (currently up to ${widest} per round)`);
|
|
2802
4298
|
const raises = [0, 1].flatMap((p) => [forPlayer(config.maxRaises ?? 1, p)].flat());
|
|
@@ -2812,8 +4308,9 @@ function defineSpot(config) {
|
|
|
2812
4308
|
totalSlots: layout.totalSlots,
|
|
2813
4309
|
bytes,
|
|
2814
4310
|
compressedBytes,
|
|
2815
|
-
|
|
2816
|
-
|
|
4311
|
+
compactBytes,
|
|
4312
|
+
secondsPerThousandIterations: walkedSlots * 4 * 1e3 / OPS_PER_SECOND,
|
|
4313
|
+
fits: needed <= maxBytes,
|
|
2817
4314
|
suggestions
|
|
2818
4315
|
};
|
|
2819
4316
|
if (!estimate.fits) throw new SpotTooLargeError(estimate, maxBytes);
|
|
@@ -2827,30 +4324,107 @@ function defineSpot(config) {
|
|
|
2827
4324
|
potUnits: Math.round(config.pot * chipScale),
|
|
2828
4325
|
stackUnits: Math.round(config.stack * chipScale),
|
|
2829
4326
|
source: config,
|
|
4327
|
+
...historyAlias ? { historyAlias } : {},
|
|
2830
4328
|
resolveBoard(cards) {
|
|
2831
4329
|
return resolveBoard(runouts, typeof cards === "string" ? parseCards(cards) : cards);
|
|
2832
4330
|
}
|
|
2833
4331
|
};
|
|
2834
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
|
+
}
|
|
2835
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
|
+
];
|
|
2836
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
|
+
}
|
|
2837
4383
|
const maxBytes = spot.source.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
2838
|
-
const {
|
|
2839
|
-
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);
|
|
2840
4396
|
const relevant = new Uint8Array(COMBOS);
|
|
2841
4397
|
for (const combo of spot.players[0].combos) relevant[combo] = 1;
|
|
2842
4398
|
for (const combo of spot.players[1].combos) relevant[combo] = 1;
|
|
2843
4399
|
const showdown = buildShowdownTable(spot.runouts, relevant);
|
|
2844
|
-
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
|
+
);
|
|
2845
4408
|
const weights = runoutWeights(spot.runouts);
|
|
2846
|
-
const
|
|
2847
|
-
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;
|
|
2848
4415
|
const allocate = (bytes2) => plan ? new SharedArrayBuffer(bytes2) : new ArrayBuffer(bytes2);
|
|
2849
4416
|
const width = PRECISION_BYTES[precision];
|
|
2850
4417
|
const quantised = precision === "i16";
|
|
2851
4418
|
const active = buildActiveLists(spot.runouts, spot.players, showdown);
|
|
2852
4419
|
const hands = buildHandSpace(spot.runouts, spot.players, active);
|
|
2853
|
-
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(
|
|
2854
4428
|
{
|
|
2855
4429
|
tree: spot.tree,
|
|
2856
4430
|
runouts: spot.runouts,
|
|
@@ -2859,10 +4433,13 @@ function createRun(spot, options = {}) {
|
|
|
2859
4433
|
active,
|
|
2860
4434
|
hands,
|
|
2861
4435
|
runoutWeight: weights,
|
|
2862
|
-
chipScale: spot.chipScale
|
|
4436
|
+
chipScale: spot.chipScale,
|
|
4437
|
+
sampling: samplingPlan,
|
|
4438
|
+
...storageEngine ? { storage: { source: options.wasmStorage, quantised, compact } } : {}
|
|
2863
4439
|
},
|
|
2864
4440
|
plan ? { plan, threads: requested, spawn: spawnEngineWorker } : void 0
|
|
2865
4441
|
) : null;
|
|
4442
|
+
const engineCompact = engine?.compact ?? null;
|
|
2866
4443
|
const ctx = {
|
|
2867
4444
|
tree: spot.tree,
|
|
2868
4445
|
runouts: spot.runouts,
|
|
@@ -2871,8 +4448,16 @@ function createRun(spot, options = {}) {
|
|
|
2871
4448
|
showdown,
|
|
2872
4449
|
regret: engine ? engine.regret : options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
|
|
2873
4450
|
strategySum: engine ? engine.strategySum : makeSumStore(precision, allocate(layout.totalSlots * width)),
|
|
2874
|
-
regretScale: quantised ? new Float64Array(allocate(layout.blocks * 8))
|
|
2875
|
-
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)),
|
|
2876
4461
|
scratch: makeTerminalScratch(),
|
|
2877
4462
|
chipScale: spot.chipScale,
|
|
2878
4463
|
active,
|
|
@@ -2894,15 +4479,37 @@ function createRun(spot, options = {}) {
|
|
|
2894
4479
|
totalMass: matchupMass(spot.players[0].weights, spot.players[1].weights) * weights[0],
|
|
2895
4480
|
dcfr: (options.algorithm ?? "dcfr") === "dcfr"
|
|
2896
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);
|
|
2897
4484
|
let warm = null;
|
|
2898
4485
|
let offset = 0;
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
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;
|
|
2903
4512
|
}
|
|
2904
|
-
const solver = makeSolver(ctx);
|
|
2905
|
-
const pool = plan && !engine ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
|
|
2906
4513
|
if (pool) solver.useRoot(pool.traverse);
|
|
2907
4514
|
if (engine) solver.useRoot(engine.root(ctx.dcfr));
|
|
2908
4515
|
const threadCount = pool ? pool.workers : engine ? engine.threads : 1;
|
|
@@ -2936,7 +4543,8 @@ function createRun(spot, options = {}) {
|
|
|
2936
4543
|
if (!spot.runouts.approximate) return this.exploitabilityPercent();
|
|
2937
4544
|
const real = createRun(defineSpot({ ...spot.source, runoutBuckets: false }), {
|
|
2938
4545
|
workers: threadCount,
|
|
2939
|
-
precision
|
|
4546
|
+
precision,
|
|
4547
|
+
wasmStorage: options.wasmStorage
|
|
2940
4548
|
});
|
|
2941
4549
|
try {
|
|
2942
4550
|
warmStart(real.solver.ctx, ctx, true);
|
|
@@ -2946,13 +4554,118 @@ function createRun(spot, options = {}) {
|
|
|
2946
4554
|
}
|
|
2947
4555
|
},
|
|
2948
4556
|
snapshot(history = [], stoppedAt = "iterations") {
|
|
2949
|
-
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
|
+
);
|
|
2950
4567
|
}
|
|
2951
4568
|
};
|
|
2952
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
|
+
}
|
|
2953
4655
|
function sameSpot(a, b) {
|
|
2954
4656
|
const plain = ({ maxBytes: _bytes, maxCombos: _combos, isomorphism: _iso, ...game }) => JSON.stringify(game, (_, value) => value instanceof Float64Array ? Array.from(value) : value);
|
|
2955
|
-
|
|
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 };
|
|
2956
4669
|
}
|
|
2957
4670
|
var DEFAULT_BUCKET_GAP = 1;
|
|
2958
4671
|
function rangeRankMerge(players, maxGap) {
|
|
@@ -2973,16 +4686,18 @@ function rangeRankMerge(players, maxGap) {
|
|
|
2973
4686
|
}
|
|
2974
4687
|
return { ranks: (a, b) => allowed[a * 13 + b] === 1, maxGap };
|
|
2975
4688
|
}
|
|
2976
|
-
var
|
|
2977
|
-
function resolveWorkers(options) {
|
|
4689
|
+
var threadWarnings = /* @__PURE__ */ new Set();
|
|
4690
|
+
function resolveWorkers(options, engine) {
|
|
2978
4691
|
if (options.analysisOnly) return 1;
|
|
2979
4692
|
const asked = options.workers === "auto" ? defaultWorkerCount() : options.workers ?? 1;
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
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}`);
|
|
2984
4699
|
}
|
|
2985
|
-
return
|
|
4700
|
+
return limit;
|
|
2986
4701
|
}
|
|
2987
4702
|
return asked;
|
|
2988
4703
|
}
|
|
@@ -2998,15 +4713,18 @@ function solve(spot, options = {}) {
|
|
|
2998
4713
|
const iterations = options.iterations ?? 500;
|
|
2999
4714
|
const target = options.targetPercent ?? 0;
|
|
3000
4715
|
const fixed = options.exploitEvery ?? 0;
|
|
4716
|
+
const settle = options.settle ? settlePlan(options.settle) : null;
|
|
3001
4717
|
const run = createRun(spot, options);
|
|
3002
4718
|
const history = [];
|
|
3003
|
-
const started = Date.now();
|
|
4719
|
+
const started = Date.now() - (coarseMs.get(run.solver) ?? 0);
|
|
4720
|
+
const before = coarseMs.has(run.solver) ? run.totalIterations : 0;
|
|
3004
4721
|
try {
|
|
3005
4722
|
return runAll();
|
|
3006
4723
|
} finally {
|
|
3007
4724
|
run.close();
|
|
3008
4725
|
}
|
|
3009
4726
|
function runAll() {
|
|
4727
|
+
if (settle) return runSettled(settle);
|
|
3010
4728
|
if (fixed <= 0 && target <= 0) {
|
|
3011
4729
|
run.step(iterations);
|
|
3012
4730
|
return run.snapshot(history, "iterations");
|
|
@@ -3036,13 +4754,13 @@ function solve(spot, options = {}) {
|
|
|
3036
4754
|
next = done + fixed;
|
|
3037
4755
|
} else {
|
|
3038
4756
|
const predicted = predictCrossing(
|
|
3039
|
-
prevIteration,
|
|
4757
|
+
before + prevIteration,
|
|
3040
4758
|
prevExploitability,
|
|
3041
|
-
done,
|
|
4759
|
+
before + done,
|
|
3042
4760
|
info.exploitability,
|
|
3043
4761
|
target * spot.potUnits / (100 * spot.chipScale)
|
|
3044
4762
|
);
|
|
3045
|
-
const aim = predicted > 0 ? predicted * 0.95 : done * MAX_GROWTH;
|
|
4763
|
+
const aim = predicted > 0 ? predicted * 0.95 - before : done * MAX_GROWTH;
|
|
3046
4764
|
const bounded = Math.min(Math.max(aim, done * MIN_GROWTH), done * MAX_GROWTH);
|
|
3047
4765
|
next = Math.max(done + 1, Math.ceil(bounded));
|
|
3048
4766
|
prevIteration = done;
|
|
@@ -3051,6 +4769,36 @@ function solve(spot, options = {}) {
|
|
|
3051
4769
|
}
|
|
3052
4770
|
return run.snapshot(history, stoppedAt);
|
|
3053
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
|
+
}
|
|
3054
4802
|
}
|
|
3055
4803
|
var FIRST_CHECK = 10;
|
|
3056
4804
|
var MIN_GROWTH = 1.1;
|
|
@@ -3062,7 +4810,7 @@ function predictCrossing(t1, e1, t2, e2, target) {
|
|
|
3062
4810
|
const predicted = t2 * (e2 / target) ** (1 / p);
|
|
3063
4811
|
return Number.isFinite(predicted) ? predicted : 0;
|
|
3064
4812
|
}
|
|
3065
|
-
function makeSolution(spot,
|
|
4813
|
+
function makeSolution(spot, solver, iterations, totalIterations, history, stoppedAt, workers, currentProfileEv) {
|
|
3066
4814
|
const { tree } = spot;
|
|
3067
4815
|
const parent = new Int32Array(tree.nodeCount).fill(-1);
|
|
3068
4816
|
const viaEdge = new Int32Array(tree.nodeCount).fill(-1);
|
|
@@ -3073,10 +4821,16 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3073
4821
|
viaEdge[tree.edgeChild[edge]] = edge;
|
|
3074
4822
|
}
|
|
3075
4823
|
}
|
|
3076
|
-
const label = (edge) =>
|
|
3077
|
-
|
|
3078
|
-
const
|
|
3079
|
-
|
|
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)";
|
|
3080
4834
|
};
|
|
3081
4835
|
const reachAt = (node, board, player) => {
|
|
3082
4836
|
const index = spot.players[player];
|
|
@@ -3094,6 +4848,8 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3094
4848
|
return reach;
|
|
3095
4849
|
};
|
|
3096
4850
|
let cachedEv = null;
|
|
4851
|
+
const rootValues = () => cachedEv ??= solver.rootValues(currentProfileEv ? Mode.Current : Mode.Average);
|
|
4852
|
+
const cachedHands = [null, null];
|
|
3097
4853
|
let cachedExploit = null;
|
|
3098
4854
|
return {
|
|
3099
4855
|
spot,
|
|
@@ -3104,8 +4860,11 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3104
4860
|
solver,
|
|
3105
4861
|
history,
|
|
3106
4862
|
ev(player) {
|
|
3107
|
-
|
|
3108
|
-
|
|
4863
|
+
return rootValues().totals[player];
|
|
4864
|
+
},
|
|
4865
|
+
handEvs(player) {
|
|
4866
|
+
cachedHands[player] ??= solver.handEvs(player, rootValues().values[player]);
|
|
4867
|
+
return cachedHands[player].slice();
|
|
3109
4868
|
},
|
|
3110
4869
|
exploitability() {
|
|
3111
4870
|
cachedExploit ??= solver.exploitability();
|
|
@@ -3123,16 +4882,7 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3123
4882
|
for (let a = 0; a < actions; a++) names.push(label(tree.firstEdge[node] + a));
|
|
3124
4883
|
return { node, board, player: owner, actions: names, combos: spot.players[owner].combos, probs };
|
|
3125
4884
|
},
|
|
3126
|
-
path
|
|
3127
|
-
const steps = [];
|
|
3128
|
-
let current = node;
|
|
3129
|
-
while (parent[current] >= 0) {
|
|
3130
|
-
const owner = parent[current];
|
|
3131
|
-
steps.push(tree.kind[owner] === NodeKind.Decision ? label(viaEdge[current]) : "/");
|
|
3132
|
-
current = owner;
|
|
3133
|
-
}
|
|
3134
|
-
return steps.reverse().join(" ") || "(root)";
|
|
3135
|
-
},
|
|
4885
|
+
path,
|
|
3136
4886
|
reachAt,
|
|
3137
4887
|
subgameAt(node, board, betting = {}) {
|
|
3138
4888
|
const opensRound = node === 0 || parent[node] >= 0 && tree.kind[parent[node]] === NodeKind.Chance;
|
|
@@ -3150,7 +4900,20 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3150
4900
|
const later = (value, listed) => listed && value.length > 1 ? value.slice(round) : value;
|
|
3151
4901
|
const laterEach = (value, listed) => value === void 0 ? void 0 : mapPlayers(value, (side) => later(side, listed(side)));
|
|
3152
4902
|
const rows = laterEach(source.betSizes, () => true);
|
|
3153
|
-
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;
|
|
3154
4917
|
return {
|
|
3155
4918
|
board: formatCards(cards),
|
|
3156
4919
|
pot: tree.pot[node] / spot.chipScale,
|
|
@@ -3158,17 +4921,21 @@ function makeSolution(spot, ctx, solver, iterations, totalIterations, history, s
|
|
|
3158
4921
|
oop: reachAt(node, board, 0),
|
|
3159
4922
|
ip: reachAt(node, board, 1),
|
|
3160
4923
|
betSizes: betting.betSizes ?? rows,
|
|
3161
|
-
raiseSizes: "raiseSizes" in betting ? betting.raiseSizes :
|
|
4924
|
+
raiseSizes: "raiseSizes" in betting ? betting.raiseSizes : raiseRows2,
|
|
3162
4925
|
maxRaises: betting.maxRaises ?? laterEach(source.maxRaises, Array.isArray),
|
|
3163
4926
|
allowAllIn: betting.allowAllIn ?? laterEach(source.allowAllIn, Array.isArray),
|
|
3164
4927
|
allInThreshold: "allInThreshold" in betting ? betting.allInThreshold : laterEach(source.allInThreshold, Array.isArray),
|
|
3165
4928
|
noDonk: "noDonk" in betting ? betting.noDonk : source.noDonk === void 0 ? void 0 : later(source.noDonk, Array.isArray(source.noDonk)),
|
|
3166
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 } : {},
|
|
3167
4933
|
minWeight: source.minWeight,
|
|
3168
4934
|
isomorphism: source.isomorphism,
|
|
3169
4935
|
maxCombos: "maxCombos" in betting ? betting.maxCombos : source.maxCombos,
|
|
3170
4936
|
maxBytes: "maxBytes" in betting ? betting.maxBytes : source.maxBytes,
|
|
3171
|
-
chipScale: spot.chipScale
|
|
4937
|
+
chipScale: spot.chipScale,
|
|
4938
|
+
compactStorage: source.compactStorage
|
|
3172
4939
|
};
|
|
3173
4940
|
}
|
|
3174
4941
|
};
|
|
@@ -3189,8 +4956,10 @@ export {
|
|
|
3189
4956
|
SUITS,
|
|
3190
4957
|
SpotTooLargeError,
|
|
3191
4958
|
activeCombos,
|
|
4959
|
+
bettingFrom,
|
|
3192
4960
|
cardOf,
|
|
3193
4961
|
categoryOf,
|
|
4962
|
+
checkedDown,
|
|
3194
4963
|
comboIndex,
|
|
3195
4964
|
createRun,
|
|
3196
4965
|
defineSpot,
|
|
@@ -3199,6 +4968,10 @@ export {
|
|
|
3199
4968
|
formatCard,
|
|
3200
4969
|
formatCards,
|
|
3201
4970
|
formatCombo,
|
|
4971
|
+
keepWithin,
|
|
4972
|
+
linesWithin,
|
|
4973
|
+
liteConfig,
|
|
4974
|
+
litePreset,
|
|
3202
4975
|
makeRegretStore,
|
|
3203
4976
|
makeSumStore,
|
|
3204
4977
|
parseCard,
|
|
@@ -3207,9 +4980,18 @@ export {
|
|
|
3207
4980
|
predictCrossing,
|
|
3208
4981
|
rangeWeight,
|
|
3209
4982
|
rankOf,
|
|
4983
|
+
rankSwap,
|
|
4984
|
+
resolveSubgame,
|
|
4985
|
+
rootPlay,
|
|
4986
|
+
rootPlayMovement,
|
|
4987
|
+
rootPlayNodes,
|
|
3210
4988
|
simdAvailable,
|
|
3211
4989
|
solve,
|
|
4990
|
+
solveLite,
|
|
4991
|
+
storageCost,
|
|
3212
4992
|
suitLanes,
|
|
3213
|
-
suitOf
|
|
4993
|
+
suitOf,
|
|
4994
|
+
trackSettle,
|
|
4995
|
+
turnLineReach
|
|
3214
4996
|
};
|
|
3215
4997
|
//# sourceMappingURL=index.js.map
|