Anticipate village arrivals and lava cutoffs with matched AI benchmarks

This commit is contained in:
coyotte508 2026-09-09 02:32:26 +02:00
commit b092cdc6c6
6 changed files with 773 additions and 33 deletions

View file

@ -7,7 +7,7 @@
"engine": {
"package": {
"name": "fuji-engine",
"version": "0.1.3"
"version": "0.1.4"
},
"entryPoint": "dist/wrapper.js"
},

View file

@ -60,7 +60,7 @@ The higher-lead candidate stalled once: two neighbors ended up sharing an isolat
## Village space
Bots already in the village prefer to leave scarce, reachable entrances for approaching comparison neighbors. The preference uses public routes, weights teammates near lava more heavily, and favors leaving shorter approaches available. It applies only to village destinations safe from the next eruption and routes without eruption markers; ordinary dice and route scoring still apply. Neighbors already in the village or with finalized routes do not add pressure to move.
Bots already in the village prefer to leave scarce, reachable entrances for approaching comparison neighbors. The preference uses public routes, weights teammates near lava more heavily, and favors leaving shorter approaches available. It applies only to village destinations safe from the next eruption and routes without eruption markers; ordinary dice and route scoring still apply. Neighbors already in the village do not add pressure to move. The later lookahead refinement below also considers finalized routes that still end outside the village.
Matched comparison for the village-space change: 240 identical seeded games using the same engine, with only the previous AI module substituted for the baseline. Scenario 1 used 50 seeds at each of 2/3/4 players; scenarios 2–7 used five seeds at each player count. Difficulty cycled through 1–4. Baseline: 70 wins (63 + 7), 13,911 moves. Updated: 72 wins (64 + 8), 13,631 moves. Both completed all games without deadlocks and with exact replays. The gain from 29.2% to 30.0% is small and does not establish statistical significance.
@ -95,3 +95,13 @@ Regression coverage verifies reroll selection against the proposed destination w
A bot staying on its current village tile can prioritise reducing its dice contributions against escaping comparison neighbours. It applies only outside the next eruption wave, with stamina strictly above the maximum possible loss for this round. Rerolls seek lower opposing contributions instead of improving the resident's own result. Buddy and Machete may set aside dice that also match the resident's own location. Residents at risk of exhaustion or threatened by lava retain the ordinary survival policy. Hidden teammate dice remain unavailable.
A matched 252-game comparison across seven scenarios, 2–4 players and difficulties 1–4 increased complete-team wins from 65 (25.8%) to 69 (27.4%). Every game terminated and replayed exactly. This four-win difference is modest; it does not establish a universally stronger policy. Results are in `ai-village-help-benchmark.json`, using seed family `village-help`, 12 seeds per scenario/player-count combination. The baseline is the AI from commit b769c7e.
## Proactive village space and lava cutoffs
Residents now look up to two movement ranges ahead for approaching comparison neighbours. Immediate entrances receive priority; if none are reachable yet, future entrances receive half weight. A confirmed teammate's destination becomes the origin for this future approach. The entrance cost increased from 18 to 36 after a three-candidate training comparison. Relocation bonuses still apply only to safe village destinations reached without eruption markers. Opposite seats in four-player games do not reserve destinations against each other.
Planning also forecasts the next lava wave plus extra waves from the candidate route's eruption markers. Destinations in that lava are heavily penalized. A non-village destination that survives but loses its connected path to any surviving village receives a cutoff penalty. This is a geometric forecast of the candidate's own successful route, not knowledge of hidden rolls or a guarantee about other players' future equipment and movement.
Training: previous policy 52/168 wins, plain lookahead 50/168, stronger lookahead 53/168. The stronger candidate's first held-out batch improved from 71/252 to 73/252. Cutoff training tied entrance-only at 51/168. A second held-out batch produced 59/252 for the previous policy and 57/252 for either entrance-only or the combined policy. The combined policy was then evaluated on the first batch too and matched 73/252.
Final matched comparison across both batches: **130/504 (25.8%) before and after**, with every game complete and replay-exact. The changes are retained for the tested tactical behavior, not a demonstrated win-rate increase. Full reports are in `ai-village-space-benchmark.json`. Reproduce the final batches with `node scripts/benchmark-policy.mjs current village-space-heldout 12` and `node scripts/benchmark-policy.mjs current cutoff-heldout 12`; use `FUJI_BENCH_ENGINE` to select a historical compiled baseline.

