Files
foxhunt/ml/src/hyperopt/observer.rs
jgrusewski 8b9abcc3c1 fix: resolve all clippy errors across 37+ workspace crates
Eliminate ~4,260 clippy deny-level errors that blocked workspace-wide
clippy runs. Errors cascaded: upstream crate failures (ctrader-openapi,
risk-data) hid thousands of downstream errors in ml, tli, backtesting.

Key changes:
- ctrader-openapi: fix shadow_unrelated/shadow_reuse (renamed vars)
- risk-data/risk: replace non-ASCII em dashes with ASCII equivalents
- tli: allow deny lints on prost-generated proto code, fix shadows
- trading_engine: fix let_underscore_must_use, wildcard matches, shadows
- broker_gateway_service: allow dead_code on unused redis_client field
- ml (4030 errors): remove local deny overrides for unwrap/expect/indexing
  (workspace warn level sufficient), add crate-level allows for non-safety
  mass-violation lints (non_ascii_literal, shadow_*, str_to_string, etc.),
  batch-fix em dashes, unseparated literal suffixes, format_push_string,
  wildcard matches, impl_trait_in_params, mutex_atomic, and more
- backtesting: replace unwrap() on first()/last() with match destructure
- tests: simplify loop-that-never-loops, fix mutex unwrap

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 12:44:10 +01:00

60 lines
1.7 KiB
Rust

use argmin::core::observers::Observe;
use argmin::core::{Error, State};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
/// Observer that enforces strict trial budget limits
///
/// Argmin's PSO does not reliably respect max_iters when set to small values (e.g., 1).
/// This observer tracks evaluations across all iterations and terminates when budget is hit.
#[derive(Clone, Debug)]
pub struct TrialBudgetObserver {
max_trials: usize,
trials_used: Arc<AtomicUsize>,
}
impl TrialBudgetObserver {
pub fn new(max_trials: usize) -> Self {
Self {
max_trials,
trials_used: Arc::new(AtomicUsize::new(0)),
}
}
pub fn increment_trial(&self) {
self.trials_used.fetch_add(1, Ordering::Relaxed);
}
pub fn should_terminate(&self) -> bool {
self.trials_used.load(Ordering::Relaxed) >= self.max_trials
}
pub fn get_trials_used(&self) -> usize {
self.trials_used.load(Ordering::Relaxed)
}
}
impl<I> Observe<I> for TrialBudgetObserver
where
I: State,
{
fn observe_iter(&mut self, _state: &I, _kv: &argmin::core::KV) -> Result<(), Error> {
// Check if we've exceeded budget
if self.should_terminate() {
tracing::warn!(
"Trial budget exhausted: {}/{} trials used. Terminating optimization.",
self.get_trials_used(),
self.max_trials
);
// Return error to signal termination
return Err(Error::msg(format!(
"Trial budget exhausted: {}/{} trials",
self.get_trials_used(),
self.max_trials
)));
}
Ok(())
}
}