tracktor 0.4.1

Multi-target tracking with random finite sets
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
//! Hungarian Algorithm for Optimal Assignment
//!
//! Implementation of the Hungarian (Kuhn-Munkres) algorithm for solving
//! the linear assignment problem in O(n³) time.

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use crate::TracktorError;

/// Result of an assignment problem.
#[derive(Debug, Clone, PartialEq)]
pub struct Assignment {
    /// Assignment mapping: row i is assigned to column assignment[i]
    /// None means the row is unassigned
    #[cfg(feature = "alloc")]
    pub mapping: Vec<Option<usize>>,
    /// Total cost of the assignment
    pub cost: f64,
}

#[cfg(feature = "alloc")]
impl Assignment {
    /// Creates a new assignment with the given mapping and cost.
    pub fn new(mapping: Vec<Option<usize>>, cost: f64) -> Self {
        Self { mapping, cost }
    }

    /// Returns the number of assigned pairs.
    pub fn num_assigned(&self) -> usize {
        self.mapping.iter().filter(|x| x.is_some()).count()
    }

    /// Returns an iterator over (row, col) pairs.
    pub fn pairs(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        self.mapping
            .iter()
            .enumerate()
            .filter_map(|(row, col)| col.map(|c| (row, c)))
    }
}

/// Cost matrix for assignment problems.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
pub struct CostMatrix {
    /// Row-major cost data
    data: Vec<f64>,
    /// Number of rows
    rows: usize,
    /// Number of columns
    cols: usize,
}

#[cfg(feature = "alloc")]
impl CostMatrix {
    /// Creates a cost matrix from row-major data.
    pub fn from_vec(data: Vec<f64>, rows: usize, cols: usize) -> Result<Self, TracktorError> {
        if data.len() != rows * cols {
            return Err(TracktorError::AssignmentFailed);
        }
        Ok(Self { data, rows, cols })
    }

    /// Creates a cost matrix filled with a value.
    pub fn filled(rows: usize, cols: usize, value: f64) -> Self {
        Self {
            data: vec![value; rows * cols],
            rows,
            cols,
        }
    }

    /// Creates a zero-filled cost matrix.
    pub fn zeros(rows: usize, cols: usize) -> Self {
        Self::filled(rows, cols, 0.0)
    }

    /// Returns the number of rows.
    pub fn rows(&self) -> usize {
        self.rows
    }

    /// Returns the number of columns.
    pub fn cols(&self) -> usize {
        self.cols
    }

    /// Gets the cost at (row, col).
    pub fn get(&self, row: usize, col: usize) -> f64 {
        self.data[row * self.cols + col]
    }

    /// Sets the cost at (row, col).
    pub fn set(&mut self, row: usize, col: usize, value: f64) {
        self.data[row * self.cols + col] = value;
    }

    /// Returns a mutable reference to the cost at (row, col).
    pub fn get_mut(&mut self, row: usize, col: usize) -> &mut f64 {
        &mut self.data[row * self.cols + col]
    }
}

