use scirs2_core::ndarray::Array2;
use std::collections::HashMap;
use std::time::{Duration, Instant};
use super::{
ActionType, ConditionType, DecompositionStrategy, LogicalOperator, PerformanceRecord,
RequirementType, RequirementValue, ResourceConstraints, SideEffectType, TrendDirection,
};
#[derive(Debug, Clone)]
pub struct DecompositionKnowledgeBase {
pub strategy_database: StrategyDatabase,
pub pattern_library: PatternLibrary,
pub performance_repository: PerformanceRepository,
pub rule_engine: RuleEngine,
}
impl DecompositionKnowledgeBase {
pub fn new() -> Result<Self, String> {
Ok(Self {
strategy_database: StrategyDatabase::new(),
pattern_library: PatternLibrary::new(),
performance_repository: PerformanceRepository::new(),
rule_engine: RuleEngine::new(),
})
}
}
#[derive(Debug, Clone)]
pub struct StrategyDatabase {
pub strategies: Vec<DecompositionStrategy>,
pub strategy_relationships: HashMap<String, Vec<String>>,
pub success_rates: HashMap<DecompositionStrategy, f64>,
}
impl StrategyDatabase {
#[must_use]
pub fn new() -> Self {
Self {
strategies: vec![
DecompositionStrategy::GraphPartitioning,
DecompositionStrategy::CommunityDetection,
DecompositionStrategy::SpectralClustering,
DecompositionStrategy::Hierarchical,
DecompositionStrategy::NoDecomposition,
],
strategy_relationships: HashMap::new(),
success_rates: HashMap::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct PatternLibrary {
pub patterns: Vec<KnownPattern>,
pub pattern_index: HashMap<String, usize>,
pub similarity_matrix: Array2<f64>,
}
impl PatternLibrary {
#[must_use]
pub fn new() -> Self {
Self {
patterns: Vec::new(),
pattern_index: HashMap::new(),
similarity_matrix: Array2::zeros((0, 0)),
}
}
}
#[derive(Debug, Clone)]
pub struct KnownPattern {
pub pattern_id: String,
pub description: String,
pub features: scirs2_core::ndarray::Array1<f64>,
pub optimal_strategies: Vec<DecompositionStrategy>,
pub frequency: f64,
}
#[derive(Debug, Clone)]
pub struct PerformanceRepository {
pub historical_data: Vec<HistoricalPerformance>,
pub performance_trends: HashMap<String, PerformanceTrend>,
pub benchmark_results: Vec<BenchmarkResult>,
}
impl PerformanceRepository {
#[must_use]
pub fn new() -> Self {
Self {
historical_data: Vec::new(),
performance_trends: HashMap::new(),
benchmark_results: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct HistoricalPerformance {
pub problem_characteristics: ProblemCharacteristics,
pub strategy_applied: DecompositionStrategy,
pub performance_achieved: PerformanceRecord,
pub context: PerformanceContext,
}
#[derive(Debug, Clone)]
pub struct ProblemCharacteristics {
pub problem_size: usize,
pub problem_type: String,
pub structural_features: scirs2_core::ndarray::Array1<f64>,
pub complexity_indicators: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct PerformanceContext {
pub hardware_config: String,
pub software_config: String,
pub resource_constraints: ResourceConstraints,
pub environmental_factors: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct PerformanceTrend {
pub trend_direction: TrendDirection,
pub trend_strength: f64,
pub data_points: Vec<(f64, f64)>, pub prediction: Option<TrendPrediction>,
}
#[derive(Debug, Clone)]
pub struct TrendPrediction {
pub predicted_value: f64,
pub confidence: f64,
pub horizon: Duration,
}
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
pub benchmark_name: String,
pub problem_set: Vec<String>,
pub strategy_results: HashMap<DecompositionStrategy, f64>,
pub best_strategy: DecompositionStrategy,
}
#[derive(Debug, Clone)]
pub struct RuleEngine {
pub rules: Vec<DecompositionRule>,
pub rule_priorities: HashMap<String, f64>,
pub application_history: Vec<RuleApplication>,
}
impl RuleEngine {
#[must_use]
pub fn new() -> Self {
Self {
rules: Vec::new(),
rule_priorities: HashMap::new(),
application_history: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct DecompositionRule {
pub rule_id: String,
pub condition: RuleCondition,
pub action: RuleAction,
pub confidence: f64,
pub applicability: RuleApplicability,
}
#[derive(Debug, Clone)]
pub struct RuleCondition {
pub condition_type: ConditionType,
pub parameters: HashMap<String, f64>,
pub logical_operator: LogicalOperator,
}
#[derive(Debug, Clone)]
pub struct RuleAction {
pub action_type: ActionType,
pub parameters: HashMap<String, f64>,
pub expected_outcome: ExpectedOutcome,
}
#[derive(Debug, Clone)]
pub struct ExpectedOutcome {
pub performance_improvement: f64,
pub outcome_confidence: f64,
pub side_effects: Vec<SideEffect>,
}
#[derive(Debug, Clone)]
pub struct SideEffect {
pub effect_type: SideEffectType,
pub magnitude: f64,
pub probability: f64,
}
#[derive(Debug, Clone)]
pub struct RuleApplicability {
pub applicable_problem_types: Vec<String>,
pub applicable_size_range: (usize, usize),
pub context_requirements: Vec<ContextRequirement>,
}
#[derive(Debug, Clone)]
pub struct ContextRequirement {
pub requirement_type: RequirementType,
pub required_value: RequirementValue,
}
#[derive(Debug, Clone)]
pub struct RuleApplication {
pub timestamp: Instant,
pub rule_id: String,
pub problem_context: ProblemCharacteristics,
pub result: ApplicationResult,
}
#[derive(Debug, Clone)]
pub struct ApplicationResult {
pub success: bool,
pub performance_impact: f64,
pub user_satisfaction: Option<f64>,
pub lessons_learned: Vec<String>,
}