Module genevo::population
source · [−]Expand description
The population
module defines the Population
struct and the
PopulationBuilder
for building random populations.
To use the PopulationBuilder
for building Population
s of a custom
genetic::Genotype
an implementation of the GenomeBuilder
must be
provided. A GenomeBuilder
can build new individuals of a custom
genetic::Genotype
.
Default implementations of GenomeBuilder
are provided for the binary
encoded types fixedbitset::FixedBitSet
and Vec<bool>
and for the
value encoded type Vec<T>
.
Examples
In the first example we build a population of binary encoded genomes. Each genome has a length of 12 bits and the population comprises 200 individuals.
use genevo::prelude::*;
use genevo::population::BinaryEncodedGenomeBuilder;
#[cfg(feature = "fixedbitset")]
use fixedbitset::FixedBitSet;
fn main() {
#[cfg(feature = "fixedbitset")]
let population: Population<FixedBitSet> = build_population()
.with_genome_builder(BinaryEncodedGenomeBuilder::new(12))
.of_size(200)
.uniform_at_random();
#[cfg(not(feature = "fixedbitset"))]
let population: Population<Vec<bool>> = build_population()
.with_genome_builder(BinaryEncodedGenomeBuilder::new(12))
.of_size(200)
.uniform_at_random();
println!("{:?}", population);
assert_eq!(200, population.size());
}
The next example builds a population of value encoded genomes. Each genome
is represented by a Vec
of 4 i64
values in the range of -200 to +200.
The generated population consists of 200 individuals.
use genevo::prelude::*;
use genevo::population::ValueEncodedGenomeBuilder;
fn main() {
let population: Population<Vec<i64>> = build_population()
.with_genome_builder(ValueEncodedGenomeBuilder::new(4, -200, 201))
.of_size(200)
.uniform_at_random();
println!("{:?}", population);
assert_eq!(200, population.size());
}
In the following example we demonstrate how to generate a population
containing individuals of the custom type Pos
. Each genome consists of 8
Pos
values. The generated population comprises 200 individuals.
use genevo::prelude::*;
#[derive(Clone,Debug,PartialEq)]
struct Pos {
x: usize,
y: usize,
}
struct PositionsBuilder;
impl GenomeBuilder<Vec<Pos>> for PositionsBuilder {
fn build_genome<R>(&self, _: usize, rng: &mut R) -> Vec<Pos>
where R: Rng + Sized
{
(0..8).map(|row|
Pos {
x: row,
y: rng.gen_range(0..8)
}
).collect()
}
}
fn main() {
let population: Population<Vec<Pos>> = build_population()
.with_genome_builder(PositionsBuilder)
.of_size(200)
.uniform_at_random();
println!("{:?}", population);
assert_eq!(200, population.size());
}
Structs
A GenomeBuilder
that builds binary encoded genetic::Genotype
s.
The Population
defines a set of possible solutions to the optimization
or search problem.
The PopulationBuilder
creates a new Population
with a number of newly
created individuals or just individual genetic::Genotype
s.
A GenomeBuilder
that builds value encoded genetic::Genotype
s.
Traits
A GenomeBuilder
defines how to build individuals of a population for
custom genetic::Genotype
s.