poker-flopsolve 0.0.0-stage → 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 +463 -2
- package/dist/browser.js +2882 -0
- package/dist/browser.js.map +1 -0
- package/dist/index.d.ts +1111 -0
- package/dist/index.js +2894 -0
- package/dist/index.js.map +1 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.js +2252 -0
- package/dist/worker.js.map +1 -0
- package/package.json +63 -4
package/dist/browser.js
ADDED
|
@@ -0,0 +1,2882 @@
|
|
|
1
|
+
// src/cards.ts
|
|
2
|
+
var RANK_CHARS = "23456789TJQKA";
|
|
3
|
+
var SUIT_CHARS = "shdc";
|
|
4
|
+
var RANKS = 13;
|
|
5
|
+
var SUITS = 4;
|
|
6
|
+
var DECK = 52;
|
|
7
|
+
var COMBOS = 1326;
|
|
8
|
+
function rankOf(card) {
|
|
9
|
+
return card >> 2;
|
|
10
|
+
}
|
|
11
|
+
function suitOf(card) {
|
|
12
|
+
return card & 3;
|
|
13
|
+
}
|
|
14
|
+
function cardOf(rank, suit) {
|
|
15
|
+
return rank << 2 | suit;
|
|
16
|
+
}
|
|
17
|
+
function formatCard(card) {
|
|
18
|
+
return RANK_CHARS[rankOf(card)] + SUIT_CHARS[suitOf(card)];
|
|
19
|
+
}
|
|
20
|
+
function formatCards(cards) {
|
|
21
|
+
return [...cards].map(formatCard).join(" ");
|
|
22
|
+
}
|
|
23
|
+
function parseCard(name) {
|
|
24
|
+
if (name.length !== 2) throw new Error(`Invalid card "${name}": expected two characters`);
|
|
25
|
+
const rank = RANK_CHARS.indexOf(name[0].toUpperCase());
|
|
26
|
+
const suit = SUIT_CHARS.indexOf(name[1].toLowerCase());
|
|
27
|
+
if (rank < 0) throw new Error(`Invalid rank "${name[0]}" in card "${name}"`);
|
|
28
|
+
if (suit < 0) throw new Error(`Invalid suit "${name[1]}" in card "${name}"`);
|
|
29
|
+
return cardOf(rank, suit);
|
|
30
|
+
}
|
|
31
|
+
function parseCards(text) {
|
|
32
|
+
const compact = text.replace(/[\s,]+/g, "");
|
|
33
|
+
if (compact.length % 2 !== 0) throw new Error(`"${text}" is not a whole number of cards`);
|
|
34
|
+
const cards = [];
|
|
35
|
+
const seen = /* @__PURE__ */ new Set();
|
|
36
|
+
for (let i = 0; i < compact.length; i += 2) {
|
|
37
|
+
const card = parseCard(compact.slice(i, i + 2));
|
|
38
|
+
if (seen.has(card)) throw new Error(`"${formatCard(card)}" appears twice in "${text}"`);
|
|
39
|
+
seen.add(card);
|
|
40
|
+
cards.push(card);
|
|
41
|
+
}
|
|
42
|
+
return cards;
|
|
43
|
+
}
|
|
44
|
+
function suitLanes(cards, lanes) {
|
|
45
|
+
lanes[0] = 0;
|
|
46
|
+
lanes[1] = 0;
|
|
47
|
+
lanes[2] = 0;
|
|
48
|
+
lanes[3] = 0;
|
|
49
|
+
for (const card of cards) lanes[card & 3] |= 1 << (card >> 2);
|
|
50
|
+
}
|
|
51
|
+
function comboIndex(a, b) {
|
|
52
|
+
const lo = a < b ? a : b;
|
|
53
|
+
const hi = a < b ? b : a;
|
|
54
|
+
return lo + (hi * (hi - 1) >> 1);
|
|
55
|
+
}
|
|
56
|
+
var COMBO_CARDS = (() => {
|
|
57
|
+
const cards = new Uint8Array(COMBOS * 2);
|
|
58
|
+
let index = 0;
|
|
59
|
+
for (let b = 1; b < DECK; b++) {
|
|
60
|
+
for (let a = 0; a < b; a++, index++) {
|
|
61
|
+
cards[index * 2] = a;
|
|
62
|
+
cards[index * 2 + 1] = b;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return cards;
|
|
66
|
+
})();
|
|
67
|
+
var COMBO_LANES = (() => {
|
|
68
|
+
const lanes = new Int32Array(COMBOS * 4);
|
|
69
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
70
|
+
const a = COMBO_CARDS[combo * 2];
|
|
71
|
+
const b = COMBO_CARDS[combo * 2 + 1];
|
|
72
|
+
lanes[combo * 4 + (a & 3)] |= 1 << (a >> 2);
|
|
73
|
+
lanes[combo * 4 + (b & 3)] |= 1 << (b >> 2);
|
|
74
|
+
}
|
|
75
|
+
return lanes;
|
|
76
|
+
})();
|
|
77
|
+
function formatCombo(combo) {
|
|
78
|
+
return formatCard(COMBO_CARDS[combo * 2 + 1]) + formatCard(COMBO_CARDS[combo * 2]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/tree.ts
|
|
82
|
+
function raisesPerRound(sizes) {
|
|
83
|
+
return sizes.length > 0 && Array.isArray(sizes[0]);
|
|
84
|
+
}
|
|
85
|
+
function perRound(value, listed, rounds, name) {
|
|
86
|
+
if (!listed) return Array.from({ length: rounds }, () => value);
|
|
87
|
+
const list = value;
|
|
88
|
+
if (list.length === 1) return Array.from({ length: rounds }, () => list[0]);
|
|
89
|
+
if (list.length !== rounds) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`${name} has ${list.length} entries but this board has ${rounds} betting rounds; give one per round, or a single value for all of them`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return [...list];
|
|
95
|
+
}
|
|
96
|
+
var NodeKind = {
|
|
97
|
+
Decision: 0,
|
|
98
|
+
Chance: 1,
|
|
99
|
+
TerminalFold: 2,
|
|
100
|
+
TerminalShowdown: 3
|
|
101
|
+
};
|
|
102
|
+
var ActionKind = {
|
|
103
|
+
Fold: 0,
|
|
104
|
+
Check: 1,
|
|
105
|
+
Call: 2,
|
|
106
|
+
Bet: 3,
|
|
107
|
+
Raise: 4,
|
|
108
|
+
AllIn: 5
|
|
109
|
+
};
|
|
110
|
+
var ACTION_LABELS = ["fold", "check", "call", "bet", "raise", "allin"];
|
|
111
|
+
function mergeBetSizes(sizes, distance) {
|
|
112
|
+
if (!(distance > 0)) return [...sizes];
|
|
113
|
+
const fractions = sizes.filter((size) => typeof size === "number").sort((a, b) => a - b);
|
|
114
|
+
const others = sizes.filter((size) => typeof size !== "number");
|
|
115
|
+
const merged = [];
|
|
116
|
+
let cluster = [];
|
|
117
|
+
const flush = () => {
|
|
118
|
+
if (cluster.length === 0) return;
|
|
119
|
+
merged.push(Math.round(cluster.reduce((a, b) => a + b, 0) / cluster.length * 1e3) / 1e3);
|
|
120
|
+
cluster = [];
|
|
121
|
+
};
|
|
122
|
+
for (const size of fractions) {
|
|
123
|
+
if (cluster.length > 0 && size - cluster[0] >= distance) flush();
|
|
124
|
+
cluster.push(size);
|
|
125
|
+
}
|
|
126
|
+
flush();
|
|
127
|
+
return [...merged, ...others];
|
|
128
|
+
}
|
|
129
|
+
function buildTree(config, runouts) {
|
|
130
|
+
const kind = [];
|
|
131
|
+
const player = [];
|
|
132
|
+
const round = [];
|
|
133
|
+
const firstEdge = [];
|
|
134
|
+
const numEdges = [];
|
|
135
|
+
const pot = [];
|
|
136
|
+
const payoff = [];
|
|
137
|
+
const edgeAction = [];
|
|
138
|
+
const edgeTarget = [];
|
|
139
|
+
const edgeChild = [];
|
|
140
|
+
const { potUnits, stackUnits, chipScale, minRaiseUnits } = config;
|
|
141
|
+
const rounds = runouts.rounds;
|
|
142
|
+
const maxRaises = perRound(config.maxRaises, Array.isArray(config.maxRaises), rounds, "maxRaises");
|
|
143
|
+
const allowAllIn = perRound(config.allowAllIn, Array.isArray(config.allowAllIn), rounds, "allowAllIn");
|
|
144
|
+
const merge = config.mergeSizes ?? 0;
|
|
145
|
+
const raiseSizes = config.raiseSizes ? perRound(config.raiseSizes, raisesPerRound(config.raiseSizes), rounds, "raiseSizes").map(
|
|
146
|
+
(row) => mergeBetSizes(row, merge)
|
|
147
|
+
) : null;
|
|
148
|
+
const betSizes = config.betSizes.map((row) => mergeBetSizes(row, merge));
|
|
149
|
+
const threshold = perRound(config.allInThreshold ?? 0, Array.isArray(config.allInThreshold), rounds, "allInThreshold");
|
|
150
|
+
const noDonk = perRound(config.noDonk ?? false, Array.isArray(config.noDonk), rounds, "noDonk");
|
|
151
|
+
const half = Math.round(potUnits / 2);
|
|
152
|
+
const addNode = (nodeKind, nodePlayer, nodeRound, nodePot, nodePayoff) => {
|
|
153
|
+
kind.push(nodeKind);
|
|
154
|
+
player.push(nodePlayer);
|
|
155
|
+
round.push(nodeRound);
|
|
156
|
+
firstEdge.push(-1);
|
|
157
|
+
numEdges.push(0);
|
|
158
|
+
pot.push(nodePot);
|
|
159
|
+
payoff.push(nodePayoff);
|
|
160
|
+
return kind.length - 1;
|
|
161
|
+
};
|
|
162
|
+
const closeRound = (state) => {
|
|
163
|
+
const total = potUnits + state.invested[0] + state.invested[1];
|
|
164
|
+
const allIn = state.invested[0] >= stackUnits || state.invested[1] >= stackUnits;
|
|
165
|
+
if (state.round + 1 >= runouts.rounds) {
|
|
166
|
+
return addNode(NodeKind.TerminalShowdown, -1, state.round, total, half + state.invested[0]);
|
|
167
|
+
}
|
|
168
|
+
const node = addNode(NodeKind.Chance, -1, state.round, total, 0);
|
|
169
|
+
const next = {
|
|
170
|
+
round: state.round + 1,
|
|
171
|
+
invested: state.invested,
|
|
172
|
+
toAct: 0,
|
|
173
|
+
aggressions: 0,
|
|
174
|
+
lastRaise: minRaiseUnits,
|
|
175
|
+
acted: 0,
|
|
176
|
+
base: state.invested[0],
|
|
177
|
+
aggressor: -1,
|
|
178
|
+
previousAggressor: state.aggressor
|
|
179
|
+
};
|
|
180
|
+
const child = allIn ? closeRound(next) : buildDecision(next);
|
|
181
|
+
firstEdge[node] = edgeChild.length;
|
|
182
|
+
numEdges[node] = 1;
|
|
183
|
+
edgeAction.push(ActionKind.Check);
|
|
184
|
+
edgeTarget.push(0);
|
|
185
|
+
edgeChild.push(child);
|
|
186
|
+
return node;
|
|
187
|
+
};
|
|
188
|
+
const resolveTarget = (size, state, cap) => {
|
|
189
|
+
const me = state.toAct;
|
|
190
|
+
const opp = 1 - me;
|
|
191
|
+
const mine = state.invested[me];
|
|
192
|
+
const theirs = state.invested[opp];
|
|
193
|
+
const owed = Math.max(0, theirs - mine);
|
|
194
|
+
const potAfterCall = potUnits + state.invested[0] + state.invested[1] + owed;
|
|
195
|
+
let target;
|
|
196
|
+
if (size === "allin") target = cap;
|
|
197
|
+
else if (typeof size === "number") target = mine + owed + Math.round(size * potAfterCall);
|
|
198
|
+
else if ("times" in size) target = state.base + Math.round(size.times * (theirs - state.base));
|
|
199
|
+
else target = mine + owed + Math.round(size.chips * chipScale);
|
|
200
|
+
target = Math.min(target, cap);
|
|
201
|
+
if (target <= theirs) return null;
|
|
202
|
+
const raiseSize = target - theirs;
|
|
203
|
+
const minimum = Math.max(state.lastRaise, minRaiseUnits);
|
|
204
|
+
if (raiseSize < minimum) {
|
|
205
|
+
const bumped = theirs + minimum;
|
|
206
|
+
if (bumped > cap) return null;
|
|
207
|
+
target = bumped;
|
|
208
|
+
}
|
|
209
|
+
return target;
|
|
210
|
+
};
|
|
211
|
+
const buildDecision = (state) => {
|
|
212
|
+
const me = state.toAct;
|
|
213
|
+
const opp = 1 - me;
|
|
214
|
+
const mine = state.invested[me];
|
|
215
|
+
const theirs = state.invested[opp];
|
|
216
|
+
const owed = Math.max(0, theirs - mine);
|
|
217
|
+
const myRemaining = stackUnits - mine;
|
|
218
|
+
const total = potUnits + state.invested[0] + state.invested[1];
|
|
219
|
+
const node = addNode(NodeKind.Decision, me, state.round, total, 0);
|
|
220
|
+
const actions = [];
|
|
221
|
+
if (owed > 0) actions.push({ action: ActionKind.Fold, target: mine });
|
|
222
|
+
actions.push({
|
|
223
|
+
action: owed > 0 ? ActionKind.Call : ActionKind.Check,
|
|
224
|
+
target: mine + Math.min(owed, myRemaining)
|
|
225
|
+
});
|
|
226
|
+
const cap = Math.min(stackUnits, theirs + (stackUnits - theirs));
|
|
227
|
+
const donk = noDonk[state.round] && me === 0 && owed === 0 && state.aggressions === 0 && state.previousAggressor === 1;
|
|
228
|
+
if (state.aggressions < maxRaises[state.round] && myRemaining > owed && !donk) {
|
|
229
|
+
const seen = /* @__PURE__ */ new Set();
|
|
230
|
+
const sizes = owed > 0 && raiseSizes ? [...raiseSizes[state.round]] : [...betSizes[state.round] ?? []].filter((size) => !(typeof size === "object" && "times" in size));
|
|
231
|
+
if (allowAllIn[state.round]) sizes.push("allin");
|
|
232
|
+
for (const size of sizes) {
|
|
233
|
+
let target = resolveTarget(size, state, cap);
|
|
234
|
+
if (target !== null && threshold[state.round] > 0 && target - mine >= threshold[state.round] * myRemaining) {
|
|
235
|
+
target = cap;
|
|
236
|
+
}
|
|
237
|
+
if (target === null || seen.has(target)) continue;
|
|
238
|
+
seen.add(target);
|
|
239
|
+
const isShove = target >= stackUnits;
|
|
240
|
+
actions.push({
|
|
241
|
+
action: isShove ? ActionKind.AllIn : owed > 0 ? ActionKind.Raise : ActionKind.Bet,
|
|
242
|
+
target
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const base = edgeChild.length;
|
|
247
|
+
firstEdge[node] = base;
|
|
248
|
+
numEdges[node] = actions.length;
|
|
249
|
+
for (const entry of actions) {
|
|
250
|
+
edgeAction.push(entry.action);
|
|
251
|
+
edgeTarget.push(entry.target);
|
|
252
|
+
edgeChild.push(-1);
|
|
253
|
+
}
|
|
254
|
+
actions.forEach((entry, i) => {
|
|
255
|
+
const invested = me === 0 ? [entry.target, theirs] : [theirs, entry.target];
|
|
256
|
+
let child;
|
|
257
|
+
if (entry.action === ActionKind.Fold) {
|
|
258
|
+
const value = me === 0 ? -(half + mine) : half + mine;
|
|
259
|
+
child = addNode(NodeKind.TerminalFold, -1, state.round, total, value);
|
|
260
|
+
} else if (entry.action === ActionKind.Check || entry.action === ActionKind.Call) {
|
|
261
|
+
const closed = invested[0] === invested[1] && state.acted + 1 >= 2;
|
|
262
|
+
child = closed ? closeRound({ ...state, invested, acted: state.acted + 1 }) : buildDecision({ ...state, invested, toAct: opp, acted: state.acted + 1 });
|
|
263
|
+
} else {
|
|
264
|
+
child = buildDecision({
|
|
265
|
+
round: state.round,
|
|
266
|
+
invested,
|
|
267
|
+
toAct: opp,
|
|
268
|
+
aggressions: state.aggressions + 1,
|
|
269
|
+
lastRaise: entry.target - theirs,
|
|
270
|
+
acted: state.acted + 1,
|
|
271
|
+
base: state.base,
|
|
272
|
+
aggressor: me,
|
|
273
|
+
previousAggressor: state.previousAggressor
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
edgeChild[base + i] = child;
|
|
277
|
+
});
|
|
278
|
+
return node;
|
|
279
|
+
};
|
|
280
|
+
buildDecision({
|
|
281
|
+
round: 0,
|
|
282
|
+
invested: [0, 0],
|
|
283
|
+
toAct: 0,
|
|
284
|
+
aggressions: 0,
|
|
285
|
+
lastRaise: minRaiseUnits,
|
|
286
|
+
acted: 0,
|
|
287
|
+
base: 0,
|
|
288
|
+
aggressor: -1,
|
|
289
|
+
previousAggressor: -1
|
|
290
|
+
});
|
|
291
|
+
const decisionNodes = [];
|
|
292
|
+
for (let i = 0; i < kind.length; i++) if (kind[i] === NodeKind.Decision) decisionNodes.push(i);
|
|
293
|
+
return {
|
|
294
|
+
nodeCount: kind.length,
|
|
295
|
+
edgeCount: edgeChild.length,
|
|
296
|
+
kind: Uint8Array.from(kind),
|
|
297
|
+
player: Int8Array.from(player),
|
|
298
|
+
round: Uint8Array.from(round),
|
|
299
|
+
firstEdge: Int32Array.from(firstEdge),
|
|
300
|
+
numEdges: Uint8Array.from(numEdges),
|
|
301
|
+
pot: Int32Array.from(pot),
|
|
302
|
+
payoff: Int32Array.from(payoff),
|
|
303
|
+
edgeAction: Uint8Array.from(edgeAction),
|
|
304
|
+
edgeTarget: Int32Array.from(edgeTarget),
|
|
305
|
+
edgeChild: Int32Array.from(edgeChild),
|
|
306
|
+
decisionNodes: Int32Array.from(decisionNodes)
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// src/abstraction.ts
|
|
311
|
+
var CardMaps = class {
|
|
312
|
+
/** `card[m][x]`: what map `m` sends card `x` to. */
|
|
313
|
+
card = [];
|
|
314
|
+
/** `combo[m][h]`: what map `m` sends combination `h` to. */
|
|
315
|
+
combo = [];
|
|
316
|
+
known = /* @__PURE__ */ new Map();
|
|
317
|
+
constructor() {
|
|
318
|
+
const identity = new Uint8Array(DECK);
|
|
319
|
+
for (let card = 0; card < DECK; card++) identity[card] = card;
|
|
320
|
+
this.add(identity);
|
|
321
|
+
}
|
|
322
|
+
/** Register a card map, returning its id; the same map always gets the same id. */
|
|
323
|
+
add(cardMap) {
|
|
324
|
+
const key = cardMap.join(",");
|
|
325
|
+
const existing = this.known.get(key);
|
|
326
|
+
if (existing !== void 0) return existing;
|
|
327
|
+
const combo = new Uint16Array(COMBOS);
|
|
328
|
+
for (let h = 0; h < COMBOS; h++) {
|
|
329
|
+
combo[h] = comboIndex(cardMap[COMBO_CARDS[h * 2]], cardMap[COMBO_CARDS[h * 2 + 1]]);
|
|
330
|
+
}
|
|
331
|
+
const id = this.card.length;
|
|
332
|
+
this.card.push(Uint8Array.from(cardMap));
|
|
333
|
+
this.combo.push(combo);
|
|
334
|
+
this.known.set(key, id);
|
|
335
|
+
return id;
|
|
336
|
+
}
|
|
337
|
+
/** The map that applies `first`, then `second`. */
|
|
338
|
+
then(first, second) {
|
|
339
|
+
const a = this.card[first];
|
|
340
|
+
const b = this.card[second];
|
|
341
|
+
const out = new Uint8Array(DECK);
|
|
342
|
+
for (let card = 0; card < DECK; card++) out[card] = b[a[card]];
|
|
343
|
+
return this.add(out);
|
|
344
|
+
}
|
|
345
|
+
/** The inverse of a map, which must be a bijection. */
|
|
346
|
+
inverse(id) {
|
|
347
|
+
const a = this.card[id];
|
|
348
|
+
const out = new Uint8Array(DECK);
|
|
349
|
+
for (let card = 0; card < DECK; card++) out[a[card]] = card;
|
|
350
|
+
return this.add(out);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
function suitPermutations() {
|
|
354
|
+
const out = [];
|
|
355
|
+
const walk2 = (prefix) => {
|
|
356
|
+
if (prefix.length === 4) {
|
|
357
|
+
out.push(prefix);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
for (let s = 0; s < 4; s++) if (!prefix.includes(s)) walk2([...prefix, s]);
|
|
361
|
+
};
|
|
362
|
+
walk2([]);
|
|
363
|
+
return out;
|
|
364
|
+
}
|
|
365
|
+
function suitMap(perm) {
|
|
366
|
+
const out = new Uint8Array(DECK);
|
|
367
|
+
for (let card = 0; card < DECK; card++) out[card] = card & ~3 | perm[card & 3];
|
|
368
|
+
return out;
|
|
369
|
+
}
|
|
370
|
+
function suitSymmetries(board, ranges) {
|
|
371
|
+
const onBoard = new Uint8Array(DECK);
|
|
372
|
+
for (const card of board) onBoard[card] = 1;
|
|
373
|
+
const out = [];
|
|
374
|
+
for (const perm of suitPermutations()) {
|
|
375
|
+
const map = suitMap(perm);
|
|
376
|
+
let ok = true;
|
|
377
|
+
for (const card of board) if (!onBoard[map[card]]) ok = false;
|
|
378
|
+
for (let h = 0; ok && h < COMBOS; h++) {
|
|
379
|
+
const image = comboIndex(map[COMBO_CARDS[h * 2]], map[COMBO_CARDS[h * 2 + 1]]);
|
|
380
|
+
for (const weights of ranges) {
|
|
381
|
+
if (weights[h] !== weights[image]) {
|
|
382
|
+
ok = false;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (ok) out.push(map);
|
|
388
|
+
}
|
|
389
|
+
return out;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// src/runouts.ts
|
|
393
|
+
function enumerateRunouts(board, symmetries, merge) {
|
|
394
|
+
if (board.length < 3 || board.length > 5) {
|
|
395
|
+
throw new Error(`A postflop board has 3, 4 or 5 cards; got ${board.length} (${formatCards(board)})`);
|
|
396
|
+
}
|
|
397
|
+
if (new Set(board).size !== board.length) {
|
|
398
|
+
throw new Error(`Board repeats a card: ${formatCards(board)}`);
|
|
399
|
+
}
|
|
400
|
+
const maps = new CardMaps();
|
|
401
|
+
const group = (symmetries ?? []).map((map) => maps.add(map));
|
|
402
|
+
if (group.length === 0 || group[0] !== 0) group.unshift(0);
|
|
403
|
+
const rounds = 5 - board.length + 1;
|
|
404
|
+
const width = [board.length];
|
|
405
|
+
const counts = [1];
|
|
406
|
+
const cards = [Uint8Array.from(board)];
|
|
407
|
+
const childStart = [new Int32Array(0)];
|
|
408
|
+
const parent = [Int32Array.of(-1)];
|
|
409
|
+
const orbitStart = [Int32Array.of(0, 1)];
|
|
410
|
+
const orbitMap = [Int32Array.of(0)];
|
|
411
|
+
const orbitCard = [new Uint8Array(1)];
|
|
412
|
+
let stabilisers = [group];
|
|
413
|
+
let symmetric = false;
|
|
414
|
+
let approximate = false;
|
|
415
|
+
const stabiliserStart = [];
|
|
416
|
+
const stabiliserMap = [];
|
|
417
|
+
const recordStabilisers = (list) => {
|
|
418
|
+
const starts = new Int32Array(list.length + 1);
|
|
419
|
+
const flat = [];
|
|
420
|
+
for (let b = 0; b < list.length; b++) {
|
|
421
|
+
starts[b] = flat.length;
|
|
422
|
+
flat.push(...list[b]);
|
|
423
|
+
}
|
|
424
|
+
starts[list.length] = flat.length;
|
|
425
|
+
stabiliserStart.push(starts);
|
|
426
|
+
stabiliserMap.push(Int32Array.from(flat));
|
|
427
|
+
};
|
|
428
|
+
recordStabilisers(stabilisers);
|
|
429
|
+
const used = new Uint8Array(DECK);
|
|
430
|
+
const seen = new Uint8Array(DECK);
|
|
431
|
+
for (let round = 1; round < rounds; round++) {
|
|
432
|
+
const previousWidth = width[round - 1];
|
|
433
|
+
const previousCount = counts[round - 1];
|
|
434
|
+
const previousCards = cards[round - 1];
|
|
435
|
+
const thisWidth = previousWidth + 1;
|
|
436
|
+
const level = [];
|
|
437
|
+
const parents = [];
|
|
438
|
+
const starts = new Int32Array(previousCount + 1);
|
|
439
|
+
const memberStart = [0];
|
|
440
|
+
const memberMap = [];
|
|
441
|
+
const memberCard = [];
|
|
442
|
+
const nextStabilisers = [];
|
|
443
|
+
for (let p = 0; p < previousCount; p++) {
|
|
444
|
+
starts[p] = parents.length;
|
|
445
|
+
used.fill(0);
|
|
446
|
+
seen.fill(0);
|
|
447
|
+
const base = p * previousWidth;
|
|
448
|
+
for (let i = 0; i < previousWidth; i++) used[previousCards[base + i]] = 1;
|
|
449
|
+
const stabiliser = stabilisers[p];
|
|
450
|
+
const children = [];
|
|
451
|
+
for (let card = 0; card < DECK; card++) {
|
|
452
|
+
if (used[card] || seen[card]) continue;
|
|
453
|
+
const child = { card, maps: [], cards: [], fixing: [], merged: false };
|
|
454
|
+
for (const sigma of stabiliser) {
|
|
455
|
+
const image = maps.card[sigma][card];
|
|
456
|
+
if (image === card) child.fixing.push(sigma);
|
|
457
|
+
if (seen[image]) continue;
|
|
458
|
+
seen[image] = 1;
|
|
459
|
+
child.maps.push(image === card ? 0 : maps.inverse(sigma));
|
|
460
|
+
child.cards.push(image);
|
|
461
|
+
}
|
|
462
|
+
children.push(child);
|
|
463
|
+
}
|
|
464
|
+
if (merge) {
|
|
465
|
+
const boardRanks = /* @__PURE__ */ new Set();
|
|
466
|
+
for (let i = 0; i < previousWidth; i++) boardRanks.add(previousCards[base + i] >> 2);
|
|
467
|
+
for (let j = 0; j < children.length; j++) {
|
|
468
|
+
const later = children[j];
|
|
469
|
+
const rankJ = later.card >> 2;
|
|
470
|
+
if (boardRanks.has(rankJ)) continue;
|
|
471
|
+
for (let i = 0; i < j; i++) {
|
|
472
|
+
const earlier = children[i];
|
|
473
|
+
if (earlier.merged || (earlier.card & 3) !== (later.card & 3)) continue;
|
|
474
|
+
const rankI = earlier.card >> 2;
|
|
475
|
+
if (boardRanks.has(rankI) || rankJ - rankI > merge.maxGap || !merge.ranks(rankI, rankJ)) continue;
|
|
476
|
+
if (connectivity(boardRanks, rankI) !== connectivity(boardRanks, rankJ)) continue;
|
|
477
|
+
const swap = maps.add(rankSwap(rankI, rankJ));
|
|
478
|
+
for (let m = 0; m < later.maps.length; m++) {
|
|
479
|
+
earlier.maps.push(maps.then(later.maps[m], swap));
|
|
480
|
+
earlier.cards.push(later.cards[m]);
|
|
481
|
+
}
|
|
482
|
+
later.merged = true;
|
|
483
|
+
approximate = true;
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
for (const child of children) {
|
|
489
|
+
if (child.merged) continue;
|
|
490
|
+
for (let i = 0; i < previousWidth; i++) level.push(previousCards[base + i]);
|
|
491
|
+
level.push(child.card);
|
|
492
|
+
parents.push(p);
|
|
493
|
+
memberMap.push(...child.maps);
|
|
494
|
+
memberCard.push(...child.cards);
|
|
495
|
+
memberStart.push(memberMap.length);
|
|
496
|
+
nextStabilisers.push(child.fixing);
|
|
497
|
+
if (child.maps.length > 1) symmetric = true;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
starts[previousCount] = parents.length;
|
|
501
|
+
width.push(thisWidth);
|
|
502
|
+
counts.push(parents.length);
|
|
503
|
+
cards.push(Uint8Array.from(level));
|
|
504
|
+
childStart.push(starts);
|
|
505
|
+
parent.push(Int32Array.from(parents));
|
|
506
|
+
orbitStart.push(Int32Array.from(memberStart));
|
|
507
|
+
orbitMap.push(Int32Array.from(memberMap));
|
|
508
|
+
orbitCard.push(Uint8Array.from(memberCard));
|
|
509
|
+
stabilisers = nextStabilisers;
|
|
510
|
+
recordStabilisers(stabilisers);
|
|
511
|
+
}
|
|
512
|
+
return {
|
|
513
|
+
rounds,
|
|
514
|
+
width,
|
|
515
|
+
counts,
|
|
516
|
+
cards,
|
|
517
|
+
childStart,
|
|
518
|
+
parent,
|
|
519
|
+
orbitStart,
|
|
520
|
+
orbitMap,
|
|
521
|
+
orbitCard,
|
|
522
|
+
stabiliserStart,
|
|
523
|
+
stabiliserMap,
|
|
524
|
+
maps,
|
|
525
|
+
symmetric,
|
|
526
|
+
approximate,
|
|
527
|
+
initial: [...board]
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
function rankSwap(a, b) {
|
|
531
|
+
const out = new Uint8Array(DECK);
|
|
532
|
+
for (let card = 0; card < DECK; card++) {
|
|
533
|
+
const rank = card >> 2;
|
|
534
|
+
out[card] = (rank === a ? b : rank === b ? a : rank) << 2 | card & 3;
|
|
535
|
+
}
|
|
536
|
+
return out;
|
|
537
|
+
}
|
|
538
|
+
function connectivity(boardRanks, rank) {
|
|
539
|
+
let best = 0;
|
|
540
|
+
for (let low = -1; low <= 8; low++) {
|
|
541
|
+
if (rank < low || rank > low + 4) {
|
|
542
|
+
if (!(low === -1 && rank === 12)) continue;
|
|
543
|
+
}
|
|
544
|
+
let count = 0;
|
|
545
|
+
for (let r = low; r <= low + 4; r++) {
|
|
546
|
+
const actual = r === -1 ? 12 : r;
|
|
547
|
+
if (actual !== rank && boardRanks.has(actual)) count++;
|
|
548
|
+
}
|
|
549
|
+
if (count > best) best = count;
|
|
550
|
+
}
|
|
551
|
+
return best;
|
|
552
|
+
}
|
|
553
|
+
function boardAtRound(runouts, board, round, target) {
|
|
554
|
+
let index = board;
|
|
555
|
+
for (let r = round; r > target; r--) index = runouts.parent[r][index];
|
|
556
|
+
return index;
|
|
557
|
+
}
|
|
558
|
+
function resolveBoard(runouts, board) {
|
|
559
|
+
const start = runouts.initial.length;
|
|
560
|
+
for (let i = 0; i < start; i++) {
|
|
561
|
+
if (board[i] !== runouts.initial[i]) {
|
|
562
|
+
throw new Error(`${formatCards(board)} does not start with the spot's board ${formatCards(runouts.initial)}`);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (board.length > 5) throw new Error(`A board has at most 5 cards; got ${board.length}`);
|
|
566
|
+
let index = 0;
|
|
567
|
+
let map = 0;
|
|
568
|
+
for (let i = start; i < board.length; i++) {
|
|
569
|
+
const round = i - start + 1;
|
|
570
|
+
const dealt = runouts.maps.card[map][board[i]];
|
|
571
|
+
let found = -1;
|
|
572
|
+
for (let child = runouts.childStart[round][index]; child < runouts.childStart[round][index + 1] && found < 0; child++) {
|
|
573
|
+
for (let m = runouts.orbitStart[round][child]; m < runouts.orbitStart[round][child + 1]; m++) {
|
|
574
|
+
if (runouts.orbitCard[round][m] !== dealt) continue;
|
|
575
|
+
found = child;
|
|
576
|
+
map = runouts.maps.then(map, runouts.orbitMap[round][m]);
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (found < 0) throw new Error(`${formatCards(board)} deals a card already on the board`);
|
|
581
|
+
index = found;
|
|
582
|
+
}
|
|
583
|
+
return { round: board.length - start, board: index, combos: runouts.maps.combo[map] };
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// src/range.ts
|
|
587
|
+
var RANK_CHARS2 = "AKQJT98765432";
|
|
588
|
+
function rankOfChar(char) {
|
|
589
|
+
const index = RANK_CHARS2.indexOf(char.toUpperCase());
|
|
590
|
+
if (index < 0) throw new Error(`Invalid rank "${char}"`);
|
|
591
|
+
return 12 - index;
|
|
592
|
+
}
|
|
593
|
+
function pairCombos(rank, out) {
|
|
594
|
+
for (let a = 0; a < SUITS; a++) {
|
|
595
|
+
for (let b = a + 1; b < SUITS; b++) out.push(comboIndex(cardOf(rank, a), cardOf(rank, b)));
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function suitedCombos(high2, low, out) {
|
|
599
|
+
for (let suit = 0; suit < SUITS; suit++) out.push(comboIndex(cardOf(high2, suit), cardOf(low, suit)));
|
|
600
|
+
}
|
|
601
|
+
function offsuitCombos(high2, low, out) {
|
|
602
|
+
for (let a = 0; a < SUITS; a++) {
|
|
603
|
+
for (let b = 0; b < SUITS; b++) {
|
|
604
|
+
if (a !== b) out.push(comboIndex(cardOf(high2, a), cardOf(low, b)));
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function ranksBetween(a, b) {
|
|
609
|
+
const lo = Math.min(a, b);
|
|
610
|
+
const hi = Math.max(a, b);
|
|
611
|
+
const ranks = [];
|
|
612
|
+
for (let r = lo; r <= hi; r++) ranks.push(r);
|
|
613
|
+
return ranks;
|
|
614
|
+
}
|
|
615
|
+
function expand(segment) {
|
|
616
|
+
const out = [];
|
|
617
|
+
const text = segment.trim();
|
|
618
|
+
if (!text) return out;
|
|
619
|
+
if (/^(random|any|100%)$/i.test(text)) {
|
|
620
|
+
for (let combo = 0; combo < COMBOS; combo++) out.push(combo);
|
|
621
|
+
return out;
|
|
622
|
+
}
|
|
623
|
+
if (/^([AKQJT2-9][shdc]){2}$/i.test(text)) {
|
|
624
|
+
const a = parseCard(text.slice(0, 2));
|
|
625
|
+
const b = parseCard(text.slice(2, 4));
|
|
626
|
+
if (a === b) throw new Error(`"${text}" uses the same card twice`);
|
|
627
|
+
out.push(comboIndex(a, b));
|
|
628
|
+
return out;
|
|
629
|
+
}
|
|
630
|
+
const wildcard = /^([AKQJT2-9])[xX]([so])?$/.exec(text);
|
|
631
|
+
if (wildcard) {
|
|
632
|
+
const rank = rankOfChar(wildcard[1]);
|
|
633
|
+
const kind = wildcard[2]?.toLowerCase();
|
|
634
|
+
for (let other = 0; other < 13; other++) {
|
|
635
|
+
if (other === rank) {
|
|
636
|
+
if (kind !== "s") pairCombos(rank, out);
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
const high2 = Math.max(rank, other);
|
|
640
|
+
const low = Math.min(rank, other);
|
|
641
|
+
if (kind !== "o") suitedCombos(high2, low, out);
|
|
642
|
+
if (kind !== "s") offsuitCombos(high2, low, out);
|
|
643
|
+
}
|
|
644
|
+
return out;
|
|
645
|
+
}
|
|
646
|
+
const emit = (high2, low, kind) => {
|
|
647
|
+
if (high2 === low) {
|
|
648
|
+
pairCombos(high2, out);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (kind !== "o") suitedCombos(high2, low, out);
|
|
652
|
+
if (kind !== "s") offsuitCombos(high2, low, out);
|
|
653
|
+
};
|
|
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
|
+
const kind = k1 ?? k2;
|
|
658
|
+
if (k1 && k2 && k1 !== k2) throw new Error(`"${text}" mixes suited and offsuit`);
|
|
659
|
+
const from1 = rankOfChar(a1);
|
|
660
|
+
const from2 = rankOfChar(a2);
|
|
661
|
+
const to1 = rankOfChar(b1);
|
|
662
|
+
const to2 = rankOfChar(b2);
|
|
663
|
+
if (from1 === from2 && to1 === to2) {
|
|
664
|
+
for (const rank of ranksBetween(from1, to1)) pairCombos(rank, out);
|
|
665
|
+
return out;
|
|
666
|
+
}
|
|
667
|
+
if (from1 !== to1) throw new Error(`"${text}" must hold its higher rank fixed, e.g. A5s-A2s`);
|
|
668
|
+
for (const low of ranksBetween(from2, to2)) {
|
|
669
|
+
if (low === from1) continue;
|
|
670
|
+
emit(Math.max(from1, low), Math.min(from1, low), kind);
|
|
671
|
+
}
|
|
672
|
+
return out;
|
|
673
|
+
}
|
|
674
|
+
const plus = /^([AKQJT2-9])([AKQJT2-9])([so])?\+$/.exec(text);
|
|
675
|
+
if (plus) {
|
|
676
|
+
const first = rankOfChar(plus[1]);
|
|
677
|
+
const second = rankOfChar(plus[2]);
|
|
678
|
+
const kind = plus[3]?.toLowerCase();
|
|
679
|
+
if (first === second) {
|
|
680
|
+
for (let rank = first; rank <= 12; rank++) pairCombos(rank, out);
|
|
681
|
+
return out;
|
|
682
|
+
}
|
|
683
|
+
const high2 = Math.max(first, second);
|
|
684
|
+
const from = Math.min(first, second);
|
|
685
|
+
for (let low = from; low < high2; low++) emit(high2, low, kind);
|
|
686
|
+
return out;
|
|
687
|
+
}
|
|
688
|
+
const single = /^([AKQJT2-9])([AKQJT2-9])([so])?$/.exec(text);
|
|
689
|
+
if (single) {
|
|
690
|
+
const first = rankOfChar(single[1]);
|
|
691
|
+
const second = rankOfChar(single[2]);
|
|
692
|
+
const kind = single[3]?.toLowerCase();
|
|
693
|
+
if (first === second && kind === "s") throw new Error(`"${text}" is impossible: a pair cannot be suited`);
|
|
694
|
+
emit(Math.max(first, second), Math.min(first, second), kind);
|
|
695
|
+
return out;
|
|
696
|
+
}
|
|
697
|
+
throw new Error(`Unrecognised range notation: "${segment.trim()}"`);
|
|
698
|
+
}
|
|
699
|
+
function parseRange(notation) {
|
|
700
|
+
const weights = new Float64Array(COMBOS);
|
|
701
|
+
if (!notation || !notation.trim()) return weights;
|
|
702
|
+
for (const segment of notation.split(",")) {
|
|
703
|
+
if (!segment.trim()) continue;
|
|
704
|
+
const [body, weightText] = segment.split(":");
|
|
705
|
+
let weight = 1;
|
|
706
|
+
if (weightText !== void 0) {
|
|
707
|
+
weight = Number(weightText.trim().replace("%", "")) / (weightText.includes("%") ? 100 : 1);
|
|
708
|
+
if (!Number.isFinite(weight) || weight < 0 || weight > 1) {
|
|
709
|
+
throw new Error(`Weight in "${segment.trim()}" must be between 0 and 1`);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
for (const combo of expand(body)) weights[combo] = weight;
|
|
713
|
+
}
|
|
714
|
+
return weights;
|
|
715
|
+
}
|
|
716
|
+
function activeCombos(weights) {
|
|
717
|
+
const combos = [];
|
|
718
|
+
for (let combo = 0; combo < COMBOS; combo++) if (weights[combo] > 0) combos.push(combo);
|
|
719
|
+
return combos;
|
|
720
|
+
}
|
|
721
|
+
function rangeWeight(weights) {
|
|
722
|
+
let total = 0;
|
|
723
|
+
for (let combo = 0; combo < COMBOS; combo++) total += weights[combo];
|
|
724
|
+
return total;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// src/eval7.ts
|
|
728
|
+
var POPCOUNT = new Uint8Array(8192);
|
|
729
|
+
var STRAIGHT = new Int8Array(8192);
|
|
730
|
+
for (let mask = 0; mask < 8192; mask++) {
|
|
731
|
+
let bits = 0;
|
|
732
|
+
for (let x = mask; x; x &= x - 1) bits++;
|
|
733
|
+
POPCOUNT[mask] = bits;
|
|
734
|
+
let run = 0;
|
|
735
|
+
let top = -1;
|
|
736
|
+
for (let rank = 12; rank >= 0; rank--) {
|
|
737
|
+
if (mask & 1 << rank) {
|
|
738
|
+
if (++run >= 5) {
|
|
739
|
+
top = rank + 4;
|
|
740
|
+
break;
|
|
741
|
+
}
|
|
742
|
+
} else {
|
|
743
|
+
run = 0;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (top < 0 && mask & 1 << 12 && (mask & 15) === 15) top = 3;
|
|
747
|
+
STRAIGHT[mask] = top;
|
|
748
|
+
}
|
|
749
|
+
function high(mask) {
|
|
750
|
+
return 31 - Math.clz32(mask);
|
|
751
|
+
}
|
|
752
|
+
var CATEGORY = {
|
|
753
|
+
highCard: 0,
|
|
754
|
+
pair: 1,
|
|
755
|
+
twoPair: 2,
|
|
756
|
+
trips: 3,
|
|
757
|
+
straight: 4,
|
|
758
|
+
flush: 5,
|
|
759
|
+
fullHouse: 6,
|
|
760
|
+
quads: 7,
|
|
761
|
+
straightFlush: 8
|
|
762
|
+
};
|
|
763
|
+
var CATEGORY_NAMES = [
|
|
764
|
+
"high card",
|
|
765
|
+
"pair",
|
|
766
|
+
"two pair",
|
|
767
|
+
"three of a kind",
|
|
768
|
+
"straight",
|
|
769
|
+
"flush",
|
|
770
|
+
"full house",
|
|
771
|
+
"four of a kind",
|
|
772
|
+
"straight flush"
|
|
773
|
+
];
|
|
774
|
+
function categoryOf(value) {
|
|
775
|
+
return value / 1048576 | 0;
|
|
776
|
+
}
|
|
777
|
+
function evaluate(s, h, d, c) {
|
|
778
|
+
const all = s | h | d | c;
|
|
779
|
+
let flush = 0;
|
|
780
|
+
if (POPCOUNT[s] >= 5) flush = s;
|
|
781
|
+
else if (POPCOUNT[h] >= 5) flush = h;
|
|
782
|
+
else if (POPCOUNT[d] >= 5) flush = d;
|
|
783
|
+
else if (POPCOUNT[c] >= 5) flush = c;
|
|
784
|
+
if (flush) {
|
|
785
|
+
const top = STRAIGHT[flush];
|
|
786
|
+
if (top >= 0) return 8 * 1048576 + top * 65536;
|
|
787
|
+
}
|
|
788
|
+
const four = s & h & d & c;
|
|
789
|
+
const threePlus = s & h & d | s & h & c | s & d & c | h & d & c;
|
|
790
|
+
const twoPlus = s & h | s & d | s & c | h & d | h & c | d & c;
|
|
791
|
+
const trips = threePlus & ~four;
|
|
792
|
+
const pairs = twoPlus & ~threePlus;
|
|
793
|
+
if (four) {
|
|
794
|
+
const quad = high(four);
|
|
795
|
+
const kicker = high(all & ~(1 << quad));
|
|
796
|
+
return 7 * 1048576 + quad * 65536 + kicker * 4096;
|
|
797
|
+
}
|
|
798
|
+
if (trips) {
|
|
799
|
+
const top = high(trips);
|
|
800
|
+
const otherTrips = trips & ~(1 << top);
|
|
801
|
+
const pair = otherTrips ? high(otherTrips) : pairs ? high(pairs) : -1;
|
|
802
|
+
if (pair >= 0) return 6 * 1048576 + top * 65536 + pair * 4096;
|
|
803
|
+
}
|
|
804
|
+
if (flush) {
|
|
805
|
+
let mask = flush;
|
|
806
|
+
let value = 5 * 1048576;
|
|
807
|
+
let shift = 65536;
|
|
808
|
+
for (let i = 0; i < 5; i++) {
|
|
809
|
+
const rank = high(mask);
|
|
810
|
+
value += rank * shift;
|
|
811
|
+
mask &= ~(1 << rank);
|
|
812
|
+
shift >>= 4;
|
|
813
|
+
}
|
|
814
|
+
return value;
|
|
815
|
+
}
|
|
816
|
+
{
|
|
817
|
+
const top = STRAIGHT[all];
|
|
818
|
+
if (top >= 0) return 4 * 1048576 + top * 65536;
|
|
819
|
+
}
|
|
820
|
+
if (trips) {
|
|
821
|
+
const top = high(trips);
|
|
822
|
+
let rest = all & ~(1 << top);
|
|
823
|
+
const k1 = high(rest);
|
|
824
|
+
rest &= ~(1 << k1);
|
|
825
|
+
const k2 = high(rest);
|
|
826
|
+
return 3 * 1048576 + top * 65536 + k1 * 4096 + k2 * 256;
|
|
827
|
+
}
|
|
828
|
+
if (pairs && pairs & pairs - 1) {
|
|
829
|
+
const p1 = high(pairs);
|
|
830
|
+
const p2 = high(pairs & ~(1 << p1));
|
|
831
|
+
const kicker = high(all & ~(1 << p1) & ~(1 << p2));
|
|
832
|
+
return 2 * 1048576 + p1 * 65536 + p2 * 4096 + kicker * 256;
|
|
833
|
+
}
|
|
834
|
+
if (pairs) {
|
|
835
|
+
const pair = high(pairs);
|
|
836
|
+
let rest = all & ~(1 << pair);
|
|
837
|
+
const k1 = high(rest);
|
|
838
|
+
rest &= ~(1 << k1);
|
|
839
|
+
const k2 = high(rest);
|
|
840
|
+
rest &= ~(1 << k2);
|
|
841
|
+
const k3 = high(rest);
|
|
842
|
+
return 1 * 1048576 + pair * 65536 + k1 * 4096 + k2 * 256 + k3 * 16;
|
|
843
|
+
}
|
|
844
|
+
{
|
|
845
|
+
let mask = all;
|
|
846
|
+
let value = 0;
|
|
847
|
+
let shift = 65536;
|
|
848
|
+
for (let i = 0; i < 5; i++) {
|
|
849
|
+
const rank = high(mask);
|
|
850
|
+
value += rank * shift;
|
|
851
|
+
mask &= ~(1 << rank);
|
|
852
|
+
shift >>= 4;
|
|
853
|
+
}
|
|
854
|
+
return value;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
function evaluateCards(cards) {
|
|
858
|
+
let s = 0;
|
|
859
|
+
let h = 0;
|
|
860
|
+
let d = 0;
|
|
861
|
+
let c = 0;
|
|
862
|
+
for (let i = 0; i < cards.length; i++) {
|
|
863
|
+
const card = cards[i];
|
|
864
|
+
const bit = 1 << (card >> 2);
|
|
865
|
+
switch (card & 3) {
|
|
866
|
+
case 0:
|
|
867
|
+
s |= bit;
|
|
868
|
+
break;
|
|
869
|
+
case 1:
|
|
870
|
+
h |= bit;
|
|
871
|
+
break;
|
|
872
|
+
case 2:
|
|
873
|
+
d |= bit;
|
|
874
|
+
break;
|
|
875
|
+
default:
|
|
876
|
+
c |= bit;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
return evaluate(s, h, d, c);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// src/showdown.ts
|
|
883
|
+
var SUBSET_SPACE = DECK + COMBOS;
|
|
884
|
+
function makeTerminalScratch() {
|
|
885
|
+
return {
|
|
886
|
+
weaker: new Float64Array(SUBSET_SPACE),
|
|
887
|
+
stronger: new Float64Array(SUBSET_SPACE),
|
|
888
|
+
all: new Float64Array(SUBSET_SPACE),
|
|
889
|
+
win: new Float64Array(COMBOS)
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
function buildShowdownTable(runouts, relevant) {
|
|
893
|
+
const round = runouts.rounds - 1;
|
|
894
|
+
const boards = runouts.counts[round];
|
|
895
|
+
const width = runouts.width[round];
|
|
896
|
+
const boardCards = runouts.cards[round];
|
|
897
|
+
const wanted = [];
|
|
898
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
899
|
+
if (!relevant || relevant[combo]) wanted.push(combo);
|
|
900
|
+
}
|
|
901
|
+
const stride = Math.max(1, wanted.length);
|
|
902
|
+
const order = new Uint16Array(boards * stride);
|
|
903
|
+
const liveCount = new Uint16Array(boards);
|
|
904
|
+
const groupStart = new Uint16Array(boards * (stride + 1));
|
|
905
|
+
const groupCount = new Uint16Array(boards);
|
|
906
|
+
const lanes = new Int32Array(4);
|
|
907
|
+
const onBoard = new Uint8Array(DECK);
|
|
908
|
+
const strength = new Int32Array(COMBOS);
|
|
909
|
+
const live = new Uint16Array(COMBOS);
|
|
910
|
+
for (let board = 0; board < boards; board++) {
|
|
911
|
+
const base = board * width;
|
|
912
|
+
onBoard.fill(0);
|
|
913
|
+
for (let i2 = 0; i2 < width; i2++) onBoard[boardCards[base + i2]] = 1;
|
|
914
|
+
suitLanes(boardCards.subarray(base, base + width), lanes);
|
|
915
|
+
const b0 = lanes[0];
|
|
916
|
+
const b1 = lanes[1];
|
|
917
|
+
const b2 = lanes[2];
|
|
918
|
+
const b3 = lanes[3];
|
|
919
|
+
let count = 0;
|
|
920
|
+
for (const combo of wanted) {
|
|
921
|
+
if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
|
|
922
|
+
const lane = combo * 4;
|
|
923
|
+
strength[combo] = evaluate(
|
|
924
|
+
COMBO_LANES[lane] | b0,
|
|
925
|
+
COMBO_LANES[lane + 1] | b1,
|
|
926
|
+
COMBO_LANES[lane + 2] | b2,
|
|
927
|
+
COMBO_LANES[lane + 3] | b3
|
|
928
|
+
);
|
|
929
|
+
live[count++] = combo;
|
|
930
|
+
}
|
|
931
|
+
const slice = live.subarray(0, count);
|
|
932
|
+
slice.sort((x, y) => strength[x] - strength[y]);
|
|
933
|
+
order.set(slice, board * stride);
|
|
934
|
+
liveCount[board] = count;
|
|
935
|
+
const groupBase = board * (stride + 1);
|
|
936
|
+
let groups = 0;
|
|
937
|
+
let i = 0;
|
|
938
|
+
while (i < count) {
|
|
939
|
+
groupStart[groupBase + groups] = i;
|
|
940
|
+
const value = strength[slice[i]];
|
|
941
|
+
while (i < count && strength[slice[i]] === value) i++;
|
|
942
|
+
groups++;
|
|
943
|
+
}
|
|
944
|
+
groupStart[groupBase + groups] = count;
|
|
945
|
+
groupCount[board] = groups;
|
|
946
|
+
}
|
|
947
|
+
return {
|
|
948
|
+
boards,
|
|
949
|
+
stride,
|
|
950
|
+
order,
|
|
951
|
+
liveCount,
|
|
952
|
+
groupStart,
|
|
953
|
+
groupCount,
|
|
954
|
+
bytes: order.byteLength + liveCount.byteLength + groupStart.byteLength + groupCount.byteLength
|
|
955
|
+
};
|
|
956
|
+
}
|
|
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;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
upStart.push(starts);
|
|
991
|
+
}
|
|
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;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
sameStart[slots] = same.length;
|
|
1009
|
+
return { upStart, up: Uint16Array.from(up), sameStart, same: Int32Array.from(same) };
|
|
1010
|
+
}
|
|
1011
|
+
function foldValuesHands(oppReach, cardA, cardB, oppFrom, oppCount, myFrom, myCount, same, sameFrom, payoff, sums, out) {
|
|
1012
|
+
let total = 0;
|
|
1013
|
+
for (let j = 0; j < oppCount; j++) {
|
|
1014
|
+
const value = oppReach[j];
|
|
1015
|
+
total += value;
|
|
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]]);
|
|
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);
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// src/pairs.ts
|
|
1058
|
+
var CHUNK = 64;
|
|
1059
|
+
function treeDepth(ctx) {
|
|
1060
|
+
const { tree } = ctx;
|
|
1061
|
+
const depth = new Int32Array(tree.nodeCount).fill(-1);
|
|
1062
|
+
depth[0] = 1;
|
|
1063
|
+
let max = 1;
|
|
1064
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
1065
|
+
const here = depth[node];
|
|
1066
|
+
if (here < 0) continue;
|
|
1067
|
+
if (here > max) max = here;
|
|
1068
|
+
for (let e = 0; e < tree.numEdges[node]; e++) {
|
|
1069
|
+
const child = tree.edgeChild[tree.firstEdge[node] + e];
|
|
1070
|
+
if (depth[child] < here + 1) depth[child] = here + 1;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
return max + 2;
|
|
1074
|
+
}
|
|
1075
|
+
function makeBuffers(ctx, width) {
|
|
1076
|
+
const depth = treeDepth(ctx);
|
|
1077
|
+
const make = (factory) => Array.from({ length: depth }, factory);
|
|
1078
|
+
const actions = ctx.layout.maxActions;
|
|
1079
|
+
return {
|
|
1080
|
+
value: make(() => new Float64Array(width * COMBOS)),
|
|
1081
|
+
cols: make(() => new Int32Array(width)),
|
|
1082
|
+
colReach: make(() => new Float64Array(width)),
|
|
1083
|
+
live: make(() => new Uint16Array(COMBOS)),
|
|
1084
|
+
hands: make(() => new Uint16Array(width)),
|
|
1085
|
+
strategy: make(() => new Float64Array(actions * COMBOS)),
|
|
1086
|
+
rank: new Int32Array(COMBOS),
|
|
1087
|
+
position: new Int32Array(COMBOS)
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
function pairValues(ctx, node, board, traverser, hands, handReach, live, liveCount) {
|
|
1091
|
+
const result = new Float64Array(hands.length * COMBOS);
|
|
1092
|
+
if (hands.length === 0) return result;
|
|
1093
|
+
const width = Math.min(CHUNK, hands.length);
|
|
1094
|
+
const buffers = makeBuffers(ctx, width);
|
|
1095
|
+
const mine = new Uint16Array(liveCount);
|
|
1096
|
+
let mineCount = 0;
|
|
1097
|
+
for (let i = 0; i < liveCount; i++) if (ctx.players[traverser].slot[live[i]] >= 0) mine[mineCount++] = live[i];
|
|
1098
|
+
for (let start = 0; start < hands.length; start += width) {
|
|
1099
|
+
const chunk = hands.subarray(start, Math.min(hands.length, start + width));
|
|
1100
|
+
const colReach = buffers.colReach[0];
|
|
1101
|
+
const cols = buffers.cols[0];
|
|
1102
|
+
let count = 0;
|
|
1103
|
+
for (let k = 0; k < chunk.length; k++) {
|
|
1104
|
+
colReach[k] = handReach[start + k];
|
|
1105
|
+
if (colReach[k] !== 0) cols[count++] = k;
|
|
1106
|
+
}
|
|
1107
|
+
const value = walk(ctx, buffers, traverser, chunk, node, board, colReach, cols, count, mine, mineCount, 0);
|
|
1108
|
+
for (let c = 0; c < count; c++) {
|
|
1109
|
+
const k = cols[c];
|
|
1110
|
+
const row = value.subarray(k * COMBOS, (k + 1) * COMBOS);
|
|
1111
|
+
for (let i = 0; i < mineCount; i++) result[(start + k) * COMBOS + mine[i]] = row[mine[i]];
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
return result;
|
|
1115
|
+
}
|
|
1116
|
+
function walk(ctx, buffers, traverser, hands, node, board, colReach, cols, colCount, live, liveCount, depth) {
|
|
1117
|
+
const { tree, runouts } = ctx;
|
|
1118
|
+
const out = buffers.value[depth];
|
|
1119
|
+
const kind = tree.kind[node];
|
|
1120
|
+
const cards = COMBO_CARDS;
|
|
1121
|
+
for (let c = 0; c < colCount; c++) {
|
|
1122
|
+
const row = cols[c] * COMBOS;
|
|
1123
|
+
for (let i = 0; i < liveCount; i++) out[row + live[i]] = 0;
|
|
1124
|
+
}
|
|
1125
|
+
if (colCount === 0) return out;
|
|
1126
|
+
if (kind === NodeKind.TerminalFold) {
|
|
1127
|
+
const signed = traverser === 0 ? tree.payoff[node] : -tree.payoff[node];
|
|
1128
|
+
const weight = signed * ctx.runoutWeight[tree.round[node]] / ctx.chipScale;
|
|
1129
|
+
for (let c = 0; c < colCount; c++) {
|
|
1130
|
+
const k = cols[c];
|
|
1131
|
+
const a = cards[hands[k] * 2];
|
|
1132
|
+
const b = cards[hands[k] * 2 + 1];
|
|
1133
|
+
const v = weight * colReach[k];
|
|
1134
|
+
const row = k * COMBOS;
|
|
1135
|
+
for (let i = 0; i < liveCount; i++) {
|
|
1136
|
+
const combo = live[i];
|
|
1137
|
+
const x = cards[combo * 2];
|
|
1138
|
+
const y = cards[combo * 2 + 1];
|
|
1139
|
+
if (x !== a && x !== b && y !== a && y !== b) out[row + combo] = v;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
return out;
|
|
1143
|
+
}
|
|
1144
|
+
if (kind === NodeKind.TerminalShowdown) {
|
|
1145
|
+
const weight = tree.payoff[node] * ctx.runoutWeight[tree.round[node]] / ctx.chipScale;
|
|
1146
|
+
const table = ctx.showdown;
|
|
1147
|
+
const rank = buffers.rank;
|
|
1148
|
+
const orderBase = board * table.stride;
|
|
1149
|
+
const groupBase = board * (table.stride + 1);
|
|
1150
|
+
for (let g = 0; g < table.groupCount[board]; g++) {
|
|
1151
|
+
for (let i = table.groupStart[groupBase + g]; i < table.groupStart[groupBase + g + 1]; i++) {
|
|
1152
|
+
rank[table.order[orderBase + i]] = g;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
for (let c = 0; c < colCount; c++) {
|
|
1156
|
+
const k = cols[c];
|
|
1157
|
+
const theirs = hands[k];
|
|
1158
|
+
const a = cards[theirs * 2];
|
|
1159
|
+
const b = cards[theirs * 2 + 1];
|
|
1160
|
+
const strength = rank[theirs];
|
|
1161
|
+
const v = weight * colReach[k];
|
|
1162
|
+
const row = k * COMBOS;
|
|
1163
|
+
for (let i = 0; i < liveCount; i++) {
|
|
1164
|
+
const combo = live[i];
|
|
1165
|
+
const x = cards[combo * 2];
|
|
1166
|
+
const y = cards[combo * 2 + 1];
|
|
1167
|
+
if (x === a || x === b || y === a || y === b) continue;
|
|
1168
|
+
const mine = rank[combo];
|
|
1169
|
+
if (mine > strength) out[row + combo] = v;
|
|
1170
|
+
else if (mine < strength) out[row + combo] = -v;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
return out;
|
|
1174
|
+
}
|
|
1175
|
+
if (kind === NodeKind.Chance) {
|
|
1176
|
+
const nextRound = tree.round[node] + 1;
|
|
1177
|
+
const child = tree.edgeChild[tree.firstEdge[node]];
|
|
1178
|
+
const width = runouts.width[nextRound];
|
|
1179
|
+
const dealtCards = runouts.cards[nextRound];
|
|
1180
|
+
const orbitStart = runouts.orbitStart[nextRound];
|
|
1181
|
+
const childLive = buffers.live[depth];
|
|
1182
|
+
const childCols2 = buffers.cols[depth + 1];
|
|
1183
|
+
const mappedHands = buffers.hands[depth];
|
|
1184
|
+
for (let childBoard = runouts.childStart[nextRound][board]; childBoard < runouts.childStart[nextRound][board + 1]; childBoard++) {
|
|
1185
|
+
for (let m = orbitStart[childBoard]; m < orbitStart[childBoard + 1]; m++) {
|
|
1186
|
+
const dealt = m === orbitStart[childBoard] ? dealtCards[childBoard * width + width - 1] : runouts.orbitCard[nextRound][m];
|
|
1187
|
+
const map = runouts.maps.combo[runouts.orbitMap[nextRound][m]];
|
|
1188
|
+
let childCount = 0;
|
|
1189
|
+
for (let i = 0; i < liveCount; i++) {
|
|
1190
|
+
const combo = live[i];
|
|
1191
|
+
if (cards[combo * 2] === dealt || cards[combo * 2 + 1] === dealt) continue;
|
|
1192
|
+
childLive[childCount++] = map[combo];
|
|
1193
|
+
}
|
|
1194
|
+
let childColCount = 0;
|
|
1195
|
+
for (let c = 0; c < colCount; c++) {
|
|
1196
|
+
const k = cols[c];
|
|
1197
|
+
if (cards[hands[k] * 2] !== dealt && cards[hands[k] * 2 + 1] !== dealt) childCols2[childColCount++] = k;
|
|
1198
|
+
}
|
|
1199
|
+
if (childColCount === 0) continue;
|
|
1200
|
+
for (let k = 0; k < hands.length; k++) mappedHands[k] = map[hands[k]];
|
|
1201
|
+
const value = walk(
|
|
1202
|
+
ctx,
|
|
1203
|
+
buffers,
|
|
1204
|
+
traverser,
|
|
1205
|
+
mappedHands.subarray(0, hands.length),
|
|
1206
|
+
child,
|
|
1207
|
+
childBoard,
|
|
1208
|
+
colReach,
|
|
1209
|
+
childCols2,
|
|
1210
|
+
childColCount,
|
|
1211
|
+
childLive,
|
|
1212
|
+
childCount,
|
|
1213
|
+
depth + 1
|
|
1214
|
+
);
|
|
1215
|
+
for (let c = 0; c < childColCount; c++) {
|
|
1216
|
+
const row = childCols2[c] * COMBOS;
|
|
1217
|
+
for (let i = 0; i < liveCount; i++) {
|
|
1218
|
+
const combo = live[i];
|
|
1219
|
+
if (cards[combo * 2] === dealt || cards[combo * 2 + 1] === dealt) continue;
|
|
1220
|
+
out[row + combo] += value[row + map[combo]];
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
return out;
|
|
1226
|
+
}
|
|
1227
|
+
const actions = tree.numEdges[node];
|
|
1228
|
+
const edgeBase = tree.firstEdge[node];
|
|
1229
|
+
const owner = tree.player[node];
|
|
1230
|
+
const lists = ctx.active;
|
|
1231
|
+
const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
|
|
1232
|
+
const active = lists.list;
|
|
1233
|
+
const activeFrom = lists.start[listSlot];
|
|
1234
|
+
const activeCount = lists.count[listSlot];
|
|
1235
|
+
const stride = activeCount;
|
|
1236
|
+
const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1237
|
+
const strategy = buffers.strategy[depth];
|
|
1238
|
+
averageStrategy(ctx.strategySum, slotBase, actions, stride, strategy, ctx.regret);
|
|
1239
|
+
if (owner === traverser) {
|
|
1240
|
+
for (let a = 0; a < actions; a++) {
|
|
1241
|
+
const offset = a * COMBOS;
|
|
1242
|
+
const value = walk(
|
|
1243
|
+
ctx,
|
|
1244
|
+
buffers,
|
|
1245
|
+
traverser,
|
|
1246
|
+
hands,
|
|
1247
|
+
tree.edgeChild[edgeBase + a],
|
|
1248
|
+
board,
|
|
1249
|
+
colReach,
|
|
1250
|
+
cols,
|
|
1251
|
+
colCount,
|
|
1252
|
+
live,
|
|
1253
|
+
liveCount,
|
|
1254
|
+
depth + 1
|
|
1255
|
+
);
|
|
1256
|
+
for (let c = 0; c < colCount; c++) {
|
|
1257
|
+
const row = cols[c] * COMBOS;
|
|
1258
|
+
for (let i = 0; i < activeCount; i++) {
|
|
1259
|
+
const combo = active[activeFrom + i];
|
|
1260
|
+
out[row + combo] += strategy[a * stride + i] * value[row + combo];
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
return out;
|
|
1265
|
+
}
|
|
1266
|
+
const childReach = buffers.colReach[depth + 1];
|
|
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;
|
|
1270
|
+
for (let a = 0; a < actions; a++) {
|
|
1271
|
+
const offset = a * COMBOS;
|
|
1272
|
+
let childCount = 0;
|
|
1273
|
+
for (let c = 0; c < colCount; c++) {
|
|
1274
|
+
const k = cols[c];
|
|
1275
|
+
const r = colReach[k] * strategy[a * stride + position[hands[k]]];
|
|
1276
|
+
childReach[k] = r;
|
|
1277
|
+
if (r !== 0) childCols[childCount++] = k;
|
|
1278
|
+
}
|
|
1279
|
+
if (childCount === 0) continue;
|
|
1280
|
+
const value = walk(
|
|
1281
|
+
ctx,
|
|
1282
|
+
buffers,
|
|
1283
|
+
traverser,
|
|
1284
|
+
hands,
|
|
1285
|
+
tree.edgeChild[edgeBase + a],
|
|
1286
|
+
board,
|
|
1287
|
+
childReach,
|
|
1288
|
+
childCols,
|
|
1289
|
+
childCount,
|
|
1290
|
+
live,
|
|
1291
|
+
liveCount,
|
|
1292
|
+
depth + 1
|
|
1293
|
+
);
|
|
1294
|
+
for (let c = 0; c < childCount; c++) {
|
|
1295
|
+
const row = childCols[c] * COMBOS;
|
|
1296
|
+
for (let i = 0; i < liveCount; i++) out[row + live[i]] += value[row + live[i]];
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
return out;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
// src/wasm/kernels.ts
|
|
1303
|
+
var KERNELS_WASM = "AGFzbQEAAAABEwJgBX9/f39/AGAHf39/f39/fwACDwEDZW52Bm1lbW9yeQIAAAMDAgABBycDC3JlZ3JldE1hdGNoAAAMYXZlcmFnZU1hdGNoAAEGbWVtb3J5AgAK0AkC8AMDB38BfAJ7IARBfnEhCANAIAQgBUoEQCACIAVBA3RqRAAAAAAAAAAAOQMAIAVBAWohBQwBCwsDQCADIAZKBEAgBCAGbEEDdCEJQQAhBQNAIAUgCEgEQCAFQQN0IgogCWoiCyAAav0ABAD9DAAAAAAAAAAAAAAAAAAAAAD99QEhDSABIAtqIA39CwQAIAIgCmoiCiAK/QAEACAN/fAB/QsEACAFQQJqIQUMAQsLA0AgBCAFSgRAIAVBA3QiCiAAIAlqaisDACIMRAAAAAAAAAAAIAxEAAAAAAAAAABkGyEMIAEgCWogCmogDDkDACACIApqIgogCisDACAMoDkDACAFQQFqIQUMAQsLIAZBAWohBgwBCwtEAAAAAAAA8D8gA7ej/RQhDgNAIAMgB0oEQCAEIAdsQQN0IQVBACEAA0AgACAISARAIABBA3QiBiABIAVqaiIJIAn9AAQAIAIgBmr9AAQAIg398wEgDiAN/QwAAAAAAAAAAAAAAAAAAAAA/Ur9Uv0LBAAgAEECaiEADAELCwNAIAAgBEgEQCAAQQN0IgYgASAFamoiCSACIAZqKwMAIgxEAAAAAAAAAABkBHwgCSsDACAMowVEAAAAAAAA8D8gA7ejCzkDACAAQQFqIQAMAQsLIAdBAWohBwwBCwsL2wUDCH8BfAN7IAVBfnEhCyADIAVBA3RqIQkDQCAFIAdKBEAgB0EDdCIMIANqRAAAAAAAAAAAOQMAIAkgDGpEAAAAAAAAAAA5AwAgB0EBaiEHDAELCwNAIAQgCEoEQCAFIAhsQQN0IQxBACEHA0AgByALSARAIAdBA3QiDSADaiIOIA79AAQAIAAgDGogDWr9AAQA/fAB/QsEACAGBEAgCSANaiIOIA79AAQAIAEgDGogDWr9AAQA/QwAAAAAAAAAAAAAAAAAAAAA/fUB/fAB/QsEAAsgB0ECaiEHDAELCwNAIAUgB0oEQCAHQQN0Ig0gA2oiDiAOKwMAIAAgDGogDWorAwCgOQMAIAYEQCAJIA1qIg4gDisDACABIAxqIA1qKwMAIg9EAAAAAAAAAAAgD0QAAAAAAAAAAGQboDkDAAsgB0EBaiEHDAELCyAIQQFqIQgMAQsLRAAAAAAAAPA/IAS3o/0UIRADQCAEIApKBEAgBSAKbEEDdCEHQQAhCANAIAggC0gEQCAIQQN0IgwgA2r9AAQAIREgAiAHaiAMaiAAIAdqIAxq/QAEACAR/fMBIAYEeyABIAdqIAxq/QAEAP0MAAAAAAAAAAAAAAAAAAAAAP31ASAJIAxq/QAEACIS/fMBIBAgEv0MAAAAAAAAAAAAAAAAAAAAAP1K/VIFIBALIBH9DAAAAAAAAAAAAAAAAAAAAAD9Sv1S/QsEACAIQQJqIQgMAQsLA0AgBSAISgRAIAhBA3QiDCACIAdqaiADIAxqKwMAIg9EAAAAAAAAAABkBHwgACAHaiAMaisDACAPowUgBgR/IAkgCEEDdGorAwBEAAAAAAAAAABkBUEACwR8IAhBA3QiDCABIAdqaisDACIPRAAAAAAAAAAAIA9EAAAAAAAAAABkGyAJIAxqKwMAowVEAAAAAAAA8D8gBLejCws5AwAgCEEBaiEIDAELCyAKQQFqIQoMAQsLCw==";
|
|
1304
|
+
|
|
1305
|
+
// src/kernels.ts
|
|
1306
|
+
var wasm = globalThis.WebAssembly;
|
|
1307
|
+
var module;
|
|
1308
|
+
function compiled() {
|
|
1309
|
+
if (module !== void 0) return module;
|
|
1310
|
+
module = null;
|
|
1311
|
+
try {
|
|
1312
|
+
if (!wasm) return null;
|
|
1313
|
+
const bytes = Uint8Array.from(atob(KERNELS_WASM), (c) => c.charCodeAt(0));
|
|
1314
|
+
if (!wasm.validate(bytes)) return null;
|
|
1315
|
+
module = new wasm.Module(bytes);
|
|
1316
|
+
} catch {
|
|
1317
|
+
module = null;
|
|
1318
|
+
}
|
|
1319
|
+
return module;
|
|
1320
|
+
}
|
|
1321
|
+
function loadKernels(depths, width) {
|
|
1322
|
+
const compiledModule = compiled();
|
|
1323
|
+
if (!compiledModule || !wasm) return null;
|
|
1324
|
+
const second = width;
|
|
1325
|
+
const totals = 2 * width;
|
|
1326
|
+
const first = 4 * width;
|
|
1327
|
+
const bytes = (first + depths * width) * 8;
|
|
1328
|
+
const memory = new wasm.Memory({ initial: Math.ceil(bytes / 65536) + 1 });
|
|
1329
|
+
const instance = new wasm.Instance(compiledModule, { env: { memory } });
|
|
1330
|
+
const regretMatch2 = instance.exports.regretMatch;
|
|
1331
|
+
const averageMatch = instance.exports.averageMatch;
|
|
1332
|
+
const heap = new Float64Array(memory.buffer);
|
|
1333
|
+
const input = heap.subarray(0, width);
|
|
1334
|
+
const secondInput = heap.subarray(second, second + width);
|
|
1335
|
+
const buffers = Array.from(
|
|
1336
|
+
{ length: depths },
|
|
1337
|
+
(_, depth) => heap.subarray(first + depth * width, first + (depth + 1) * width)
|
|
1338
|
+
);
|
|
1339
|
+
return {
|
|
1340
|
+
input,
|
|
1341
|
+
second: secondInput,
|
|
1342
|
+
strategy: (depth) => buffers[depth],
|
|
1343
|
+
regretMatch(out, actions, count) {
|
|
1344
|
+
regretMatch2(0, out.byteOffset, totals * 8, actions, count);
|
|
1345
|
+
},
|
|
1346
|
+
averageMatch(out, actions, count, hasRegret) {
|
|
1347
|
+
averageMatch(0, second * 8, out.byteOffset, totals * 8, actions, count, hasRegret ? 1 : 0);
|
|
1348
|
+
}
|
|
1349
|
+
};
|
|
1350
|
+
}
|
|
1351
|
+
function simdAvailable() {
|
|
1352
|
+
return compiled() !== null;
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
// src/cfr.ts
|
|
1356
|
+
function buildPlayerIndex(weights) {
|
|
1357
|
+
const combos = [];
|
|
1358
|
+
const slot = new Int32Array(COMBOS).fill(-1);
|
|
1359
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
1360
|
+
if (weights[combo] > 0) {
|
|
1361
|
+
slot[combo] = combos.length;
|
|
1362
|
+
combos.push(combo);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
return { combos: Uint16Array.from(combos), count: combos.length, slot, weights };
|
|
1366
|
+
}
|
|
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
|
+
}
|
|
1391
|
+
const offset = new Int32Array(tree.nodeCount).fill(-1);
|
|
1392
|
+
const blockOffset = new Int32Array(tree.nodeCount).fill(-1);
|
|
1393
|
+
let total = 0;
|
|
1394
|
+
let blocks = 0;
|
|
1395
|
+
let maxActions = 0;
|
|
1396
|
+
for (let i = 0; i < tree.decisionNodes.length; i++) {
|
|
1397
|
+
const node = tree.decisionNodes[i];
|
|
1398
|
+
const actions = tree.numEdges[node];
|
|
1399
|
+
if (actions > maxActions) maxActions = actions;
|
|
1400
|
+
offset[node] = total;
|
|
1401
|
+
blockOffset[node] = blocks;
|
|
1402
|
+
total += actions * totals[tree.round[node] * 2 + tree.player[node]];
|
|
1403
|
+
blocks += runouts.counts[tree.round[node]];
|
|
1404
|
+
}
|
|
1405
|
+
const scales = bytesPerElement === 2 ? blocks * 8 * 2 : 0;
|
|
1406
|
+
return {
|
|
1407
|
+
offset,
|
|
1408
|
+
blockOffset,
|
|
1409
|
+
blocks,
|
|
1410
|
+
totalSlots: total,
|
|
1411
|
+
maxActions,
|
|
1412
|
+
rowStart,
|
|
1413
|
+
roundBase,
|
|
1414
|
+
bytes: total * bytesPerElement * 2 + scales
|
|
1415
|
+
};
|
|
1416
|
+
}
|
|
1417
|
+
var REGRET_STEPS = 32767;
|
|
1418
|
+
var SUM_STEPS = 65535;
|
|
1419
|
+
function runoutWeights(runouts) {
|
|
1420
|
+
const weights = new Float64Array(runouts.rounds).fill(1);
|
|
1421
|
+
for (let round = 0; round < runouts.rounds; round++) {
|
|
1422
|
+
let weight = 1;
|
|
1423
|
+
for (let r = round + 1; r < runouts.rounds; r++) weight *= DECK - 4 - runouts.width[r - 1];
|
|
1424
|
+
weights[round] = weight;
|
|
1425
|
+
}
|
|
1426
|
+
return weights;
|
|
1427
|
+
}
|
|
1428
|
+
function matchupMass(a, b) {
|
|
1429
|
+
const single = new Float64Array(DECK);
|
|
1430
|
+
const pair = new Float64Array(COMBOS);
|
|
1431
|
+
let totalB = 0;
|
|
1432
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
1433
|
+
const w = b[combo];
|
|
1434
|
+
if (w === 0) continue;
|
|
1435
|
+
totalB += w;
|
|
1436
|
+
single[COMBO_CARDS[combo * 2]] += w;
|
|
1437
|
+
single[COMBO_CARDS[combo * 2 + 1]] += w;
|
|
1438
|
+
pair[combo] += w;
|
|
1439
|
+
}
|
|
1440
|
+
let mass = 0;
|
|
1441
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
1442
|
+
const w = a[combo];
|
|
1443
|
+
if (w === 0) continue;
|
|
1444
|
+
const clash = single[COMBO_CARDS[combo * 2]] + single[COMBO_CARDS[combo * 2 + 1]] - pair[combo];
|
|
1445
|
+
mass += w * (totalB - clash);
|
|
1446
|
+
}
|
|
1447
|
+
return mass;
|
|
1448
|
+
}
|
|
1449
|
+
function buildActiveLists(runouts, players, showdown = null) {
|
|
1450
|
+
const roundBase = new Int32Array(runouts.rounds + 1);
|
|
1451
|
+
for (let round = 0; round < runouts.rounds; round++) {
|
|
1452
|
+
roundBase[round + 1] = roundBase[round] + runouts.counts[round];
|
|
1453
|
+
}
|
|
1454
|
+
const slots = roundBase[runouts.rounds] * 2;
|
|
1455
|
+
const start = new Int32Array(slots);
|
|
1456
|
+
const count = new Uint16Array(slots);
|
|
1457
|
+
const entries = [];
|
|
1458
|
+
const entrySlots = [];
|
|
1459
|
+
const onBoard = new Uint8Array(DECK);
|
|
1460
|
+
for (let round = 0; round < runouts.rounds; round++) {
|
|
1461
|
+
const width = runouts.width[round];
|
|
1462
|
+
const cards = runouts.cards[round];
|
|
1463
|
+
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1464
|
+
onBoard.fill(0);
|
|
1465
|
+
for (let i = 0; i < width; i++) onBoard[cards[board * width + i]] = 1;
|
|
1466
|
+
for (let player = 0; player < 2; player++) {
|
|
1467
|
+
const index = players[player];
|
|
1468
|
+
const slot = (roundBase[round] + board) * 2 + player;
|
|
1469
|
+
start[slot] = entries.length;
|
|
1470
|
+
const here = [];
|
|
1471
|
+
for (let i = 0; i < index.count; i++) {
|
|
1472
|
+
const combo = index.combos[i];
|
|
1473
|
+
if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
|
|
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]);
|
|
1482
|
+
entrySlots.push(i);
|
|
1483
|
+
}
|
|
1484
|
+
count[slot] = entries.length - start[slot];
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
const tieStart = new Int32Array(slots + 1);
|
|
1489
|
+
const ties = [];
|
|
1490
|
+
for (let round = 0; round < runouts.rounds; round++) {
|
|
1491
|
+
const stabStart = runouts.stabiliserStart[round];
|
|
1492
|
+
const stabMap = runouts.stabiliserMap[round];
|
|
1493
|
+
for (let board = 0; board < runouts.counts[round]; board++) {
|
|
1494
|
+
for (let player = 0; player < 2; player++) {
|
|
1495
|
+
const slot = (roundBase[round] + board) * 2 + player;
|
|
1496
|
+
tieStart[slot] = ties.length;
|
|
1497
|
+
if (stabStart[board + 1] - stabStart[board] < 2) continue;
|
|
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]);
|
|
1501
|
+
for (let e = start[slot]; e < start[slot] + count[slot]; e++) {
|
|
1502
|
+
if (seen[e - start[slot]]) continue;
|
|
1503
|
+
const members = [];
|
|
1504
|
+
for (let k = stabStart[board]; k < stabStart[board + 1]; k++) {
|
|
1505
|
+
const image = position.get(runouts.maps.combo[stabMap[k]][entries[e]]);
|
|
1506
|
+
if (image === void 0 || seen[image]) continue;
|
|
1507
|
+
seen[image] = 1;
|
|
1508
|
+
members.push(image);
|
|
1509
|
+
}
|
|
1510
|
+
if (members.length > 1) ties.push(members.length, ...members);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
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
|
+
}
|
|
1534
|
+
return {
|
|
1535
|
+
start,
|
|
1536
|
+
count,
|
|
1537
|
+
list,
|
|
1538
|
+
slot: Uint16Array.from(entrySlots),
|
|
1539
|
+
roundBase,
|
|
1540
|
+
tieStart,
|
|
1541
|
+
ties: Int32Array.from(ties),
|
|
1542
|
+
cardA,
|
|
1543
|
+
cardB,
|
|
1544
|
+
strength
|
|
1545
|
+
};
|
|
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
|
+
}
|
|
1558
|
+
function tieMirrors(store, slotBase, actions, stride, ties, from, to, integer) {
|
|
1559
|
+
for (let a = 0; a < actions; a++) {
|
|
1560
|
+
const row = slotBase + a * stride;
|
|
1561
|
+
for (let k = from; k < to; ) {
|
|
1562
|
+
const size = ties[k];
|
|
1563
|
+
let sum = 0;
|
|
1564
|
+
for (let j = 1; j <= size; j++) sum += store[row + ties[k + j]];
|
|
1565
|
+
let mean = sum / size;
|
|
1566
|
+
if (integer) mean = Math.round(mean);
|
|
1567
|
+
for (let j = 1; j <= size; j++) store[row + ties[k + j]] = mean;
|
|
1568
|
+
k += size + 1;
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
function tieBlock(ctx, node, board) {
|
|
1573
|
+
const { tree } = ctx;
|
|
1574
|
+
const owner = tree.player[node];
|
|
1575
|
+
const lists = ctx.active;
|
|
1576
|
+
const listSlot = (lists.roundBase[tree.round[node]] + board) * 2 + owner;
|
|
1577
|
+
const from = lists.tieStart[listSlot];
|
|
1578
|
+
const to = lists.tieStart[listSlot + 1];
|
|
1579
|
+
if (from === to) return;
|
|
1580
|
+
const actions = tree.numEdges[node];
|
|
1581
|
+
const count = lists.count[listSlot];
|
|
1582
|
+
const base = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1583
|
+
const integer = ctx.regretScale !== null;
|
|
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);
|
|
1586
|
+
}
|
|
1587
|
+
function makeBuffers2(tree, maxActions, simd) {
|
|
1588
|
+
const depth = treeDepth2(tree) + 2;
|
|
1589
|
+
const make = (factory) => Array.from({ length: depth }, factory);
|
|
1590
|
+
const kernels = simd ? loadKernels(depth, maxActions * COMBOS) : null;
|
|
1591
|
+
return {
|
|
1592
|
+
kernels,
|
|
1593
|
+
cardSums: new Float64Array(DECK),
|
|
1594
|
+
value: make(() => new Float64Array(COMBOS)),
|
|
1595
|
+
reachFor: [make(() => new Float64Array(COMBOS)), make(() => new Float64Array(COMBOS))],
|
|
1596
|
+
strategy: kernels ? Array.from({ length: depth }, (_, d) => kernels.strategy(d)) : make(() => new Float64Array(maxActions * COMBOS)),
|
|
1597
|
+
actionValues: make(() => new Float64Array(maxActions * COMBOS)),
|
|
1598
|
+
live: make(() => new Uint16Array(COMBOS)),
|
|
1599
|
+
mappedMe: make(() => new Float64Array(COMBOS)),
|
|
1600
|
+
mappedOpp: make(() => new Float64Array(COMBOS)),
|
|
1601
|
+
mappedLive: make(() => new Uint16Array(COMBOS))
|
|
1602
|
+
};
|
|
1603
|
+
}
|
|
1604
|
+
function treeDepth2(tree) {
|
|
1605
|
+
const depth = new Int32Array(tree.nodeCount).fill(-1);
|
|
1606
|
+
depth[0] = 1;
|
|
1607
|
+
let max = 1;
|
|
1608
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
1609
|
+
const here = depth[node];
|
|
1610
|
+
if (here < 0) continue;
|
|
1611
|
+
if (here > max) max = here;
|
|
1612
|
+
for (let e = 0; e < tree.numEdges[node]; e++) {
|
|
1613
|
+
const child = tree.edgeChild[tree.firstEdge[node] + e];
|
|
1614
|
+
if (depth[child] < here + 1) depth[child] = here + 1;
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
return max;
|
|
1618
|
+
}
|
|
1619
|
+
var DCFR_ALPHA = 1.5;
|
|
1620
|
+
var DCFR_BETA = 0;
|
|
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
|
+
}
|
|
1632
|
+
var Mode = { Train: 0, BestResponse: 1, Average: 2 };
|
|
1633
|
+
function normalise(source, base, actions, count, strategy) {
|
|
1634
|
+
const uniform = 1 / actions;
|
|
1635
|
+
for (let i = 0; i < count; i++) {
|
|
1636
|
+
const at = base + i;
|
|
1637
|
+
let total = 0;
|
|
1638
|
+
for (let a = 0; a < actions; a++) {
|
|
1639
|
+
const value = source[at + a * count];
|
|
1640
|
+
const positive = value > 0 ? value : 0;
|
|
1641
|
+
strategy[a * count + i] = positive;
|
|
1642
|
+
total += positive;
|
|
1643
|
+
}
|
|
1644
|
+
if (total > 0) {
|
|
1645
|
+
for (let a = 0; a < actions; a++) strategy[a * count + i] /= total;
|
|
1646
|
+
} else {
|
|
1647
|
+
for (let a = 0; a < actions; a++) strategy[a * count + i] = uniform;
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
function regretMatch(regret, base, actions, count, strategy) {
|
|
1652
|
+
normalise(regret, base, actions, count, strategy);
|
|
1653
|
+
}
|
|
1654
|
+
function averageStrategy(strategySum, base, actions, count, strategy, regret) {
|
|
1655
|
+
const fallback = regret !== void 0 && regret.length > 0 ? regret : null;
|
|
1656
|
+
const uniform = 1 / actions;
|
|
1657
|
+
for (let i = 0; i < count; i++) {
|
|
1658
|
+
const at = base + i;
|
|
1659
|
+
let total = 0;
|
|
1660
|
+
for (let a = 0; a < actions; a++) {
|
|
1661
|
+
const value = strategySum[at + a * count];
|
|
1662
|
+
strategy[a * count + i] = value;
|
|
1663
|
+
total += value;
|
|
1664
|
+
}
|
|
1665
|
+
if (total > 0) {
|
|
1666
|
+
for (let a = 0; a < actions; a++) strategy[a * count + i] /= total;
|
|
1667
|
+
continue;
|
|
1668
|
+
}
|
|
1669
|
+
if (fallback !== null) {
|
|
1670
|
+
for (let a = 0; a < actions; a++) {
|
|
1671
|
+
const value = fallback[at + a * count];
|
|
1672
|
+
const positive = value > 0 ? value : 0;
|
|
1673
|
+
strategy[a * count + i] = positive;
|
|
1674
|
+
total += positive;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
if (total > 0) {
|
|
1678
|
+
for (let a = 0; a < actions; a++) strategy[a * count + i] /= total;
|
|
1679
|
+
} else {
|
|
1680
|
+
for (let a = 0; a < actions; a++) strategy[a * count + i] = uniform;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
function traverse(ctx, buffers, mode, traverser, iterWeight, node, board, reachMe, reachOpp, depth) {
|
|
1685
|
+
const { tree, runouts } = ctx;
|
|
1686
|
+
const out = buffers.value[depth];
|
|
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];
|
|
1697
|
+
if (kind === NodeKind.TerminalFold) {
|
|
1698
|
+
const signed = traverser === 0 ? tree.payoff[node] : -tree.payoff[node];
|
|
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
|
+
);
|
|
1715
|
+
return out;
|
|
1716
|
+
}
|
|
1717
|
+
if (kind === NodeKind.TerminalShowdown) {
|
|
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
|
+
);
|
|
1732
|
+
return out;
|
|
1733
|
+
}
|
|
1734
|
+
if (kind === NodeKind.Chance) {
|
|
1735
|
+
for (let i = 0; i < myCount; i++) out[i] = 0;
|
|
1736
|
+
const nextRound = round + 1;
|
|
1737
|
+
if (ctx.entryIndexOf !== null && nextRound === ctx.shardRound) {
|
|
1738
|
+
const entry = ctx.entryIndexOf[node];
|
|
1739
|
+
if (entry >= 0) {
|
|
1740
|
+
if (ctx.chanceCollect !== null) {
|
|
1741
|
+
ctx.chanceCollect(node, reachMe, reachOpp);
|
|
1742
|
+
return out;
|
|
1743
|
+
}
|
|
1744
|
+
if (ctx.chanceOverride !== null) {
|
|
1745
|
+
const base = entry * COMBOS;
|
|
1746
|
+
for (let i = 0; i < myCount; i++) out[i] = ctx.chanceOverride[base + i];
|
|
1747
|
+
return out;
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
const child = tree.edgeChild[tree.firstEdge[node]];
|
|
1752
|
+
const orbitStart = runouts.orbitStart[nextRound];
|
|
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];
|
|
1757
|
+
const sharded = nextRound === ctx.shardRound;
|
|
1758
|
+
const first = runouts.childStart[nextRound][board];
|
|
1759
|
+
const last = runouts.childStart[nextRound][board + 1];
|
|
1760
|
+
const from = sharded && ctx.shardStart > first ? ctx.shardStart : first;
|
|
1761
|
+
const to = sharded && ctx.shardEnd < last ? ctx.shardEnd : last;
|
|
1762
|
+
for (let childBoard = from; childBoard < to; childBoard++) {
|
|
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];
|
|
1768
|
+
const membersEnd = orbitStart[childBoard + 1];
|
|
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);
|
|
1777
|
+
}
|
|
1778
|
+
for (let k = 0; k < nMe; k++) out[up[upMe + k]] += value[k];
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
return out;
|
|
1782
|
+
}
|
|
1783
|
+
const actions = tree.numEdges[node];
|
|
1784
|
+
const edgeBase = tree.firstEdge[node];
|
|
1785
|
+
const owner = tree.player[node];
|
|
1786
|
+
const isTraverser = owner === traverser;
|
|
1787
|
+
const listSlot = isTraverser ? mySlot : oppSlot;
|
|
1788
|
+
const activeCount = lists.count[listSlot];
|
|
1789
|
+
const stride = activeCount;
|
|
1790
|
+
const slotBase = ctx.layout.offset[node] + actions * ctx.layout.rowStart[listSlot];
|
|
1791
|
+
const strategy = buffers.strategy[depth];
|
|
1792
|
+
const length = actions * stride;
|
|
1793
|
+
if (mode === Mode.Train && buffers.kernels !== null) {
|
|
1794
|
+
const kernels = buffers.kernels;
|
|
1795
|
+
kernels.input.set(ctx.regret.subarray(slotBase, slotBase + length));
|
|
1796
|
+
kernels.regretMatch(strategy, actions, stride);
|
|
1797
|
+
} else if (mode === Mode.Train) {
|
|
1798
|
+
regretMatch(ctx.regret, slotBase, actions, stride, strategy);
|
|
1799
|
+
} else if (buffers.kernels !== null) {
|
|
1800
|
+
const kernels = buffers.kernels;
|
|
1801
|
+
kernels.input.set(ctx.strategySum.subarray(slotBase, slotBase + length));
|
|
1802
|
+
const hasRegret = ctx.regret.length > 0;
|
|
1803
|
+
if (hasRegret) kernels.second.set(ctx.regret.subarray(slotBase, slotBase + length));
|
|
1804
|
+
kernels.averageMatch(strategy, actions, stride, hasRegret);
|
|
1805
|
+
} else {
|
|
1806
|
+
averageStrategy(ctx.strategySum, slotBase, actions, stride, strategy, ctx.regret);
|
|
1807
|
+
}
|
|
1808
|
+
if (isTraverser && mode === Mode.BestResponse) {
|
|
1809
|
+
for (let a = 0; a < actions; a++) {
|
|
1810
|
+
const value = traverse(
|
|
1811
|
+
ctx,
|
|
1812
|
+
buffers,
|
|
1813
|
+
mode,
|
|
1814
|
+
traverser,
|
|
1815
|
+
iterWeight,
|
|
1816
|
+
tree.edgeChild[edgeBase + a],
|
|
1817
|
+
board,
|
|
1818
|
+
reachMe,
|
|
1819
|
+
reachOpp,
|
|
1820
|
+
depth + 1
|
|
1821
|
+
);
|
|
1822
|
+
if (a === 0) {
|
|
1823
|
+
for (let i = 0; i < myCount; i++) out[i] = value[i];
|
|
1824
|
+
} else {
|
|
1825
|
+
for (let i = 0; i < myCount; i++) if (value[i] > out[i]) out[i] = value[i];
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
return out;
|
|
1829
|
+
}
|
|
1830
|
+
for (let i = 0; i < myCount; i++) out[i] = 0;
|
|
1831
|
+
const childReach = buffers.reachFor[owner][depth];
|
|
1832
|
+
const actionValues = buffers.actionValues[depth];
|
|
1833
|
+
for (let a = 0; a < actions; a++) {
|
|
1834
|
+
const row = a * stride;
|
|
1835
|
+
const source = isTraverser ? reachMe : reachOpp;
|
|
1836
|
+
for (let i = 0; i < activeCount; i++) childReach[i] = source[i] * strategy[row + i];
|
|
1837
|
+
const value = traverse(
|
|
1838
|
+
ctx,
|
|
1839
|
+
buffers,
|
|
1840
|
+
mode,
|
|
1841
|
+
traverser,
|
|
1842
|
+
iterWeight,
|
|
1843
|
+
tree.edgeChild[edgeBase + a],
|
|
1844
|
+
board,
|
|
1845
|
+
isTraverser ? childReach : reachMe,
|
|
1846
|
+
isTraverser ? reachOpp : childReach,
|
|
1847
|
+
depth + 1
|
|
1848
|
+
);
|
|
1849
|
+
if (isTraverser) {
|
|
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];
|
|
1854
|
+
}
|
|
1855
|
+
} else {
|
|
1856
|
+
for (let i = 0; i < myCount; i++) out[i] += value[i];
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.regretScale !== null) {
|
|
1860
|
+
updateQuantised(
|
|
1861
|
+
ctx,
|
|
1862
|
+
ctx.layout.blockOffset[node] + board,
|
|
1863
|
+
slotBase,
|
|
1864
|
+
actions,
|
|
1865
|
+
stride,
|
|
1866
|
+
iterWeight,
|
|
1867
|
+
reachMe,
|
|
1868
|
+
strategy,
|
|
1869
|
+
actionValues,
|
|
1870
|
+
out
|
|
1871
|
+
);
|
|
1872
|
+
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates && ctx.dcfr) {
|
|
1873
|
+
const { keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight);
|
|
1874
|
+
const regret = ctx.regret;
|
|
1875
|
+
const strategySum = ctx.strategySum;
|
|
1876
|
+
for (let a = 0; a < actions; a++) {
|
|
1877
|
+
const offset = a * activeCount;
|
|
1878
|
+
const slotRow = slotBase + a * stride;
|
|
1879
|
+
const row = a * stride;
|
|
1880
|
+
for (let i = 0; i < activeCount; i++) {
|
|
1881
|
+
const at = slotRow + i;
|
|
1882
|
+
const accumulated = regret[at];
|
|
1883
|
+
regret[at] = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + i] - out[i];
|
|
1884
|
+
strategySum[at] = strategySum[at] * keepSum + reachMe[i] * strategy[row + i];
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
} else if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
1888
|
+
const regret = ctx.regret;
|
|
1889
|
+
const strategySum = ctx.strategySum;
|
|
1890
|
+
for (let a = 0; a < actions; a++) {
|
|
1891
|
+
const offset = a * activeCount;
|
|
1892
|
+
const slotRow = slotBase + a * stride;
|
|
1893
|
+
const row = a * stride;
|
|
1894
|
+
for (let i = 0; i < activeCount; i++) {
|
|
1895
|
+
const at = slotRow + i;
|
|
1896
|
+
const updated = regret[at] + actionValues[offset + i] - out[i];
|
|
1897
|
+
regret[at] = updated > 0 ? updated : 0;
|
|
1898
|
+
strategySum[at] += iterWeight * reachMe[i] * strategy[row + i];
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
if (isTraverser && mode === Mode.Train && ctx.applyUpdates) {
|
|
1903
|
+
const tieFrom = lists.tieStart[listSlot];
|
|
1904
|
+
const tieTo = lists.tieStart[listSlot + 1];
|
|
1905
|
+
if (tieFrom < tieTo) {
|
|
1906
|
+
const integer = ctx.regretScale !== null;
|
|
1907
|
+
tieMirrors(ctx.regret, slotBase, actions, stride, lists.ties, tieFrom, tieTo, integer);
|
|
1908
|
+
tieMirrors(ctx.strategySum, slotBase, actions, stride, lists.ties, tieFrom, tieTo, integer);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
return out;
|
|
1912
|
+
}
|
|
1913
|
+
function updateQuantised(ctx, block, slotBase, actions, count, iterWeight, reachMe, strategy, actionValues, out) {
|
|
1914
|
+
const regret = ctx.regret;
|
|
1915
|
+
const strategySum = ctx.strategySum;
|
|
1916
|
+
const regretScale = ctx.regretScale;
|
|
1917
|
+
const sumScale = ctx.sumScale;
|
|
1918
|
+
const oldRegret = regretScale[block];
|
|
1919
|
+
const oldSum = sumScale[block];
|
|
1920
|
+
let keepPositive = 1;
|
|
1921
|
+
let keepNegative = 0;
|
|
1922
|
+
let keepSum = 1;
|
|
1923
|
+
let weight = iterWeight;
|
|
1924
|
+
const clamp = !ctx.dcfr;
|
|
1925
|
+
if (ctx.dcfr) {
|
|
1926
|
+
({ keepPositive, keepNegative, keepSum } = dcfrFactors(iterWeight));
|
|
1927
|
+
weight = 1;
|
|
1928
|
+
}
|
|
1929
|
+
let largestRegret = 0;
|
|
1930
|
+
let largestSum = 0;
|
|
1931
|
+
for (let a = 0; a < actions; a++) {
|
|
1932
|
+
const offset = a * count;
|
|
1933
|
+
const slotRow = slotBase + a * count;
|
|
1934
|
+
for (let i = 0; i < count; i++) {
|
|
1935
|
+
const at = slotRow + i;
|
|
1936
|
+
const accumulated = regret[at] * oldRegret;
|
|
1937
|
+
let updated = accumulated * (accumulated > 0 ? keepPositive : keepNegative) + actionValues[offset + i] - out[i];
|
|
1938
|
+
if (clamp && updated < 0) updated = 0;
|
|
1939
|
+
const magnitude = updated < 0 ? -updated : updated;
|
|
1940
|
+
if (magnitude > largestRegret) largestRegret = magnitude;
|
|
1941
|
+
actionValues[offset + i] = updated;
|
|
1942
|
+
const sum = strategySum[at] * oldSum * keepSum + weight * reachMe[i] * strategy[a * count + i];
|
|
1943
|
+
if (sum > largestSum) largestSum = sum;
|
|
1944
|
+
strategy[a * count + i] = sum;
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
const regretStep = largestRegret > 0 ? REGRET_STEPS / largestRegret : 0;
|
|
1948
|
+
const sumStep = largestSum > 0 ? SUM_STEPS / largestSum : 0;
|
|
1949
|
+
for (let a = 0; a < actions; a++) {
|
|
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;
|
|
1955
|
+
regret[at] = r < 0 ? r - 0.5 : r + 0.5;
|
|
1956
|
+
const s = strategy[a * count + i] * sumStep + dither(at, iterWeight);
|
|
1957
|
+
strategySum[at] = s < SUM_STEPS ? s : SUM_STEPS;
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
regretScale[block] = largestRegret / REGRET_STEPS;
|
|
1961
|
+
sumScale[block] = largestSum / SUM_STEPS;
|
|
1962
|
+
}
|
|
1963
|
+
function dither(slot, iteration) {
|
|
1964
|
+
let h = Math.imul(slot ^ Math.imul(iteration, 2654435761), 2246822507);
|
|
1965
|
+
h ^= h >>> 13;
|
|
1966
|
+
h = Math.imul(h, 3266489909);
|
|
1967
|
+
h ^= h >>> 16;
|
|
1968
|
+
return (h >>> 0) / 4294967296;
|
|
1969
|
+
}
|
|
1970
|
+
function makeSolver(ctx) {
|
|
1971
|
+
const buffers = makeBuffers2(ctx.tree, ctx.layout.maxActions, ctx.simd);
|
|
1972
|
+
const reach0 = new Float64Array(COMBOS);
|
|
1973
|
+
const reach1 = new Float64Array(COMBOS);
|
|
1974
|
+
const rootLive = new Uint16Array(COMBOS);
|
|
1975
|
+
let rootCount = 0;
|
|
1976
|
+
{
|
|
1977
|
+
const onBoard = new Uint8Array(DECK);
|
|
1978
|
+
for (const card of ctx.runouts.initial) onBoard[card] = 1;
|
|
1979
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
1980
|
+
if (ctx.players[0].slot[combo] < 0 && ctx.players[1].slot[combo] < 0) continue;
|
|
1981
|
+
if (onBoard[COMBO_CARDS[combo * 2]] || onBoard[COMBO_CARDS[combo * 2 + 1]]) continue;
|
|
1982
|
+
rootLive[rootCount++] = combo;
|
|
1983
|
+
}
|
|
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
|
+
};
|
|
1994
|
+
const run = (mode, traverser, iterWeight) => {
|
|
1995
|
+
gather(ctx.players[0].weights, 0, 0, 0, reach0);
|
|
1996
|
+
gather(ctx.players[1].weights, 0, 0, 1, reach1);
|
|
1997
|
+
const me = traverser === 0 ? reach0 : reach1;
|
|
1998
|
+
const opp = traverser === 0 ? reach1 : reach0;
|
|
1999
|
+
return traverse(ctx, buffers, mode, traverser, iterWeight, 0, 0, me, opp, 0);
|
|
2000
|
+
};
|
|
2001
|
+
let root = run;
|
|
2002
|
+
const total = (values, traverser) => {
|
|
2003
|
+
const weights = ctx.players[traverser].weights;
|
|
2004
|
+
const [from, count] = listOf(0, 0, traverser);
|
|
2005
|
+
let sum = 0;
|
|
2006
|
+
for (let i = 0; i < count; i++) sum += weights[lists.list[from + i]] * values[i];
|
|
2007
|
+
return sum / ctx.totalMass;
|
|
2008
|
+
};
|
|
2009
|
+
return {
|
|
2010
|
+
ctx,
|
|
2011
|
+
rootLive,
|
|
2012
|
+
get rootCount() {
|
|
2013
|
+
return rootCount;
|
|
2014
|
+
},
|
|
2015
|
+
traverseRoot: run,
|
|
2016
|
+
useRoot(next) {
|
|
2017
|
+
root = next ?? run;
|
|
2018
|
+
},
|
|
2019
|
+
traverseChance(mode, traverser, iterWeight, node, me, opp) {
|
|
2020
|
+
return traverse(ctx, buffers, mode, traverser, iterWeight, node, 0, me, opp, 0);
|
|
2021
|
+
},
|
|
2022
|
+
iterate(count, from) {
|
|
2023
|
+
for (let i = 0; i < count; i++) {
|
|
2024
|
+
const t = from + i + 1;
|
|
2025
|
+
root(Mode.Train, 0, t);
|
|
2026
|
+
root(Mode.Train, 1, t);
|
|
2027
|
+
}
|
|
2028
|
+
},
|
|
2029
|
+
expectedValue() {
|
|
2030
|
+
return [total(root(Mode.Average, 0, 0), 0), total(root(Mode.Average, 1, 0), 1)];
|
|
2031
|
+
},
|
|
2032
|
+
exploitability() {
|
|
2033
|
+
const br0 = total(root(Mode.BestResponse, 0, 0), 0);
|
|
2034
|
+
const br1 = total(root(Mode.BestResponse, 1, 0), 1);
|
|
2035
|
+
return (br0 + br1) / 2;
|
|
2036
|
+
},
|
|
2037
|
+
valuesAt(node, board, traverser, reachMe, reachOpp, live, liveCount) {
|
|
2038
|
+
const expanded = ctx.expandOrbits;
|
|
2039
|
+
ctx.expandOrbits = true;
|
|
2040
|
+
try {
|
|
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;
|
|
2054
|
+
} finally {
|
|
2055
|
+
ctx.expandOrbits = expanded;
|
|
2056
|
+
}
|
|
2057
|
+
},
|
|
2058
|
+
pairValuesAt(node, board, traverser, hands, handReach, live, liveCount) {
|
|
2059
|
+
return pairValues(ctx, node, board, traverser, hands, handReach, live, liveCount);
|
|
2060
|
+
},
|
|
2061
|
+
averageStrategyAt(node, board) {
|
|
2062
|
+
const owner = ctx.tree.player[node];
|
|
2063
|
+
const index = ctx.players[owner];
|
|
2064
|
+
const actions = ctx.tree.numEdges[node];
|
|
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];
|
|
2075
|
+
}
|
|
2076
|
+
return { actions, probs };
|
|
2077
|
+
}
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
// src/warm.ts
|
|
2082
|
+
var SIZE_TOLERANCE = 1.35;
|
|
2083
|
+
var REGRET_STEPS2 = 32767;
|
|
2084
|
+
var SUM_STEPS2 = 65535;
|
|
2085
|
+
function matchNodes(to, from) {
|
|
2086
|
+
const match = new Int32Array(to.nodeCount).fill(-1);
|
|
2087
|
+
const walk2 = (a, b) => {
|
|
2088
|
+
if (to.kind[a] !== from.kind[b] || to.round[a] !== from.round[b]) return;
|
|
2089
|
+
if (to.kind[a] === NodeKind.Chance) {
|
|
2090
|
+
walk2(to.edgeChild[to.firstEdge[a]], from.edgeChild[from.firstEdge[b]]);
|
|
2091
|
+
return;
|
|
2092
|
+
}
|
|
2093
|
+
if (to.kind[a] !== NodeKind.Decision || to.player[a] !== from.player[b]) return;
|
|
2094
|
+
match[a] = b;
|
|
2095
|
+
for (let e = 0; e < to.numEdges[a]; e++) {
|
|
2096
|
+
const edge = to.firstEdge[a] + e;
|
|
2097
|
+
const other = matchEdge(to, edge, a, from, b);
|
|
2098
|
+
if (other >= 0) walk2(to.edgeChild[edge], from.edgeChild[other]);
|
|
2099
|
+
}
|
|
2100
|
+
};
|
|
2101
|
+
walk2(0, 0);
|
|
2102
|
+
return match;
|
|
2103
|
+
}
|
|
2104
|
+
function matchEdge(to, edge, node, from, other) {
|
|
2105
|
+
const kind = to.edgeAction[edge];
|
|
2106
|
+
const sized = kind === ActionKind.Bet || kind === ActionKind.Raise;
|
|
2107
|
+
const size = to.edgeTarget[edge] / to.pot[node];
|
|
2108
|
+
let best = -1;
|
|
2109
|
+
let bestGap = Infinity;
|
|
2110
|
+
for (let e = 0; e < from.numEdges[other]; e++) {
|
|
2111
|
+
const candidate = from.firstEdge[other] + e;
|
|
2112
|
+
if (from.edgeAction[candidate] !== kind) continue;
|
|
2113
|
+
if (!sized) return candidate;
|
|
2114
|
+
const gap = Math.abs(Math.log(from.edgeTarget[candidate] / from.pot[other] / size));
|
|
2115
|
+
if (gap < bestGap) {
|
|
2116
|
+
bestGap = gap;
|
|
2117
|
+
best = candidate;
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
return bestGap <= Math.log(SIZE_TOLERANCE) ? best : -1;
|
|
2121
|
+
}
|
|
2122
|
+
function readBlock(store, scale, block, base, length, out) {
|
|
2123
|
+
const factor = scale ? scale[block] : 1;
|
|
2124
|
+
for (let i = 0; i < length; i++) out[i] = store[base + i] * factor;
|
|
2125
|
+
}
|
|
2126
|
+
function writeBlock(store, scale, steps, block, base, length, values) {
|
|
2127
|
+
if (!scale) {
|
|
2128
|
+
for (let i = 0; i < length; i++) store[base + i] = values[i];
|
|
2129
|
+
return;
|
|
2130
|
+
}
|
|
2131
|
+
let largest = 0;
|
|
2132
|
+
for (let i = 0; i < length; i++) {
|
|
2133
|
+
const magnitude = Math.abs(values[i]);
|
|
2134
|
+
if (magnitude > largest) largest = magnitude;
|
|
2135
|
+
}
|
|
2136
|
+
const step = largest > 0 ? steps / largest : 0;
|
|
2137
|
+
for (let i = 0; i < length; i++) store[base + i] = Math.round(values[i] * step);
|
|
2138
|
+
scale[block] = largest / steps;
|
|
2139
|
+
}
|
|
2140
|
+
function realCards(runouts, round, board) {
|
|
2141
|
+
const width = runouts.width[round];
|
|
2142
|
+
return Array.from(runouts.cards[round].subarray(board * width, (board + 1) * width));
|
|
2143
|
+
}
|
|
2144
|
+
function warmStart(to, from, averages) {
|
|
2145
|
+
const a = to.runouts.initial;
|
|
2146
|
+
const b = from.runouts.initial;
|
|
2147
|
+
if (a.length !== b.length || a.some((card, i) => card !== b[i])) {
|
|
2148
|
+
throw new Error("A warm start must come from a solve of the same board");
|
|
2149
|
+
}
|
|
2150
|
+
const match = matchNodes(to.tree, from.tree);
|
|
2151
|
+
const { tree } = to;
|
|
2152
|
+
const width = to.layout.maxActions * COMBOS;
|
|
2153
|
+
const oldRegret = new Float64Array(width);
|
|
2154
|
+
const oldSum = new Float64Array(width);
|
|
2155
|
+
const newRegret = new Float64Array(width);
|
|
2156
|
+
const newSum = new Float64Array(width);
|
|
2157
|
+
const hasRegret = to.regret.length > 0 && from.regret.length > 0;
|
|
2158
|
+
const position = new Int32Array(COMBOS).fill(-1);
|
|
2159
|
+
let matched = 0;
|
|
2160
|
+
for (const node of tree.decisionNodes) {
|
|
2161
|
+
const old = match[node];
|
|
2162
|
+
if (old < 0) continue;
|
|
2163
|
+
matched++;
|
|
2164
|
+
const round = tree.round[node];
|
|
2165
|
+
const owner = tree.player[node];
|
|
2166
|
+
const actions = tree.numEdges[node];
|
|
2167
|
+
const oldActions = from.tree.numEdges[old];
|
|
2168
|
+
const actionOf = new Int32Array(actions).fill(-1);
|
|
2169
|
+
for (let e = 0; e < actions; e++) {
|
|
2170
|
+
const other = matchEdge(tree, tree.firstEdge[node] + e, node, from.tree, old);
|
|
2171
|
+
if (other >= 0) actionOf[e] = other - from.tree.firstEdge[old];
|
|
2172
|
+
}
|
|
2173
|
+
for (let board = 0; board < to.runouts.counts[round]; board++) {
|
|
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];
|
|
2178
|
+
const oldBlock = from.layout.blockOffset[old] + resolved.board;
|
|
2179
|
+
const oldBase = from.layout.offset[old] + oldActions * from.layout.rowStart[oldSlot];
|
|
2180
|
+
const oldLength = oldActions * oldCount;
|
|
2181
|
+
if (hasRegret) readBlock(from.regret, from.regretScale, oldBlock, oldBase, oldLength, oldRegret);
|
|
2182
|
+
readBlock(from.strategySum, from.sumScale, oldBlock, oldBase, oldLength, oldSum);
|
|
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;
|
|
2188
|
+
newRegret.fill(0, 0, length);
|
|
2189
|
+
newSum.fill(0, 0, length);
|
|
2190
|
+
for (let i = 0; i < count; i++) {
|
|
2191
|
+
const j = position[resolved.combos[to.active.list[newFrom + i]]];
|
|
2192
|
+
if (j < 0) continue;
|
|
2193
|
+
for (let e = 0; e < actions; e++) {
|
|
2194
|
+
const action = actionOf[e];
|
|
2195
|
+
if (action < 0) continue;
|
|
2196
|
+
newRegret[e * count + i] = oldRegret[action * oldCount + j];
|
|
2197
|
+
newSum[e * count + i] = oldSum[action * oldCount + j];
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
for (let j = 0; j < oldCount; j++) position[from.active.list[oldFrom + j]] = -1;
|
|
2201
|
+
const block = to.layout.blockOffset[node] + board;
|
|
2202
|
+
const base = to.layout.offset[node] + actions * to.layout.rowStart[newSlot];
|
|
2203
|
+
if (hasRegret) writeBlock(to.regret, to.regretScale, REGRET_STEPS2, block, base, length, newRegret);
|
|
2204
|
+
if (averages) writeBlock(to.strategySum, to.sumScale, SUM_STEPS2, block, base, length, newSum);
|
|
2205
|
+
tieBlock(to, node, board);
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
return { matchedNodes: matched, decisionNodes: tree.decisionNodes.length, averages };
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
// src/platform/browser.ts
|
|
2212
|
+
var PLATFORM = "browser";
|
|
2213
|
+
var THREADS_AVAILABLE = false;
|
|
2214
|
+
var DEFAULT_MAX_BYTES = 512 * 1024 * 1024;
|
|
2215
|
+
function defaultWorkerCount() {
|
|
2216
|
+
return 1;
|
|
2217
|
+
}
|
|
2218
|
+
function spawnWorker(_setup, _onError) {
|
|
2219
|
+
throw new Error("Worker threads are only available in the Node build; use workers: 1 in the browser");
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
// src/parallel.ts
|
|
2223
|
+
var CONTROL = {
|
|
2224
|
+
generation: 0,
|
|
2225
|
+
done: 1,
|
|
2226
|
+
command: 2,
|
|
2227
|
+
traverser: 3,
|
|
2228
|
+
iterWeight: 4,
|
|
2229
|
+
ready: 5,
|
|
2230
|
+
mode: 6,
|
|
2231
|
+
size: 8
|
|
2232
|
+
};
|
|
2233
|
+
var COMMAND = { work: 0, exit: 1 };
|
|
2234
|
+
function planShards(tree, runouts, workers) {
|
|
2235
|
+
if (workers < 2) return null;
|
|
2236
|
+
let round = -1;
|
|
2237
|
+
for (let r = 1; r < runouts.rounds; r++) {
|
|
2238
|
+
if (runouts.counts[r] > 1) {
|
|
2239
|
+
round = r;
|
|
2240
|
+
break;
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
if (round < 0) return null;
|
|
2244
|
+
const boards = runouts.counts[round];
|
|
2245
|
+
if (boards < workers * 2) return null;
|
|
2246
|
+
const entryIndexOf = new Int32Array(tree.nodeCount).fill(-1);
|
|
2247
|
+
const entryNodes = [];
|
|
2248
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
2249
|
+
if (tree.kind[node] !== NodeKind.Chance) continue;
|
|
2250
|
+
if (tree.round[node] + 1 !== round) continue;
|
|
2251
|
+
entryIndexOf[node] = entryNodes.length;
|
|
2252
|
+
entryNodes.push(node);
|
|
2253
|
+
}
|
|
2254
|
+
if (entryNodes.length === 0) return null;
|
|
2255
|
+
const bounds = new Int32Array(workers + 1);
|
|
2256
|
+
for (let w = 0; w <= workers; w++) bounds[w] = Math.round(boards * w / workers);
|
|
2257
|
+
return { round, boards, entryNodes: Int32Array.from(entryNodes), entryIndexOf, bounds };
|
|
2258
|
+
}
|
|
2259
|
+
function startPool(ctx, solver, plan, payload, workers) {
|
|
2260
|
+
const entryCount = plan.entryNodes.length;
|
|
2261
|
+
const spawned = workers - 1;
|
|
2262
|
+
const reachFloats = entryCount * 2 * COMBOS;
|
|
2263
|
+
const partialFloats = spawned * entryCount * COMBOS;
|
|
2264
|
+
const exchangeBuffer = new SharedArrayBuffer((reachFloats + partialFloats) * 8);
|
|
2265
|
+
const exchange = new Float64Array(exchangeBuffer);
|
|
2266
|
+
const controlBuffer = new SharedArrayBuffer(CONTROL.size * 4);
|
|
2267
|
+
const control = new Int32Array(controlBuffer);
|
|
2268
|
+
const threads = [];
|
|
2269
|
+
let failure = null;
|
|
2270
|
+
for (let w = 0; w < spawned; w++) {
|
|
2271
|
+
const setup = {
|
|
2272
|
+
workerId: w,
|
|
2273
|
+
payload,
|
|
2274
|
+
plan: { round: plan.round, entryNodes: plan.entryNodes, entryIndexOf: plan.entryIndexOf },
|
|
2275
|
+
shardStart: plan.bounds[w + 1],
|
|
2276
|
+
shardEnd: plan.bounds[w + 2],
|
|
2277
|
+
regret: ctx.regret.buffer,
|
|
2278
|
+
strategySum: ctx.strategySum.buffer,
|
|
2279
|
+
regretScale: ctx.regretScale?.buffer ?? null,
|
|
2280
|
+
sumScale: ctx.sumScale?.buffer ?? null,
|
|
2281
|
+
control: controlBuffer,
|
|
2282
|
+
exchange: exchangeBuffer,
|
|
2283
|
+
entryCount
|
|
2284
|
+
};
|
|
2285
|
+
const thread = spawnWorker(setup, (error) => {
|
|
2286
|
+
failure ??= error;
|
|
2287
|
+
Atomics.store(control, CONTROL.ready, spawned);
|
|
2288
|
+
Atomics.add(control, CONTROL.done, spawned);
|
|
2289
|
+
Atomics.notify(control, CONTROL.done);
|
|
2290
|
+
Atomics.notify(control, CONTROL.ready);
|
|
2291
|
+
});
|
|
2292
|
+
threads.push(thread);
|
|
2293
|
+
}
|
|
2294
|
+
for (; ; ) {
|
|
2295
|
+
const ready = Atomics.load(control, CONTROL.ready);
|
|
2296
|
+
if (ready >= spawned || failure) break;
|
|
2297
|
+
Atomics.wait(control, CONTROL.ready, ready, 20);
|
|
2298
|
+
}
|
|
2299
|
+
if (failure) {
|
|
2300
|
+
for (const thread of threads) thread.terminate();
|
|
2301
|
+
throw failure;
|
|
2302
|
+
}
|
|
2303
|
+
const total = new Float64Array(COMBOS);
|
|
2304
|
+
const chanceValues = new Float64Array(entryCount * COMBOS);
|
|
2305
|
+
const mainReachMe = new Float64Array(COMBOS);
|
|
2306
|
+
const mainReachOpp = new Float64Array(COMBOS);
|
|
2307
|
+
const dispatch = (mode, traverser, iterWeight) => {
|
|
2308
|
+
Atomics.store(control, CONTROL.done, 0);
|
|
2309
|
+
Atomics.store(control, CONTROL.mode, mode);
|
|
2310
|
+
Atomics.store(control, CONTROL.traverser, traverser);
|
|
2311
|
+
Atomics.store(control, CONTROL.iterWeight, iterWeight);
|
|
2312
|
+
Atomics.store(control, CONTROL.command, COMMAND.work);
|
|
2313
|
+
Atomics.add(control, CONTROL.generation, 1);
|
|
2314
|
+
Atomics.notify(control, CONTROL.generation);
|
|
2315
|
+
const fullStart = ctx.shardStart;
|
|
2316
|
+
const fullEnd = ctx.shardEnd;
|
|
2317
|
+
ctx.shardStart = plan.bounds[0];
|
|
2318
|
+
ctx.shardEnd = plan.bounds[1];
|
|
2319
|
+
ctx.applyUpdates = true;
|
|
2320
|
+
try {
|
|
2321
|
+
for (let entry = 0; entry < entryCount; entry++) {
|
|
2322
|
+
for (let i = 0; i < COMBOS; i++) {
|
|
2323
|
+
mainReachMe[i] = exchange[entry * 2 * COMBOS + i];
|
|
2324
|
+
mainReachOpp[i] = exchange[entry * 2 * COMBOS + COMBOS + i];
|
|
2325
|
+
}
|
|
2326
|
+
const value = solver.traverseChance(
|
|
2327
|
+
mode,
|
|
2328
|
+
traverser,
|
|
2329
|
+
iterWeight,
|
|
2330
|
+
plan.entryNodes[entry],
|
|
2331
|
+
mainReachMe,
|
|
2332
|
+
mainReachOpp
|
|
2333
|
+
);
|
|
2334
|
+
chanceValues.set(value.subarray(0, COMBOS), entry * COMBOS);
|
|
2335
|
+
}
|
|
2336
|
+
} finally {
|
|
2337
|
+
ctx.shardStart = fullStart;
|
|
2338
|
+
ctx.shardEnd = fullEnd;
|
|
2339
|
+
}
|
|
2340
|
+
for (; ; ) {
|
|
2341
|
+
const done = Atomics.load(control, CONTROL.done);
|
|
2342
|
+
if (done >= spawned) break;
|
|
2343
|
+
if (failure) throw failure;
|
|
2344
|
+
Atomics.wait(control, CONTROL.done, done, 50);
|
|
2345
|
+
}
|
|
2346
|
+
if (failure) throw failure;
|
|
2347
|
+
};
|
|
2348
|
+
return {
|
|
2349
|
+
workers,
|
|
2350
|
+
traverse(mode, traverser, iterWeight) {
|
|
2351
|
+
ctx.applyUpdates = false;
|
|
2352
|
+
ctx.chanceOverride = null;
|
|
2353
|
+
ctx.chanceCollect = (node, reachMe, reachOpp) => {
|
|
2354
|
+
const entry = plan.entryIndexOf[node];
|
|
2355
|
+
exchange.set(reachMe.subarray(0, COMBOS), entry * 2 * COMBOS);
|
|
2356
|
+
exchange.set(reachOpp.subarray(0, COMBOS), entry * 2 * COMBOS + COMBOS);
|
|
2357
|
+
};
|
|
2358
|
+
try {
|
|
2359
|
+
solver.traverseRoot(mode, traverser, iterWeight);
|
|
2360
|
+
} finally {
|
|
2361
|
+
ctx.chanceCollect = null;
|
|
2362
|
+
}
|
|
2363
|
+
dispatch(mode, traverser, iterWeight);
|
|
2364
|
+
for (let w = 0; w < spawned; w++) {
|
|
2365
|
+
const base = reachFloats + w * entryCount * COMBOS;
|
|
2366
|
+
for (let i = 0; i < entryCount * COMBOS; i++) chanceValues[i] += exchange[base + i];
|
|
2367
|
+
}
|
|
2368
|
+
ctx.chanceOverride = chanceValues;
|
|
2369
|
+
ctx.applyUpdates = true;
|
|
2370
|
+
try {
|
|
2371
|
+
const value = solver.traverseRoot(mode, traverser, iterWeight);
|
|
2372
|
+
total.set(value.subarray(0, COMBOS));
|
|
2373
|
+
} finally {
|
|
2374
|
+
ctx.chanceOverride = null;
|
|
2375
|
+
}
|
|
2376
|
+
return total;
|
|
2377
|
+
},
|
|
2378
|
+
close() {
|
|
2379
|
+
Atomics.store(control, CONTROL.command, COMMAND.exit);
|
|
2380
|
+
Atomics.add(control, CONTROL.generation, 1);
|
|
2381
|
+
Atomics.notify(control, CONTROL.generation);
|
|
2382
|
+
for (const thread of threads) thread.terminate();
|
|
2383
|
+
}
|
|
2384
|
+
};
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
// src/index.ts
|
|
2388
|
+
var DEFAULT_CHIP_SCALE = 100;
|
|
2389
|
+
var OPS_PER_SECOND = 2e8;
|
|
2390
|
+
var SpotTooLargeError = class extends Error {
|
|
2391
|
+
estimate;
|
|
2392
|
+
constructor(estimate, maxBytes) {
|
|
2393
|
+
super(
|
|
2394
|
+
`This spot needs ~${formatBytes(estimate.compressedBytes)} even at 16-bit precision, but the limit is ${formatBytes(maxBytes)}.
|
|
2395
|
+
${estimate.decisionNodes} decision nodes x up to ${estimate.runouts.toLocaleString()} runouts x ${Math.max(...estimate.combos)} combos.
|
|
2396
|
+
` + (estimate.suggestions.length ? `Try:
|
|
2397
|
+
${estimate.suggestions.map((s) => ` - ${s}`).join("\n")}
|
|
2398
|
+
` : "") + "Raise the ceiling with `maxBytes` if you have the memory."
|
|
2399
|
+
);
|
|
2400
|
+
this.name = "SpotTooLargeError";
|
|
2401
|
+
this.estimate = estimate;
|
|
2402
|
+
}
|
|
2403
|
+
};
|
|
2404
|
+
function formatBytes(bytes) {
|
|
2405
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
2406
|
+
let value = bytes;
|
|
2407
|
+
let unit = 0;
|
|
2408
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
2409
|
+
value /= 1024;
|
|
2410
|
+
unit++;
|
|
2411
|
+
}
|
|
2412
|
+
return `${value < 10 ? value.toFixed(1) : Math.round(value)} ${units[unit]}`;
|
|
2413
|
+
}
|
|
2414
|
+
function defineSpot(config) {
|
|
2415
|
+
const chipScale = config.chipScale ?? DEFAULT_CHIP_SCALE;
|
|
2416
|
+
const maxBytes = config.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
2417
|
+
const board = parseCards(config.board);
|
|
2418
|
+
if (board.length < 3 || board.length > 5) {
|
|
2419
|
+
throw new Error(`A postflop board has 3, 4 or 5 cards; got ${board.length} (${formatCards(board)})`);
|
|
2420
|
+
}
|
|
2421
|
+
const rounds = 6 - board.length;
|
|
2422
|
+
if (config.pot <= 0) throw new Error("pot must be positive");
|
|
2423
|
+
if (config.stack <= 0) throw new Error("stack must be positive");
|
|
2424
|
+
const rows = config.betSizes.length === 1 ? Array.from({ length: rounds }, () => config.betSizes[0]) : config.betSizes;
|
|
2425
|
+
if (rows.length !== rounds) {
|
|
2426
|
+
throw new Error(
|
|
2427
|
+
`betSizes has ${config.betSizes.length} rows but this board has ${rounds} betting rounds; give one row per round, or a single row for all of them`
|
|
2428
|
+
);
|
|
2429
|
+
}
|
|
2430
|
+
const onBoard = new Set(board);
|
|
2431
|
+
const trim = (range, who) => {
|
|
2432
|
+
const weights = typeof range === "string" ? parseRange(range) : Float64Array.from(range);
|
|
2433
|
+
if (weights.length !== COMBOS) {
|
|
2434
|
+
throw new Error(`The ${who} range has ${weights.length} weights; one per combination is ${COMBOS}`);
|
|
2435
|
+
}
|
|
2436
|
+
const floor = config.minWeight ?? 0;
|
|
2437
|
+
for (let combo = 0; combo < COMBOS; combo++) {
|
|
2438
|
+
if (weights[combo] === 0) continue;
|
|
2439
|
+
if (onBoard.has(COMBO_CARDS[combo * 2]) || onBoard.has(COMBO_CARDS[combo * 2 + 1])) {
|
|
2440
|
+
weights[combo] = 0;
|
|
2441
|
+
} else if (weights[combo] < floor) {
|
|
2442
|
+
weights[combo] = 0;
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
const live = activeCombos(weights).length;
|
|
2446
|
+
if (live === 0) throw new Error(`The ${who} range is empty once the board is removed`);
|
|
2447
|
+
if (config.maxCombos !== void 0 && live > config.maxCombos) {
|
|
2448
|
+
throw new Error(
|
|
2449
|
+
`The ${who} range has ${live} combinations, over the ${config.maxCombos} limit. Narrow the range or raise \`maxCombos\`.`
|
|
2450
|
+
);
|
|
2451
|
+
}
|
|
2452
|
+
return weights;
|
|
2453
|
+
};
|
|
2454
|
+
const players = [
|
|
2455
|
+
buildPlayerIndex(trim(config.oop, "out-of-position")),
|
|
2456
|
+
buildPlayerIndex(trim(config.ip, "in-position"))
|
|
2457
|
+
];
|
|
2458
|
+
const symmetries = config.isomorphism === false || board.length === 5 ? void 0 : suitSymmetries(board, [players[0].weights, players[1].weights]);
|
|
2459
|
+
const runouts = enumerateRunouts(
|
|
2460
|
+
board,
|
|
2461
|
+
symmetries,
|
|
2462
|
+
config.runoutBuckets && board.length < 5 ? rangeRankMerge(players, config.runoutBuckets === true ? DEFAULT_BUCKET_GAP : config.runoutBuckets) : void 0
|
|
2463
|
+
);
|
|
2464
|
+
const tree = buildTree(
|
|
2465
|
+
{
|
|
2466
|
+
potUnits: Math.round(config.pot * chipScale),
|
|
2467
|
+
stackUnits: Math.round(config.stack * chipScale),
|
|
2468
|
+
betSizes: rows,
|
|
2469
|
+
raiseSizes: config.raiseSizes,
|
|
2470
|
+
maxRaises: config.maxRaises ?? 1,
|
|
2471
|
+
allowAllIn: config.allowAllIn ?? false,
|
|
2472
|
+
allInThreshold: config.allInThreshold,
|
|
2473
|
+
noDonk: config.noDonk,
|
|
2474
|
+
mergeSizes: config.mergeSizes,
|
|
2475
|
+
chipScale,
|
|
2476
|
+
minRaiseUnits: chipScale
|
|
2477
|
+
},
|
|
2478
|
+
runouts
|
|
2479
|
+
);
|
|
2480
|
+
const layout = buildLayout(tree, runouts, players, 4);
|
|
2481
|
+
const last = runouts.rounds - 1;
|
|
2482
|
+
const showdownBytes = runouts.counts[last] * (2 * COMBOS + COMBOS + 1) * 2;
|
|
2483
|
+
const bytes = layout.bytes + showdownBytes;
|
|
2484
|
+
const compressedBytes = buildLayout(tree, runouts, players, 2).bytes + showdownBytes;
|
|
2485
|
+
const suggestions = [];
|
|
2486
|
+
if (compressedBytes > maxBytes) {
|
|
2487
|
+
const widest = Math.max(...rows.map((r) => r.length));
|
|
2488
|
+
if (widest > 1) suggestions.push(`use fewer bet sizes (currently up to ${widest} per round)`);
|
|
2489
|
+
if (Math.max(...[config.maxRaises ?? 1].flat()) > 1) suggestions.push(`lower maxRaises to 1`);
|
|
2490
|
+
suggestions.push(`narrow the ranges (currently ${players[0].count} and ${players[1].count} combos)`);
|
|
2491
|
+
if (runouts.rounds === 3) suggestions.push("solve from the turn instead of the flop");
|
|
2492
|
+
}
|
|
2493
|
+
const estimate = {
|
|
2494
|
+
rounds: runouts.rounds,
|
|
2495
|
+
runouts: runouts.counts[last],
|
|
2496
|
+
combos: [players[0].count, players[1].count],
|
|
2497
|
+
decisionNodes: tree.decisionNodes.length,
|
|
2498
|
+
totalSlots: layout.totalSlots,
|
|
2499
|
+
bytes,
|
|
2500
|
+
compressedBytes,
|
|
2501
|
+
secondsPerThousandIterations: layout.totalSlots * 4 * 1e3 / OPS_PER_SECOND,
|
|
2502
|
+
fits: compressedBytes <= maxBytes,
|
|
2503
|
+
suggestions
|
|
2504
|
+
};
|
|
2505
|
+
if (!estimate.fits) throw new SpotTooLargeError(estimate, maxBytes);
|
|
2506
|
+
return {
|
|
2507
|
+
board,
|
|
2508
|
+
tree,
|
|
2509
|
+
runouts,
|
|
2510
|
+
players,
|
|
2511
|
+
estimate,
|
|
2512
|
+
chipScale,
|
|
2513
|
+
potUnits: Math.round(config.pot * chipScale),
|
|
2514
|
+
stackUnits: Math.round(config.stack * chipScale),
|
|
2515
|
+
source: config,
|
|
2516
|
+
resolveBoard(cards) {
|
|
2517
|
+
return resolveBoard(runouts, typeof cards === "string" ? parseCards(cards) : cards);
|
|
2518
|
+
}
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
var PRECISION_BYTES = { f64: 8, f32: 4, i16: 2 };
|
|
2522
|
+
function createRun(spot, options = {}) {
|
|
2523
|
+
const maxBytes = spot.source.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
2524
|
+
const { totalSlots, bytes } = spot.estimate;
|
|
2525
|
+
const precision = options.analysisOnly ? "f32" : options.precision ?? (totalSlots < 4e6 && bytes * 2 <= maxBytes ? "f64" : bytes <= maxBytes ? "f32" : "i16");
|
|
2526
|
+
const relevant = new Uint8Array(COMBOS);
|
|
2527
|
+
for (const combo of spot.players[0].combos) relevant[combo] = 1;
|
|
2528
|
+
for (const combo of spot.players[1].combos) relevant[combo] = 1;
|
|
2529
|
+
const showdown = buildShowdownTable(spot.runouts, relevant);
|
|
2530
|
+
const layout = buildLayout(spot.tree, spot.runouts, spot.players, PRECISION_BYTES[precision]);
|
|
2531
|
+
const weights = runoutWeights(spot.runouts);
|
|
2532
|
+
const requested = resolveWorkers(options);
|
|
2533
|
+
const plan = requested > 1 ? planShards(spot.tree, spot.runouts, requested) : null;
|
|
2534
|
+
const allocate = (bytes2) => plan ? new SharedArrayBuffer(bytes2) : new ArrayBuffer(bytes2);
|
|
2535
|
+
const width = PRECISION_BYTES[precision];
|
|
2536
|
+
const quantised = precision === "i16";
|
|
2537
|
+
const active = buildActiveLists(spot.runouts, spot.players, showdown);
|
|
2538
|
+
const ctx = {
|
|
2539
|
+
tree: spot.tree,
|
|
2540
|
+
runouts: spot.runouts,
|
|
2541
|
+
players: spot.players,
|
|
2542
|
+
layout,
|
|
2543
|
+
showdown,
|
|
2544
|
+
regret: options.analysisOnly ? new Float32Array(0) : makeRegretStore(precision, allocate(layout.totalSlots * width)),
|
|
2545
|
+
strategySum: makeSumStore(precision, allocate(layout.totalSlots * width)),
|
|
2546
|
+
regretScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
|
|
2547
|
+
sumScale: quantised ? new Float64Array(allocate(layout.blocks * 8)) : null,
|
|
2548
|
+
scratch: makeTerminalScratch(),
|
|
2549
|
+
chipScale: spot.chipScale,
|
|
2550
|
+
active,
|
|
2551
|
+
hands: buildHandSpace(spot.runouts, spot.players, active),
|
|
2552
|
+
runoutWeight: weights,
|
|
2553
|
+
shardStart: 0,
|
|
2554
|
+
shardEnd: plan ? plan.boards : 0,
|
|
2555
|
+
shardRound: plan ? plan.round : -1,
|
|
2556
|
+
applyUpdates: true,
|
|
2557
|
+
chanceCollect: null,
|
|
2558
|
+
chanceOverride: null,
|
|
2559
|
+
entryIndexOf: plan ? plan.entryIndexOf : null,
|
|
2560
|
+
expandOrbits: false,
|
|
2561
|
+
simd: options.simd ?? true,
|
|
2562
|
+
// The traversal sums over every runout, so the denominator has to count
|
|
2563
|
+
// them too: a hand pair is dealt once per compatible completion, not
|
|
2564
|
+
// once. With a river board that factor is 1, which is exactly why this
|
|
2565
|
+
// is easy to miss.
|
|
2566
|
+
totalMass: matchupMass(spot.players[0].weights, spot.players[1].weights) * weights[0],
|
|
2567
|
+
dcfr: (options.algorithm ?? "dcfr") === "dcfr"
|
|
2568
|
+
};
|
|
2569
|
+
let warm = null;
|
|
2570
|
+
let offset = 0;
|
|
2571
|
+
if (options.warmStart) {
|
|
2572
|
+
const source = options.warmStart;
|
|
2573
|
+
warm = warmStart(ctx, source.solver.ctx, sameSpot(spot.source, source.spot.source));
|
|
2574
|
+
offset = options.warmIteration ?? source.totalIterations;
|
|
2575
|
+
}
|
|
2576
|
+
const solver = makeSolver(ctx);
|
|
2577
|
+
const pool = plan ? startPool(ctx, solver, plan, { config: spot.source, dcfr: ctx.dcfr, precision, approximations: options.approximations, simd: ctx.simd }, requested) : null;
|
|
2578
|
+
if (pool) solver.useRoot(pool.traverse);
|
|
2579
|
+
let completed = 0;
|
|
2580
|
+
return {
|
|
2581
|
+
spot,
|
|
2582
|
+
solver,
|
|
2583
|
+
workers: pool ? pool.workers : 1,
|
|
2584
|
+
get completed() {
|
|
2585
|
+
return completed;
|
|
2586
|
+
},
|
|
2587
|
+
warmStart: warm,
|
|
2588
|
+
get totalIterations() {
|
|
2589
|
+
return offset + completed;
|
|
2590
|
+
},
|
|
2591
|
+
step(count) {
|
|
2592
|
+
if (count <= 0) return;
|
|
2593
|
+
solver.iterate(count, offset + completed);
|
|
2594
|
+
completed += count;
|
|
2595
|
+
},
|
|
2596
|
+
close() {
|
|
2597
|
+
solver.useRoot(null);
|
|
2598
|
+
pool?.close();
|
|
2599
|
+
},
|
|
2600
|
+
exploitabilityPercent() {
|
|
2601
|
+
return 100 * solver.exploitability() / (spot.potUnits / spot.chipScale);
|
|
2602
|
+
},
|
|
2603
|
+
trueExploitabilityPercent() {
|
|
2604
|
+
if (!spot.runouts.approximate) return this.exploitabilityPercent();
|
|
2605
|
+
const real = createRun(defineSpot({ ...spot.source, runoutBuckets: false }), {
|
|
2606
|
+
workers: pool ? pool.workers : 1,
|
|
2607
|
+
precision
|
|
2608
|
+
});
|
|
2609
|
+
try {
|
|
2610
|
+
warmStart(real.solver.ctx, ctx, true);
|
|
2611
|
+
return real.exploitabilityPercent();
|
|
2612
|
+
} finally {
|
|
2613
|
+
real.close();
|
|
2614
|
+
}
|
|
2615
|
+
},
|
|
2616
|
+
snapshot(history = [], stoppedAt = "iterations") {
|
|
2617
|
+
return makeSolution(spot, ctx, solver, completed, offset + completed, [...history], stoppedAt, pool ? pool.workers : 1);
|
|
2618
|
+
}
|
|
2619
|
+
};
|
|
2620
|
+
}
|
|
2621
|
+
function sameSpot(a, b) {
|
|
2622
|
+
const plain = ({ maxBytes: _bytes, maxCombos: _combos, isomorphism: _iso, ...game }) => JSON.stringify(game, (_, value) => value instanceof Float64Array ? Array.from(value) : value);
|
|
2623
|
+
return plain(a) === plain(b);
|
|
2624
|
+
}
|
|
2625
|
+
var DEFAULT_BUCKET_GAP = 1;
|
|
2626
|
+
function rangeRankMerge(players, maxGap) {
|
|
2627
|
+
const allowed = new Uint8Array(13 * 13);
|
|
2628
|
+
for (let a = 0; a < 13; a++) {
|
|
2629
|
+
for (let b = a; b < 13; b++) {
|
|
2630
|
+
const map = rankSwap(a, b);
|
|
2631
|
+
let ok = true;
|
|
2632
|
+
for (const player of players) {
|
|
2633
|
+
for (let i = 0; ok && i < player.count; i++) {
|
|
2634
|
+
const combo = player.combos[i];
|
|
2635
|
+
const image = comboIndex(map[COMBO_CARDS[combo * 2]], map[COMBO_CARDS[combo * 2 + 1]]);
|
|
2636
|
+
if (player.slot[image] < 0) ok = false;
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
allowed[a * 13 + b] = allowed[b * 13 + a] = ok ? 1 : 0;
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
return { ranks: (a, b) => allowed[a * 13 + b] === 1, maxGap };
|
|
2643
|
+
}
|
|
2644
|
+
var warnedThreads = false;
|
|
2645
|
+
function resolveWorkers(options) {
|
|
2646
|
+
if (options.analysisOnly) return 1;
|
|
2647
|
+
const asked = options.workers === "auto" ? defaultWorkerCount() : options.workers ?? 1;
|
|
2648
|
+
if (asked > 1 && !THREADS_AVAILABLE) {
|
|
2649
|
+
if (!warnedThreads) {
|
|
2650
|
+
warnedThreads = true;
|
|
2651
|
+
console.warn(`poker-flopsolve: worker threads are Node-only; solving on one thread (${PLATFORM} build)`);
|
|
2652
|
+
}
|
|
2653
|
+
return 1;
|
|
2654
|
+
}
|
|
2655
|
+
return asked;
|
|
2656
|
+
}
|
|
2657
|
+
function makeRegretStore(precision, buffer) {
|
|
2658
|
+
if (precision === "f64") return new Float64Array(buffer);
|
|
2659
|
+
return precision === "f32" ? new Float32Array(buffer) : new Int16Array(buffer);
|
|
2660
|
+
}
|
|
2661
|
+
function makeSumStore(precision, buffer) {
|
|
2662
|
+
if (precision === "f64") return new Float64Array(buffer);
|
|
2663
|
+
return precision === "f32" ? new Float32Array(buffer) : new Uint16Array(buffer);
|
|
2664
|
+
}
|
|
2665
|
+
function solve(spot, options = {}) {
|
|
2666
|
+
const iterations = options.iterations ?? 500;
|
|
2667
|
+
const target = options.targetPercent ?? 0;
|
|
2668
|
+
const fixed = options.exploitEvery ?? 0;
|
|
2669
|
+
const run = createRun(spot, options);
|
|
2670
|
+
const history = [];
|
|
2671
|
+
const started = Date.now();
|
|
2672
|
+
try {
|
|
2673
|
+
return runAll();
|
|
2674
|
+
} finally {
|
|
2675
|
+
run.close();
|
|
2676
|
+
}
|
|
2677
|
+
function runAll() {
|
|
2678
|
+
if (fixed <= 0 && target <= 0) {
|
|
2679
|
+
run.step(iterations);
|
|
2680
|
+
return run.snapshot(history, "iterations");
|
|
2681
|
+
}
|
|
2682
|
+
let stoppedAt = "iterations";
|
|
2683
|
+
let done = 0;
|
|
2684
|
+
let next = fixed > 0 ? fixed : Math.min(iterations, FIRST_CHECK);
|
|
2685
|
+
let prevIteration = 0;
|
|
2686
|
+
let prevExploitability = 0;
|
|
2687
|
+
while (done < iterations) {
|
|
2688
|
+
const until = Math.min(next, iterations);
|
|
2689
|
+
run.step(until - done);
|
|
2690
|
+
done = until;
|
|
2691
|
+
const measured = run.snapshot();
|
|
2692
|
+
const info = {
|
|
2693
|
+
iteration: done,
|
|
2694
|
+
exploitability: measured.exploitability(),
|
|
2695
|
+
elapsedMs: Date.now() - started
|
|
2696
|
+
};
|
|
2697
|
+
history.push(info);
|
|
2698
|
+
options.onProgress?.(info);
|
|
2699
|
+
if (target > 0 && measured.exploitabilityPercent() <= target) {
|
|
2700
|
+
stoppedAt = "target";
|
|
2701
|
+
break;
|
|
2702
|
+
}
|
|
2703
|
+
if (fixed > 0) {
|
|
2704
|
+
next = done + fixed;
|
|
2705
|
+
} else {
|
|
2706
|
+
const predicted = predictCrossing(
|
|
2707
|
+
prevIteration,
|
|
2708
|
+
prevExploitability,
|
|
2709
|
+
done,
|
|
2710
|
+
info.exploitability,
|
|
2711
|
+
target * spot.potUnits / (100 * spot.chipScale)
|
|
2712
|
+
);
|
|
2713
|
+
const aim = predicted > 0 ? predicted * 0.95 : done * MAX_GROWTH;
|
|
2714
|
+
const bounded = Math.min(Math.max(aim, done * MIN_GROWTH), done * MAX_GROWTH);
|
|
2715
|
+
next = Math.max(done + 1, Math.ceil(bounded));
|
|
2716
|
+
prevIteration = done;
|
|
2717
|
+
prevExploitability = info.exploitability;
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
return run.snapshot(history, stoppedAt);
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
var FIRST_CHECK = 10;
|
|
2724
|
+
var MIN_GROWTH = 1.1;
|
|
2725
|
+
var MAX_GROWTH = 2;
|
|
2726
|
+
function predictCrossing(t1, e1, t2, e2, target) {
|
|
2727
|
+
if (!(e1 > e2) || !(e2 > 0) || t2 <= t1) return 0;
|
|
2728
|
+
const p = Math.log(e1 / e2) / Math.log(t2 / t1);
|
|
2729
|
+
if (!(p > 0)) return 0;
|
|
2730
|
+
const predicted = t2 * (e2 / target) ** (1 / p);
|
|
2731
|
+
return Number.isFinite(predicted) ? predicted : 0;
|
|
2732
|
+
}
|
|
2733
|
+
function makeSolution(spot, ctx, solver, iterations, totalIterations, history, stoppedAt, workers) {
|
|
2734
|
+
const { tree } = spot;
|
|
2735
|
+
const parent = new Int32Array(tree.nodeCount).fill(-1);
|
|
2736
|
+
const viaEdge = new Int32Array(tree.nodeCount).fill(-1);
|
|
2737
|
+
for (let node = 0; node < tree.nodeCount; node++) {
|
|
2738
|
+
for (let e = 0; e < tree.numEdges[node]; e++) {
|
|
2739
|
+
const edge = tree.firstEdge[node] + e;
|
|
2740
|
+
parent[tree.edgeChild[edge]] = node;
|
|
2741
|
+
viaEdge[tree.edgeChild[edge]] = edge;
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
const label = (edge) => {
|
|
2745
|
+
const kind = tree.edgeAction[edge];
|
|
2746
|
+
const amount = tree.edgeTarget[edge] / spot.chipScale;
|
|
2747
|
+
return kind >= ActionKind.Bet ? `${ACTION_LABELS[kind]} ${amount}` : ACTION_LABELS[kind];
|
|
2748
|
+
};
|
|
2749
|
+
const reachAt = (node, board, player) => {
|
|
2750
|
+
const index = spot.players[player];
|
|
2751
|
+
const reach = new Float64Array(COMBOS);
|
|
2752
|
+
for (let i = 0; i < index.count; i++) reach[index.combos[i]] = index.weights[index.combos[i]];
|
|
2753
|
+
const round = tree.round[node];
|
|
2754
|
+
for (let current = node; parent[current] >= 0; current = parent[current]) {
|
|
2755
|
+
const owner = parent[current];
|
|
2756
|
+
if (tree.kind[owner] !== NodeKind.Decision || tree.player[owner] !== player) continue;
|
|
2757
|
+
const at = boardAtRound(spot.runouts, board, round, tree.round[owner]);
|
|
2758
|
+
const { actions, probs } = solver.averageStrategyAt(owner, at);
|
|
2759
|
+
const action = viaEdge[current] - tree.firstEdge[owner];
|
|
2760
|
+
for (let i = 0; i < index.count; i++) reach[index.combos[i]] *= probs[i * actions + action];
|
|
2761
|
+
}
|
|
2762
|
+
return reach;
|
|
2763
|
+
};
|
|
2764
|
+
let cachedEv = null;
|
|
2765
|
+
let cachedExploit = null;
|
|
2766
|
+
return {
|
|
2767
|
+
spot,
|
|
2768
|
+
iterations,
|
|
2769
|
+
totalIterations,
|
|
2770
|
+
stoppedAt,
|
|
2771
|
+
workers,
|
|
2772
|
+
solver,
|
|
2773
|
+
history,
|
|
2774
|
+
ev(player) {
|
|
2775
|
+
cachedEv ??= solver.expectedValue();
|
|
2776
|
+
return cachedEv[player];
|
|
2777
|
+
},
|
|
2778
|
+
exploitability() {
|
|
2779
|
+
cachedExploit ??= solver.exploitability();
|
|
2780
|
+
return cachedExploit;
|
|
2781
|
+
},
|
|
2782
|
+
exploitabilityPercent() {
|
|
2783
|
+
cachedExploit ??= solver.exploitability();
|
|
2784
|
+
return 100 * cachedExploit / (spot.potUnits / spot.chipScale);
|
|
2785
|
+
},
|
|
2786
|
+
strategyAt(node, board) {
|
|
2787
|
+
if (tree.kind[node] !== NodeKind.Decision) throw new Error(`Node ${node} is not a decision`);
|
|
2788
|
+
const owner = tree.player[node];
|
|
2789
|
+
const { actions, probs } = solver.averageStrategyAt(node, board);
|
|
2790
|
+
const names = [];
|
|
2791
|
+
for (let a = 0; a < actions; a++) names.push(label(tree.firstEdge[node] + a));
|
|
2792
|
+
return { node, board, player: owner, actions: names, combos: spot.players[owner].combos, probs };
|
|
2793
|
+
},
|
|
2794
|
+
path(node) {
|
|
2795
|
+
const steps = [];
|
|
2796
|
+
let current = node;
|
|
2797
|
+
while (parent[current] >= 0) {
|
|
2798
|
+
const owner = parent[current];
|
|
2799
|
+
steps.push(tree.kind[owner] === NodeKind.Decision ? label(viaEdge[current]) : "/");
|
|
2800
|
+
current = owner;
|
|
2801
|
+
}
|
|
2802
|
+
return steps.reverse().join(" ") || "(root)";
|
|
2803
|
+
},
|
|
2804
|
+
reachAt,
|
|
2805
|
+
subgameAt(node, board, betting = {}) {
|
|
2806
|
+
const opensRound = node === 0 || parent[node] >= 0 && tree.kind[parent[node]] === NodeKind.Chance;
|
|
2807
|
+
if (tree.kind[node] !== NodeKind.Decision || !opensRound) {
|
|
2808
|
+
throw new Error(`Node ${node} does not open a betting round; a subgame must start where one does`);
|
|
2809
|
+
}
|
|
2810
|
+
const round = tree.round[node];
|
|
2811
|
+
if (board < 0 || board >= spot.runouts.counts[round]) {
|
|
2812
|
+
throw new Error(`Board ${board} is out of range for round ${round}`);
|
|
2813
|
+
}
|
|
2814
|
+
const width = spot.runouts.width[round];
|
|
2815
|
+
const cards = spot.runouts.cards[round].subarray(board * width, board * width + width);
|
|
2816
|
+
const invested = (tree.pot[node] - spot.potUnits) / 2;
|
|
2817
|
+
const { source } = spot;
|
|
2818
|
+
const later = (value, listed) => listed && value.length > 1 ? value.slice(round) : value;
|
|
2819
|
+
const rows = later(source.betSizes, true);
|
|
2820
|
+
const raiseRows = source.raiseSizes && later(source.raiseSizes, raisesPerRound(source.raiseSizes));
|
|
2821
|
+
return {
|
|
2822
|
+
board: formatCards(cards),
|
|
2823
|
+
pot: tree.pot[node] / spot.chipScale,
|
|
2824
|
+
stack: (spot.stackUnits - invested) / spot.chipScale,
|
|
2825
|
+
oop: reachAt(node, board, 0),
|
|
2826
|
+
ip: reachAt(node, board, 1),
|
|
2827
|
+
betSizes: betting.betSizes ?? rows,
|
|
2828
|
+
raiseSizes: "raiseSizes" in betting ? betting.raiseSizes : raiseRows,
|
|
2829
|
+
maxRaises: betting.maxRaises ?? (source.maxRaises === void 0 ? void 0 : later(source.maxRaises, Array.isArray(source.maxRaises))),
|
|
2830
|
+
allowAllIn: betting.allowAllIn ?? (source.allowAllIn === void 0 ? void 0 : later(source.allowAllIn, Array.isArray(source.allowAllIn))),
|
|
2831
|
+
allInThreshold: "allInThreshold" in betting ? betting.allInThreshold : source.allInThreshold === void 0 ? void 0 : later(source.allInThreshold, Array.isArray(source.allInThreshold)),
|
|
2832
|
+
noDonk: "noDonk" in betting ? betting.noDonk : source.noDonk === void 0 ? void 0 : later(source.noDonk, Array.isArray(source.noDonk)),
|
|
2833
|
+
mergeSizes: "mergeSizes" in betting ? betting.mergeSizes : source.mergeSizes,
|
|
2834
|
+
minWeight: source.minWeight,
|
|
2835
|
+
isomorphism: source.isomorphism,
|
|
2836
|
+
maxCombos: "maxCombos" in betting ? betting.maxCombos : source.maxCombos,
|
|
2837
|
+
maxBytes: "maxBytes" in betting ? betting.maxBytes : source.maxBytes,
|
|
2838
|
+
chipScale: spot.chipScale
|
|
2839
|
+
};
|
|
2840
|
+
}
|
|
2841
|
+
};
|
|
2842
|
+
}
|
|
2843
|
+
export {
|
|
2844
|
+
ACTION_LABELS,
|
|
2845
|
+
ActionKind,
|
|
2846
|
+
CATEGORY,
|
|
2847
|
+
CATEGORY_NAMES,
|
|
2848
|
+
COMBOS,
|
|
2849
|
+
COMBO_CARDS,
|
|
2850
|
+
COMBO_LANES,
|
|
2851
|
+
DECK,
|
|
2852
|
+
NodeKind,
|
|
2853
|
+
PLATFORM,
|
|
2854
|
+
PRECISION_BYTES,
|
|
2855
|
+
RANKS,
|
|
2856
|
+
SUITS,
|
|
2857
|
+
SpotTooLargeError,
|
|
2858
|
+
activeCombos,
|
|
2859
|
+
cardOf,
|
|
2860
|
+
categoryOf,
|
|
2861
|
+
comboIndex,
|
|
2862
|
+
createRun,
|
|
2863
|
+
defineSpot,
|
|
2864
|
+
evaluate,
|
|
2865
|
+
evaluateCards,
|
|
2866
|
+
formatCard,
|
|
2867
|
+
formatCards,
|
|
2868
|
+
formatCombo,
|
|
2869
|
+
makeRegretStore,
|
|
2870
|
+
makeSumStore,
|
|
2871
|
+
parseCard,
|
|
2872
|
+
parseCards,
|
|
2873
|
+
parseRange,
|
|
2874
|
+
predictCrossing,
|
|
2875
|
+
rangeWeight,
|
|
2876
|
+
rankOf,
|
|
2877
|
+
simdAvailable,
|
|
2878
|
+
solve,
|
|
2879
|
+
suitLanes,
|
|
2880
|
+
suitOf
|
|
2881
|
+
};
|
|
2882
|
+
//# sourceMappingURL=browser.js.map
|