Files
wikideck/scripts/sim/duel-run.ts
T

138 lines
7.8 KiB
TypeScript

// Duel balance simulation.
// npx tsx scripts/sim/duel-run.ts --engine duel --matches 2000 --workers 4 --out sim-out/duel [--rules '{"startingLife":18}']
// Writes <out>/<label>.jsonl, <out>/<label>.stats.json and <out>/<label>.md. Workers are capped at 4 (r350 memory).
import { fork } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { createWriteStream, mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { mulberry32 } from '../../shared/bots/index.ts';
import { makeDuelBot } from '../../shared/bots/duel-bots.ts';
import type { BotFn, DeckSpec, Seat } from '../../shared/bots/types.ts';
import { deckSet, type SimDeck } from './duel-decks.ts';
import type { DuelSim } from './duel-sim-types.ts';
import { aggregateDuel, duelMarkdown, type DuelRecord } from './duel-stats.ts';
export const MAX_WORKERS = 4;
export const MAX_ACTIONS = 3000;
const SAMPLE = fileURLToPath(new URL('../../docs/game/card-sample.json', import.meta.url));
export async function loadDuelSim(name: string, rules: Record<string, unknown> = {}): Promise<DuelSim<any, any>> {
if (name === 'standin') return (await import('./duel-standin-adapter.ts')).makeStandinSim(SAMPLE, rules as any);
if (name === 'duel') { const mod = './duel-core-sim.ts'; return (await import(mod)).makeDuelSim(SAMPLE, rules); }
throw new Error(`unknown duel engine ${name}`);
}
export function playDuel<S, A>(sim: DuelSim<S, A>, decks: [SimDeck, SimDeck], botNames: [string, string], seed: number, bots?: [BotFn<unknown, A>, BotFn<unknown, A>]): DuelRecord {
const t0 = performance.now();
const { engine } = sim;
const b = bots ?? [makeDuelBot(botNames[0], engine, sim.lens), makeDuelBot(botNames[1], engine, sim.lens)];
const rng = [mulberry32(seed ^ 0x9e3779b9), mulberry32(seed ^ 0x85ebca6b)];
const specs: [DeckSpec, DeckSpec] = [0, 1].map(i => ({ id: decks[i].id, tags: decks[i].tags, payload: sim.deckPayload(decks[i].cards) })) as [DeckSpec, DeckSpec];
let s = engine.createMatch('duel', specs, seed);
const first = (engine.firstSeat ? engine.firstSeat(s) : engine.toAct(s)) as Seat;
const played: [string[], string[]] = [[], []];
let actions = 0;
while (!engine.isOver(s) && actions < MAX_ACTIONS) {
const seat = engine.toAct(s);
const a = b[seat](engine.observe ? engine.observe(s, seat) : s, seat, rng[seat]);
for (const c of engine.cardsOf(s, a)) played[seat].push(c);
s = engine.applyAction(s, a);
actions++;
}
const truncated = !engine.isOver(s);
const res = truncated ? { winner: null, reason: 'action-cap' } : engine.result(s);
const turns = engine.turn(s);
return {
seed, bots: botNames, decks: [decks[0].id, decks[1].id], tags: [decks[0].tags, decks[1].tags],
first, winner: res.winner, reason: res.reason ?? 'none', turns, turnsEach: Math.ceil(turns / 2), actions, truncated,
// Clock model: 10 s of overhead per turn plus 4 s per atomic action (taps, plays, blocks).
minutes: (turns * 10 + actions * 4) / 60,
played, metrics: sim.metrics ? sim.metrics(s) : {}, ms: performance.now() - t0,
};
}
/** Bot pairings and their share of the schedule. Symmetric pairs carry the balance stats. */
export const DUEL_MIXES: Record<string, [string, string, number][]> = {
default: [['heuristic', 'heuristic', 0.7], ['mc-lite', 'mc-lite', 0.06], ['heuristic', 'mc-lite', 0.08], ['random', 'heuristic', 0.06], ['greedy', 'heuristic', 0.05], ['random', 'random', 0.05]],
fast: [['heuristic', 'heuristic', 0.9], ['random', 'heuristic', 0.05], ['greedy', 'heuristic', 0.05]],
skill: [['heuristic', 'mc', 0.4], ['random', 'heuristic', 0.2], ['greedy', 'heuristic', 0.2], ['heuristic', 'mc-lite', 0.2]],
};
interface Job { i: number; seed: number; decks: [number, number]; bots: [string, string] }
export function schedule(n: number, nDecks: number, baseSeed: number, mix: [string, string, number][]): Job[] {
const rng = mulberry32(baseSeed);
const total = mix.reduce((t, m) => t + m[2], 0);
return Array.from({ length: n }, (_, i) => {
let r = rng() * total, k = 0;
while (r > mix[k][2] && k < mix.length - 1) { r -= mix[k][2]; k++; }
const flip = rng() < 0.5;
let d0 = Math.floor(rng() * nDecks), d1 = Math.floor(rng() * nDecks);
if (d1 === d0) d1 = (d1 + 1) % nDecks;
return { i, seed: (baseSeed * 1000003 + i * 7919) >>> 0, decks: [d0, d1], bots: flip ? [mix[k][1], mix[k][0]] : [mix[k][0], mix[k][1]] };
});
}
function arg(name: string, def?: string): string | undefined { const i = process.argv.indexOf(`--${name}`); return i >= 0 ? process.argv[i + 1] : def; }
interface Ctx { engineName: string; rules: Record<string, unknown>; nDecks: number; deckSeed: number }
async function setup(ctx: Ctx) {
const sim = await loadDuelSim(ctx.engineName, ctx.rules);
const decks = deckSet(sim.pool, ctx.nDecks, ctx.deckSeed, Number(ctx.rules.deckSize ?? 40));
return { sim, decks };
}
function runJobs(sim: DuelSim<any, any>, decks: SimDeck[], jobs: Job[], on: (r: DuelRecord | { error: string }) => void) {
for (const j of jobs) {
try { on(playDuel(sim, [decks[j.decks[0]], decks[j.decks[1]]], j.bots, j.seed)); }
catch (e) { on({ error: String((e as Error)?.stack ?? e).slice(0, 800) }); }
}
}
async function main() {
const ctx: Ctx = { engineName: arg('engine', 'standin')!, rules: JSON.parse(arg('rules', '{}')!), nDecks: Number(arg('decks', '120')), deckSeed: Number(arg('deck-seed', '7')) };
const n = Number(arg('matches', '1000'));
const workers = Math.max(1, Math.min(MAX_WORKERS, Number(arg('workers', '4'))));
const seed = Number(arg('seed', '1'));
const out = arg('out', 'sim-out/duel')!;
const label = arg('label', ctx.engineName)!;
mkdirSync(out, { recursive: true });
const { sim, decks } = await setup(ctx);
const jobs = schedule(n, decks.length, seed, DUEL_MIXES[arg('mix', 'default')!]);
const records: DuelRecord[] = [], errors: string[] = [];
const file = createWriteStream(join(out, `${label}.jsonl`));
const t0 = Date.now();
const handle = (r: any) => {
if (r.error) { errors.push(r.error); return; }
records.push(r); file.write(JSON.stringify(r) + '\n');
if ((records.length + errors.length) % 250 === 0) process.stderr.write(`[${label}] ${records.length + errors.length}/${n} ${((Date.now() - t0) / 1000).toFixed(0)}s\n`);
};
if (workers === 1) runJobs(sim, decks, jobs, handle);
else {
const chunks: Job[][] = Array.from({ length: workers }, () => []);
jobs.forEach((j, i) => chunks[i % workers].push(j));
await Promise.all(chunks.map(chunk => new Promise<void>((resolve, reject) => {
const w = fork(fileURLToPath(import.meta.url), ['--child'], { execArgv: ['--import', 'tsx', '--max-old-space-size=384'], serialization: 'advanced' });
w.on('message', (m: any) => (m === 'done' ? w.kill() : handle(m)));
w.on('error', reject);
w.on('exit', () => resolve());
w.send({ ctx, jobs: chunk });
})));
}
file.end();
const stats = aggregateDuel(records, sim, decks, errors.length, { engine: ctx.engineName, rules: ctx.rules, label, sha: arg('sha') });
writeFileSync(join(out, `${label}.stats.json`), JSON.stringify(stats, null, 1));
if (errors.length) writeFileSync(join(out, `${label}.errors.txt`), errors.slice(0, 30).join('\n----\n'));
const md = duelMarkdown(stats);
writeFileSync(join(out, `${label}.md`), md);
process.stderr.write(`[${label}] done: ${records.length} matches, ${errors.length} errors, ${((Date.now() - t0) / 1000).toFixed(1)}s\n`);
process.stdout.write(md);
}
const isEntry = !!process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
if (isEntry && process.argv.includes('--child')) {
process.once('message', async (msg: any) => {
const { sim, decks } = await setup(msg.ctx);
runJobs(sim, decks, msg.jobs, r => process.send!(r));
process.send!('done');
});
} else if (isEntry) main().catch(e => { console.error(e); process.exit(1); });