poker-flopsolve 0.1.0 → 0.3.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/dist/browser.js CHANGED
@@ -651,9 +651,9 @@ function expand(segment) {
651
651
  if (kind !== "o") suitedCombos(high2, low, out);
652
652
  if (kind !== "s") offsuitCombos(high2, low, out);
653
653
  };
654
- const span = /^([AKQJT2-9])([AKQJT2-9])([so])?-([AKQJT2-9])([AKQJT2-9])([so])?$/.exec(text);
655
- if (span) {
656
- const [, a1, a2, k1, b1, b2, k2] = span;
654
+ const span2 = /^([AKQJT2-9])([AKQJT2-9])([so])?-([AKQJT2-9])([AKQJT2-9])([so])?$/.exec(text);
655
+ if (span2) {
656
+ const [, a1, a2, k1, b1, b2, k2] = span2;
657
657
  const kind = k1 ?? k2;
658
658
  if (k1 && k2 && k1 !== k2) throw new Error(`"${text}" mixes suited and offsuit`);
659
659
  const from1 = rankOfChar(a1);
@@ -954,100 +954,104 @@ function buildShowdownTable(runouts, relevant) {
954
954
  bytes: order.byteLength + liveCount.byteLength + groupStart.byteLength + groupCount.byteLength
955
955
  };
956
956
  }
957
- function showdownValues(reach, table, board, payoff, scratch, out) {
958
- const { weaker, stronger, win } = scratch;
959
- const cards = COMBO_CARDS;
960
- const order = table.order;
961
- const starts = table.groupStart;
962
- const orderBase = board * table.stride;
963
- const groupBase = board * (table.stride + 1);
964
- const groups = table.groupCount[board];
965
- if (table.liveCount[board] === 0) return;
966
- let running = 0;
967
- for (let g = 0; g < groups; g++) {
968
- const from = starts[groupBase + g];
969
- const to = starts[groupBase + g + 1];
970
- if (to - from === 1) {
971
- const combo = order[orderBase + from];
972
- const a = cards[combo * 2];
973
- const b = cards[combo * 2 + 1];
974
- win[combo] = running - (weaker[a] + weaker[b]);
975
- const value = reach[combo];
976
- if (value !== 0) {
977
- running += value;
978
- weaker[a] += value;
979
- weaker[b] += value;
957
+
958
+ // src/hands.ts
959
+ function span(lists, round, board, player) {
960
+ const slot = (lists.roundBase[round] + board) * 2 + player;
961
+ return [lists.start[slot], lists.count[slot]];
962
+ }
963
+ function buildHandSpace(runouts, players, lists) {
964
+ void players;
965
+ const indexOf = new Int32Array(COMBOS).fill(-1);
966
+ const upStart = [new Int32Array(0)];
967
+ const up = [];
968
+ for (let round = 1; round < runouts.rounds; round++) {
969
+ const orbitStart = runouts.orbitStart[round];
970
+ const starts = new Int32Array(runouts.orbitMap[round].length * 2);
971
+ for (let child = 0; child < runouts.counts[round]; child++) {
972
+ const parent = runouts.parent[round][child];
973
+ for (let player = 0; player < 2; player++) {
974
+ const [parentFrom, parentCount] = span(lists, round - 1, parent, player);
975
+ for (let i = 0; i < parentCount; i++) indexOf[lists.list[parentFrom + i]] = i;
976
+ const [childFrom, childCount] = span(lists, round, child, player);
977
+ for (let m = orbitStart[child]; m < orbitStart[child + 1]; m++) {
978
+ const back = runouts.maps.combo[runouts.maps.inverse(runouts.orbitMap[round][m])];
979
+ starts[m * 2 + player] = up.length;
980
+ for (let k = 0; k < childCount; k++) {
981
+ const real = back[lists.list[childFrom + k]];
982
+ const at = indexOf[real];
983
+ if (at < 0) throw new Error("A relabelled hand is not in its range; the relabelling must preserve both ranges");
984
+ up.push(at);
985
+ }
986
+ }
987
+ for (let i = 0; i < parentCount; i++) indexOf[lists.list[parentFrom + i]] = -1;
980
988
  }
981
- continue;
982
- }
983
- for (let i = from; i < to; i++) {
984
- const combo = order[orderBase + i];
985
- win[combo] = running - (weaker[cards[combo * 2]] + weaker[cards[combo * 2 + 1]]);
986
- }
987
- for (let i = from; i < to; i++) {
988
- const combo = order[orderBase + i];
989
- const value = reach[combo];
990
- if (value === 0) continue;
991
- running += value;
992
- weaker[cards[combo * 2]] += value;
993
- weaker[cards[combo * 2 + 1]] += value;
994
989
  }
990
+ upStart.push(starts);
995
991
  }
996
- running = 0;
997
- for (let g = groups - 1; g >= 0; g--) {
998
- const from = starts[groupBase + g];
999
- const to = starts[groupBase + g + 1];
1000
- if (to - from === 1) {
1001
- const combo = order[orderBase + from];
1002
- const a = cards[combo * 2];
1003
- const b = cards[combo * 2 + 1];
1004
- out[combo] = payoff * (win[combo] - (running - (stronger[a] + stronger[b])));
1005
- const value = reach[combo];
1006
- if (value !== 0) {
1007
- running += value;
1008
- stronger[a] += value;
1009
- stronger[b] += value;
992
+ const slots = lists.roundBase[runouts.rounds] * 2;
993
+ const sameStart = new Int32Array(slots + 1);
994
+ const same = [];
995
+ for (let round = 0; round < runouts.rounds; round++) {
996
+ for (let board = 0; board < runouts.counts[round]; board++) {
997
+ for (let player = 0; player < 2; player++) {
998
+ const slot = (lists.roundBase[round] + board) * 2 + player;
999
+ const [otherFrom, otherCount] = span(lists, round, board, 1 - player);
1000
+ for (let j = 0; j < otherCount; j++) indexOf[lists.list[otherFrom + j]] = j;
1001
+ const [from, count] = span(lists, round, board, player);
1002
+ sameStart[slot] = same.length;
1003
+ for (let i = 0; i < count; i++) same.push(indexOf[lists.list[from + i]]);
1004
+ for (let j = 0; j < otherCount; j++) indexOf[lists.list[otherFrom + j]] = -1;
1010
1005
  }
1011
- continue;
1012
- }
1013
- for (let i = from; i < to; i++) {
1014
- const combo = order[orderBase + i];
1015
- out[combo] = payoff * (win[combo] - (running - (stronger[cards[combo * 2]] + stronger[cards[combo * 2 + 1]])));
1016
- }
1017
- for (let i = from; i < to; i++) {
1018
- const combo = order[orderBase + i];
1019
- const value = reach[combo];
1020
- if (value === 0) continue;
1021
- running += value;
1022
- stronger[cards[combo * 2]] += value;
1023
- stronger[cards[combo * 2 + 1]] += value;
1024
1006
  }
1025
1007
  }
1026
- weaker.fill(0, 0, DECK);
1027
- stronger.fill(0, 0, DECK);
1008
+ sameStart[slots] = same.length;
1009
+ return { upStart, up: Uint16Array.from(up), sameStart, same: Int32Array.from(same) };
1028
1010
  }
1029
- function foldValues(reach, live, liveCount, payoff, scratch, out) {
1030
- const sums = scratch.all;
1031
- const cards = COMBO_CARDS;
1032
- const deck = DECK;
1033
- if (liveCount === 0) return;
1011
+ function foldValuesHands(oppReach, cardA, cardB, oppFrom, oppCount, myFrom, myCount, same, sameFrom, payoff, sums, out) {
1034
1012
  let total = 0;
1035
- for (let i = 0; i < liveCount; i++) {
1036
- const combo = live[i];
1037
- const value = reach[combo];
1038
- if (value === 0) continue;
1013
+ for (let j = 0; j < oppCount; j++) {
1014
+ const value = oppReach[j];
1039
1015
  total += value;
1040
- sums[cards[combo * 2]] += value;
1041
- sums[cards[combo * 2 + 1]] += value;
1042
- sums[deck + combo] += value;
1016
+ sums[cardA[oppFrom + j]] += value;
1017
+ sums[cardB[oppFrom + j]] += value;
1043
1018
  }
1044
- for (let i = 0; i < liveCount; i++) {
1045
- const combo = live[i];
1046
- const pair = deck + combo;
1047
- out[combo] = payoff * (total - (sums[cards[combo * 2]] + sums[cards[combo * 2 + 1]] - sums[pair]));
1048
- sums[pair] = 0;
1019
+ for (let i = 0; i < myCount; i++) {
1020
+ const twin = same[sameFrom + i];
1021
+ const both = twin >= 0 ? oppReach[twin] : 0;
1022
+ out[i] = payoff * (total - (sums[cardA[myFrom + i]] + sums[cardB[myFrom + i]] - both));
1049
1023
  }
1050
- sums.fill(0, 0, deck);
1024
+ sums.fill(0, 0, DECK);
1025
+ }
1026
+ function showdownValuesHands(oppReach, cardA, cardB, strength, oppFrom, oppCount, myFrom, myCount, payoff, sums, out) {
1027
+ let running = 0;
1028
+ let j = 0;
1029
+ for (let i = 0; i < myCount; i++) {
1030
+ const level = strength[myFrom + i];
1031
+ while (j < oppCount && strength[oppFrom + j] < level) {
1032
+ const value = oppReach[j];
1033
+ running += value;
1034
+ sums[cardA[oppFrom + j]] += value;
1035
+ sums[cardB[oppFrom + j]] += value;
1036
+ j++;
1037
+ }
1038
+ out[i] = running - (sums[cardA[myFrom + i]] + sums[cardB[myFrom + i]]);
1039
+ }
1040
+ sums.fill(0, 0, DECK);
1041
+ running = 0;
1042
+ j = oppCount - 1;
1043
+ for (let i = myCount - 1; i >= 0; i--) {
1044
+ const level = strength[myFrom + i];
1045
+ while (j >= 0 && strength[oppFrom + j] > level) {
1046
+ const value = oppReach[j];
1047
+ running += value;
1048
+ sums[cardA[oppFrom + j]] += value;
1049
+ sums[cardB[oppFrom + j]] += value;
1050
+ j--;
1051
+ }
1052
+ out[i] = payoff * (out[i] - (running - (sums[cardA[myFrom + i]] + sums[cardB[myFrom + i]])));
1053
+ }
1054
+ sums.fill(0, 0, DECK);
1051
1055
  }
1052
1056
 
1053
1057
  // src/pairs.ts
@@ -1079,7 +1083,8 @@ function makeBuffers(ctx, width) {
1079
1083
  live: make(() => new Uint16Array(COMBOS)),
1080
1084
  hands: make(() => new Uint16Array(width)),
1081
1085
  strategy: make(() => new Float64Array(actions * COMBOS)),
1082
- rank: new Int32Array(COMBOS)
1086
+ rank: new Int32Array(COMBOS),
1087
+ position: new Int32Array(COMBOS)
1083
1088
  };
1084
1089
  }
1085
1090
  function pairValues(ctx, node, board, traverser, hands, handReach, live, liveCount) {
@@ -1222,16 +1227,15 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
1222
1227
  const actions = tree.numEdges[node];
1223
1228
  const edgeBase = tree.firstEdge[node];
1224
1229
  const owner = tree.player[node];
1225
- const index = ctx.players[owner];
1226
- const stride = index.count;
1227
- const slotBase = ctx.layout.offset[node] + board * actions * stride;
1228
1230
  const lists = ctx.active;
1229
1231
  const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
1230
1232
  const active = lists.list;
1231
1233
  const activeFrom = lists.start[listSlot];
1232
1234
  const activeCount = lists.count[listSlot];
1235
+ const stride = activeCount;
1236
+ const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
1233
1237
  const strategy = buffers.strategy[depth];
1234
- averageStrategy(ctx.strategySum, slotBase, actions, stride, active, lists.slot, activeFrom, activeCount, strategy, ctx.regret);
1238
+ averageStrategy(ctx.strategySum, slotBase, actions, stride, strategy, ctx.regret);
1235
1239
  if (owner === traverser) {
1236
1240
  for (let a = 0; a < actions; a++) {
1237
1241
  const offset = a * COMBOS;
@@ -1253,7 +1257,7 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
1253
1257
  const row = cols[c] * COMBOS;
1254
1258
  for (let i = 0; i < activeCount; i++) {
1255
1259
  const combo = active[activeFrom + i];
1256
- out[row + combo] += strategy[a * stride + lists.slot[activeFrom + i]] * value[row + combo];
1260
+ out[row + combo] += strategy[a * stride + i] * value[row + combo];
1257
1261
  }
1258
1262
  }
1259
1263
  }
@@ -1261,12 +1265,14 @@ function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCo
1261
1265
  }
1262
1266
  const childReach = buffers.colReach[depth + 1];
1263
1267
  const childCols = buffers.cols[depth + 1];
1268
+ const position = buffers.position;
1269
+ for (let i = 0; i < activeCount; i++) position[active[activeFrom + i]] = i;
1264
1270
  for (let a = 0; a < actions; a++) {
1265
1271
  const offset = a * COMBOS;
1266
1272
  let childCount = 0;
1267
1273
  for (let c = 0; c < colCount; c++) {
1268
1274
  const k = cols[c];
1269
- const r = colReach[k] * strategy[a * stride + index.slot[hands[k]]];
1275
+ const r = colReach[k] * strategy[a * stride + position[hands[k]]];
1270
1276
  childReach[k] = r;
1271
1277
  if (r !== 0) childCols[childCount++] = k;
1272
1278
  }
@@ -1359,6 +1365,29 @@ function buildPlayerIndex(weights) {
1359
1365
  return { combos: Uint16Array.from(combos), count: combos.length, slot, weights };
1360
1366
  }
1361
1367
  function buildLayout(tree, runouts, players, bytesPerElement) {
1368
+ const roundBase = new Int32Array(runouts.rounds + 1);
1369
+ for (let round = 0; round < runouts.rounds; round++) roundBase[round + 1] = roundBase[round] + runouts.counts[round];
1370
+ const rowStart = new Int32Array(roundBase[runouts.rounds] * 2);
1371
+ const totals = new Int32Array(runouts.rounds * 2);
1372
+ const onBoard = new Uint8Array(DECK);
1373
+ for (let round = 0; round < runouts.rounds; round++) {
1374
+ const width = runouts.width[round];
1375
+ const cards = runouts.cards[round];
1376
+ for (let board = 0; board < runouts.counts[round]; board++) {
1377
+ onBoard.fill(0);
1378
+ for (let i = 0; i < width; i++) onBoard[cards[board * width + i]] = 1;
1379
+ for (let player = 0; player < 2; player++) {
1380
+ const index = players[player];
1381
+ let live = 0;
1382
+ for (let i = 0; i < index.count; i++) {
1383
+ const combo = index.combos[i];
1384
+ if (!onBoard[COMBO_CARDS[combo * 2]] && !onBoard[COMBO_CARDS[combo * 2 + 1]]) live++;
1385
+ }
1386
+ rowStart[(roundBase[round] + board) * 2 + player] = totals[round * 2 + player];
1387
+ totals[round * 2 + player] += live;
1388
+ }
1389
+ }
1390
+ }
1362
1391
  const offset = new Int32Array(tree.nodeCount).fill(-1);
1363
1392
  const blockOffset = new Int32Array(tree.nodeCount).fill(-1);
1364
1393
  let total = 0;
@@ -1370,11 +1399,20 @@ function buildLayout(tree, runouts, players, bytesPerElement) {
1370
1399
  if (actions > maxActions) maxActions = actions;
1371
1400
  offset[node] = total;
1372
1401
  blockOffset[node] = blocks;
1373
- total += runouts.counts[tree.round[node]] * actions * players[tree.player[node]].count;
1402
+ total += actions * totals[tree.round[node] * 2 + tree.player[node]];
1374
1403
  blocks += runouts.counts[tree.round[node]];
1375
1404
  }
1376
1405
  const scales = bytesPerElement === 2 ? blocks * 8 * 2 : 0;
1377
- return { offset, blockOffset, blocks, totalSlots: total, maxActions, bytes: total * bytesPerElement * 2 + scales };
1406
+ return {
1407
+ offset,
1408
+ blockOffset,
1409
+ blocks,
1410
+ totalSlots: total,
1411
+ maxActions,
1412
+ rowStart,
1413
+ roundBase,
1414
+ bytes: total * bytesPerElement * 2 + scales
1415
+ };
1378
1416
  }
1379
1417
  var REGRET_STEPS = 32767;
1380
1418
  var SUM_STEPS = 65535;
@@ -1408,7 +1446,7 @@ function matchupMass(a, b) {
1408
1446
  }
1409
1447
  return mass;
1410
1448
  }
1411
- function buildActiveLists(runouts, players) {
1449
+ function buildActiveLists(runouts, players, showdown = null) {
1412
1450
  const roundBase = new Int32Array(runouts.rounds + 1);
1413
1451
  for (let round = 0; round < runouts.rounds; round++) {
1414
1452
  roundBase[round + 1] = roundBase[round] + runouts.counts[round];
@@ -1429,10 +1467,18 @@ function buildActiveLists(runouts, players) {
1429
1467
  const index = players[player];
1430
1468
  const slot = (roundBase[round] + board) * 2 + player;
1431
1469
  start[slot] = entries.length;
1470
+ const here = [];
1432
1471
  for (let i = 0; i < index.count; i++) {
1433
1472
  const combo = index.combos[i];
1434
1473
  if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
1435
- entries.push(combo);
1474
+ here.push(i);
1475
+ }
1476
+ if (showdown !== null && round === runouts.rounds - 1) {
1477
+ const group = strengthOn(showdown, board);
1478
+ here.sort((x, y) => group[index.combos[x]] - group[index.combos[y]] || x - y);
1479
+ }
1480
+ for (const i of here) {
1481
+ entries.push(index.combos[i]);
1436
1482
  entrySlots.push(i);
1437
1483
  }
1438
1484
  count[slot] = entries.length - start[slot];
@@ -1449,15 +1495,15 @@ function buildActiveLists(runouts, players) {
1449
1495
  const slot = (roundBase[round] + board) * 2 + player;
1450
1496
  tieStart[slot] = ties.length;
1451
1497
  if (stabStart[board + 1] - stabStart[board] < 2) continue;
1452
- const index = players[player];
1453
- const seen = new Uint8Array(index.count);
1498
+ const position = /* @__PURE__ */ new Map();
1499
+ for (let e = start[slot]; e < start[slot] + count[slot]; e++) position.set(entries[e], e - start[slot]);
1500
+ const seen = new Uint8Array(count[slot]);
1454
1501
  for (let e = start[slot]; e < start[slot] + count[slot]; e++) {
1455
- const first = entrySlots[e];
1456
- if (seen[first]) continue;
1502
+ if (seen[e - start[slot]]) continue;
1457
1503
  const members = [];
1458
1504
  for (let k = stabStart[board]; k < stabStart[board + 1]; k++) {
1459
- const image = index.slot[runouts.maps.combo[stabMap[k]][entries[e]]];
1460
- if (image < 0 || seen[image]) continue;
1505
+ const image = position.get(runouts.maps.combo[stabMap[k]][entries[e]]);
1506
+ if (image === void 0 || seen[image]) continue;
1461
1507
  seen[image] = 1;
1462
1508
  members.push(image);
1463
1509
  }
@@ -1467,16 +1513,48 @@ function buildActiveLists(runouts, players) {
1467
1513
  }
1468
1514
  }
1469
1515
  tieStart[slots] = ties.length;
1516
+ const list = Uint16Array.from(entries);
1517
+ const cardA = new Uint8Array(list.length);
1518
+ const cardB = new Uint8Array(list.length);
1519
+ for (let e = 0; e < list.length; e++) {
1520
+ cardA[e] = COMBO_CARDS[list[e] * 2];
1521
+ cardB[e] = COMBO_CARDS[list[e] * 2 + 1];
1522
+ }
1523
+ const strength = new Int32Array(list.length);
1524
+ if (showdown !== null) {
1525
+ const last = runouts.rounds - 1;
1526
+ for (let board = 0; board < runouts.counts[last]; board++) {
1527
+ const group = strengthOn(showdown, board);
1528
+ for (let player = 0; player < 2; player++) {
1529
+ const slot = (roundBase[last] + board) * 2 + player;
1530
+ for (let e = start[slot]; e < start[slot] + count[slot]; e++) strength[e] = group[list[e]];
1531
+ }
1532
+ }
1533
+ }
1470
1534
  return {
1471
1535
  start,
1472
1536
  count,
1473
- list: Uint16Array.from(entries),
1537
+ list,
1474
1538
  slot: Uint16Array.from(entrySlots),
1475
1539
  roundBase,
1476
1540
  tieStart,
1477
- ties: Int32Array.from(ties)
1541
+ ties: Int32Array.from(ties),
1542
+ cardA,
1543
+ cardB,
1544
+ strength
1478
1545
  };
1479
1546
  }
1547
+ var strengthScratch = new Int32Array(COMBOS);
1548
+ function strengthOn(table, board) {
1549
+ const orderBase = board * table.stride;
1550
+ const groupBase = board * (table.stride + 1);
1551
+ for (let g = 0; g < table.groupCount[board]; g++) {
1552
+ for (let i = table.groupStart[groupBase + g]; i < table.groupStart[groupBase + g + 1]; i++) {
1553
+ strengthScratch[table.order[orderBase + i]] = g;
1554
+ }
1555
+ }
1556
+ return strengthScratch;
1557
+ }
1480
1558
  function tieMirrors(store, slotBase, actions, stride, ties, from, to, integer) {
1481
1559
  for (let a = 0; a < actions; a++) {
1482
1560
  const row = slotBase + a * stride;
@@ -1500,11 +1578,11 @@ function tieBlock(ctx, node, board) {
1500
1578
  const to = lists.tieStart[listSlot + 1];
1501
1579
  if (from === to) return;
1502
1580
  const actions = tree.numEdges[node];
1503
- const stride = ctx.players[owner].count;
1504
- const base = ctx.layout.offset[node] + board * actions * stride;
1581
+ const count = lists.count[listSlot];
1582
+ const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
1505
1583
  const integer = ctx.regretScale !== null;
1506
- if (ctx.regret.length > 0) tieMirrors(ctx.regret, base, actions, stride, lists.ties, from, to, integer);
1507
- tieMirrors(ctx.strategySum, base, actions, stride, lists.ties, from, to, integer);
1584
+ if (ctx.regret.length > 0) tieMirrors(ctx.regret, base, actions, count, lists.ties, from, to, integer);
1585
+ tieMirrors(ctx.strategySum, base, actions, count, lists.ties, from, to, integer);
1508
1586
  }
1509
1587
  function makeBuffers2(tree, maxActions, simd) {
1510
1588
  const depth = treeDepth2(tree) + 2;
@@ -1512,6 +1590,7 @@ function makeBuffers2(tree, maxActions, simd) {
1512
1590
  const kernels = simd ? loadKernels(depth, maxActions * COMBOS) : null;
1513
1591
  return {
1514
1592
  kernels,
1593
+ cardSums: new Float64Array(DECK),
1515
1594
  value: make(() => new Float64Array(COMBOS)),
1516
1595
  reachFor: [make(() => new Float64Array(COMBOS)), make(() => new Float64Array(COMBOS))],
1517
1596
  strategy: kernels ? Array.from({ length: depth }, (_, d) => kernels.strategy(d)) : make(() => new Float64Array(maxActions * COMBOS)),
@@ -1539,79 +1618,122 @@ function treeDepth2(tree) {
1539
1618
  }
1540
1619
  var DCFR_ALPHA = 1.5;
1541
1620
  var DCFR_BETA = 0;
1542
- var DCFR_GAMMA = 2;
1621
+ var DCFR_GAMMA = 3;
1622
+ function dcfrFactors(t) {
1623
+ const age = t > 2 ? t - 2 : 0;
1624
+ const grown = age ** DCFR_ALPHA;
1625
+ const shrunk = age ** DCFR_BETA;
1626
+ return {
1627
+ keepPositive: grown / (grown + 1),
1628
+ keepNegative: shrunk / (shrunk + 1),
1629
+ keepSum: ((t - 1) / t) ** DCFR_GAMMA
1630
+ };
1631
+ }
1543
1632
  var Mode = { Train: 0, BestResponse: 1, Average: 2 };
1544
- function normalise(source, base, actions, stride, active, slots, from, count, strategy) {
1633
+ function normalise(source, base, actions, count, strategy) {
1545
1634
  const uniform = 1 / actions;
1546
1635
  for (let i = 0; i < count; i++) {
1547
- const slot = slots[from + i];
1548
- const at = base + slot;
1636
+ const at = base + i;
1549
1637
  let total = 0;
1550
1638
  for (let a = 0; a < actions; a++) {
1551
- const value = source[at + a * stride];
1639
+ const value = source[at + a * count];
1552
1640
  const positive = value > 0 ? value : 0;
1553
- strategy[a * stride + slot] = positive;
1641
+ strategy[a * count + i] = positive;
1554
1642
  total += positive;
1555
1643
  }
1556
1644
  if (total > 0) {
1557
- for (let a = 0; a < actions; a++) strategy[a * stride + slot] /= total;
1645
+ for (let a = 0; a < actions; a++) strategy[a * count + i] /= total;
1558
1646
  } else {
1559
- for (let a = 0; a < actions; a++) strategy[a * stride + slot] = uniform;
1647
+ for (let a = 0; a < actions; a++) strategy[a * count + i] = uniform;
1560
1648
  }
1561
1649
  }
1562
1650
  }
1563
- function regretMatch(regret, base, actions, stride, active, slots, from, count, strategy) {
1564
- normalise(regret, base, actions, stride, active, slots, from, count, strategy);
1651
+ function regretMatch(regret, base, actions, count, strategy) {
1652
+ normalise(regret, base, actions, count, strategy);
1565
1653
  }
1566
- function averageStrategy(strategySum, base, actions, stride, active, slots, from, count, strategy, regret) {
1654
+ function averageStrategy(strategySum, base, actions, count, strategy, regret) {
1567
1655
  const fallback = regret !== void 0 && regret.length > 0 ? regret : null;
1568
1656
  const uniform = 1 / actions;
1569
1657
  for (let i = 0; i < count; i++) {
1570
- const slot = slots[from + i];
1571
- const at = base + slot;
1658
+ const at = base + i;
1572
1659
  let total = 0;
1573
1660
  for (let a = 0; a < actions; a++) {
1574
- const value = strategySum[at + a * stride];
1575
- strategy[a * stride + slot] = value;
1661
+ const value = strategySum[at + a * count];
1662
+ strategy[a * count + i] = value;
1576
1663
  total += value;
1577
1664
  }
1578
1665
  if (total > 0) {
1579
- for (let a = 0; a < actions; a++) strategy[a * stride + slot] /= total;
1666
+ for (let a = 0; a < actions; a++) strategy[a * count + i] /= total;
1580
1667
  continue;
1581
1668
  }
1582
1669
  if (fallback !== null) {
1583
1670
  for (let a = 0; a < actions; a++) {
1584
- const value = fallback[at + a * stride];
1671
+ const value = fallback[at + a * count];
1585
1672
  const positive = value > 0 ? value : 0;
1586
- strategy[a * stride + slot] = positive;
1673
+ strategy[a * count + i] = positive;
1587
1674
  total += positive;
1588
1675
  }
1589
1676
  }
1590
1677
  if (total > 0) {
1591
- for (let a = 0; a < actions; a++) strategy[a * stride + slot] /= total;
1678
+ for (let a = 0; a < actions; a++) strategy[a * count + i] /= total;
1592
1679
  } else {
1593
- for (let a = 0; a < actions; a++) strategy[a * stride + slot] = uniform;
1680
+ for (let a = 0; a < actions; a++) strategy[a * count + i] = uniform;
1594
1681
  }
1595
1682
  }
1596
1683
  }
1597
- function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachMe, reachOpp, live, liveCount, depth) {
1684
+ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachMe, reachOpp, depth) {
1598
1685
  const { tree, runouts } = ctx;
1599
1686
  const out = buffers.value[depth];
1600
1687
  const kind = tree.kind[node];
1688
+ const round = tree.round[node];
1689
+ const lists = ctx.active;
1690
+ const opponent = 1 - traverser;
1691
+ const mySlot = (lists.roundBase[round] + board) * 2 + traverser;
1692
+ const oppSlot = (lists.roundBase[round] + board) * 2 + opponent;
1693
+ const myFrom = lists.start[mySlot];
1694
+ const myCount = lists.count[mySlot];
1695
+ const oppFrom = lists.start[oppSlot];
1696
+ const oppCount = lists.count[oppSlot];
1601
1697
  if (kind === NodeKind.TerminalFold) {
1602
1698
  const signed = traverser === 0 ? tree.payoff[node] : -tree.payoff[node];
1603
- const weight = ctx.runoutWeight[tree.round[node]] / ctx.chipScale;
1604
- foldValues(reachOpp, live, liveCount, signed * weight, ctx.scratch, out);
1699
+ const weight = ctx.runoutWeight[round] / ctx.chipScale;
1700
+ const hands = ctx.hands;
1701
+ foldValuesHands(
1702
+ reachOpp,
1703
+ lists.cardA,
1704
+ lists.cardB,
1705
+ oppFrom,
1706
+ oppCount,
1707
+ myFrom,
1708
+ myCount,
1709
+ hands.same,
1710
+ hands.sameStart[mySlot],
1711
+ signed * weight,
1712
+ buffers.cardSums,
1713
+ out
1714
+ );
1605
1715
  return out;
1606
1716
  }
1607
1717
  if (kind === NodeKind.TerminalShowdown) {
1608
- const weight = ctx.runoutWeight[tree.round[node]] / ctx.chipScale;
1609
- showdownValues(reachOpp, ctx.showdown, board, tree.payoff[node] * weight, ctx.scratch, out);
1718
+ const weight = ctx.runoutWeight[round] / ctx.chipScale;
1719
+ showdownValuesHands(
1720
+ reachOpp,
1721
+ lists.cardA,
1722
+ lists.cardB,
1723
+ lists.strength,
1724
+ oppFrom,
1725
+ oppCount,
1726
+ myFrom,
1727
+ myCount,
1728
+ tree.payoff[node] * weight,
1729
+ buffers.cardSums,
1730
+ out
1731
+ );
1610
1732
  return out;
1611
1733
  }
1612
1734
  if (kind === NodeKind.Chance) {
1613
- for (let i = 0; i < liveCount; i++) out[live[i]] = 0;
1614
- const nextRound = tree.round[node] + 1;
1735
+ for (let i = 0; i < myCount; i++) out[i] = 0;
1736
+ const nextRound = round + 1;
1615
1737
  if (ctx.entryIndexOf !== null && nextRound === ctx.shardRound) {
1616
1738
  const entry = ctx.entryIndexOf[node];
1617
1739
  if (entry >= 0) {
@@ -1621,91 +1743,39 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1621
1743
  }
1622
1744
  if (ctx.chanceOverride !== null) {
1623
1745
  const base = entry * COMBOS;
1624
- for (let i = 0; i < liveCount; i++) out[live[i]] = ctx.chanceOverride[base + live[i]];
1746
+ for (let i = 0; i < myCount; i++) out[i] = ctx.chanceOverride[base + i];
1625
1747
  return out;
1626
1748
  }
1627
1749
  }
1628
1750
  }
1629
1751
  const child = tree.edgeChild[tree.firstEdge[node]];
1630
- const width = runouts.width[nextRound];
1631
- const cards = runouts.cards[nextRound];
1632
1752
  const orbitStart = runouts.orbitStart[nextRound];
1633
- const orbitMap = runouts.orbitMap[nextRound];
1634
- const orbitCard = runouts.orbitCard[nextRound];
1635
- const comboMaps = runouts.maps.combo;
1636
- const childLive = buffers.live[depth];
1637
- const comboCards = COMBO_CARDS;
1753
+ const upStart = ctx.hands.upStart[nextRound];
1754
+ const up = ctx.hands.up;
1755
+ const childMe = buffers.reachFor[traverser][depth];
1756
+ const childOpp = buffers.reachFor[opponent][depth];
1638
1757
  const sharded = nextRound === ctx.shardRound;
1639
1758
  const first = runouts.childStart[nextRound][board];
1640
1759
  const last = runouts.childStart[nextRound][board + 1];
1641
1760
  const from = sharded && ctx.shardStart > first ? ctx.shardStart : first;
1642
1761
  const to = sharded && ctx.shardEnd < last ? ctx.shardEnd : last;
1643
1762
  for (let childBoard = from; childBoard < to; childBoard++) {
1644
- const dealt = cards[childBoard * width + width - 1];
1645
- let childCount = 0;
1646
- for (let i = 0; i < liveCount; i++) {
1647
- const combo = live[i];
1648
- if (comboCards[combo * 2] === dealt || comboCards[combo * 2 + 1] === dealt) continue;
1649
- childLive[childCount++] = combo;
1650
- }
1651
- const value = traverse(
1652
- ctx,
1653
- buffers,
1654
- mode,
1655
- traverser,
1656
- iterWeight,
1657
- child,
1658
- childBoard,
1659
- reachMe,
1660
- reachOpp,
1661
- childLive,
1662
- childCount,
1663
- depth + 1
1664
- );
1665
- for (let i = 0; i < childCount; i++) out[childLive[i]] += value[childLive[i]];
1763
+ const childMySlot = (lists.roundBase[nextRound] + childBoard) * 2 + traverser;
1764
+ const childOppSlot = (lists.roundBase[nextRound] + childBoard) * 2 + opponent;
1765
+ const nMe = lists.count[childMySlot];
1766
+ const nOpp = lists.count[childOppSlot];
1767
+ const firstMember = orbitStart[childBoard];
1666
1768
  const membersEnd = orbitStart[childBoard + 1];
1667
- for (let m = orbitStart[childBoard] + 1; m < membersEnd; m++) {
1668
- const map = comboMaps[orbitMap[m]];
1669
- const real = orbitCard[m];
1670
- if (!ctx.expandOrbits) {
1671
- for (let i = 0; i < liveCount; i++) {
1672
- const combo = live[i];
1673
- if (comboCards[combo * 2] === real || comboCards[combo * 2 + 1] === real) continue;
1674
- out[combo] += value[map[combo]];
1675
- }
1676
- continue;
1677
- }
1678
- const mappedMe = buffers.mappedMe[depth];
1679
- const mappedOpp = buffers.mappedOpp[depth];
1680
- const mappedLive = buffers.mappedLive[depth];
1681
- let mappedCount = 0;
1682
- for (let i = 0; i < liveCount; i++) {
1683
- const combo = live[i];
1684
- if (comboCards[combo * 2] === real || comboCards[combo * 2 + 1] === real) continue;
1685
- const image = map[combo];
1686
- mappedMe[image] = reachMe[combo];
1687
- mappedOpp[image] = reachOpp[combo];
1688
- mappedLive[mappedCount++] = image;
1689
- }
1690
- const mirrored = traverse(
1691
- ctx,
1692
- buffers,
1693
- mode,
1694
- traverser,
1695
- iterWeight,
1696
- child,
1697
- childBoard,
1698
- mappedMe,
1699
- mappedOpp,
1700
- mappedLive,
1701
- mappedCount,
1702
- depth + 1
1703
- );
1704
- for (let i = 0; i < liveCount; i++) {
1705
- const combo = live[i];
1706
- if (comboCards[combo * 2] === real || comboCards[combo * 2 + 1] === real) continue;
1707
- out[combo] += mirrored[map[combo]];
1769
+ let value = null;
1770
+ for (let m = firstMember; m < membersEnd; m++) {
1771
+ const upMe = upStart[m * 2 + traverser];
1772
+ if (value === null || ctx.expandOrbits) {
1773
+ const upOpp = upStart[m * 2 + opponent];
1774
+ for (let k = 0; k < nMe; k++) childMe[k] = reachMe[up[upMe + k]];
1775
+ for (let k = 0; k < nOpp; k++) childOpp[k] = reachOpp[up[upOpp + k]];
1776
+ value = traverse(ctx, buffers, mode, traverser, iterWeight, child, childBoard, childMe, childOpp, depth + 1);
1708
1777
  }
1778
+ for (let k = 0; k < nMe; k++) out[up[upMe + k]] += value[k];
1709
1779
  }
1710
1780
  }
1711
1781
  return out;
@@ -1713,36 +1783,29 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1713
1783
  const actions = tree.numEdges[node];
1714
1784
  const edgeBase = tree.firstEdge[node];
1715
1785
  const owner = tree.player[node];
1716
- const index = ctx.players[owner];
1717
- const stride = index.count;
1718
- const slotBase = ctx.layout.offset[node] + board * actions * stride;
1719
1786
  const isTraverser = owner === traverser;
1720
- const lists = ctx.active;
1721
- const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
1722
- const active = lists.list;
1723
- const activeSlot = lists.slot;
1724
- const activeFrom = lists.start[listSlot];
1787
+ const listSlot = isTraverser ? mySlot : oppSlot;
1725
1788
  const activeCount = lists.count[listSlot];
1789
+ const stride = activeCount;
1790
+ const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
1726
1791
  const strategy = buffers.strategy[depth];
1792
+ const length = actions * stride;
1727
1793
  if (mode === Mode.Train && buffers.kernels !== null) {
1728
1794
  const kernels = buffers.kernels;
1729
- kernels.input.set(ctx.regret.subarray(slotBase, slotBase + actions * stride));
1795
+ kernels.input.set(ctx.regret.subarray(slotBase, slotBase + length));
1730
1796
  kernels.regretMatch(strategy, actions, stride);
1731
1797
  } else if (mode === Mode.Train) {
1732
- regretMatch(ctx.regret, slotBase, actions, stride, active, activeSlot, activeFrom, activeCount, strategy);
1798
+ regretMatch(ctx.regret, slotBase, actions, stride, strategy);
1733
1799
  } else if (buffers.kernels !== null) {
1734
1800
  const kernels = buffers.kernels;
1735
- const length = actions * stride;
1736
1801
  kernels.input.set(ctx.strategySum.subarray(slotBase, slotBase + length));
1737
1802
  const hasRegret = ctx.regret.length > 0;
1738
1803
  if (hasRegret) kernels.second.set(ctx.regret.subarray(slotBase, slotBase + length));
1739
1804
  kernels.averageMatch(strategy, actions, stride, hasRegret);
1740
1805
  } else {
1741
- averageStrategy(ctx.strategySum, slotBase, actions, stride, active, activeSlot, activeFrom, activeCount, strategy, ctx.regret);
1806
+ averageStrategy(ctx.strategySum, slotBase, actions, stride, strategy, ctx.regret);
1742
1807
  }
1743
- for (let i = 0; i < liveCount; i++) out[live[i]] = 0;
1744
1808
  if (isTraverser && mode === Mode.BestResponse) {
1745
- let first = true;
1746
1809
  for (let a = 0; a < actions; a++) {
1747
1810
  const value = traverse(
1748
1811
  ctx,
@@ -1754,32 +1817,23 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1754
1817
  board,
1755
1818
  reachMe,
1756
1819
  reachOpp,
1757
- live,
1758
- liveCount,
1759
1820
  depth + 1
1760
1821
  );
1761
- if (first) {
1762
- for (let i = 0; i < activeCount; i++) out[active[activeFrom + i]] = value[active[activeFrom + i]];
1763
- first = false;
1822
+ if (a === 0) {
1823
+ for (let i = 0; i < myCount; i++) out[i] = value[i];
1764
1824
  } else {
1765
- for (let i = 0; i < activeCount; i++) {
1766
- const combo = active[activeFrom + i];
1767
- if (value[combo] > out[combo]) out[combo] = value[combo];
1768
- }
1825
+ for (let i = 0; i < myCount; i++) if (value[i] > out[i]) out[i] = value[i];
1769
1826
  }
1770
1827
  }
1771
1828
  return out;
1772
1829
  }
1830
+ for (let i = 0; i < myCount; i++) out[i] = 0;
1773
1831
  const childReach = buffers.reachFor[owner][depth];
1774
1832
  const actionValues = buffers.actionValues[depth];
1775
1833
  for (let a = 0; a < actions; a++) {
1776
- const offset = a * COMBOS;
1777
1834
  const row = a * stride;
1778
1835
  const source = isTraverser ? reachMe : reachOpp;
1779
- for (let i = 0; i < activeCount; i++) {
1780
- const combo = active[activeFrom + i];
1781
- childReach[combo] = source[combo] * strategy[row + activeSlot[activeFrom + i]];
1782
- }
1836
+ for (let i = 0; i < activeCount; i++) childReach[i] = source[i] * strategy[row + i];
1783
1837
  const value = traverse(
1784
1838
  ctx,
1785
1839
  buffers,
@@ -1790,18 +1844,16 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1790
1844
  board,
1791
1845
  isTraverser ? childReach : reachMe,
1792
1846
  isTraverser ? reachOpp : childReach,
1793
- live,
1794
- liveCount,
1795
1847
  depth + 1
1796
1848
  );
1797
1849
  if (isTraverser) {
1798
- for (let i = 0; i < activeCount; i++) {
1799
- const combo = active[activeFrom + i];
1800
- actionValues[offset + combo] = value[combo];
1801
- out[combo] += strategy[row + activeSlot[activeFrom + i]] * value[combo];
1850
+ const offset = a * myCount;
1851
+ for (let i = 0; i < myCount; i++) {
1852
+ actionValues[offset + i] = value[i];
1853
+ out[i] += strategy[row + i] * value[i];
1802
1854
  }
1803
1855
  } else {
1804
- for (let i = 0; i < liveCount; i++) out[live[i]] += value[live[i]];
1856
+ for (let i = 0; i < myCount; i++) out[i] += value[i];
1805
1857
  }
1806
1858
  }
1807
1859
  if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.regretScale !== null) {
@@ -1811,10 +1863,6 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1811
1863
  slotBase,
1812
1864
  actions,
1813
1865
  stride,
1814
- active,
1815
- activeSlot,
1816
- activeFrom,
1817
- activeCount,
1818
1866
  iterWeight,
1819
1867
  reachMe,
1820
1868
  strategy,
@@ -1822,37 +1870,32 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1822
1870
  out
1823
1871
  );
1824
1872
  } else if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.dcfr) {
1825
- const previous = iterWeight - 1;
1826
- const grown = previous ** DCFR_ALPHA;
1827
- const shrunk = previous ** DCFR_BETA;
1828
- const keepPositive = grown / (grown + 1);
1829
- const keepNegative = shrunk / (shrunk + 1);
1830
- const keepSum = (previous / iterWeight) ** DCFR_GAMMA;
1873
+ const { keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight);
1831
1874
  const regret = ctx.regret;
1832
1875
  const strategySum = ctx.strategySum;
1833
1876
  for (let a = 0; a < actions; a++) {
1834
- const offset = a * COMBOS;
1877
+ const offset = a * activeCount;
1835
1878
  const slotRow = slotBase + a * stride;
1879
+ const row = a * stride;
1836
1880
  for (let i = 0; i < activeCount; i++) {
1837
- const combo = active[activeFrom + i];
1838
- const at = slotRow + activeSlot[activeFrom + i];
1881
+ const at = slotRow + i;
1839
1882
  const accumulated = regret[at];
1840
- regret[at] = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + combo] - out[combo];
1841
- strategySum[at] = strategySum[at] * keepSum + reachMe[combo] * strategy[a * stride + activeSlot[activeFrom + i]];
1883
+ regret[at] = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + i] - out[i];
1884
+ strategySum[at] = strategySum[at] * keepSum + reachMe[i] * strategy[row + i];
1842
1885
  }
1843
1886
  }
1844
1887
  } else if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
1845
1888
  const regret = ctx.regret;
1846
1889
  const strategySum = ctx.strategySum;
1847
1890
  for (let a = 0; a < actions; a++) {
1848
- const offset = a * COMBOS;
1891
+ const offset = a * activeCount;
1849
1892
  const slotRow = slotBase + a * stride;
1893
+ const row = a * stride;
1850
1894
  for (let i = 0; i < activeCount; i++) {
1851
- const combo = active[activeFrom + i];
1852
- const at = slotRow + activeSlot[activeFrom + i];
1853
- const updated = regret[at] + actionValues[offset + combo] - out[combo];
1895
+ const at = slotRow + i;
1896
+ const updated = regret[at] + actionValues[offset + i] - out[i];
1854
1897
  regret[at] = updated > 0 ? updated : 0;
1855
- strategySum[at] += iterWeight * reachMe[combo] * strategy[a * stride + activeSlot[activeFrom + i]];
1898
+ strategySum[at] += iterWeight * reachMe[i] * strategy[row + i];
1856
1899
  }
1857
1900
  }
1858
1901
  }
@@ -1867,7 +1910,7 @@ function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachM
1867
1910
  }
1868
1911
  return out;
1869
1912
  }
1870
- function updateQuantised(ctx, block, slotBase, actions, stride, active, activeSlot, activeFrom, activeCount, iterWeight, reachMe, strategy, actionValues, out) {
1913
+ function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reachMe, strategy, actionValues, out) {
1871
1914
  const regret = ctx.regret;
1872
1915
  const strategySum = ctx.strategySum;
1873
1916
  const regretScale = ctx.regretScale;
@@ -1880,44 +1923,37 @@ function updateQuantised(ctx, block, slotBase, actions, stride, active, activeSl
1880
1923
  let weight = iterWeight;
1881
1924
  const clamp = !ctx.dcfr;
1882
1925
  if (ctx.dcfr) {
1883
- const previous = iterWeight - 1;
1884
- const grown = previous ** DCFR_ALPHA;
1885
- const shrunk = previous ** DCFR_BETA;
1886
- keepPositive = grown / (grown + 1);
1887
- keepNegative = shrunk / (shrunk + 1);
1888
- keepSum = (previous / iterWeight) ** DCFR_GAMMA;
1926
+ ({ keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight));
1889
1927
  weight = 1;
1890
1928
  }
1891
1929
  let largestRegret = 0;
1892
1930
  let largestSum = 0;
1893
1931
  for (let a = 0; a < actions; a++) {
1894
- const offset = a * COMBOS;
1895
- const slotRow = slotBase + a * stride;
1896
- for (let i = 0; i < activeCount; i++) {
1897
- const combo = active[activeFrom + i];
1898
- const at = slotRow + activeSlot[activeFrom + i];
1932
+ const offset = a * count;
1933
+ const slotRow = slotBase + a * count;
1934
+ for (let i = 0; i < count; i++) {
1935
+ const at = slotRow + i;
1899
1936
  const accumulated = regret[at] * oldRegret;
1900
- let updated = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + combo] - out[combo];
1937
+ let updated = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + i] - out[i];
1901
1938
  if (clamp && updated < 0) updated = 0;
1902
1939
  const magnitude = updated < 0 ? -updated : updated;
1903
1940
  if (magnitude > largestRegret) largestRegret = magnitude;
1904
- actionValues[offset + combo] = updated;
1905
- const sum = strategySum[at] * oldSum * keepSum + weight * reachMe[combo] * strategy[a * stride + activeSlot[activeFrom + i]];
1941
+ actionValues[offset + i] = updated;
1942
+ const sum = strategySum[at] * oldSum * keepSum + weight * reachMe[i] * strategy[a * count + i];
1906
1943
  if (sum > largestSum) largestSum = sum;
1907
- strategy[a * stride + activeSlot[activeFrom + i]] = sum;
1944
+ strategy[a * count + i] = sum;
1908
1945
  }
1909
1946
  }
1910
1947
  const regretStep = largestRegret > 0 ? REGRET_STEPS / largestRegret : 0;
1911
1948
  const sumStep = largestSum > 0 ? SUM_STEPS / largestSum : 0;
1912
1949
  for (let a = 0; a < actions; a++) {
1913
- const offset = a * COMBOS;
1914
- const slotRow = slotBase + a * stride;
1915
- for (let i = 0; i < activeCount; i++) {
1916
- const combo = active[activeFrom + i];
1917
- const at = slotRow + activeSlot[activeFrom + i];
1918
- const r = actionValues[offset + combo] * regretStep;
1950
+ const offset = a * count;
1951
+ const slotRow = slotBase + a * count;
1952
+ for (let i = 0; i < count; i++) {
1953
+ const at = slotRow + i;
1954
+ const r = actionValues[offset + i] * regretStep;
1919
1955
  regret[at] = r < 0 ? r - 0.5 : r + 0.5;
1920
- const s = strategy[a * stride + activeSlot[activeFrom + i]] * sumStep + dither(at, iterWeight);
1956
+ const s = strategy[a * count + i] * sumStep + dither(at, iterWeight);
1921
1957
  strategySum[at] = s < SUM_STEPS ? s : SUM_STEPS;
1922
1958
  }
1923
1959
  }
@@ -1946,25 +1982,28 @@ function makeSolver(ctx) {
1946
1982
  rootLive[rootCount++] = combo;
1947
1983
  }
1948
1984
  }
1985
+ const lists = ctx.active;
1986
+ const listOf = (round, board, player) => {
1987
+ const slot = (lists.roundBase[round] + board) * 2 + player;
1988
+ return [lists.start[slot], lists.count[slot]];
1989
+ };
1990
+ const gather = (source, round, board, player, into) => {
1991
+ const [from, count] = listOf(round, board, player);
1992
+ for (let i = 0; i < count; i++) into[i] = source[lists.list[from + i]];
1993
+ };
1949
1994
  const run = (mode, traverser, iterWeight) => {
1950
- reach0.set(ctx.players[0].weights);
1951
- reach1.set(ctx.players[1].weights);
1952
- for (let i = 0; i < COMBOS; i++) {
1953
- if (ctx.players[0].slot[i] < 0) reach0[i] = 0;
1954
- if (ctx.players[1].slot[i] < 0) reach1[i] = 0;
1955
- }
1995
+ gather(ctx.players[0].weights, 0, 0, 0, reach0);
1996
+ gather(ctx.players[1].weights, 0, 0, 1, reach1);
1956
1997
  const me = traverser === 0 ? reach0 : reach1;
1957
1998
  const opp = traverser === 0 ? reach1 : reach0;
1958
- return traverse(ctx, buffers, mode, traverser, iterWeight, 0, 0, me, opp, rootLive, rootCount, 0);
1999
+ return traverse(ctx, buffers, mode, traverser, iterWeight, 0, 0, me, opp, 0);
1959
2000
  };
1960
2001
  let root = run;
1961
2002
  const total = (values, traverser) => {
1962
2003
  const weights = ctx.players[traverser].weights;
2004
+ const [from, count] = listOf(0, 0, traverser);
1963
2005
  let sum = 0;
1964
- for (let i = 0; i < rootCount; i++) {
1965
- const combo = rootLive[i];
1966
- sum += weights[combo] * values[combo];
1967
- }
2006
+ for (let i = 0; i < count; i++) sum += weights[lists.list[from + i]] * values[i];
1968
2007
  return sum / ctx.totalMass;
1969
2008
  };
1970
2009
  return {
@@ -1978,20 +2017,7 @@ function makeSolver(ctx) {
1978
2017
  root = next ?? run;
1979
2018
  },
1980
2019
  traverseChance(mode, traverser, iterWeight, node, me, opp) {
1981
- return traverse(
1982
- ctx,
1983
- buffers,
1984
- mode,
1985
- traverser,
1986
- iterWeight,
1987
- node,
1988
- 0,
1989
- me,
1990
- opp,
1991
- rootLive,
1992
- rootCount,
1993
- 0
1994
- );
2020
+ return traverse(ctx, buffers, mode, traverser, iterWeight, node, 0, me, opp, 0);
1995
2021
  },
1996
2022
  iterate(count, from) {
1997
2023
  for (let i = 0; i < count; i++) {
@@ -2012,21 +2038,19 @@ function makeSolver(ctx) {
2012
2038
  const expanded = ctx.expandOrbits;
2013
2039
  ctx.expandOrbits = true;
2014
2040
  try {
2015
- const value = traverse(
2016
- ctx,
2017
- buffers,
2018
- Mode.Average,
2019
- traverser,
2020
- 0,
2021
- node,
2022
- board,
2023
- reachMe,
2024
- reachOpp,
2025
- live,
2026
- liveCount,
2027
- 0
2028
- );
2029
- return Float64Array.from(value.subarray(0, COMBOS));
2041
+ const round = ctx.tree.round[node];
2042
+ const opponent = 1 - traverser;
2043
+ const me = new Float64Array(COMBOS);
2044
+ const opp = new Float64Array(COMBOS);
2045
+ gather(reachMe, round, board, traverser, me);
2046
+ gather(reachOpp, round, board, opponent, opp);
2047
+ const value = traverse(ctx, buffers, Mode.Average, traverser, 0, node, board, me, opp, 0);
2048
+ const result = new Float64Array(COMBOS);
2049
+ const [from, count] = listOf(round, board, traverser);
2050
+ for (let i = 0; i < count; i++) result[lists.list[from + i]] = value[i];
2051
+ void live;
2052
+ void liveCount;
2053
+ return result;
2030
2054
  } finally {
2031
2055
  ctx.expandOrbits = expanded;
2032
2056
  }
@@ -2038,30 +2062,278 @@ function makeSolver(ctx) {
2038
2062
  const owner = ctx.tree.player[node];
2039
2063
  const index = ctx.players[owner];
2040
2064
  const actions = ctx.tree.numEdges[node];
2041
- const base = ctx.layout.offset[node] + board * actions * index.count;
2042
- const probs = new Float64Array(index.count * actions);
2043
- const regret = ctx.regret;
2044
- for (let i = 0; i < index.count; i++) {
2045
- let sum = 0;
2046
- for (let a = 0; a < actions; a++) sum += ctx.strategySum[base + a * index.count + i];
2047
- let source = ctx.strategySum;
2048
- if (!(sum > 0) && regret.length > 0) {
2049
- for (let a = 0; a < actions; a++) {
2050
- const value = regret[base + a * index.count + i];
2051
- if (value > 0) sum += value;
2052
- }
2053
- source = regret;
2054
- }
2055
- for (let a = 0; a < actions; a++) {
2056
- const value = source[base + a * index.count + i];
2057
- probs[i * actions + a] = sum > 0 ? value > 0 ? value / sum : 0 : 1 / actions;
2058
- }
2065
+ const slot = (lists.roundBase[ctx.tree.round[node]] + board) * 2 + owner;
2066
+ const from = lists.start[slot];
2067
+ const count = lists.count[slot];
2068
+ const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[slot];
2069
+ const strategy = new Float64Array(actions * count);
2070
+ averageStrategy(ctx.strategySum, base, actions, count, strategy, ctx.regret);
2071
+ const probs = new Float64Array(index.count * actions).fill(1 / actions);
2072
+ for (let i = 0; i < count; i++) {
2073
+ const at = lists.slot[from + i];
2074
+ for (let a = 0; a < actions; a++) probs[at * actions + a] = strategy[a * count + i];
2059
2075
  }
2060
2076
  return { actions, probs };
2061
2077
  }
2062
2078
  };
2063
2079
  }
2064
2080
 
2081
+ // src/wasm/engine.ts
2082
+ var ENGINE_WASM = "AGFzbQEAAAABTglgAX8AYAZ/f39/f38Bf2AFf39/f38AYAd/f39/f3x/AGADf39/AGACf38AYAR/f39/AGAKf39/f399fX19fwF/YAl/f39/fX19fX8BfwIPAQNlbnYGbWVtb3J5AgAAAwsKAQIDBAUABgcIAAaOAjJ/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAAt8AUQAAAAAAADwPwt/AUF/C38BQQALfwFBAAt/AUEAC38BQQELfwFBAAt/AUEAC38BQQALfwFBAAt/AUEAC30BQwAAgD8LfQFDAAAAAAt9AUMAAIA/C30BQwAAgD8LfwFBAAt/AUEBCwdXCAhzZXRTaGFyZAADC3NldEV4Y2hhbmdlAAYIc2V0UGhhc2UABARpbml0AAkNc2V0TWF4QWN0aW9ucwAFA3J1bgAICXJ1bkNoYW5jZQAHBm1lbW9yeQIACqshCuIYBBV/An0FewN8Ix0gBSMebGohDSACIwkjASABai0AACIHQQJ0aigCAGpBAXQiCCAAaiIPQQJ0IgkjCmooAgAhCiMLIAlqKAIAIQtBASAAayIQIAhqIghBAnQiESMKaigCACESIwsgEWooAgAhESMAIAFqLQAAIhNBAkYEQCMUIAlqKAIAIQIjBiABQQJ0aigCALciIpogIiAAQQFGGyMaIAdBA3RqKwMAoiMhoyEkQQAhAANAIAAgEUgEQCAjIAQgAEECdGoqAgC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QQN0aiIMIAwrAwAgDqA5AwAjICMNIApqLQAAQQN0aiIKIAorAwAgDqA5AwAgB0EBaiEHDAELCyAJIAhBA3RqIA0jICADIAhqIgojDGotAABBA3RqKwMAIyAjDSAKai0AAEEDdGorAwCgoTkDACAIQQFqIQgMAQsLIyBBAEGgA/wLAEQAAAAAAAAAACENIAJBAWshByAEQQFrIQIDQCACQQBOBEAjDiACIANqQQJ0aigCACEEA0AgB0EATgR/Iw4gASAHakECdGooAgAgBEoFQQALBEAgDSAAIAdBAnRqKgIAuyIOoCENIyAgASAHaiIIIwxqLQAAQQN0aiIKIAorAwAgDqA5AwAjICMNIAhqLQAAQQN0aiIIIAgrAwAgDqA5AwAgB0EBayEHDAELCyAGIAJBAnRqIAUgCSACQQN0aisDACANIyAgAiADaiIEIwxqLQAAQQN0aisDACMgIw0gBGotAABBA3RqKwMAoKGhorY4AgAgAkEBayECDAELCyMgQQBBoAP8CwALDgAgACQiIAEkIyACJCQLDQAgACQlIAFBAEckJgsGACAAJDELFQAgACQnIAEkKCACJCkgA0ECdCQqCysAIAAkKyAFJCwgBiQtIAckLiAIJC8gCUEARyQwIAEgAkEAIAMgBEEAEAALKwAgACQrIAQkLCAFJC0gBiQuIAckLyAIQQBHJDAgAUEAQQAgAiADQQAQAAv2AQAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBuCQhCw==";
2083
+
2084
+ // src/wasm/engine-shared.ts
2085
+ var ENGINE_SHARED_WASM = "AG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QQFrIQIDQCACQQBOBEAjDiACIANqQQJ0aigCACEEA0AgB0EATgR/Iw4gASAHakECdGooAgAgBEoFQQALBEAgDSAAIAdBAnRqKgIAuyIOoCENIyAgASAHaiIIIwxqLQAAQQN0aiIKIAorAwAgDqA5AwAjICMNIAhqLQAAQQN0aiIIIAgrAwAgDqA5AwAgB0EBayEHDAELCyAGIAJBAnRqIAUgCSACQQN0aisDACANIyAgAiADaiIEIwxqLQAAQQN0aisDACMgIw0gBGotAABBA3RqKwMAoKGhorY4AgAgAkEBayECDAELCyMgQQBBoAP8CwALDgAgACQiIAEkIyACJCQLDQAgACQlIAFBAEckJgsGACAAJDELFQAgACQnIAEkKCACJCkgA0ECdCQqCysAIAAkKyAFJCwgBiQtIAckLiAIJC8gCUEARyQwIAEgAkEAIAMgBEEAEAALKwAgACQrIAQkLCAFJC0gBiQuIAckLyAIQQBHJDAgAUEAQQAgAiADQQAQAAv2AQAgACgCACQAIAAoAgQkASAAKAIIJAIgACgCDCQDIAAoAhAkBCAAKAIUJAUgACgCGCQGIAAoAhwkByAAKAIgJAggACgCJCQJIAAoAigkCiAAKAIsJAsgACgCMCQMIAAoAjQkDSAAKAI4JA4gACgCPCQPIABBQGsoAgAkECAAKAJEJBEgACgCSCQSIAAoAkwkEyAAKAJQJBQgACgCVCQVIAAoAlgkFiAAKAJcJBcgACgCYCQYIAAoAmQkGSAAKAJoJBogACgCbCQbIAAoAnAkHCAAKAJ0JB0gACgCeCQeIAAoAnwkHyAAKAKAASQgIAAoAoQBuCQhCw==";
2086
+
2087
+ // src/engine.ts
2088
+ var wasm2 = globalThis.WebAssembly;
2089
+ var modules = { plain: void 0, shared: void 0 };
2090
+ function compiledEngine(kind) {
2091
+ if (modules[kind] !== void 0) return modules[kind];
2092
+ modules[kind] = null;
2093
+ try {
2094
+ if (!wasm2) return null;
2095
+ const text = kind === "plain" ? ENGINE_WASM : ENGINE_SHARED_WASM;
2096
+ const bytes = Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
2097
+ if (!wasm2.validate(bytes)) return null;
2098
+ modules[kind] = new wasm2.Module(bytes);
2099
+ } catch {
2100
+ modules[kind] = null;
2101
+ }
2102
+ return modules[kind];
2103
+ }
2104
+ var ENGINE_CONTROL = { generation: 0, done: 1, command: 2, mode: 3, traverser: 4, ready: 5, size: 8 };
2105
+ function createEngine(input, pool) {
2106
+ const shared = pool !== void 0;
2107
+ const compiledModule = compiledEngine(shared ? "shared" : "plain");
2108
+ if (!compiledModule || !wasm2) return null;
2109
+ const { tree, runouts, layout, active, hands } = input;
2110
+ const threads = pool ? pool.threads : 1;
2111
+ const flatten = (parts) => {
2112
+ const bases = new Int32Array(parts.length + 1);
2113
+ for (let i = 0; i < parts.length; i++) bases[i + 1] = bases[i] + parts[i].length;
2114
+ const flat = new Int32Array(bases[parts.length]);
2115
+ parts.forEach((part, i) => flat.set(part, bases[i]));
2116
+ return [bases, flat];
2117
+ };
2118
+ const [upRound, upStart] = flatten(hands.upStart);
2119
+ const [childRound, childStart] = flatten(runouts.childStart);
2120
+ const [orbitRound, orbitStart] = flatten(runouts.orbitStart);
2121
+ const maxCount = Math.max(4, ...active.count);
2122
+ const stride = Math.ceil(maxCount / 4) * 4;
2123
+ const maxActions = Math.max(1, layout.maxActions);
2124
+ const depths = treeDepth2(tree) + 2;
2125
+ const rowBytes = stride * 4;
2126
+ const depthBytes = rowBytes * (3 + 2 * maxActions);
2127
+ const pieces = [];
2128
+ const add = (data, bytes2 = data ? data.byteLength : 0) => {
2129
+ pieces.push({ data, bytes: Math.ceil(Math.max(bytes2, 4) / 16) * 16 });
2130
+ return pieces.length - 1;
2131
+ };
2132
+ const common = [
2133
+ add(tree.kind),
2134
+ add(tree.round),
2135
+ add(Uint8Array.from(tree.player, (p) => p < 0 ? 255 : p)),
2136
+ add(tree.firstEdge),
2137
+ add(tree.numEdges),
2138
+ add(tree.edgeChild),
2139
+ add(tree.payoff),
2140
+ add(layout.offset),
2141
+ add(layout.rowStart),
2142
+ add(active.roundBase),
2143
+ add(active.start),
2144
+ add(Int32Array.from(active.count)),
2145
+ add(active.cardA),
2146
+ add(active.cardB),
2147
+ add(active.strength),
2148
+ add(active.tieStart),
2149
+ add(active.ties),
2150
+ add(upRound),
2151
+ add(upStart),
2152
+ add(hands.up),
2153
+ add(hands.sameStart),
2154
+ add(hands.same),
2155
+ add(childRound),
2156
+ add(childStart),
2157
+ add(orbitRound),
2158
+ add(orbitStart),
2159
+ add(input.runoutWeight),
2160
+ add(null, layout.totalSlots * 4),
2161
+ // regret
2162
+ add(null, layout.totalSlots * 4)
2163
+ // strategy sums
2164
+ ];
2165
+ const REGRET = common[27];
2166
+ const SUMS = common[28];
2167
+ const own = Array.from({ length: threads }, () => ({
2168
+ buffers: add(null, depths * depthBytes),
2169
+ cardSums: add(null, 52 * 8 + stride * 8),
2170
+ rootMe: add(null, rowBytes),
2171
+ rootOpp: add(null, rowBytes),
2172
+ header: add(null, 40 * 4)
2173
+ }));
2174
+ const entries = pool ? pool.plan.entryNodes.length : 0;
2175
+ const entryIndexOf = pool ? add(pool.plan.entryIndexOf) : -1;
2176
+ const reach = pool ? add(null, entries * 2 * rowBytes) : -1;
2177
+ const values = pool ? add(null, entries * rowBytes) : -1;
2178
+ const partials = pool ? add(null, (threads - 1) * entries * rowBytes) : -1;
2179
+ const offsets = [];
2180
+ let total = 64;
2181
+ for (const piece of pieces) {
2182
+ offsets.push(total);
2183
+ total += piece.bytes;
2184
+ }
2185
+ if (total > 4294901760) return null;
2186
+ const pages = Math.ceil(total / 65536) + 1;
2187
+ const memory = shared ? new wasm2.Memory({ initial: pages, maximum: pages, shared: true }) : new wasm2.Memory({ initial: pages });
2188
+ const bytes = new Uint8Array(memory.buffer);
2189
+ pieces.forEach((piece, i) => {
2190
+ if (piece.data) bytes.set(new Uint8Array(piece.data.buffer, piece.data.byteOffset, piece.data.byteLength), offsets[i]);
2191
+ });
2192
+ for (const mine of own) {
2193
+ const head = new Uint32Array(memory.buffer, offsets[mine.header], 40);
2194
+ common.forEach((slot, i) => head[i] = offsets[slot]);
2195
+ head[29] = offsets[mine.buffers];
2196
+ head[30] = depthBytes;
2197
+ head[31] = rowBytes;
2198
+ head[32] = offsets[mine.cardSums];
2199
+ head[33] = input.chipScale;
2200
+ }
2201
+ const instance = new wasm2.Instance(compiledModule, { env: { memory, abort: () => {
2202
+ throw new Error("engine abort");
2203
+ } } });
2204
+ const exports = instance.exports;
2205
+ exports.init(offsets[own[0].header]);
2206
+ exports.setMaxActions(maxActions);
2207
+ const regret = new Float32Array(memory.buffer, offsets[REGRET], layout.totalSlots);
2208
+ const strategySum = new Float32Array(memory.buffer, offsets[SUMS], layout.totalSlots);
2209
+ const me = new Float32Array(memory.buffer, offsets[own[0].rootMe], stride);
2210
+ const opp = new Float32Array(memory.buffer, offsets[own[0].rootOpp], stride);
2211
+ const result = new Float64Array(COMBOS);
2212
+ const rootList = (player) => {
2213
+ const slot = active.roundBase[0] * 2 + player;
2214
+ return [active.start[slot], active.count[slot]];
2215
+ };
2216
+ const settingsFor = (mode, iterWeight, dcfr) => {
2217
+ if (mode === Mode.Train && dcfr) {
2218
+ const { keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight);
2219
+ return [keepPositive, keepNegative, keepSum, 1, 0];
2220
+ }
2221
+ return [1, 1, 1, iterWeight, dcfr ? 0 : 1];
2222
+ };
2223
+ let control = null;
2224
+ let settings = null;
2225
+ const workers = [];
2226
+ let failure = null;
2227
+ if (pool) {
2228
+ const plan = pool.plan;
2229
+ const controlBuffer = new SharedArrayBuffer(ENGINE_CONTROL.size * 4);
2230
+ const settingsBuffer = new SharedArrayBuffer(8 * 8);
2231
+ control = new Int32Array(controlBuffer);
2232
+ settings = new Float64Array(settingsBuffer);
2233
+ exports.setExchange(offsets[entryIndexOf], offsets[reach], offsets[values], stride);
2234
+ for (let w = 1; w < threads; w++) {
2235
+ const setup = {
2236
+ memory,
2237
+ module: compiledModule,
2238
+ header: offsets[own[w].header],
2239
+ maxActions,
2240
+ exchange: [offsets[entryIndexOf], offsets[reach], offsets[values], stride],
2241
+ shard: [plan.round, plan.bounds[w], plan.bounds[w + 1]],
2242
+ entryNodes: plan.entryNodes,
2243
+ rootMe: offsets[own[w].rootMe],
2244
+ rootOpp: offsets[own[w].rootOpp],
2245
+ partial: offsets[partials] + (w - 1) * entries * rowBytes,
2246
+ stride,
2247
+ control: controlBuffer,
2248
+ settings: settingsBuffer
2249
+ };
2250
+ workers.push(pool.spawn(setup, (error) => {
2251
+ failure ??= error;
2252
+ Atomics.store(control, ENGINE_CONTROL.ready, threads);
2253
+ Atomics.add(control, ENGINE_CONTROL.done, threads);
2254
+ Atomics.notify(control, ENGINE_CONTROL.done);
2255
+ Atomics.notify(control, ENGINE_CONTROL.ready);
2256
+ }));
2257
+ }
2258
+ for (; ; ) {
2259
+ const ready = Atomics.load(control, ENGINE_CONTROL.ready);
2260
+ if (ready >= threads - 1 || failure) break;
2261
+ Atomics.wait(control, ENGINE_CONTROL.ready, ready, 20);
2262
+ }
2263
+ if (failure) {
2264
+ for (const worker of workers) worker.terminate();
2265
+ throw failure;
2266
+ }
2267
+ }
2268
+ const run = (mode, traverser, iterWeight, dcfr) => {
2269
+ for (const player of [0, 1]) {
2270
+ const [from, count2] = rootList(player);
2271
+ const into = player === traverser ? me : opp;
2272
+ const weights = input.players[player].weights;
2273
+ for (let i = 0; i < count2; i++) into[i] = weights[active.list[from + i]];
2274
+ }
2275
+ const [positive, negative, sum, weight, clamp] = settingsFor(mode, iterWeight, dcfr);
2276
+ let at;
2277
+ if (!pool) {
2278
+ at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
2279
+ } else {
2280
+ const plan = pool.plan;
2281
+ exports.setShard(plan.round, 0, 0);
2282
+ exports.setPhase(1, 0);
2283
+ exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
2284
+ settings.set([positive, negative, sum, weight, clamp]);
2285
+ Atomics.store(control, ENGINE_CONTROL.done, 0);
2286
+ Atomics.store(control, ENGINE_CONTROL.mode, mode);
2287
+ Atomics.store(control, ENGINE_CONTROL.traverser, traverser);
2288
+ Atomics.store(control, ENGINE_CONTROL.command, 0);
2289
+ Atomics.add(control, ENGINE_CONTROL.generation, 1);
2290
+ Atomics.notify(control, ENGINE_CONTROL.generation);
2291
+ exports.setShard(plan.round, plan.bounds[0], plan.bounds[1]);
2292
+ exports.setPhase(0, 1);
2293
+ const valueView = new Float32Array(memory.buffer, offsets[values], entries * stride);
2294
+ for (let entry = 0; entry < entries; entry++) {
2295
+ const slot = offsets[reach] + entry * 2 * rowBytes;
2296
+ const out = exports.runChance(mode, traverser, plan.entryNodes[entry], slot, slot + rowBytes, positive, negative, sum, weight, clamp);
2297
+ valueView.set(new Float32Array(memory.buffer, out, stride), entry * stride);
2298
+ }
2299
+ for (; ; ) {
2300
+ const done = Atomics.load(control, ENGINE_CONTROL.done);
2301
+ if (done >= threads - 1 || failure) break;
2302
+ Atomics.wait(control, ENGINE_CONTROL.done, done, 50);
2303
+ }
2304
+ if (failure) throw failure;
2305
+ const partialView = new Float32Array(memory.buffer, offsets[partials], (threads - 1) * entries * stride);
2306
+ for (let w = 0; w < threads - 1; w++) {
2307
+ for (let i = 0; i < entries * stride; i++) valueView[i] += partialView[w * entries * stride + i];
2308
+ }
2309
+ exports.setShard(plan.round, 0, 0);
2310
+ exports.setPhase(2, 1);
2311
+ at = exports.run(mode, traverser, offsets[own[0].rootMe], offsets[own[0].rootOpp], positive, negative, sum, weight, clamp);
2312
+ exports.setPhase(0, 1);
2313
+ exports.setShard(-1, 0, 0);
2314
+ }
2315
+ const [, count] = rootList(traverser);
2316
+ const values32 = new Float32Array(memory.buffer, at, count);
2317
+ for (let i = 0; i < count; i++) result[i] = values32[i];
2318
+ return result;
2319
+ };
2320
+ return {
2321
+ regret,
2322
+ strategySum,
2323
+ threads,
2324
+ root(dcfr) {
2325
+ return (mode, traverser, iterWeight) => run(mode, traverser, iterWeight, dcfr);
2326
+ },
2327
+ close() {
2328
+ if (!control) return;
2329
+ Atomics.store(control, ENGINE_CONTROL.command, 1);
2330
+ Atomics.add(control, ENGINE_CONTROL.generation, 1);
2331
+ Atomics.notify(control, ENGINE_CONTROL.generation);
2332
+ for (const worker of workers) worker.terminate();
2333
+ }
2334
+ };
2335
+ }
2336
+
2065
2337
  // src/warm.ts
2066
2338
  var SIZE_TOLERANCE = 1.35;
2067
2339
  var REGRET_STEPS2 = 32767;
@@ -2139,6 +2411,7 @@ function warmStart(to, from, averages) {
2139
2411
  const newRegret = new Float64Array(width);
2140
2412
  const newSum = new Float64Array(width);
2141
2413
  const hasRegret = to.regret.length > 0 && from.regret.length > 0;
2414
+ const position = new Int32Array(COMBOS).fill(-1);
2142
2415
  let matched = 0;
2143
2416
  for (const node of tree.decisionNodes) {
2144
2417
  const old = match[node];
@@ -2148,8 +2421,6 @@ function warmStart(to, from, averages) {
2148
2421
  const owner = tree.player[node];
2149
2422
  const actions = tree.numEdges[node];
2150
2423
  const oldActions = from.tree.numEdges[old];
2151
- const index = to.players[owner];
2152
- const oldIndex = from.players[owner];
2153
2424
  const actionOf = new Int32Array(actions).fill(-1);
2154
2425
  for (let e = 0; e < actions; e++) {
2155
2426
  const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old);
@@ -2157,26 +2428,34 @@ function warmStart(to, from, averages) {
2157
2428
  }
2158
2429
  for (let board = 0; board < to.runouts.counts[round]; board++) {
2159
2430
  const resolved = resolveBoard(from.runouts, realCards(to.runouts, round, board));
2431
+ const oldSlot = (from.active.roundBase[round] + resolved.board) * 2 + owner;
2432
+ const oldFrom = from.active.start[oldSlot];
2433
+ const oldCount = from.active.count[oldSlot];
2160
2434
  const oldBlock = from.layout.blockOffset[old] + resolved.board;
2161
- const oldBase = from.layout.offset[old] + resolved.board * oldActions * oldIndex.count;
2162
- const oldLength = oldActions * oldIndex.count;
2435
+ const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
2436
+ const oldLength = oldActions * oldCount;
2163
2437
  if (hasRegret) readBlock(from.regret, from.regretScale, oldBlock, oldBase, oldLength, oldRegret);
2164
2438
  readBlock(from.strategySum, from.sumScale, oldBlock, oldBase, oldLength, oldSum);
2165
- const length = actions * index.count;
2439
+ for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = j;
2440
+ const newSlot = (to.active.roundBase[round] + board) * 2 + owner;
2441
+ const newFrom = to.active.start[newSlot];
2442
+ const count = to.active.count[newSlot];
2443
+ const length = actions * count;
2166
2444
  newRegret.fill(0, 0, length);
2167
2445
  newSum.fill(0, 0, length);
2168
- for (let i = 0; i < index.count; i++) {
2169
- const slot = oldIndex.slot[resolved.combos[index.combos[i]]];
2170
- if (slot < 0) continue;
2446
+ for (let i = 0; i < count; i++) {
2447
+ const j = position[resolved.combos[to.active.list[newFrom + i]]];
2448
+ if (j < 0) continue;
2171
2449
  for (let e = 0; e < actions; e++) {
2172
2450
  const action = actionOf[e];
2173
2451
  if (action < 0) continue;
2174
- newRegret[e * index.count + i] = oldRegret[action * oldIndex.count + slot];
2175
- newSum[e * index.count + i] = oldSum[action * oldIndex.count + slot];
2452
+ newRegret[e * count + i] = oldRegret[action * oldCount + j];
2453
+ newSum[e * count + i] = oldSum[action * oldCount + j];
2176
2454
  }
2177
2455
  }
2456
+ for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = -1;
2178
2457
  const block = to.layout.blockOffset[node] + board;
2179
- const base = to.layout.offset[node] + board * length;
2458
+ const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
2180
2459
  if (hasRegret) writeBlock(to.regret, to.regretScale, REGRET_STEPS2, block, base, length, newRegret);
2181
2460
  if (averages) writeBlock(to.strategySum, to.sumScale, SUM_STEPS2, block, base, length, newSum);
2182
2461
  tieBlock(to, node, board);
@@ -2195,6 +2474,9 @@ function defaultWorkerCount() {
2195
2474
  function spawnWorker(_setup, _onError) {
2196
2475
  throw new Error("Worker threads are only available in the Node build; use workers: 1 in the browser");
2197
2476
  }
2477
+ function spawnEngineWorker(_setup, _onError) {
2478
+ throw new Error("Worker threads are only available in the Node build; use workers: 1 in the browser");
2479
+ }
2198
2480
 
2199
2481
  // src/parallel.ts
2200
2482
  var CONTROL = {
@@ -2499,7 +2781,7 @@ var PRECISION_BYTES = { f64: 8, f32: 4, i16: 2 };
2499
2781
  function createRun(spot, options = {}) {
2500
2782
  const maxBytes = spot.source.maxBytes ?? DEFAULT_MAX_BYTES;
2501
2783
  const { totalSlots, bytes } = spot.estimate;
2502
- const precision = options.analysisOnly ? "f32" : options.precision ?? (totalSlots < 4e6 && bytes * 2 <= maxBytes ? "f64" : bytes <= maxBytes ? "f32" : "i16");
2784
+ const precision = options.analysisOnly ? "f32" : options.precision ?? (bytes <= maxBytes ? "f32" : "i16");
2503
2785
  const relevant = new Uint8Array(COMBOS);
2504
2786
  for (const combo of spot.players[0].combos) relevant[combo] = 1;
2505
2787
  for (const combo of spot.players[1].combos) relevant[combo] = 1;
@@ -2511,19 +2793,35 @@ function createRun(spot, options = {}) {
2511
2793
  const allocate = (bytes2) => plan ? new SharedArrayBuffer(bytes2) : new ArrayBuffer(bytes2);
2512
2794
  const width = PRECISION_BYTES[precision];
2513
2795
  const quantised = precision === "i16";
2796
+ const active = buildActiveLists(spot.runouts, spot.players, showdown);
2797
+ const hands = buildHandSpace(spot.runouts, spot.players, active);
2798
+ const engine = options.engine !== "js" && precision === "f32" && !options.analysisOnly && options.simd !== false ? createEngine(
2799
+ {
2800
+ tree: spot.tree,
2801
+ runouts: spot.runouts,
2802
+ players: spot.players,
2803
+ layout,
2804
+ active,
2805
+ hands,
2806
+ runoutWeight: weights,
2807
+ chipScale: spot.chipScale
2808
+ },
2809
+ plan ? { plan, threads: requested, spawn: spawnEngineWorker } : void 0
2810
+ ) : null;
2514
2811
  const ctx = {
2515
2812
  tree: spot.tree,
2516
2813
  runouts: spot.runouts,
2517
2814
  players: spot.players,
2518
2815
  layout,
2519
2816
  showdown,
2520
- regret: options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
2521
- strategySum: makeSumStore(precision, allocate(layout.totalSlots * width)),
2817
+ regret: engine ? engine.regret : options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
2818
+ strategySum: engine ? engine.strategySum : makeSumStore(precision, allocate(layout.totalSlots * width)),
2522
2819
  regretScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
2523
2820
  sumScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
2524
2821
  scratch: makeTerminalScratch(),
2525
2822
  chipScale: spot.chipScale,
2526
- active: buildActiveLists(spot.runouts, spot.players),
2823
+ active,
2824
+ hands,
2527
2825
  runoutWeight: weights,
2528
2826
  shardStart: 0,
2529
2827
  shardEnd: plan ? plan.boards : 0,
@@ -2549,17 +2847,20 @@ function createRun(spot, options = {}) {
2549
2847
  offset = options.warmIteration ?? source.totalIterations;
2550
2848
  }
2551
2849
  const solver = makeSolver(ctx);
2552
- const pool = plan ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
2850
+ const pool = plan && !engine ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
2553
2851
  if (pool) solver.useRoot(pool.traverse);
2852
+ if (engine) solver.useRoot(engine.root(ctx.dcfr));
2853
+ const threadCount = pool ? pool.workers : engine ? engine.threads : 1;
2554
2854
  let completed = 0;
2555
2855
  return {
2556
2856
  spot,
2557
2857
  solver,
2558
- workers: pool ? pool.workers : 1,
2858
+ workers: threadCount,
2559
2859
  get completed() {
2560
2860
  return completed;
2561
2861
  },
2562
2862
  warmStart: warm,
2863
+ engine: engine ? "wasm" : "js",
2563
2864
  get totalIterations() {
2564
2865
  return offset + completed;
2565
2866
  },
@@ -2571,6 +2872,7 @@ function createRun(spot, options = {}) {
2571
2872
  close() {
2572
2873
  solver.useRoot(null);
2573
2874
  pool?.close();
2875
+ engine?.close();
2574
2876
  },
2575
2877
  exploitabilityPercent() {
2576
2878
  return 100 * solver.exploitability() / (spot.potUnits / spot.chipScale);
@@ -2578,7 +2880,7 @@ function createRun(spot, options = {}) {
2578
2880
  trueExploitabilityPercent() {
2579
2881
  if (!spot.runouts.approximate) return this.exploitabilityPercent();
2580
2882
  const real = createRun(defineSpot({ ...spot.source, runoutBuckets: false }), {
2581
- workers: pool ? pool.workers : 1,
2883
+ workers: threadCount,
2582
2884
  precision
2583
2885
  });
2584
2886
  try {
@@ -2589,7 +2891,7 @@ function createRun(spot, options = {}) {
2589
2891
  }
2590
2892
  },
2591
2893
  snapshot(history = [], stoppedAt = "iterations") {
2592
- return makeSolution(spot, ctx, solver, completed, offset + completed, [...history], stoppedAt, pool ? pool.workers : 1);
2894
+ return makeSolution(spot, ctx, solver, completed, offset + completed, [...history], stoppedAt, threadCount);
2593
2895
  }
2594
2896
  };
2595
2897
  }