Remove Serde

This commit is contained in:
Manuel Raimann
2026-01-30 18:38:43 +01:00
committed by Manuel
parent aaf880e1c7
commit daab3ea202
11 changed files with 0 additions and 443 deletions
-141
View File
@@ -1142,144 +1142,3 @@ fn test_best_trial_with_nan_values() {
let best = study.best_trial();
assert!(best.is_ok());
}
// =============================================================================
// Serde tests (only run when serde feature is enabled)
// =============================================================================
#[cfg(feature = "serde")]
mod serde_tests {
use super::*;
#[test]
fn test_direction_serde() {
// Test Direction serialization
let min = Direction::Minimize;
let max = Direction::Maximize;
let min_json = serde_json::to_string(&min).unwrap();
let max_json = serde_json::to_string(&max).unwrap();
let min_deser: Direction = serde_json::from_str(&min_json).unwrap();
let max_deser: Direction = serde_json::from_str(&max_json).unwrap();
assert_eq!(min, min_deser);
assert_eq!(max, max_deser);
}
#[test]
fn test_study_serde_with_categorical() {
let study: Study<f64> = Study::new(Direction::Minimize);
study
.optimize(5, |trial| {
let x = trial.suggest_float("x", 0.0, 10.0)?;
let opt = trial.suggest_categorical("opt", &["a", "b", "c"])?;
let _ = opt;
Ok::<_, TpeError>(x)
})
.unwrap();
// Serialize
let json = serde_json::to_string(&study).unwrap();
// Deserialize
let loaded: Study<f64> = serde_json::from_str(&json).unwrap();
assert_eq!(loaded.n_trials(), 5);
assert_eq!(loaded.direction(), Direction::Minimize);
}
#[test]
fn test_study_serde_with_all_param_types() {
let study: Study<f64> = Study::new(Direction::Maximize);
study
.optimize(3, |trial| {
let x = trial.suggest_float("x", 0.0, 10.0)?;
let y = trial.suggest_float_log("y", 0.001, 1.0)?;
let z = trial.suggest_float_step("z", 0.0, 1.0, 0.1)?;
let a = trial.suggest_int("a", 1, 10)?;
let b = trial.suggest_int_log("b", 1, 100)?;
let c = trial.suggest_int_step("c", 0, 100, 10)?;
let d = trial.suggest_categorical("d", &["p", "q"])?;
let _ = (y, z, b, c, d);
Ok::<_, TpeError>(x + a as f64)
})
.unwrap();
let json = serde_json::to_string(&study).unwrap();
let loaded: Study<f64> = serde_json::from_str(&json).unwrap();
assert_eq!(loaded.n_trials(), 3);
assert_eq!(loaded.direction(), Direction::Maximize);
// Verify we can continue optimization
loaded
.optimize(2, |trial| {
let x = trial.suggest_float("x", 0.0, 10.0)?;
Ok::<_, TpeError>(x)
})
.unwrap();
assert_eq!(loaded.n_trials(), 5);
}
#[test]
fn test_study_serde_empty() {
// Test serializing a study with no trials
let study: Study<f64> = Study::new(Direction::Minimize);
let json = serde_json::to_string(&study).unwrap();
let loaded: Study<f64> = serde_json::from_str(&json).unwrap();
assert_eq!(loaded.n_trials(), 0);
assert_eq!(loaded.direction(), Direction::Minimize);
assert!(loaded.best_trial().is_err());
}
#[test]
fn test_study_serde_with_custom_value_type() {
// Test Study with i32 value type
let study: Study<i32> = Study::new(Direction::Minimize);
study
.optimize(5, |trial| {
let n = trial.suggest_int("n", 1, 100)?;
Ok::<_, TpeError>(n as i32)
})
.unwrap();
let json = serde_json::to_string(&study).unwrap();
let loaded: Study<i32> = serde_json::from_str(&json).unwrap();
assert_eq!(loaded.n_trials(), 5);
let best = loaded.best_trial().unwrap();
assert!(best.value >= 1 && best.value <= 100);
}
#[test]
fn test_completed_trial_access_after_serde() {
let study: Study<f64> = Study::new(Direction::Minimize);
study
.optimize(3, |trial| {
let x = trial.suggest_float("x", 0.0, 10.0)?;
Ok::<_, TpeError>(x * x)
})
.unwrap();
let json = serde_json::to_string(&study).unwrap();
let loaded: Study<f64> = serde_json::from_str(&json).unwrap();
// Access all trials
let trials = loaded.trials();
assert_eq!(trials.len(), 3);
for trial in &trials {
assert!(trial.params.contains_key("x"));
assert!(trial.distributions.contains_key("x"));
assert!(trial.value >= 0.0); // x^2 is non-negative
}
}
}