Files
foxhunt/crates/ml/tests/action_masking_smoke_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

188 lines
6.6 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,
)]
//! Smoke tests for action masking functionality (Wave 9 Agent 2)
//!
//! Validates position limit enforcement via action masking.
//! With 7-level ExposureLevel, the mask has 7 elements:
//! [ShortSmall, ShortHalf, ShortFull, Flat, LongSmall, LongHalf, LongFull]
use ml::dqn::action_space::{get_valid_action_mask, ExposureLevel, FactoredAction};
#[test]
fn test_action_masking_at_neutral_position() {
// At position 0.0 with max_position=2.0, all 7 exposure levels valid
let mask = get_valid_action_mask(0.0, 2.0);
assert_eq!(mask.len(), 7, "Mask should have 7 elements (one per exposure level)");
assert_eq!(
mask.iter().filter(|&&v| v).count(),
7,
"All exposure levels should be valid at position 0.0 with max_position=2.0"
);
}
#[test]
fn test_action_masking_very_restrictive_limit() {
// With max_position=0.6, ShortFull and LongFull should be masked (|1.0| > 0.6)
let mask = get_valid_action_mask(0.0, 0.6);
let valid_count = mask.iter().filter(|&&v| v).count();
assert_eq!(
valid_count, 5,
"ShortSmall, ShortHalf, Flat, LongSmall, LongHalf valid (5/7 exposures)"
);
assert!(mask[0], "ShortSmall (0.25) should be VALID");
assert!(mask[1], "ShortHalf (0.50) should be VALID");
assert!(!mask[2], "ShortFull (1.0) should be INVALID");
assert!(mask[3], "Flat (0.0) should be VALID");
assert!(mask[4], "LongSmall (0.25) should be VALID");
assert!(mask[5], "LongHalf (0.50) should be VALID");
assert!(!mask[6], "LongFull (1.0) should be INVALID");
}
#[test]
fn test_action_masking_preserves_all_action_variants() {
// With masking at max_position=1.0, all 7 exposure levels valid
let mask = get_valid_action_mask(0.0, 1.0);
assert_eq!(mask.len(), 7);
assert!(mask.iter().all(|&v| v), "All 7 exposures should be valid at max_position=1.0");
}
#[test]
fn test_action_masking_index_mapping_correctness() {
let mask = get_valid_action_mask(0.0, 0.6);
assert_eq!(mask.len(), 7);
// ShortFull (idx 2) and LongFull (idx 6) should be masked
assert!(mask[0], "ShortSmall should be valid");
assert!(mask[1], "ShortHalf should be valid");
assert!(!mask[2], "ShortFull should be masked");
assert!(mask[3], "Flat should be valid");
assert!(mask[4], "LongSmall should be valid");
assert!(mask[5], "LongHalf should be valid");
assert!(!mask[6], "LongFull should be masked");
// Verify FactoredAction mapping still works for the 63-action space
let action_0 = FactoredAction::from_index(0).unwrap();
assert_eq!(action_0.exposure, ExposureLevel::ShortSmall);
let action_27 = FactoredAction::from_index(27).unwrap();
assert_eq!(action_27.exposure, ExposureLevel::Flat);
}
#[test]
fn test_action_masking_boundary_conditions() {
// At max_position=1.0, all exposure levels valid (max |exp| = 1.0 <= 1.0)
let mask = get_valid_action_mask(0.0, 1.0);
let valid_count = mask.iter().filter(|&&v| v).count();
assert_eq!(valid_count, 7, "All exposures valid when max |exposure| equals max_position");
// At max_position=0.99, ShortFull/LongFull masked (|1.0| > 0.99)
let mask_below = get_valid_action_mask(0.0, 0.99);
let valid_below = mask_below.iter().filter(|&&v| v).count();
assert_eq!(valid_below, 5, "Only 5 exposures valid when max_position < 1.0");
}
#[test]
fn test_action_masking_flat_always_valid() {
// Flat (exposure=0.0) should ALWAYS be valid
for max_pos in &[0.1, 0.5, 1.0, 2.0, 10.0] {
let mask = get_valid_action_mask(0.0, *max_pos);
assert!(
mask[3], // Flat is index 3 in the 7-element mask
"Flat should always be valid (max_position={})",
max_pos
);
}
}
#[test]
fn test_action_masking_edge_case_zero_max_position() {
// max_position=0.0 should only allow Flat (|0.0| <= 0.0)
let mask = get_valid_action_mask(0.0, 0.0);
let valid_count = mask.iter().filter(|&&v| v).count();
assert_eq!(valid_count, 1, "Only Flat should be valid when max_position=0.0");
assert!(!mask[0], "ShortSmall invalid at max_position=0.0");
assert!(!mask[1], "ShortHalf invalid at max_position=0.0");
assert!(!mask[2], "ShortFull invalid at max_position=0.0");
assert!(mask[3], "Flat valid at max_position=0.0");
assert!(!mask[4], "LongSmall invalid at max_position=0.0");
assert!(!mask[5], "LongHalf invalid at max_position=0.0");
assert!(!mask[6], "LongFull invalid at max_position=0.0");
}