View file

@ -0,0 +1,629 @@
{
"baselineCommit": "6aa8dc7",
"scope": "All seven scenarios, 2\u20134 players, difficulties 1\u20134, seeded random skills. Equal seeds across versions. Every game checked for termination and exact replay.",
"finalComparison": {
"before": [
{
"name": "old",
"family": "village-space-heldout",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 252,
"wins": 71,
"moves": 14971,
"byScenario": {
"1": {
"games": 36,
"wins": 18
},
"2": {
"games": 36,
"wins": 8
},
"3": {
"games": 36,
"wins": 3
},
"4": {
"games": 36,
"wins": 11
},
"5": {
"games": 36,
"wins": 13
},
"6": {
"games": 36,
"wins": 10
},
"7": {
"games": 36,
"wins": 8
}
},
"equipment": {
"tape": 30,
"flare": 53,
"machete": 49,
"torch": 75,
"rope": 66,
"water": 83,
"shovel": 113,
"binoculars": 105,
"aid": 87,
"compass": 17,
"map": 58,
"radio": 78
},
"stalled": []
},
{
"name": "old",
"family": "cutoff-heldout",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 252,
"wins": 59,
"moves": 14249,
"byScenario": {
"1": {
"games": 36,
"wins": 20
},
"2": {
"games": 36,
"wins": 3
},
"3": {
"games": 36,
"wins": 10
},
"4": {
"games": 36,
"wins": 3
},
"5": {
"games": 36,
"wins": 8
},
"6": {
"games": 36,
"wins": 9
},
"7": {
"games": 36,
"wins": 6
}
},
"equipment": {
"water": 130,
"flare": 88,
"aid": 151,
"torch": 28,
"compass": 50,
"tape": 11,
"radio": 68,
"shovel": 48,
"map": 42,
"machete": 45,
"rope": 10,
"binoculars": 13
},
"stalled": []
}
],
"after": [
{
"name": "current",
"family": "village-space-heldout",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 252,
"wins": 73,
"moves": 14962,
"byScenario": {
"1": {
"games": 36,
"wins": 19
},
"2": {
"games": 36,
"wins": 8
},
"3": {
"games": 36,
"wins": 4
},
"4": {
"games": 36,
"wins": 11
},
"5": {
"games": 36,
"wins": 14
},
"6": {
"games": 36,
"wins": 9
},
"7": {
"games": 36,
"wins": 8
}
},
"equipment": {
"tape": 29,
"flare": 52,
"machete": 48,
"torch": 77,
"rope": 66,
"water": 84,
"shovel": 113,
"binoculars": 105,
"aid": 87,
"compass": 17,
"map": 58,
"radio": 77
},
"stalled": []
},
{
"name": "current",
"family": "cutoff-heldout",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 252,
"wins": 57,
"moves": 14156,
"byScenario": {
"1": {
"games": 36,
"wins": 20
},
"2": {
"games": 36,
"wins": 3
},
"3": {
"games": 36,
"wins": 10
},
"4": {
"games": 36,
"wins": 3
},
"5": {
"games": 36,
"wins": 7
},
"6": {
"games": 36,
"wins": 9
},
"7": {
"games": 36,
"wins": 5
}
},
"equipment": {
"water": 130,
"flare": 88,
"aid": 151,
"torch": 28,
"compass": 50,
"tape": 11,
"radio": 68,
"shovel": 48,
"map": 42,
"machete": 45,
"rope": 10,
"binoculars": 13
},
"stalled": []
}
]
},
"training": [
{
"name": "old",
"family": "village-space-training",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 168,
"wins": 52,
"moves": 10350,
"byScenario": {
"1": {
"games": 24,
"wins": 12
},
"2": {
"games": 24,
"wins": 8
},
"3": {
"games": 24,
"wins": 4
},
"4": {
"games": 24,
"wins": 5
},
"5": {
"games": 24,
"wins": 6
},
"6": {
"games": 24,
"wins": 6
},
"7": {
"games": 24,
"wins": 11
}
},
"equipment": {
"radio": 97,
"torch": 42,
"water": 72,
"aid": 166,
"machete": 18,
"map": 58,
"rope": 27,
"flare": 34,
"compass": 4,
"shovel": 44,
"tape": 7
},
"stalled": []
},
{
"name": "route",
"family": "village-space-training",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 168,
"wins": 50,
"moves": 10335,
"byScenario": {
"1": {
"games": 24,
"wins": 12
},
"2": {
"games": 24,
"wins": 6
},
"3": {
"games": 24,
"wins": 4
},
"4": {
"games": 24,
"wins": 5
},
"5": {
"games": 24,
"wins": 6
},
"6": {
"games": 24,
"wins": 6
},
"7": {
"games": 24,
"wins": 11
}
},
"equipment": {
"radio": 97,
"torch": 42,
"water": 71,
"aid": 166,
"machete": 18,
"map": 58,
"rope": 27,
"flare": 34,
"compass": 4,
"shovel": 44,
"tape": 7
},
"stalled": []
},
{
"name": "current",
"family": "village-space-training",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 168,
"wins": 53,
"moves": 10377,
"byScenario": {
"1": {
"games": 24,
"wins": 13
},
"2": {
"games": 24,
"wins": 7
},
"3": {
"games": 24,
"wins": 4
},
"4": {
"games": 24,
"wins": 6
},
"5": {
"games": 24,
"wins": 6
},
"6": {
"games": 24,
"wins": 6
},
"7": {
"games": 24,
"wins": 11
}
},
"equipment": {
"radio": 97,
"torch": 42,
"water": 71,
"flare": 35,
"aid": 166,
"machete": 18,
"map": 58,
"rope": 27,
"compass": 4,
"shovel": 44,
"tape": 7
},
"stalled": []
}
],
"cutoffTraining": [
{
"name": "route",
"family": "cutoff-training",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 168,
"wins": 51,
"moves": 10097,
"byScenario": {
"1": {
"games": 24,
"wins": 13
},
"2": {
"games": 24,
"wins": 6
},
"3": {
"games": 24,
"wins": 2
},
"4": {
"games": 24,
"wins": 8
},
"5": {
"games": 24,
"wins": 10
},
"6": {
"games": 24,
"wins": 7
},
"7": {
"games": 24,
"wins": 5
}
},
"equipment": {
"machete": 71,
"map": 59,
"flare": 79,
"aid": 84,
"water": 79,
"shovel": 40,
"rope": 60,
"torch": 17,
"compass": 10,
"radio": 24,
"binoculars": 22,
"tape": 7
},
"stalled": []
},
{
"name": "current",
"family": "cutoff-training",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 168,
"wins": 51,
"moves": 10097,
"byScenario": {
"1": {
"games": 24,
"wins": 13
},
"2": {
"games": 24,
"wins": 6
},
"3": {
"games": 24,
"wins": 2
},
"4": {
"games": 24,
"wins": 8
},
"5": {
"games": 24,
"wins": 10
},
"6": {
"games": 24,
"wins": 7
},
"7": {
"games": 24,
"wins": 5
}
},
"equipment": {
"machete": 71,
"map": 59,
"flare": 79,
"aid": 84,
"water": 79,
"shovel": 40,
"rope": 60,
"torch": 17,
"compass": 10,
"radio": 24,
"binoculars": 22,
"tape": 7
},
"stalled": []
}
],
"entranceOnlyValidation": {
"firstBatch": {
"games": 252,
"beforeWins": 71,
"afterWins": 73
},
"secondBatch": {
"name": "route",
"family": "cutoff-heldout",
"policy": {
"equipment": true,
"lead": 1,
"progress": 4,
"success": 18,
"stamina": 1,
"teamwork": 0.4,
"cost": 0.8
},
"games": 252,
"wins": 57,
"moves": 14156,
"byScenario": {
"1": {
"games": 36,
"wins": 20
},
"2": {
"games": 36,
"wins": 3
},
"3": {
"games": 36,
"wins": 10
},
"4": {
"games": 36,
"wins": 3
},
"5": {
"games": 36,
"wins": 7
},
"6": {
"games": 36,
"wins": 9
},
"7": {
"games": 36,
"wins": 5
}
},
"equipment": {
"water": 130,
"flare": 88,
"aid": 151,
"torch": 28,
"compass": 50,
"tape": 11,
"radio": 68,
"shovel": 48,
"map": 42,
"machete": 45,
"rope": 10,
"binoculars": 13
},
"stalled": []
}
},
"conclusion": "130/504 wins before and after (25.8%). Retained for concrete tactical behavior, not a demonstrated win-rate improvement."
}

