feat(async): AsyncProblem trait + run_async on RandomSearch and DifferentialEvolution
Adds the headline async/await capability for IO-bound evaluations (HTTP services, RPC clients, spawned subprocesses) — the differentiator vs pymoo / hyperopt / MOEA Framework. No public-API breaks for synchronous users. The new surface is gated behind a new `async` feature flag. - core::async_problem::AsyncProblem trait (async fn evaluate_async). - algorithms::parallel_eval_async::evaluate_batch_async helper using futures::stream::FuturesOrdered with concurrency-bounded chunks; preserves input order so seeded determinism holds when evaluations are themselves deterministic. - run_async on RandomSearch and DifferentialEvolution. - examples/async_eval.rs: simulated 20 ms remote service. concurrency=1 → 4.2 s, concurrency=4 → 2.1 s (2× speedup). Bumps Cargo.toml to 0.8.0; CHANGELOG entry covers the above plus a note that 0.6.0/0.7.0 on crates.io are yanked experimentals and 0.8 picks up cleanly from 0.5.
This commit is contained in:
@@ -184,6 +184,106 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl DifferentialEvolution {
|
||||
/// 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 trials).
|
||||
pub async fn run_async<P>(
|
||||
&mut self,
|
||||
problem: &P,
|
||||
concurrency: usize,
|
||||
) -> OptimizationResult<Vec<f64>>
|
||||
where
|
||||
P: crate::core::async_problem::AsyncProblem<Decision = Vec<f64>>,
|
||||
{
|
||||
use rand::Rng as _;
|
||||
|
||||
use crate::algorithms::parallel_eval_async::evaluate_batch_async;
|
||||
use crate::traits::Initializer as _;
|
||||
|
||||
assert!(
|
||||
self.config.population_size >= 4,
|
||||
"DifferentialEvolution requires population_size >= 4",
|
||||
);
|
||||
assert!(
|
||||
(0.0..=1.0).contains(&self.config.crossover_probability),
|
||||
"DifferentialEvolution crossover_probability must be in [0.0, 1.0]",
|
||||
);
|
||||
|
||||
let objectives = problem.objectives();
|
||||
assert!(
|
||||
objectives.is_single_objective(),
|
||||
"DifferentialEvolution only supports single-objective problems",
|
||||
);
|
||||
let direction = objectives.objectives[0].direction;
|
||||
|
||||
let dim = self.bounds.bounds.len();
|
||||
let n = self.config.population_size;
|
||||
let mut rng = rng_from_seed(self.config.seed);
|
||||
|
||||
let mut decisions: Vec<Vec<f64>> = self.bounds.initialize(n, &mut rng);
|
||||
let initial_pop = evaluate_batch_async(problem, decisions.clone(), concurrency).await;
|
||||
let mut evaluations = initial_pop.len();
|
||||
let mut current_pop = initial_pop;
|
||||
let mut evals: Vec<f64> = current_pop
|
||||
.iter()
|
||||
.map(|c| c.evaluation.objectives[0])
|
||||
.collect();
|
||||
|
||||
for _generation in 0..self.config.generations {
|
||||
let trials: Vec<Vec<f64>> = (0..n)
|
||||
.map(|i| {
|
||||
let (r1, r2, r3) = pick_three_distinct(n, i, &mut rng);
|
||||
let j_rand = rng.random_range(0..dim);
|
||||
let mut trial = decisions[i].clone();
|
||||
for j in 0..dim {
|
||||
let take_donor =
|
||||
rng.random_bool(self.config.crossover_probability) || j == j_rand;
|
||||
if take_donor {
|
||||
let mutant = decisions[r1][j]
|
||||
+ self.config.differential_weight
|
||||
* (decisions[r2][j] - decisions[r3][j]);
|
||||
let (lo, hi) = self.bounds.bounds[j];
|
||||
trial[j] = mutant.clamp(lo, hi);
|
||||
}
|
||||
}
|
||||
trial
|
||||
})
|
||||
.collect();
|
||||
let trial_cands: Vec<Candidate<Vec<f64>>> =
|
||||
evaluate_batch_async(problem, trials, concurrency).await;
|
||||
evaluations += trial_cands.len();
|
||||
for (i, trial_cand) in trial_cands.into_iter().enumerate() {
|
||||
let trial_obj = trial_cand.evaluation.objectives[0];
|
||||
let target_obj = evals[i];
|
||||
let trial_better = match direction {
|
||||
Direction::Minimize => trial_obj <= target_obj,
|
||||
Direction::Maximize => trial_obj >= target_obj,
|
||||
};
|
||||
if trial_better {
|
||||
decisions[i] = trial_cand.decision.clone();
|
||||
evals[i] = trial_obj;
|
||||
current_pop[i] = trial_cand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let front = pareto_front(¤t_pop, &objectives);
|
||||
let best = best_candidate(¤t_pop, &objectives);
|
||||
OptimizationResult::new(
|
||||
Population::new(current_pop),
|
||||
front,
|
||||
best,
|
||||
evaluations,
|
||||
self.config.generations,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn pick_three_distinct(
|
||||
n: usize,
|
||||
exclude: usize,
|
||||
|
||||
@@ -22,6 +22,8 @@ pub mod nsga3;
|
||||
pub mod one_plus_one_es;
|
||||
pub mod paes;
|
||||
pub(crate) mod parallel_eval;
|
||||
#[cfg(feature = "async")]
|
||||
pub(crate) mod parallel_eval_async;
|
||||
pub mod particle_swarm;
|
||||
pub mod pesa2;
|
||||
pub mod random_search;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
//! Async population evaluator.
|
||||
//!
|
||||
//! Available only with the `async` feature. Used by the `run_async`
|
||||
//! method on algorithms that support async problems.
|
||||
|
||||
use futures::stream::{FuturesOrdered, StreamExt};
|
||||
|
||||
use crate::core::async_problem::AsyncProblem;
|
||||
use crate::core::candidate::Candidate;
|
||||
|
||||
/// Evaluate every decision concurrently against `problem`, preserving
|
||||
/// input order in the returned vector. Concurrency is bounded by
|
||||
/// `concurrency` (≥ 1) — too high a value wastes memory and may
|
||||
/// overload downstream services; too low forfeits parallelism.
|
||||
///
|
||||
/// Returns a future that the caller drives via their preferred
|
||||
/// runtime (typically tokio).
|
||||
pub async fn evaluate_batch_async<P>(
|
||||
problem: &P,
|
||||
decisions: Vec<P::Decision>,
|
||||
concurrency: usize,
|
||||
) -> Vec<Candidate<P::Decision>>
|
||||
where
|
||||
P: AsyncProblem,
|
||||
{
|
||||
assert!(
|
||||
concurrency >= 1,
|
||||
"evaluate_batch_async concurrency must be >= 1"
|
||||
);
|
||||
let mut out: Vec<Candidate<P::Decision>> = Vec::with_capacity(decisions.len());
|
||||
|
||||
// Process in concurrency-bounded chunks to keep peak memory low
|
||||
// and avoid blasting downstream services. Each chunk uses
|
||||
// FuturesOrdered to preserve per-chunk order, and chunks are
|
||||
// emitted in their natural order.
|
||||
let mut iter = decisions.into_iter();
|
||||
loop {
|
||||
let mut futs = FuturesOrdered::new();
|
||||
for _ in 0..concurrency {
|
||||
match iter.next() {
|
||||
Some(d) => {
|
||||
futs.push_back(async move {
|
||||
let e = problem.evaluate_async(&d).await;
|
||||
Candidate::new(d, e)
|
||||
});
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
if futs.is_empty() {
|
||||
break;
|
||||
}
|
||||
while let Some(c) = futs.next().await {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -113,6 +113,51 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<I> RandomSearch<I> {
|
||||
/// Async version of [`Optimizer::run`] — drives evaluations through
|
||||
/// the user-chosen async runtime (typically tokio). Useful when
|
||||
/// `evaluate` is IO-bound (HTTP, RPC, subprocess).
|
||||
///
|
||||
/// `concurrency` bounds how many evaluations are in-flight at once;
|
||||
/// `1` is sequential, larger values push more load to the
|
||||
/// downstream service.
|
||||
///
|
||||
/// Available only with the `async` feature.
|
||||
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>,
|
||||
{
|
||||
use crate::algorithms::parallel_eval_async::evaluate_batch_async;
|
||||
let objectives = problem.objectives();
|
||||
let mut rng = rng_from_seed(self.config.seed);
|
||||
let mut all: Vec<Candidate<P::Decision>> = Vec::new();
|
||||
let mut evaluations = 0usize;
|
||||
for _ in 0..self.config.iterations {
|
||||
let decisions = self
|
||||
.initializer
|
||||
.initialize(self.config.batch_size, &mut rng);
|
||||
evaluations += decisions.len();
|
||||
let cands = evaluate_batch_async(problem, decisions, concurrency).await;
|
||||
all.extend(cands);
|
||||
}
|
||||
let front = pareto_front(&all, &objectives);
|
||||
let best = best_candidate(&all, &objectives);
|
||||
OptimizationResult::new(
|
||||
Population::new(all),
|
||||
front,
|
||||
best,
|
||||
evaluations,
|
||||
self.config.iterations,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user