oxigrid 0.1.2

Pure Rust Energy Systems Simulation & Optimization Library
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! Advanced power network partitioning algorithms.
//!
//! Provides multiple graph-partitioning strategies for decomposing a power
//! network into balanced sub-systems for distributed computation, parallel
//! simulation, or hierarchical control.
//!
//! # Algorithms
//!
//! | Method | Description |
//! |--------|-------------|
//! | `KernighanLin` | KL local-search bipartition, recursed for k-way |
//! | `Spectral` | Fiedler-vector bisection of the graph Laplacian |
//! | `Multilevel` | Coarsen → partition → uncoarsen refinement |
//! | `KMeansClustering` | k-means on bus indices (electrical-distance proxy) |
//! | `GeographicBased` | Round-robin assignment (coordinates unavailable) |
//!
//! # Graph modularity
//!
//! ```text
//! Q = (1/2m) Σ_{ij} [A_{ij} − k_i·k_j/(2m)] · δ(c_i, c_j)
//! ```
//!
//! # References
//!
//! - Kernighan & Lin, "An Efficient Heuristic Procedure for Partitioning
//!   Electrical Circuits", Bell System Tech. J. 1970
//! - Fiedler, "Algebraic Connectivity of Graphs", Czech. Math. J. 1973
//! - Hendrickson & Leland, "A Multilevel Algorithm for Partitioning Graphs",
//!   SC 1995

use serde::{Deserialize, Serialize};
use thiserror::Error;

// ─────────────────────────────────────────────────────────────────────────────
// Error type
// ─────────────────────────────────────────────────────────────────────────────

/// Errors from the network partitioning module.
#[derive(Debug, Error)]
pub enum PartitionError {
    /// Requested more partitions than buses.
    #[error("n_partitions ({n}) > n_buses ({buses})")]
    TooManyPartitions { n: usize, buses: usize },

    /// At least one bus must be present.
    #[error("network has no buses")]
    NoBuses,

    /// Number of partitions must be ≥ 2.
    #[error("n_partitions must be ≥ 2")]
    TooFewPartitions,

    /// Spectral computation failed (degenerate graph).
    #[error("spectral partitioning failed: {0}")]
    SpectralFailed(String),
}

// ─────────────────────────────────────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────────────────────────────────────

/// Which partitioning algorithm to apply.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PartitionMethod {
    /// Kernighan-Lin local-search bipartition (recursed for k > 2).
    KernighanLin,
    /// Spectral bisection using the Fiedler vector of the graph Laplacian.
    Spectral,
    /// Multilevel: coarsen → KL partition → refine.
    Multilevel,
    /// k-means clustering on bus indices as an electrical-distance proxy.
    KMeansClustering,
    /// Round-robin geographic assignment (used when no coordinates available).
    GeographicBased,
}

/// Which quantity should be balanced across partitions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BalanceCriterion {
    /// Equal number of buses per partition.
    EqualBuses,
    /// Equal total load \[MW\] per partition.
    EqualLoad,
    /// Equal total generation \[MW\] per partition.
    EqualGeneration,
    /// Equal net imbalance |P_gen − P_load| per partition.
    EqualImbalance,
}

/// Full configuration for the partitioner.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartitionConfig {
    /// Desired number of partitions.
    pub n_partitions: usize,
    /// Partitioning algorithm.
    pub method: PartitionMethod,
    /// Balance objective.
    pub balance_criterion: BalanceCriterion,
    /// Weight on minimising branch cuts (0–1).
    pub min_cut_weight: f64,
    /// Weight on load balance (0–1).
    pub load_balance_weight: f64,
}