/// Solves the linear assignment problem using the Hungarian algorithm.
///
/// Given a cost matrix C, finds an assignment that minimizes the total cost.
/// This is a correct implementation of the Kuhn-Munkres algorithm.
///
/// # Arguments
/// - `cost`: The cost matrix (rows = workers, columns = jobs)
///
/// # Returns
/// An `Assignment` with the optimal row-to-column mapping.
#[cfg(feature = "alloc")]
pub fn hungarian(cost: &CostMatrix) -> Result<Assignment, TracktorError> {
    let n_rows = cost.rows();
    let n_cols = cost.cols();

    if n_rows == 0 || n_cols == 0 {
        return Ok(Assignment::new(vec![], 0.0));
    }

    // Make the matrix square by padding with large values
    let n = n_rows.max(n_cols);
    let large = 1e20_f64;

    let mut matrix = vec![large; n * n];
    for i in 0..n_rows {
        for j in 0..n_cols {
            matrix[i * n + j] = cost.get(i, j);
        }
    }

    // Potential vectors for rows and columns (dual variables)
    let mut u = vec![0.0_f64; n]; // Row potentials
    let mut v = vec![0.0_f64; n]; // Column potentials

    // Assignment arrays
    let mut row_assignment: Vec<Option<usize>> = vec![None; n];
    let mut col_assignment: Vec<Option<usize>> = vec![None; n];

    // Process each row
    for i in 0..n {
        // Start augmenting path from row i
        let mut min_to = vec![f64::INFINITY; n]; // Minimum reduced cost to reach each column
        let mut way = vec![None::<usize>; n]; // Previous column in augmenting path
        let mut used = vec![false; n]; // Columns visited in this iteration

        let mut cur_row = i;
        let mut cur_col: Option<usize> = None;

        // Find augmenting path using Dijkstra-like approach
        loop {
            let mut min_val = f64::INFINITY;
            let mut min_col = 0;

            // Find minimum reduced cost among unvisited columns
            for j in 0..n {
                if used[j] {
                    continue;
                }

                let reduced_cost = matrix[cur_row * n + j] - u[cur_row] - v[j];

                if reduced_cost < min_to[j] {
                    min_to[j] = reduced_cost;
                    way[j] = cur_col;
                }

                if min_to[j] < min_val {
                    min_val = min_to[j];
                    min_col = j;
                }
            }

            // Update potentials
            for j in 0..n {
                if used[j] {
                    u[col_assignment[j].unwrap()] += min_val;
                    v[j] -= min_val;
                } else {
                    min_to[j] -= min_val;
                }
            }
            u[i] += min_val;

            // Move to next column
            used[min_col] = true;
            cur_col = Some(min_col);

            // Check if this column is unassigned
            if col_assignment[min_col].is_none() {
                break;
            }

            // Follow assignment to next row
            cur_row = col_assignment[min_col].unwrap();
        }

        // Trace back augmenting path and update assignments
        while let Some(col) = cur_col {
            let prev_col = way[col];

            if let Some(pc) = prev_col {
                // Get the row that was assigned to prev_col
                col_assignment[col] = col_assignment[pc];
            } else {
                // This is the start of the path
                col_assignment[col] = Some(i);
            }

            cur_col = prev_col;
        }
    }

    // Build row_assignment from col_assignment
    for (j, col_asgn) in col_assignment.iter().enumerate().take(n) {
        if let Some(i) = col_asgn {
            row_assignment[*i] = Some(j);
        }
    }

    // Calculate total cost and build result
    let mut total_cost = 0.0;
    let mut result_mapping = Vec::with_capacity(n_rows);

    for (i, row_asgn) in row_assignment.iter().enumerate().take(n_rows) {
        if let Some(j) = row_asgn {
            if *j < n_cols {
                total_cost += cost.get(i, *j);
                result_mapping.push(Some(*j));
            } else {
                result_mapping.push(None);
            }
        } else {
            result_mapping.push(None);
        }
    }

    Ok(Assignment::new(result_mapping, total_cost))
}

/// Solves the assignment problem with a gating threshold.
///
/// Assignments with cost above the threshold are not made.
#[cfg(feature = "alloc")]
pub fn hungarian_gated(
    cost: &CostMatrix,
    gate_threshold: f64,
) -> Result<Assignment, TracktorError> {
    // Create a gated cost matrix
    let mut gated = CostMatrix::zeros(cost.rows(), cost.cols());
    let large = 1e20_f64;

    for i in 0..cost.rows() {
        for j in 0..cost.cols() {
            let c = cost.get(i, j);
            if c <= gate_threshold {
                gated.set(i, j, c);
            } else {
                gated.set(i, j, large);
            }
        }
    }

    let mut result = hungarian(&gated)?;

    // Filter out assignments above threshold
    for i in 0..result.mapping.len() {
        if let Some(j) = result.mapping[i] {
            if cost.get(i, j) > gate_threshold {
                result.mapping[i] = None;
            }
        }
    }

    // Recalculate cost
    result.cost = result.pairs().map(|(i, j)| cost.get(i, j)).sum();

    Ok(result)
}

