use crate::mechanics::evolution::policies::RateCalculationPolicy;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ThresholdRate;
impl ThresholdRate {
pub const LOWER_THRESHOLD: f32 = 0.3;
pub const UPPER_THRESHOLD: f32 = 0.7;
fn threshold_multiplier(normalized: f32) -> f32 {
if normalized < Self::LOWER_THRESHOLD {
1.0 } else if normalized < Self::UPPER_THRESHOLD {
0.5 } else {
0.25 }
}
}
impl RateCalculationPolicy for ThresholdRate {
fn calculate_rate(
base_rate: f32,
current_value: f32,
min: f32,
max: f32,
direction_multiplier: f32,
environmental_multiplier: f32,
) -> f32 {
let range = max - min;
if range.abs() < f32::EPSILON {
return 0.0;
}
let normalized = ((current_value - min) / range).clamp(0.0, 1.0);
let threshold_mult = Self::threshold_multiplier(normalized);
base_rate * threshold_mult * direction_multiplier * environmental_multiplier
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_threshold_below_lower() {
let rate = ThresholdRate::calculate_rate(2.0, 20.0, 0.0, 100.0, 1.0, 1.0);
assert_eq!(rate, 2.0); }
#[test]
fn test_threshold_at_lower() {
let rate = ThresholdRate::calculate_rate(2.0, 30.0, 0.0, 100.0, 1.0, 1.0);
assert_eq!(rate, 1.0); }
#[test]
fn test_threshold_middle() {
let rate = ThresholdRate::calculate_rate(2.0, 50.0, 0.0, 100.0, 1.0, 1.0);
assert_eq!(rate, 1.0); }
#[test]
fn test_threshold_at_upper() {
let rate = ThresholdRate::calculate_rate(2.0, 70.0, 0.0, 100.0, 1.0, 1.0);
assert_eq!(rate, 0.5); }
#[test]
fn test_threshold_above_upper() {
let rate = ThresholdRate::calculate_rate(2.0, 90.0, 0.0, 100.0, 1.0, 1.0);
assert_eq!(rate, 0.5); }
#[test]
fn test_threshold_decay() {
let rate = ThresholdRate::calculate_rate(2.0, 20.0, 0.0, 100.0, -1.0, 1.0);
assert_eq!(rate, -2.0); }
#[test]
fn test_threshold_with_environment() {
let rate = ThresholdRate::calculate_rate(2.0, 50.0, 0.0, 100.0, 1.0, 2.0);
assert_eq!(rate, 2.0); }
#[test]
fn test_threshold_different_ranges() {
let rate = ThresholdRate::calculate_rate(2.0, 32.0, 20.0, 80.0, 1.0, 1.0);
assert_eq!(rate, 2.0); }
#[test]
fn test_threshold_prevents_division_by_zero() {
let rate = ThresholdRate::calculate_rate(2.0, 50.0, 0.0, 0.0, 1.0, 1.0);
assert_eq!(rate, 0.0);
}
#[test]
fn test_threshold_step_function() {
let rate1 = ThresholdRate::calculate_rate(2.0, 29.0, 0.0, 100.0, 1.0, 1.0);
let rate2 = ThresholdRate::calculate_rate(2.0, 31.0, 0.0, 100.0, 1.0, 1.0);
assert_eq!(rate1, 2.0); assert_eq!(rate2, 1.0); assert!((rate1 - rate2).abs() > 0.5); }
}