kizzasi-logic 0.2.1

TensorLogic bridge for Kizzasi - constraint enforcement and safety guardrails
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Online Constraint Learning
//!
//! This module provides algorithms for learning constraints from streaming data:
//! - Incremental constraint refinement
//! - Anomaly-based constraint discovery
//! - Constraint parameter tuning from feedback
//! - Active learning for constraint boundaries

use crate::{LinearConstraint, LogicResult};
use scirs2_core::ndarray::Array1;
use std::collections::VecDeque;

/// Online learner for refining constraints from streaming data
#[derive(Debug, Clone)]
pub struct OnlineConstraintLearner {
    /// Current constraint estimate
    constraint: LinearConstraint,
    /// Learning rate for parameter updates
    #[allow(dead_code)]
    learning_rate: f32,
    /// Historical data buffer
    data_buffer: VecDeque<(Array1<f32>, bool)>, // (sample, is_feasible)
    /// Maximum buffer size
    #[allow(dead_code)]
    max_buffer_size: usize,
    /// Number of updates performed
    update_count: usize,
}

impl OnlineConstraintLearner {
    /// Create a new online constraint learner
    pub fn new(
        initial_constraint: LinearConstraint,
        learning_rate: f32,
        max_buffer_size: usize,
    ) -> Self {
        Self {
            constraint: initial_constraint,
            learning_rate,
            data_buffer: VecDeque::new(),
            max_buffer_size,
            update_count: 0,
        }
    }

    /// Add a new observation and update the constraint
    pub fn observe(&mut self, sample: Array1<f32>, is_feasible: bool) -> LogicResult<()> {
        // Add to buffer
        self.data_buffer.push_back((sample.clone(), is_feasible));
        if self.data_buffer.len() > self.max_buffer_size {
            self.data_buffer.pop_front();
        }

        // Update constraint using stochastic gradient descent
        self.refine_constraint(&sample, is_feasible)?;
        self.update_count += 1;

        Ok(())
    }

    /// Refine constraint based on a single observation
    fn refine_constraint(&mut self, sample: &Array1<f32>, is_feasible: bool) -> LogicResult<()> {
        let sample_slice = sample.as_slice().unwrap_or(&[]);
        let current_satisfied = self.constraint.check(sample_slice);

        // If prediction matches observation, no update needed
        if current_satisfied == is_feasible {
            return Ok(());
        }

        // Compute gradient for constraint refinement
        // For a·x <= b, we adjust 'a' and 'b' to better fit the data
        let violation = self.constraint.violation(sample_slice);

        // Update using perceptron-like rule
        let update_scale = if is_feasible {
            // Sample should be feasible but is violated: loosen constraint
            self.learning_rate * violation
        } else {
            // Sample should be infeasible but is satisfied: tighten constraint
            -self.learning_rate
        };

        // Create updated constraint (simplified - in practice would update coefficients)
        // This is a placeholder for demonstration
        let _ = update_scale; // TODO: implement actual coefficient updates

        Ok(())
    }

    /// Get the current learned constraint
    pub fn get_constraint(&self) -> &LinearConstraint {
        &self.constraint
    }

    /// Get number of updates performed
    pub fn update_count(&self) -> usize {
        self.update_count
    }

    /// Evaluate confidence in current constraint
    pub fn confidence(&self) -> f32 {
        if self.data_buffer.is_empty() {
            return 0.0;
        }

        // Compute accuracy on buffered data
        let correct = self
            .data_buffer
            .iter()
            .filter(|(sample, is_feasible)| {
                let satisfied = self.constraint.check(sample.as_slice().unwrap_or(&[]));
                satisfied == *is_feasible
            })
            .count();

        correct as f32 / self.data_buffer.len() as f32
    }
}

