Skip to main content

tensorlogic_quantrs_hooks/loopy_bp/
config.rs

1//! Configuration and result types for Loopy Belief Propagation.
2
3use scirs2_core::ndarray::{Array1, ArrayD};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7use super::cycle::CycleAnalysis;
8use super::energy::BetheFreeEnergy;
9use super::types::{LbpConvergenceMonitor, LbpDampingPolicy, UpdateSchedule};
10
11// ──────────────────────────────────────────────────────────────────────────────
12// Loopy BP configuration
13// ──────────────────────────────────────────────────────────────────────────────
14
15/// Configuration for Loopy Belief Propagation.
16#[derive(Clone, Debug, Serialize, Deserialize)]
17pub struct LoopyBpConfig {
18    /// Maximum number of sweeps (full passes over all messages).
19    pub max_iterations: usize,
20    /// Convergence tolerance (L∞ norm of message residuals).
21    pub tolerance: f64,
22    /// Message damping policy.
23    pub damping: LbpDampingPolicy,
24    /// Message update schedule.
25    pub schedule: UpdateSchedule,
26    /// Whether to compute Bethe free energy at the end.
27    pub compute_bethe: bool,
28    /// Random seed for the residual-BP priority queue tie-breaking.
29    pub seed: u64,
30}
31
32impl Default for LoopyBpConfig {
33    fn default() -> Self {
34        Self {
35            max_iterations: 200,
36            tolerance: 1e-6,
37            damping: LbpDampingPolicy::Uniform(0.5),
38            schedule: UpdateSchedule::Synchronous,
39            compute_bethe: true,
40            seed: 42,
41        }
42    }
43}
44
45impl LoopyBpConfig {
46    /// Create with default settings.
47    pub fn new() -> Self {
48        Self::default()
49    }
50
51    /// Builder: set maximum iterations.
52    pub fn with_max_iterations(mut self, n: usize) -> Self {
53        self.max_iterations = n;
54        self
55    }
56
57    /// Builder: set tolerance.
58    pub fn with_tolerance(mut self, tol: f64) -> Self {
59        self.tolerance = tol;
60        self
61    }
62
63    /// Builder: set damping policy.
64    pub fn with_damping(mut self, d: LbpDampingPolicy) -> Self {
65        self.damping = d;
66        self
67    }
68
69    /// Builder: set update schedule.
70    pub fn with_schedule(mut self, s: UpdateSchedule) -> Self {
71        self.schedule = s;
72        self
73    }
74}
75
76// ──────────────────────────────────────────────────────────────────────────────
77// Result type
78// ──────────────────────────────────────────────────────────────────────────────
79
80/// Full output from a Loopy BP run.
81#[derive(Clone, Debug)]
82pub struct LoopyBpResult {
83    /// Variable marginal beliefs: var_name → probability vector.
84    pub beliefs: HashMap<String, Array1<f64>>,
85    /// Factor joint beliefs: factor_id → joint probability tensor.
86    pub factor_beliefs: HashMap<String, ArrayD<f64>>,
87    /// Convergence monitor with full iteration history.
88    pub convergence: LbpConvergenceMonitor,
89    /// Bethe free energy (if `config.compute_bethe` was set).
90    pub bethe: Option<BetheFreeEnergy>,
91    /// Cycle analysis of the input factor graph.
92    pub cycle_analysis: CycleAnalysis,
93}