spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / balance.rs
| 1 | use crate::models::BalanceVerdict; |
| 2 | |
| 3 | /// Native Rust balance kernel: scores the tension between Drive and Humanity. |
| 4 | pub struct BalanceKernel; |
| 5 | |
| 6 | impl BalanceKernel { |
| 7 | /// Scalar evaluation of one drive score against one humanity score. |
| 8 | pub fn evaluate(drive: f64, humanity: f64) -> BalanceVerdict { |
| 9 | Self::scalar(drive, humanity) |
| 10 | } |
| 11 | |
| 12 | /// Evaluation of 4-lane drive and humanity vectors: each side is summed, then scored. |
| 13 | pub fn evaluate_simd(drive_vec: [f32; 4], humanity_vec: [f32; 4]) -> BalanceVerdict { |
| 14 | let d_sum: f32 = drive_vec.iter().sum(); |
| 15 | let h_sum: f32 = humanity_vec.iter().sum(); |
| 16 | let mut v = Self::scalar(d_sum as f64, h_sum as f64); |
| 17 | v.mode = "vector".to_string(); |
| 18 | v |
| 19 | } |
| 20 | |
| 21 | fn scalar(drive: f64, humanity: f64) -> BalanceVerdict { |
| 22 | if drive == 0.0 && humanity == 0.0 { |
| 23 | return BalanceVerdict { |
| 24 | kernel: "rust".to_string(), |
| 25 | mode: "scalar".to_string(), |
| 26 | drive: 0.0, |
| 27 | humanity: 0.0, |
| 28 | ratio: 0.0, |
| 29 | score: 0.0, |
| 30 | verdict: "dormant".to_string(), |
| 31 | guidance: "Baseline absent. Both drive and humanity are zero.".to_string(), |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | if humanity == 0.0 { |
| 36 | return BalanceVerdict { |
| 37 | kernel: "rust".to_string(), |
| 38 | mode: "scalar".to_string(), |
| 39 | drive, |
| 40 | humanity: 0.0, |
| 41 | ratio: 999.0, |
| 42 | score: 0.1, |
| 43 | verdict: "drive_dominant".to_string(), |
| 44 | guidance: "Humanity absent. Restraint and ethics required.".to_string(), |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | if drive == 0.0 { |
| 49 | return BalanceVerdict { |
| 50 | kernel: "rust".to_string(), |
| 51 | mode: "scalar".to_string(), |
| 52 | drive: 0.0, |
| 53 | humanity, |
| 54 | ratio: 0.0, |
| 55 | score: 0.1, |
| 56 | verdict: "humanity_dominant".to_string(), |
| 57 | guidance: "Drive absent. Ambition and curiosity required.".to_string(), |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | let ratio = drive / humanity; |
| 62 | let (verdict, guidance, score) = if ratio > 2.0 { |
| 63 | ( |
| 64 | "drive_dominant".to_string(), |
| 65 | "Drive exceeds humanity threshold. Slow down and verify discipline.".to_string(), |
| 66 | 2.0 / ratio, |
| 67 | ) |
| 68 | } else if ratio < 0.5 { |
| 69 | ( |
| 70 | "humanity_dominant".to_string(), |
| 71 | "Humanity suppresses drive. Increase problem-solving velocity.".to_string(), |
| 72 | ratio * 2.0, |
| 73 | ) |
| 74 | } else { |
| 75 | let s = if ratio <= 1.0 { ratio } else { 1.0 / ratio }; |
| 76 | ( |
| 77 | "balanced".to_string(), |
| 78 | "Tension harmonized. Proceed with atomic ratification.".to_string(), |
| 79 | s, |
| 80 | ) |
| 81 | }; |
| 82 | |
| 83 | BalanceVerdict { |
| 84 | kernel: "rust".to_string(), |
| 85 | mode: "scalar".to_string(), |
| 86 | drive, |
| 87 | humanity, |
| 88 | ratio, |
| 89 | score, |
| 90 | verdict, |
| 91 | guidance, |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[cfg(test)] |
| 97 | mod tests { |
| 98 | use super::*; |
| 99 | |
| 100 | #[test] |
| 101 | fn test_scalar_balanced() { |
| 102 | let v = BalanceKernel::scalar(10.0, 10.0); |
| 103 | assert_eq!(v.verdict, "balanced"); |
| 104 | assert_eq!(v.ratio, 1.0); |
| 105 | assert_eq!(v.score, 1.0); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn test_scalar_drive_dominant() { |
| 110 | let v = BalanceKernel::scalar(30.0, 5.0); |
| 111 | assert_eq!(v.verdict, "drive_dominant"); |
| 112 | assert!(v.ratio > 2.0); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn test_scalar_humanity_dominant() { |
| 117 | let v = BalanceKernel::scalar(2.0, 10.0); |
| 118 | assert_eq!(v.verdict, "humanity_dominant"); |
| 119 | assert!(v.ratio < 0.5); |
| 120 | } |
| 121 | } |