mobhub
Decentralized model and agent weights distribution network
Brought by drakestapleton
| 1 | //! Vladar Training Formula V2.0 in native Rust and Mojo execution engine. |
| 2 | //! Bit-for-bit validated against Vladar's canonical reference data and community gym guides. |
| 3 | |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | use std::process::Command; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 8 | #[serde(rename_all = "snake_case")] |
| 9 | pub enum StatName { |
| 10 | Strength, |
| 11 | Speed, |
| 12 | Dexterity, |
| 13 | Defense, |
| 14 | } |
| 15 | |
| 16 | impl StatName { |
| 17 | pub fn as_str(&self) -> &'static str { |
| 18 | match self { |
| 19 | StatName::Strength => "strength", |
| 20 | StatName::Speed => "speed", |
| 21 | StatName::Dexterity => "dexterity", |
| 22 | StatName::Defense => "defense", |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn from_str(s: &str) -> Option<Self> { |
| 27 | match s.to_lowercase().as_str() { |
| 28 | "strength" | "str" => Some(StatName::Strength), |
| 29 | "speed" | "spd" => Some(StatName::Speed), |
| 30 | "dexterity" | "dex" => Some(StatName::Dexterity), |
| 31 | "defense" | "def" => Some(StatName::Defense), |
| 32 | _ => None, |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub struct StatConstants { |
| 38 | pub a: f64, |
| 39 | pub b: f64, |
| 40 | } |
| 41 | |
| 42 | pub fn get_stat_constants(stat: StatName) -> StatConstants { |
| 43 | match stat { |
| 44 | StatName::Strength => StatConstants { a: 1600.0, b: 1700.0 }, |
| 45 | StatName::Speed => StatConstants { a: 1600.0, b: 2000.0 }, |
| 46 | StatName::Dexterity => StatConstants { a: 1800.0, b: 1500.0 }, |
| 47 | StatName::Defense => StatConstants { a: 2100.0, b: -600.0 }, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub const STAT_SOFT_CAP: f64 = 50_000_000.0; |
| 52 | pub const SOFT_CAP_DIVISOR_FACTOR: f64 = 8.77635; |
| 53 | pub const HAPPY_LOG_COEFF: f64 = 0.07; |
| 54 | pub const HAPPY_LOG_OFFSET: f64 = 250.0; |
| 55 | pub const HAPPY_POWER_COEFF: f64 = 8.0; |
| 56 | pub const HAPPY_POWER_EXP: f64 = 1.05; |
| 57 | pub const HAPPY_MAX: f64 = 99_999.0; |
| 58 | pub const FORMULA_DIVISOR: f64 = 200_000.0; |
| 59 | |
| 60 | fn round_to(x: f64, dp: i32) -> f64 { |
| 61 | let p = 10f64.powi(dp); |
| 62 | (x * p).round() / p |
| 63 | } |
| 64 | |
| 65 | pub fn soft_cap_stat(stat: f64) -> f64 { |
| 66 | if stat <= STAT_SOFT_CAP { |
| 67 | stat |
| 68 | } else { |
| 69 | STAT_SOFT_CAP + (stat - STAT_SOFT_CAP) / (SOFT_CAP_DIVISOR_FACTOR * stat.log10()) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 74 | pub struct GainParams { |
| 75 | pub stat: StatName, |
| 76 | pub stat_value: f64, |
| 77 | pub happy: f64, |
| 78 | pub energy_per_train: f64, |
| 79 | pub gym_dots: f64, |
| 80 | #[serde(default)] |
| 81 | pub perk_bonuses: Vec<f64>, |
| 82 | } |
| 83 | |
| 84 | pub fn perk_multiplier(perks: &[f64]) -> f64 { |
| 85 | perks.iter().fold(1.0, |acc, &x| acc * (1.0 + x)) |
| 86 | } |
| 87 | |
| 88 | /// Calculate expected single-train stat gain according to Vladar Training Formula V2.0. |
| 89 | pub fn expected_gain_per_train(p: &GainParams) -> f64 { |
| 90 | let c = get_stat_constants(p.stat); |
| 91 | let h = p.happy.max(0.0); |
| 92 | let seff = soft_cap_stat(p.stat_value); |
| 93 | |
| 94 | let log_term = round_to((1.0 + h / HAPPY_LOG_OFFSET).ln(), 4); |
| 95 | let happy_mult = round_to(1.0 + HAPPY_LOG_COEFF * log_term, 4); |
| 96 | |
| 97 | let inner = seff * happy_mult |
| 98 | + HAPPY_POWER_COEFF * h.powf(HAPPY_POWER_EXP) |
| 99 | + (1.0 - (h / HAPPY_MAX).powi(2)) * c.a |
| 100 | + c.b; |
| 101 | |
| 102 | (inner / FORMULA_DIVISOR) * p.gym_dots * p.energy_per_train * perk_multiplier(&p.perk_bonuses) |
| 103 | } |
| 104 | |
| 105 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 106 | pub struct SessionParams { |
| 107 | pub stat: StatName, |
| 108 | pub start_stat: f64, |
| 109 | pub start_happy: f64, |
| 110 | pub energy_available: f64, |
| 111 | pub energy_per_train: f64, |
| 112 | pub gym_dots: f64, |
| 113 | #[serde(default)] |
| 114 | pub perk_bonuses: Vec<f64>, |
| 115 | pub happy_loss_per_train: Option<f64>, |
| 116 | pub min_happy: Option<f64>, |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 120 | pub struct SessionResult { |
| 121 | pub trains: usize, |
| 122 | pub energy_spent: f64, |
| 123 | pub total_gain: f64, |
| 124 | pub end_stat: f64, |
| 125 | pub start_happy: f64, |
| 126 | pub end_happy: f64, |
| 127 | pub average_gain_per_train: f64, |
| 128 | } |
| 129 | |
| 130 | /// Simulate a full training session with decaying happy. |
| 131 | pub fn simulate_session(p: &SessionParams) -> SessionResult { |
| 132 | let loss = p.happy_loss_per_train.unwrap_or(0.5 * p.energy_per_train); |
| 133 | let min_h = p.min_happy.unwrap_or(0.0); |
| 134 | |
| 135 | let mut stat = p.start_stat; |
| 136 | let mut happy = p.start_happy; |
| 137 | let mut energy = p.energy_available; |
| 138 | let mut trains = 0; |
| 139 | let mut total_gain = 0.0; |
| 140 | |
| 141 | let p_gain = GainParams { |
| 142 | stat: p.stat, |
| 143 | stat_value: stat, |
| 144 | happy, |
| 145 | energy_per_train: p.energy_per_train, |
| 146 | gym_dots: p.gym_dots, |
| 147 | perk_bonuses: p.perk_bonuses.clone(), |
| 148 | }; |
| 149 | |
| 150 | while energy >= p.energy_per_train { |
| 151 | let mut curr_p = p_gain.clone(); |
| 152 | curr_p.stat_value = stat; |
| 153 | curr_p.happy = happy; |
| 154 | |
| 155 | let gain = expected_gain_per_train(&curr_p); |
| 156 | stat += gain; |
| 157 | total_gain += gain; |
| 158 | happy = (happy - loss).max(min_h); |
| 159 | energy -= p.energy_per_train; |
| 160 | trains += 1; |
| 161 | } |
| 162 | |
| 163 | SessionResult { |
| 164 | trains, |
| 165 | energy_spent: (trains as f64) * p.energy_per_train, |
| 166 | total_gain, |
| 167 | end_stat: stat, |
| 168 | start_happy: p.start_happy, |
| 169 | end_happy: happy, |
| 170 | average_gain_per_train: if trains > 0 { total_gain / (trains as f64) } else { 0.0 }, |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// Execute compiled Mojo gym optimizer or fallback to native Rust session engine. |
| 175 | pub fn run_mojo_optimizer(p: &SessionParams) -> SessionResult { |
| 176 | let mult = perk_multiplier(&p.perk_bonuses); |
| 177 | let loss = p.happy_loss_per_train.unwrap_or(0.5 * p.energy_per_train); |
| 178 | let min_h = p.min_happy.unwrap_or(0.0); |
| 179 | |
| 180 | let candidates = [ |
| 181 | std::env::var("MOJO_GYM_OPTIMIZER_BIN").ok().map(std::path::PathBuf::from), |
| 182 | std::env::current_exe().ok().and_then(|exe| exe.parent().map(|dir| dir.join("mojo/gym_optimizer"))), |
| 183 | std::env::current_exe().ok().and_then(|exe| exe.parent().map(|dir| dir.join("gym_optimizer"))), |
| 184 | Some(std::path::PathBuf::from("mojo/gym_optimizer")), |
| 185 | Some(std::path::PathBuf::from("./gym_optimizer")), |
| 186 | Some(std::path::PathBuf::from("/home/drakestapleton/workspace/mobhub/mojo/gym_optimizer")), |
| 187 | ]; |
| 188 | |
| 189 | for cand in candidates.into_iter().flatten() { |
| 190 | if cand.is_file() { |
| 191 | if let Ok(out) = Command::new(&cand) |
| 192 | .args([ |
| 193 | p.stat.as_str(), |
| 194 | &p.start_stat.to_string(), |
| 195 | &p.start_happy.to_string(), |
| 196 | &p.energy_available.to_string(), |
| 197 | &p.energy_per_train.to_string(), |
| 198 | &p.gym_dots.to_string(), |
| 199 | &mult.to_string(), |
| 200 | &loss.to_string(), |
| 201 | &min_h.to_string(), |
| 202 | ]) |
| 203 | .output() |
| 204 | { |
| 205 | if out.status.success() { |
| 206 | let stdout = String::from_utf8_lossy(&out.stdout); |
| 207 | if let Some(json_start) = stdout.find('{') { |
| 208 | if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&stdout[json_start..]) { |
| 209 | let trains = parsed.get("trains").and_then(|v| v.as_u64()).unwrap_or(0) as usize; |
| 210 | let total_gain = parsed.get("total_gain").and_then(|v| v.as_f64()).unwrap_or(0.0); |
| 211 | let end_stat = parsed.get("end_stat").and_then(|v| v.as_f64()).unwrap_or(p.start_stat); |
| 212 | let end_happy = parsed.get("end_happy").and_then(|v| v.as_f64()).unwrap_or(p.start_happy); |
| 213 | let energy_spent = parsed.get("energy_spent").and_then(|v| v.as_f64()).unwrap_or(0.0); |
| 214 | |
| 215 | return SessionResult { |
| 216 | trains, |
| 217 | energy_spent, |
| 218 | total_gain, |
| 219 | end_stat, |
| 220 | start_happy: p.start_happy, |
| 221 | end_happy, |
| 222 | average_gain_per_train: if trains > 0 { total_gain / (trains as f64) } else { 0.0 }, |
| 223 | }; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Native Rust fallback (guaranteed fast and bit-exact) |
| 232 | simulate_session(p) |
| 233 | } |
| 234 | |
| 235 | #[cfg(test)] |
| 236 | mod tests { |
| 237 | use super::*; |
| 238 | |
| 239 | #[test] |
| 240 | fn test_vladar_formula_reference_cases() { |
| 241 | // Reference 1: STR S=1,000,000 H=5,000 G=7.3 E=5 no perks -> dS approx 233.17 |
| 242 | let p1 = GainParams { |
| 243 | stat: StatName::Strength, |
| 244 | stat_value: 1_000_000.0, |
| 245 | happy: 5000.0, |
| 246 | energy_per_train: 5.0, |
| 247 | gym_dots: 7.3, |
| 248 | perk_bonuses: vec![], |
| 249 | }; |
| 250 | let gain1 = expected_gain_per_train(&p1); |
| 251 | assert!((gain1 - 233.17).abs() < 0.01, "STR H=5000: got {}", gain1); |
| 252 | |
| 253 | // Reference 2: STR S=1,000,000 H=0 G=7.3 E=5 no perks -> dS approx 183.10 |
| 254 | let p2 = GainParams { |
| 255 | stat: StatName::Strength, |
| 256 | stat_value: 1_000_000.0, |
| 257 | happy: 0.0, |
| 258 | energy_per_train: 5.0, |
| 259 | gym_dots: 7.3, |
| 260 | perk_bonuses: vec![], |
| 261 | }; |
| 262 | let gain2 = expected_gain_per_train(&p2); |
| 263 | assert!((gain2 - 183.10).abs() < 0.01, "STR H=0: got {}", gain2); |
| 264 | |
| 265 | // Reference 3: DEF S=1,000,000 H=10,000 G=6.0 E=25 no perks -> dS approx 1041.20 |
| 266 | let p3 = GainParams { |
| 267 | stat: StatName::Defense, |
| 268 | stat_value: 1_000_000.0, |
| 269 | happy: 10_000.0, |
| 270 | energy_per_train: 25.0, |
| 271 | gym_dots: 6.0, |
| 272 | perk_bonuses: vec![], |
| 273 | }; |
| 274 | let gain3 = expected_gain_per_train(&p3); |
| 275 | assert!((gain3 - 1041.20).abs() < 0.01, "DEF H=10000: got {}", gain3); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn test_simulate_session() { |
| 280 | let p = SessionParams { |
| 281 | stat: StatName::Strength, |
| 282 | start_stat: 1_000_000.0, |
| 283 | start_happy: 5000.0, |
| 284 | energy_available: 100.0, |
| 285 | energy_per_train: 5.0, |
| 286 | gym_dots: 7.3, |
| 287 | perk_bonuses: vec![], |
| 288 | happy_loss_per_train: Some(2.5), |
| 289 | min_happy: Some(0.0), |
| 290 | }; |
| 291 | let res = simulate_session(&p); |
| 292 | assert_eq!(res.trains, 20); |
| 293 | assert_eq!(res.energy_spent, 100.0); |
| 294 | assert_eq!(res.end_happy, 5000.0 - 20.0 * 2.5); |
| 295 | assert!(res.total_gain > 4000.0); |
| 296 | } |
| 297 | |
| 298 | #[test] |
| 299 | fn test_mojo_optimizer_execution() { |
| 300 | let p = SessionParams { |
| 301 | stat: StatName::Strength, |
| 302 | start_stat: 1_000_000.0, |
| 303 | start_happy: 5000.0, |
| 304 | energy_available: 50.0, |
| 305 | energy_per_train: 5.0, |
| 306 | gym_dots: 7.3, |
| 307 | perk_bonuses: vec![], |
| 308 | happy_loss_per_train: Some(2.5), |
| 309 | min_happy: Some(0.0), |
| 310 | }; |
| 311 | let res = run_mojo_optimizer(&p); |
| 312 | assert_eq!(res.trains, 10); |
| 313 | assert_eq!(res.energy_spent, 50.0); |
| 314 | assert!((res.end_happy - 4975.0).abs() < 0.1); |
| 315 | assert!(res.total_gain > 2000.0); |
| 316 | } |
| 317 | } |