coreason-engine 0.1.0

Coreason execution engine for deterministic computation
Documentation
// Copyright (c) 2026 CoReason, Inc
//
// This software is proprietary and dual-licensed
// Licensed under the Prosperity Public License 3.0 (the "License")
// A copy of the license is available at <https://prosperitylicense.com/versions/3.0.0>
// For details, see the LICENSE file
// Commercial use beyond a 30-day trial requires a separate license
//
// Source Code: <https://github.com/CoReason-AI/coreason-runtime>

use petgraph::graph::{DiGraph, NodeIndex};

use rayon::prelude::*;

/// A node in the Active Inference policy tree representing a state/belief projection

#[derive(Debug, Clone)]

pub struct PolicyNode {
    pub name: String,

    pub expected_free_energy: f64,
}

/// Active Inference Policy Search Tree built on petgraph

pub struct PolicySearchTree {
    pub graph: DiGraph<PolicyNode, ()>,
}

impl PolicySearchTree {
    /// Creates an empty policy tree

    pub fn new() -> Self {
        Self {
            graph: DiGraph::new(),
        }
    }

    /// Adds a policy belief node to the search tree

    pub fn add_node(&mut self, name: &str, efe: f64) -> NodeIndex {
        self.graph.add_node(PolicyNode {
            name: name.to_string(),

            expected_free_energy: efe,
        })
    }

    /// Connects two belief states representing a temporal action transition

    pub fn add_transition(&mut self, from: NodeIndex, to: NodeIndex) {
        self.graph.add_edge(from, to, ());
    }

    /// Evaluates the cumulative Expected Free Energy (EFE) of all nodes in parallel

    /// utilizing Rayon to leverage multi-threaded CPU substrates.

    pub fn evaluate_tree_parallel(&self) -> f64 {
        let raw_nodes: Vec<&PolicyNode> = self.graph.node_weights().collect();

        raw_nodes
            .par_iter()
            .map(|node| node.expected_free_energy)
            .sum()
    }
}