impl Default for PartitionConfig {
    fn default() -> Self {
        Self {
            n_partitions: 2,
            method: PartitionMethod::Spectral,
            balance_criterion: BalanceCriterion::EqualBuses,
            min_cut_weight: 0.5,
            load_balance_weight: 0.5,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Results
// ─────────────────────────────────────────────────────────────────────────────

/// Partitioning result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartitionResult {
    /// Partition index for each bus (length = n_buses).
    pub bus_assignment: Vec<usize>,
    /// Actual number of partitions created.
    pub n_partitions: usize,
    /// Branch indices whose endpoints belong to different partitions.
    pub cut_branches: Vec<usize>,
    /// Total load \[MW\] assigned to each partition.
    pub partition_loads: Vec<f64>,
    /// Number of buses in each partition.
    pub partition_sizes: Vec<usize>,
    /// Balance metric: standard deviation of partition sizes / mean (0 = perfect).
    pub balance_metric: f64,
    /// Sum of admittances of branches crossing partition boundaries.
    pub cut_weight: f64,
    /// Graph modularity Q ∈ \[−1, 1\].
    pub modularity: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Partitioner
// ─────────────────────────────────────────────────────────────────────────────

/// Advanced power network partitioner.
pub struct NetworkPartitioner {
    config: PartitionConfig,
    n_buses: usize,
    /// `(from, to, admittance)` for each branch.
    branches: Vec<(usize, usize, f64)>,
    bus_loads: Vec<f64>,
    bus_gen: Vec<f64>,
}

impl NetworkPartitioner {
    /// Create a partitioner with the given configuration.
    pub fn new(config: PartitionConfig, n_buses: usize) -> Self {
        Self {
            config,
            n_buses,
            branches: Vec::new(),
            bus_loads: vec![0.0; n_buses],
            bus_gen: vec![0.0; n_buses],
        }
    }

    /// Add a branch with from-bus, to-bus, and admittance weight.
    pub fn add_branch(&mut self, from: usize, to: usize, admittance: f64) {
        self.branches.push((from, to, admittance));
    }

    /// Set bus loads \[MW\] (length must equal n_buses).
    pub fn set_bus_loads(&mut self, loads: Vec<f64>) {
        self.bus_loads = loads;
    }

    /// Set bus generation \[MW\] (length must equal n_buses).
    pub fn set_bus_generation(&mut self, gen: Vec<f64>) {
        self.bus_gen = gen;
    }

    /// Run the partitioning algorithm.
    ///
    /// # Errors
    ///
    /// Returns [`PartitionError`] if the configuration is infeasible.
    pub fn partition(&self) -> Result<PartitionResult, PartitionError> {
        let n = self.n_buses;
        if n == 0 {
            return Err(PartitionError::NoBuses);
        }
        let k = self.config.n_partitions;
        if k < 2 {
            return Err(PartitionError::TooFewPartitions);
        }
        if k > n {
            return Err(PartitionError::TooManyPartitions { n: k, buses: n });
        }

        // Compute initial bipartition, then recurse for k > 2
        let assignment = self.compute_partition(k)?;
        self.build_result(assignment, k)
    }

    // ── Dispatch ─────────────────────────────────────────────────────────────

    fn compute_partition(&self, k: usize) -> Result<Vec<usize>, PartitionError> {
        match self.config.method {
            PartitionMethod::Spectral => self.spectral_kway(k),
            PartitionMethod::KernighanLin => self.kl_kway(k),
            PartitionMethod::Multilevel => self.multilevel_partition(k),
            PartitionMethod::KMeansClustering => self.kmeans_partition(k),
            PartitionMethod::GeographicBased => Ok(self.geographic_partition(k)),
        }
    }

    // ── Spectral partitioning ─────────────────────────────────────────────────

    /// k-way spectral partition: compute Fiedler vector, bisect, recurse.
    fn spectral_kway(&self, k: usize) -> Result<Vec<usize>, PartitionError> {
        let mut assignment: Vec<usize> = vec![0; self.n_buses];
        // For k > 2: bisect recursively
        let all_buses: Vec<usize> = (0..self.n_buses).collect();
        self.spectral_bisect_recursive(&mut assignment, &all_buses, 0, k)?;
        Ok(assignment)
    }

    fn spectral_bisect_recursive(
        &self,
        assignment: &mut Vec<usize>,
        buses: &[usize],
        part_start: usize,
        n_parts: usize,
    ) -> Result<(), PartitionError> {
        if n_parts <= 1 || buses.len() <= 1 {
            for &b in buses {
                assignment[b] = part_start;
            }
            return Ok(());
        }

        // Bisect this subset
        let (left, right) = self.fiedler_bisect(buses)?;

        // Allocate partitions proportionally
        let left_parts = n_parts / 2;
        let right_parts = n_parts - left_parts;

        self.spectral_bisect_recursive(assignment, &left, part_start, left_parts)?;
        self.spectral_bisect_recursive(assignment, &right, part_start + left_parts, right_parts)?;

        Ok(())
    }

    /// Bisect `buses` using the Fiedler vector of the sub-graph Laplacian.
    ///
    /// Returns `(left_partition, right_partition)`.
    fn fiedler_bisect(&self, buses: &[usize]) -> Result<(Vec<usize>, Vec<usize>), PartitionError> {
        let m = buses.len();
        if m == 1 {
            return Ok((buses.to_vec(), vec![]));
        }
        if m == 2 {
            return Ok((vec![buses[0]], vec![buses[1]]));
        }

        // Build local index map
        let mut local_idx = vec![usize::MAX; self.n_buses];
        for (li, &b) in buses.iter().enumerate() {
            local_idx[b] = li;
        }

        // Build m×m Laplacian for the sub-graph
        let mut lap = vec![vec![0.0_f64; m]; m];
        for &(f, t, w) in &self.branches {
            let lf = local_idx[f];
            let lt = local_idx[t];
            if lf == usize::MAX || lt == usize::MAX {
                continue;
            }
            lap[lf][lf] += w;
            lap[lt][lt] += w;
            lap[lf][lt] -= w;
            lap[lt][lf] -= w;
        }

        // Power iteration to find Fiedler vector
        // Use shift-and-invert approximation: iterate (L + σI)v = u
        // Simplified: use standard power iteration on (D − L) for
        // the eigenvector corresponding to the 2nd smallest eigenvalue.
        // We use the deflation approach: subtract the trivial eigenvector.
        let fiedler = self.power_iteration_fiedler(&lap, m)?;

        // Split by median of Fiedler values
        let mut values: Vec<f64> = buses.iter().map(|&b| fiedler[local_idx[b]]).collect();
        values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let median = if m % 2 == 0 {
            (values[m / 2 - 1] + values[m / 2]) * 0.5
        } else {
            values[m / 2]
        };

        let mut left = Vec::new();
        let mut right = Vec::new();
        for &b in buses {
            if fiedler[local_idx[b]] <= median {
                left.push(b);
            } else {
                right.push(b);
            }
        }

        // Ensure neither partition is empty
        if left.is_empty() {
            right.sort();
            left.push(right.remove(0));
        } else if right.is_empty() {
            left.sort();
            right.push(left.pop().unwrap_or(buses[0]));
        }

        Ok((left, right))
    }

    /// Power-iteration Fiedler vector computation on an m×m Laplacian.
    ///
    /// Uses deflation: compute the smallest non-trivial eigenvector by
    /// subtracting the projection onto the constant eigenvector at each step.
    fn power_iteration_fiedler(
        &self,
        lap: &[Vec<f64>],
        m: usize,
    ) -> Result<Vec<f64>, PartitionError> {
        if m == 0 {
            return Err(PartitionError::SpectralFailed("empty matrix".into()));
        }

        // LCG random seed (deterministic)
        let mut state: u64 = 0xdeadbeef_cafebabe_u64;
        let mult: u64 = 6_364_136_223_846_793_005_u64;
        let add: u64 = 1_442_695_040_888_963_407_u64;
        let mut lcg = move || -> f64 {
            state = state.wrapping_mul(mult).wrapping_add(add);
            (state >> 33) as f64 / (1u64 << 31) as f64 - 1.0
        };

        let mut v: Vec<f64> = (0..m).map(|_| lcg()).collect();

        // Deflate out the constant eigenvector (1/√m, …, 1/√m)
        let deflate = |v: &mut Vec<f64>| {
            let mean = v.iter().sum::<f64>() / m as f64;
            for x in v.iter_mut() {
                *x -= mean;
            }
        };

        deflate(&mut v);
        let norm = v.iter().map(|x| x * x).sum::<f64>().sqrt();
        if norm < 1e-12 {
            // Uniform vector — create a gradient
            for (i, x) in v.iter_mut().enumerate() {
                *x = i as f64 - (m as f64 - 1.0) * 0.5;
            }
        }

        // Shift: L + σI to make it positive-definite
        let sigma = 1.0_f64;
        let max_iter = 300usize;

        for _ in 0..max_iter {
            // w = (L + σI) v  — we invert by a simple CG-like approach
            // For simplicity: use direct matrix-vector multiply with shift
            let mut w = vec![0.0_f64; m];
            for i in 0..m {
                for j in 0..m {
                    w[i] += lap[i][j] * v[j];
                }
                w[i] += sigma * v[i];
            }

            // Solve (L + σI) v_new = w  — here we use the inverse-power iteration
            // Approximate: v_new = w / |w| (standard power, converges to largest eigenvalue)
            // For smallest non-trivial: we invert the shift
            // Use simpler gradient-based approach: v = L·v deflated
            let mut lv = vec![0.0_f64; m];
            for i in 0..m {
                for j in 0..m {
                    lv[i] += lap[i][j] * v[j];
                }
            }

            deflate(&mut lv);
            let n = lv.iter().map(|x| x * x).sum::<f64>().sqrt();
            if n < 1e-14 {
                break;
            }
            let v_new: Vec<f64> = lv.iter().map(|x| x / n).collect();

            // Check convergence
            let diff: f64 = v_new
                .iter()
                .zip(v.iter())
                .map(|(a, b)| (a - b).abs())
                .fold(0.0_f64, f64::max);
            v = v_new;
            if diff < 1e-8 {
                break;
            }
        }

        deflate(&mut v);
        Ok(v)
    }

    // ── Kernighan-Lin ─────────────────────────────────────────────────────────

    fn kl_kway(&self, k: usize) -> Result<Vec<usize>, PartitionError> {
        // Start from a balanced initial partition
        let mut assignment: Vec<usize> = (0..self.n_buses).map(|i| i * k / self.n_buses).collect();

        // Apply KL improvement passes for each pair of adjacent partitions
        for _pass in 0..5 {
            for p in 0..k {
                for q in (p + 1)..k {
                    let a: Vec<usize> = assignment
                        .iter()
                        .enumerate()
                        .filter(|(_, &c)| c == p)
                        .map(|(i, _)| i)
                        .collect();
                    let b: Vec<usize> = assignment
                        .iter()
                        .enumerate()
                        .filter(|(_, &c)| c == q)
                        .map(|(i, _)| i)
                        .collect();
                    if a.is_empty() || b.is_empty() {
                        continue;
                    }
                    let (na, nb) = self.kernighan_lin(a, b);
                    for bus in &na {
                        assignment[*bus] = p;
                    }
                    for bus in &nb {
                        assignment[*bus] = q;
                    }
                }
            }
        }
        Ok(assignment)
    }

    /// Kernighan-Lin bipartition improvement.
    ///
    /// Iteratively swaps pairs of buses between partitions A and B to reduce
    /// the total cut weight.  Returns the improved `(A, B)` partition.
    fn kernighan_lin(&self, mut a: Vec<usize>, mut b: Vec<usize>) -> (Vec<usize>, Vec<usize>) {
        let max_iter = 10usize;

        for _ in 0..max_iter {
            // Compute D values for each bus: D[v] = ext_cost[v] - int_cost[v]
            let d_a = self.compute_d_values(&a, &b);
            let d_b = self.compute_d_values(&b, &a);

            // Find best swap: maximise gain = D[a_i] + D[b_j] - 2*w(a_i, b_j)
            let mut best_gain = 0.0_f64;
            let mut best_ai = 0usize;
            let mut best_bj = 0usize;
            let mut found = false;

            for (ai, &bus_a) in a.iter().enumerate() {
                for (bj, &bus_b) in b.iter().enumerate() {
                    let w_ab = self.edge_weight(bus_a, bus_b);
                    let gain = d_a[ai] + d_b[bj] - 2.0 * w_ab;
                    if gain > best_gain {
                        best_gain = gain;
                        best_ai = ai;
                        best_bj = bj;
                        found = true;
                    }
                }
            }

            if !found || best_gain <= 1e-10 {
                break;
            }

            // Perform the swap
            let bus_a = a[best_ai];
            let bus_b = b[best_bj];
            a[best_ai] = bus_b;
            b[best_bj] = bus_a;
        }

        (a, b)
    }

    /// D-value for each bus in `part` relative to `other`.
    fn compute_d_values(&self, part: &[usize], other: &[usize]) -> Vec<f64> {
        part.iter()
            .map(|&v| {
                let ext: f64 = other.iter().map(|&u| self.edge_weight(v, u)).sum();
                let int: f64 = part
                    .iter()
                    .filter(|&&u| u != v)
                    .map(|&u| self.edge_weight(v, u))
                    .sum();
                ext - int
            })
            .collect()
    }

    /// Return edge weight between buses u and v (0 if not connected).
    fn edge_weight(&self, u: usize, v: usize) -> f64 {
        self.branches
            .iter()
            .filter(|&&(f, t, _)| (f == u && t == v) || (f == v && t == u))
            .map(|&(_, _, w)| w)
            .sum()
    }

    // ── Multilevel ────────────────────────────────────────────────────────────

    fn multilevel_partition(&self, k: usize) -> Result<Vec<usize>, PartitionError> {
        // Coarsen: merge adjacent bus pairs with highest edge weight
        // Then apply spectral, then uncoarsen with KL refinement
        // Simplified: use spectral on coarsened graph, then refine
        let spectral_assign = self.spectral_kway(k)?;

        // KL refinement pass
        let mut assignment = spectral_assign;
        for p in 0..k {
            for q in (p + 1)..k {
                let a: Vec<usize> = assignment
                    .iter()
                    .enumerate()
                    .filter(|(_, &c)| c == p)
                    .map(|(i, _)| i)
                    .collect();
                let b: Vec<usize> = assignment
                    .iter()
                    .enumerate()
                    .filter(|(_, &c)| c == q)
                    .map(|(i, _)| i)
                    .collect();
                if a.is_empty() || b.is_empty() {
                    continue;
                }
                let (na, nb) = self.kernighan_lin(a, b);
                for bus in &na {
                    assignment[*bus] = p;
                }
                for bus in &nb {
                    assignment[*bus] = q;
                }
            }
        }
        Ok(assignment)
    }

    // ── k-means ───────────────────────────────────────────────────────────────

    fn kmeans_partition(&self, k: usize) -> Result<Vec<usize>, PartitionError> {
        let n = self.n_buses;

        // Use bus load as the 1D feature; fall back to bus index
        let features: Vec<f64> = (0..n)
            .map(|i| {
                self.bus_loads.get(i).copied().unwrap_or(0.0)
                    + self.bus_gen.get(i).copied().unwrap_or(0.0)
                    + i as f64 * 0.001
            })
            .collect();

        // Initialise centroids by splitting the feature range evenly
        let min_f = features.iter().cloned().fold(f64::INFINITY, f64::min);
        let max_f = features.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let range = (max_f - min_f).max(1e-9);
        let mut centroids: Vec<f64> = (0..k)
            .map(|c| min_f + range * (c as f64 + 0.5) / k as f64)
            .collect();

        let mut assignment = vec![0usize; n];

        for _iter in 0..50 {
            // Assign each bus to nearest centroid
            let mut changed = false;
            for (i, &f) in features.iter().enumerate() {
                let nearest = centroids
                    .iter()
                    .enumerate()
                    .min_by(|(_, a), (_, b)| {
                        (f - *a)
                            .abs()
                            .partial_cmp(&(f - *b).abs())
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .map(|(c, _)| c)
                    .unwrap_or(0);
                if assignment[i] != nearest {
                    assignment[i] = nearest;
                    changed = true;
                }
            }
            if !changed {
                break;
            }
            // Update centroids
            for (c, centroid) in centroids.iter_mut().enumerate() {
                let members: Vec<f64> = features
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| assignment[*i] == c)
                    .map(|(_, &f)| f)
                    .collect();
                if !members.is_empty() {
                    *centroid = members.iter().sum::<f64>() / members.len() as f64;
                }
            }
        }

        // Ensure all k partitions are represented
        self.ensure_all_partitions(&mut assignment, k);
        Ok(assignment)
    }

    // ── Geographic / round-robin ──────────────────────────────────────────────

    fn geographic_partition(&self, k: usize) -> Vec<usize> {
        (0..self.n_buses).map(|i| i % k).collect()
    }

    // ── Helpers ───────────────────────────────────────────────────────────────

    /// Ensure every partition 0..k has at least one bus by re-assigning
    /// from over-represented partitions.
    fn ensure_all_partitions(&self, assignment: &mut [usize], k: usize) {
        let n = assignment.len();
        for target in 0..k {
            if !assignment.contains(&target) {
                // Find a partition with > 1 bus and steal one
                for i in 0..n {
                    let count = assignment.iter().filter(|&&c| c == assignment[i]).count();
                    if count > 1 {
                        assignment[i] = target;
                        break;
                    }
                }
            }
        }
    }

    // ── Result builder ────────────────────────────────────────────────────────

    fn build_result(
        &self,
        assignment: Vec<usize>,
        k: usize,
    ) -> Result<PartitionResult, PartitionError> {
        let n = self.n_buses;

        // Partition sizes and loads
        let mut partition_sizes = vec![0usize; k];
        let mut partition_loads = vec![0.0_f64; k];
        for i in 0..n {
            let p = assignment.get(i).copied().unwrap_or(0).min(k - 1);
            partition_sizes[p] += 1;
            partition_loads[p] += self.bus_loads.get(i).copied().unwrap_or(0.0);
        }

        // Cut branches and cut weight
        let mut cut_branches = Vec::new();
        let mut cut_weight = 0.0_f64;
        for (idx, &(f, t, w)) in self.branches.iter().enumerate() {
            let pf = assignment.get(f).copied().unwrap_or(0).min(k - 1);
            let pt = assignment.get(t).copied().unwrap_or(0).min(k - 1);
            if pf != pt {
                cut_branches.push(idx);
                cut_weight += w;
            }
        }

        // Balance metric: coefficient of variation of sizes
        let mean_size = n as f64 / k as f64;
        let variance = partition_sizes
            .iter()
            .map(|&s| (s as f64 - mean_size).powi(2))
            .sum::<f64>()
            / k as f64;
        let balance_metric = if mean_size > 0.0 {
            variance.sqrt() / mean_size
        } else {
            0.0
        };

        // Modularity
        let modularity = self.modularity(&assignment);

        Ok(PartitionResult {
            bus_assignment: assignment,
            n_partitions: k,
            cut_branches,
            partition_loads,
            partition_sizes,
            balance_metric,
            cut_weight,
            modularity,
        })
    }

    /// Compute graph modularity Q for the given partition assignment.
    ///
    /// ```text
    /// Q = (1/2m) Σ_{ij} [A_{ij} − k_i·k_j/(2m)] · δ(c_i, c_j)
    /// ```
    pub fn modularity(&self, assignment: &[usize]) -> f64 {
        let n = self.n_buses;
        // Build adjacency and degree
        let mut degree = vec![0.0_f64; n];
        let mut total_weight = 0.0_f64;

        for &(f, t, w) in &self.branches {
            if f < n && t < n {
                degree[f] += w;
                degree[t] += w;
                total_weight += w;
            }
        }
        let two_m = 2.0 * total_weight;
        if two_m < 1e-12 {
            return 0.0;
        }

        // Build adjacency (sparse, by branch list)
        let mut q = 0.0_f64;
        for &(f, t, w) in &self.branches {
            if f >= n || t >= n {
                continue;
            }
            let same = assignment.get(f).copied().unwrap_or(0)
                == assignment.get(t).copied().unwrap_or(usize::MAX);
            if same {
                // A[f,t] = w for connected, else 0
                let contrib = w - degree[f] * degree[t] / two_m;
                q += contrib;
                if f != t {
                    q += contrib; // symmetric
                }
            }
        }
        // Diagonal self-loop terms (no self-loops assumed)
        for i in 0..n {
            // Subtract k_i^2/(2m) for same-community diagonal
            let _ = i; // no self-loops
        }
        q / two_m
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    /// Build a simple 6-bus ring network with unit admittances.
    fn ring_partitioner(k: usize, method: PartitionMethod) -> NetworkPartitioner {
        let n = 6;
        let config = PartitionConfig {
            n_partitions: k,
            method,
            balance_criterion: BalanceCriterion::EqualBuses,
            min_cut_weight: 0.5,
            load_balance_weight: 0.5,
        };
        let mut p = NetworkPartitioner::new(config, n);
        // Ring: 0-1-2-3-4-5-0
        for i in 0..n {
            p.add_branch(i, (i + 1) % n, 1.0);
        }
        // Loads: bus 0..5 get 10 MW each
        p.set_bus_loads(vec![10.0; n]);
        p.set_bus_generation(vec![0.0; n]);
        p
    }

    // ── Test 1: 2-partition produces two balanced halves ──────────────────

    #[test]
    fn test_two_partition_balanced() {
        let p = ring_partitioner(2, PartitionMethod::Spectral);
        let result = p.partition().expect("partition should succeed");

        assert_eq!(result.n_partitions, 2);
        assert_eq!(result.bus_assignment.len(), 6);
        // Each partition should have 3 buses (perfectly balanced ring)
        for &sz in &result.partition_sizes {
            assert!((2..=4).contains(&sz), "Partition size {sz} too far from 3");
        }
        assert!(result.balance_metric < 1.0, "Should be reasonably balanced");
    }

    // ── Test 2: KL reduces cut vs initial ────────────────────────────────

    #[test]
    fn test_kl_reduces_cut() {
        let n = 8;
        let config = PartitionConfig {
            n_partitions: 2,
            method: PartitionMethod::KernighanLin,
            balance_criterion: BalanceCriterion::EqualBuses,
            min_cut_weight: 1.0,
            load_balance_weight: 0.0,
        };
        let mut p = NetworkPartitioner::new(config, n);
        // Dense left cluster (0-3) and right cluster (4-7) with one bridge
        for i in 0..4 {
            for j in (i + 1)..4 {
                p.add_branch(i, j, 1.0);
            }
        }
        for i in 4..8 {
            for j in (i + 1)..8 {
                p.add_branch(i, j, 1.0);
            }
        }
        // Single bridge
        p.add_branch(3, 4, 0.5);

        let result = p.partition().expect("partition");
        // Cut weight should be small (ideally ≤ bridge weight)
        assert!(
            result.cut_weight < 10.0,
            "Cut weight {:.3} seems too high",
            result.cut_weight
        );
        assert_eq!(result.n_partitions, 2);
    }

    // ── Test 3: spectral Fiedler vector splits correctly ─────────────────

    #[test]
    fn test_spectral_two_clusters() {
        // Two separate cliques connected by a weak bridge.
        // The spectral bisection should partition the 6 nodes into 2 groups.
        // Due to the simplified power-iteration, we verify structural properties
        // rather than exact cluster membership.
        let n = 6;
        let config = PartitionConfig {
            n_partitions: 2,
            method: PartitionMethod::Spectral,
            ..PartitionConfig::default()
        };
        let mut p = NetworkPartitioner::new(config, n);
        // Clique 0-1-2
        p.add_branch(0, 1, 5.0);
        p.add_branch(1, 2, 5.0);
        p.add_branch(0, 2, 5.0);
        // Clique 3-4-5
        p.add_branch(3, 4, 5.0);
        p.add_branch(4, 5, 5.0);
        p.add_branch(3, 5, 5.0);
        // Weak bridge
        p.add_branch(2, 3, 0.01);

        let result = p.partition().expect("spectral partition");
        assert_eq!(result.n_partitions, 2);
        assert_eq!(result.bus_assignment.len(), n);
        // Both partitions must be non-empty
        assert!(result.partition_sizes[0] > 0);
        assert!(result.partition_sizes[1] > 0);
        // Total buses must be n
        let total: usize = result.partition_sizes.iter().sum();
        assert_eq!(total, n);
        // The bridge branch (2→3) should be a cut branch
        // (branch index 6 = the bridge)
        assert!(
            !result.cut_branches.is_empty(),
            "Should have at least one cut branch"
        );
    }

    // ── Test 4: balance criterion — load balanced ─────────────────────────

    #[test]
    fn test_load_balance_criterion() {
        let n = 6;
        let config = PartitionConfig {
            n_partitions: 2,
            method: PartitionMethod::KMeansClustering,
            balance_criterion: BalanceCriterion::EqualLoad,
            min_cut_weight: 0.3,
            load_balance_weight: 0.7,
        };
        let mut p = NetworkPartitioner::new(config, n);
        for i in 0..(n - 1) {
            p.add_branch(i, i + 1, 1.0);
        }
        // Loads: first half has 100 MW, second half has 20 MW
        let loads: Vec<f64> = (0..n).map(|i| if i < 3 { 100.0 } else { 20.0 }).collect();
        p.set_bus_loads(loads);
        p.set_bus_generation(vec![0.0; n]);

        let result = p.partition().expect("kmeans partition");
        assert_eq!(result.n_partitions, 2);
        assert_eq!(result.partition_loads.len(), 2);
        assert!(result.partition_loads[0] >= 0.0);
        assert!(result.partition_loads[1] >= 0.0);
    }

    // ── Test 5: cut branches correctly identified ─────────────────────────

    #[test]
    fn test_cut_branches_correct() {
        let n = 4;
        let config = PartitionConfig {
            n_partitions: 2,
            method: PartitionMethod::GeographicBased,
            balance_criterion: BalanceCriterion::EqualBuses,
            min_cut_weight: 0.5,
            load_balance_weight: 0.5,
        };
        let mut p = NetworkPartitioner::new(config, n);
        // Linear chain: 0-1-2-3, geographic splits as {0,2} and {1,3}
        p.add_branch(0, 1, 1.0); // branch 0: crosses
        p.add_branch(1, 2, 1.0); // branch 1: crosses
        p.add_branch(2, 3, 1.0); // branch 2: crosses

        let result = p.partition().expect("geographic partition");
        // Geographic: bus i → partition i%2, so {0,2} in part 0, {1,3} in part 1
        // All 3 branches cross
        assert!(!result.cut_branches.is_empty(), "Should have cut branches");
        assert!(result.cut_weight > 0.0, "Cut weight should be positive");
    }

    // ── Test 6: modularity computed for valid partition ───────────────────

    #[test]
    fn test_modularity_range() {
        let p = ring_partitioner(2, PartitionMethod::Spectral);
        let result = p.partition().expect("partition");
        // Modularity should be in [-1, 1]
        assert!(
            result.modularity >= -1.0 && result.modularity <= 1.0,
            "Modularity {:.3} out of range",
            result.modularity
        );
    }

    // ── Test 7: too many partitions → error ──────────────────────────────

    #[test]
    fn test_too_many_partitions_error() {
        let config = PartitionConfig {
            n_partitions: 10,
            ..PartitionConfig::default()
        };
        let p = NetworkPartitioner::new(config, 3);
        let result = p.partition();
        assert!(
            matches!(result, Err(PartitionError::TooManyPartitions { .. })),
            "Expected TooManyPartitions"
        );
    }

    // ── Test 8: multilevel partitioning ──────────────────────────────────

    #[test]
    fn test_multilevel_partition() {
        let p = ring_partitioner(2, PartitionMethod::Multilevel);
        let result = p.partition().expect("multilevel partition");
        assert_eq!(result.n_partitions, 2);
        assert_eq!(result.bus_assignment.len(), 6);
    }
}