1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
//! 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. //! //! ```rust //! 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. //! //! ```rust //! 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. //! //! ```rust //! 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()); //! } //! ``` use crate::{ genetic::Genotype, random::{get_rng, random_seed, Prng, Rng, Seed}, }; use rand::distributions::uniform::SampleUniform; use rayon; use std::{fmt::Debug, marker::PhantomData}; /// The `Population` defines a set of possible solutions to the optimization /// or search problem. #[derive(Clone, Debug, PartialEq)] pub struct Population<G> where G: Genotype, { /// The individuals or members of the population. individuals: Vec<G>, } impl<G> Population<G> where G: Genotype, { /// Creates a new `Population` with the given individuals as members. pub fn with_individuals(individuals: Vec<G>) -> Population<G> { Population { individuals } } /// Returns a slice of all individuals of this `Population`. pub fn individuals(&self) -> &[G] { &self.individuals } /// Returns the number of individuals in this `Population`. pub fn size(&self) -> usize { self.individuals.len() } } /// The `PopulationBuilder` creates a new `Population` with a number of newly /// created individuals or just individual `genetic::Genotype`s. /// /// Typically the `PopulationBuilder` is used to create the initial population /// with randomly created individuals. /// /// To use this `PopulationBuilder` for a custom `genetic::Genotype` the trait /// `GenomeBuilder` must be implemented for the custom `genetic::Genotype`. #[allow(missing_copy_implementations)] #[derive(Clone, Debug, PartialEq)] pub struct PopulationBuilder; impl PopulationBuilder { fn build_population<B, G>(genome_builder: &B, size: usize, rng: Prng) -> Population<G> where B: GenomeBuilder<G>, G: Genotype, { if size < 60 { let mut rng = rng; Population { individuals: (0..size) .map(|index| genome_builder.build_genome(index, &mut rng)) .collect(), } } else { let mut rng1 = rng.clone(); rng1.jump(); let mut rng2 = rng1.clone(); rng2.jump(); let left_size = size / 2; let right_size = size - left_size; let (left_population, right_population) = rayon::join( || Self::build_population(genome_builder, left_size, rng1), || Self::build_population(genome_builder, right_size, rng2), ); let mut right_individuals = right_population.individuals; let mut individuals = left_population.individuals; individuals.append(&mut right_individuals); Population { individuals } } } } /// A `GenomeBuilder` defines how to build individuals of a population for /// custom `genetic::Genotype`s. /// /// Typically the individuals are generated randomly. pub trait GenomeBuilder<G>: Sync where G: Genotype, { /// Builds a new genome of type `genetic::Genotype` for the given /// `index` using the given random number generator `rng`. fn build_genome<R>(&self, index: usize, rng: &mut R) -> G where R: Rng + Sized; } #[allow(missing_copy_implementations)] #[derive(Clone, Debug, PartialEq)] pub struct EmptyPopulationBuilder { // Phantom data to prevent direct instantiation by lib users. _empty: PhantomData<bool>, } impl EmptyPopulationBuilder { pub fn with_genome_builder<B, G>( self, genome_builder: B, ) -> PopulationWithGenomeBuilderBuilder<B, G> where B: GenomeBuilder<G>, G: Genotype, { PopulationWithGenomeBuilderBuilder { _g: PhantomData, genome_builder, } } } #[derive(Clone, Debug, PartialEq)] pub struct PopulationWithGenomeBuilderBuilder<B, G> where B: GenomeBuilder<G>, G: Genotype, { _g: PhantomData<G>, genome_builder: B, } impl<B, G> PopulationWithGenomeBuilderBuilder<B, G> where B: GenomeBuilder<G>, G: Genotype, { pub fn of_size( self, population_size: usize, ) -> PopulationWithGenomeBuilderAndSizeBuilder<B, G> { PopulationWithGenomeBuilderAndSizeBuilder { _g: self._g, genome_builder: self.genome_builder, population_size, } } } #[derive(Clone, Debug, PartialEq)] pub struct PopulationWithGenomeBuilderAndSizeBuilder<B, G> where B: GenomeBuilder<G>, G: Genotype, { _g: PhantomData<G>, genome_builder: B, population_size: usize, } impl<B, G> PopulationWithGenomeBuilderAndSizeBuilder<B, G> where B: GenomeBuilder<G>, G: Genotype, { pub fn uniform_at_random(self) -> Population<G> { PopulationBuilder::build_population( &self.genome_builder, self.population_size, get_rng(random_seed()), ) } pub fn using_seed(self, seed: Seed) -> Population<G> { PopulationBuilder::build_population( &self.genome_builder, self.population_size, get_rng(seed), ) } } pub fn build_population() -> EmptyPopulationBuilder { EmptyPopulationBuilder { _empty: PhantomData, } } /// A `GenomeBuilder` that builds binary encoded `genetic::Genotype`s. /// /// The default implementation can build `fixedbitset::FixedBitSet` genomes /// and `Vec<bool>` genomes. #[allow(missing_copy_implementations)] #[derive(Clone, Debug, PartialEq)] pub struct BinaryEncodedGenomeBuilder { genome_length: usize, } impl BinaryEncodedGenomeBuilder { /// Returns a new instance of the `BinaryEncodedGenomeBuilder` that builds /// binary encoded genomes of length specified by the given `genome_length`. pub fn new(genome_length: usize) -> Self { BinaryEncodedGenomeBuilder { genome_length } } } impl GenomeBuilder<Vec<bool>> for BinaryEncodedGenomeBuilder { fn build_genome<R>(&self, _index: usize, rng: &mut R) -> Vec<bool> where R: Rng + Sized, { (0..self.genome_length).map(|_| rng.gen()).collect() } } #[cfg(feature = "fixedbitset")] mod fixedbitset_genome_builder { use super::{BinaryEncodedGenomeBuilder, GenomeBuilder}; use fixedbitset::FixedBitSet; use rand::Rng; impl GenomeBuilder<FixedBitSet> for BinaryEncodedGenomeBuilder { fn build_genome<R>(&self, _index: usize, rng: &mut R) -> FixedBitSet where R: Rng + Sized, { let mut genome = FixedBitSet::with_capacity(self.genome_length); for bit in 0..self.genome_length { genome.set(bit, rng.gen()); } genome } } } #[cfg(feature = "smallvec")] mod smallvec_genome_builder { use super::{BinaryEncodedGenomeBuilder, GenomeBuilder, ValueEncodedGenomeBuilder}; use rand::{distributions::uniform::SampleUniform, Rng}; use smallvec::{Array, SmallVec}; use std::fmt::Debug; impl<A> GenomeBuilder<SmallVec<A>> for BinaryEncodedGenomeBuilder where A: Array<Item = bool> + Sync, { fn build_genome<R>(&self, _index: usize, rng: &mut R) -> SmallVec<A> where R: Rng + Sized, { (0..self.genome_length).map(|_| rng.gen()).collect() } } impl<A, V> GenomeBuilder<SmallVec<A>> for ValueEncodedGenomeBuilder<V> where A: Array<Item = V> + Sync, V: Clone + Debug + PartialEq + PartialOrd + SampleUniform + Send + Sync, { fn build_genome<R>(&self, _: usize, rng: &mut R) -> SmallVec<A> where R: Rng + Sized, { (0..self.genome_length) .map(|_| rng.gen_range(self.min_value.clone(), self.max_value.clone())) .collect() } } } /// A `GenomeBuilder` that builds value encoded `genetic::Genotype`s. /// /// The default implementation can build `Vec<T>` genomes. The values of /// `T` are generated randomly in the range between a min value and a max /// value. #[derive(Clone, Debug, PartialEq)] pub struct ValueEncodedGenomeBuilder<V> { genome_length: usize, min_value: V, max_value: V, } impl<V> ValueEncodedGenomeBuilder<V> { /// Returns a new instance of the `ValueEncodedGenomeBuilder` that builds /// value encoded genomes of length specified by the given `genome_length`. /// /// The values of the generated genomes are in the range between the given /// `min_value` (inclusive) and `max_value` (exclusive). pub fn new(genome_length: usize, min_value: V, max_value: V) -> Self { ValueEncodedGenomeBuilder { genome_length, min_value, max_value, } } } impl<V> GenomeBuilder<Vec<V>> for ValueEncodedGenomeBuilder<V> where V: Clone + Debug + PartialEq + PartialOrd + SampleUniform + Send + Sync, { fn build_genome<R>(&self, _: usize, rng: &mut R) -> Vec<V> where R: Rng + Sized, { (0..self.genome_length) .map(|_| rng.gen_range(self.min_value.clone(), self.max_value.clone())) .collect() } } #[cfg(test)] mod tests;