Switched to petgraph as graph representation. Initialization doesn't work yet
This commit is contained in:
parent
b36038696e
commit
0eaa9d04b8
4 changed files with 103 additions and 89 deletions
|
|
@ -1,16 +1,16 @@
|
|||
use std::fmt::Display;
|
||||
use petgraph::graph::{Graph, NodeIndex};
|
||||
|
||||
use crate::genetics::gene::Gene;
|
||||
|
||||
#[derive(Default,Clone,PartialEq,Eq)]
|
||||
#[derive(Default,Clone)]
|
||||
pub struct Genome {
|
||||
pub(crate) graph: Vec<(Gene,Vec<usize>)>,
|
||||
pub(crate) graph: Graph<Gene,()>,
|
||||
}
|
||||
|
||||
impl Genome {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
graph: Vec::new()
|
||||
graph: Graph::<Gene,()>::new()
|
||||
}
|
||||
}
|
||||
pub fn from_edges(nodes: Vec<Gene>,edges: Vec<(usize,usize)>) -> Result<Self,PlantCreationError> {
|
||||
|
|
@ -22,13 +22,9 @@ impl Genome {
|
|||
return Err(PlantCreationError::EmptyEdges);
|
||||
}
|
||||
|
||||
let mut graph: Vec<(Gene,Vec<usize>)> = Vec::new();
|
||||
for gene in nodes {
|
||||
graph.push((gene,Vec::new()));
|
||||
}
|
||||
for edge in edges {
|
||||
graph[edge.0].1.push(edge.1);
|
||||
}
|
||||
let mut graph = Graph::<Gene,()>::new();
|
||||
let gene_indicies = nodes.into_iter().map(|gene|{graph.add_node(gene)}).collect::<Vec<NodeIndex>>();
|
||||
edges.into_iter().map(|(node_a,node_b)|graph.add_edge(gene_indicies[node_a], gene_indicies[node_b], ()));
|
||||
|
||||
return Ok(Self {
|
||||
graph
|
||||
|
|
@ -36,27 +32,34 @@ impl Genome {
|
|||
}
|
||||
}
|
||||
|
||||
// Vibecoded
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
impl Display for Genome {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let indents = {
|
||||
let mut result: Vec<(usize, Gene)> = Vec::new();
|
||||
let mut stack: Vec<(usize, usize)> = vec![(0, 0)]; // (node_index, depth)
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let graph = &self.graph;
|
||||
|
||||
while let Some((node, depth)) = stack.pop() {
|
||||
result.push((depth, self.graph[node].0.clone()));
|
||||
if graph.node_count() == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// push in reverse so left-most child is processed first
|
||||
for &child in self.graph[node].1.iter().rev() {
|
||||
stack.push((child, depth + 1));
|
||||
}
|
||||
}
|
||||
let root = NodeIndex::new(0);
|
||||
let mut stack: Vec<(NodeIndex, usize)> = vec![(root, 0)];
|
||||
|
||||
result
|
||||
};
|
||||
|
||||
for (depth,gene) in indents {
|
||||
while let Some((node, depth)) = stack.pop() {
|
||||
let indent = "\t".repeat(depth);
|
||||
writeln!(f,"{}{}",indent,gene)?;
|
||||
writeln!(f, "{}{}", indent, graph[node])?;
|
||||
|
||||
// push in reverse to preserve child order
|
||||
let mut children: Vec<NodeIndex> = graph
|
||||
.neighbors(node)
|
||||
.filter(|&n| n != node) // skip self-loops
|
||||
.collect();
|
||||
|
||||
children.reverse();
|
||||
for child in children {
|
||||
stack.push((child, depth + 1));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
@ -68,3 +71,4 @@ pub enum PlantCreationError {
|
|||
EmptyNodes,
|
||||
EmptyEdges,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue