#![allow( clippy::assertions_on_constants, clippy::assertions_on_result_states, clippy::clone_on_copy, clippy::decimal_literal_representation, clippy::doc_markdown, clippy::empty_line_after_doc_comments, clippy::field_reassign_with_default, clippy::get_unwrap, clippy::identity_op, clippy::inconsistent_digit_grouping, clippy::indexing_slicing, clippy::integer_division, clippy::len_zero, clippy::let_underscore_must_use, clippy::manual_div_ceil, clippy::manual_let_else, clippy::manual_range_contains, clippy::modulo_arithmetic, clippy::needless_range_loop, clippy::non_ascii_literal, clippy::redundant_clone, clippy::shadow_reuse, clippy::shadow_same, clippy::shadow_unrelated, clippy::single_match_else, clippy::str_to_string, clippy::string_slice, clippy::tests_outside_test_module, clippy::too_many_lines, clippy::unnecessary_wraps, clippy::unseparated_literal_suffix, clippy::use_debug, clippy::useless_vec, clippy::wildcard_enum_match_arm, clippy::else_if_without_else, clippy::expect_used, clippy::missing_const_for_fn, clippy::similar_names, clippy::type_complexity, clippy::collapsible_else_if, clippy::doc_lazy_continuation, clippy::items_after_test_module, clippy::map_clone, clippy::multiple_unsafe_ops_per_block, clippy::unwrap_or_default, clippy::assign_op_pattern, clippy::needless_borrow, clippy::println_empty_string, clippy::unnecessary_cast, clippy::used_underscore_binding, clippy::create_dir, clippy::implicit_saturating_sub, clippy::exit, clippy::expect_fun_call, clippy::too_many_arguments, clippy::unnecessary_map_or, clippy::unwrap_used, dead_code, unused_imports, unused_variables, clippy::cloned_ref_to_slice_refs, clippy::neg_multiply, clippy::while_let_loop, clippy::bool_assert_comparison, clippy::excessive_precision, clippy::trivially_copy_pass_by_ref, clippy::op_ref, clippy::redundant_closure, clippy::unnecessary_lazy_evaluations, clippy::if_then_some_else_none, clippy::unnecessary_to_owned, clippy::single_component_path_imports, )] //! Tests for adaptive dropout scheduling (Wave 26 P1.6) //! //! Tests verify that dropout rate decreases linearly from initial to final //! over a specified number of training steps. use ml::dqn::network::{DropoutScheduler, QNetwork, QNetworkConfig}; #[test] fn test_dropout_scheduler_creation() { let scheduler = DropoutScheduler::new(0.5, 0.1, 10000); assert_eq!(scheduler.get_rate(), 0.5, "Initial rate should be 0.5"); } #[test] fn test_dropout_scheduler_linear_decay() { let mut scheduler = DropoutScheduler::new(0.5, 0.1, 10000); // At step 0, should be at initial rate assert_eq!(scheduler.get_rate(), 0.5); // At 25% progress (2500 steps) scheduler.step(2500); let rate_25 = scheduler.get_rate(); assert!( (rate_25 - 0.4).abs() < 1e-6, "At 25% progress, rate should be ~0.4, got {}", rate_25 ); // At 50% progress (5000 steps) scheduler.step(2500); // Total 5000 steps let rate_50 = scheduler.get_rate(); assert!( (rate_50 - 0.3).abs() < 1e-6, "At 50% progress, rate should be ~0.3, got {}", rate_50 ); // At 75% progress (7500 steps) scheduler.step(2500); // Total 7500 steps let rate_75 = scheduler.get_rate(); assert!( (rate_75 - 0.2).abs() < 1e-6, "At 75% progress, rate should be ~0.2, got {}", rate_75 ); // At 100% progress (10000 steps) scheduler.step(2500); // Total 10000 steps assert_eq!(scheduler.get_rate(), 0.1, "Final rate should be 0.1"); // Beyond decay steps, should stay at final rate scheduler.step(5000); // Total 15000 steps assert_eq!( scheduler.get_rate(), 0.1, "Rate should stay at final after decay_steps" ); } #[test] fn test_dropout_scheduler_step_increment() { let mut scheduler = DropoutScheduler::new(0.8, 0.2, 1000); // Step by single increments for _ in 0..500 { scheduler.step(1); } let rate_half = scheduler.get_rate(); assert!( (rate_half - 0.5).abs() < 1e-6, "At halfway point, should be midpoint of range: {}", rate_half ); } #[test] fn test_dropout_scheduler_current_step_tracking() { let mut scheduler = DropoutScheduler::new(0.5, 0.1, 10000); assert_eq!(scheduler.current_step(), 0); scheduler.step(100); assert_eq!(scheduler.current_step(), 100); scheduler.step(200); assert_eq!(scheduler.current_step(), 300); } #[test] fn test_qnetwork_config_with_dropout_schedule() { let config = QNetworkConfig { state_dim: 10, num_actions: 3, hidden_dims: vec![64, 32], dropout_schedule: Some((0.5, 0.1, 10000)), // (initial, final, steps) ..Default::default() }; assert!(config.dropout_schedule.is_some()); let (initial, final_rate, steps) = config.dropout_schedule.unwrap(); assert_eq!(initial, 0.5); assert_eq!(final_rate, 0.1); assert_eq!(steps, 10000); } #[test] fn test_qnetwork_with_adaptive_dropout() -> anyhow::Result<()> { let config = QNetworkConfig { state_dim: 4, num_actions: 3, hidden_dims: vec![8], dropout_schedule: Some((0.5, 0.1, 1000)), ..Default::default() }; let network = QNetwork::new(config)?; let state = vec![1.0, 0.5, -0.5, 0.0]; // Initial dropout rate should be 0.5 let initial_rate = network.get_dropout_rate(); assert!( (initial_rate - 0.5).abs() < 1e-6, "Initial dropout rate should be 0.5" ); // Perform forward passes to advance training steps for _ in 0..500 { network.forward(&state, true)?; } // After 500 steps (50% progress), rate should be ~0.3 let mid_rate = network.get_dropout_rate(); assert!( mid_rate < 0.5 && mid_rate > 0.1, "Mid-training dropout rate should be between initial and final: {}", mid_rate ); Ok(()) } #[test] fn test_dropout_scheduler_zero_decay_steps() { // Edge case: if decay_steps is 0, should immediately be at final rate let scheduler = DropoutScheduler::new(0.5, 0.1, 0); assert_eq!( scheduler.get_rate(), 0.1, "With 0 decay steps, should be at final rate" ); } #[test] fn test_dropout_scheduler_same_initial_and_final() { // Edge case: if initial == final, rate should stay constant let mut scheduler = DropoutScheduler::new(0.3, 0.3, 1000); assert_eq!(scheduler.get_rate(), 0.3); scheduler.step(500); assert_eq!(scheduler.get_rate(), 0.3, "Rate should stay constant"); scheduler.step(500); assert_eq!(scheduler.get_rate(), 0.3, "Rate should stay constant"); } #[test] fn test_dropout_scheduler_realistic_training_schedule() { // Test realistic schedule: 0.5 -> 0.05 over 100k steps let mut scheduler = DropoutScheduler::new(0.5, 0.05, 100_000); // Early training (10k steps): high dropout for regularization scheduler.step(10_000); let early_rate = scheduler.get_rate(); assert!( early_rate > 0.4, "Early training should have high dropout: {}", early_rate ); // Mid training (50k steps): moderate dropout scheduler.step(40_000); // Total 50k let mid_rate = scheduler.get_rate(); assert!( (mid_rate - 0.275).abs() < 0.01, "Mid training should have moderate dropout: {}", mid_rate ); // Late training (100k steps): low dropout for fine-tuning scheduler.step(50_000); // Total 100k let late_rate = scheduler.get_rate(); assert_eq!(late_rate, 0.05, "Late training should have low dropout"); }