feat(async): add run_async to every algorithm in the catalog
Async coverage was incomplete in 0.7 (only RandomSearch and DifferentialEvolution had run_async). 0.8 closes the gap: every one of the 33 algorithms now exposes run_async(&problem, concurrency).await, gated on the async feature. - Population-based algorithms fan out per-generation evaluations through evaluate_batch_async with concurrency-bounded FuturesOrdered chunks. - Steady-state algorithms (HillClimber, SimulatedAnnealing, OnePlusOneEs, Paes, NelderMead) await each step sequentially; they accept the concurrency parameter for API uniformity. - TabuSearch fans out the K-neighbor batch each step. - Surrogate algorithms (BayesianOpt, Tpe) batch the initial design and await per-iteration acquisitions sequentially so the surrogate can update between picks. - Hyperband uses a new AsyncPartialProblem trait (mirroring PartialProblem for multi-fidelity workloads) and a parallel evaluate_batch_at_budget_async helper; each Successive-Halving rung fans out its budgeted evaluations. All paths preserve seeded determinism: RNG draws happen on the main task in the same order as the sync path, and only the evaluations are concurrent. Adds a dedicated cookbook recipe at docs/book/src/cookbook/async.md with a worked example (DifferentialEvolution under tokio) and guidance on picking concurrency. Cross-references in SUMMARY.md and cookbook.md are updated to surface the new recipe. The follow-up docs commit reconciles the rest of the user guide and README to describe the new feature; this commit is the bare async surface.
This commit is contained in:
@@ -206,6 +206,106 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<I, D> Hyperband<I, D>
|
||||
where
|
||||
D: Clone,
|
||||
I: Initializer<D>,
|
||||
{
|
||||
/// Async version of [`Hyperband::run`] — evaluates each
|
||||
/// Successive-Halving rung's configurations concurrently through the
|
||||
/// caller's async runtime. Available only with the `async` feature.
|
||||
///
|
||||
/// `concurrency` bounds in-flight evaluations per rung.
|
||||
pub async fn run_async<P>(&mut self, problem: &P, concurrency: usize) -> OptimizationResult<D>
|
||||
where
|
||||
P: crate::core::async_problem::AsyncPartialProblem<Decision = D>,
|
||||
D: Send + Sync,
|
||||
{
|
||||
use crate::algorithms::parallel_eval_async::evaluate_batch_at_budget_async;
|
||||
|
||||
assert!(
|
||||
self.config.max_budget > 0.0,
|
||||
"Hyperband max_budget must be > 0"
|
||||
);
|
||||
assert!(self.config.eta > 1.0, "Hyperband eta must be > 1");
|
||||
assert!(
|
||||
self.config.max_brackets >= 1,
|
||||
"Hyperband max_brackets must be >= 1"
|
||||
);
|
||||
let objectives = problem.objectives();
|
||||
assert!(
|
||||
objectives.is_single_objective(),
|
||||
"Hyperband requires exactly one objective",
|
||||
);
|
||||
let direction = objectives.objectives[0].direction;
|
||||
let mut rng = rng_from_seed(self.config.seed);
|
||||
|
||||
let s_max = (self.config.max_budget.ln() / self.config.eta.ln()).floor() as i64;
|
||||
let s_max = (s_max as usize).min(self.config.max_brackets);
|
||||
|
||||
let mut total_evaluations = 0usize;
|
||||
let mut total_iterations = 0usize;
|
||||
let mut best_seen: Option<Candidate<D>> = None;
|
||||
|
||||
for s in (0..=s_max).rev() {
|
||||
let s_f = s as f64;
|
||||
let n =
|
||||
((s_max as f64 + 1.0) / (s_f + 1.0) * self.config.eta.powf(s_f)).ceil() as usize;
|
||||
let r = self.config.max_budget / self.config.eta.powf(s_f);
|
||||
|
||||
let mut configs: Vec<D> = self.initializer.initialize(n, &mut rng);
|
||||
for i in 0..=s {
|
||||
let n_i = (n as f64 / self.config.eta.powi(i as i32)).floor() as usize;
|
||||
let r_i = r * self.config.eta.powi(i as i32);
|
||||
if configs.is_empty() {
|
||||
break;
|
||||
}
|
||||
let evals: Vec<Evaluation> =
|
||||
evaluate_batch_at_budget_async(problem, &configs, r_i, concurrency).await;
|
||||
total_evaluations += configs.len();
|
||||
|
||||
for (cfg, e) in configs.iter().zip(evals.iter()) {
|
||||
let beats = match &best_seen {
|
||||
None => true,
|
||||
Some(b) => better(e, &b.evaluation, direction),
|
||||
};
|
||||
if beats {
|
||||
best_seen = Some(Candidate::new(cfg.clone(), e.clone()));
|
||||
}
|
||||
}
|
||||
total_iterations += 1;
|
||||
|
||||
let next_size = (n_i / self.config.eta as usize).max(1);
|
||||
if next_size >= configs.len() {
|
||||
continue;
|
||||
}
|
||||
let mut order: Vec<usize> = (0..configs.len()).collect();
|
||||
order.sort_by(|&a, &b| compare(&evals[a], &evals[b], direction));
|
||||
let keep: std::collections::HashSet<usize> =
|
||||
order.into_iter().take(next_size).collect();
|
||||
let new_configs: Vec<D> = configs
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(idx, c)| if keep.contains(&idx) { Some(c) } else { None })
|
||||
.collect();
|
||||
configs = new_configs;
|
||||
}
|
||||
}
|
||||
|
||||
let best = best_seen.expect("at least one bracket ran");
|
||||
let population = Population::new(vec![best.clone()]);
|
||||
let front = vec![best.clone()];
|
||||
OptimizationResult::new(
|
||||
population,
|
||||
front,
|
||||
Some(best),
|
||||
total_evaluations,
|
||||
total_iterations,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn compare(a: &Evaluation, b: &Evaluation, direction: Direction) -> std::cmp::Ordering {
|
||||
match (a.is_feasible(), b.is_feasible()) {
|
||||
(true, false) => std::cmp::Ordering::Less,
|
||||
|
||||
Reference in New Issue
Block a user