<commit_before>#[cfg(test)]
use rand::Rand;

use rand::Rng;

/// Modifies ind1 and ind2 in-place.
pub fn simulated_binary_crossover<R: Rng>(rng: &mut R,
                                          ind1: &mut [f32],
                                          ind2: &mut [f32],
                                          eta: f32) {
    assert!(ind1.len() == ind2.len());

    let mut iter = ind1.iter_mut().zip(ind2.iter_mut());
    for (x1, x2) in iter {
        let (old_x1, old_x2) = (*x1, *x2);

        let u = rng.gen::<f32>();
        debug_assert!(u >= 0.0 && u < 1.0);

        let beta = if u <= 0.5 {
                       2.0 * u
                   } else {
                       1.0 / (2.0 * (1.0 - u))
                   }
                   .powf(1.0 / (eta + 1.0));

        *x1 = 0.5 * (((1.0 + beta) * old_x1) + ((1.0 - beta) * old_x2));
        *x2 = 0.5 * (((1.0 - beta) * old_x1) + ((1.0 + beta) * old_x2));
    }
}

#[test]
fn test_sbx() {
    let mut rng = ::rand::isaac::Isaac64Rng::new_unseeded();
<commit_msg>Factor out some code<commit_after>#[cfg(test)]
use rand::Rand;

use rand::Rng;

#[inline]
fn sbx_beta(u: f32, eta: f32) -> f32 {
    debug_assert!(u >= 0.0 && u < 1.0);

    if u <= 0.5 {
        2.0 * u
    } else {
        1.0 / (2.0 * (1.0 - u))
    }
    .powf(1.0 / (eta + 1.0))
}

/// Modifies ind1 and ind2 in-place.
/// Reference: http://www.iitk.ac.in/kangal/papers/k2011017.pdf
pub fn simulated_binary_crossover<R: Rng>(rng: &mut R,
                                          ind1: &mut [f32],
                                          ind2: &mut [f32],
                                          eta: f32) {
    assert!(ind1.len() == ind2.len());

    let mut iter = ind1.iter_mut().zip(ind2.iter_mut());
    for (x1, x2) in iter {
        let (p1, p2) = (*x1, *x2);

        let u = rng.gen::<f32>();
        let beta = sbx_beta(u, eta);

        let c1 = 0.5 * (((1.0 + beta) * p1) + ((1.0 - beta) * p2));
        let c2 = 0.5 * (((1.0 - beta) * p1) + ((1.0 + beta) * p2));

        *x1 = c1;
        *x2 = c2;
    }
}

#[test]
fn test_sbx() {
    let mut rng = ::rand::isaac::Isaac64Rng::new_unseeded();

{'fraction_non_alphanumeric': 0.19227188081936686, 'fraction_numerical': 0.06378026070763501, 'mean_word_length': 1.374585635359116, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4286309', 'n_tokens_mistral': 883, 'n_tokens_neox': 837, 'n_words': 190}