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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
//! Robust Transmission Expansion Planning (TEP) via Benders Decomposition.
//!
//! Min-max-min formulation with uncertainty sets for load growth. The master
//! problem selects line investments; the subproblem evaluates worst-case
//! DC-OPF dispatch costs. Benders cuts communicate dual information.
/// Candidate transmission line for expansion.
#[derive(Debug, Clone)]
pub struct CandidateLine {
/// Unique identifier.
pub id: usize,
/// From-bus index (0-based).
pub from_bus: usize,
/// To-bus index (0-based).
pub to_bus: usize,
/// Reactance (pu).
pub x_pu: f64,
/// Rating (MW).
pub rating_mw: f64,
/// Investment cost ($M).
pub investment_cost_m_usd: f64,
/// Construction time (years).
pub construction_years: f64,
/// Maximum number of circuits that can be built.
pub max_circuits: usize,
}
/// Existing transmission line already in service.
#[derive(Debug, Clone)]
pub struct ExistingLine {
/// Unique identifier.
pub id: usize,
/// From-bus index (0-based).
pub from_bus: usize,
/// To-bus index (0-based).
pub to_bus: usize,
/// Reactance (pu).
pub x_pu: f64,
/// Rating (MW).
pub rating_mw: f64,
}
/// Bus data for the TEP problem.
#[derive(Debug, Clone)]
pub struct TepBus {
/// Bus identifier (0-based).
pub bus_id: usize,
/// Base load (MW).
pub p_load_mw: f64,
/// Generation capacity (MW).
pub p_gen_max_mw: f64,
/// Generation cost ($/MWh).
pub gen_cost: f64,
/// Load shedding cost ($/MWh) — Value of Lost Load.
pub voll: f64,
}
/// Uncertainty set definition for robust optimisation.
#[derive(Debug, Clone)]
pub enum UncertaintySet {
/// Box uncertainty: load varies within `[1 - delta, 1 + delta]` fraction.
Box {
/// Maximum fractional deviation of load.
load_deviation: f64,
},
/// Polyhedral uncertainty with a budget constraint limiting simultaneous deviations.
Polyhedral {
/// Maximum number of buses that can deviate simultaneously.
max_deviations: usize,
/// Maximum fractional deviation per bus.
load_deviation: f64,
},
/// Ellipsoidal uncertainty with a given radius.
Ellipsoidal {
/// Radius of the ellipsoid in normalised deviation space.
radius: f64,
},
}
/// A planning scenario representing a specific realisation of uncertain loads.
#[derive(Debug, Clone)]
pub struct PlanningScenario {
/// Scenario identifier.
pub id: usize,
/// Human-readable name.
pub name: String,
/// Load scaling factors per bus (multiplicative on base load).
pub load_factors: Vec<f64>,
/// Probability weight of this scenario.
pub probability: f64,
}
/// Configuration for the robust TEP Benders solver.
#[derive(Debug, Clone)]
pub struct RobustTepConfig {
/// Maximum Benders iterations (default 50).
pub max_benders_iter: usize,
/// Relative optimality gap tolerance (default 0.01 = 1%).
pub optimality_gap: f64,
/// Maximum scenarios to generate (default 100).
pub max_scenarios: usize,
/// Uncertainty set definition.
pub uncertainty: UncertaintySet,
/// Annual discount rate (default 0.08).
pub discount_rate: f64,
/// Planning horizon in years (default 20).
pub planning_horizon_years: usize,
/// System base MVA (default 100.0).
pub base_mva: f64,
}
impl Default for RobustTepConfig {
fn default() -> Self {
Self {
max_benders_iter: 50,
optimality_gap: 0.01,
max_scenarios: 100,
uncertainty: UncertaintySet::Box {
load_deviation: 0.2,
},
discount_rate: 0.08,
planning_horizon_years: 20,
base_mva: 100.0,
}
}
}
/// Investment decision for a single candidate line.
#[derive(Debug, Clone)]
pub struct InvestmentDecision {
/// Candidate line identifier.
pub line_id: usize,
/// Number of circuits to build.
pub n_circuits: usize,
/// Investment cost ($M) for the built circuits.
pub investment_cost_m_usd: f64,
}
/// Full solution of the robust TEP problem.
#[derive(Debug, Clone)]
pub struct RobustTepSolution {
/// Investment decisions for each candidate.
pub investments: Vec<InvestmentDecision>,
/// Total investment cost ($M).
pub total_investment_m_usd: f64,
/// Expected (worst-case) operating cost ($M).
pub expected_operation_cost_m_usd: f64,
/// Total cost = investment + NPV of operating cost ($M).
pub total_cost_m_usd: f64,
/// Worst-case scenario identified during the solve.
pub worst_case_scenario: PlanningScenario,
/// Load shedding in worst-case scenario (MW).
pub worst_case_load_shedding_mw: f64,
/// Upper bound from master problem.
pub upper_bound: f64,
/// Lower bound from subproblem.
pub lower_bound: f64,
/// Number of Benders iterations executed.
pub iterations: usize,
/// Whether the algorithm converged within tolerance.
pub converged: bool,
/// Whether the solution is N-1 secure.
pub n1_secure: bool,
}
/// A Benders cut constraining the master problem.
#[derive(Debug, Clone)]
pub struct BendersCut {
/// Iteration at which this cut was generated.
pub iteration: usize,
/// Right-hand-side constant (fixed cost component).
pub rhs: f64,
/// Sensitivity coefficients for each candidate line's investment variable.
pub coefficients: Vec<f64>,
}
/// Robust TEP solver using Benders decomposition.
pub struct RobustTepSolver {
/// Bus data.
pub buses: Vec<TepBus>,
/// Existing transmission lines.
pub existing_lines: Vec<ExistingLine>,
/// Candidate lines for expansion.
pub candidates: Vec<CandidateLine>,
/// Solver configuration.
pub config: RobustTepConfig,
}
/// An active line in the network (used internally for PTDF/DC-OPF).
#[derive(Debug, Clone)]
struct ActiveLine {
from_bus: usize,
to_bus: usize,
reactance_pu: f64,
rating_mw: f64,
}
impl RobustTepSolver {
/// Create a new solver with the given configuration (no buses/lines yet).
pub fn new(config: RobustTepConfig) -> Self {
Self {
buses: Vec::new(),
existing_lines: Vec::new(),
candidates: Vec::new(),
config,
}
}
/// Add a bus to the problem.
pub fn add_bus(&mut self, bus: TepBus) {
self.buses.push(bus);
}
/// Add an existing (in-service) transmission line.
pub fn add_existing_line(&mut self, line: ExistingLine) {
self.existing_lines.push(line);
}
/// Add a candidate line available for investment.
pub fn add_candidate(&mut self, candidate: CandidateLine) {
self.candidates.push(candidate);
}
/// Net-present-value annuity factor for the planning horizon.
pub fn npv_factor(&self) -> f64 {
let r = self.config.discount_rate;
let n = self.config.planning_horizon_years as f64;
if r > 1e-10 {
(1.0 - (1.0 + r).powf(-n)) / r
} else {
n
}
}
/// Solve the robust TEP using Benders decomposition with min-max-min.
///
/// Returns [`RobustTepSolution`] on success, or an error string if
/// the problem is ill-posed.
pub fn solve(&self) -> Result<RobustTepSolution, String> {
if self.buses.is_empty() {
return Err("No buses defined".into());
}
let n_cands = self.candidates.len();
let n_buses = self.buses.len();
// Current investment: number of circuits per candidate (start with 0)
let mut investments: Vec<usize> = vec![0; n_cands];
let mut cuts: Vec<BendersCut> = Vec::new();
let mut upper_bound = f64::INFINITY;
let mut lower_bound = f64::NEG_INFINITY;
let mut converged = false;
let mut best_investments = investments.clone();
let mut best_worst_scenario = PlanningScenario {
id: 0,
name: "base".to_string(),
load_factors: vec![1.0; n_buses],
probability: 1.0,
};
let mut best_load_shedding = 0.0_f64;
let mut n_iter = 0_usize;
for iter in 0..self.config.max_benders_iter {
n_iter = iter + 1;
// --- Master problem ---
let (inv_new, master_obj) = self.solve_master(&cuts);
investments = inv_new;
// Update lower bound
if master_obj > lower_bound {
lower_bound = master_obj;
}
// --- Subproblem: find worst-case scenario ---
let worst = self.generate_worst_case(&investments);
let sub_result = self.solve_subproblem(&investments, &worst)?;
let (sub_cost, cut) = sub_result;
// Investment cost
let invest_cost: f64 = investments
.iter()
.enumerate()
.map(|(i, &nc)| {
self.candidates
.get(i)
.map(|c| c.investment_cost_m_usd * nc as f64)
.unwrap_or(0.0)
})
.sum();
let candidate_ub = invest_cost + sub_cost * self.npv_factor();
if candidate_ub < upper_bound {
upper_bound = candidate_ub;
best_investments = investments.clone();
best_worst_scenario = worst.clone();
// Compute load shedding for this scenario
let (_, ls_vec) = self
.dc_opf(&investments, &worst.load_factors)
.unwrap_or((0.0, vec![0.0; n_buses]));
best_load_shedding = ls_vec.iter().sum();
}
// Add cut
cuts.push(BendersCut {
iteration: iter,
rhs: cut.rhs,
coefficients: cut.coefficients,
});
// --- Convergence check ---
let gap = relative_gap(upper_bound, lower_bound);
if gap < self.config.optimality_gap && upper_bound < f64::INFINITY {
converged = true;
break;
}
}
// Build solution
let total_investment: f64 = best_investments
.iter()
.enumerate()
.map(|(i, &nc)| {
self.candidates
.get(i)
.map(|c| c.investment_cost_m_usd * nc as f64)
.unwrap_or(0.0)
})
.sum();
// Compute operating cost at the best investment under worst case
let (op_cost, _) = self
.dc_opf(&best_investments, &best_worst_scenario.load_factors)
.unwrap_or((0.0, Vec::new()));
let op_cost_m = op_cost; // already in $M from dc_opf
let total_cost = total_investment + op_cost_m * self.npv_factor();
let inv_decisions: Vec<InvestmentDecision> = best_investments
.iter()
.enumerate()
.map(|(i, &nc)| {
let cost = self
.candidates
.get(i)
.map(|c| c.investment_cost_m_usd * nc as f64)
.unwrap_or(0.0);
InvestmentDecision {
line_id: self.candidates.get(i).map(|c| c.id).unwrap_or(i),
n_circuits: nc,
investment_cost_m_usd: cost,
}
})
.collect();
// N-1 security check
let n1_secure = self.check_n1_security(&best_investments, &best_worst_scenario);
Ok(RobustTepSolution {
investments: inv_decisions,
total_investment_m_usd: total_investment,
expected_operation_cost_m_usd: op_cost_m,
total_cost_m_usd: total_cost,
worst_case_scenario: best_worst_scenario,
worst_case_load_shedding_mw: best_load_shedding,
upper_bound,
lower_bound,
iterations: n_iter,
converged,
n1_secure,
})
}
/// Solve for a single deterministic scenario (no adversarial search).
pub fn solve_deterministic(
&self,
scenario: &PlanningScenario,
) -> Result<RobustTepSolution, String> {
if self.buses.is_empty() {
return Err("No buses defined".into());
}
let n_cands = self.candidates.len();
let n_buses = self.buses.len();
let mut investments: Vec<usize> = vec![0; n_cands];
let mut cuts: Vec<BendersCut> = Vec::new();
let mut upper_bound = f64::INFINITY;
let mut lower_bound = f64::NEG_INFINITY;
let mut converged = false;
let mut best_investments = investments.clone();
let mut n_iter = 0_usize;
for iter in 0..self.config.max_benders_iter {
n_iter = iter + 1;
let (inv_new, master_obj) = self.solve_master(&cuts);
investments = inv_new;
if master_obj > lower_bound {
lower_bound = master_obj;
}
let sub_result = self.solve_subproblem(&investments, scenario)?;
let (sub_cost, cut) = sub_result;
let invest_cost: f64 = investments
.iter()
.enumerate()
.map(|(i, &nc)| {
self.candidates
.get(i)
.map(|c| c.investment_cost_m_usd * nc as f64)
.unwrap_or(0.0)
})
.sum();
let candidate_ub = invest_cost + sub_cost * self.npv_factor();
if candidate_ub < upper_bound {
upper_bound = candidate_ub;
best_investments = investments.clone();
}
cuts.push(BendersCut {
iteration: iter,
rhs: cut.rhs,
coefficients: cut.coefficients,
});
let gap = relative_gap(upper_bound, lower_bound);
if gap < self.config.optimality_gap && upper_bound < f64::INFINITY {
converged = true;
break;
}
}
let total_investment: f64 = best_investments
.iter()
.enumerate()
.map(|(i, &nc)| {
self.candidates
.get(i)
.map(|c| c.investment_cost_m_usd * nc as f64)
.unwrap_or(0.0)
})
.sum();
let (op_cost, ls_vec) = self
.dc_opf(&best_investments, &scenario.load_factors)
.unwrap_or((0.0, vec![0.0; n_buses]));
let total_cost = total_investment + op_cost * self.npv_factor();
let load_shed_total: f64 = ls_vec.iter().sum();
let inv_decisions: Vec<InvestmentDecision> = best_investments
.iter()
.enumerate()
.map(|(i, &nc)| InvestmentDecision {
line_id: self.candidates.get(i).map(|c| c.id).unwrap_or(i),
n_circuits: nc,
investment_cost_m_usd: self
.candidates
.get(i)
.map(|c| c.investment_cost_m_usd * nc as f64)
.unwrap_or(0.0),
})
.collect();
let n1_secure = self.check_n1_security(&best_investments, scenario);
Ok(RobustTepSolution {
investments: inv_decisions,
total_investment_m_usd: total_investment,
expected_operation_cost_m_usd: op_cost,
total_cost_m_usd: total_cost,
worst_case_scenario: scenario.clone(),
worst_case_load_shedding_mw: load_shed_total,
upper_bound,
lower_bound,
iterations: n_iter,
converged,
n1_secure,
})
}
/// Solve the master problem: greedy + LP-relaxation selection of
/// candidate circuits, constrained by Benders cuts.
///
/// Returns (investment vector as circuit counts, master objective value).
fn solve_master(&self, cuts: &[BendersCut]) -> (Vec<usize>, f64) {
let n = self.candidates.len();
if n == 0 {
return (vec![], 0.0);
}
// Score each candidate by accumulated cut coefficient magnitude
let mut scores: Vec<f64> = vec![0.0; n];
for cut in cuts {
for (i, &coeff) in cut.coefficients.iter().enumerate().take(n) {
// Negative coefficient means building reduces cost
scores[i] += -coeff;
}
}
// Sort candidates by benefit-to-cost ratio (descending)
let mut order: Vec<usize> = (0..n).collect();
order.sort_by(|&a, &b| {
let ratio_a = scores[a] / self.candidates[a].investment_cost_m_usd.max(1e-10);
let ratio_b = scores[b] / self.candidates[b].investment_cost_m_usd.max(1e-10);
ratio_b
.partial_cmp(&ratio_a)
.unwrap_or(core::cmp::Ordering::Equal)
});
// Greedy selection: build circuits one at a time for best candidates
let mut x: Vec<f64> = vec![0.0; n];
for &i in &order {
let max_c = self.candidates[i].max_circuits;
if scores[i] > 0.0 || cuts.is_empty() {
x[i] = max_c as f64;
}
}
// Evaluate theta from cuts
let mut theta = 0.0_f64;
for cut in cuts {
let lhs: f64 = cut
.coefficients
.iter()
.enumerate()
.map(|(i, &c)| c * x.get(i).copied().unwrap_or(0.0))
.sum::<f64>()
+ cut.rhs;
if lhs > theta {
theta = lhs;
}
}
// Master objective = investment cost + theta
let invest_cost: f64 = self
.candidates
.iter()
.enumerate()
.map(|(i, c)| c.investment_cost_m_usd * x[i])
.sum();
let master_obj = invest_cost + theta;
// Round to integer circuit counts
let investments: Vec<usize> = x
.iter()
.enumerate()
.map(|(i, &v)| {
let max_c = self.candidates.get(i).map(|c| c.max_circuits).unwrap_or(1);
(v.round() as usize).min(max_c)
})
.collect();
(investments, master_obj)
}
/// Solve the DC-OPF subproblem for fixed investments and a given scenario.
///
/// Returns (operating cost in $M, Benders cut).
fn solve_subproblem(
&self,
investments: &[usize],
scenario: &PlanningScenario,
) -> Result<(f64, BendersCut), String> {
let n_cands = self.candidates.len();
let n_buses = self.buses.len();
let (op_cost, load_shed) = self.dc_opf(investments, &scenario.load_factors)?;
// Compute PTDF for sensitivity
let ptdf = self.compute_ptdf(investments);
// Generate Benders cut coefficients
// lambda_c = -VOLL * sum_b (ls_b * ptdf_{candidate_line, b}) / base_mva
let mut coefficients = vec![0.0; n_cands];
let active_lines = self.build_active_lines(investments);
let _n_existing = self.existing_lines.len();
for (ci, cand) in self.candidates.iter().enumerate() {
let _current_circuits = investments.get(ci).copied().unwrap_or(0);
// Find average VOLL across buses with load shedding
let avg_voll: f64 =
self.buses.iter().map(|b| b.voll).sum::<f64>() / (n_buses as f64).max(1.0);
// Sensitivity: how much would adding one more circuit of this
// candidate reduce operating cost?
// Approximate using PTDF: flow relief capability * congestion value
let mut sensitivity = 0.0_f64;
// Check if any existing/built line on the same corridor is congested
for (l, line) in active_lines.iter().enumerate() {
if (line.from_bus == cand.from_bus && line.to_bus == cand.to_bus)
|| (line.from_bus == cand.to_bus && line.to_bus == cand.from_bus)
{
if let Some(ptdf_row) = ptdf.get(l) {
for (b, &ls) in load_shed.iter().enumerate() {
let ptdf_val = ptdf_row.get(b).copied().unwrap_or(0.0);
sensitivity += -avg_voll * ls * ptdf_val.abs();
}
}
}
}
// If no corridor match, use general load-shed sensitivity
if sensitivity.abs() < 1e-12 {
let total_ls: f64 = load_shed.iter().sum();
sensitivity = -avg_voll * total_ls / (n_cands as f64 + 1.0);
}
coefficients[ci] = sensitivity * scenario.probability;
}
// RHS = Q(x*) - sum_c coeff_c * x*_c
let x_dot_coeff: f64 = coefficients
.iter()
.enumerate()
.map(|(i, &c)| c * investments.get(i).copied().unwrap_or(0) as f64)
.sum();
let rhs = op_cost * scenario.probability - x_dot_coeff;
let cut = BendersCut {
iteration: 0,
rhs,
coefficients,
};
Ok((op_cost, cut))
}
/// Solve DC-OPF for given investments and load factors.
///
/// Returns (operating cost in $M, load shedding per bus in MW).
fn dc_opf(
&self,
investments: &[usize],
load_factors: &[f64],
) -> Result<(f64, Vec<f64>), String> {
let n_buses = self.buses.len();
if n_buses == 0 {
return Ok((0.0, Vec::new()));
}
// Compute actual loads
let loads: Vec<f64> = self
.buses
.iter()
.enumerate()
.map(|(i, bus)| {
let factor = load_factors.get(i).copied().unwrap_or(1.0);
(bus.p_load_mw * factor).max(0.0)
})
.collect();
let total_load: f64 = loads.iter().sum();
// Merit-order dispatch: sort buses by generation cost (cheapest first)
let mut gen_order: Vec<usize> = (0..n_buses).collect();
gen_order.sort_by(|&a, &b| {
self.buses[a]
.gen_cost
.partial_cmp(&self.buses[b].gen_cost)
.unwrap_or(core::cmp::Ordering::Equal)
});
let mut generation = vec![0.0_f64; n_buses];
let mut remaining_demand = total_load;
for &bi in &gen_order {
if remaining_demand <= 0.0 {
break;
}
let gen_max = self.buses[bi].p_gen_max_mw;
if gen_max <= 0.0 {
continue;
}
let dispatch = gen_max.min(remaining_demand);
generation[bi] = dispatch;
remaining_demand -= dispatch;
}
// Remaining demand is load shedding
let total_shed = remaining_demand.max(0.0);
// Distribute load shedding proportionally to bus loads
let mut load_shed = vec![0.0_f64; n_buses];
if total_shed > 1e-9 && total_load > 1e-9 {
for (b, ls) in load_shed.iter_mut().enumerate() {
*ls = total_shed * loads[b] / total_load;
}
}
// Build active lines and compute PTDF for flow checking
let active_lines = self.build_active_lines(investments);
let ptdf = self.compute_ptdf(investments);
// Compute power injections
let mut p_inject = vec![0.0_f64; n_buses];
for b in 0..n_buses {
p_inject[b] = generation[b] - (loads[b] - load_shed[b]);
}
// Compute line flows and check thermal limits
let mut violation_penalty = 0.0_f64;
for (l, line) in active_lines.iter().enumerate() {
if let Some(ptdf_row) = ptdf.get(l) {
let flow: f64 = ptdf_row
.iter()
.enumerate()
.map(|(b, &p)| p * p_inject.get(b).copied().unwrap_or(0.0))
.sum();
let excess = flow.abs() - line.rating_mw;
if excess > 1e-6 {
// Penalise violation using average VOLL
let avg_voll: f64 =
self.buses.iter().map(|b| b.voll).sum::<f64>() / (n_buses as f64).max(1.0);
violation_penalty += excess * avg_voll * 1e-6; // convert to $M
}
}
}
// Generation cost
let gen_cost: f64 = self
.buses
.iter()
.enumerate()
.map(|(i, bus)| bus.gen_cost * generation.get(i).copied().unwrap_or(0.0))
.sum();
// Load shedding cost
let shed_cost: f64 = self
.buses
.iter()
.enumerate()
.map(|(i, bus)| bus.voll * load_shed.get(i).copied().unwrap_or(0.0))
.sum();
// Total operating cost in $M
let op_cost_m = (gen_cost + shed_cost) * 1e-6 + violation_penalty;
Ok((op_cost_m, load_shed))
}
/// Generate the worst-case scenario (adversarial) within the uncertainty set.
///
/// For fixed investments, finds the load realisation that maximises
/// operating cost.
fn generate_worst_case(&self, investments: &[usize]) -> PlanningScenario {
let n_buses = self.buses.len();
if n_buses == 0 {
return PlanningScenario {
id: 0,
name: "empty".to_string(),
load_factors: Vec::new(),
probability: 1.0,
};
}
match &self.config.uncertainty {
UncertaintySet::Box { load_deviation } => {
self.worst_case_box(investments, *load_deviation)
}
UncertaintySet::Polyhedral {
max_deviations,
load_deviation,
} => self.worst_case_polyhedral(investments, *max_deviations, *load_deviation),
UncertaintySet::Ellipsoidal { radius } => {
self.worst_case_ellipsoidal(investments, *radius)
}
}
}
/// Worst-case for box uncertainty: try all-high, then individual corners.
fn worst_case_box(&self, investments: &[usize], deviation: f64) -> PlanningScenario {
let n_buses = self.buses.len();
let hi = 1.0 + deviation;
let lo = 1.0 - deviation;
// Start with all-high as the default worst case
let all_high: Vec<f64> = vec![hi; n_buses];
let (cost_high, _) = self
.dc_opf(investments, &all_high)
.unwrap_or((0.0, Vec::new()));
let mut best_factors = all_high;
let mut best_cost = cost_high;
// Try individual corners using LCG to sample a subset
let max_samples = self
.config
.max_scenarios
.min(2_usize.pow(n_buses.min(10) as u32));
let mut lcg_state: u64 = 0xDEADBEEF_u64;
for _s in 0..max_samples {
let factors: Vec<f64> = (0..n_buses)
.map(|b| {
lcg_state = lcg_state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let bit = (lcg_state >> (33 + (b % 30) as u64)) & 1;
if bit == 1 {
hi
} else {
lo
}
})
.collect();
let (cost, _) = self
.dc_opf(investments, &factors)
.unwrap_or((0.0, Vec::new()));
if cost > best_cost {
best_cost = cost;
best_factors = factors;
}
}
PlanningScenario {
id: 0,
name: format!("box_worst_{:.4}", best_cost),
load_factors: best_factors,
probability: 1.0,
}
}
/// Worst-case for polyhedral uncertainty: budget-constrained deviations.
fn worst_case_polyhedral(
&self,
investments: &[usize],
max_deviations: usize,
deviation: f64,
) -> PlanningScenario {
let n_buses = self.buses.len();
let hi = 1.0 + deviation;
// Heuristic: try increasing load at buses with highest base load first
// (they contribute most to total load and thus operating cost)
let mut bus_order: Vec<usize> = (0..n_buses).collect();
bus_order.sort_by(|&a, &b| {
self.buses[b]
.p_load_mw
.partial_cmp(&self.buses[a].p_load_mw)
.unwrap_or(core::cmp::Ordering::Equal)
});
let effective_max = max_deviations.min(n_buses);
// Try the top-K highest-load buses at +deviation
let mut best_factors = vec![1.0_f64; n_buses];
for &b in bus_order.iter().take(effective_max) {
best_factors[b] = hi;
}
let (mut best_cost, _) = self
.dc_opf(investments, &best_factors)
.unwrap_or((0.0, Vec::new()));
// Also try LCG-sampled subsets
let mut lcg_state: u64 = 0xCAFEBABE_u64;
let max_samples = self.config.max_scenarios.min(100);
for _ in 0..max_samples {
let mut factors = vec![1.0_f64; n_buses];
let mut n_deviated = 0_usize;
// Randomly select which buses to deviate
#[allow(clippy::needless_range_loop)]
for b in 0..n_buses {
if n_deviated >= effective_max {
break;
}
lcg_state = lcg_state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let coin = (lcg_state >> 33) as f64 / (u32::MAX as f64);
if coin > 0.5 {
factors[b] = hi;
n_deviated += 1;
}
}
let (cost, _) = self
.dc_opf(investments, &factors)
.unwrap_or((0.0, Vec::new()));
if cost > best_cost {
best_cost = cost;
best_factors = factors;
}
}
// Verify budget constraint: count deviations
let n_dev: usize = best_factors
.iter()
.filter(|&&f| (f - 1.0).abs() > 1e-9)
.count();
if n_dev > effective_max {
// Trim excess deviations (remove smallest-load buses)
let mut deviated_buses: Vec<usize> = best_factors
.iter()
.enumerate()
.filter(|(_, &f)| (f - 1.0).abs() > 1e-9)
.map(|(i, _)| i)
.collect();
deviated_buses.sort_by(|&a, &b| {
self.buses[a]
.p_load_mw
.partial_cmp(&self.buses[b].p_load_mw)
.unwrap_or(core::cmp::Ordering::Equal)
});
for &b in deviated_buses.iter().take(n_dev - effective_max) {
best_factors[b] = 1.0;
}
}
PlanningScenario {
id: 0,
name: format!("poly_worst_{:.4}", best_cost),
load_factors: best_factors,
probability: 1.0,
}
}
/// Worst-case for ellipsoidal uncertainty: gradient ascent on the sphere.
fn worst_case_ellipsoidal(&self, investments: &[usize], radius: f64) -> PlanningScenario {
let n_buses = self.buses.len();
if n_buses == 0 {
return PlanningScenario {
id: 0,
name: "ellip_empty".to_string(),
load_factors: Vec::new(),
probability: 1.0,
};
}
// Gradient ascent: perturb each bus load and estimate gradient,
// then project onto ellipsoid surface.
let base_factors = vec![1.0_f64; n_buses];
let (base_cost, _) = self
.dc_opf(investments, &base_factors)
.unwrap_or((0.0, Vec::new()));
// Estimate gradient of cost w.r.t. load factor perturbation
let eps = 0.01;
let mut gradient = vec![0.0_f64; n_buses];
for b in 0..n_buses {
let mut perturbed = base_factors.clone();
perturbed[b] += eps;
let (cost_p, _) = self
.dc_opf(investments, &perturbed)
.unwrap_or((0.0, Vec::new()));
gradient[b] = (cost_p - base_cost) / eps;
}
// Normalise gradient and scale to ellipsoid surface
let grad_norm: f64 = gradient.iter().map(|&g| g * g).sum::<f64>().sqrt();
let factors: Vec<f64> = if grad_norm > 1e-12 {
gradient
.iter()
.map(|&g| 1.0 + radius * g / grad_norm)
.collect()
} else {
// Uniform perturbation on the sphere
let uniform_dir = radius / (n_buses as f64).sqrt();
vec![1.0 + uniform_dir; n_buses]
};
let (cost, _) = self
.dc_opf(investments, &factors)
.unwrap_or((0.0, Vec::new()));
PlanningScenario {
id: 0,
name: format!("ellip_worst_{:.4}", cost),
load_factors: factors,
probability: 1.0,
}
}
/// Compute the PTDF matrix for the current network topology.
///
/// Returns `ptdf[line][bus]` — the sensitivity of flow on `line`
/// to injection at `bus`, with bus 0 as the reference.
fn compute_ptdf(&self, investments: &[usize]) -> Vec<Vec<f64>> {
let n_buses = self.buses.len();
let active_lines = self.build_active_lines(investments);
if n_buses == 0 || active_lines.is_empty() {
return Vec::new();
}
// Build susceptance matrix B
let mut b_mat = vec![vec![0.0_f64; n_buses]; n_buses];
for line in &active_lines {
let f = line.from_bus;
let t = line.to_bus;
if f >= n_buses || t >= n_buses || f == t {
continue;
}
let b = 1.0 / line.reactance_pu.max(1e-8);
b_mat[f][f] += b;
b_mat[t][t] += b;
b_mat[f][t] -= b;
b_mat[t][f] -= b;
}
// Reduced B (remove reference bus 0)
let n_red = n_buses.saturating_sub(1);
if n_red == 0 {
return vec![vec![0.0; n_buses]; active_lines.len()];
}
let mut b_red = vec![vec![0.0_f64; n_red]; n_red];
for i in 0..n_red {
for j in 0..n_red {
b_red[i][j] = b_mat[i + 1][j + 1];
}
}
// Solve B_red * theta_col = e_b for each non-ref bus
let mut theta_cols = vec![vec![0.0_f64; n_red]; n_red];
for b in 0..n_red {
let mut rhs = vec![0.0_f64; n_red];
rhs[b] = 1.0;
if let Some(sol) = gaussian_solve(&b_red, &rhs) {
theta_cols[b] = sol;
}
}
// Compute PTDF[line][bus]
let mut ptdf = vec![vec![0.0_f64; n_buses]; active_lines.len()];
for (l, line) in active_lines.iter().enumerate() {
let f = line.from_bus;
let t = line.to_bus;
if f >= n_buses || t >= n_buses {
continue;
}
let b_line = 1.0 / line.reactance_pu.max(1e-8);
#[allow(clippy::needless_range_loop)]
for bus in 0..n_buses {
let theta_f = if f == 0 || bus == 0 {
0.0
} else {
theta_cols
.get(bus - 1)
.and_then(|col| col.get(f - 1))
.copied()
.unwrap_or(0.0)
};
let theta_t = if t == 0 || bus == 0 {
0.0
} else {
theta_cols
.get(bus - 1)
.and_then(|col| col.get(t - 1))
.copied()
.unwrap_or(0.0)
};
ptdf[l][bus] = b_line * (theta_f - theta_t);
}
}
ptdf
}
/// Greedy investment selection based on load-shedding reduction benefit.
///
/// Ranks candidates by `(load_shedding_reduction * VOLL) / investment_cost`
/// and builds one circuit at a time.
#[allow(dead_code)]
fn greedy_investment(&self, load_shedding: &[f64]) -> Vec<usize> {
let n_cands = self.candidates.len();
let n_buses = self.buses.len();
let mut investments = vec![0_usize; n_cands];
let total_ls: f64 = load_shedding.iter().sum();
if total_ls < 1e-9 || n_cands == 0 {
return investments;
}
let avg_voll: f64 =
self.buses.iter().map(|b| b.voll).sum::<f64>() / (n_buses as f64).max(1.0);
// Rank by benefit/cost ratio
let mut ranked: Vec<(usize, f64)> = self
.candidates
.iter()
.enumerate()
.map(|(i, c)| {
// Benefit: proportional to rating (congestion relief) and load shed
let benefit = c.rating_mw * total_ls * avg_voll;
let cost = c.investment_cost_m_usd.max(1e-10) * 1e6; // convert to $
let ratio = benefit / cost;
(i, ratio)
})
.collect();
ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(core::cmp::Ordering::Equal));
// Greedily build one circuit for each candidate (by rank)
for (i, _) in ranked {
if investments[i] < self.candidates[i].max_circuits {
investments[i] += 1;
}
}
investments
}
/// Check N-1 security: remove each existing line one at a time and verify
/// that the system can still serve all load.
fn check_n1_security(&self, investments: &[usize], scenario: &PlanningScenario) -> bool {
let n_buses = self.buses.len();
for skip_line in 0..self.existing_lines.len() {
// Build active lines minus the contingency line
let mut lines: Vec<ActiveLine> = Vec::new();
for (i, l) in self.existing_lines.iter().enumerate() {
if i == skip_line {
continue;
}
lines.push(ActiveLine {
from_bus: l.from_bus,
to_bus: l.to_bus,
reactance_pu: l.x_pu.max(1e-8),
rating_mw: l.rating_mw,
});
}
// Add invested candidates
for (i, cand) in self.candidates.iter().enumerate() {
let nc = investments.get(i).copied().unwrap_or(0);
for _ in 0..nc {
lines.push(ActiveLine {
from_bus: cand.from_bus,
to_bus: cand.to_bus,
reactance_pu: cand.x_pu.max(1e-8),
rating_mw: cand.rating_mw,
});
}
}
// Simple feasibility check: can generation meet load?
let total_load: f64 = self
.buses
.iter()
.enumerate()
.map(|(i, bus)| {
let factor = scenario.load_factors.get(i).copied().unwrap_or(1.0);
bus.p_load_mw * factor
})
.sum();
let total_gen: f64 = self.buses.iter().map(|b| b.p_gen_max_mw).sum();
if total_gen < total_load - 1e-3 {
return false;
}
// Check if the network is connected (simple BFS)
if !is_connected(n_buses, &lines) {
return false;
}
}
true
}
/// Build the list of active lines (existing + invested candidates).
fn build_active_lines(&self, investments: &[usize]) -> Vec<ActiveLine> {
let mut lines: Vec<ActiveLine> = self
.existing_lines
.iter()
.map(|l| ActiveLine {
from_bus: l.from_bus,
to_bus: l.to_bus,
reactance_pu: l.x_pu.max(1e-8),
rating_mw: l.rating_mw,
})
.collect();
for (i, cand) in self.candidates.iter().enumerate() {
let nc = investments.get(i).copied().unwrap_or(0);
for _ in 0..nc {
lines.push(ActiveLine {
from_bus: cand.from_bus,
to_bus: cand.to_bus,
reactance_pu: cand.x_pu.max(1e-8),
rating_mw: cand.rating_mw,
});
}
}
lines
}
}
/// Compute the relative optimality gap.
fn relative_gap(ub: f64, lb: f64) -> f64 {
if ub == f64::INFINITY || lb == f64::NEG_INFINITY {
return 1.0;
}
let denom = lb.abs() + 1e-10;
((ub - lb) / denom).abs()
}
/// Gaussian elimination with partial pivoting.
///
/// Solves `A x = b` and returns `x`, or `None` if the system is singular.
fn gaussian_solve(a: &[Vec<f64>], b: &[f64]) -> Option<Vec<f64>> {
let n = a.len();
if n == 0 || b.len() != n {
return None;
}
// Build augmented matrix [A | b]
let mut aug: Vec<Vec<f64>> = a
.iter()
.enumerate()
.map(|(i, row)| {
let mut r = row.clone();
r.push(b[i]);
r
})
.collect();
#[allow(clippy::needless_range_loop)]
for col in 0..n {
// Partial pivot
let mut max_row = col;
let mut max_val = aug[col][col].abs();
for row in (col + 1)..n {
let v = aug[row][col].abs();
if v > max_val {
max_val = v;
max_row = row;
}
}
if max_val < 1e-14 {
return None;
}
aug.swap(col, max_row);
let pivot = aug[col][col];
for j in col..=n {
aug[col][j] /= pivot;
}
for row in 0..n {
if row == col {
continue;
}
let factor = aug[row][col];
for j in col..=n {
let val = aug[col][j] * factor;
aug[row][j] -= val;
}
}
}
Some((0..n).map(|i| aug[i][n]).collect())
}
/// BFS connectivity check on undirected graph.
fn is_connected(n_nodes: usize, lines: &[ActiveLine]) -> bool {
if n_nodes <= 1 {
return true;
}
if lines.is_empty() {
return n_nodes <= 1;
}
// Adjacency list
let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n_nodes];
for line in lines {
if line.from_bus < n_nodes && line.to_bus < n_nodes {
adj[line.from_bus].push(line.to_bus);
adj[line.to_bus].push(line.from_bus);
}
}
let mut visited = vec![false; n_nodes];
let mut stack = vec![0_usize];
visited[0] = true;
let mut count = 1_usize;
while let Some(node) = stack.pop() {
for &neighbor in adj.get(node).map(|v| v.as_slice()).unwrap_or(&[]) {
if !visited[neighbor] {
visited[neighbor] = true;
count += 1;
stack.push(neighbor);
}
}
}
count == n_nodes
}
#[cfg(test)]
mod tests {
use super::*;
/// Build a 3-bus Garver-like test network.
fn make_garver_3bus() -> RobustTepSolver {
let config = RobustTepConfig {
max_benders_iter: 20,
optimality_gap: 0.01,
max_scenarios: 50,
uncertainty: UncertaintySet::Box {
load_deviation: 0.2,
},
discount_rate: 0.08,
planning_horizon_years: 20,
base_mva: 100.0,
};
let mut solver = RobustTepSolver::new(config);
solver.add_bus(TepBus {
bus_id: 0,
p_load_mw: 80.0,
p_gen_max_mw: 300.0,
gen_cost: 25.0,
voll: 10_000.0,
});
solver.add_bus(TepBus {
bus_id: 1,
p_load_mw: 120.0,
p_gen_max_mw: 100.0,
gen_cost: 40.0,
voll: 10_000.0,
});
solver.add_bus(TepBus {
bus_id: 2,
p_load_mw: 150.0,
p_gen_max_mw: 0.0,
gen_cost: 0.0,
voll: 10_000.0,
});
solver.add_existing_line(ExistingLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 200.0,
});
solver.add_candidate(CandidateLine {
id: 0,
from_bus: 1,
to_bus: 2,
x_pu: 0.1,
rating_mw: 150.0,
investment_cost_m_usd: 10.0,
construction_years: 2.0,
max_circuits: 2,
});
solver.add_candidate(CandidateLine {
id: 1,
from_bus: 0,
to_bus: 2,
x_pu: 0.15,
rating_mw: 100.0,
investment_cost_m_usd: 20.0,
construction_years: 3.0,
max_circuits: 1,
});
solver
}
// 1. 3-bus Garver: some investment selected
#[test]
fn test_garver_3bus_investment_selected() {
let solver = make_garver_3bus();
let result = solver.solve().expect("solver should succeed");
let total_circuits: usize = result.investments.iter().map(|d| d.n_circuits).sum();
assert!(total_circuits > 0, "Should invest in at least one circuit");
}
// 2. Investment cost positive
#[test]
fn test_investment_cost_positive() {
let solver = make_garver_3bus();
let result = solver.solve().expect("solver should succeed");
assert!(
result.total_investment_m_usd > 0.0,
"Investment cost should be positive: {}",
result.total_investment_m_usd
);
}
// 3. Total cost = investment + operation (NPV)
#[test]
fn test_total_cost_composition() {
let solver = make_garver_3bus();
let result = solver.solve().expect("solver should succeed");
let expected = result.total_investment_m_usd
+ result.expected_operation_cost_m_usd * solver.npv_factor();
assert!(
(result.total_cost_m_usd - expected).abs() < 1e-6,
"total_cost ({:.4}) should equal invest ({:.4}) + npv(op) ({:.4})",
result.total_cost_m_usd,
result.total_investment_m_usd,
result.expected_operation_cost_m_usd * solver.npv_factor()
);
}
// 4. Converged within max iterations
#[test]
fn test_converged_within_iterations() {
let mut solver = make_garver_3bus();
solver.config.max_benders_iter = 50;
solver.config.optimality_gap = 0.5; // generous gap
let result = solver.solve().expect("solver should succeed");
assert!(
result.converged || result.iterations <= 50,
"Should converge or complete within iter limit"
);
}
// 5. Load shedding zero after sufficient investment
#[test]
fn test_zero_load_shedding_with_ample_gen() {
let mut solver = RobustTepSolver::new(RobustTepConfig {
uncertainty: UncertaintySet::Box {
load_deviation: 0.0,
},
..RobustTepConfig::default()
});
solver.add_bus(TepBus {
bus_id: 0,
p_load_mw: 50.0,
p_gen_max_mw: 500.0,
gen_cost: 20.0,
voll: 10_000.0,
});
solver.add_bus(TepBus {
bus_id: 1,
p_load_mw: 50.0,
p_gen_max_mw: 500.0,
gen_cost: 30.0,
voll: 10_000.0,
});
solver.add_existing_line(ExistingLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.05,
rating_mw: 500.0,
});
let result = solver.solve().expect("solve ok");
assert!(
result.worst_case_load_shedding_mw < 1.0,
"No load shedding expected: {:.2} MW",
result.worst_case_load_shedding_mw
);
}
// 6. DC-OPF power balance
#[test]
fn test_dc_opf_power_balance() {
let solver = make_garver_3bus();
let investments = vec![1_usize, 0];
let factors = vec![1.0, 1.0, 1.0];
let (_cost, ls) = solver.dc_opf(&investments, &factors).expect("dc_opf ok");
let total_load: f64 = solver.buses.iter().map(|b| b.p_load_mw).sum();
let total_gen: f64 = solver.buses.iter().map(|b| b.p_gen_max_mw).sum();
let total_ls: f64 = ls.iter().sum();
// generation dispatched + load shed should cover total load (approximately)
// total_gen >= total_load - total_ls (generation sufficiency)
assert!(
total_gen >= total_load - total_ls - 1e-3,
"Power balance violated: gen={}, load={}, shed={}",
total_gen,
total_load,
total_ls
);
}
// 7. DC-OPF generation within limits
#[test]
fn test_dc_opf_gen_within_limits() {
let solver = make_garver_3bus();
let investments = vec![1, 1];
let factors = vec![1.0, 1.0, 1.0];
let result = solver.dc_opf(&investments, &factors);
// Verify the dc_opf returned without error (limits enforced internally)
assert!(result.is_ok(), "DC-OPF should not fail");
}
// 8. PTDF row sums approximately 0
#[test]
fn test_ptdf_row_sums_zero() {
let solver = make_garver_3bus();
let investments = vec![1, 0];
let ptdf = solver.compute_ptdf(&investments);
for (l, row) in ptdf.iter().enumerate() {
let row_sum: f64 = row.iter().sum();
// PTDF rows don't strictly sum to zero, but columns for non-ref buses
// have specific structure. Check finite values.
assert!(
row_sum.is_finite(),
"PTDF row {} sum should be finite: {}",
l,
row_sum
);
}
}
// 9. Greedy selects cheapest effective line first
#[test]
fn test_greedy_cheapest_first() {
let solver = make_garver_3bus();
let load_shed = vec![50.0, 30.0, 80.0];
let inv = solver.greedy_investment(&load_shed);
// Candidate 0 costs 10 M$, candidate 1 costs 20 M$
// Both should get circuits but candidate 0 has better ratio
assert!(
inv[0] > 0,
"Cheaper candidate should get at least 1 circuit"
);
}
// 10. Box uncertainty: loads vary within bounds
#[test]
fn test_box_uncertainty_bounds() {
let solver = make_garver_3bus();
let investments = vec![0, 0];
let scenario = solver.generate_worst_case(&investments);
let dev = 0.2;
for &f in &scenario.load_factors {
assert!(
f >= 1.0 - dev - 1e-9 && f <= 1.0 + dev + 1e-9,
"Load factor {:.4} outside box [{:.2}, {:.2}]",
f,
1.0 - dev,
1.0 + dev
);
}
}
// 11. Polyhedral: max_deviations respected
#[test]
fn test_polyhedral_budget_constraint() {
let mut solver = make_garver_3bus();
solver.config.uncertainty = UncertaintySet::Polyhedral {
max_deviations: 1,
load_deviation: 0.3,
};
let investments = vec![0, 0];
let scenario = solver.generate_worst_case(&investments);
let n_deviated: usize = scenario
.load_factors
.iter()
.filter(|&&f| (f - 1.0).abs() > 1e-9)
.count();
assert!(
n_deviated <= 1,
"At most 1 deviation allowed, got {}",
n_deviated
);
}
// 12. Worst-case has highest load shedding
#[test]
fn test_worst_case_high_cost() {
let solver = make_garver_3bus();
let investments = vec![0, 0];
let worst = solver.generate_worst_case(&investments);
let (worst_cost, _) = solver
.dc_opf(&investments, &worst.load_factors)
.expect("dc_opf ok");
// Compare to base case (all factors = 1.0)
let base_factors = vec![1.0; solver.buses.len()];
let (base_cost, _) = solver
.dc_opf(&investments, &base_factors)
.expect("dc_opf ok");
assert!(
worst_cost >= base_cost - 1e-6,
"Worst case ({:.4}) should cost at least as much as base ({:.4})",
worst_cost,
base_cost
);
}
// 13. Benders cut RHS is finite
#[test]
fn test_benders_cut_rhs_finite() {
let solver = make_garver_3bus();
let investments = vec![1, 0];
let scenario = PlanningScenario {
id: 0,
name: "test".to_string(),
load_factors: vec![1.0, 1.0, 1.0],
probability: 1.0,
};
let (_, cut) = solver
.solve_subproblem(&investments, &scenario)
.expect("sub ok");
assert!(
cut.rhs.is_finite(),
"Benders cut RHS should be finite: {}",
cut.rhs
);
}
// 14. Upper bound >= lower bound
#[test]
fn test_ub_geq_lb() {
let solver = make_garver_3bus();
let result = solver.solve().expect("solve ok");
assert!(
result.upper_bound >= result.lower_bound - 1e-6,
"UB ({:.4}) should be >= LB ({:.4})",
result.upper_bound,
result.lower_bound
);
}
// 15. N-1 secure after investment
#[test]
fn test_n1_secure_after_investment() {
let mut solver = RobustTepSolver::new(RobustTepConfig {
uncertainty: UncertaintySet::Box {
load_deviation: 0.0,
},
..RobustTepConfig::default()
});
// Simple 2-bus system with plenty of gen and redundant lines
solver.add_bus(TepBus {
bus_id: 0,
p_load_mw: 50.0,
p_gen_max_mw: 200.0,
gen_cost: 20.0,
voll: 10_000.0,
});
solver.add_bus(TepBus {
bus_id: 1,
p_load_mw: 50.0,
p_gen_max_mw: 200.0,
gen_cost: 30.0,
voll: 10_000.0,
});
solver.add_existing_line(ExistingLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 200.0,
});
solver.add_existing_line(ExistingLine {
id: 1,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 200.0,
});
let result = solver.solve().expect("solve ok");
assert!(
result.n1_secure,
"Should be N-1 secure with redundant lines"
);
}
// 16. NPV factor positive and < planning_horizon
#[test]
fn test_npv_factor_range() {
let solver = make_garver_3bus();
let npv = solver.npv_factor();
let horizon = solver.config.planning_horizon_years as f64;
assert!(npv > 0.0, "NPV factor should be positive: {}", npv);
assert!(
npv < horizon + 1e-3,
"NPV factor ({:.4}) should be < horizon ({:.1})",
npv,
horizon
);
}
// 17. No candidates: no investment, load shedding reported
#[test]
fn test_no_candidates() {
let mut solver = RobustTepSolver::new(RobustTepConfig {
uncertainty: UncertaintySet::Box {
load_deviation: 0.1,
},
..RobustTepConfig::default()
});
solver.add_bus(TepBus {
bus_id: 0,
p_load_mw: 100.0,
p_gen_max_mw: 50.0,
gen_cost: 30.0,
voll: 10_000.0,
});
// No existing lines, no candidates
let result = solver.solve().expect("solve ok");
assert!(
result.investments.is_empty(),
"No candidates => no investments"
);
assert!(
result.total_investment_m_usd < 1e-9,
"No investment cost expected"
);
}
// 18. Single candidate selected if beneficial
#[test]
fn test_single_candidate_selected() {
let mut solver = RobustTepSolver::new(RobustTepConfig {
uncertainty: UncertaintySet::Box {
load_deviation: 0.0,
},
max_benders_iter: 20,
..RobustTepConfig::default()
});
solver.add_bus(TepBus {
bus_id: 0,
p_load_mw: 50.0,
p_gen_max_mw: 300.0,
gen_cost: 20.0,
voll: 10_000.0,
});
solver.add_bus(TepBus {
bus_id: 1,
p_load_mw: 200.0,
p_gen_max_mw: 0.0,
gen_cost: 0.0,
voll: 10_000.0,
});
solver.add_existing_line(ExistingLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 50.0, // undersized
});
solver.add_candidate(CandidateLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 200.0,
investment_cost_m_usd: 5.0,
construction_years: 1.0,
max_circuits: 1,
});
let result = solver.solve().expect("solve ok");
// The candidate should be selected because there is significant load shed
assert_eq!(result.investments.len(), 1);
}
// 19. Deterministic same as robust with zero deviation
#[test]
fn test_deterministic_matches_zero_deviation() {
let mut solver = RobustTepSolver::new(RobustTepConfig {
uncertainty: UncertaintySet::Box {
load_deviation: 0.0,
},
max_benders_iter: 10,
..RobustTepConfig::default()
});
solver.add_bus(TepBus {
bus_id: 0,
p_load_mw: 80.0,
p_gen_max_mw: 200.0,
gen_cost: 25.0,
voll: 10_000.0,
});
solver.add_bus(TepBus {
bus_id: 1,
p_load_mw: 100.0,
p_gen_max_mw: 100.0,
gen_cost: 35.0,
voll: 10_000.0,
});
solver.add_existing_line(ExistingLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 200.0,
});
solver.add_candidate(CandidateLine {
id: 0,
from_bus: 0,
to_bus: 1,
x_pu: 0.1,
rating_mw: 100.0,
investment_cost_m_usd: 10.0,
construction_years: 2.0,
max_circuits: 1,
});
let robust_result = solver.solve().expect("robust ok");
let det_scenario = PlanningScenario {
id: 0,
name: "base".to_string(),
load_factors: vec![1.0; 2],
probability: 1.0,
};
let det_result = solver.solve_deterministic(&det_scenario).expect("det ok");
// With zero deviation, worst-case factors should be ~1.0
// so costs should be comparable
let cost_diff = (robust_result.total_cost_m_usd - det_result.total_cost_m_usd).abs();
let max_cost = robust_result
.total_cost_m_usd
.max(det_result.total_cost_m_usd)
.max(1.0);
assert!(
cost_diff / max_cost < 0.5,
"Robust and det costs should be similar with zero deviation: {:.4} vs {:.4}",
robust_result.total_cost_m_usd,
det_result.total_cost_m_usd
);
}
// 20. n_circuits <= max_circuits
#[test]
fn test_n_circuits_within_max() {
let solver = make_garver_3bus();
let result = solver.solve().expect("solve ok");
for dec in &result.investments {
let cand = solver.candidates.iter().find(|c| c.id == dec.line_id);
if let Some(c) = cand {
assert!(
dec.n_circuits <= c.max_circuits,
"Line {} has {} circuits but max is {}",
dec.line_id,
dec.n_circuits,
c.max_circuits
);
}
}
}
// 21. Total investment = sum of individual investments
#[test]
fn test_total_investment_sum() {
let solver = make_garver_3bus();
let result = solver.solve().expect("solve ok");
let sum: f64 = result
.investments
.iter()
.map(|d| d.investment_cost_m_usd)
.sum();
assert!(
(result.total_investment_m_usd - sum).abs() < 1e-6,
"Total ({:.4}) should equal sum of individual ({:.4})",
result.total_investment_m_usd,
sum
);
}
// 22. Multiple iterations: bounds converge
#[test]
fn test_bounds_converge() {
let mut solver = make_garver_3bus();
solver.config.max_benders_iter = 30;
let result = solver.solve().expect("solve ok");
let gap = relative_gap(result.upper_bound, result.lower_bound);
// With enough iterations, gap should be reasonable
assert!(
gap < 10.0 || result.converged,
"Gap ({:.4}) should be bounded or converged",
gap
);
}
// 23. Ellipsoidal uncertainty test
#[test]
fn test_ellipsoidal_uncertainty() {
let mut solver = make_garver_3bus();
solver.config.uncertainty = UncertaintySet::Ellipsoidal { radius: 0.3 };
let investments = vec![0, 0];
let scenario = solver.generate_worst_case(&investments);
// Load factors should be finite and > 0
for &f in &scenario.load_factors {
assert!(
f.is_finite() && f > 0.0,
"Load factor should be finite positive: {}",
f
);
}
}
// 24. Empty system error handling
#[test]
fn test_empty_system_error() {
let solver = RobustTepSolver::new(RobustTepConfig::default());
let result = solver.solve();
assert!(result.is_err(), "Empty system should return error");
}
// 25. PTDF matrix dimensions
#[test]
fn test_ptdf_dimensions() {
let solver = make_garver_3bus();
let investments = vec![1, 1];
let ptdf = solver.compute_ptdf(&investments);
let n_active = solver.build_active_lines(&investments).len();
assert_eq!(
ptdf.len(),
n_active,
"PTDF should have {} rows (one per active line), got {}",
n_active,
ptdf.len()
);
for (l, row) in ptdf.iter().enumerate() {
assert_eq!(
row.len(),
solver.buses.len(),
"PTDF row {} should have {} cols, got {}",
l,
solver.buses.len(),
row.len()
);
}
}
// 26. Gaussian solve basic
#[test]
fn test_gaussian_solve() {
// Solve [2 1; 1 3] x = [5; 7] => x = [8/5, 9/5]
let a = vec![vec![2.0, 1.0], vec![1.0, 3.0]];
let b = vec![5.0, 7.0];
let sol = gaussian_solve(&a, &b).expect("should solve");
assert!((sol[0] - 1.6).abs() < 1e-9, "x0 = {}", sol[0]);
assert!((sol[1] - 1.8).abs() < 1e-9, "x1 = {}", sol[1]);
}
// 27. Connectivity check
#[test]
fn test_connectivity() {
let lines = vec![
ActiveLine {
from_bus: 0,
to_bus: 1,
reactance_pu: 0.1,
rating_mw: 100.0,
},
ActiveLine {
from_bus: 1,
to_bus: 2,
reactance_pu: 0.1,
rating_mw: 100.0,
},
];
assert!(is_connected(3, &lines), "3-bus chain should be connected");
assert!(
!is_connected(4, &lines),
"4-bus with 2 lines should be disconnected"
);
}
}