/// Auction algorithm for assignment (alternative to Hungarian).
///
/// Better for sparse problems or when approximate solutions are acceptable.
#[cfg(feature = "alloc")]
pub fn auction(
    cost: &CostMatrix,
    epsilon: f64,
    max_iter: usize,
) -> Result<Assignment, TracktorError> {
    let n_rows = cost.rows();
    let n_cols = cost.cols();

    if n_rows == 0 || n_cols == 0 {
        return Ok(Assignment::new(vec![], 0.0));
    }

    // Initialize prices and assignments
    let mut prices = vec![0.0; n_cols];
    let mut row_assignment: Vec<Option<usize>> = vec![None; n_rows];
    let mut col_assignment: Vec<Option<usize>> = vec![None; n_cols];

    for _ in 0..max_iter {
        let mut any_unassigned = false;

        for i in 0..n_rows {
            if row_assignment[i].is_some() {
                continue;
            }
            any_unassigned = true;

            // Find best and second-best columns for row i
            let mut best_j = 0;
            let mut best_value = f64::NEG_INFINITY;
            let mut second_best = f64::NEG_INFINITY;

            for (j, price) in prices.iter().enumerate().take(n_cols) {
                let value = -cost.get(i, j) - price; // Negative because we minimize
                if value > best_value {
                    second_best = best_value;
                    best_value = value;
                    best_j = j;
                } else if value > second_best {
                    second_best = value;
                }
            }

            // Bid
            let bid_increment = best_value - second_best + epsilon;
            prices[best_j] += bid_increment;

            // Update assignments
            if let Some(prev_i) = col_assignment[best_j] {
                row_assignment[prev_i] = None;
            }
            row_assignment[i] = Some(best_j);
            col_assignment[best_j] = Some(i);
        }

        if !any_unassigned {
            break;
        }
    }

    // Calculate total cost
    let total_cost: f64 = row_assignment
        .iter()
        .enumerate()
        .filter_map(|(i, j)| j.map(|j| cost.get(i, j)))
        .sum();

    Ok(Assignment::new(row_assignment, total_cost))
}

// ============================================================================
// Murty's Algorithm for K-Best Assignments
// ============================================================================

/// A node in Murty's partition tree.
///
/// Each node represents a constrained assignment problem where some
/// assignments are required (fixed) and others are forbidden (excluded).
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
struct MurtyNode {
    /// The assignment solution for this node
    assignment: Assignment,
    /// Row indices that have fixed assignments (row -> col)
    fixed: Vec<(usize, usize)>,
    /// Excluded assignments (row, col) that cannot be made
    excluded: Vec<(usize, usize)>,
    /// The row index up to which we've partitioned
    partition_row: usize,
}

#[cfg(feature = "alloc")]
impl MurtyNode {
    fn new(assignment: Assignment) -> Self {
        Self {
            assignment,
            fixed: Vec::new(),
            excluded: Vec::new(),
            partition_row: 0,
        }
    }

    fn with_constraints(
        assignment: Assignment,
        fixed: Vec<(usize, usize)>,
        excluded: Vec<(usize, usize)>,
        partition_row: usize,
    ) -> Self {
        Self {
            assignment,
            fixed,
            excluded,
            partition_row,
        }
    }
}

/// Solves a constrained assignment problem.
///
/// Some assignments are required (fixed) and some are forbidden (excluded).
/// Returns None if no valid assignment exists.
#[cfg(feature = "alloc")]
fn hungarian_constrained(
    cost: &CostMatrix,
    fixed: &[(usize, usize)],
    excluded: &[(usize, usize)],
) -> Option<Assignment> {
    let n_rows = cost.rows();
    let n_cols = cost.cols();

    if n_rows == 0 || n_cols == 0 {
        return Some(Assignment::new(vec![], 0.0));
    }

    // Check for conflicting constraints
    for &(r1, c1) in fixed {
        for &(r2, c2) in fixed {
            if r1 != r2 && c1 == c2 {
                // Two different rows fixed to same column - infeasible
                return None;
            }
        }
        // Check if fixed assignment is also excluded
        if excluded.contains(&(r1, c1)) {
            return None;
        }
    }

    // Create a modified cost matrix
    let large = 1e20_f64;
    let mut modified = CostMatrix::zeros(n_rows, n_cols);

    for i in 0..n_rows {
        for j in 0..n_cols {
            modified.set(i, j, cost.get(i, j));
        }
    }

    // Set excluded assignments to large cost
    for &(row, col) in excluded {
        if row < n_rows && col < n_cols {
            modified.set(row, col, large);
        }
    }

    // For fixed assignments, we need to ensure they're selected
    // We do this by setting all other costs in that row/column to large
    for &(row, col) in fixed {
        if row < n_rows && col < n_cols {
            // Set all other columns in this row to large cost
            for j in 0..n_cols {
                if j != col {
                    modified.set(row, j, large);
                }
            }
            // Set all other rows in this column to large cost
            for i in 0..n_rows {
                if i != row {
                    modified.set(i, col, large);
                }
            }
        }
    }

    // Solve using standard Hungarian
    let result = hungarian(&modified).ok()?;

    // Verify all fixed assignments were made
    for &(row, col) in fixed {
        if row < n_rows && result.mapping.get(row).copied().flatten() != Some(col) {
            return None; // Fixed assignment not satisfied
        }
    }

    // Verify no excluded assignments were made
    for &(row, col) in excluded {
        if row < n_rows && result.mapping.get(row).copied().flatten() == Some(col) {
            return None; // Excluded assignment was made
        }
    }

    // Recalculate cost using original cost matrix
    let real_cost: f64 = result
        .mapping
        .iter()
        .enumerate()
        .filter_map(|(i, &j)| j.filter(|&jj| jj < n_cols).map(|jj| cost.get(i, jj)))
        .sum();

    Some(Assignment::new(result.mapping, real_cost))
}

