Struct Graph

Source
pub struct Graph<T> { /* private fields */ }
Expand description

A ‘Graph’ is simply a ‘Vec’ of ’GraphNode’s.

Its important to note that this graph differs from a traditional graph in that it is not a collection of edges and vertices. Instead, it is a collection of nodes that are connected to one another. Each node has a unique index that is used to reference it in the graph and must be identical to its position in the ‘Vec’. Each ‘GraphNode’ has a set of ordered incoming and outgoing connections. These connections are represented by the index of the connected node in the graph. Because of this representation, an edge is not a separate entity, its just a node. The ‘NodeType’ enum is used to distinguish different types of nodes. This allows for a more flexible representation of the graph while still maintaining the ability to represent traditional graphs.

By default, a ‘Graph’ is a directed acyclic graph (DAG). However, it is possible to create cycles in the graph by setting the ‘direction’ field of a ‘GraphNode’ to ‘Direction::Backward’. The ‘Graph’ struct provides methods for attaching and detaching nodes from one another. It also provides methods for iterating over the nodes in the graph in a sudo topological order.

Implementations§

Source§

impl<T: Clone + Default> Graph<T>

Source

pub fn directed( input_size: usize, output_size: usize, values: impl Into<NodeStore<T>>, ) -> Graph<T>

Creates a directed graph with the given input and output sizes. The values are used to initialize the nodes in the graph with the given values.

§Example
use radiate::*;
use radiate_gp::*;

let values = vec![
    (NodeType::Input, vec![Op::var(0), Op::var(1), Op::var(2)]),
    (NodeType::Output, vec![Op::sigmoid()]),
];

let graph = Graph::directed(3, 3, values);

assert_eq!(graph.len(), 6);

The graph will have 6 nodes, 3 input nodes and 3 output nodes where each input node is connected to each output node. Such as:

[0, 1, 2] -> [3, 4, 5]
§Arguments
  • input_size - The number of input nodes.
  • output_size - The number of output nodes.
  • values - The values to initialize the nodes with.
§Returns

A new directed graph.

Source

pub fn recurrent( input_size: usize, output_size: usize, values: impl Into<NodeStore<T>>, ) -> Graph<T>

Creates a recurrent graph with the given input and output sizes. The values are used to initialize the nodes in the graph with the given values. The graph will have a recurrent connection from each hidden vertex to itself. The graph will have a one-to-one connection from each input node to each hidden vertex. The graph will have an all-to-all connection from each hidden vertex to each output node.

§Example
use radiate::*;
use radiate_gp::*;

let values = vec![
  (NodeType::Input, vec![Op::var(0), Op::var(1), Op::var(2)]),
  (NodeType::Vertex, vec![Op::linear()]),
  (NodeType::Output, vec![Op::sigmoid()]),
];

let graph = Graph::recurrent(3, 3, values);

assert_eq!(graph.len(), 12);

The graph will have 12 nodes, 3 input nodes, 3 hidden nodes with recurrent connections to themselves, and 3 output nodes. Such as:

[0, 1, 2] -> [3, 4, 5]
    [3, 4, 5] -> [6, 7, 8]
        [6, 7, 8] -> [3, 4, 5]
[3, 4, 5] -> [9, 10, 11]
§Arguments
  • input_size - The number of input nodes.
  • output_size - The number of output nodes.
  • values - The values to initialize the nodes with.
§Returns

A new recurrent graph.

Source

pub fn weighted_directed( input_size: usize, output_size: usize, values: impl Into<NodeStore<T>>, ) -> Graph<T>

Creates a weighted directed graph with the given input and output sizes.

This will result in the same graph as Graph::directed but with an additional edge connecting each input node to each output node.

§Arguments
  • input_size - The number of input nodes.
  • output_size - The number of output nodes.
§Returns

A new weighted directed graph.

Source

pub fn weighted_recurrent( input_size: usize, output_size: usize, values: impl Into<NodeStore<T>>, ) -> Graph<T>

