From 8616a41ce8085c9e629ce274652302fd0909bac6 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 28 Mar 2024 14:02:04 +0100 Subject: [PATCH] fmt --- src/ga.rs | 5 ++- src/ga/operators/crossover.rs | 81 +++++++++++++++++++++++++++++------ tests/crossover_tests.rs | 2 +- 3 files changed, 73 insertions(+), 15 deletions(-) diff --git a/src/ga.rs b/src/ga.rs index 69f5be0..dba1c67 100644 --- a/src/ga.rs +++ b/src/ga.rs @@ -317,7 +317,10 @@ where // FIXME: Do not assume that population size is an even number. self.timer.start(); for parents in mating_pool.chunks(2) { - let crt_children = self.config.crossover_operator.apply(&self.metadata, parents[0], parents[1]); + let crt_children = + self.config + .crossover_operator + .apply(&self.metadata, parents[0], parents[1]); children.push(crt_children.0); children.push(crt_children.1); diff --git a/src/ga/operators/crossover.rs b/src/ga/operators/crossover.rs index a6683d1..874e21a 100644 --- a/src/ga/operators/crossover.rs +++ b/src/ga/operators/crossover.rs @@ -4,8 +4,8 @@ use std::collections::{HashMap, HashSet}; use std::hash::Hash; use std::ops::{Index, IndexMut}; -use crate::ga::GAMetadata; use crate::ga::individual::{Chromosome, IndividualTrait}; +use crate::ga::GAMetadata; use push_trait::{Nothing, Push}; use rand::prelude::SliceRandom; use rand::{rngs::ThreadRng, Rng}; @@ -21,7 +21,12 @@ pub trait CrossoverOperator { /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT); + fn apply( + &mut self, + metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT); } /// # Single point crossover operator @@ -70,7 +75,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { let chromosome_len = parent_1.chromosome().len(); let cut_point = self.rng.gen_range(0..chromosome_len); @@ -137,7 +147,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -239,7 +254,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -325,7 +345,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -403,7 +428,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -535,7 +565,12 @@ where /// /// * `parent_1` - First parent to take part in crossover /// * `parent_2` - Second parent to take part in crossover - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -657,7 +692,12 @@ where /// /// * `parent_1` - one of the parents to take part in crossover /// * `parent_2` - one of the parents to take part in crossover - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -820,7 +860,12 @@ where /// /// * `parent_1` - First parent to take part in crossover /// * `parent_2` - Second parent to take part in crossover - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { assert_eq!( parent_1.chromosome().len(), parent_2.chromosome().len(), @@ -889,7 +934,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { let chromosome_len = parent_1.chromosome().len(); let cut_point = self.rng.gen_range(0..chromosome_len); @@ -948,7 +998,12 @@ where /// /// * `parent_1` - First parent to take part in recombination /// * `parent_2` - Second parent to take part in recombination - fn apply(&mut self, _metadata: &GAMetadata, parent_1: &IndividualT, parent_2: &IndividualT) -> (IndividualT, IndividualT) { + fn apply( + &mut self, + _metadata: &GAMetadata, + parent_1: &IndividualT, + parent_2: &IndividualT, + ) -> (IndividualT, IndividualT) { let mut child_1 = parent_1.clone(); let mut child_2 = parent_2.clone(); @@ -966,7 +1021,7 @@ mod test { use crate::ga::individual::IndividualTrait; use crate::ga::operators::crossover::Ppx; use crate::ga::operators::crossover::{CrossoverOperator, FixedPoint, Pmx, Shuffle}; - use crate::ga::{Individual, GAMetadata}; + use crate::ga::{GAMetadata, Individual}; use std::iter::zip; #[test] diff --git a/tests/crossover_tests.rs b/tests/crossover_tests.rs index b49feb1..dad43ca 100644 --- a/tests/crossover_tests.rs +++ b/tests/crossover_tests.rs @@ -1,8 +1,8 @@ #![cfg(feature = "ga")] -use ecrs::ga::GAMetadata; use ecrs::ga::individual::{IndividualTrait, RealValueIndividual}; use ecrs::ga::operators::crossover::Ppx; +use ecrs::ga::GAMetadata; use ecrs::ga::{ operators::crossover::{CrossoverOperator, MultiPoint, SinglePoint, TwoPoint, Uniform}, population::{PopulationGenerator, RandomPoints},