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:
@@ -3,6 +3,7 @@
|
||||
//! This is the reference example for spec §2.4 / §12.1: read this file before
|
||||
//! writing your own optimizer.
|
||||
|
||||
use crate::algorithms::parallel_eval::evaluate_batch;
|
||||
use crate::core::candidate::Candidate;
|
||||
use crate::core::population::Population;
|
||||
use crate::core::problem::Problem;
|
||||
@@ -50,7 +51,8 @@ impl<I> RandomSearch<I> {
|
||||
|
||||
impl<P, I> Optimizer<P> for RandomSearch<I>
|
||||
where
|
||||
P: Problem,
|
||||
P: Problem + Sync,
|
||||
P::Decision: Send,
|
||||
I: Initializer<P::Decision>,
|
||||
{
|
||||
fn run(&mut self, problem: &P) -> OptimizationResult<P::Decision> {
|
||||
@@ -61,11 +63,8 @@ where
|
||||
|
||||
for _ in 0..self.config.iterations {
|
||||
let decisions = self.initializer.initialize(self.config.batch_size, &mut rng);
|
||||
for decision in decisions {
|
||||
let eval = problem.evaluate(&decision);
|
||||
evaluations += 1;
|
||||
all.push(Candidate::new(decision, eval));
|
||||
}
|
||||
evaluations += decisions.len();
|
||||
all.extend(evaluate_batch(problem, decisions));
|
||||
}
|
||||
|
||||
let front = pareto_front(&all, &objectives);
|
||||
|
||||
Reference in New Issue
Block a user