View file

@ -1,6 +1,6 @@
{
"name": "fuji-engine",
"version": "0.1.3",
"version": "0.1.4",
"private": true,
"license": "AGPL-3.0-only",
"files": [

View file

@ -53,15 +53,15 @@ function villageHelper(game: View, seat: number): boolean {
);
}
function distancesToVillage(game: View): Map<string, number> {
function distancesToVillage(game: View, blocked = new Set<string>()): Map<string, number> {
// Measure progress along the trail, including detours around gaps and lava.
const villageDistances = new Map<string, number>();
const frontier = game.board.filter((c) => walkable(c) && terrain(c.terrain).kind === "village");
const frontier = game.board.filter((c) => walkable(c) && !blocked.has(c.id) && terrain(c.terrain).kind === "village");
frontier.forEach((c) => villageDistances.set(c.id, 0));
for (let i = 0; i < frontier.length; i++) {
const current = frontier[i]!;
for (const next of game.board) {
if (!walkable(next) || villageDistances.has(next.id)) continue;
if (!walkable(next) || blocked.has(next.id) || villageDistances.has(next.id)) continue;
if (Math.abs(current.x - next.x) + Math.abs(current.y - next.y) !== 1) continue;
villageDistances.set(next.id, villageDistances.get(current.id)! + 1);
frontier.push(next);
@ -418,39 +418,78 @@ export function chooseMove(game: View, seat: number, policy: AiPolicy = DEFAULT_
// Only public routes matter; never inspect their hidden rolls.
const arrivals =
terrain(cell(game, p.position).terrain).kind === "village"
? neighbors(game, seat)
.filter(
(i) =>
!game.players[i]!.ready && terrain(cell(game, game.players[i]!.position).terrain).kind !== "village"
)
.map((i) => {
const claimed = new Set(
neighbors(game, i)
.filter((j) => j !== seat && game.players[j]!.ready)
.map((j) => game.players[j]!.path.at(-1))
);
const options = Object.values(paths(game, i)).filter((route) => {
const target = cell(game, route.at(-1)!);
return (
terrain(target.terrain).kind === "village" &&
!danger.has(target.id) &&
!claimed.has(target.id) &&
!route.slice(1).some((id) => cell(game, id).eruption)
);
});
return { seat: i, options };
})
? neighbors(game, seat).flatMap((i) => {
const other = game.players[i]!;
const origin = other.ready ? other.path.at(-1)! : other.position;
if (terrain(cell(game, origin).terrain).kind === "village") return [];
const limit = hasSkill(other, "scout") ? 4 : 3;
const claimed = new Set(
neighbors(game, i)
.filter((j) => j !== seat && game.players[j]!.ready)
.map((j) => game.players[j]!.path.at(-1))
);
// Look one move beyond current reach, so residents clear entrances
// before a teammate arrives. Compare public geometry, not secret dice.
const distances = new Map([[origin, 0]]);
const queue = [origin];
for (let index = 0; index < queue.length; index++) {
const id = queue[index]!,
from = cell(game, id),
steps = distances.get(id)!;
if (steps >= limit * 2) continue;
for (const next of game.board) {
if (
distances.has(next.id) ||
!walkable(next) ||
next.eruption ||
Math.abs(next.x - from.x) + Math.abs(next.y - from.y) !== 1
)
continue;
distances.set(next.id, steps + 1);
queue.push(next.id);
}
}
const reachable = game.board.filter(
(c) =>
terrain(c.terrain).kind === "village" && !danger.has(c.id) && !claimed.has(c.id) && distances.has(c.id)
);
const immediate = other.ready ? [] : reachable.filter((c) => distances.get(c.id)! <= limit);
const options = immediate.length ? immediate : reachable;
return [{ seat: i, options, distances, weight: immediate.length ? 1 : 0.5 }];
})
: [];
const entranceCost = (destination: string) =>
arrivals.reduce((cost, arrival) => {
const route = arrival.options.find((route) => route.at(-1) === destination);
if (!route) return cost;
// Fewer alternatives and shorter approaches make this entrance more valuable.
return cost + (18 * urgency(game, arrival.seat)) / arrival.options.length / Math.max(1, route.length - 1);
if (!arrival.options.some((c) => c.id === destination)) return cost;
return (
cost +
(36 * arrival.weight * urgency(game, arrival.seat)) /
arrival.options.length /
Math.max(1, arrival.distances.get(destination)!)
);
}, 0);
const villageDistances = distancesToVillage(game);
const forecasts = new Map<number, { lava: Set<string>; distances: Map<string, number> }>();
const forecast = (waves: number) => {
const cached = forecasts.get(waves);
if (cached) return cached;
const lava = new Set(game.board.filter((c) => c.lava).map((c) => c.id));
for (let wave = 0; wave < waves; wave++) {
const spread = game.board.filter(
(c) =>
!lava.has(c.id) && game.board.some((l) => lava.has(l.id) && Math.abs(c.x - l.x) + Math.abs(c.y - l.y) === 1)
);
spread.forEach((c) => lava.add(c.id));
}
const result = { lava, distances: distancesToVillage(game, lava) };
forecasts.set(waves, result);
return result;
};
const score = (path: string[]) => {
const c = cell(game, path.at(-1)!);
const waves = 1 + path.slice(1).reduce((sum, id) => sum + cell(game, id).eruption, 0);
const future = forecast(waves);
const cutOff = terrain(c.terrain).kind !== "village" && !future.distances.has(c.id);
const closestVillage = villageDistances.get(c.id) ?? game.board.length;
const lavaDistance = Math.min(
...game.board.filter((tile) => tile.lava).map((tile) => Math.abs(c.x - tile.x) + Math.abs(c.y - tile.y))
@ -479,7 +518,7 @@ export function chooseMove(game: View, seat: number, policy: AiPolicy = DEFAULT_
);
const expectedLead = expected - Math.max(...opposition);
return (
(danger.has(c.id) ? -1000 : 0) -
(future.lava.has(c.id) ? -1000 : cutOff ? -150 : 0) -
12 / Math.max(1, lavaDistance) +
expectedLead * policy.lead -
closestVillage * policy.progress +

View file

@ -1357,3 +1357,65 @@ test("players choose distinct skills before equipment is dealt, with exact repla
assert.deepEqual(activePlayers(s), [0, 1]);
assert.deepEqual(replay(s), s);
});
test("village residents clear entrances before a neighbor is within one move", async () => {
const { chooseMove } = await import("../index.js");
const s = initGame(3, {}, "future-entrance");
s.phase = "planning";
s.board.forEach((c) => {
c.lava = false;
c.eruption = 0;
if (terrain(c.terrain).kind === "village") c.terrain = 28;
});
s.players.forEach((p, i) => {
p.cards = [];
p.ready = false;
p.position = ["4,2", "1,5", "6,0"][i]!;
p.path = [p.position];
p.dice.forEach((d) => (d.face = 1));
});
assert(!Object.keys(paths(s, 1)).some((id) => terrain(cell(s, id).terrain).kind === "village"));
for (const ready of [false, true]) {
s.players[1]!.ready = ready;
const move = chooseMove(stripSecret(s, 0), 0);
assert.equal(move.action, "plan");
assert.notEqual((move.path as string[]).at(-1), "4,2");
assert.equal(terrain(cell(s, (move.path as string[]).at(-1)!).terrain).kind, "village");
s.players[1]!.dice.forEach((d) => (d.face = 6));
assert.deepEqual(chooseMove(stripSecret(s, 0), 0), move);
}
});
test("AI crosses a choke point before lava disconnects the otherwise safe side", async () => {
const { chooseMove } = await import("../index.js");
const s = initGame(3, {}, "cutoff-regression");
s.phase = "planning";
s.board = Array.from({ length: 6 }, (_, x) => ({
id: `${x},0`,
x,
y: 0,
terrain: x === 5 ? 33 : x === 3 ? 15 : 16,
lava: false,
equipment: false,
eruption: 0,
}));
s.board.push({ id: "2,1", x: 2, y: 1, terrain: 1, lava: true, equipment: false, eruption: 0 });
s.players.forEach((p, i) => {
p.cards = [];
p.ready = false;
p.position = i === 0 ? "0,0" : "5,0";
p.path = [p.position];
p.dice.forEach((d) => (d.face = 6));
});
assert(!threatened(s).includes("1,0"), "the tempting tile itself is not threatened");
assert(threatened(s).includes("2,0"), "the connecting bridge will burn");
const move = chooseMove(stripSecret(s, 0), 0);
assert.deepEqual(move, { action: "plan", path: ["0,0", "1,0", "2,0", "3,0"] });
cell(s, "3,0").eruption = 1;
const extraWave = chooseMove(stripSecret(s, 0), 0);
assert.notEqual(
(extraWave.path as string[] | undefined)?.at(-1),
"3,0",
"the extra wave would consume that destination"
);
});