/// Anomaly detector for discovering new constraints
#[derive(Debug, Clone)]
pub struct AnomalyBasedConstraintDiscovery {
    /// Historical normal samples
    normal_samples: VecDeque<Array1<f32>>,
    /// Maximum number of normal samples to keep
    max_samples: usize,
    /// Anomaly threshold (number of standard deviations)
    anomaly_threshold: f32,
    /// Discovered constraints
    discovered_constraints: Vec<LinearConstraint>,
}

impl AnomalyBasedConstraintDiscovery {
    /// Create a new anomaly-based constraint discoverer
    pub fn new(max_samples: usize, anomaly_threshold: f32) -> Self {
        Self {
            normal_samples: VecDeque::new(),
            max_samples,
            anomaly_threshold,
            discovered_constraints: Vec::new(),
        }
    }

    /// Add a normal sample for baseline estimation
    pub fn add_normal_sample(&mut self, sample: Array1<f32>) {
        self.normal_samples.push_back(sample);
        if self.normal_samples.len() > self.max_samples {
            self.normal_samples.pop_front();
        }
    }

    /// Check if a sample is anomalous and potentially discover new constraint
    pub fn detect_anomaly(&mut self, sample: &Array1<f32>) -> bool {
        if self.normal_samples.len() < 2 {
            return false; // Not enough data for detection
        }

        // Compute statistics of normal samples
        let dim = sample.len();
        let n = self.normal_samples.len();

        // Compute mean and std dev for each dimension
        let mut is_anomalous = false;

        for d in 0..dim {
            let mean: f32 = self.normal_samples.iter().map(|s| s[d]).sum::<f32>() / n as f32;

            let variance: f32 = self
                .normal_samples
                .iter()
                .map(|s| (s[d] - mean).powi(2))
                .sum::<f32>()
                / n as f32;

            let std_dev = variance.sqrt();

            // Check if sample is outside threshold
            let z_score = (sample[d] - mean).abs() / (std_dev + 1e-8);
            if z_score > self.anomaly_threshold {
                is_anomalous = true;

                // Discover constraint: x[d] should be within [mean - threshold*std, mean + threshold*std]
                self.discover_bound_constraint(d, mean, std_dev);
            }
        }

        is_anomalous
    }

    /// Discover a bound constraint for a dimension
    fn discover_bound_constraint(&mut self, dim: usize, mean: f32, std_dev: f32) {
        // Create constraint: x[dim] <= mean + threshold * std_dev
        let upper_bound = mean + self.anomaly_threshold * std_dev;
        let mut coeffs = vec![0.0; dim + 1];
        coeffs[dim] = 1.0;

        let constraint = LinearConstraint::less_eq(coeffs, upper_bound);

        // Check if we already have a similar constraint
        let is_duplicate = self.discovered_constraints.iter().any(|c| {
            c.coefficients().len() == constraint.coefficients().len()
                && c.coefficients()
                    .iter()
                    .zip(constraint.coefficients().iter())
                    .all(|(a, b)| (a - b).abs() < 0.1)
        });

        if !is_duplicate {
            self.discovered_constraints.push(constraint);
        }
    }

    /// Get discovered constraints
    pub fn discovered_constraints(&self) -> &[LinearConstraint] {
        &self.discovered_constraints
    }

    /// Get number of discovered constraints
    pub fn num_discovered(&self) -> usize {
        self.discovered_constraints.len()
    }
}

/// Active learner for exploring constraint boundaries
#[derive(Debug, Clone)]
pub struct ActiveConstraintBoundaryLearner {
    /// Current constraint estimate
    constraint: LinearConstraint,
    /// Samples near the boundary (uncertain region)
    boundary_samples: Vec<(Array1<f32>, Option<bool>)>, // (sample, label if known)
    /// Uncertainty threshold for boundary detection
    uncertainty_threshold: f32,
    /// Maximum number of boundary samples to track
    max_boundary_samples: usize,
}

impl ActiveConstraintBoundaryLearner {
    /// Create a new active boundary learner
    pub fn new(
        initial_constraint: LinearConstraint,
        uncertainty_threshold: f32,
        max_boundary_samples: usize,
    ) -> Self {
        Self {
            constraint: initial_constraint,
            boundary_samples: Vec::new(),
            uncertainty_threshold,
            max_boundary_samples,
        }
    }

