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:
@@ -215,6 +215,124 @@ fn better_than(
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<I, V> SimulatedAnnealing<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 SA 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(),
|
||||
"SimulatedAnnealing requires exactly one objective",
|
||||
);
|
||||
assert!(
|
||||
self.config.initial_temperature > 0.0,
|
||||
"SimulatedAnnealing initial_temperature must be positive",
|
||||
);
|
||||
assert!(
|
||||
self.config.final_temperature > 0.0,
|
||||
"SimulatedAnnealing final_temperature must be positive",
|
||||
);
|
||||
assert!(
|
||||
self.config.final_temperature <= self.config.initial_temperature,
|
||||
"SimulatedAnnealing final_temperature must be <= initial_temperature",
|
||||
);
|
||||
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(),
|
||||
"SimulatedAnnealing initializer returned no decisions",
|
||||
);
|
||||
let mut current_decision = initial.remove(0);
|
||||
let mut current_eval = problem.evaluate_async(¤t_decision).await;
|
||||
let mut best_decision = current_decision.clone();
|
||||
let mut best_eval = current_eval.clone();
|
||||
let mut evaluations = 1usize;
|
||||
|
||||
let cooling = if self.config.iterations <= 1 {
|
||||
1.0
|
||||
} else {
|
||||
(self.config.final_temperature / self.config.initial_temperature)
|
||||
.powf(1.0 / (self.config.iterations as f64 - 1.0))
|
||||
};
|
||||
let mut temperature = self.config.initial_temperature;
|
||||
|
||||
for _ in 0..self.config.iterations {
|
||||
let parents = vec![current_decision.clone()];
|
||||
let children = self.variation.vary(&parents, &mut rng);
|
||||
assert!(
|
||||
!children.is_empty(),
|
||||
"SimulatedAnnealing variation returned no children"
|
||||
);
|
||||
let child_decision = children.into_iter().next().unwrap();
|
||||
let child_eval = problem.evaluate_async(&child_decision).await;
|
||||
evaluations += 1;
|
||||
|
||||
let accept = 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) => {
|
||||
let delta = match direction {
|
||||
Direction::Minimize => {
|
||||
child_eval.objectives[0] - current_eval.objectives[0]
|
||||
}
|
||||
Direction::Maximize => {
|
||||
current_eval.objectives[0] - child_eval.objectives[0]
|
||||
}
|
||||
};
|
||||
if delta <= 0.0 {
|
||||
true
|
||||
} else {
|
||||
let prob = (-delta / temperature).exp();
|
||||
rng.random::<f64>() < prob
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if accept {
|
||||
current_decision = child_decision;
|
||||
current_eval = child_eval;
|
||||
if better_than(¤t_eval, &best_eval, direction) {
|
||||
best_decision = current_decision.clone();
|
||||
best_eval = current_eval.clone();
|
||||
}
|
||||
}
|
||||
temperature *= cooling;
|
||||
}
|
||||
|
||||
let best = Candidate::new(best_decision, best_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