use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Direction {
Maximize,
Minimize,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ParamValue {
Categorical(u32),
}
impl ParamValue {
#[must_use]
pub const fn as_categorical(&self) -> Option<u32> {
match self {
Self::Categorical(idx) => Some(*idx),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrozenTrial {
pub number: usize,
pub params: BTreeMap<String, ParamValue>,
pub value: f64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn param_value_as_categorical() {
let v = ParamValue::Categorical(3);
assert_eq!(v.as_categorical(), Some(3));
}
#[test]
fn frozen_trial_serde_round_trip() {
let trial = FrozenTrial {
number: 0,
params: BTreeMap::from([
("x".into(), ParamValue::Categorical(2)),
("y".into(), ParamValue::Categorical(1)),
]),
value: 0.85,
};
let json = serde_json::to_string(&trial).unwrap();
let restored: FrozenTrial = serde_json::from_str(&json).unwrap();
assert_eq!(restored.number, 0);
assert_eq!(restored.params["x"], ParamValue::Categorical(2));
assert!((restored.value - 0.85).abs() < f64::EPSILON);
}
#[test]
fn direction_serde() {
let json = serde_json::to_string(&Direction::Maximize).unwrap();
let restored: Direction = serde_json::from_str(&json).unwrap();
assert_eq!(restored, Direction::Maximize);
}
}