Files
foxhunt/crates/ml/tests/dqn_hyperopt_test.rs
jgrusewski 3d7d9c3d03 fix: update 9 test files for 14D DQNParams restructure
Tests referenced old DQNParams fields (learning_rate, batch_size,
ensemble_size, etc.) that were absorbed into family intensity scalars.
Rewrote all affected tests to validate the 14D search space layout,
intensity bounds [0.0, 2.0], and round-trip serialization.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 01:30:33 +02:00

253 lines
7.8 KiB
Rust

#![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,
)]
//! DQN Hyperopt End-to-End Test
//!
//! Proves that the hyperparameter optimizer works end-to-end with real DQN training.
//! This test runs 10 trials of Bayesian optimization (Argmin PSO) and verifies
//! convergence, result structure, and checkpoint artifacts.
//!
//! Run manually:
//! SQLX_OFFLINE=true cargo test -p ml --test dqn_hyperopt_test -- --ignored --nocapture
//!
//! Expected runtime: 10-20 minutes (GPU), 30-60 minutes (CPU)
#![allow(unused_crate_dependencies)]
use anyhow::{Context, Result};
use std::path::PathBuf;
use tracing::info;
use tracing::warn;
use ml::hyperopt::adapters::dqn::DQNTrainer;
use ml::hyperopt::ArgminOptimizer;
/// Locate the real training data directory.
///
/// Returns `Ok(path)` if the directory exists, `Err` otherwise so the test
/// can skip gracefully without marking as failed.
fn get_data_dir() -> Result<PathBuf> {
let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.context("Failed to resolve workspace root")?
.to_path_buf();
let data_dir = workspace_root.join("test_data/real/databento/ml_training_small");
if !data_dir.exists() {
anyhow::bail!(
"Training data not found at {}. \
This test requires real Databento data to run.",
data_dir.display()
);
}
Ok(data_dir)
}
#[test]
#[ignore]
fn test_dqn_hyperopt_10_trials() -> Result<()> {
// --- Setup ---
let data_dir = match get_data_dir() {
Ok(dir) => dir,
Err(e) => {
warn!(reason = %e, "Skipping test: data not available");
return Ok(());
}
};
let start = std::time::Instant::now();
let num_trials: usize = 10;
let n_initial: usize = 3;
let epochs_per_trial: usize = 5;
// --- Create trainer and optimizer ---
let trainer = DQNTrainer::new(&data_dir, epochs_per_trial)
.context("Failed to create DQNTrainer")?;
let optimizer = ArgminOptimizer::builder()
.max_trials(num_trials)
.n_initial(n_initial)
.n_particles(5) // small swarm for test speed
.seed(42)
.build();
// --- Run optimization ---
let result = optimizer
.optimize(trainer)
.context("Hyperopt optimization failed")?;
let elapsed = start.elapsed();
// --- Assertions ---
// 1. All trials should have completed (at least n_initial; PSO may add more)
let trials_completed = result.all_trials.len();
assert!(
trials_completed >= n_initial,
"Expected at least {n_initial} trials, got {trials_completed}"
);
// 2. Best objective should be finite (not NaN or Inf)
assert!(
result.best_objective.is_finite(),
"Best objective is not finite: {}",
result.best_objective
);
// 3. Every trial objective should be finite
for trial in &result.all_trials {
assert!(
trial.objective.is_finite(),
"Trial {} has non-finite objective: {}",
trial.trial_num,
trial.objective
);
assert!(
trial.duration_secs > 0.0,
"Trial {} has non-positive duration: {}",
trial.trial_num,
trial.duration_secs
);
}
// 4. Best objective should be <= first trial (optimizer should not regress)
if let Some(first_trial) = result.all_trials.first() {
assert!(
result.best_objective <= first_trial.objective,
"Optimizer regressed: best={} > first={}",
result.best_objective,
first_trial.objective
);
}
// 5. Convergence plot should have entries matching trial count
assert_eq!(
result.convergence_plot_data.len(),
trials_completed,
"Convergence plot entries ({}) should match trial count ({trials_completed})",
result.convergence_plot_data.len()
);
// 6. Convergence plot should be monotonically non-increasing
for window in result.convergence_plot_data.windows(2) {
let (_, prev_best) = window[0];
let (_, curr_best) = window[1];
assert!(
curr_best <= prev_best + f64::EPSILON,
"Convergence plot is not monotonically non-increasing: {prev_best} -> {curr_best}"
);
}
// 7. Best params should have sane gamma (in [0.95, 0.999])
let best_gamma = result.best_params.gamma;
assert!(
best_gamma >= 0.95 && best_gamma <= 0.999,
"Best gamma out of sane range: {best_gamma}"
);
// 8. Best params learning_intensity should be in [0.0, 2.0]
let best_li = result.best_params.learning_intensity;
assert!(
best_li >= 0.0 && best_li <= 2.0,
"Best learning_intensity out of range: {best_li}"
);
// --- Report ---
info!(
trials_completed,
best_objective = result.best_objective,
best_gamma = result.best_params.gamma,
best_learning_intensity = result.best_params.learning_intensity,
best_exploration_intensity = result.best_params.exploration_intensity,
total_secs = elapsed.as_secs_f64(),
avg_secs_per_trial = elapsed.as_secs_f64() / trials_completed as f64,
"DQN HYPEROPT REPORT"
);
for trial in &result.all_trials {
info!(
trial_num = trial.trial_num,
objective = trial.objective,
duration_secs = trial.duration_secs,
gamma = trial.params.gamma,
learning_intensity = trial.params.learning_intensity,
"Trial history"
);
}
for (trial_num, best_so_far) in &result.convergence_plot_data {
info!(trial_num, best_so_far, "Convergence");
}
Ok(())
}