clump 0.5.4

Dense clustering primitives (k-means, DBSCAN, HDBSCAN, EVoC, COP-Kmeans, DenStream, correlation clustering)
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
//! Constrained clustering algorithms.
//!
//! Semi-supervised clustering where pairwise constraints guide the assignment.
//! Constraints come from domain knowledge or limited labeled data: "these two
//! items must be together" or "these two items must be apart."
//!
//! # COP-Kmeans (Wagstaff et al., 2001)
//!
//! COP-Kmeans modifies the assignment step of standard k-means to respect
//! pairwise constraints. During each iteration:
//!
//! 1. **Assign**: For each point, find the nearest centroid whose assignment
//!    would not violate any cannot-link constraint. If a must-link partner
//!    has already been assigned, the point must join the same cluster.
//! 2. **Update**: Recompute centroids as the mean of assigned points (identical
//!    to standard k-means).
//!
//! If no valid assignment exists for a point (all candidate clusters violate
//! some constraint), the algorithm returns [`Error::ConstraintViolation`].
//!
//! ## Constraint Types
//!
//! - **Must-link**: Two points must belong to the same cluster. Transitive:
//!   if (a, b) and (b, c) are must-linked, then a, b, c are all co-clustered.
//! - **Cannot-link**: Two points must belong to different clusters.
//!
//! ## Limitations
//!
//! - Constraint feasibility is NP-complete in general. COP-Kmeans uses a
//!   greedy assignment order, so it may fail even when a feasible solution
//!   exists under a different ordering.
//! - Like standard k-means, it assumes roughly spherical clusters and
//!   requires k to be specified.
//!
//! ## References
//!
//! Wagstaff, K. et al. (2001). "Constrained K-means Clustering with
//! Background Knowledge." ICML 2001.

use super::distance::{DistanceMetric, SquaredEuclidean};
use super::flat::DataRef;
use super::util;
use crate::error::{Error, Result};
use rand::prelude::*;

/// A pairwise constraint for semi-supervised clustering.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Constraint {
    /// These two points must be in the same cluster.
    MustLink(usize, usize),
    /// These two points must be in different clusters.
    CannotLink(usize, usize),
}

/// COP-Kmeans: constrained k-means clustering (Wagstaff et al., 2001).
///
/// Standard k-means with pairwise must-link and cannot-link constraints
/// enforced during the assignment step.
///
/// ```
/// use clump::cluster::constrained::{CopKmeans, Constraint};
///
/// let data = vec![
///     vec![0.0f32, 0.0],
///     vec![0.1, 0.1],
///     vec![10.0, 10.0],
///     vec![10.1, 10.1],
/// ];
///
/// // Force points 0 and 1 together, points 0 and 2 apart.
/// let constraints = vec![
///     Constraint::MustLink(0, 1),
///     Constraint::CannotLink(0, 2),
/// ];
///
/// let labels = CopKmeans::new(2)
///     .with_seed(42)
///     .fit_predict_constrained(&data, &constraints)
///     .unwrap();
///
/// assert_eq!(labels[0], labels[1]);
/// assert_ne!(labels[0], labels[2]);
/// ```
#[derive(Debug, Clone)]
pub struct CopKmeans<D: DistanceMetric = SquaredEuclidean> {
    /// Number of clusters.
    k: usize,
    /// Maximum iterations.
    max_iter: usize,
    /// Convergence tolerance.
    tol: f64,
    /// Random seed.
    seed: Option<u64>,
    /// Distance metric.
    metric: D,
}

impl CopKmeans<SquaredEuclidean> {
    /// Create a new COP-Kmeans clusterer with default squared Euclidean distance.
    ///
    /// # Panics
    ///
    /// Panics if `k == 0`.
    pub fn new(k: usize) -> Self {
        assert!(k > 0, "k must be at least 1");
        Self {
            k,
            max_iter: 100,
            tol: 1e-4,
            seed: None,
            metric: SquaredEuclidean,
        }
    }
}

impl<D: DistanceMetric> CopKmeans<D> {
    /// Create a new COP-Kmeans clusterer with a custom distance metric.
    ///
    /// # Panics
    ///
    /// Panics if `k == 0`.
    pub fn with_metric(k: usize, metric: D) -> Self {
        assert!(k > 0, "k must be at least 1");
        Self {
            k,
            max_iter: 100,
            tol: 1e-4,
            seed: None,
            metric,
        }
    }

    /// Set maximum iterations.
    pub fn with_max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set convergence tolerance.
    pub fn with_tol(mut self, tol: f64) -> Self {
        self.tol = tol;
        self
    }