    /// Get the most informative sample to query (closest to boundary)
    pub fn query_next(&self) -> Option<Array1<f32>> {
        // Find unlabeled sample closest to decision boundary
        self.boundary_samples
            .iter()
            .filter(|(_, label)| label.is_none())
            .min_by(|(s1, _), (s2, _)| {
                let v1 = self
                    .constraint
                    .violation(s1.as_slice().unwrap_or(&[]))
                    .abs();
                let v2 = self
                    .constraint
                    .violation(s2.as_slice().unwrap_or(&[]))
                    .abs();
                v1.partial_cmp(&v2).unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(s, _)| s.clone())
    }

    /// Add a labeled sample
    pub fn add_labeled_sample(&mut self, sample: Array1<f32>, is_feasible: bool) {
        let violation = self
            .constraint
            .violation(sample.as_slice().unwrap_or(&[]))
            .abs();

        // Add to boundary samples if near boundary
        if violation < self.uncertainty_threshold {
            self.boundary_samples.push((sample, Some(is_feasible)));
            if self.boundary_samples.len() > self.max_boundary_samples {
                self.boundary_samples.remove(0);
            }
        }
    }

    /// Add an unlabeled sample for potential querying
    pub fn add_unlabeled_sample(&mut self, sample: Array1<f32>) {
        let violation = self
            .constraint
            .violation(sample.as_slice().unwrap_or(&[]))
            .abs();

        if violation < self.uncertainty_threshold {
            self.boundary_samples.push((sample, None));
            if self.boundary_samples.len() > self.max_boundary_samples {
                self.boundary_samples.remove(0);
            }
        }
    }

    /// Refine constraint based on labeled boundary samples
    pub fn refine(&mut self) -> LogicResult<()> {
        // Use labeled boundary samples to refine constraint
        // This is a simplified version - in practice would use SVM or similar
        let labeled: Vec<_> = self
            .boundary_samples
            .iter()
            .filter_map(|(s, l)| l.map(|label| (s, label)))
            .collect();

        if labeled.len() < 2 {
            return Ok(()); // Not enough data
        }

        // Placeholder for actual refinement logic
        // Would typically use margin-based learning or similar
        Ok(())
    }

    /// Get the current constraint
    pub fn get_constraint(&self) -> &LinearConstraint {
        &self.constraint
    }

    /// Get number of boundary samples
    pub fn num_boundary_samples(&self) -> usize {
        self.boundary_samples.len()
    }

    /// Get number of unlabeled samples
    pub fn num_unlabeled(&self) -> usize {
        self.boundary_samples
            .iter()
            .filter(|(_, l)| l.is_none())
            .count()
    }
}

/// Feedback-based constraint tuner
#[derive(Debug, Clone)]
pub struct FeedbackConstraintTuner {
    /// Current constraint
    constraint: LinearConstraint,
    /// Feedback history (violation amount, user satisfaction)
    feedback_history: Vec<(f32, f32)>, // (violation, satisfaction in [0, 1])
    /// Adaptation rate
    #[allow(dead_code)]
    adaptation_rate: f32,
    /// Target satisfaction level
    target_satisfaction: f32,
}

impl FeedbackConstraintTuner {
    /// Create a new feedback-based tuner
    pub fn new(
        initial_constraint: LinearConstraint,
        adaptation_rate: f32,
        target_satisfaction: f32,
    ) -> Self {
        Self {
            constraint: initial_constraint,
            feedback_history: Vec::new(),
            adaptation_rate,
            target_satisfaction,
        }
    }

    /// Add user feedback for a sample
    pub fn add_feedback(&mut self, sample: &Array1<f32>, satisfaction: f32) -> LogicResult<()> {
        let violation = self.constraint.violation(sample.as_slice().unwrap_or(&[]));
        self.feedback_history.push((violation, satisfaction));

        // Tune constraint based on feedback
        self.tune()?;

        Ok(())
    }

