use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct HDCConfig {
pub hypervector_dim: usize,
pub sparsity: f64,
pub similarity_threshold: f64,
pub training_iterations: usize,
pub learning_rate: f64,
pub bundling_capacity: usize,
pub binding_strength: f64,
pub cleanup_threshold: f64,
}
impl Default for HDCConfig {
fn default() -> Self {
Self {
hypervector_dim: 10000,
sparsity: 0.01, similarity_threshold: 0.8,
training_iterations: 10,
learning_rate: 0.1,
bundling_capacity: 100,
binding_strength: 1.0,
cleanup_threshold: 0.7,
}
}
}
#[derive(Debug, Clone)]
pub struct Hypervector {
pub sparse_data: Vec<(usize, f64)>,
pub dimension: usize,
pub norm: f64,
}
#[derive(Debug, Clone)]
pub struct PatternMatch {
pub label: String,
pub confidence: f64,
pub position: (usize, usize),
pub size: (usize, usize),
}
#[derive(Debug, Clone)]
pub struct FeatureDetection {
pub feature_type: String,
pub position: (usize, usize),
pub strength: f64,
pub hypervector: Hypervector,
pub patch_size: (usize, usize),
}
#[derive(Debug, Clone)]
pub struct SequenceEncoding {
pub encoding: Hypervector,
pub temporal_positions: Vec<usize>,
pub confidence: f64,
}
#[derive(Debug, Clone)]
pub struct Experience {
pub encoding: Hypervector,
pub label: String,
pub timestamp: usize,
pub importance: f64,
}
#[derive(Debug, Clone)]
pub struct ConsolidationResult {
pub interference_prevented: usize,
pub effectiveness_score: f64,
pub replay_cycles_used: usize,
}
#[derive(Debug, Clone)]
pub struct PredictionResult {
pub predicted_label: String,
pub confidence: f64,
pub alternatives: Vec<(String, f64)>,
}
#[derive(Debug, Clone)]
pub struct UpdateResult {
pub memory_updated: bool,
pub learning_rate_used: f64,
pub performance_change: f64,
}
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
pub accuracy: f64,
pub learning_speed: f64,
pub memory_efficiency: f64,
pub adaptation_effectiveness: f64,
}
#[derive(Debug, Clone)]
pub struct OnlineLearningResult {
pub prediction: PredictionResult,
pub learning_update: UpdateResult,
pub system_performance: PerformanceMetrics,
pub adaptation_rate: f64,
}
#[derive(Debug, Clone)]
pub struct CompositionResult {
pub query_similarity: f64,
pub concept_presence: HashMap<String, f64>,
pub composed_representation: Hypervector,
pub image_representation: Hypervector,
}
#[derive(Debug, Clone)]
pub struct AbstractionLevel {
pub level: usize,
pub concepts: HashMap<String, Hypervector>,
pub resolution: f64,
pub complexity: f64,
}
#[derive(Debug, Clone)]
pub struct ReasoningChain {
pub chain_id: String,
pub concepts: Vec<String>,
pub confidence: f64,
pub support_evidence: f64,
}
#[derive(Debug, Clone)]
pub struct MetaCognitiveAssessment {
pub confidence_score: f64,
pub reasoning_depth: usize,
pub uncertainty_estimate: f64,
}
#[derive(Debug, Clone)]
pub struct MultiModalFusionConfig {
pub visual_weight: f64,
pub temporal_weight: f64,
pub semantic_weight: f64,
pub attention_strength: f64,
pub fusion_method: FusionMethod,
}
impl Default for MultiModalFusionConfig {
fn default() -> Self {
Self {
visual_weight: 0.4,
temporal_weight: 0.3,
semantic_weight: 0.3,
attention_strength: 0.8,
fusion_method: FusionMethod::WeightedBundle,
}
}
}
#[derive(Debug, Clone)]
pub enum FusionMethod {
WeightedBundle,
AttentionFusion,
HierarchicalFusion,
}
#[derive(Debug, Clone)]
pub struct AdaptationParameters {
pub base_rate: f64,
pub current_rate: f64,
pub min_rate: f64,
pub max_rate: f64,
pub adaptation_speed: f64,
pub performance_threshold: f64,
}
impl Default for AdaptationParameters {
fn default() -> Self {
Self {
base_rate: 0.1,
current_rate: 0.1,
min_rate: 0.001,
max_rate: 0.5,
adaptation_speed: 0.05,
performance_threshold: 0.8,
}
}
}
impl AdaptationParameters {
pub fn adjust_based_on_performance(
&mut self,
tracker: &crate::hyperdimensional_computing::memory::PerformanceTracker,
) {
let recent_change = tracker.get_recent_performance_change();
if recent_change > 0.05 {
self.current_rate =
(self.current_rate * (1.0 + self.adaptation_speed)).min(self.max_rate);
} else if recent_change < -0.05 {
self.current_rate =
(self.current_rate * (1.0 - self.adaptation_speed)).max(self.min_rate);
}
}
pub fn reset(&mut self) {
self.current_rate = self.base_rate;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hdc_config_default() {
let config = HDCConfig::default();
assert_eq!(config.hypervector_dim, 10000);
assert_eq!(config.sparsity, 0.01);
assert_eq!(config.similarity_threshold, 0.8);
assert_eq!(config.training_iterations, 10);
assert_eq!(config.learning_rate, 0.1);
assert_eq!(config.bundling_capacity, 100);
assert_eq!(config.binding_strength, 1.0);
assert_eq!(config.cleanup_threshold, 0.7);
}
#[test]
fn test_hypervector_creation() {
let hv = Hypervector {
sparse_data: vec![(0, 1.0), (100, -1.0), (200, 1.0)],
dimension: 1000,
norm: 1.732, };
assert_eq!(hv.dimension, 1000);
assert_eq!(hv.sparse_data.len(), 3);
assert!((hv.norm - 1.732).abs() < 0.01);
}
#[test]
fn test_pattern_match() {
let pattern_match = PatternMatch {
label: "test_pattern".to_string(),
confidence: 0.95,
position: (10, 20),
size: (32, 32),
};
assert_eq!(pattern_match.label, "test_pattern");
assert_eq!(pattern_match.confidence, 0.95);
assert_eq!(pattern_match.position, (10, 20));
assert_eq!(pattern_match.size, (32, 32));
}
#[test]
fn test_multimodal_fusion_config_default() {
let config = MultiModalFusionConfig::default();
let sum = config.visual_weight + config.temporal_weight + config.semantic_weight;
assert!((sum - 1.0).abs() < 0.1);
assert!(config.attention_strength > 0.0);
assert!(config.attention_strength <= 1.0);
matches!(config.fusion_method, FusionMethod::WeightedBundle);
}
#[test]
fn test_adaptation_parameters() {
let mut params = AdaptationParameters::default();
let original_rate = params.current_rate;
params.current_rate = 0.3;
params.reset();
assert_eq!(params.current_rate, params.base_rate);
assert!(params.min_rate < params.max_rate);
assert!(params.current_rate >= params.min_rate);
assert!(params.current_rate <= params.max_rate);
}
#[test]
fn test_prediction_result() {
let prediction = PredictionResult {
predicted_label: "cat".to_string(),
confidence: 0.9,
alternatives: vec![("dog".to_string(), 0.1), ("bird".to_string(), 0.05)],
};
assert_eq!(prediction.predicted_label, "cat");
assert_eq!(prediction.confidence, 0.9);
assert_eq!(prediction.alternatives.len(), 2);
assert_eq!(prediction.alternatives[0].0, "dog");
assert_eq!(prediction.alternatives[0].1, 0.1);
}
#[test]
fn test_reasoning_chain() {
let chain = ReasoningChain {
chain_id: "chain_1".to_string(),
concepts: vec!["A".to_string(), "B".to_string(), "C".to_string()],
confidence: 0.8,
support_evidence: 0.75,
};
assert_eq!(chain.chain_id, "chain_1");
assert_eq!(chain.concepts.len(), 3);
assert_eq!(chain.concepts[0], "A");
assert_eq!(chain.confidence, 0.8);
assert_eq!(chain.support_evidence, 0.75);
}
}