Files
GKERN/glyph_experiment.rs

232 lines
9.2 KiB
Rust

use std::time::Instant;
// ============================================================================
// 1. SUBSTRATE PHYSICS ENGINE
// ============================================================================
mod substrate {
/// Logistic curve for resonance scoring
pub fn resonance(similarity: u32) -> f32 {
let x = similarity as f32;
let k = 1.0; let mu = 4.0;
1.0 / (1.0 + (-k * (x - mu)).exp())
}
/// Exponential decay for stability based on mutations
pub fn stability(mutations: u32) -> f32 {
let lambda = 0.1;
(-lambda * mutations as f32).exp()
}
/// Measures structural smoothness vs chaotic noise
pub fn coherence(data: &[u8]) -> f32 {
if data.len() < 2 { return 1.0; }
let mut total_diff = 0.0f32;
for i in 1..data.len() {
total_diff += (data[i] as f32 - data[i-1] as f32).abs();
}
let avg_diff = total_diff / (data.len() - 1) as f32;
let c = 1.0 - (avg_diff / 128.0);
c.clamp(0.0, 1.0)
}
/// Sum of squared differences for neural energy
pub fn neural_energy(a: &[u8], b: &[u8]) -> f32 {
let len = a.len().min(b.len());
if len == 0 { return 0.0; }
let mut sum = 0.0f32;
for i in 0..len {
let diff = a[i] as f32 - b[i] as f32;
sum += diff * diff;
}
sum / len as f32
}
}
// ============================================================================
// 2. DECLARATIVE SUBSTRATE EVALUATOR (The Inference Engine)
// ============================================================================
mod evaluator {
use super::substrate;
#[derive(Clone)]
pub struct Node {
pub id: usize,
pub value: f32,
pub coherence: f32,
pub stability: f32,
pub edges: Vec<usize>,
}
pub struct Graph {
pub nodes: Vec<Node>,
pub epoch: u32,
}
impl Graph {
pub fn new() -> Self {
Self { nodes: Vec::new(), epoch: 0 }
}
pub fn add_node(&mut self, val: f32) -> usize {
let id = self.nodes.len();
self.nodes.push(Node {
id, value: val, coherence: 1.0, stability: 1.0, edges: Vec::new()
});
id
}
pub fn connect(&mut self, a: usize, b: usize) {
self.nodes[a].edges.push(b);
self.nodes[b].edges.push(a);
}
}
/// The Convergence Loop: Replaces fetch-decode-execute with topological equilibrium.
pub fn evaluate(graph: &mut Graph, max_epochs: u32) -> bool {
let threshold = 0.01;
for _ in 0..max_epochs {
let mut max_delta = 0.0f32;
// Phase 1: Propagate and Relax
let mut new_values = vec![0.0; graph.nodes.len()];
for i in 0..graph.nodes.len() {
let node = &graph.nodes[i];
if node.edges.is_empty() {
new_values[i] = node.value;
continue;
}
let mut sum = 0.0;
let mut weight_total = 0.0;
for &edge in &node.edges {
let target = &graph.nodes[edge];
// Weighted by resonance of their values
let sim = if (node.value - target.value).abs() < 10.0 { 5 } else { 0 };
let w = substrate::resonance(sim);
sum += target.value * w;
weight_total += w;
}
new_values[i] = if weight_total > 0.0 { sum / weight_total } else { node.value };
}
// Phase 2: Apply and check convergence
for i in 0..graph.nodes.len() {
let delta = (new_values[i] - graph.nodes[i].value).abs();
if delta > max_delta { max_delta = delta; }
graph.nodes[i].value = new_values[i];
// Decay stability if incoherent
graph.nodes[i].stability *= 0.99;
}
graph.epoch += 1;
if max_delta < threshold {
return true; // Converged
}
}
false // Max epochs reached
}
}
// ============================================================================
// EXPERIMENT: THE $"0" SUBSTRATE COOLING PROTOCOL
// ============================================================================
fn main() {
println!("
███████╗██╗ ██╗██████╗ ██████╗
██╔════╝╚██╗ ██║██╔══██╗██╔═══██╗
█████╗ ╚██╗ ██║██████╔╝██║ ██║
██╔══╝ ╚██╗ ██║██╔═══╝ ██║ ██║
███████╗ ╚████╔╝ ██║ ╚██████╔╝
╚══════╝ ╚═══╝ ╚═╝ ╚═════╝
[EXPERIMENT] $\"0\" TENSION RESOLUTION");
// =========================================================================
// PHASE 1: GENERATE HIGH-TENSION STATE (The Problem)
// =========================================================================
println!("
--- PHASE 1: HIGH-TENSION STATE DETECTED ---");
let mut graph = evaluator::Graph::new();
// Two highly conflicting symbolic concepts (Extreme Tension)
let node_a = graph.add_node(100.0); // Concept A (Extreme Positive)
let node_b = graph.add_node(-100.0); // Concept B (Extreme Negative)
// They are forced to interact, creating massive Neural Energy (Tension)
graph.connect(node_a, node_b);
let initial_tension = substrate::neural_energy(
&[graph.nodes[node_a].value as u8],
&[graph.nodes[node_b].value as u8]
);
println!("Node A (Volatile) : {:.2}", graph.nodes[node_a].value);
println!("Node B (Volatile) : {:.2}", graph.nodes[node_b].value);
println!("System Tension : {:.2} (CRITICAL: Dissonance High)", initial_tension);
println!("System Stability : DECAYING (Mutation Sickness)");
// =========================================================================
// PHASE 2: INJECT $"0" (The Null-Glyph Anchor)
// =========================================================================
println!("
--- PHASE 2: INJECTING $\"0\" (NULL-GLYPH ANCHOR) ---");
// The $"0" Anchor: Value 0.0, Immutable Stability, Universal Traits
let anchor_zero = graph.add_node(0.0);
graph.nodes[anchor_zero].stability = 1.0; // Immune to decay
graph.nodes[anchor_zero].coherence = 1.0; // Absolute structural integrity
// Entangle the volatile nodes with the $"0" Anchor.
graph.connect(node_a, anchor_zero);
graph.connect(node_b, anchor_zero);
// FIX: Escaped quotes for Rust string literal
println!("> $\"0\" Anchor spawned at Node {}", anchor_zero);
println!("> Entanglement bonds established. Initiating cooling loop...");
// =========================================================================
// PHASE 3: SUBSTRATE CONVERGENCE (The Resolution)
// =========================================================================
println!("
--- PHASE 3: SUBSTRATE CONVERGENCE ---");
let start = Instant::now();
let converged = evaluator::evaluate(&mut graph, 50);
let duration = start.elapsed();
let final_tension = substrate::neural_energy(
&[graph.nodes[node_a].value as u8],
&[graph.nodes[node_b].value as u8]
);
println!("Status : {}", if converged { "EQUILIBRIUM REACHED" } else { "COOLING INCOMPLETE" });
println!("Epochs Run : {}", graph.epoch);
println!("Time Elapsed : {:?}", duration);
println!("
--- FINAL STATE (POST-$\"0\" INJECTION) ---");
println!("Node A (Cooled) : {:.2}", graph.nodes[node_a].value);
println!("Node B (Cooled) : {:.2}", graph.nodes[node_b].value);
println!("$\"0\" Anchor : {:.2} (Unchanged, Absolute)", graph.nodes[anchor_zero].value);
println!("System Tension : {:.2} (RESOLVED: Energy dissipated into $\"0\")", final_tension);
// =========================================================================
// PHASE 4: IMPERATIVE STABILITY LOCK (VM Memory)
// =========================================================================
println!("
--- PHASE 4: IMPERATIVE STABILITY LOCK (VM) ---");
let mut chaotic_memory = vec![255, 12, 200, 45, 99, 10, 250]; // High noise, low coherence
let initial_coherence = substrate::coherence(&chaotic_memory);
println!("Initial Memory : {:?} (Chaotic)", chaotic_memory);
println!("Initial Coherence : {:.4} (Low)", initial_coherence);
// THE $"0" PROTOCOL: Fill with constant 0 and SEAL the region.
for byte in chaotic_memory.iter_mut() { *byte = 0; }
let final_coherence = substrate::coherence(&chaotic_memory);
println!("Post-$\"0\" Memory : {:?} (Grounded)", chaotic_memory);
println!("Final Coherence : {:.4} (Absolute)", final_coherence);
println!("Stability Status : LOCKED (Mutations halted, decay prevented)");
println!("
[SYSTEM] $\"0\" Protocol Complete. Tension resolved. Stability secured.");
}