poker-flopsolve 0.1.0 → 0.2.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/README.md +27 -6
- package/dist/browser.js +380 -355
- package/dist/browser.js.map +1 -1
- package/dist/index.d.ts +62 -1
- package/dist/index.js +380 -355
- package/dist/index.js.map +1 -1
- package/dist/worker.js +362 -343
- package/dist/worker.js.map +1 -1
- package/package.json +3 -2
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
|
|
655
|
-
if (
|
|
656
|
-
const [, a1, a2, k1, b1, b2, k2] =
|
|
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
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
const
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
const
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
const
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
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
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
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
|
-
|
|
1027
|
-
|
|
1008
|
+
sameStart[slots] = same.length;
|
|
1009
|
+
return { upStart, up: Uint16Array.from(up), sameStart, same: Int32Array.from(same) };
|
|
1028
1010
|
}
|
|
1029
|
-
function
|
|
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
|
|
1036
|
-
const
|
|
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[
|
|
1041
|
-
sums[
|
|
1042
|
-
|
|
1016
|
+
sums[cardA[oppFrom + j]] += value;
|
|
1017
|
+
sums[cardB[oppFrom + j]] += value;
|
|
1018
|
+
}
|
|
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));
|
|
1023
|
+
}
|
|
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]]);
|
|
1043
1039
|
}
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
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]])));
|
|
1049
1053
|
}
|
|
1050
|
-
sums.fill(0, 0,
|
|
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,
|
|
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 +
|
|
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 +
|
|
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 +=
|
|
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 {
|
|
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
|
-
|
|
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
|
|
1453
|
-
|
|
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
|
-
|
|
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 =
|
|
1460
|
-
if (image
|
|
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
|
|
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
|
|
1504
|
-
const base = ctx.layout.offset[node] +
|
|
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,
|
|
1507
|
-
tieMirrors(ctx.strategySum, base, actions,
|
|
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 =
|
|
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,
|
|
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
|
|
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 *
|
|
1639
|
+
const value = source[at + a * count];
|
|
1552
1640
|
const positive = value > 0 ? value : 0;
|
|
1553
|
-
strategy[a *
|
|
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 *
|
|
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 *
|
|
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,
|
|
1564
|
-
normalise(regret, base, actions,
|
|
1651
|
+
function regretMatch(regret, base, actions, count, strategy) {
|
|
1652
|
+
normalise(regret, base, actions, count, strategy);
|
|
1565
1653
|
}
|
|
1566
|
-
function averageStrategy(strategySum, base, actions,
|
|
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
|
|
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 *
|
|
1575
|
-
strategy[a *
|
|
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 *
|
|
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 *
|
|
1671
|
+
const value = fallback[at + a * count];
|
|
1585
1672
|
const positive = value > 0 ? value : 0;
|
|
1586
|
-
strategy[a *
|
|
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 *
|
|
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 *
|
|
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,
|
|
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[
|
|
1604
|
-
|
|
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[
|
|
1609
|
-
|
|
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 <
|
|
1614
|
-
const nextRound =
|
|
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 <
|
|
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
|
|
1634
|
-
const
|
|
1635
|
-
const
|
|
1636
|
-
const
|
|
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
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
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
|
-
|
|
1668
|
-
|
|
1669
|
-
const
|
|
1670
|
-
if (
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
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
|
|
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 +
|
|
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,
|
|
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,
|
|
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 (
|
|
1762
|
-
for (let i = 0; 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 <
|
|
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
|
-
|
|
1799
|
-
|
|
1800
|
-
actionValues[offset +
|
|
1801
|
-
out[
|
|
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 <
|
|
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
|
|
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 *
|
|
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
|
|
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 +
|
|
1841
|
-
strategySum[at] = strategySum[at] * keepSum + reachMe[
|
|
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 *
|
|
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
|
|
1852
|
-
const
|
|
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[
|
|
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,
|
|
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
|
-
|
|
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 *
|
|
1895
|
-
const slotRow = slotBase + a *
|
|
1896
|
-
for (let i = 0; i <
|
|
1897
|
-
const
|
|
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 +
|
|
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 +
|
|
1905
|
-
const sum = strategySum[at] * oldSum * keepSum + weight * reachMe[
|
|
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 *
|
|
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 *
|
|
1914
|
-
const slotRow = slotBase + a *
|
|
1915
|
-
for (let i = 0; i <
|
|
1916
|
-
const
|
|
1917
|
-
const
|
|
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 *
|
|
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
|
-
|
|
1951
|
-
|
|
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,
|
|
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 <
|
|
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
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
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,24 +2062,16 @@ 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
|
|
2042
|
-
const
|
|
2043
|
-
const
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
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
|
}
|
|
@@ -2139,6 +2155,7 @@ function warmStart(to, from, averages) {
|
|
|
2139
2155
|
const newRegret = new Float64Array(width);
|
|
2140
2156
|
const newSum = new Float64Array(width);
|
|
2141
2157
|
const hasRegret = to.regret.length > 0 && from.regret.length > 0;
|
|
2158
|
+
const position = new Int32Array(COMBOS).fill(-1);
|
|
2142
2159
|
let matched = 0;
|
|
2143
2160
|
for (const node of tree.decisionNodes) {
|
|
2144
2161
|
const old = match[node];
|
|
@@ -2148,8 +2165,6 @@ function warmStart(to, from, averages) {
|
|
|
2148
2165
|
const owner = tree.player[node];
|
|
2149
2166
|
const actions = tree.numEdges[node];
|
|
2150
2167
|
const oldActions = from.tree.numEdges[old];
|
|
2151
|
-
const index = to.players[owner];
|
|
2152
|
-
const oldIndex = from.players[owner];
|
|
2153
2168
|
const actionOf = new Int32Array(actions).fill(-1);
|
|
2154
2169
|
for (let e = 0; e < actions; e++) {
|
|
2155
2170
|
const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old);
|
|
@@ -2157,26 +2172,34 @@ function warmStart(to, from, averages) {
|
|
|
2157
2172
|
}
|
|
2158
2173
|
for (let board = 0; board < to.runouts.counts[round]; board++) {
|
|
2159
2174
|
const resolved = resolveBoard(from.runouts, realCards(to.runouts, round, board));
|
|
2175
|
+
const oldSlot = (from.active.roundBase[round] + resolved.board) * 2 + owner;
|
|
2176
|
+
const oldFrom = from.active.start[oldSlot];
|
|
2177
|
+
const oldCount = from.active.count[oldSlot];
|
|
2160
2178
|
const oldBlock = from.layout.blockOffset[old] + resolved.board;
|
|
2161
|
-
const oldBase = from.layout.offset[old] +
|
|
2162
|
-
const oldLength = oldActions *
|
|
2179
|
+
const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
|
|
2180
|
+
const oldLength = oldActions * oldCount;
|
|
2163
2181
|
if (hasRegret) readBlock(from.regret, from.regretScale, oldBlock, oldBase, oldLength, oldRegret);
|
|
2164
2182
|
readBlock(from.strategySum, from.sumScale, oldBlock, oldBase, oldLength, oldSum);
|
|
2165
|
-
|
|
2183
|
+
for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = j;
|
|
2184
|
+
const newSlot = (to.active.roundBase[round] + board) * 2 + owner;
|
|
2185
|
+
const newFrom = to.active.start[newSlot];
|
|
2186
|
+
const count = to.active.count[newSlot];
|
|
2187
|
+
const length = actions * count;
|
|
2166
2188
|
newRegret.fill(0, 0, length);
|
|
2167
2189
|
newSum.fill(0, 0, length);
|
|
2168
|
-
for (let i = 0; i <
|
|
2169
|
-
const
|
|
2170
|
-
if (
|
|
2190
|
+
for (let i = 0; i < count; i++) {
|
|
2191
|
+
const j = position[resolved.combos[to.active.list[newFrom + i]]];
|
|
2192
|
+
if (j < 0) continue;
|
|
2171
2193
|
for (let e = 0; e < actions; e++) {
|
|
2172
2194
|
const action = actionOf[e];
|
|
2173
2195
|
if (action < 0) continue;
|
|
2174
|
-
newRegret[e *
|
|
2175
|
-
newSum[e *
|
|
2196
|
+
newRegret[e * count + i] = oldRegret[action * oldCount + j];
|
|
2197
|
+
newSum[e * count + i] = oldSum[action * oldCount + j];
|
|
2176
2198
|
}
|
|
2177
2199
|
}
|
|
2200
|
+
for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = -1;
|
|
2178
2201
|
const block = to.layout.blockOffset[node] + board;
|
|
2179
|
-
const base = to.layout.offset[node] +
|
|
2202
|
+
const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
|
|
2180
2203
|
if (hasRegret) writeBlock(to.regret, to.regretScale, REGRET_STEPS2, block, base, length, newRegret);
|
|
2181
2204
|
if (averages) writeBlock(to.strategySum, to.sumScale, SUM_STEPS2, block, base, length, newSum);
|
|
2182
2205
|
tieBlock(to, node, board);
|
|
@@ -2511,6 +2534,7 @@ function createRun(spot, options = {}) {
|
|
|
2511
2534
|
const allocate = (bytes2) => plan ? new SharedArrayBuffer(bytes2) : new ArrayBuffer(bytes2);
|
|
2512
2535
|
const width = PRECISION_BYTES[precision];
|
|
2513
2536
|
const quantised = precision === "i16";
|
|
2537
|
+
const active = buildActiveLists(spot.runouts, spot.players, showdown);
|
|
2514
2538
|
const ctx = {
|
|
2515
2539
|
tree: spot.tree,
|
|
2516
2540
|
runouts: spot.runouts,
|
|
@@ -2523,7 +2547,8 @@ function createRun(spot, options = {}) {
|
|
|
2523
2547
|
sumScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
|
|
2524
2548
|
scratch: makeTerminalScratch(),
|
|
2525
2549
|
chipScale: spot.chipScale,
|
|
2526
|
-
active
|
|
2550
|
+
active,
|
|
2551
|
+
hands: buildHandSpace(spot.runouts, spot.players, active),
|
|
2527
2552
|
runoutWeight: weights,
|
|
2528
2553
|
shardStart: 0,
|
|
2529
2554
|
shardEnd: plan ? plan.boards : 0,
|