Skip to main content

NCP

Struct NCP 

Source
pub struct NCP { /* private fields */ }
Expand description

Neural Circuit Policy (NCP) wiring with biologically-inspired 4-layer architecture.

NCPs implement sparse, structured connectivity patterns inspired by the nervous system of C. elegans. This architecture provides:

  • Parameter efficiency: Fewer synapses than fully-connected networks
  • Interpretability: Clear information flow through defined layers
  • Biological plausibility: Excitatory/inhibitory synapse types

§Architecture

NCPs organize neurons into 4 functional layers:

Sensory Inputs ──► Inter Neurons ──► Command Neurons ──► Motor Neurons
   (input)         (processing)      (integration)       (output)
                                          │
                                          └──► Recurrent connections

§Layer Descriptions

LayerRoleConnectivity
SensoryExternal inputs→ Inter (via sensory_fanout)
InterFeature extraction→ Command (via inter_fanout)
CommandDecision/integration→ Motor + self (recurrent)
MotorOutput neurons← Command (via motor_fanin)

§Connectivity Parameters

  • sensory_fanout: How many inter neurons each input connects to
  • inter_fanout: How many command neurons each inter neuron connects to
  • recurrent_command_synapses: Number of command→command recurrent connections
  • motor_fanin: How many command neurons connect to each motor neuron

§Example

use ncps::wirings::{NCP, Wiring};

// Create NCP with explicit layer sizes
let mut wiring = NCP::new(
    10,  // inter_neurons: feature processing
    8,   // command_neurons: integration layer
    4,   // motor_neurons: output size
    4,   // sensory_fanout: each input → 4 inter neurons
    4,   // inter_fanout: each inter → 4 command neurons
    6,   // recurrent_command_synapses
    4,   // motor_fanin: each motor ← 4 command neurons
    42,  // seed for reproducibility
);

// Must build before use
wiring.build(16);  // 16 input features

// Total neurons = inter + command + motor = 22
assert_eq!(wiring.units(), 22);
assert_eq!(wiring.output_dim(), Some(4));

§Neuron ID Layout

Neurons are assigned IDs in this order:

[0..motor) [motor..motor+command) [motor+command..units)
  Motor        Command                Inter

§When to Use

Use NCP directly when you need fine-grained control over:

  • Exact layer sizes
  • Connectivity density (fanout/fanin parameters)
  • Recurrent connection count

For automatic parameter selection, use AutoNCP instead.

§Panics

The constructor panics if constraints are violated:

  • motor_fanin > command_neurons
  • sensory_fanout > inter_neurons
  • inter_fanout > command_neurons

Implementations§

Source§

impl NCP

Source

pub fn new( inter_neurons: usize, command_neurons: usize, motor_neurons: usize, sensory_fanout: usize, inter_fanout: usize, recurrent_command_synapses: usize, motor_fanin: usize, seed: u64, ) -> Self

Source

pub fn from_config(config: WiringConfig) -> Self

Trait Implementations§

Source§

impl Clone for NCP

Source§

fn clone(&self) -> NCP

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 Debug for NCP

Source§

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

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

impl Wiring for NCP

Source§

fn units(&self) -> usize

Returns the total number of neurons (hidden units) in this wiring. Read more
Source§

fn input_dim(&self) -> Option<usize>

Returns the input dimension (number of input features), or None if not yet built. Read more
Source§

fn output_dim(&self) -> Option<usize>

Returns the output dimension (number of motor neurons). Read more
Source§

fn num_layers(&self) -> usize

Returns the number of logical layers in this wiring. Read more
Source§

fn get_neurons_of_layer(&self, layer_id: usize) -> Vec<usize>

Returns the neuron IDs belonging to a specific layer. Read more
Source§

fn get_type_of_neuron(&self, neuron_id: usize) -> &'static str

Returns the type of a neuron by its ID. Read more
Source§

fn build(&mut self, input_dim: usize)

Builds the wiring by setting the input dimension and creating sensory connections. Read more
Source§

fn adjacency_matrix(&self) -> &Array2<i32>

Returns the internal adjacency matrix representing neuron-to-neuron synapses. Read more
Source§

fn sensory_adjacency_matrix(&self) -> Option<&Array2<i32>>

Returns the sensory adjacency matrix (input-to-neuron connections). Read more
Source§

fn add_synapse(&mut self, src: usize, dest: usize, polarity: i32)

Adds or modifies an internal synapse between two neurons. Read more
Source§

fn add_sensory_synapse(&mut self, src: usize, dest: usize, polarity: i32)

Adds or modifies a sensory synapse from an input feature to a neuron. Read more
Source§

fn get_config(&self) -> WiringConfig

Creates a serializable configuration for this wiring. Read more
Source§

fn is_built(&self) -> bool

Returns true if the wiring has been built (input dimension is set). Read more
Source§

fn erev_initializer(&self) -> Array2<i32>

Returns the reversal potential initializer (same as adjacency matrix). Read more
Source§

fn sensory_erev_initializer(&self) -> Option<Array2<i32>>

Returns the sensory reversal potential initializer. Read more
Source§

fn synapse_count(&self) -> usize

Returns the total number of internal synapses (non-zero entries in adjacency matrix). Read more
Source§

fn sensory_synapse_count(&self) -> usize

Returns the total number of sensory synapses (input-to-neuron connections). Read more
Source§

fn input_required(&self) -> bool

Returns true if this wiring requires external input (has sensory connections). Read more

Auto Trait Implementations§

§

impl Freeze for NCP

§

impl RefUnwindSafe for NCP

§

impl Send for NCP

§

impl Sync for NCP

§

impl Unpin for NCP

§

impl UnwindSafe for NCP

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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<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