Files
wikideck/shared/bots/index.ts
T
claudeandClaude Opus 5.5 9841622ac7 sim-balance: bots (random/greedy/lookahead), sim runner and stats
Bots are pure (state, player, rng) => action over an EngineAdapter;
shared/bots/game-adapter.ts adapts the shared/game API (hidden staged
plays, determinized lookahead). scripts/sim/run.ts runs N matches per
ruleset across child processes and writes per-ruleset markdown/JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 22:54:53 -05:00

103 lines
4.3 KiB
TypeScript

// Reference bots: random, greedy (one-ply) and flat Monte Carlo lookahead.
// Each factory takes an EngineAdapter and returns a pure BotFn (state, player, rng) => action.
import type { BotFn, EngineAdapter, Rng, Seat } from './types.ts';
export type * from './types.ts';
export function mulberry32(seed: number): Rng {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const pick = <T>(xs: T[], rng: Rng): T => xs[Math.floor(rng() * xs.length)];
function terminalValue<S, A>(engine: EngineAdapter<S, A>, s: S, seat: Seat): number {
const { winner } = engine.result(s);
return winner === null ? 0 : winner === seat ? 1 : -1;
}
function value<S, A>(engine: EngineAdapter<S, A>, s: S, seat: Seat): number {
return engine.isOver(s) ? terminalValue(engine, s, seat) * 10 : engine.evaluate(s, seat);
}
/** Uniformly random legal action. */
export function randomBot<S, A>(engine: EngineAdapter<S, A>): BotFn<S, A> {
return (state, player, rng) => pick(engine.legalActions(state, player), rng);
}
function greedyChoice<S, A>(engine: EngineAdapter<S, A>, state: S, seat: Seat, actions: A[], rng: Rng): A {
if (actions.length === 1) return actions[0];
let best: A[] = [];
let bestV = -Infinity;
for (const a of actions) {
const v = value(engine, engine.applyAction(state, a), seat);
if (v > bestV + 1e-9) { bestV = v; best = [a]; }
else if (Math.abs(v - bestV) <= 1e-9) best.push(a);
}
return pick(best, rng);
}
/** One-ply: take the action whose resulting state evaluates best for us. Ties broken randomly. */
export function greedyBot<S, A>(engine: EngineAdapter<S, A>): BotFn<S, A> {
return (state, player, rng) => greedyChoice(engine, state, player, engine.legalActions(state, player), rng);
}
export interface LookaheadOptions { k?: number; rollouts?: number; horizon?: number; epsilon?: number }
/**
* Flat Monte Carlo lookahead: shortlist the top-k greedy candidates, then for each run
* `rollouts` epsilon-greedy playouts for both seats, up to `horizon` actions, scoring
* terminal results (or the clamped heuristic at the horizon).
* Rollouts run on the true state, so the bot is clairvoyant about hidden info and seeded
* draws: an upper bound on skill, not a fair human-like player.
*/
export function lookaheadBot<S, A>(engine: EngineAdapter<S, A>, opts: LookaheadOptions = {}): BotFn<S, A> {
const k = opts.k ?? 4, rollouts = opts.rollouts ?? 8, horizon = opts.horizon ?? 40, eps = opts.epsilon ?? 0.25;
return (state, seat, rng) => {
const actions = engine.legalActions(state, seat);
if (actions.length === 1) return actions[0];
const shortlist = actions.map(a => {
const next = engine.applyAction(state, a);
return { a, next, v: value(engine, next, seat) };
}).sort((x, y) => y.v - x.v).slice(0, k);
let best = shortlist[0], bestScore = -Infinity;
for (const c of shortlist) {
let total = 0;
for (let r = 0; r < rollouts; r++) {
let s = engine.determinize ? engine.applyAction(engine.determinize(state, seat, rng), c.a) : c.next, steps = 0;
while (!engine.isOver(s) && steps < horizon) {
const who = engine.toAct(s);
const legal = engine.legalActions(s, who);
if (!legal.length) break;
const a = rng() < eps ? pick(legal, rng) : greedyChoice(engine, s, who, legal, rng);
s = engine.applyAction(s, a);
steps++;
}
total += engine.isOver(s) ? terminalValue(engine, s, seat) : Math.max(-1, Math.min(1, engine.evaluate(s, seat)));
}
const score = total / rollouts + c.v * 1e-3; // tiny greedy tiebreak
if (score > bestScore) { bestScore = score; best = c; }
}
return best.a;
};
}
export const BOT_NAMES = ['random', 'greedy', 'lookahead', 'lookahead-lite'] as const;
export type BotName = typeof BOT_NAMES[number];
export function makeBot<S, A>(name: string, engine: EngineAdapter<S, A>): BotFn<S, A> {
switch (name) {
case 'random': return randomBot(engine);
case 'greedy': return greedyBot(engine);
case 'lookahead': return lookaheadBot(engine);
case 'lookahead-lite': return lookaheadBot(engine, { k: 3, rollouts: 4, horizon: 24 });
default: throw new Error(`unknown bot ${name}`);
}
}