Skip to main content

feagi_evolutionary/circuit/
boundary.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Circuit boundary resolution from region membership and cortical mappings.
5
6use std::collections::{BTreeMap, BTreeSet};
7
8use serde::{Deserialize, Serialize};
9
10use super::CircuitBoundary;
11
12/// Interface role of a member area, derived from its cortical group.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum AreaRole {
16    /// Receives activity from sensors (IPU); always a boundary input.
17    Sensory,
18    /// Drives actuators (OPU); always a boundary output.
19    Motor,
20    /// Any other area (custom, memory, core).
21    Internal,
22}
23
24/// One directed cortical mapping between two areas.
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26pub struct MappingEdge {
27    /// Source area id.
28    pub src_area_id: String,
29    /// Destination area id.
30    pub dst_area_id: String,
31}
32
33/// Computes the circuit boundary from member roles and the brain's mapping edges.
34///
35/// A member is an input when it is sensory or receives a mapping from a non-member, and an
36/// output when it is motor or sends a mapping to a non-member. Self-mappings and mappings
37/// between two non-members are ignored. A member may be both an input and an output.
38pub fn resolve_boundary(
39    members: &BTreeMap<String, AreaRole>,
40    edges: &[MappingEdge],
41) -> CircuitBoundary {
42    let mut input_area_ids = BTreeSet::new();
43    let mut output_area_ids = BTreeSet::new();
44    for (area_id, role) in members {
45        match role {
46            AreaRole::Sensory => {
47                input_area_ids.insert(area_id.clone());
48            }
49            AreaRole::Motor => {
50                output_area_ids.insert(area_id.clone());
51            }
52            AreaRole::Internal => {}
53        }
54    }
55    for edge in edges {
56        let src_in = members.contains_key(&edge.src_area_id);
57        let dst_in = members.contains_key(&edge.dst_area_id);
58        match (src_in, dst_in) {
59            (false, true) => {
60                input_area_ids.insert(edge.dst_area_id.clone());
61            }
62            (true, false) => {
63                output_area_ids.insert(edge.src_area_id.clone());
64            }
65            (true, true) | (false, false) => {}
66        }
67    }
68    CircuitBoundary {
69        input_area_ids,
70        output_area_ids,
71    }
72}