(Adaptive Loop) Pass 2
This commit is contained in:
1 parent
7fcbfcd476
commit
4c39fe67f1
34 files changed
+5990
No files matched your search
@@ -0,0 +1,218 @@
|
||||
// Bounded deterministic state-space search. Proves each baseline scenario has a
|
||||
// winning route and a losing route, verifies every reachable campaign inheritance
|
||||
// combination stays solvable, and flags route properties (ownership change,
|
||||
// adaptation exploit, adaptation avoidance). Offline-only; not imported by the UI.
|
||||
|
||||
import { ALL_ACTIONS } from "./constants";
|
||||
import { nextState } from "./pipeline";
|
||||
import { buildBaselineState, emptyInheritance, newlyUnlockedTraits, type Inheritance, withPreservedTrait } from "./scenario";
|
||||
import { canonicalStringify } from "./serialize";
|
||||
import { getScenario, SCENARIOS } from "../content/scenarios";
|
||||
import type { GameState, LineageId, PressureId, RotationAction } from "./types";
|
||||
|
||||
// Dedup key: a fast deterministic string over the transition-relevant fields
|
||||
// (action log excluded — it never affects future transitions). Hand-rolled in a
|
||||
// fixed field order so semantically-equal states collapse without a recursive
|
||||
// key-sort on every node.
|
||||
export function stateKey(s: GameState): string {
|
||||
const c = s.chambers;
|
||||
let out = `${s.scenarioId}|${s.turn}|${s.outcome}|${s.consecutiveObjectiveTurns}|`;
|
||||
out += `${s.loops.A.direction}${s.loops.B.direction}${s.loops.C.direction}|`;
|
||||
out += `${s.sharedOwner[2]}${s.sharedOwner[3]}${s.sharedOwner[4]}${s.sharedOwner[5]}${s.sharedOwner[8]}${s.sharedOwner[9]}|`;
|
||||
out += `${s.environmentActive.heat ? 1 : 0}${s.environmentActive.hunger ? 1 : 0}${s.environmentActive.toxin ? 1 : 0}${s.environmentActive.immunity ? 1 : 0}|`;
|
||||
const lp = s.lineage;
|
||||
out += `${lp.producer.pressure.heat}${lp.producer.pressure.hunger}${lp.producer.pressure.toxin}${lp.producer.pressure.immunity}${Number(lp.producer.unlocked.heat)}${Number(lp.producer.unlocked.hunger)}${Number(lp.producer.unlocked.toxin)}${Number(lp.producer.unlocked.immunity)}`;
|
||||
out += `${lp.grazer.pressure.heat}${lp.grazer.pressure.hunger}${lp.grazer.pressure.toxin}${lp.grazer.pressure.immunity}${Number(lp.grazer.unlocked.heat)}${Number(lp.grazer.unlocked.hunger)}${Number(lp.grazer.unlocked.toxin)}${Number(lp.grazer.unlocked.immunity)}|`;
|
||||
for (let i = 0; i < c.length; i++) {
|
||||
out += `${c[i].nutrient},${c[i].heat},${c[i].toxin},${c[i].producer},${c[i].grazer},${c[i].phage};`;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export type Route = {
|
||||
actions: RotationAction[];
|
||||
outcome: GameState["outcome"];
|
||||
unlocks: Record<LineageId, PressureId[]>;
|
||||
ownershipChanged: boolean;
|
||||
maxPressure: number;
|
||||
};
|
||||
|
||||
function describeRoute(base: GameState, actions: RotationAction[]): Route {
|
||||
let s = base;
|
||||
const seenOwners = new Set(Object.keys(s.sharedOwner).map((k) => `${k}:${s.sharedOwner[k]}`));
|
||||
let ownershipChanged = false;
|
||||
let maxPressure = 0;
|
||||
for (const a of actions) {
|
||||
const r = nextState(s, a);
|
||||
s = r.state;
|
||||
for (const k of Object.keys(s.sharedOwner)) {
|
||||
const tag = `${k}:${s.sharedOwner[k]}`;
|
||||
if (!seenOwners.has(tag)) { ownershipChanged = true; seenOwners.add(tag); }
|
||||
}
|
||||
for (const lin of ["producer", "grazer"] as LineageId[]) for (const p of ["heat", "hunger", "toxin", "immunity"] as PressureId[]) maxPressure = Math.max(maxPressure, s.lineage[lin].pressure[p]);
|
||||
if (s.outcome !== "playing") break;
|
||||
}
|
||||
return {
|
||||
actions,
|
||||
outcome: s.outcome,
|
||||
unlocks: newlyUnlockedTraits(s),
|
||||
ownershipChanged,
|
||||
maxPressure,
|
||||
};
|
||||
}
|
||||
|
||||
// Compact 64-bit hash for dedup (avoids storing ~300-char keys per node).
|
||||
// Two FNV-1a passes with different seeds, combined into a hex string.
|
||||
function hash64(s: GameState): string {
|
||||
const key = stateKey(s);
|
||||
let h1 = 0x811c9dc5;
|
||||
let h2 = 0x1ba48dc5;
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
const c = key.charCodeAt(i);
|
||||
h1 = Math.imul(h1 ^ c, 0x01000193);
|
||||
h2 = Math.imul(h2 ^ c, 0x05033143);
|
||||
}
|
||||
return (h1 >>> 0).toString(16).padStart(8, "0") + (h2 >>> 0).toString(16).padStart(8, "0");
|
||||
}
|
||||
|
||||
// Recursive DFS for a winning route. Memory-bounded: no node array, no per-push
|
||||
// path copy — only the live recursion path (≤ turnLimit) and a visited set of
|
||||
// 64-bit hashes. Deterministic (fixed action order).
|
||||
export function findWin(scenarioId: string, inherited: Inheritance = emptyInheritance(), nodeCap = 1_500_000): Route | null {
|
||||
const sc = getScenario(scenarioId);
|
||||
const base = buildBaselineState(scenarioId, "campaign", inherited);
|
||||
const visited = new Set<string>([hash64(base)]);
|
||||
let count = 0;
|
||||
let result: RotationAction[] | null = null;
|
||||
const path: RotationAction[] = [];
|
||||
const dfs = (state: GameState): boolean => {
|
||||
if (count >= nodeCap) return false;
|
||||
count++;
|
||||
if (state.outcome === "won") { result = path.slice(); return true; }
|
||||
if (state.outcome !== "playing") return false;
|
||||
if (path.length >= sc.turnLimit) return false;
|
||||
for (const action of ALL_ACTIONS) {
|
||||
const ns = nextState(state, action).state;
|
||||
const h = hash64(ns);
|
||||
if (visited.has(h)) continue;
|
||||
visited.add(h);
|
||||
path.push(action);
|
||||
if (dfs(ns)) return true;
|
||||
path.pop();
|
||||
}
|
||||
return false;
|
||||
};
|
||||
dfs(base);
|
||||
return result ? describeRoute(base, result) : null;
|
||||
}
|
||||
|
||||
// Search for a winning route that AVOIDS unlocking any adaptation (every pressure
|
||||
// track stays below 3 the whole route). SPEC §8/Gate 4 require at least one such
|
||||
// route. Prunes any branch where a pressure track reaches 3.
|
||||
export function findWinAvoidingAdaptation(scenarioId: string, inherited: Inheritance = emptyInheritance(), nodeCap = 1_500_000): Route | null {
|
||||
const sc = getScenario(scenarioId);
|
||||
const base = buildBaselineState(scenarioId, "campaign", inherited);
|
||||
const visited = new Set<string>([hash64(base)]);
|
||||
let count = 0;
|
||||
let result: RotationAction[] | null = null;
|
||||
const path: RotationAction[] = [];
|
||||
const pressuresHigh = (s: GameState) => {
|
||||
for (const lin of ["producer", "grazer"] as LineageId[]) for (const p of ["heat", "hunger", "toxin", "immunity"] as PressureId[]) if (s.lineage[lin].pressure[p] >= 3) return true;
|
||||
return false;
|
||||
};
|
||||
const dfs = (state: GameState): boolean => {
|
||||
if (count >= nodeCap) return false;
|
||||
count++;
|
||||
if (state.outcome === "won") { result = path.slice(); return true; }
|
||||
if (state.outcome !== "playing") return false;
|
||||
if (path.length >= sc.turnLimit) return false;
|
||||
for (const action of ALL_ACTIONS) {
|
||||
const ns = nextState(state, action).state;
|
||||
if (pressuresHigh(ns)) continue; // would unlock a trait — avoid
|
||||
const h = hash64(ns);
|
||||
if (visited.has(h)) continue;
|
||||
visited.add(h);
|
||||
path.push(action);
|
||||
if (dfs(ns)) return true;
|
||||
path.pop();
|
||||
}
|
||||
return false;
|
||||
};
|
||||
dfs(base);
|
||||
return result ? describeRoute(base, result) : null;
|
||||
}
|
||||
|
||||
// A losing route: a policy that reaches a non-win terminal. Try each fixed
|
||||
// single-action policy; the first that expires or collapses without winning wins.
|
||||
export function findLose(scenarioId: string, inherited: Inheritance = emptyInheritance()): Route | null {
|
||||
const base = buildBaselineState(scenarioId, "campaign", inherited);
|
||||
for (const fixed of ALL_ACTIONS) {
|
||||
let s = base;
|
||||
const path: RotationAction[] = [];
|
||||
while (s.outcome === "playing" && path.length < getScenario(scenarioId).turnLimit) {
|
||||
const r = nextState(s, fixed);
|
||||
s = r.state;
|
||||
path.push(fixed);
|
||||
}
|
||||
if (s.outcome === "collapsed" || s.outcome === "expired") return describeRoute(base, path);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Enumerate every reachable campaign inheritance set entering each scenario and
|
||||
// confirm each is solvable. SPEC §10 reachability verification.
|
||||
export type ReachabilityResult = {
|
||||
scenarioId: string;
|
||||
inheritance: Inheritance;
|
||||
solvable: boolean;
|
||||
route: Route | null;
|
||||
};
|
||||
|
||||
export function verifyReachableInheritance(
|
||||
perScenarioNodeCap: Record<string, number> = {},
|
||||
): { ok: boolean; results: ReachabilityResult[] } {
|
||||
const results: ReachabilityResult[] = [];
|
||||
// BFS over inheritance tree. Each scenario i is entered with some inheritance;
|
||||
// after a win, branch by preserving 0 or 1 newly-unlocked trait.
|
||||
let frontier: { scenarioIndex: number; inheritance: Inheritance }[] = [{ scenarioIndex: 0, inheritance: emptyInheritance() }];
|
||||
for (let i = 0; i < SCENARIOS.length; i++) {
|
||||
const sc = SCENARIOS[i];
|
||||
const cap = perScenarioNodeCap[sc.id] ?? 400_000;
|
||||
const nextFrontier: { scenarioIndex: number; inheritance: Inheritance }[] = [];
|
||||
const seenInh = new Set<string>();
|
||||
for (const f of frontier) {
|
||||
const key = canonicalStringify(f.inheritance);
|
||||
if (seenInh.has(key)) continue;
|
||||
seenInh.add(key);
|
||||
const route = findWin(sc.id, f.inheritance, cap);
|
||||
const solvable = route !== null;
|
||||
results.push({ scenarioId: sc.id, inheritance: f.inheritance, solvable, route });
|
||||
if (i < SCENARIOS.length - 1 && solvable) {
|
||||
const unlocks = route!.unlocks;
|
||||
// decline
|
||||
nextFrontier.push({ scenarioIndex: i + 1, inheritance: f.inheritance });
|
||||
// preserve each newly unlocked trait
|
||||
for (const lin of ["producer", "grazer"] as LineageId[]) {
|
||||
for (const p of unlocks[lin]) {
|
||||
nextFrontier.push({ scenarioIndex: i + 1, inheritance: withPreservedTrait(f.inheritance, { lineage: lin, pressure: p }) });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
frontier = nextFrontier;
|
||||
if (results.some((r) => !r.solvable)) break;
|
||||
}
|
||||
return { ok: results.every((r) => r.solvable), results };
|
||||
}
|
||||
|
||||
export function routeProperties(routes: Route[]): {
|
||||
anyOwnershipChange: boolean;
|
||||
anyExploitsAdaptation: boolean;
|
||||
anyAvoidsAdaptation: boolean;
|
||||
} {
|
||||
return {
|
||||
anyOwnershipChange: routes.some((r) => r.ownershipChanged),
|
||||
anyExploitsAdaptation: routes.some((r) => r.maxPressure >= 3),
|
||||
anyAvoidsAdaptation: routes.some((r) => r.maxPressure < 3),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user