Creates a weighted recurrent graph with the given input and output sizes. This will result in the same graph as Graph::recurrent but with an additional edge connecting each hidden vertex to each output node.

§Arguments
  • input_size - The number of input nodes.
  • output_size - The number of output nodes.
§Returns

A new weighted recurrent graph.

Source§

impl<T> Graph<T>

The ‘Graph’ struct provides methods for creating, modifying, and iterating over a graph.

Source

pub fn new(nodes: Vec<GraphNode<T>>) -> Self

Create a new ‘Graph’ from a ‘Vec’ of ’GraphNode’s.

§Arguments
  • nodes: A ‘Vec’ of ’GraphNode’s.
Source

pub fn push(&mut self, node: impl Into<GraphNode<T>>)

Push a ‘GraphNode’ onto the last position in the graph.

Source

pub fn insert(&mut self, node_type: NodeType, val: T) -> usize

Source

pub fn pop(&mut self) -> Option<GraphNode<T>>

Pop the last ‘GraphNode’ from the graph.

Source

pub fn len(&self) -> usize

Returns the number of nodes in the graph.

Source

pub fn is_empty(&self) -> bool

Returns true if the graph is empty.

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut GraphNode<T>>

Returns a mutable reference to the node at the specified index.

Source

pub fn get(&self, index: usize) -> Option<&GraphNode<T>>

Returns a reference to the node at the specified index.

Source

pub fn iter(&self) -> impl Iterator<Item = &GraphNode<T>>

iterates over the nodes in the graph. The nodes are returned in the order they were added, so there is no real order to this iterator.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut GraphNode<T>>

mutably iterates over the nodes in the graph. The nodes are returned in the order they

Source

pub fn attach(&mut self, incoming: usize, outgoing: usize) -> &mut Self

Attach and detach nodes from one another. This is the primary way to modify the graph. Note that this method does not check if the nodes are already connected. This is because the connections are represented by ’HashSet’s which do not allow duplicates. Its also important to note that the ‘incoming’ and ‘outgoing’ indices are the indices of the nodes in the graph, not the indices of the connections in the ‘incoming’ and ‘outgoing’ ’HashSet’s. We must also remember that the ‘GraphNode’ cares about the ‘Arity’ of the ‘Operation’ it contains, so if we add a connection that would violate the ‘Arity’ of the ‘Operation’, the connection will result in a ‘GraphNode’ that is not ‘Valid’.

Attaches the node at the ‘incoming’ index to the node at the ‘outgoing’ index. This means that the node at the ‘incoming’ index will have an outgoing connection to the node at the ‘outgoing’ index, and the node at the ‘outgoing’ index will have an incoming connection from the node at the ‘incoming’ index.

§Arguments
  • incoming: The index of the node that will have an outgoing connection to the node at the ‘outgoing’ index.
  • outgoing: The index of the node that will have an incoming connection from the node at the ‘incoming’ index.
Source

pub fn detach(&mut self, incoming: usize, outgoing: usize) -> &mut Self

Detaches the node at the ‘incoming’ index from the node at the ‘outgoing’ index. This means that the node at the ‘incoming’ index will no longer have an outgoing connection to the node at the ‘outgoing’ index, and the node at the ‘outgoing’ index will no longer have an incoming connection from the node at the ‘incoming’ index.

§Arguments
  • incoming: The index of the node that will no longer have an outgoing connection to the node at the ‘outgoing’ index.
  • outgoing: The index of the node that will no longer have an incoming connection from the node at the ‘incoming’ index.
Source§

impl<T> Graph<T>

Functinos for modifying the graph.

Source

pub fn set_cycles(&mut self, indecies: Vec<usize>)

Given a list of node indices, this function will set the ‘direction’ field of the nodes at those indices to ‘Direction::Backward’ if they are part of a cycle. If they are not part of a cycle, the ‘direction’ field will be set to ‘Direction::Forward’. If no indices are provided, the function will set the ‘direction’ field of all nodes in the graph.

