hyperopt_core/distribution.rs
1use serde::{Deserialize, Serialize};
2
3/// The search-space shape recorded for a single suggested parameter.
4///
5/// In the define-by-run model a `Distribution` is produced by each
6/// `trial.suggest_*` call and handed to the active [`crate::Sampler`], which
7/// returns a matching [`crate::Value`]. The distribution is also recorded on
8/// the [`crate::Trial`] so later phases (pruning, adaptive sampling, storage,
9/// importance analysis) can reconstruct exactly what was searched.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum Distribution {
12 /// Continuous uniform over `[low, high]`.
13 Uniform { low: f64, high: f64 },
14 /// Log-uniform over `[low, high]` (both must be `> 0`). Sampling is uniform
15 /// in log-space — appropriate for scale parameters like learning rates.
16 LogUniform { low: f64, high: f64 },
17 /// Integer uniform over the inclusive range `[low, high]`.
18 IntUniform { low: i64, high: i64 },
19 /// Categorical over a fixed set of string labels.
20 Categorical { choices: Vec<String> },
21}
22
23impl Distribution {
24 /// Returns `true` if `value` lies inside this distribution's support.
25 pub fn contains(&self, value: &crate::Value) -> bool {
26 use crate::Value;
27 match (self, value) {
28 (Distribution::Uniform { low, high }, Value::Float(x))
29 | (Distribution::LogUniform { low, high }, Value::Float(x)) => *low <= *x && *x <= *high,
30 (Distribution::IntUniform { low, high }, Value::Int(x)) => *low <= *x && *x <= *high,
31 (Distribution::Categorical { choices }, Value::Categorical(s)) => choices.contains(s),
32 _ => false,
33 }
34 }
35}