diff --git a/lab5/src/algo/mod.rs b/lab5/src/algo/mod.rs new file mode 100644 index 0000000..fef34b6 --- /dev/null +++ b/lab5/src/algo/mod.rs @@ -0,0 +1,3 @@ +mod product; +mod mutate; +mod simulation; \ No newline at end of file diff --git a/lab5/src/algo/mutate.rs b/lab5/src/algo/mutate.rs new file mode 100644 index 0000000..0cf3bba --- /dev/null +++ b/lab5/src/algo/mutate.rs @@ -0,0 +1,8 @@ +use rand::Rng; + +pub(super) fn mutate(g: &mut [usize], rng: &mut impl Rng) { + let swap_start = rng.random_range(0..(g.len() - 1)); + let swap_end = rng.random_range(swap_start..g.len()); + + g.swap(swap_start, swap_end); +} diff --git a/lab5/src/algo/product.rs b/lab5/src/algo/product.rs new file mode 100644 index 0000000..29c9c22 --- /dev/null +++ b/lab5/src/algo/product.rs @@ -0,0 +1,27 @@ +use rand::Rng; +use std::collections::HashSet; +/** + * OX (Order Crossover) + */ +pub(super) fn product(p1: &[usize], p2: &[usize], out: &mut [usize], rng: &mut impl Rng) { + assert_eq!(p1.len(), p2.len()); + assert_eq!(p1.len(), out.len()); + + let split_start = rng.random_range(0..(p1.len() - 1)); + let split_end = rng.random_range(split_start..p1.len()); + + let used: HashSet = p1[split_start..split_end].iter().map(|x| *x).collect(); + for i in split_start..split_end { + out[i] = p1[i]; + } + + let mut j = split_end; + for i in 0..p2.len() { + let i = i % p2.len(); + if used.contains(&p2[i]) { + continue; + } + out[j % out.len()] = p2[i]; + j += 1; + } +} diff --git a/lab5/src/algo/simulation.rs b/lab5/src/algo/simulation.rs new file mode 100644 index 0000000..e69de29 diff --git a/lab5/src/main.rs b/lab5/src/main.rs index e71fdf5..552b8c2 100644 --- a/lab5/src/main.rs +++ b/lab5/src/main.rs @@ -1 +1,3 @@ +mod algo; + fn main() {} \ No newline at end of file