Source

pub fn try_modify<F>(&mut self, mutation: F) -> TransactionResult<T>
where F: FnOnce(GraphTransaction<'_, T>) -> TransactionResult<T>, T: Clone + Default + PartialEq,

tries to modify the graph using a ‘GraphTransaction’. If the transaction is successful, we return true and do nothing. If the transaction is not successful, we rollback the transaction by undoing all the changes made by the transaction and return false.

§Arguments
  • mutation: A closure that takes a mutable reference to a ‘GraphTransaction’ and returns a ‘bool’.
Source§

impl<T> Graph<T>

Functions for checking the validity of the graph or connections between nodes. These are useful for modifying the graph in a way that maintains its integrity.

Source

pub fn get_cycles(&self, index: usize) -> Vec<usize>

Get the cycles in the graph that include the node at the specified index.

§Arguments
  • index: The index of the node to get the cycles for.

Trait Implementations§

Source§

impl<T> AsMut<[GraphNode<T>]> for Graph<T>

Source§

fn as_mut(&mut self) -> &mut [GraphNode<T>]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<[GraphNode<T>]> for Graph<T>

Source§

fn as_ref(&self) -> &[GraphNode<T>]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone> Clone for Graph<T>

Source§

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

Returns a copy 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> Codex<GraphChromosome<T>, Graph<T>> for GraphCodex<T>
where T: Clone + PartialEq + Default,

Source§

fn encode(&self) -> Genotype<GraphChromosome<T>>

Source§

fn decode(&self, genotype: &Genotype<GraphChromosome<T>>) -> Graph<T>

Source§

fn spawn(&self, num: usize) -> Vec<T>

Spawn a new instance of T from the Codex. This will encode num new Genotypes and then decode it to a new instance of T.
Source§

fn spawn_genotypes(&self, num: usize) -> Vec<Genotype<C>>

Spawn a new instance of Genotype<G, A> from the Codex. This will encode num a new Genotypes.
Source§

fn spawn_population(&self, num: usize) -> Population<C>

Spawn a new instance of Population<G, A> from the Codex. This will encode num a new Genotypes
Source§

impl<T: Debug + PartialEq + Clone> Debug for Graph<T>

Source§

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

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

impl<T: Default> Default for Graph<T>

Source§

fn default() -> Graph<T>

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

impl Eval<Graph<Op<f32>>, f32> for Regression

Source§

fn eval(&self, graph: &Graph<Op<f32>>) -> f32

Source§

impl<T, V> Eval<Vec<Vec<V>>, Vec<Vec<V>>> for Graph<T>
where T: Eval<[V], V>, V: Clone + Default,

Source§

fn eval(&self, input: &Vec<Vec<V>>) -> Vec<Vec<V>>

Evaluates the Graph with the given input ‘Vec<Vec>’. Returns the output of the Graph as ‘Vec<Vec>’. This is inteded to be used when evaluating a batch of inputs.

§Arguments
  • input - A Vec<Vec<T>> to evaluate the Graph with.
§Returns
  • A Vec<Vec<T>> which is the output of the Graph.
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> Index<usize> for Graph<T>

Source§

type Output = GraphNode<T>

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Graph<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IntoIterator for Graph<T>

Source§

type Item = GraphNode<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<GraphNode<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for Graph<T>

Source§

fn eq(&self, other: &Graph<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 Graph<T>

Source§

fn is_valid(&self) -> bool

Source§

impl<T> StructuralPartialEq for Graph<T>

Auto Trait Implementations§

§

impl<T> Freeze for Graph<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Graph<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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, G, T> GraphIterator<'a, T> for G
where G: AsRef<[GraphNode<T>]>,

Source§

fn iter_topological(&'a self) -> GraphTopologicalIterator<'a, T>

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<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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V