Tests
This commit is contained in:
parent
b4583ff917
commit
25c04be661
5 changed files with 59 additions and 16 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"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use std::{collections::VecDeque, fmt::Display};
|
||||||
|
|
||||||
use crate::genetics::gene::Gene;
|
use crate::genetics::gene::Gene;
|
||||||
|
|
||||||
|
#[derive(PartialEq,Eq)]
|
||||||
pub struct PlantGenome {
|
pub struct PlantGenome {
|
||||||
pub(crate) graph: Vec<(Gene,Vec<usize>)>,
|
pub(crate) graph: Vec<(Gene,Vec<usize>)>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,40 @@
|
||||||
use std::mem::swap;
|
use std::mem::swap;
|
||||||
|
|
||||||
use rand::RngExt;
|
use rand::{RngExt, SeedableRng, rngs::StdRng};
|
||||||
|
|
||||||
use crate::genetics::genome::*;
|
use crate::genetics::genome::*;
|
||||||
|
|
||||||
pub struct GenomeModificator<'a>(&'a mut PlantGenome, &'a mut PlantGenome);
|
pub struct GenomeModificator<'a> {
|
||||||
|
genome_a: &'a mut PlantGenome,
|
||||||
|
genome_b: &'a mut PlantGenome,
|
||||||
|
generator: StdRng
|
||||||
|
}
|
||||||
impl<'a> GenomeModificator<'a> {
|
impl<'a> GenomeModificator<'a> {
|
||||||
pub fn new(genome_a: &'a mut PlantGenome,genome_b: &'a mut PlantGenome) -> Self {
|
pub fn new(genome_a: &'a mut PlantGenome,genome_b: &'a mut PlantGenome) -> Self {
|
||||||
return Self(genome_a,genome_b);
|
return Self {
|
||||||
|
genome_a,
|
||||||
|
genome_b,
|
||||||
|
generator: rand::make_rng(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn with_seed(genome_a: &'a mut PlantGenome, genome_b: &'a mut PlantGenome, seed: u64) -> Self {
|
||||||
|
return Self {
|
||||||
|
genome_a,
|
||||||
|
genome_b,
|
||||||
|
generator: StdRng::seed_from_u64(seed)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
pub fn allelic_crossingover(&mut self,chance: Option<u8>) {
|
pub fn allelic_crossingover(&mut self,chance: Option<u8>) {
|
||||||
let mut generator = rand::rng();
|
let amount: usize = *[self.genome_a.graph.len(),self.genome_b.graph.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 = self.generator.random_range(0..chance.unwrap_or(2).max(2));
|
||||||
if computed != 0 {continue;}
|
if computed != 0 {continue;}
|
||||||
swap(&mut self.0.graph[i].0, &mut self.1.graph[i].0);
|
swap(&mut self.genome_a.graph[i].0, &mut self.genome_b.graph[i].0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*pub fn categoric_crossingover(&mut self,chance: Option<u8>) {
|
/*pub fn categoric_crossingover(&mut self,chance: Option<u8>) {
|
||||||
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);
|
helper(self.genome_a.graph.iter().map(|mut gene|(gene.place.clone(),&mut gene)).collect::<Vec<(String,&mut Gene)>>().append(&mut self.genome_b.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>) {
|
||||||
}*/
|
}*/
|
||||||
|
|
|
||||||
|
|
@ -1 +1,35 @@
|
||||||
|
use crate::genetics::{flow::Flow, gene::{Gene, GeneType}, genome::PlantGenome, manipulations::GenomeModificator, plant_templates::{peashooter_template, sunflower_template}};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_display() {
|
||||||
|
assert!(peashooter_template().to_string() == "(Peashooter) root\n\t(Peashooter) stem\n\t\t(Peashooter) head\n\t\t\t(Peashooter) leaf\n\t\t\t(Peashooter) face\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_allelic_crossingover() {
|
||||||
|
let mut peashooter = peashooter_template();
|
||||||
|
let mut sunflower = sunflower_template();
|
||||||
|
GenomeModificator::with_seed(&mut peashooter, &mut sunflower,3996684975687038250u64).allelic_crossingover(None);
|
||||||
|
assert!(peashooter == PlantGenome::from_edges(vec![
|
||||||
|
Gene::new("Sunflower","root", GeneType::Dummy),
|
||||||
|
Gene::new("Peashooter","stem", GeneType::fill_all()),
|
||||||
|
Gene::new("Peashooter","head",GeneType::random_distribution()),
|
||||||
|
Gene::new("Sunflower","face", GeneType::consumer(|_|{})),
|
||||||
|
Gene::new("Peashooter","face",GeneType::consumer(|_|{}))
|
||||||
|
],vec![
|
||||||
|
(0,1),
|
||||||
|
(1,2),
|
||||||
|
(2,3),
|
||||||
|
(2,4)
|
||||||
|
]).unwrap());
|
||||||
|
assert!(sunflower == PlantGenome::from_edges(vec![
|
||||||
|
Gene::new("Peashooter","root", GeneType::pure_producer(|_|{Flow::empty()})),
|
||||||
|
Gene::new("Sunflower","stem", GeneType::Dummy),
|
||||||
|
Gene::new("Sunflower","head", GeneType::modifier(|_|{None})),
|
||||||
|
Gene::new("Peashooter","leaf",GeneType::modifier(|_|{None})),
|
||||||
|
], vec![
|
||||||
|
(0,1),
|
||||||
|
(1,2),
|
||||||
|
(2,3),
|
||||||
|
]).unwrap());
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
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