Allelic manipulations
This commit is contained in:
parent
aa27328286
commit
6be5d415ab
6 changed files with 439 additions and 0 deletions
|
|
@ -1,3 +1,5 @@
|
|||
use std::{collections::VecDeque, fmt::Display};
|
||||
|
||||
use crate::genetics::gene::Gene;
|
||||
|
||||
pub struct PlantGenome {
|
||||
|
|
@ -33,6 +35,49 @@ impl PlantGenome {
|
|||
}
|
||||
}
|
||||
|
||||
impl Display for PlantGenome {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let indents = {
|
||||
// Vibe code
|
||||
let n = self.genes.len();
|
||||
|
||||
// Build adjacency list
|
||||
let mut adj = vec![Vec::new(); n];
|
||||
for &(u, v) in &self.edges {
|
||||
adj[u].push(v);
|
||||
adj[v].push(u); // remove if directed
|
||||
}
|
||||
|
||||
let mut visited = vec![false; n];
|
||||
let mut queue = VecDeque::new();
|
||||
let mut result = Vec::new();
|
||||
|
||||
queue.push_back((0, 0));
|
||||
visited[0] = true;
|
||||
|
||||
while let Some((node, depth)) = queue.pop_front() {
|
||||
result.push((depth, self.genes[node].place.clone()));
|
||||
|
||||
for &next in &adj[node] {
|
||||
if !visited[next] {
|
||||
visited[next] = true;
|
||||
queue.push_back((next, depth + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
};
|
||||
|
||||
for (depth,name) in indents {
|
||||
let indent = "____".repeat(depth);
|
||||
writeln!(f,"{}{}",indent,name)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PlantCreationError {
|
||||
EmptyNodes,
|
||||
|
|
|
|||
27
rust-pvz-genetics/src/genetics/manipulations.rs
Normal file
27
rust-pvz-genetics/src/genetics/manipulations.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use std::mem::swap;
|
||||
|
||||
use rand::random_range;
|
||||
|
||||
use crate::genetics::genome::*;
|
||||
|
||||
pub struct GenomeModificator<'a>(&'a mut PlantGenome, &'a mut PlantGenome);
|
||||
|
||||
impl<'a> GenomeModificator<'a> {
|
||||
pub fn new(genome_a: &'a mut PlantGenome,genome_b: &'a mut PlantGenome) -> Self {
|
||||
return Self(genome_a,genome_b);
|
||||
}
|
||||
pub fn allelic_crossingover(&mut self,chance: Option<u8>) {
|
||||
let amount: usize = *[self.0.genes.len(),self.1.genes.len()].iter().min().unwrap_or(&0);
|
||||
|
||||
for i in 0..amount {
|
||||
let computed = random_range(1..chance.unwrap_or(2).max(2));
|
||||
if computed != 1 {continue;}
|
||||
swap(&mut self.0.genes[i], &mut self.1.genes[i]);
|
||||
}
|
||||
}
|
||||
/*pub fn categoric_crossingover(&mut self,chance: Option<u8>) {
|
||||
helper(self.0.genes.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::<Vec<(String,&mut Gene)>>().append(&mut self.1.genes.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::<Vec<(String,&mut Gene)>>()),chance);
|
||||
}
|
||||
fn helper(test: HashMap<String,Vec<&mut Gene>>,chance: Option<u8>) {
|
||||
}*/
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ pub mod plant_templates;
|
|||
pub mod gene;
|
||||
pub mod genome;
|
||||
pub mod arguments;
|
||||
pub mod manipulations;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::genetics::genome::*;
|
||||
|
|
|
|||
5
rust-pvz-genetics/src/main.rs
Normal file
5
rust-pvz-genetics/src/main.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use crate::genetics::plant_templates::peashooter_template;
|
||||
|
||||
fn main() {
|
||||
let peashooter = peashooter_template();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue