Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kkafar committed Mar 28, 2024
1 parent 4397344 commit 8616a41
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
81 changes: 68 additions & 13 deletions src/ga/operators/crossover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -21,7 +21,12 @@ pub trait CrossoverOperator<IndividualT: IndividualTrait> {
///
/// * `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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/crossover_tests.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down

0 comments on commit 8616a41

Please sign in to comment.