use crate::genotypes::List;
use rand::Rng;
use std::fmt::Debug;
pub fn list_random_initialization<T>(
genes_per_chromosome: usize,
alleles: Option<&[List<T>]>,
_needs_unique_ids: Option<bool>,
) -> Vec<List<T>>
where
T: Clone + Sync + Send + Default + Debug,
{
let alleles = alleles.expect("Alleles must be provided for list_random_initialization");
let mut rng = crate::rng::make_rng();
let mut dna = Vec::with_capacity(genes_per_chromosome);
for _ in 0..genes_per_chromosome {
let template = &alleles[rng.random_range(0..alleles.len())];
let index = rng.random_range(0..template.alleles.len());
let gene = List::new(
index as i32,
template.alleles.clone(),
template.alleles[0].clone(),
)
.expect("list_random_initialization: index always valid");
dna.push(gene);
}
dna
}
pub fn list_random_initialization_without_repetitions<T>(
genes_per_chromosome: usize,
alleles: Option<&[List<T>]>,
_needs_unique_ids: Option<bool>,
) -> Vec<List<T>>
where
T: Clone + Sync + Send + Default + Debug,
{
let alleles = alleles
.expect("Alleles must be provided for list_random_initialization_without_repetitions");
let template = &alleles[0];
assert!(
genes_per_chromosome <= template.alleles.len(),
"genes_per_chromosome exceeds allele set size for without-repetitions initialization"
);
let mut rng = crate::rng::make_rng();
let mut indices: Vec<usize> = (0..template.alleles.len()).collect();
for i in (1..indices.len()).rev() {
let j = rng.random_range(0..=i);
indices.swap(i, j);
}
let mut dna = Vec::with_capacity(genes_per_chromosome);
for &idx in indices.iter().take(genes_per_chromosome) {
let gene = List::new(
idx as i32,
template.alleles.clone(),
template.alleles[0].clone(),
)
.expect("list_random_initialization_without_repetitions: index always valid");
dna.push(gene);
}
dna
}