    /// Tune constraint based on accumulated feedback
    fn tune(&mut self) -> LogicResult<()> {
        if self.feedback_history.len() < 5 {
            return Ok(()); // Need more data
        }

        // Compute average satisfaction
        let avg_satisfaction: f32 = self.feedback_history.iter().map(|(_, s)| s).sum::<f32>()
            / self.feedback_history.len() as f32;

        // If satisfaction is below target, adjust constraint
        let satisfaction_gap = self.target_satisfaction - avg_satisfaction;

        if satisfaction_gap.abs() > 0.1 {
            // Significant gap - adjust constraint tightness
            // Positive gap means we need to be less strict
            // This is a placeholder for actual tuning logic
            let _ = satisfaction_gap; // TODO: implement actual tuning
        }

        Ok(())
    }

    /// Get the current constraint
    pub fn get_constraint(&self) -> &LinearConstraint {
        &self.constraint
    }

    /// Get average satisfaction from recent feedback
    pub fn average_satisfaction(&self) -> f32 {
        if self.feedback_history.is_empty() {
            return 0.0;
        }

        self.feedback_history.iter().map(|(_, s)| s).sum::<f32>()
            / self.feedback_history.len() as f32
    }

    /// Get number of feedback samples
    pub fn num_feedback_samples(&self) -> usize {
        self.feedback_history.len()
    }
}

/// Unified online learning system combining multiple strategies
#[derive(Debug, Clone)]
pub struct OnlineLearningSystem {
    /// Incremental learner
    incremental_learner: OnlineConstraintLearner,
    /// Anomaly detector
    anomaly_detector: AnomalyBasedConstraintDiscovery,
    /// Active learner
    active_learner: ActiveConstraintBoundaryLearner,
    /// Feedback tuner
    feedback_tuner: FeedbackConstraintTuner,
    /// Enable/disable each component
    use_incremental: bool,
    use_anomaly: bool,
    use_active: bool,
    use_feedback: bool,
}

impl OnlineLearningSystem {
    /// Create a new online learning system
    pub fn new(initial_constraint: LinearConstraint) -> Self {
        Self {
            incremental_learner: OnlineConstraintLearner::new(
                initial_constraint.clone(),
                0.01,
                1000,
            ),
            anomaly_detector: AnomalyBasedConstraintDiscovery::new(1000, 3.0),
            active_learner: ActiveConstraintBoundaryLearner::new(
                initial_constraint.clone(),
                0.1,
                100,
            ),
            feedback_tuner: FeedbackConstraintTuner::new(initial_constraint, 0.01, 0.8),
            use_incremental: true,
            use_anomaly: true,
            use_active: true,
            use_feedback: true,
        }
    }

    /// Process a new labeled sample
    pub fn process_labeled_sample(
        &mut self,
        sample: Array1<f32>,
        is_feasible: bool,
    ) -> LogicResult<()> {
        if self.use_incremental {
            self.incremental_learner
                .observe(sample.clone(), is_feasible)?;
        }

        if self.use_active {
            self.active_learner
                .add_labeled_sample(sample.clone(), is_feasible);
        }

        if is_feasible && self.use_anomaly {
            self.anomaly_detector.add_normal_sample(sample);
        }

        Ok(())
    }

    /// Process an unlabeled sample (for anomaly detection and active learning)
    pub fn process_unlabeled_sample(&mut self, sample: Array1<f32>) {
        if self.use_anomaly {
            self.anomaly_detector.detect_anomaly(&sample);
        }

        if self.use_active {
            self.active_learner.add_unlabeled_sample(sample);
        }
    }

    /// Add user feedback
    pub fn add_feedback(&mut self, sample: &Array1<f32>, satisfaction: f32) -> LogicResult<()> {
        if self.use_feedback {
            self.feedback_tuner.add_feedback(sample, satisfaction)?;
        }
        Ok(())
    }

