Skip to main content

GraphNode

Struct GraphNode 

Source
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 implement Clone, PartialEq, and other traits required by the genetic programming operations.

§Fields

  • value - The actual value stored in the node
  • id - A unique identifier for the node (GraphNodeId)
  • index - The position of the node in the graph’s node collection
  • direction - 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 node
  • outgoing - 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:

  • Input nodes should have no incoming connections and at least one outgoing connection
  • Output nodes should have at least one incoming connection
  • Vertex nodes can have both incoming and outgoing connections
  • Edge nodes 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:

  • Input nodes are valid when they have no incoming connections and at least one outgoing connection
  • Output nodes are valid when they have at least one incoming connection
  • Vertex nodes are valid when they have both incoming and outgoing connections
  • Edge nodes 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 information
  • Gene - Enables genetic operations for the node making it compatible with genetic algorithms
  • Valid - Defines validity rules for the node
  • Debug - Provides debug formatting
  • Clone, 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>

Source

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.

Source

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.

Source

pub fn with_incoming<I: IntoIterator<Item = usize>>(self, incoming: I) -> Self

Source

pub fn with_outgoing<O: IntoIterator<Item = usize>>(self, outgoing: O) -> Self

Source

pub fn direction(&self) -> Direction

Source

pub fn set_direction(&mut self, direction: Direction)

Source

pub fn index(&self) -> usize

Source

pub fn id(&self) -> &GraphNodeId

Source

pub fn is_recurrent(&self) -> bool

Source

pub fn incoming(&self) -> &[usize]

Source

pub fn outgoing(&self) -> &[usize]

Source

pub fn incoming_mut(&mut self) -> &mut [usize]

Source

pub fn outgoing_mut(&mut self) -> &mut [usize]

Source

pub fn is_locked(&self) -> bool

Source

pub fn insert_incoming(&mut self, value: usize)

Source

pub fn remove_incoming(&mut self, value: &usize)

Source

pub fn insert_outgoing(&mut self, value: usize)

Source

pub fn remove_outgoing(&mut self, value: &usize)

Trait Implementations§

Source§

impl<T: Clone> Clone for GraphNode<T>

Source§

fn clone(&self) -> GraphNode<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for GraphNode<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for GraphNode<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, V> Eval<[V], V> for GraphNode<T>
where T: Eval<[V], V>, V: Copy,

Source§

fn eval(&self, inputs: &[V]) -> V

Evaluates the GraphNode with the given input. Returns the output of the GraphNode.

§Arguments
  • inputs - A Vec of V to evaluate the GraphNode with.
§Returns
Source§

impl<T> From<(usize, NodeType, T)> for GraphNode<T>

Source§

fn from((index, node_type, value): (usize, NodeType, T)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<(usize, NodeType, T, Arity)> for GraphNode<T>

Source§

fn from((index, node_type, value, arity): (usize, NodeType, T, Arity)) -> Self

Converts to this type from the input type.
Source§

impl<T, I> From<(usize, NodeType, T, I, I)> for GraphNode<T>
where I: Into<SortedBuffer<usize>>,

Source§

fn from( (index, node_type, value, incoming, outgoing): (usize, NodeType, T, I, I), ) -> Self

Converts to this type from the input type.
Source§

impl<T: Default> From<(usize, T)> for GraphNode<T>

Source§

fn from((index, value): (usize, T)) -> Self

Converts to this type from the input type.
Source§

impl<T: Default> From<(usize, T, Arity)> for GraphNode<T>

Source§

fn from((index, value, arity): (usize, T, Arity)) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<GraphNode<T>> for Graph<T>

Source§

fn from_iter<I: IntoIterator<Item = GraphNode<T>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> FromIterator<GraphNode<T>> for GraphChromosome<T>

Source§

fn from_iter<I: IntoIterator<Item = GraphNode<T>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Gene for GraphNode<T>
where T: Clone + PartialEq,

Source§

type Allele = T

Source§

fn allele(&self) -> &Self::Allele

Get the allele of the Gene. This is the value that the Gene represents or “expresses”.
Source§

fn allele_mut(&mut self) -> &mut Self::Allele

Get a mutable reference to the allele of the Gene.
Source§

fn new_instance(&self) -> GraphNode<T>

Create a new instance of the Gene.
Source§

fn with_allele(&self, allele: &Self::Allele) -> GraphNode<T>

Create a new Gene with the given allele.
Source§

impl<T: Hash> Hash for GraphNode<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
where Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Node for GraphNode<T>

Implementing the Node trait for GraphNode This joins common functionality for nodes in a graph structure together.

Source§

type Value = T

Source§

fn value(&self) -> &Self::Value

Get a reference to the node’s value.
Source§

fn value_mut(&mut self) -> &mut Self::Value

Get a mutable reference to the node’s value.
Source§

fn node_type(&self) -> NodeType

Get the NodeType of the node. As previously mentioned, if the NodeType is not supplied during creation, this value is determined by the node’s relationship to the rest of the structure holding it. IE, a GraphNode with 0 incoming connections is likely an Input, while a TreeNode with 0 children is likely a Leaf.
Source§

fn arity(&self) -> Arity

Get the arity of the node, which is the number of incoming connections it can have. In a genetic programming sense, this is the number of allowed inputs for a node. In a Graph, this is the number of allowed incoming connections while for a Tree, this is the number of children it is allowed to have.
Source§

impl<T: PartialEq> PartialEq for GraphNode<T>

Source§

fn eq(&self, other: &GraphNode<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Valid for GraphNode<T>

Implementing the Valid trait for GraphNode This trait checks if the node is valid based on its type and connections. A valid node must have the correct number of incoming and outgoing connections according to its arity and node type.

A node is considered valid based on its type and connections:

  • Input nodes are valid when they have no incoming connections and at least one outgoing connection
  • Output nodes are valid when they have at least one incoming connection
  • Vertex nodes are valid when they have both incoming and outgoing connections
  • Edge nodes are valid when they have exactly one incoming and one outgoing connection
Source§

fn is_valid(&self) -> bool

Source§

impl<T> StructuralPartialEq for GraphNode<T>

Auto Trait Implementations§

§

impl<T> Freeze for GraphNode<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for GraphNode<T>
where T: RefUnwindSafe,

§

impl<T> Send for GraphNode<T>
where T: Send,

§

impl<T> Sync for GraphNode<T>
where T: Sync,

§

impl<T> Unpin for GraphNode<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for GraphNode<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for GraphNode<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<I, O, T> EvalMut<I, O> for T
where T: Eval<I, O>, I: ?Sized,

Source§

fn eval_mut(&mut self, input: &I) -> O

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<N> NodeExt for N
where N: Node,

Source§

fn set_value(&mut self, value: Self::Value)

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<G, T> NumericGene for G
where G: Gene<Allele = T>, T: NumericAllele,