use criterion::*;
use genetic_algorithm::chromosome::Chromosome;
use genetic_algorithm::genotype::*;
use genetic_algorithm::population::Population;
use rand::prelude::*;
use rand::rngs::SmallRng;
pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("neighbouring_population");
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
group.plot_config(plot_config);
let mut rng1 = SmallRng::from_entropy();
let mut rng2 = SmallRng::from_entropy();
group.bench_function("range-neighbouring_population-relative", |b| {
let genotype = RangeGenotype::builder()
.with_genes_size(10)
.with_allele_range(-1.0..=1.0)
.with_mutation_type(MutationType::Range(0.1))
.build()
.unwrap();
b.iter_batched(
|| {
(
Chromosome::new(genotype.random_genes_factory(&mut rng1)),
genotype.clone(),
Population::new(vec![], true),
)
},
|(c, g, mut p)| g.fill_neighbouring_population(&c, &mut p, &mut rng2),
BatchSize::SmallInput,
);
});
group.bench_function("range-neighbouring_population-scaled", |b| {
let mut genotype = RangeGenotype::builder()
.with_genes_size(10)
.with_allele_range(-1.0..=1.0)
.with_mutation_type(MutationType::StepScaled(vec![0.1, 0.01, 0.001]))
.build()
.unwrap();
genotype.increment_scale_index();
b.iter_batched(
|| {
(
Chromosome::new(genotype.random_genes_factory(&mut rng1)),
genotype.clone(),
Population::new(vec![], true),
)
},
|(c, g, mut p)| g.fill_neighbouring_population(&c, &mut p, &mut rng2),
BatchSize::SmallInput,
);
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);