Get benchmarks running again.
This commit is contained in:
@@ -63,64 +63,74 @@ impl Individual for Parabole {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn criterion_benchmark(c: &mut Criterion) {
|
fn criterion_benchmark(c: &mut Criterion) {
|
||||||
let initial = (0..1000)
|
c.bench_function("calculate", move |b| {
|
||||||
.map(|i| Parabole { x: i as f64 })
|
let initial = (0..1000)
|
||||||
.collect::<Vec<_>>();
|
.map(|i| Parabole { x: f64::from(i) })
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let population_size = initial.len();
|
let mut simulation = Simulation::with_population(initial);
|
||||||
|
|
||||||
let mut simulation = Simulation::with_population(initial);
|
b.iter(|| {
|
||||||
|
simulation.calculate();
|
||||||
c.bench_function("calculate", |b| b.iter(|| {
|
|
||||||
simulation.calculate();
|
|
||||||
}));
|
|
||||||
|
|
||||||
c.bench_function("evolve", |b| b.iter(|| {
|
|
||||||
simulation.evolve(|parents, population| {
|
|
||||||
// Mate top 10 to get 5 children.
|
|
||||||
parents
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.filter(|&(n, _)| n % 2 == 0)
|
|
||||||
.map(|(_, wrapper)| wrapper)
|
|
||||||
.take(5)
|
|
||||||
.zip(
|
|
||||||
parents
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.filter(|&(n, _)| n % 2 == 1)
|
|
||||||
.map(|(_, wrapper)| wrapper)
|
|
||||||
.take(5),
|
|
||||||
)
|
|
||||||
.map(|(a, b)| a.individual.mate(&b.individual))
|
|
||||||
.map(|individual| {
|
|
||||||
Wrapper {
|
|
||||||
individual: individual,
|
|
||||||
fitness: None,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.for_each(|wrapper| { population.push(wrapper); });
|
|
||||||
|
|
||||||
// Mutate all to get new children.
|
|
||||||
parents
|
|
||||||
.par_iter()
|
|
||||||
.map(|wrapper| wrapper.individual.clone())
|
|
||||||
.map(|mut individual| {
|
|
||||||
individual.mutate();
|
|
||||||
|
|
||||||
Wrapper {
|
|
||||||
individual: individual,
|
|
||||||
fitness: None,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect_into(population);
|
|
||||||
|
|
||||||
// Add all parents again.
|
|
||||||
population.extend(parents.iter().cloned());
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
simulation.population.truncate(population_size);
|
c.bench_function("evolve", move |b| {
|
||||||
}));
|
let initial = (0..1000)
|
||||||
|
.map(|i| Parabole { x: f64::from(i) })
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let population_size = initial.len();
|
||||||
|
|
||||||
|
let mut simulation = Simulation::with_population(initial);
|
||||||
|
|
||||||
|
b.iter(|| {
|
||||||
|
simulation.evolve(|parents, population| {
|
||||||
|
// Mate top 10 to get 5 children.
|
||||||
|
parents
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(n, _)| n % 2 == 0)
|
||||||
|
.map(|(_, wrapper)| wrapper)
|
||||||
|
.take(5)
|
||||||
|
.zip(
|
||||||
|
parents
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(n, _)| n % 2 == 1)
|
||||||
|
.map(|(_, wrapper)| wrapper)
|
||||||
|
.take(5),
|
||||||
|
)
|
||||||
|
.map(|(a, b)| a.individual.mate(&b.individual))
|
||||||
|
.map(|individual| {
|
||||||
|
Wrapper {
|
||||||
|
individual: individual,
|
||||||
|
fitness: None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.for_each(|wrapper| { population.push(wrapper); });
|
||||||
|
|
||||||
|
// Mutate all to get new children.
|
||||||
|
parents
|
||||||
|
.par_iter()
|
||||||
|
.map(|wrapper| wrapper.individual.clone())
|
||||||
|
.map(|mut individual| {
|
||||||
|
individual.mutate();
|
||||||
|
|
||||||
|
Wrapper {
|
||||||
|
individual: individual,
|
||||||
|
fitness: None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect_into(population);
|
||||||
|
|
||||||
|
// Add all parents again.
|
||||||
|
population.extend(parents.iter().cloned());
|
||||||
|
});
|
||||||
|
|
||||||
|
simulation.population.truncate(population_size);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(benches, criterion_benchmark);
|
criterion_group!(benches, criterion_benchmark);
|
||||||
|
|||||||
Reference in New Issue
Block a user