From 958952305ad7958a439ee3b2c2ada27c27aafc1e Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Thu, 29 Oct 2026 14:48:02 +0000 Subject: [PATCH 1/2] test: add seeded graph perturbation helpers perturbEdges and addRandomEdges take their random source from the caller, and shuffle with Fisher-Yates instead of a biased sort comparator. --- .../validation/common/graph-perturbation.ts | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/experiments/evaluation/__tests__/validation/common/graph-perturbation.ts diff --git a/src/experiments/evaluation/__tests__/validation/common/graph-perturbation.ts b/src/experiments/evaluation/__tests__/validation/common/graph-perturbation.ts new file mode 100644 index 0000000..39b18a6 --- /dev/null +++ b/src/experiments/evaluation/__tests__/validation/common/graph-perturbation.ts @@ -0,0 +1,77 @@ +/** + * Seeded graph perturbation helpers for robustness tests. + * + * Every random choice is drawn from a caller-supplied generator so a test's + * perturbed graphs are identical on every run. + */ + +export interface PerturbableEdge { + source: string; + target: string; +} + +/** + * Pseudo-random source returning values in [0, 1), such as `createSeededRng(seed)`. + */ +export type RandomSource = () => number; + +/** + * Creates a perturbed copy of an edge list by removing a random subset of edges. + * The subset is chosen by a Fisher-Yates shuffle, so every subset of the kept size is equally likely. + * @param edges - Original edges, left unmodified + * @param removeFraction - Fraction of edges to remove, in [0, 1] + * @param random - Source of randomness + * @returns The `floor(edges.length * (1 - removeFraction))` kept edges, in shuffled order + */ +export const perturbEdges = ( + edges: readonly E[], + removeFraction: number, + random: RandomSource, +): E[] => { + const shuffled = [...edges]; + for (let index = shuffled.length - 1; index > 0; index--) { + const swapIndex = Math.floor(random() * (index + 1)); + [shuffled[index], shuffled[swapIndex]] = [shuffled[swapIndex], shuffled[index]]; + } + + return shuffled.slice(0, Math.floor(edges.length * (1 - removeFraction))); +}; + +/** + * Creates a perturbed copy of an edge list by adding random non-loop edges that are not already present. + * Gives up after ten attempts per requested edge, so a near-complete graph may gain fewer edges than requested. + * @param edges - Original edges, left unmodified + * @param nodeIds - Nodes new edges may connect + * @param addFraction - Number of edges to add as a fraction of `edges.length` + * @param random - Source of randomness + * @returns The original edges followed by the added edges + */ +export const addRandomEdges = ( + edges: readonly E[], + nodeIds: readonly string[], + addFraction: number, + random: RandomSource, +): Array => { + const addCount = Math.floor(edges.length * addFraction); + const newEdges: Array = [...edges]; + const existing = new Set(edges.map((e) => `${e.source}-${e.target}`)); + + let added = 0; + let attempts = 0; + const maxAttempts = addCount * 10; + + while (added < addCount && attempts < maxAttempts) { + const source = nodeIds[Math.floor(random() * nodeIds.length)]; + const target = nodeIds[Math.floor(random() * nodeIds.length)]; + const key = `${source}-${target}`; + + if (source !== target && !existing.has(key)) { + newEdges.push({ source, target }); + existing.add(key); + added++; + } + attempts++; + } + + return newEdges; +}; From 8220ee4c1e04d07bfc886fd24b1cfc23b32bb8e4 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Thu, 29 Oct 2026 14:48:08 +0000 Subject: [PATCH 2/2] fix(test): seed the perturbed graph robustness tests The method ranking test compared path diversity on graphs perturbed with Math.random, so DP lost on two of three perturbations on a fraction of runs and failed CI nondeterministically. Both copies of the test now draw their perturbations from a fixed seeded generator. --- .../thesis-validation.exp.integration.test.ts | 61 +++--------------- ...bation-consistency.exp.integration.test.ts | 63 +++---------------- 2 files changed, 19 insertions(+), 105 deletions(-) diff --git a/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts b/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts index 0998a05..64aee88 100644 --- a/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts +++ b/src/experiments/evaluation/__tests__/thesis-validation.exp.integration.test.ts @@ -19,11 +19,13 @@ import type { GraphExpander, Neighbor } from "../../../interfaces/graph-expander import { FrontierBalancedExpansion } from "../../baselines/frontier-balanced"; import { RandomPriorityExpansion } from "../../baselines/random-priority"; import { StandardBfsExpansion } from "../../baselines/standard-bfs"; +import { createSeededRng } from "../../proofs/test-utils"; import { createBenchmarkMeta, loadBenchmarkByIdFromUrl, loadBenchmarkFromContent, } from "../fixtures/benchmark-datasets"; +import { addRandomEdges, perturbEdges } from "./validation/common/graph-perturbation"; // ============================================================================ // Benchmark Graph Adapter @@ -1203,61 +1205,15 @@ describe("Thesis Validation: Summary", () => { // ========================================================================= describe("Thesis Validation: Perturbed Graph Robustness", () => { - /** - * Create a perturbed version of a graph by removing random edges. - * @param edges - * @param removeFraction - */ - const perturbEdges = ( - edges: Array<{ source: string; target: string }>, - removeFraction: number - ): Array<{ source: string; target: string }> => { - const shuffled = [...edges].sort(() => Math.random() - 0.5); - const keepCount = Math.floor(edges.length * (1 - removeFraction)); - return shuffled.slice(0, keepCount); - }; - - /** - * Create a perturbed version by adding random edges. - * @param edges - * @param nodeIds - * @param addFraction - */ - const addRandomEdges = ( - edges: Array<{ source: string; target: string }>, - nodeIds: string[], - addFraction: number - ): Array<{ source: string; target: string }> => { - const addCount = Math.floor(edges.length * addFraction); - const newEdges: Array<{ source: string; target: string }> = [...edges]; - const existing = new Set(edges.map((e) => `${e.source}-${e.target}`)); - - let added = 0; - let attempts = 0; - const maxAttempts = addCount * 10; - - while (added < addCount && attempts < maxAttempts) { - const source = nodeIds[Math.floor(Math.random() * nodeIds.length)]; - const target = nodeIds[Math.floor(Math.random() * nodeIds.length)]; - const key = `${source}-${target}`; - - if (source !== target && !existing.has(key)) { - newEdges.push({ source, target }); - existing.add(key); - added++; - } - attempts++; - } - - return newEdges; - }; + /** Fixed so the perturbed graphs, and therefore the method ranking, are identical on every run. */ + const PERTURBATION_SEED = 1; it("should maintain performance under edge removal", async () => { const benchmark = await loadBenchmarkByIdFromUrl("karate"); const originalEdges = benchmark.graph.getAllEdges(); // Remove 10% of edges - const perturbedEdges = perturbEdges(originalEdges, 0.1); + const perturbedEdges = perturbEdges(originalEdges, 0.1, createSeededRng(PERTURBATION_SEED)); const perturbedMeta = createBenchmarkMeta({ id: "karate-perturbed-removed", @@ -1294,7 +1250,7 @@ describe("Thesis Validation: Summary", () => { const allNodes = benchmark.graph.getAllNodes().map((n) => n.id); // Add 20% more random edges - const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2); + const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2, createSeededRng(PERTURBATION_SEED)); const perturbedMeta = createBenchmarkMeta({ id: "karate-perturbed-added", @@ -1327,13 +1283,14 @@ describe("Thesis Validation: Summary", () => { const benchmark = await loadBenchmarkByIdFromUrl("lesmis"); const originalEdges = benchmark.graph.getAllEdges(); + const random = createSeededRng(PERTURBATION_SEED); const results: Array<{ perturbation: string; dpDiversity: number; bfsDiversity: number }> = []; // Test original and two perturbed versions const perturbations = [ { name: "original", edges: originalEdges }, - { name: "5% removed", edges: perturbEdges(originalEdges, 0.05) }, - { name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1) }, + { name: "5% removed", edges: perturbEdges(originalEdges, 0.05, random) }, + { name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1, random) }, ]; for (const { name, edges } of perturbations) { diff --git a/src/experiments/evaluation/__tests__/validation/comparative/perturbation-consistency.exp.integration.test.ts b/src/experiments/evaluation/__tests__/validation/comparative/perturbation-consistency.exp.integration.test.ts index 5d12f0a..1a312f4 100644 --- a/src/experiments/evaluation/__tests__/validation/comparative/perturbation-consistency.exp.integration.test.ts +++ b/src/experiments/evaluation/__tests__/validation/comparative/perturbation-consistency.exp.integration.test.ts @@ -10,70 +10,26 @@ import { describe, expect, it } from "vitest"; import { DegreePrioritisedExpansion } from "../../../../../algorithms/traversal/degree-prioritised-expansion"; import { StandardBfsExpansion } from "../../../../../experiments/baselines/standard-bfs" +import { createSeededRng } from "../../../../proofs/test-utils"; import { createBenchmarkMeta, loadBenchmarkByIdFromUrl, loadBenchmarkFromContent, } from "../../../fixtures/benchmark-datasets"; import { BenchmarkGraphExpander } from "../common/benchmark-graph-expander"; +import { addRandomEdges, perturbEdges } from "../common/graph-perturbation"; import { pathDiversity } from "../common/statistical-functions"; -describe("Thesis Validation: Perturbed Graph Robustness", () => { - /** - * Create a perturbed version of a graph by removing random edges. - * @param edges - * @param removeFraction - */ - const perturbEdges = ( - edges: Array<{ source: string; target: string }>, - removeFraction: number - ): Array<{ source: string; target: string }> => { - const shuffled = [...edges].sort(() => Math.random() - 0.5); - const keepCount = Math.floor(edges.length * (1 - removeFraction)); - return shuffled.slice(0, keepCount); - }; - - /** - * Create a perturbed version by adding random edges. - * @param edges - * @param nodeIds - * @param addFraction - */ - const addRandomEdges = ( - edges: Array<{ source: string; target: string }>, - nodeIds: string[], - addFraction: number - ): Array<{ source: string; target: string }> => { - const addCount = Math.floor(edges.length * addFraction); - const newEdges: Array<{ source: string; target: string }> = [...edges]; - const existing = new Set(edges.map((e) => `${e.source}-${e.target}`)); - - let added = 0; - let attempts = 0; - const maxAttempts = addCount * 10; - - while (added < addCount && attempts < maxAttempts) { - const source = nodeIds[Math.floor(Math.random() * nodeIds.length)]; - const target = nodeIds[Math.floor(Math.random() * nodeIds.length)]; - const key = `${source}-${target}`; - - if (source !== target && !existing.has(key)) { - newEdges.push({ source, target }); - existing.add(key); - added++; - } - attempts++; - } - - return newEdges; - }; +/** Fixed so the perturbed graphs, and therefore the method ranking, are identical on every run. */ +const PERTURBATION_SEED = 1; +describe("Thesis Validation: Perturbed Graph Robustness", () => { it("should maintain performance under edge removal", async () => { const benchmark = await loadBenchmarkByIdFromUrl("karate"); const originalEdges = benchmark.graph.getAllEdges(); // Remove 10% of edges - const perturbedEdges = perturbEdges(originalEdges, 0.1); + const perturbedEdges = perturbEdges(originalEdges, 0.1, createSeededRng(PERTURBATION_SEED)); const perturbedMeta = createBenchmarkMeta({ id: "karate-perturbed-removed", @@ -110,7 +66,7 @@ describe("Thesis Validation: Perturbed Graph Robustness", () => { const allNodes = benchmark.graph.getAllNodes().map((n) => n.id); // Add 20% more random edges - const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2); + const perturbedEdges = addRandomEdges(originalEdges, allNodes, 0.2, createSeededRng(PERTURBATION_SEED)); const perturbedMeta = createBenchmarkMeta({ id: "karate-perturbed-added", @@ -144,13 +100,14 @@ describe("Thesis Validation: Perturbed Graph Robustness", () => { const benchmark = await loadBenchmarkByIdFromUrl("lesmis"); const originalEdges = benchmark.graph.getAllEdges(); + const random = createSeededRng(PERTURBATION_SEED); const results: Array<{ perturbation: string; dpDiversity: number; bfsDiversity: number }> = []; // Test original and two perturbed versions const perturbations = [ { name: "original", edges: originalEdges }, - { name: "5% removed", edges: perturbEdges(originalEdges, 0.05) }, - { name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1) }, + { name: "5% removed", edges: perturbEdges(originalEdges, 0.05, random) }, + { name: "10% added", edges: addRandomEdges(originalEdges, benchmark.graph.getAllNodes().map((n) => n.id), 0.1, random) }, ]; for (const { name, edges } of perturbations) {