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:
@@ -202,6 +202,125 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl Umda {
|
||||
/// Async version of [`Optimizer::run`] — drives evaluations through
|
||||
/// the user-chosen async runtime. Available only with the `async`
|
||||
/// feature.
|
||||
///
|
||||
/// `concurrency` bounds in-flight evaluations per batch (initial
|
||||
/// population and per-generation samples).
|
||||
pub async fn run_async<P>(
|
||||
&mut self,
|
||||
problem: &P,
|
||||
concurrency: usize,
|
||||
) -> OptimizationResult<Vec<bool>>
|
||||
where
|
||||
P: crate::core::async_problem::AsyncProblem<Decision = Vec<bool>>,
|
||||
{
|
||||
use crate::algorithms::parallel_eval_async::evaluate_batch_async;
|
||||
|
||||
assert!(
|
||||
self.config.population_size >= 2,
|
||||
"Umda population_size must be >= 2"
|
||||
);
|
||||
assert!(
|
||||
self.config.selected_size >= 1,
|
||||
"Umda selected_size must be >= 1",
|
||||
);
|
||||
assert!(
|
||||
self.config.selected_size <= self.config.population_size,
|
||||
"Umda selected_size must be <= population_size",
|
||||
);
|
||||
assert!(self.config.bits >= 1, "Umda bits must be >= 1");
|
||||
let objectives = problem.objectives();
|
||||
assert!(
|
||||
objectives.is_single_objective(),
|
||||
"Umda requires exactly one objective",
|
||||
);
|
||||
let direction = objectives.objectives[0].direction;
|
||||
let n = self.config.population_size;
|
||||
let bits = self.config.bits;
|
||||
let mu = self.config.selected_size;
|
||||
let mut rng = rng_from_seed(self.config.seed);
|
||||
|
||||
let mut decisions: Vec<Vec<bool>> = (0..n)
|
||||
.map(|_| (0..bits).map(|_| rng.random_bool(0.5)).collect())
|
||||
.collect();
|
||||
let mut population = evaluate_batch_async(problem, decisions.clone(), concurrency).await;
|
||||
let mut evaluations = population.len();
|
||||
|
||||
let smoothing = 1.0 / (2.0 * mu as f64);
|
||||
let prob_min = smoothing;
|
||||
let prob_max = 1.0 - smoothing;
|
||||
|
||||
let mut best_seen: Option<Candidate<Vec<bool>>> = None;
|
||||
for c in &population {
|
||||
let beats = match &best_seen {
|
||||
None => true,
|
||||
Some(b) => better_than_so(&c.evaluation, &b.evaluation, direction),
|
||||
};
|
||||
if beats {
|
||||
best_seen = Some(c.clone());
|
||||
}
|
||||
}
|
||||
|
||||
for _ in 0..self.config.generations {
|
||||
let mut order: Vec<usize> = (0..population.len()).collect();
|
||||
order.sort_by(|&a, &b| {
|
||||
compare_so(
|
||||
&population[a].evaluation,
|
||||
&population[b].evaluation,
|
||||
direction,
|
||||
)
|
||||
});
|
||||
let selected: Vec<&Candidate<Vec<bool>>> =
|
||||
order.iter().take(mu).map(|&i| &population[i]).collect();
|
||||
|
||||
let mut probs = vec![0.0_f64; bits];
|
||||
for c in &selected {
|
||||
for (i, b) in c.decision.iter().enumerate() {
|
||||
if *b {
|
||||
probs[i] += 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
for p in probs.iter_mut() {
|
||||
*p = (*p / mu as f64).clamp(prob_min, prob_max);
|
||||
}
|
||||
|
||||
decisions = (0..n)
|
||||
.map(|_| probs.iter().map(|&p| rng.random_bool(p)).collect())
|
||||
.collect();
|
||||
|
||||
population = evaluate_batch_async(problem, decisions.clone(), concurrency).await;
|
||||
evaluations += population.len();
|
||||
|
||||
for c in &population {
|
||||
let beats = match &best_seen {
|
||||
None => true,
|
||||
Some(b) => better_than_so(&c.evaluation, &b.evaluation, direction),
|
||||
};
|
||||
if beats {
|
||||
best_seen = Some(c.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let best = best_seen.expect("at least one generation evaluated");
|
||||
let final_pop = vec![best.clone()];
|
||||
let front = vec![best.clone()];
|
||||
let best_opt = best_candidate(&final_pop, &objectives);
|
||||
OptimizationResult::new(
|
||||
Population::new(final_pop),
|
||||
front,
|
||||
best_opt,
|
||||
evaluations,
|
||||
self.config.generations,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn compare_so(
|
||||
a: &crate::core::evaluation::Evaluation,
|
||||
b: &crate::core::evaluation::Evaluation,
|
||||
|
||||
Reference in New Issue
Block a user