hyperopt_core/traits.rs
1use crate::{Distribution, StudyState, Trial, Value};
2
3/// A pluggable search algorithm.
4///
5/// A sampler decides which concrete [`Value`] to return for each parameter a
6/// trial requests. It receives the whole [`StudyState`] (read access to prior
7/// trials) so adaptive samplers such as TPE have their history without a
8/// separate channel, plus the current partially-built `trial`, the parameter
9/// `name`, and its `distribution`.
10///
11/// Implementations must return a `Value` whose variant matches the
12/// `distribution` (`Float` for `Uniform`/`LogUniform`, `Int` for `IntUniform`,
13/// `Categorical` for `Categorical`).
14///
15/// `Send` is required so a sampler can be shared across worker threads (behind
16/// a lock) under [`crate::Study::optimize_parallel`].
17pub trait Sampler: Send {
18 fn suggest(
19 &mut self,
20 study_state: &StudyState,
21 trial: &Trial,
22 param_name: &str,
23 distribution: &Distribution,
24 ) -> Value;
25}
26
27/// A pluggable early-stopping policy.
28///
29/// Called from [`crate::TrialContext::should_prune`] inside the user's
30/// objective, typically right after a `report(step, value)` call. The user's
31/// loop is expected to break early when this returns `true`. Takes `&self` plus
32/// the [`StudyState`] so cross-trial policies can compare against other trials'
33/// intermediate histories.
34pub trait Pruner: Send + Sync {
35 fn should_prune(&self, study_state: &StudyState, trial: &Trial) -> bool;
36}