Files
foxhunt/crates/ml/tests/dqn_action_masking_integration_test.rs
jgrusewski 448b61d095 refactor: collapse 9-level to 7-level ExposureLevel — eliminate degenerate Flat variants
The 4-branch DQN (direction x magnitude) had 3 degenerate variants
(Short25, Flat, Long25) that all mapped to 0.0 target exposure when
direction=Flat, causing 82% Flat collapse. Collapse these into a
single Flat variant, giving 7 levels (ShortSmall/Half/Full, Flat,
LongSmall/Half/Full) and 63 total factored actions (7x3x3).

- ExposureLevel enum: 9 variants -> 7 (add direction/magnitude/from_dir_mag)
- FactoredAction: 81 -> 63 total actions, from_index/to_index updated
- DQN epsilon-greedy: use from_dir_mag() instead of dir*3+mag indexing
- DQN config: num_actions default 9 -> 7
- PPO action space: 45 -> 63 actions, action masking updated
- Signal adapter CUDA kernel: 5-bin -> 7-bin exposure aggregation
- All tests updated for new variant names and index ranges

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 11:54:09 +02:00

384 lines
11 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,
)]
//! Integration tests for DQN action masking with position limits
//!
//! Tests verify that:
//! 1. Actions are correctly masked based on current position
//! 2. Position limits prevent invalid actions from being selected
//! 3. Masked action count tracking and logging works correctly
use ml::dqn::action_space::{get_valid_action_mask, ExposureLevel, FactoredAction};
#[test]
fn test_action_masking_at_positive_limit() {
// With max_position=1.0, only actions with |exposure| <= 1.0 are valid
let mask = get_valid_action_mask(2.0, 1.0);
// Long100 (exposure=+1.0) should be valid (exactly at limit)
for idx in 36..=44 {
assert!(
mask[idx],
"Long100 action {} should be valid (exposure=1.0 <= max=1.0)",
idx
);
}
// Long50 (exposure=+0.5) should be valid
for idx in 27..=35 {
assert!(
mask[idx],
"Long50 action {} should be valid (exposure=0.5 <= max=1.0)",
idx
);
}
// Flat (exposure=0.0) should be valid
for idx in 18..=26 {
assert!(
mask[idx],
"Flat action {} should be valid (exposure=0.0 <= max=1.0)",
idx
);
}
// Short actions (exposure=-0.5 to -1.0) should be valid
for idx in 0..=17 {
assert!(
mask[idx],
"Short action {} should be valid (|exposure| <= max=1.0)",
idx
);
}
}
#[test]
fn test_action_masking_at_negative_limit() {
// With max_position=1.0, only actions with |exposure| <= 1.0 are valid
let mask = get_valid_action_mask(-2.0, 1.0);
// Short100 (exposure=-1.0) should be valid (exactly at limit)
for idx in 0..=8 {
assert!(
mask[idx],
"Short100 action {} should be valid (|exposure|=1.0 <= max=1.0)",
idx
);
}
// Short50 (exposure=-0.5) should be valid
for idx in 9..=17 {
assert!(
mask[idx],
"Short50 action {} should be valid (|exposure|=0.5 <= max=1.0)",
idx
);
}
// Flat (exposure=0.0) should be valid
for idx in 18..=26 {
assert!(
mask[idx],
"Flat action {} should be valid (exposure=0.0 <= max=1.0)",
idx
);
}
// Long actions (exposure=+0.5 to +1.0) should be valid
for idx in 27..=44 {
assert!(
mask[idx],
"Long action {} should be valid (|exposure| <= max=1.0)",
idx
);
}
}
#[test]
fn test_action_masking_at_zero_position() {
// At position 0.0, all actions should be valid
let mask = get_valid_action_mask(0.0, 2.0);
for idx in 0..=44 {
assert!(mask[idx], "Action {} should be valid at position 0.0", idx);
}
}
#[test]
fn test_action_masking_prevents_long_at_max() {
// Position +1.5 with max 2.0:
// - Long100 (exposure=+1.0) would bring position to +1.0, which is valid
// - Long50 (exposure=+0.5) would bring position to +0.5, which is valid
// Note: Our implementation uses ABSOLUTE exposure, not deltas
let mask = get_valid_action_mask(1.5, 2.0);
// At position +1.5, actions with exposure <= 2.0 should be valid
// Since max exposure is ±1.0 (Long100/Short100), all actions are valid
for idx in 0..=44 {
let action = FactoredAction::from_index(idx).unwrap();
let exposure = action.target_exposure();
if exposure.abs() <= 2.0 {
assert!(
mask[idx],
"Action {} (exposure={}) should be valid at position 1.5 with max 2.0",
idx, exposure
);
}
}
}
#[test]
fn test_action_masking_prevents_short_at_min() {
// Position -1.5 with max 2.0:
// - Short100 (exposure=-1.0) would bring position to -1.0, which is valid
// - Short50 (exposure=-0.5) would bring position to -0.5, which is valid
let mask = get_valid_action_mask(-1.5, 2.0);
// At position -1.5, actions with |exposure| <= 2.0 should be valid
for idx in 0..=44 {
let action = FactoredAction::from_index(idx).unwrap();
let exposure = action.target_exposure();
if exposure.abs() <= 2.0 {
assert!(
mask[idx],
"Action {} (exposure={}) should be valid at position -1.5 with max 2.0",
idx, exposure
);
}
}
}
#[test]
fn test_flat_actions_always_valid() {
// Flat actions (exposure=0.0) should always be valid regardless of position
let positions = vec![-2.0, -1.5, -1.0, 0.0, 1.0, 1.5, 2.0];
for pos in positions {
let mask = get_valid_action_mask(pos, 2.0);
// Flat actions are indices 18-26
for idx in 18..=26 {
assert!(
mask[idx],
"Flat action {} should be valid at position {}",
idx, pos
);
}
}
}
#[test]
fn test_masked_action_count() {
// With max_position=0.6, actions with |exposure| > 0.6 are masked
// Long100 (|exposure|=1.0) and Short100 (|exposure|=1.0) should be masked
let mask = get_valid_action_mask(0.0, 0.6);
let masked_count = mask.iter().filter(|&&v| !v).count();
// Long100 (9 actions: indices 36-44) + Short100 (9 actions: indices 0-8) = 18
assert_eq!(
masked_count, 18,
"Expected 18 masked actions with max_position=0.6"
);
// With max_position=1.0, all actions should be valid (max exposure is ±1.0)
let mask = get_valid_action_mask(0.0, 1.0);
let masked_count = mask.iter().filter(|&&v| !v).count();
assert_eq!(
masked_count, 0,
"Expected 0 masked actions with max_position=1.0"
);
// With max_position=2.0, all actions should be valid
let mask = get_valid_action_mask(0.0, 2.0);
let masked_count = mask.iter().filter(|&&v| !v).count();
assert_eq!(
masked_count, 0,
"Expected 0 masked actions with max_position=2.0"
);
}
#[test]
fn test_restrictive_max_position() {
// With max_position=0.6, only Flat and ±50% exposure should be valid
let mask = get_valid_action_mask(0.0, 0.6);
// Short100 (exposure=-1.0) should be masked
for idx in 0..=8 {
assert!(
!mask[idx],
"Short100 action {} should be masked with max_position=0.6",
idx
);
}
// Short50 (exposure=-0.5) should be valid
for idx in 9..=17 {
assert!(
mask[idx],
"Short50 action {} should be valid with max_position=0.6",
idx
);
}
// Flat (exposure=0.0) should be valid
for idx in 18..=26 {
assert!(
mask[idx],
"Flat action {} should be valid with max_position=0.6",
idx
);
}
// Long50 (exposure=+0.5) should be valid
for idx in 27..=35 {
assert!(
mask[idx],
"Long50 action {} should be valid with max_position=0.6",
idx
);
}
// Long100 (exposure=+1.0) should be masked
for idx in 36..=44 {
assert!(
!mask[idx],
"Long100 action {} should be masked with max_position=0.6",
idx
);
}
}
#[test]
fn test_action_mapping_correctness() {
// Verify that action indices map to correct exposure levels
// This ensures our masking logic uses correct indices
// Short100: indices 0-8
for idx in 0..=8 {
let action = FactoredAction::from_index(idx).unwrap();
assert_eq!(
action.exposure,
ExposureLevel::ShortSmall,
"Action {} should have Short100 exposure",
idx
);
}
// Short50: indices 9-17
for idx in 9..=17 {
let action = FactoredAction::from_index(idx).unwrap();
assert_eq!(
action.exposure,
ExposureLevel::ShortFull,
"Action {} should have Short50 exposure",
idx
);
}
// Flat: indices 18-26
for idx in 18..=26 {
let action = FactoredAction::from_index(idx).unwrap();
assert_eq!(
action.exposure,
ExposureLevel::Flat,
"Action {} should have Flat exposure",
idx
);
}
// Long50: indices 27-35
for idx in 27..=35 {
let action = FactoredAction::from_index(idx).unwrap();
assert_eq!(
action.exposure,
ExposureLevel::LongSmall,
"Action {} should have Long50 exposure",
idx
);
}
// Long100: indices 36-44
for idx in 36..=44 {
let action = FactoredAction::from_index(idx).unwrap();
assert_eq!(
action.exposure,
ExposureLevel::LongFull,
"Action {} should have Long100 exposure",
idx
);
}
}