quantrs2_anneal/active_learning_decomposition/
knowledge_base.rs1use scirs2_core::ndarray::Array2;
4use std::collections::HashMap;
5use std::time::{Duration, Instant};
6
7use super::{
8 ActionType, ConditionType, DecompositionStrategy, LogicalOperator, PerformanceRecord,
9 RequirementType, RequirementValue, ResourceConstraints, SideEffectType, TrendDirection,
10};
11
12#[derive(Debug, Clone)]
14pub struct DecompositionKnowledgeBase {
15 pub strategy_database: StrategyDatabase,
17 pub pattern_library: PatternLibrary,
19 pub performance_repository: PerformanceRepository,
21 pub rule_engine: RuleEngine,
23}
24
25impl DecompositionKnowledgeBase {
26 pub fn new() -> Result<Self, String> {
27 Ok(Self {
28 strategy_database: StrategyDatabase::new(),
29 pattern_library: PatternLibrary::new(),
30 performance_repository: PerformanceRepository::new(),
31 rule_engine: RuleEngine::new(),
32 })
33 }
34}
35
36#[derive(Debug, Clone)]
38pub struct StrategyDatabase {
39 pub strategies: Vec<DecompositionStrategy>,
41 pub strategy_relationships: HashMap<String, Vec<String>>,
43 pub success_rates: HashMap<DecompositionStrategy, f64>,
45}
46
47impl StrategyDatabase {
48 #[must_use]
49 pub fn new() -> Self {
50 Self {
51 strategies: vec![
52 DecompositionStrategy::GraphPartitioning,
53 DecompositionStrategy::CommunityDetection,
54 DecompositionStrategy::SpectralClustering,
55 DecompositionStrategy::Hierarchical,
56 DecompositionStrategy::NoDecomposition,
57 ],
58 strategy_relationships: HashMap::new(),
59 success_rates: HashMap::new(),
60 }
61 }
62}
63
64#[derive(Debug, Clone)]
66pub struct PatternLibrary {
67 pub patterns: Vec<KnownPattern>,
69 pub pattern_index: HashMap<String, usize>,
71 pub similarity_matrix: Array2<f64>,
73}
74
75impl PatternLibrary {
76 #[must_use]
77 pub fn new() -> Self {
78 Self {
79 patterns: Vec::new(),
80 pattern_index: HashMap::new(),
81 similarity_matrix: Array2::zeros((0, 0)),
82 }
83 }
84}
85
86#[derive(Debug, Clone)]
88pub struct KnownPattern {
89 pub pattern_id: String,
91 pub description: String,
93 pub features: scirs2_core::ndarray::Array1<f64>,
95 pub optimal_strategies: Vec<DecompositionStrategy>,
97 pub frequency: f64,
99}
100
101#[derive(Debug, Clone)]
103pub struct PerformanceRepository {
104 pub historical_data: Vec<HistoricalPerformance>,
106 pub performance_trends: HashMap<String, PerformanceTrend>,
108 pub benchmark_results: Vec<BenchmarkResult>,
110}
111
112impl PerformanceRepository {
113 #[must_use]
114 pub fn new() -> Self {
115 Self {
116 historical_data: Vec::new(),
117 performance_trends: HashMap::new(),
118 benchmark_results: Vec::new(),
119 }
120 }
121}
122
123#[derive(Debug, Clone)]
125pub struct HistoricalPerformance {
126 pub problem_characteristics: ProblemCharacteristics,
128 pub strategy_applied: DecompositionStrategy,
130 pub performance_achieved: PerformanceRecord,
132 pub context: PerformanceContext,
134}
135
136#[derive(Debug, Clone)]
138pub struct ProblemCharacteristics {
139 pub problem_size: usize,
141 pub problem_type: String,
143 pub structural_features: scirs2_core::ndarray::Array1<f64>,
145 pub complexity_indicators: HashMap<String, f64>,
147}
148
149#[derive(Debug, Clone)]
151pub struct PerformanceContext {
152 pub hardware_config: String,
154 pub software_config: String,
156 pub resource_constraints: ResourceConstraints,
158 pub environmental_factors: HashMap<String, f64>,
160}
161
162#[derive(Debug, Clone)]
164pub struct PerformanceTrend {
165 pub trend_direction: TrendDirection,
167 pub trend_strength: f64,
169 pub data_points: Vec<(f64, f64)>, pub prediction: Option<TrendPrediction>,
173}
174
175#[derive(Debug, Clone)]
177pub struct TrendPrediction {
178 pub predicted_value: f64,
180 pub confidence: f64,
182 pub horizon: Duration,
184}
185
186#[derive(Debug, Clone)]
188pub struct BenchmarkResult {
189 pub benchmark_name: String,
191 pub problem_set: Vec<String>,
193 pub strategy_results: HashMap<DecompositionStrategy, f64>,
195 pub best_strategy: DecompositionStrategy,
197}
198
199#[derive(Debug, Clone)]
201pub struct RuleEngine {
202 pub rules: Vec<DecompositionRule>,
204 pub rule_priorities: HashMap<String, f64>,
206 pub application_history: Vec<RuleApplication>,
208}
209
210impl RuleEngine {
211 #[must_use]
212 pub fn new() -> Self {
213 Self {
214 rules: Vec::new(),
215 rule_priorities: HashMap::new(),
216 application_history: Vec::new(),
217 }
218 }
219}
220
221#[derive(Debug, Clone)]
223pub struct DecompositionRule {
224 pub rule_id: String,
226 pub condition: RuleCondition,
228 pub action: RuleAction,
230 pub confidence: f64,
232 pub applicability: RuleApplicability,
234}
235
236#[derive(Debug, Clone)]
238pub struct RuleCondition {
239 pub condition_type: ConditionType,
241 pub parameters: HashMap<String, f64>,
243 pub logical_operator: LogicalOperator,
245}
246
247#[derive(Debug, Clone)]
249pub struct RuleAction {
250 pub action_type: ActionType,
252 pub parameters: HashMap<String, f64>,
254 pub expected_outcome: ExpectedOutcome,
256}
257
258#[derive(Debug, Clone)]
260pub struct ExpectedOutcome {
261 pub performance_improvement: f64,
263 pub outcome_confidence: f64,
265 pub side_effects: Vec<SideEffect>,
267}
268
269#[derive(Debug, Clone)]
271pub struct SideEffect {
272 pub effect_type: SideEffectType,
274 pub magnitude: f64,
276 pub probability: f64,
278}
279
280#[derive(Debug, Clone)]
282pub struct RuleApplicability {
283 pub applicable_problem_types: Vec<String>,
285 pub applicable_size_range: (usize, usize),
287 pub context_requirements: Vec<ContextRequirement>,
289}
290
291#[derive(Debug, Clone)]
293pub struct ContextRequirement {
294 pub requirement_type: RequirementType,
296 pub required_value: RequirementValue,
298}
299
300#[derive(Debug, Clone)]
302pub struct RuleApplication {
303 pub timestamp: Instant,
305 pub rule_id: String,
307 pub problem_context: ProblemCharacteristics,
309 pub result: ApplicationResult,
311}
312
313#[derive(Debug, Clone)]
315pub struct ApplicationResult {
316 pub success: bool,
318 pub performance_impact: f64,
320 pub user_satisfaction: Option<f64>,
322 pub lessons_learned: Vec<String>,
324}