Genome graph rewritten to more straightforward approach
This commit is contained in:
parent
4ed86fda31
commit
b4583ff917
4 changed files with 26 additions and 37 deletions
|
|
@ -7,6 +7,6 @@ edition = "2024"
|
||||||
godot = "0.4.5"
|
godot = "0.4.5"
|
||||||
rand = "0.10.0"
|
rand = "0.10.0"
|
||||||
|
|
||||||
[lib]
|
#[lib]
|
||||||
crate-type = ["cdylib"]
|
#crate-type = ["cdylib"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,13 @@ use std::{collections::VecDeque, fmt::Display};
|
||||||
use crate::genetics::gene::Gene;
|
use crate::genetics::gene::Gene;
|
||||||
|
|
||||||
pub struct PlantGenome {
|
pub struct PlantGenome {
|
||||||
pub(crate) genes: Vec<Gene>,
|
pub(crate) graph: Vec<(Gene,Vec<usize>)>,
|
||||||
pub(crate) edges: Vec<(usize,usize)>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlantGenome {
|
impl PlantGenome {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
genes: Vec::new(),
|
graph: Vec::new()
|
||||||
edges: Vec::new()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn from_edges(nodes: Vec<Gene>,edges: Vec<(usize,usize)>) -> Result<Self,PlantCreationError> {
|
pub fn from_edges(nodes: Vec<Gene>,edges: Vec<(usize,usize)>) -> Result<Self,PlantCreationError> {
|
||||||
|
|
@ -23,9 +21,13 @@ impl PlantGenome {
|
||||||
return Err(PlantCreationError::EmptyEdges);
|
return Err(PlantCreationError::EmptyEdges);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut graph: Vec<(Gene,Vec<usize>)>= nodes.iter().map(|gene|{(gene.clone(),Vec::new())}).collect();
|
||||||
|
for edge in edges {
|
||||||
|
graph[edge.0].1.push(edge.1);
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(Self {
|
return Ok(Self {
|
||||||
genes: nodes,
|
graph
|
||||||
edges
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -33,31 +35,13 @@ impl PlantGenome {
|
||||||
impl Display for PlantGenome {
|
impl Display for PlantGenome {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let indents = {
|
let indents = {
|
||||||
// Vibe code
|
let mut result: Vec<(usize,Gene)> = vec![(0,self.graph[0].0.clone())];
|
||||||
let n = self.genes.len();
|
let mut tree: VecDeque<(usize,&Vec<usize>)> = VecDeque::from([(0,&self.graph[0].1)]);
|
||||||
|
while tree.is_empty() == false {
|
||||||
// Build adjacency list
|
let (depth,children) = tree.pop_front().unwrap();
|
||||||
let mut adj = vec![Vec::new(); n];
|
for child in children {
|
||||||
for &(u, v) in &self.edges {
|
result.push((depth+1,self.graph[*child].0.clone()));
|
||||||
adj[u].push(v);
|
tree.push_back((depth+1,&self.graph[*child].1));
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,7 +49,7 @@ impl Display for PlantGenome {
|
||||||
};
|
};
|
||||||
|
|
||||||
for (depth,gene) in indents {
|
for (depth,gene) in indents {
|
||||||
let indent = "____".repeat(depth);
|
let indent = "\t".repeat(depth);
|
||||||
writeln!(f,"{}{}",indent,gene)?;
|
writeln!(f,"{}{}",indent,gene)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,16 +12,16 @@ impl<'a> GenomeModificator<'a> {
|
||||||
}
|
}
|
||||||
pub fn allelic_crossingover(&mut self,chance: Option<u8>) {
|
pub fn allelic_crossingover(&mut self,chance: Option<u8>) {
|
||||||
let mut generator = rand::rng();
|
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 {
|
for i in 0..amount {
|
||||||
let computed = generator.random_range(0..chance.unwrap_or(2).max(2));
|
let computed = generator.random_range(0..chance.unwrap_or(2).max(2));
|
||||||
if computed != 0 {continue;}
|
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<u8>) {
|
/*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);
|
helper(self.0.graph.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::<Vec<(String,&mut Gene)>>().append(&mut self.1.graph.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>) {
|
fn helper(test: HashMap<String,Vec<&mut Gene>>,chance: Option<u8>) {
|
||||||
}*/
|
}*/
|
||||||
|
|
|
||||||
5
rust-pvz-genetics/src/main.rs
Normal file
5
rust-pvz-genetics/src/main.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
use rust_pvz_genetics::genetics::plant_templates::peashooter_template;
|
||||||
|
fn main() {
|
||||||
|
let peashooter = peashooter_template();
|
||||||
|
println!("{}",peashooter);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue