feat: unify optimize and optimize_with via blanket Objective impl

- Add blanket `impl Objective<V> for Fn(&mut Trial) -> Result<V, E>`
  so closures work directly with `optimize`
- Rewrite optimize, optimize_async, optimize_parallel to accept
  `impl Objective<V>` with before_trial/after_trial hooks
- Remove optimize_with, optimize_with_async, optimize_with_parallel
- Remove max_retries and retry logic from Objective trait
- Add explicit closure type annotations for HRTB inference
- Convert FnMut test closures to Fn via RefCell/Cell
This commit is contained in:
Manuel Raimann
2026-02-12 13:09:14 +01:00
parent c20a53dfba
commit 47b5f9cec8
41 changed files with 316 additions and 793 deletions
+7 -5
View File
@@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::collections::HashMap;
use optimizer::parameter::{FloatParam, IntParam, ParamValue, Parameter};
@@ -73,16 +74,17 @@ fn test_enqueue_with_optimize() {
study.enqueue(HashMap::from([(x.id(), ParamValue::Float(1.0))]));
study.enqueue(HashMap::from([(x.id(), ParamValue::Float(2.0))]));
let mut values = Vec::new();
let values = RefCell::new(Vec::new());
study
.optimize(5, |trial| {
.optimize(5, |trial: &mut optimizer::Trial| {
let x_val = x.suggest(trial)?;
values.push(x_val);
values.borrow_mut().push(x_val);
Ok::<_, Error>(x_val * x_val)
})
.unwrap();
let values = values.into_inner();
// First two trials should use enqueued values
assert_eq!(values[0], 1.0);
assert_eq!(values[1], 2.0);
@@ -117,7 +119,7 @@ fn test_enqueue_trials_appear_in_completed_trials() {
study.enqueue(HashMap::from([(x.id(), ParamValue::Float(7.0))]));
study
.optimize(1, |trial| {
.optimize(1, |trial: &mut optimizer::Trial| {
let x_val = x.suggest(trial)?;
Ok::<_, Error>(x_val)
})
@@ -178,7 +180,7 @@ fn test_enqueue_counted_in_n_trials() {
study.enqueue(HashMap::from([(x.id(), ParamValue::Float(2.0))]));
study
.optimize(5, |trial| {
.optimize(5, |trial: &mut optimizer::Trial| {
let x_val = x.suggest(trial)?;
Ok::<_, Error>(x_val)
})