    /// Get the current best constraint estimate
    pub fn get_best_constraint(&self) -> &LinearConstraint {
        // Use the incremental learner's constraint as primary
        // Could be enhanced to ensemble multiple learned constraints
        self.incremental_learner.get_constraint()
    }

    /// Get confidence in current constraint
    pub fn confidence(&self) -> f32 {
        self.incremental_learner.confidence()
    }

    /// Get discovered anomaly-based constraints
    pub fn discovered_constraints(&self) -> &[LinearConstraint] {
        self.anomaly_detector.discovered_constraints()
    }

    /// Get next sample to query (active learning)
    pub fn query_next(&self) -> Option<Array1<f32>> {
        if self.use_active {
            self.active_learner.query_next()
        } else {
            None
        }
    }

    /// Enable/disable components
    pub fn set_use_incremental(&mut self, use_it: bool) {
        self.use_incremental = use_it;
    }

    pub fn set_use_anomaly(&mut self, use_it: bool) {
        self.use_anomaly = use_it;
    }

    pub fn set_use_active(&mut self, use_it: bool) {
        self.use_active = use_it;
    }

    pub fn set_use_feedback(&mut self, use_it: bool) {
        self.use_feedback = use_it;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_online_learner_basic() -> LogicResult<()> {
        let constraint = LinearConstraint::less_eq(vec![1.0], 5.0);
        let mut learner = OnlineConstraintLearner::new(constraint, 0.1, 100);

        // Add some observations
        learner.observe(Array1::from_vec(vec![3.0]), true)?; // Feasible
        learner.observe(Array1::from_vec(vec![7.0]), false)?; // Infeasible

        assert_eq!(learner.update_count(), 2);
        assert!(learner.confidence() > 0.0);

        Ok(())
    }

    #[test]
    fn test_anomaly_detection() {
        let mut detector = AnomalyBasedConstraintDiscovery::new(100, 3.0);

        // Add normal samples
        for _ in 0..20 {
            detector.add_normal_sample(Array1::from_vec(vec![5.0, 10.0]));
        }

        // Detect anomaly
        let is_anomaly = detector.detect_anomaly(&Array1::from_vec(vec![50.0, 100.0]));
        assert!(is_anomaly);
    }

    #[test]
    fn test_active_learning() {
        let constraint = LinearConstraint::less_eq(vec![1.0], 5.0);
        let mut learner = ActiveConstraintBoundaryLearner::new(constraint, 1.0, 100);

        learner.add_unlabeled_sample(Array1::from_vec(vec![4.9])); // Near boundary
        learner.add_unlabeled_sample(Array1::from_vec(vec![10.0])); // Far from boundary

        assert_eq!(learner.num_unlabeled(), 1); // Only near-boundary sample added
    }

    #[test]
    fn test_feedback_tuner() -> LogicResult<()> {
        let constraint = LinearConstraint::less_eq(vec![1.0], 5.0);
        let mut tuner = FeedbackConstraintTuner::new(constraint, 0.1, 0.8);

        tuner.add_feedback(&Array1::from_vec(vec![3.0]), 0.9)?; // High satisfaction
        tuner.add_feedback(&Array1::from_vec(vec![4.0]), 0.7)?; // Medium satisfaction

        assert_eq!(tuner.num_feedback_samples(), 2);
        assert!(tuner.average_satisfaction() > 0.0);

        Ok(())
    }

    #[test]
    fn test_online_learning_system() -> LogicResult<()> {
        let constraint = LinearConstraint::less_eq(vec![1.0], 5.0);
        let mut system = OnlineLearningSystem::new(constraint);

        system.process_labeled_sample(Array1::from_vec(vec![3.0]), true)?;
        system.process_unlabeled_sample(Array1::from_vec(vec![4.5]));
        system.add_feedback(&Array1::from_vec(vec![3.5]), 0.9)?;

        assert!(system.confidence() > 0.0);

        Ok(())
    }
}