forked from WikiDeck/wikideck
131 lines
5.6 KiB
TypeScript
131 lines
5.6 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)];
|
|
|
|
/** The state a bot reasons about: a determinization of its observation (or the state itself if the engine has no views). */
|
|
function see<S, A>(engine: EngineAdapter<S, A>, obs: unknown, seat: Seat, rng: Rng): S {
|
|
return engine.assume ? engine.assume(obs, seat, rng) : (obs as S);
|
|
}
|
|
|
|
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<unknown, A> {
|
|
return (obs, player, rng) => pick(engine.legalActions(see(engine, obs, player, rng), 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<unknown, A> {
|
|
return (obs, player, rng) => {
|
|
const state = see(engine, obs, player, rng);
|
|
return greedyChoice(engine, state, player, engine.legalActions(state, player), rng);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Naive "always play the highest-power card" player (DESIGN §8 target: < 55% vs a smart bot).
|
|
* Plays the highest printed-power affordable card into a random legal lane; ends the turn when nothing fits.
|
|
*/
|
|
export function maxPowerBot<S, A>(engine: EngineAdapter<S, A>): BotFn<unknown, A> {
|
|
return (obs, player, rng) => {
|
|
const state = see(engine, obs, player, rng);
|
|
const actions = engine.legalActions(state, player);
|
|
if (!engine.actionPower) return greedyChoice(engine, state, player, actions, rng);
|
|
let best: A[] = [], bestP = -Infinity;
|
|
for (const a of actions) {
|
|
const p = engine.actionPower(state, a);
|
|
if (p > bestP) { bestP = p; best = [a]; } else if (p === bestP) best.push(a);
|
|
}
|
|
return pick(best, 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<unknown, A> {
|
|
const k = opts.k ?? 4, rollouts = opts.rollouts ?? 8, horizon = opts.horizon ?? 40, eps = opts.epsilon ?? 0.25;
|
|
return (obs, seat, rng) => {
|
|
const state = see(engine, obs, 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.assume ? engine.applyAction(engine.assume(obs, seat, rng), c.a) : c.next, steps = 0; // fresh hidden sample per rollout
|
|
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', 'maxpower', 'greedy', 'lookahead-lite', 'lookahead'] as const;
|
|
export type BotName = typeof BOT_NAMES[number];
|
|
|
|
export function makeBot<S, A>(name: string, engine: EngineAdapter<S, A>): BotFn<unknown, A> {
|
|
switch (name) {
|
|
case 'random': return randomBot(engine);
|
|
case 'maxpower': return maxPowerBot(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}`);
|
|
}
|
|
}
|