From b4583ff917566042cde8992a8c9f026624ba83a1 Mon Sep 17 00:00:00 2001 From: rendo Date: Mon, 30 Mar 2026 12:17:29 +0500 Subject: [PATCH] Genome graph rewritten to more straightforward approach --- rust-pvz-genetics/Cargo.toml | 4 +- rust-pvz-genetics/src/genetics/genome.rs | 48 +++++++------------ .../src/genetics/manipulations.rs | 6 +-- rust-pvz-genetics/src/main.rs | 5 ++ 4 files changed, 26 insertions(+), 37 deletions(-) create mode 100644 rust-pvz-genetics/src/main.rs diff --git a/rust-pvz-genetics/Cargo.toml b/rust-pvz-genetics/Cargo.toml index 0462e89..a3dcad9 100644 --- a/rust-pvz-genetics/Cargo.toml +++ b/rust-pvz-genetics/Cargo.toml @@ -7,6 +7,6 @@ edition = "2024" godot = "0.4.5" rand = "0.10.0" -[lib] -crate-type = ["cdylib"] +#[lib] +#crate-type = ["cdylib"] diff --git a/rust-pvz-genetics/src/genetics/genome.rs b/rust-pvz-genetics/src/genetics/genome.rs index 4e433a5..5f72b05 100644 --- a/rust-pvz-genetics/src/genetics/genome.rs +++ b/rust-pvz-genetics/src/genetics/genome.rs @@ -3,15 +3,13 @@ use std::{collections::VecDeque, fmt::Display}; use crate::genetics::gene::Gene; pub struct PlantGenome { - pub(crate) genes: Vec, - pub(crate) edges: Vec<(usize,usize)> + pub(crate) graph: Vec<(Gene,Vec)>, } impl PlantGenome { pub fn new() -> Self { Self { - genes: Vec::new(), - edges: Vec::new() + graph: Vec::new() } } pub fn from_edges(nodes: Vec,edges: Vec<(usize,usize)>) -> Result { @@ -23,9 +21,13 @@ impl PlantGenome { return Err(PlantCreationError::EmptyEdges); } + let mut graph: Vec<(Gene,Vec)>= nodes.iter().map(|gene|{(gene.clone(),Vec::new())}).collect(); + for edge in edges { + graph[edge.0].1.push(edge.1); + } + return Ok(Self { - genes: nodes, - edges + graph }); } } @@ -33,31 +35,13 @@ 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].clone())); - - for &next in &adj[node] { - if !visited[next] { - visited[next] = true; - queue.push_back((next, depth + 1)); - } + let mut result: Vec<(usize,Gene)> = vec![(0,self.graph[0].0.clone())]; + let mut tree: VecDeque<(usize,&Vec)> = VecDeque::from([(0,&self.graph[0].1)]); + while tree.is_empty() == false { + let (depth,children) = tree.pop_front().unwrap(); + for child in children { + result.push((depth+1,self.graph[*child].0.clone())); + tree.push_back((depth+1,&self.graph[*child].1)); } } @@ -65,7 +49,7 @@ impl Display for PlantGenome { }; for (depth,gene) in indents { - let indent = "____".repeat(depth); + let indent = "\t".repeat(depth); writeln!(f,"{}{}",indent,gene)?; } diff --git a/rust-pvz-genetics/src/genetics/manipulations.rs b/rust-pvz-genetics/src/genetics/manipulations.rs index d8f7717..e7ab30a 100644 --- a/rust-pvz-genetics/src/genetics/manipulations.rs +++ b/rust-pvz-genetics/src/genetics/manipulations.rs @@ -12,16 +12,16 @@ impl<'a> GenomeModificator<'a> { } pub fn allelic_crossingover(&mut self,chance: Option) { let mut generator = rand::rng(); - let amount: usize = *[self.0.genes.len(),self.1.genes.len()].iter().min().unwrap_or(&0); + let amount: usize = *[self.0.graph.len(),self.1.graph.len()].iter().min().unwrap_or(&0); for i in 0..amount { let computed = generator.random_range(0..chance.unwrap_or(2).max(2)); if computed != 0 {continue;} - swap(&mut self.0.genes[i], &mut self.1.genes[i]); + swap(&mut self.0.graph[i].0, &mut self.1.graph[i].0); } } /*pub fn categoric_crossingover(&mut self,chance: Option) { - helper(self.0.genes.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::>().append(&mut self.1.genes.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::>()),chance); + helper(self.0.graph.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::>().append(&mut self.1.graph.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::>()),chance); } fn helper(test: HashMap>,chance: Option) { }*/ diff --git a/rust-pvz-genetics/src/main.rs b/rust-pvz-genetics/src/main.rs new file mode 100644 index 0000000..747e2cd --- /dev/null +++ b/rust-pvz-genetics/src/main.rs @@ -0,0 +1,5 @@ +use rust_pvz_genetics::genetics::plant_templates::peashooter_template; +fn main() { + let peashooter = peashooter_template(); + println!("{}",peashooter); +}