/// Finds the k-best assignments using Murty's algorithm.
///
/// Murty's algorithm partitions the solution space to efficiently find
/// the k best solutions without exhaustive enumeration.
///
/// # Arguments
/// - `cost`: The cost matrix
/// - `k`: The number of best assignments to find
///
/// # Returns
/// A vector of up to k assignments, sorted by cost (ascending).
#[cfg(feature = "alloc")]
pub fn murty_k_best(cost: &CostMatrix, k: usize) -> Vec<Assignment> {
    use alloc::collections::BinaryHeap;
    use core::cmp::Ordering;

    if k == 0 {
        return Vec::new();
    }

    // Wrapper for BinaryHeap (max-heap, but we want min-heap for costs)
    #[derive(Debug)]
    struct HeapNode(MurtyNode);

    impl PartialEq for HeapNode {
        fn eq(&self, other: &Self) -> bool {
            self.0.assignment.cost == other.0.assignment.cost
        }
    }

    impl Eq for HeapNode {}

    impl PartialOrd for HeapNode {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }

    impl Ord for HeapNode {
        fn cmp(&self, other: &Self) -> Ordering {
            // Reverse ordering for min-heap behavior
            other
                .0
                .assignment
                .cost
                .partial_cmp(&self.0.assignment.cost)
                .unwrap_or(Ordering::Equal)
        }
    }

    let mut results: Vec<Assignment> = Vec::with_capacity(k);
    let mut heap: BinaryHeap<HeapNode> = BinaryHeap::new();

    // Get the optimal assignment
    let best = match hungarian(cost) {
        Ok(a) => a,
        Err(_) => return Vec::new(),
    };

    // Add root node to heap
    heap.push(HeapNode(MurtyNode::new(best)));

    while results.len() < k {
        // Get the best unexpanded node
        let node = match heap.pop() {
            Some(HeapNode(n)) => n,
            None => break, // No more solutions
        };

        // Add this solution to results
        results.push(node.assignment.clone());

        if results.len() >= k {
            break;
        }

        // Generate child nodes by partitioning
        let n_rows = cost.rows();
        let mapping = &node.assignment.mapping;

        // For each assigned pair starting from partition_row, create a child
        // where that assignment is excluded
        for row in node.partition_row..n_rows {
            if let Some(col) = mapping[row] {
                // Create child node where (row, col) is excluded
                let mut child_fixed = node.fixed.clone();
                let mut child_excluded = node.excluded.clone();

                // Fix all assignments before this row (same as in current solution)
                for (prev_row, prev_col) in mapping
                    .iter()
                    .enumerate()
                    .take(row)
                    .skip(node.partition_row)
                    .filter_map(|(r, c)| c.map(|col| (r, col)))
                {
                    if !child_fixed.iter().any(|&(r, _)| r == prev_row) {
                        child_fixed.push((prev_row, prev_col));
                    }
                }

                // Exclude the current (row, col) assignment
                child_excluded.push((row, col));

                // Solve the constrained problem
                if let Some(child_assignment) =
                    hungarian_constrained(cost, &child_fixed, &child_excluded)
                {
                    heap.push(HeapNode(MurtyNode::with_constraints(
                        child_assignment,
                        child_fixed,
                        child_excluded,
                        row,
                    )));
                }
            }
        }
    }

    results
}

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

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_simple() {
        // Simple 3x3 cost matrix
        let cost =
            CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], 3, 3).unwrap();

        let result = hungarian(&cost).unwrap();

        // Optimal assignment: 0->2, 1->1, 2->0 (cost = 3+5+7 = 15)
        assert_eq!(result.num_assigned(), 3);
        assert!(
            (result.cost - 15.0).abs() < 0.01,
            "Expected cost 15.0, got {}",
            result.cost
        );
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_asymmetric() {
        // Cost matrix where diagonal is not optimal
        let cost =
            CostMatrix::from_vec(vec![10.0, 5.0, 13.0, 3.0, 15.0, 8.0, 7.0, 4.0, 12.0], 3, 3)
                .unwrap();

        let result = hungarian(&cost).unwrap();

        // Optimal: 0->1 (5), 1->0 (3), 2->2 (12) = 20
        // or: 0->2 (13), 1->0 (3), 2->1 (4) = 20
        assert_eq!(result.num_assigned(), 3);
        assert!(
            (result.cost - 20.0).abs() < 0.01,
            "Expected cost 20.0, got {}",
            result.cost
        );
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_rectangular() {
        // More rows than columns
        let cost = CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2).unwrap();

        let result = hungarian(&cost).unwrap();

        // Only 2 assignments possible
        assert!(result.num_assigned() <= 2);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_gated() {
        let cost = CostMatrix::from_vec(vec![1.0, 100.0, 100.0, 2.0], 2, 2).unwrap();

        let result = hungarian_gated(&cost, 10.0).unwrap();

        // Both assignments should be made (1.0 and 2.0 are below threshold)
        assert_eq!(result.num_assigned(), 2);
        assert!((result.cost - 3.0).abs() < 0.1);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_auction() {
        let cost =
            CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], 3, 3).unwrap();

        let result = auction(&cost, 0.1, 100).unwrap();

        assert_eq!(result.num_assigned(), 3);
        // Auction is epsilon-optimal, should be within 3*epsilon of optimal (15.0)
        assert!(
            result.cost >= 14.5 && result.cost <= 15.5,
            "Auction cost {} not within bounds",
            result.cost
        );
    }

    // ============================================================================
    // Murty's Algorithm Tests
    // ============================================================================

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_k1() {
        // With k=1, should return same result as Hungarian
        let cost =
            CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], 3, 3).unwrap();

        let hungarian_result = hungarian(&cost).unwrap();
        let murty_results = murty_k_best(&cost, 1);

        assert_eq!(murty_results.len(), 1);
        assert!(
            (murty_results[0].cost - hungarian_result.cost).abs() < 0.01,
            "Murty k=1 should match Hungarian: {} vs {}",
            murty_results[0].cost,
            hungarian_result.cost
        );
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_k3_ordering() {
        // Test that results are returned in cost-ascending order
        let cost =
            CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], 3, 3).unwrap();

        let results = murty_k_best(&cost, 3);

        assert!(results.len() >= 2, "Should find at least 2 solutions");

        // Verify costs are non-decreasing
        for i in 1..results.len() {
            assert!(
                results[i].cost >= results[i - 1].cost - 0.001,
                "Results should be ordered: cost[{}]={} >= cost[{}]={}",
                i,
                results[i].cost,
                i - 1,
                results[i - 1].cost
            );
        }
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_unique_solutions() {
        // Test that all returned solutions are unique
        let cost =
            CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], 3, 3).unwrap();

        let results = murty_k_best(&cost, 5);

        // Check for duplicates
        for i in 0..results.len() {
            for j in (i + 1)..results.len() {
                assert_ne!(
                    results[i].mapping, results[j].mapping,
                    "Solutions {} and {} should be different",
                    i, j
                );
            }
        }
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_2x2() {
        // Simple 2x2 case where we can verify all solutions
        // Cost matrix:
        // [1, 10]
        // [10, 2]
        let cost = CostMatrix::from_vec(vec![1.0, 10.0, 10.0, 2.0], 2, 2).unwrap();

        let results = murty_k_best(&cost, 2);

        assert_eq!(results.len(), 2);

        // Best: (0,0) + (1,1) = 1 + 2 = 3
        assert!(
            (results[0].cost - 3.0).abs() < 0.01,
            "Best should be 3.0, got {}",
            results[0].cost
        );

        // Second best: (0,1) + (1,0) = 10 + 10 = 20
        assert!(
            (results[1].cost - 20.0).abs() < 0.01,
            "Second best should be 20.0, got {}",
            results[1].cost
        );
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_3x3_comprehensive() {
        // 3x3 case with known solutions
        // Cost matrix:
        // [1, 2, 3]
        // [4, 5, 6]
        // [7, 8, 9]
        let cost =
            CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], 3, 3).unwrap();

        let results = murty_k_best(&cost, 6);

        // All 6 possible assignments for 3x3:
        // Optimal: 0->2, 1->1, 2->0 = 3+5+7 = 15
        assert!(
            (results[0].cost - 15.0).abs() < 0.01,
            "Optimal should be 15.0, got {}",
            results[0].cost
        );

        // Verify all solutions are valid assignments
        for (idx, result) in results.iter().enumerate() {
            let mut cols_used: Vec<bool> = vec![false; 3];
            for &col in &result.mapping {
                if let Some(c) = col {
                    assert!(
                        !cols_used[c],
                        "Solution {} has duplicate column assignment",
                        idx
                    );
                    cols_used[c] = true;
                }
            }
        }
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_rectangular() {
        // Rectangular matrix (more rows than columns)
        let cost = CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2).unwrap();

        let results = murty_k_best(&cost, 3);

        assert!(!results.is_empty(), "Should find at least one solution");

        // Each solution should assign at most 2 rows (since we have 2 columns)
        for result in &results {
            let assigned = result.mapping.iter().filter(|x| x.is_some()).count();
            assert!(
                assigned <= 2,
                "Should assign at most 2 rows, got {}",
                assigned
            );
        }
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_empty() {
        let cost = CostMatrix::zeros(0, 0);
        let results = murty_k_best(&cost, 5);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].cost, 0.0);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_murty_k0() {
        let cost = CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2).unwrap();
        let results = murty_k_best(&cost, 0);
        assert!(results.is_empty());
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_constrained_fixed() {
        // Test that fixed constraints work
        let cost = CostMatrix::from_vec(vec![1.0, 10.0, 10.0, 2.0], 2, 2).unwrap();

        // Without constraints: optimal is (0,0)+(1,1) = 3
        // Force (0,1) to be selected
        let fixed = vec![(0, 1)];
        let excluded = vec![];

        let result = hungarian_constrained(&cost, &fixed, &excluded);

        assert!(result.is_some());
        let result = result.unwrap();
        assert_eq!(result.mapping[0], Some(1), "Row 0 should be fixed to col 1");
        // Cost should be 10 + 10 = 20 (forced suboptimal)
        assert!(
            (result.cost - 20.0).abs() < 0.01,
            "Cost should be 20.0, got {}",
            result.cost
        );
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_constrained_excluded() {
        // Test that excluded constraints work
        let cost = CostMatrix::from_vec(vec![1.0, 10.0, 10.0, 2.0], 2, 2).unwrap();

        // Without constraints: optimal is (0,0)+(1,1) = 3
        // Exclude (0,0) - forces second best
        let fixed = vec![];
        let excluded = vec![(0, 0)];

        let result = hungarian_constrained(&cost, &fixed, &excluded);

        assert!(result.is_some());
        let result = result.unwrap();
        assert_ne!(
            result.mapping[0],
            Some(0),
            "Row 0 should not be assigned to col 0"
        );
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hungarian_constrained_infeasible() {
        // Test that infeasible constraints return None
        let cost = CostMatrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2).unwrap();

        // Fix both rows to same column - infeasible
        let fixed = vec![(0, 0), (1, 0)];
        let excluded = vec![];

        let result = hungarian_constrained(&cost, &fixed, &excluded);

        assert!(result.is_none(), "Should be infeasible");
    }
}