    /// Set random seed for reproducibility.
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = Some(seed);
        self
    }

    /// Check whether assigning `point_idx` to `cluster` violates any cannot-link constraint.
    fn violates_cannot_link(
        point_idx: usize,
        cluster: usize,
        labels: &[Option<usize>],
        cannot_links: &[Vec<usize>],
    ) -> bool {
        for &other in &cannot_links[point_idx] {
            if labels[other] == Some(cluster) {
                return true;
            }
        }
        false
    }

    /// Find the cluster that a must-link partner has already been assigned to, if any.
    fn must_link_cluster(
        point_idx: usize,
        labels: &[Option<usize>],
        must_links: &[Vec<usize>],
    ) -> Option<usize> {
        for &other in &must_links[point_idx] {
            if let Some(c) = labels[other] {
                return Some(c);
            }
        }
        None
    }

    /// Compute transitive closure of must-link constraints via DFS.
    ///
    /// Returns groups of mutually must-linked indices.
    fn transitive_closure(adjacency: &[Vec<usize>], n: usize) -> Vec<Vec<usize>> {
        let mut visited = vec![false; n];
        let mut groups = Vec::new();

        for start in 0..n {
            if visited[start] || adjacency[start].is_empty() {
                continue;
            }

            let mut group = Vec::new();
            let mut stack = vec![start];

            while let Some(node) = stack.pop() {
                if visited[node] {
                    continue;
                }
                visited[node] = true;
                group.push(node);

                for &neighbor in &adjacency[node] {
                    if !visited[neighbor] {
                        stack.push(neighbor);
                    }
                }
            }

            groups.push(group);
        }

        groups
    }

    /// Assign all points under must-link / cannot-link constraints.
    ///
    /// Processes points in `order`, assigning each to the nearest valid cluster.
    /// Returns `labels[i] = Some(cluster_id)` for every point, or an error if
    /// no feasible assignment exists.
    fn constrained_assign(
        &self,
        data: &(impl DataRef + ?Sized),
        centroids: &[Vec<f32>],
        must_links: &[Vec<usize>],
        cannot_links: &[Vec<usize>],
        order: &[usize],
    ) -> Result<Vec<Option<usize>>> {
        let n = data.n();
        let mut labels: Vec<Option<usize>> = vec![None; n];
        // Pre-allocate candidate buffer outside the per-point loop.
        let mut candidates: Vec<(usize, f32)> = Vec::with_capacity(self.k);

        for &i in order {
            // If a must-link partner is already assigned, force that cluster.
            if let Some(forced) = Self::must_link_cluster(i, &labels, must_links) {
                if Self::violates_cannot_link(i, forced, &labels, cannot_links) {
                    return Err(Error::ConstraintViolation(format!(
                        "point {i}: must-link forces cluster {forced} but cannot-link forbids it"
                    )));
                }
                labels[i] = Some(forced);
                continue;
            }

            // Sort candidate clusters by distance (nearest first).
            candidates.clear();
            candidates
                .extend((0..self.k).map(|k| (k, self.metric.distance(data.row(i), &centroids[k]))));
            candidates.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

            // Pick nearest valid cluster.
            let mut assigned = false;
            for (k, _) in &candidates {
                if !Self::violates_cannot_link(i, *k, &labels, cannot_links) {
                    labels[i] = Some(*k);
                    assigned = true;
                    break;
                }
            }

            if !assigned {
                return Err(Error::ConstraintViolation(format!(
                    "point {i}: no valid cluster assignment exists"
                )));
            }
        }

        Ok(labels)
    }
}

