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:
@@ -146,6 +146,84 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<I, V> HillClimber<I, V> {
|
||||
/// Async version of [`Optimizer::run`] — drives evaluations through
|
||||
/// the user-chosen async runtime. Available only with the `async`
|
||||
/// feature.
|
||||
///
|
||||
/// `concurrency` is mostly inert here because HillClimber evaluates
|
||||
/// one child per iteration; it's accepted for API parity with other
|
||||
/// algorithms.
|
||||
pub async fn run_async<P>(
|
||||
&mut self,
|
||||
problem: &P,
|
||||
concurrency: usize,
|
||||
) -> OptimizationResult<P::Decision>
|
||||
where
|
||||
P: crate::core::async_problem::AsyncProblem,
|
||||
I: Initializer<P::Decision>,
|
||||
V: Variation<P::Decision>,
|
||||
{
|
||||
let _ = concurrency;
|
||||
let objectives = problem.objectives();
|
||||
assert!(
|
||||
objectives.is_single_objective(),
|
||||
"HillClimber requires exactly one objective",
|
||||
);
|
||||
let direction = objectives.objectives[0].direction;
|
||||
let mut rng = rng_from_seed(self.config.seed);
|
||||
|
||||
let mut initial = self.initializer.initialize(1, &mut rng);
|
||||
assert!(
|
||||
!initial.is_empty(),
|
||||
"HillClimber initializer returned no decisions"
|
||||
);
|
||||
let mut current_decision = initial.remove(0);
|
||||
let mut current_eval = problem.evaluate_async(¤t_decision).await;
|
||||
let mut evaluations = 1usize;
|
||||
|
||||
for _ in 0..self.config.iterations {
|
||||
let parents = vec![current_decision.clone()];
|
||||
let children = self.variation.vary(&parents, &mut rng);
|
||||
assert!(
|
||||
!children.is_empty(),
|
||||
"HillClimber variation returned no children"
|
||||
);
|
||||
let child_decision = children.into_iter().next().unwrap();
|
||||
let child_eval = problem.evaluate_async(&child_decision).await;
|
||||
evaluations += 1;
|
||||
|
||||
let child_better = match (child_eval.is_feasible(), current_eval.is_feasible()) {
|
||||
(true, false) => true,
|
||||
(false, true) => false,
|
||||
(false, false) => {
|
||||
child_eval.constraint_violation < current_eval.constraint_violation
|
||||
}
|
||||
(true, true) => match direction {
|
||||
Direction::Minimize => child_eval.objectives[0] < current_eval.objectives[0],
|
||||
Direction::Maximize => child_eval.objectives[0] > current_eval.objectives[0],
|
||||
},
|
||||
};
|
||||
if child_better {
|
||||
current_decision = child_decision;
|
||||
current_eval = child_eval;
|
||||
}
|
||||
}
|
||||
|
||||
let best = Candidate::new(current_decision, current_eval);
|
||||
let population = Population::new(vec![best.clone()]);
|
||||
let front = vec![best.clone()];
|
||||
OptimizationResult::new(
|
||||
population,
|
||||
front,
|
||||
Some(best),
|
||||
evaluations,
|
||||
self.config.iterations,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user