Skip to main content

GraphTransaction

Struct GraphTransaction 

Source
pub struct GraphTransaction<'a, T> { /* private fields */ }
Expand description

Tracks reversible changes to a Graph<T> and provides commit/rollback.

Usage:

  • Mutate via push(...), attach(...), detach(...), change_direction(...).
  • Call commit(), commit_with(...), or try_commit() to finalize.
  • On invalid commit, the graph is rolled back and you receive replay steps you can pass to replay(...) in a fresh transaction.

Implementations§

Source§

impl<'a, T> GraphTransaction<'a, T>

Source

pub fn new(graph: &'a mut Graph<T>) -> Self

Source

pub fn commit(self) -> TransactionResult<T>

Finalize the transaction:

  • Marks cycles (set_cycles()).
  • Validates with Graph<T>: Valid.
  • If valid, returns Valid(steps).
  • If invalid, rolls back and returns Invalid(steps, replay).
Source

pub fn commit_with( self, validator: impl Fn(&Graph<T>) -> bool, ) -> TransactionResult<T>

Like commit(), but also requires validator(&Graph<T>) to pass.

This is useful for domain-specific acceptance checks in addition to structural validity.

Source

pub fn try_commit(self) -> TransactionResult<T>

Attempt to commit the transaction, repairing invalid nodes if possible.

  • Calls repair_invalid_nodes() up to MAX_REPAIR_ATTEMPTS times.

Note: This may produce different graphs on each call due to possible random repairs.

Source

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

Append a node to the graph and record the change. Returns the new node’s index.

Source

pub fn attach(&mut self, from: usize, to: usize)

Create an edge from from to to and record the change.

Source

pub fn detach(&mut self, from: usize, to: usize)

Remove an edge from from to to and record the change.

Source

pub fn change_direction(&mut self, index: usize, direction: Direction)

Change the direction of the node at index if it differs from its current direction.

Records the previous direction so the change can be rolled back or replayed.

Source

pub fn rollback(self) -> Vec<ReplayStep<T>>

Undo all recorded changes in reverse order, mutating the graph back to its original state.

Returns a sequence of ReplaySteps that can be fed to replay(...) on a new transaction to re-apply the same operational effects.

Source

pub fn replay(&mut self, steps: Vec<ReplayStep<T>>)

Apply ReplaySteps (typically from a prior rollback()) to this transaction/graph.

Steps are recorded as normal mutations (so they can be committed or rolled back again).

Source

pub fn set_cycles(&mut self)

Mark cycle participation for nodes touched in this transaction:

  • Nodes without cycles are set to Direction::Forward.
  • Nodes in cycles (via get_cycles(idx)) are set to Direction::Backward.
Source

pub fn set_innovation( &mut self, node_idx: usize, innovation: Option<InnovationId>, )

Source

pub fn get_insertion_steps( &self, source_idx: usize, target_idx: usize, new_node_idx: usize, rand: &mut RdRand<'_>, ) -> Vec<InsertStep>

Compute the steps needed to insert new_node_idx between source_idx and target_idx.

Behavior:

  • If new_node has Arity::Zero and target is not locked, connect new_node -> target.
  • If source is an Edge, re-route its single outgoing through new_node.
  • If target is an Edge or is locked, detach one incoming and rewire via new_node.
  • Otherwise connect source -> new_node -> target.
Source

pub fn random_source_node(&self, rand: &mut RdRand<'_>) -> Option<&GraphNode<T>>

The below functions are used to get random nodes from the graph. These are useful for creating connections between nodes. Neither of these functions will return an edge node. This is because edge nodes are not valid source or target nodes for connections as they only allow one incoming and one outgoing connection, thus they can’t be used to create new connections. Instead, edge nodes are used to represent the weights of the connections

Get a random node that can be used as a source node for a connection. A source node can be either an input or a vertex node.

Source

pub fn random_target_node(&self, rand: &mut RdRand<'_>) -> Option<&GraphNode<T>>

Get a random node that can be used as a target node for a connection. A target node can be either an output or a vertex node.

Source

pub fn unique_random_source_node( &self, arity: &Arity, rand: &mut RdRand<'_>, ) -> Option<Vec<&GraphNode<T>>>

Source

pub fn random_target_node_where<F>( &self, rand: &mut RdRand<'_>, filter: F, ) -> Option<&GraphNode<T>>
where F: Fn(&GraphNode<T>) -> bool,

Get a random target node that satisfies the provided filter function. This is essentially a filtered version of the above function random_target_node.

Methods from Deref<Target = Graph<T>>§

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

pub fn get_cycles(&self, from: usize) -> HashSet<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> Deref for GraphTransaction<'_, T>

Source§

type Target = Graph<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<'a, T> !UnwindSafe for GraphTransaction<'a, T>

§

impl<'a, T> Freeze for GraphTransaction<'a, T>
where &'a mut Graph<T>: Freeze,

§

impl<'a, T> RefUnwindSafe for GraphTransaction<'a, T>
where &'a mut Graph<T>: RefUnwindSafe,

§

impl<'a, T> Send for GraphTransaction<'a, T>
where &'a mut Graph<T>: Send,

§

impl<'a, T> Sync for GraphTransaction<'a, T>
where &'a mut Graph<T>: Sync,

§

impl<'a, T> Unpin for GraphTransaction<'a, T>
where &'a mut Graph<T>: Unpin,

§

impl<'a, T> UnsafeUnpin for GraphTransaction<'a, T>
where &'a mut Graph<T>: UnsafeUnpin,

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> 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<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.