impl<D: DistanceMetric> CopKmeans<D> {
    /// Fit the model with pairwise constraints.
    pub fn fit_predict_constrained(
        &self,
        data: &(impl DataRef + ?Sized),
        constraints: &[Constraint],
    ) -> Result<Vec<usize>> {
        if data.n() == 0 {
            return Err(Error::EmptyInput);
        }

        if self.k == 0 {
            return Err(Error::InvalidParameter {
                name: "k",
                message: "must be at least 1",
            });
        }

        let n = data.n();
        let d = data.d();

        if d == 0 {
            return Err(Error::InvalidParameter {
                name: "dimension",
                message: "must be at least 1",
            });
        }

        if self.k > n {
            return Err(Error::InvalidClusterCount {
                requested: self.k,
                n_items: n,
            });
        }

        // Validate dimensions.
        for i in 0..n {
            if data.row(i).len() != d {
                return Err(Error::DimensionMismatch {
                    expected: d,
                    found: data.row(i).len(),
                });
            }
        }

        util::validate_finite(data)?;

        // Validate constraint indices.
        for c in constraints {
            let (a, b) = match c {
                Constraint::MustLink(a, b) | Constraint::CannotLink(a, b) => (*a, *b),
            };
            if a >= n || b >= n {
                return Err(Error::InvalidParameter {
                    name: "constraint index",
                    message: "exceeds dataset size",
                });
            }
        }

        // Build adjacency lists for fast lookup.
        let mut must_links: Vec<Vec<usize>> = vec![Vec::new(); n];
        let mut cannot_links: Vec<Vec<usize>> = vec![Vec::new(); n];

        for c in constraints {
            match c {
                Constraint::MustLink(a, b) => {
                    must_links[*a].push(*b);
                    must_links[*b].push(*a);
                }
                Constraint::CannotLink(a, b) => {
                    cannot_links[*a].push(*b);
                    cannot_links[*b].push(*a);
                }
            }
        }

        // Compute transitive closure of must-link constraints via DFS.
        // After this, must_links[i] contains all points transitively must-linked
        // to i (not just direct neighbors). This makes the assignment step O(1)
        // for must-link lookups instead of requiring repeated traversal.
        let must_link_groups = Self::transitive_closure(&must_links, n);
        let mut must_links_closed: Vec<Vec<usize>> = vec![Vec::new(); n];
        for group in &must_link_groups {
            for &member in group {
                for &other in group {
                    if other != member {
                        must_links_closed[member].push(other);
                    }
                }
            }
        }
        let must_links = must_links_closed;

        // Check for contradictions: a pair that is both must-link and cannot-link.
        for c in constraints {
            if let Constraint::CannotLink(a, b) = c {
                if must_links[*a].contains(b) {
                    return Err(Error::ConstraintViolation(format!(
                        "points {} and {} are both must-linked (transitively) and cannot-linked",
                        a, b
                    )));
                }
            }
        }

        // Initialize RNG.
        let mut rng = match self.seed {
            Some(s) => StdRng::seed_from_u64(s),
            None => StdRng::from_os_rng(),
        };

        // Initialize centroids via k-means++.
        let mut centroids = util::kmeanspp_init(data, self.k, &self.metric, 2.0, &mut rng);

        // Pre-allocate working buffers outside iteration loop.
        let mut new_centroids = vec![vec![0.0f32; d]; self.k];
        let mut counts = vec![0usize; self.k];
        let mut sums_f64 = vec![vec![0.0f64; d]; self.k];
        let mut order: Vec<usize> = (0..n).collect();
        let effective_tol = (self.tol * util::mean_variance(data) * self.k as f64) as f32;

        for _ in 0..self.max_iter {
            // Assignment step (constrained).
            order.iter_mut().enumerate().for_each(|(i, v)| *v = i);
            order.shuffle(&mut rng);
            let labels =
                self.constrained_assign(data, &centroids, &must_links, &cannot_links, &order)?;

            // Update step: recompute centroids.
            for c in &mut new_centroids {
                c.fill(0.0);
            }
            counts.fill(0);

            // Accumulate in f64 for precision at large n.
            for s in &mut sums_f64 {
                s.fill(0.0);
            }
            for (i, label) in labels.iter().enumerate() {
                let k = label.expect("constrained_assign guarantees all labels are Some");
                let row = data.row(i);
                for j in 0..d {
                    sums_f64[k][j] += row[j] as f64;
                }
                counts[k] += 1;
            }

            for k in 0..self.k {
                if counts[k] > 0 {
                    let divisor = counts[k] as f64;
                    for j in 0..d {
                        new_centroids[k][j] = (sums_f64[k][j] / divisor) as f32;
                    }
                } else {
                    // Empty cluster: reinitialize randomly.
                    let idx = rng.random_range(0..n);
                    new_centroids[k] = data.row(idx).to_vec();
                }
            }

            // Spherical k-means: L2-normalize centroids for cosine distance.
            if self.metric.normalize_centroids() {
                for c in &mut new_centroids {
                    let norm: f32 = c.iter().map(|&x| x * x).sum::<f32>().sqrt();
                    if norm > f32::EPSILON {
                        for val in c.iter_mut() {
                            *val /= norm;
                        }
                    }
                }
            }

            // Convergence check.
            let shift: f32 = centroids
                .iter()
                .zip(new_centroids.iter())
                .flat_map(|(old, new)| old.iter().zip(new.iter()).map(|(a, b)| (a - b).powi(2)))
                .sum();

            std::mem::swap(&mut centroids, &mut new_centroids);

            if shift < effective_tol {
                break;
            }
        }

        // Final assignment with constraints.
        let mut order: Vec<usize> = (0..n).collect();
        order.shuffle(&mut rng);
        let labels =
            self.constrained_assign(data, &centroids, &must_links, &cannot_links, &order)?;

        Ok(labels
            .into_iter()
            .map(|l| l.expect("final constrained_assign guarantees all labels are Some"))
            .collect())
    }
}

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

    fn assert_autotraits<T: Send + Sync + Sized + Unpin>() {}

    #[test]
    fn cop_kmeans_is_send_sync() {
        assert_autotraits::<CopKmeans<SquaredEuclidean>>();
        assert_autotraits::<CopKmeans<super::super::distance::Euclidean>>();
    }
}

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

    #[test]
    fn must_link_forces_same_cluster() {
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        let constraints = vec![Constraint::MustLink(0, 1)];

        let labels = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints)
            .unwrap();

        assert_eq!(
            labels[0], labels[1],
            "must-linked points should share a cluster"
        );
    }

    #[test]
    fn cannot_link_forces_different_clusters() {
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        let constraints = vec![Constraint::CannotLink(0, 1)];

        let labels = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints)
            .unwrap();

        assert_ne!(
            labels[0], labels[1],
            "cannot-linked points should be in different clusters"
        );
    }

    #[test]
    fn must_link_transitive() {
        // If (0,1) must-link and (1,2) must-link, then 0,1,2 should all be co-clustered.
        let data = vec![
            vec![0.0f32, 0.0],
            vec![5.0, 5.0], // Midpoint -- would normally go to either cluster.
            vec![0.1, 0.1],
            vec![10.0, 10.0],
        ];

        let constraints = vec![Constraint::MustLink(0, 1), Constraint::MustLink(1, 2)];

        let labels = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints)
            .unwrap();

        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[1], labels[2]);
    }

    #[test]
    fn infeasible_constraints_return_error() {
        // Three points, k=2, all pairwise cannot-link: impossible.
        let data = vec![vec![0.0f32, 0.0], vec![1.0, 1.0], vec![2.0, 2.0]];

        let constraints = vec![
            Constraint::CannotLink(0, 1),
            Constraint::CannotLink(0, 2),
            Constraint::CannotLink(1, 2),
        ];

        let result = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints);

        assert!(
            result.is_err(),
            "infeasible constraints should return error"
        );
        if let Err(Error::ConstraintViolation(msg)) = result {
            assert!(
                msg.contains("no valid cluster"),
                "error message should mention no valid cluster: {msg}"
            );
        }
    }

    #[test]
    fn conflicting_must_and_cannot_link_error() {
        // (0,1) must-link AND (0,1) cannot-link: contradictory.
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        let constraints = vec![Constraint::MustLink(0, 1), Constraint::CannotLink(0, 1)];

        let result = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints);

        assert!(result.is_err(), "contradictory constraints should fail");
    }

    #[test]
    fn no_constraints_matches_kmeans_structure() {
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        let labels = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &[])
            .unwrap();

        // Well-separated data should still cluster correctly without constraints.
        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[2], labels[3]);
        assert_ne!(labels[0], labels[2]);
    }

    #[test]
    fn empty_input_error() {
        let result = CopKmeans::new(2)
            .with_seed(1)
            .fit_predict_constrained(&[] as &[Vec<f32>], &[]);
        assert!(result.is_err());
    }

    #[test]
    fn invalid_constraint_index_error() {
        let data = vec![vec![0.0f32, 0.0], vec![1.0, 1.0]];
        let constraints = vec![Constraint::MustLink(0, 5)]; // Index 5 out of bounds.

        let result = CopKmeans::new(2)
            .with_seed(1)
            .fit_predict_constrained(&data, &constraints);

        assert!(result.is_err());
    }

    #[test]
    fn with_custom_metric() {
        use crate::cluster::distance::Euclidean;

        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        let constraints = vec![Constraint::MustLink(0, 1), Constraint::CannotLink(0, 2)];

        let labels = CopKmeans::with_metric(2, Euclidean)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints)
            .unwrap();

        assert_eq!(labels[0], labels[1]);
        assert_ne!(labels[0], labels[2]);
    }

    /// Early contradiction detection: contradictory must-link + cannot-link
    /// should be caught before any iteration, not as a runtime failure.
    #[test]
    fn transitive_contradiction_detected_early() {
        // (0,1) must-link, (1,2) must-link => 0,1,2 transitively linked.
        // (0,2) cannot-link => contradicts the transitive must-link.
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![0.2, 0.2],
            vec![10.0, 10.0],
        ];

        let constraints = vec![
            Constraint::MustLink(0, 1),
            Constraint::MustLink(1, 2),
            Constraint::CannotLink(0, 2),
        ];

        let result = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &constraints);

        assert!(result.is_err(), "transitive contradiction should be caught");
        if let Err(Error::ConstraintViolation(msg)) = result {
            assert!(
                msg.contains("must-linked") && msg.contains("cannot-linked"),
                "error should mention both constraint types: {msg}"
            );
        }
    }

    /// Monotonicity: adding a must-link between two points already in the
    /// same cluster should not change the partition.
    #[test]
    fn monotonicity_same_cluster_must_link() {
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        // Without constraints.
        let labels_none = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &[])
            .unwrap();

        // Points 0 and 1 should already be co-clustered.
        assert_eq!(labels_none[0], labels_none[1]);

        // Adding a must-link between already-co-clustered points.
        let labels_with = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &[Constraint::MustLink(0, 1)])
            .unwrap();

        // Partition structure should be identical (labels may differ in absolute value).
        assert_eq!(
            labels_none[0] == labels_none[2],
            labels_with[0] == labels_with[2]
        );
        assert_eq!(
            labels_none[0] == labels_none[3],
            labels_with[0] == labels_with[3]
        );
    }

    #[test]
    fn deterministic_with_seed() {
        let data = vec![
            vec![0.0f32, 0.0],
            vec![0.1, 0.1],
            vec![5.0, 5.0],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
        ];

        let constraints = vec![Constraint::MustLink(0, 1), Constraint::CannotLink(0, 3)];

        let labels1 = CopKmeans::new(2)
            .with_seed(99)
            .fit_predict_constrained(&data, &constraints)
            .unwrap();

        let labels2 = CopKmeans::new(2)
            .with_seed(99)
            .fit_predict_constrained(&data, &constraints)
            .unwrap();

        assert_eq!(
            labels1, labels2,
            "same seed should produce identical results"
        );
    }

    #[test]
    fn nan_input_rejected() {
        let data = vec![vec![0.0, f32::NAN], vec![1.0, 1.0]];
        let result = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &[]);
        assert!(result.is_err());
    }

    #[test]
    fn inf_input_rejected() {
        let data = vec![vec![0.0, 0.0], vec![f32::INFINITY, 1.0]];
        let result = CopKmeans::new(2)
            .with_seed(42)
            .fit_predict_constrained(&data, &[]);
        assert!(result.is_err());
    }
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    /// Generate well-separated cluster data where constraints are satisfiable.
    fn separated_data_and_constraints() -> impl Strategy<Value = (Vec<Vec<f32>>, Vec<Constraint>)> {
        // 4 points: 2 near origin, 2 near (100, 100).
        // Must-link within each pair, cannot-link across.
        Just((
            vec![
                vec![0.0, 0.0],
                vec![0.1, 0.1],
                vec![100.0, 100.0],
                vec![100.1, 100.1],
            ],
            vec![
                Constraint::MustLink(0, 1),
                Constraint::MustLink(2, 3),
                Constraint::CannotLink(0, 2),
            ],
        ))
    }

    proptest! {
        #[test]
        fn must_links_satisfied((data, constraints) in separated_data_and_constraints()) {
            let labels = CopKmeans::new(2)
                .with_seed(42)
                .fit_predict_constrained(&data, &constraints)
                .unwrap();

            for c in &constraints {
                if let Constraint::MustLink(a, b) = c {
                    prop_assert_eq!(
                        labels[*a], labels[*b],
                        "must-link ({}, {}) violated", a, b
                    );
                }
            }
        }

        #[test]
        fn cannot_links_satisfied((data, constraints) in separated_data_and_constraints()) {
            let labels = CopKmeans::new(2)
                .with_seed(42)
                .fit_predict_constrained(&data, &constraints)
                .unwrap();

            for c in &constraints {
                if let Constraint::CannotLink(a, b) = c {
                    prop_assert_ne!(
                        labels[*a], labels[*b],
                        "cannot-link ({}, {}) violated", a, b
                    );
                }
            }
        }

        #[test]
        fn labels_in_range((data, constraints) in separated_data_and_constraints()) {
            let labels = CopKmeans::new(2)
                .with_seed(42)
                .fit_predict_constrained(&data, &constraints)
                .unwrap();

            for (i, &l) in labels.iter().enumerate() {
                prop_assert!(l < 2, "point {i}: label {l} >= k=2");
            }
        }
    }
}