genetic_algorithms 3.0.0

Library for solving genetic algorithm problems
Documentation
//! Clone crossover implementation.

use crate::error::GaError;
use crate::traits::LinearChromosome;
/// Clone crossover: copies parents directly as offspring without any genetic exchange.
///
/// Given parents `P1` and `P2`, produces children that are exact clones:
/// child_1 = clone of P1, child_2 = clone of P2.
///
/// # Use cases
///
/// - Mutation-only evolutionary strategies where crossover should be a no-op.
/// - Baseline comparisons in experiments to isolate the effect of crossover.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::operations::crossover::clone_crossover;
/// use genetic_algorithms::chromosomes::Binary;
/// let parent1 = Binary::new();
/// let parent2 = Binary::new();
/// let _ = clone_crossover(&parent1, &parent2);
/// ```
///
/// # Errors
///
/// Returns `Err(GaError::CrossoverError)` if parents have different DNA lengths.
pub fn clone_crossover<U: LinearChromosome>(parent_1: &U, parent_2: &U) -> Result<Vec<U>, GaError> {
    if parent_1.dna().len() != parent_2.dna().len() {
        return Err(GaError::CrossoverError(format!(
            "Parents must have the same DNA length. Parent 1: {}, Parent 2: {}",
            parent_1.dna().len(),
            parent_2.dna().len()
        )));
    }

    crate::log_debug!(target="crossover_events", method="clone"; "Starting clone crossover");

    let child_1 = parent_1.clone();
    let child_2 = parent_2.clone();

    crate::log_debug!(target="crossover_events", method="clone"; "Clone crossover finished");
    Ok(vec![child_1, child_2])
}