feat: add optional parallel feature for population-evaluation parallelism
Adds a `parallel` Cargo feature that pulls in rayon and parallelizes the only step that's actually expensive in practice — calls to `Problem::evaluate` — across the population. RNG-driven steps (parent and donor selection, variation, replacement decisions) stay serial, so seeded runs remain deterministic regardless of feature state, and the default and `--features parallel` builds produce bit-identical results. Wiring: - New `algorithms::parallel_eval::evaluate_batch` helper with two cfg-gated implementations (rayon's `into_par_iter` when the feature is on, plain `into_iter` otherwise). Both preserve input order, so pareto_front and crowding-distance decisions remain reproducible. - `RandomSearch`, `Nsga2`, and `DifferentialEvolution` now route population/offspring evaluation through the helper. NSGA-II's main loop is restructured into a serial selection-and-variation phase followed by a parallel-friendly batch evaluation phase. - DE's per-target loop is restructured into three phases (serial trial construction → batch evaluation → serial replacement). Side effect of the restructuring: DE is now the canonical synchronous DE/rand/1/bin rather than the asynchronous variant where target `i+1` sees `i`'s in-flight update. Synchronous is the textbook formulation, so this is a small correctness improvement on top of the parallelism enable. - PAES stays serial — its main loop has a sequential dependency on the current candidate and would gain nothing from rayon. Cost: algorithm impls now require `P: Sync` and `P::Decision: Send` unconditionally so a single impl serves both feature modes. This is a small bound tightening that any plain-data Problem already satisfies; in return the public `Problem` trait itself stays unchanged and the default build picks up no new dependencies. Verified: - `cargo test` and `cargo test --features parallel` both pass; the Nsga2 `deterministic_with_same_seed` test confirms reproducibility. - `cargo run --release --example benchmarks` and the same with `--features parallel` produce bit-identical ZDT1 / Rastrigin results.
This commit is contained in:
+15
-17
@@ -2,6 +2,7 @@
|
||||
|
||||
use rand::Rng as _;
|
||||
|
||||
use crate::algorithms::parallel_eval::evaluate_batch;
|
||||
use crate::core::candidate::Candidate;
|
||||
use crate::core::population::Population;
|
||||
use crate::core::problem::Problem;
|
||||
@@ -56,7 +57,8 @@ struct Nsga2Entry<D> {
|
||||
|
||||
impl<P, I, V> Optimizer<P> for Nsga2<I, V>
|
||||
where
|
||||
P: Problem,
|
||||
P: Problem + Sync,
|
||||
P::Decision: Send,
|
||||
I: Initializer<P::Decision>,
|
||||
V: Variation<P::Decision>,
|
||||
{
|
||||
@@ -76,13 +78,8 @@ where
|
||||
n,
|
||||
"NSGA-II initializer must return exactly population_size decisions",
|
||||
);
|
||||
let mut population: Vec<Candidate<P::Decision>> = initial_decisions
|
||||
.into_iter()
|
||||
.map(|d| {
|
||||
let e = problem.evaluate(&d);
|
||||
Candidate::new(d, e)
|
||||
})
|
||||
.collect();
|
||||
let population: Vec<Candidate<P::Decision>> =
|
||||
evaluate_batch(problem, initial_decisions);
|
||||
let mut evaluations = population.len();
|
||||
|
||||
// Annotate the starting population with rank and crowding so the first
|
||||
@@ -90,9 +87,9 @@ where
|
||||
let mut annotated = annotate(population, &objectives);
|
||||
|
||||
for _ in 0..self.config.generations {
|
||||
// --- Parent selection + offspring generation ---
|
||||
let mut offspring: Vec<Candidate<P::Decision>> = Vec::with_capacity(n);
|
||||
while offspring.len() < n {
|
||||
// --- Phase 1: serial parent selection + variation ---
|
||||
let mut offspring_decisions: Vec<P::Decision> = Vec::with_capacity(n);
|
||||
while offspring_decisions.len() < n {
|
||||
let p1 = binary_tournament(&annotated, &mut rng);
|
||||
let p2 = binary_tournament(&annotated, &mut rng);
|
||||
let parents = vec![
|
||||
@@ -105,14 +102,16 @@ where
|
||||
"NSGA-II variation returned no children",
|
||||
);
|
||||
for child_decision in children {
|
||||
if offspring.len() >= n {
|
||||
if offspring_decisions.len() >= n {
|
||||
break;
|
||||
}
|
||||
let eval = problem.evaluate(&child_decision);
|
||||
evaluations += 1;
|
||||
offspring.push(Candidate::new(child_decision, eval));
|
||||
offspring_decisions.push(child_decision);
|
||||
}
|
||||
}
|
||||
// --- Phase 2: parallel-friendly batch evaluation ---
|
||||
let offspring: Vec<Candidate<P::Decision>> =
|
||||
evaluate_batch(problem, offspring_decisions);
|
||||
evaluations += offspring.len();
|
||||
|
||||
// --- Combine + survival selection ---
|
||||
let mut combined: Vec<Candidate<P::Decision>> = Vec::with_capacity(2 * n);
|
||||
@@ -143,8 +142,7 @@ where
|
||||
break;
|
||||
}
|
||||
}
|
||||
population = next;
|
||||
annotated = annotate(population, &objectives);
|
||||
annotated = annotate(next, &objectives);
|
||||
}
|
||||
|
||||
// Return final state.
|
||||
|
||||
Reference in New Issue
Block a user