pub struct GraphNode<T> { /* private fields */ }Expand description
A node in a graph structure that represents a single element with connections to other nodes.
The GraphNode struct is a fundamental building block for graph-based genetic programming in Radiate. It represents a node in a directed graph that can have both incoming and outgoing connections to other nodes. Each node has a unique identifier, an index in the graph, a value of type T, and maintains sets of incoming and outgoing connections.
§Type Parameters
T- The type of value stored in the node. This type must implementClone,PartialEq, and other traits required by the genetic programming operations.
§Fields
value- The actual value stored in the nodeid- A unique identifier for the node (GraphNodeId)index- The position of the node in the graph’s node collectiondirection- The direction of the node’s connections (Forward or Backward)node_type- Optional NodeType that specifies the role of the node (Input, Output, Vertex, Edge, etc.)arity- Optional Arity that specifies how many incoming connections the node can have. If the arity is not supplied, the node will try it’s best to determine it based on the node type and the number of connections.incoming- Set of indices of nodes that have connections to this nodeoutgoing- Set of indices of nodes that this node has connections to
§Examples
use radiate_gp::{collections::{GraphNode, NodeType}, Arity};
// Create a new input node with value 42
let node = GraphNode::new(0, NodeType::Input, 42);
// Create a node with specific arity
// This node will be invalid if it has a number of incoming connections other than 2
let node_with_arity = GraphNode::with_arity(1, NodeType::Vertex, 42, Arity::Exact(2));§Node Types and Arity
The node’s type and arity determine its behavior and validity:
Inputnodes should have no incoming connections and at least one outgoing connectionOutputnodes should have at least one incoming connectionVertexnodes can have both incoming and outgoing connectionsEdgenodes should have exactly one incoming and one outgoing connection
§Recurrent Connections
Nodes can form recurrent connections (cycles) in the graph by:
- Setting the node’s direction to
Direction::Backward - Having a connection to itself (index in incoming/outgoing sets)
§Validity
A node is considered valid based on its type and connections:
Inputnodes are valid when they have no incoming connections and at least one outgoing connectionOutputnodes are valid when they have at least one incoming connectionVertexnodes are valid when they have both incoming and outgoing connectionsEdgenodes are valid when they have exactly one incoming and one outgoing connection
§Implementation Details
The struct implements several traits:
Node- Provides common node behavior and access to value and type informationGene- Enables genetic operations for the node making it compatible with genetic algorithmsValid- Defines validity rules for the nodeDebug- Provides debug formattingClone,PartialEq- Required for genetic programming operations
§Serialization
When the “serde” feature is enabled, the struct implements Serialize and Deserialize traits.
Implementations§
Source§impl<T> GraphNode<T>
impl<T> GraphNode<T>
Sourcepub fn new(index: usize, node_type: NodeType, value: T) -> Self
pub fn new(index: usize, node_type: NodeType, value: T) -> Self
Creates a new GraphNode with the specified index, node type, and value.
This is the most basic constructor for a graph node, initializing it with default direction (Forward) and no specific arity or node type.
Sourcepub fn with_arity(
index: usize,
node_type: NodeType,
value: T,
arity: Arity,
) -> Self
pub fn with_arity( index: usize, node_type: NodeType, value: T, arity: Arity, ) -> Self
Creates a new GraphNode with the specified index, node type, value, and arity.
This constructor allows for more control over the node’s behavior by specifying the arity, which defines how many incoming connections the node can accept - if the number of connections does not match the arity, the node will be considered invalid.
pub fn with_incoming<I: IntoIterator<Item = usize>>(self, incoming: I) -> Self
pub fn with_outgoing<O: IntoIterator<Item = usize>>(self, outgoing: O) -> Self
pub fn direction(&self) -> Direction
pub fn set_direction(&mut self, direction: Direction)
pub fn innovation(&self) -> Option<InnovationId>
pub fn set_innovation(&mut self, innovation: Option<InnovationId>)
pub fn index(&self) -> usize
pub fn id(&self) -> &GraphNodeId
pub fn is_recurrent(&self) -> bool
pub fn incoming(&self) -> &[usize]
pub fn outgoing(&self) -> &[usize]
pub fn incoming_mut(&mut self) -> &mut [usize]
pub fn outgoing_mut(&mut self) -> &mut [usize]
pub fn is_locked(&self) -> bool
pub fn insert_incoming(&mut self, value: usize)
pub fn remove_incoming(&mut self, value: &usize)
pub fn insert_outgoing(&mut self, value: usize)
pub fn remove_outgoing(&mut self, value: &usize)
Trait Implementations§
Source§impl<T> FromIterator<GraphNode<T>> for GraphChromosome<T>
impl<T> FromIterator<GraphNode<T>> for GraphChromosome<T>
Source§impl<T> FromIterator<GraphNode<T>> for Graph<T>
impl<T> FromIterator<GraphNode<T>> for Graph<T>
Source§impl<T> Gene for GraphNode<T>
impl<T> Gene for GraphNode<T>
type Allele = T
Source§fn allele_mut(&mut self) -> &mut Self::Allele
fn allele_mut(&mut self) -> &mut Self::Allele
allele of the Gene.Source§fn new_instance(&self) -> GraphNode<T>
fn new_instance(&self) -> GraphNode<T>
Source§fn with_allele(&self, allele: &Self::Allele) -> GraphNode<T>
fn with_allele(&self, allele: &Self::Allele) -> GraphNode<T>
allele.Source§fn set_allele(&mut self, allele: Self::Allele)
fn set_allele(&mut self, allele: Self::Allele)
allele of the Gene to the given value.Source§impl<T> Node for GraphNode<T>
impl<T> Node for GraphNode<T>
type Value = T
Source§fn node_type(&self) -> NodeType
fn node_type(&self) -> NodeType
GraphNode with 0 incoming connections is likely an Input,
while a TreeNode with 0 children is likely a Leaf.Source§fn arity(&self) -> Arity
fn arity(&self) -> Arity
impl<T: PartialEq> StructuralPartialEq for GraphNode<T>
Source§impl<T> Valid for GraphNode<T>
impl<T> Valid for GraphNode<T>
A node is considered valid based on its type and connections:
Inputnodes are valid when they have no incoming connections and at least one outgoing connectionOutputnodes are valid when they have at least one incoming connectionVertexnodes are valid when they have both incoming and outgoing connectionsEdgenodes are valid when they have exactly one incoming and one outgoing connection