|
| 1 | +//! CAKES + CHAODA over the HHTL basins of a real grid core — the similarity |
| 2 | +//! (attraction) / anomaly (repulsion) pair from the CLAM family, scoring grid |
| 3 | +//! compartments the way CHAODA scores papillary muscles or terrain tiles. |
| 4 | +//! |
| 5 | +//! HHTL is the family basin ("where"); CAKES finds each basin's relatives ("who |
| 6 | +//! looks similar"); CHAODA scores how far a basin sits from its family ("why am I |
| 7 | +//! different") — the top-anomaly basin is the fail-first compartment. |
| 8 | +//! |
| 9 | +//! Run: cargo run --release --manifest-path crates/perturbation-sim/Cargo.toml \ |
| 10 | +//! --example chaoda -- /tmp/pypsa/buses.csv /tmp/pypsa/lines.csv ES |
| 11 | +//! (no args → a synthetic 8×8 grid with one deliberately weakened block). |
| 12 | +
|
| 13 | +use perturbation_sim::{ |
| 14 | + anomaly_ranking, cakes_neighbors, chaoda_scores, resilience_basin_features, Edge, Grid, |
| 15 | + HhtlKey, CHAODA_FLAG, |
| 16 | +}; |
| 17 | + |
| 18 | +/// Synthetic 8×8 lattice with one corner block weakened (low-susceptance internal |
| 19 | +/// edges) — a planted fail-first compartment for the no-data demo. |
| 20 | +fn synthetic() -> Grid { |
| 21 | + let (rows, cols) = (8usize, 8usize); |
| 22 | + let id = |r: usize, c: usize| r * cols + c; |
| 23 | + let mut e = Vec::new(); |
| 24 | + for r in 0..rows { |
| 25 | + for c in 0..cols { |
| 26 | + // The 3×3 top-left block is brittle (weak internal coupling). |
| 27 | + let brittle = r < 3 && c < 3; |
| 28 | + let b = if brittle { 0.05 } else { 1.0 }; |
| 29 | + if c + 1 < cols { |
| 30 | + e.push(Edge::new(id(r, c), id(r, c + 1), b, 1.0)); |
| 31 | + } |
| 32 | + if r + 1 < rows { |
| 33 | + e.push(Edge::new(id(r, c), id(r + 1, c), b, 1.0)); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + Grid::new(rows * cols, e) |
| 38 | +} |
| 39 | + |
| 40 | +/// Deterministic per-bus inertia proxy (real `H` is not in PyPSA topology): a small |
| 41 | +/// fixed cycle, decoupled from wiring, so the buffer axis is an independent input — |
| 42 | +/// the structure (basins as families, the outlier as fail-first) holds regardless. |
| 43 | +fn proxy_inertia(n: usize) -> Vec<f64> { |
| 44 | + (0..n).map(|i| 2.0 + (i % 5) as f64).collect() |
| 45 | +} |
| 46 | + |
| 47 | +fn fmt_key(k: &HhtlKey) -> String { |
| 48 | + format!("{}.{}.{}", k.heel, k.hip, k.twig) |
| 49 | +} |
| 50 | + |
| 51 | +fn main() { |
| 52 | + let args: Vec<String> = std::env::args().collect(); |
| 53 | + let grid = if args.len() >= 3 { |
| 54 | + let buses = std::fs::read_to_string(&args[1]).expect("buses.csv"); |
| 55 | + let lines = std::fs::read_to_string(&args[2]).expect("lines.csv"); |
| 56 | + let cc = args.get(3).map(|s| s.as_str()).unwrap_or("ES"); |
| 57 | + let imp = perturbation_sim::from_pypsa_csv(&buses, &lines, Some(cc)) |
| 58 | + .expect("import") |
| 59 | + .largest_component(); |
| 60 | + println!("grid: {cc} PyPSA core — {} buses", imp.grid.n); |
| 61 | + imp.grid |
| 62 | + } else { |
| 63 | + let g = synthetic(); |
| 64 | + println!( |
| 65 | + "grid: synthetic 8×8 with a planted brittle 3×3 block — {} buses", |
| 66 | + g.n |
| 67 | + ); |
| 68 | + g |
| 69 | + }; |
| 70 | + |
| 71 | + let h = proxy_inertia(grid.n); |
| 72 | + let (basins, rows) = resilience_basin_features(&grid, &h, 0.2); |
| 73 | + let scores = chaoda_scores(&rows, 2); |
| 74 | + |
| 75 | + println!("\n== HHTL family basins — CAKES (similar) + CHAODA (anomalous) =="); |
| 76 | + println!( |
| 77 | + " {:>10} {:>6} {:>8} {:>8} {:>8} flag", |
| 78 | + "basin", "size", "λ₂n", "inertia", "CHAODA" |
| 79 | + ); |
| 80 | + for (i, k) in basins.iter().enumerate() { |
| 81 | + let flag = if scores[i] >= CHAODA_FLAG { |
| 82 | + "◀ ANOMALY" |
| 83 | + } else { |
| 84 | + "" |
| 85 | + }; |
| 86 | + println!( |
| 87 | + " {:>10} {:>6.2} {:>8.3} {:>8.3} {:>8.3} {}", |
| 88 | + fmt_key(k), |
| 89 | + rows[i][1], |
| 90 | + rows[i][0], |
| 91 | + rows[i][2], |
| 92 | + scores[i], |
| 93 | + flag |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + // CHAODA: the fail-first compartment is the top anomaly. |
| 98 | + let rank = anomaly_ranking(&rows, 2); |
| 99 | + if let Some(&(top, score)) = rank.first() { |
| 100 | + println!( |
| 101 | + "\n CHAODA → fail-first compartment: basin {} (score {:.3}{})\n \ |
| 102 | + \"why am I different\" — the basin whose resilience profile deviates most\n \ |
| 103 | + from its family. {} basins; flag threshold {CHAODA_FLAG}.", |
| 104 | + fmt_key(&basins[top]), |
| 105 | + score, |
| 106 | + if score >= CHAODA_FLAG { |
| 107 | + ", FLAGGED" |
| 108 | + } else { |
| 109 | + "" |
| 110 | + }, |
| 111 | + basins.len() |
| 112 | + ); |
| 113 | + |
| 114 | + // CAKES: the top-anomaly basin's nearest relatives ("who looks similar"). |
| 115 | + let nbrs = cakes_neighbors(&rows, top, 3); |
| 116 | + let rel: Vec<String> = nbrs |
| 117 | + .iter() |
| 118 | + .map(|(i, d)| format!("{} (d={:.3})", fmt_key(&basins[*i]), d)) |
| 119 | + .collect(); |
| 120 | + println!( |
| 121 | + " CAKES → {}'s nearest relatives: [{}]\n \ |
| 122 | + attraction vs repulsion: the family it resembles, and how far it still sits from them.", |
| 123 | + fmt_key(&basins[top]), |
| 124 | + rel.join(", ") |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + println!( |
| 129 | + "\n CAKES pulls in the similar; CHAODA pushes out the unusual.\n \ |
| 130 | + Family basin (HHTL) + deviation-from-family (CHAODA) = the fail-first locator.\n \ |
| 131 | + (CHAODA-lite kNN scorer; ndarray::clam's ClamTree ensemble is the gated production path.)" |
| 132 | + ); |
| 133 | +} |
0 commit comments