scirs2-optimize 0.4.2

Optimization module for SciRS2 (scirs2-optimize)
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
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
//! Penalty Methods for Constrained Optimization
//!
//! Penalty methods transform constrained problems into a sequence of unconstrained
//! problems by adding a penalty term to the objective function that penalizes
//! constraint violations.
//!
//! Two major classes are implemented:
//!
//! ## External Penalty Method
//! Adds a penalty for constraint violations:
//! ```text
//! P(x, mu) = f(x) + mu * sum_i max(0, g_i(x))^2 + mu * sum_j h_j(x)^2
//! ```
//! The penalty parameter mu is increased until the constraints are satisfied.
//!
//! ## Interior Penalty (Barrier) Method
//! Adds a barrier function that prevents leaving the feasible region:
//! ```text
//! B(x, mu) = f(x) - mu * sum_i ln(-g_i(x))   (log barrier)
//! ```
//! The barrier parameter mu is decreased to zero.
//!
//! # References
//! - Fiacco, A.V. & McCormick, G.P. (1968). "Nonlinear Programming: Sequential
//!   Unconstrained Minimization Techniques." SIAM.
//! - Nocedal, J. & Wright, S.J. (2006). "Numerical Optimization." Chapter 17.

use crate::error::{OptimizeError, OptimizeResult};
use crate::result::OptimizeResults;
use scirs2_core::ndarray::{Array1, ArrayView1};

/// Type of penalty method
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PenaltyKind {
    /// External penalty: quadratic penalty for violations
    External,
    /// Interior penalty (log-barrier): only works from strictly feasible start
    Interior,
    /// Exact L1 penalty: |max(0, g_i(x))| + |h_j(x)|
    ExactL1,
}

/// Options for the penalty method
#[derive(Debug, Clone)]
pub struct PenaltyOptions {
    /// Penalty method kind
    pub kind: PenaltyKind,
    /// Initial penalty parameter
    pub mu_init: f64,
    /// Maximum penalty parameter (external) / min barrier (interior)
    pub mu_max: f64,
    /// Penalty increase factor (external) or decrease factor (interior)
    pub mu_factor: f64,
    /// Maximum number of outer iterations
    pub max_outer_iter: usize,
    /// Tolerance for constraint violation
    pub constraint_tol: f64,
    /// Tolerance for optimality of subproblem
    pub optimality_tol: f64,
    /// Finite difference step for gradient
    pub eps: f64,
}

impl Default for PenaltyOptions {
    fn default() -> Self {
        PenaltyOptions {
            kind: PenaltyKind::External,
            mu_init: 1.0,
            mu_max: 1e10,
            mu_factor: 10.0,
            max_outer_iter: 100,
            constraint_tol: 1e-6,
            optimality_tol: 1e-8,
            eps: 1e-7,
        }
    }
}

/// Result from penalty method
#[derive(Debug, Clone)]
pub struct PenaltyResult {
    /// Optimal solution
    pub x: Array1<f64>,
    /// Optimal objective value
    pub fun: f64,
    /// Number of outer iterations
    pub nit: usize,
    /// Total number of function evaluations
    pub nfev: usize,
    /// Success flag
    pub success: bool,
    /// Status message
    pub message: String,
    /// Final penalty parameter
    pub mu: f64,
    /// Final constraint violation
    pub constraint_violation: f64,
}

impl From<PenaltyResult> for OptimizeResults<f64> {
    fn from(r: PenaltyResult) -> Self {
        OptimizeResults {
            x: r.x,
            fun: r.fun,
            jac: None,
            hess: None,
            constr: None,
            nit: r.nit,
            nfev: r.nfev,
            njev: 0,
            nhev: 0,
            maxcv: 0,
            message: r.message,
            success: r.success,
            status: if r.success { 0 } else { 1 },
        }
    }
}

/// Internal gradient-based minimizer for penalty subproblems.
/// Uses L-BFGS-like updates with simple backtracking line search.
fn minimize_penalty_subproblem<P>(
    penalty_fn: P,
    x0: &[f64],
    max_iter: usize,
    gtol: f64,
    eps: f64,
    nfev: &mut usize,
) -> Vec<f64>
where
    P: Fn(&[f64]) -> f64,
{
    let n = x0.len();
    let mut x = x0.to_vec();
    let m = 5usize; // L-BFGS history

    let mut s_hist: Vec<Vec<f64>> = Vec::new();
    let mut y_hist: Vec<Vec<f64>> = Vec::new();
    let mut rho_hist: Vec<f64> = Vec::new();

    let compute_grad = |xv: &[f64], nfev: &mut usize| -> Vec<f64> {
        let h = eps;
        let mut g = vec![0.0; n];
        let mut xp = xv.to_vec();
        let mut xm = xv.to_vec();
        *nfev += 2 * n;
        for i in 0..n {
            xp[i] = xv[i] + h;
            xm[i] = xv[i] - h;
            g[i] = (penalty_fn(&xp) - penalty_fn(&xm)) / (2.0 * h);
            xp[i] = xv[i];
            xm[i] = xv[i];
        }
        g
    };

    let mut g = compute_grad(&x, nfev);

    for _iter in 0..max_iter {
        let gnorm: f64 = g.iter().map(|v| v * v).sum::<f64>().sqrt();
        if gnorm < gtol {
            break;
        }

        // L-BFGS two-loop recursion
        let mut q = g.clone();
        let hist_len = s_hist.len();
        let mut alpha_hist = vec![0.0_f64; hist_len];

        for i in (0..hist_len).rev() {
            let si = &s_hist[i];
            let yi = &y_hist[i];
            let rho_i = rho_hist[i];
            let dot: f64 = si.iter().zip(q.iter()).map(|(&s, &qi)| s * qi).sum();
            alpha_hist[i] = rho_i * dot;
            let a = alpha_hist[i];
            for j in 0..n {
                q[j] -= a * yi[j];
            }
        }

        // Initial Hessian scaling
        let mut r = if hist_len > 0 {
            let last_s = &s_hist[hist_len - 1];
            let last_y = &y_hist[hist_len - 1];
            let sy: f64 = last_s.iter().zip(last_y.iter()).map(|(&s, &y)| s * y).sum();
            let yy: f64 = last_y.iter().map(|v| v * v).sum();
            let scale = if yy > 1e-15 { sy / yy } else { 1.0 };
            q.iter().map(|&qi| scale * qi).collect::<Vec<f64>>()
        } else {
            q.clone()
        };

        for i in 0..hist_len {
            let si = &s_hist[i];
            let yi = &y_hist[i];
            let rho_i = rho_hist[i];
            let dot: f64 = yi.iter().zip(r.iter()).map(|(&y, &ri)| y * ri).sum();
            let beta = rho_i * dot;
            let diff = alpha_hist[i] - beta;
            for j in 0..n {
                r[j] += si[j] * diff;
            }
        }

        // Direction: d = -r
        let d: Vec<f64> = r.iter().map(|v| -v).collect();

        // Backtracking line search
        *nfev += 1;
        let fx = penalty_fn(&x);
        let dg: f64 = d.iter().zip(g.iter()).map(|(&di, &gi)| di * gi).sum();
        let mut alpha = 1.0_f64;

        for _ls in 0..20 {
            let xnew: Vec<f64> = x.iter().zip(d.iter()).map(|(&xi, &di)| xi + alpha * di).collect();
            *nfev += 1;
            let fnew = penalty_fn(&xnew);
            if fnew <= fx + 1e-4 * alpha * dg.min(0.0) {
                break;
            }
            alpha *= 0.5;
        }

        let xnew: Vec<f64> = x.iter().zip(d.iter()).map(|(&xi, &di)| xi + alpha * di).collect();
        let gnew = compute_grad(&xnew, nfev);

        // L-BFGS update
        let s: Vec<f64> = xnew.iter().zip(x.iter()).map(|(&xni, &xi)| xni - xi).collect();
        let y: Vec<f64> = gnew.iter().zip(g.iter()).map(|(&gni, &gi)| gni - gi).collect();
        let sy: f64 = s.iter().zip(y.iter()).map(|(&si, &yi)| si * yi).sum();

        if sy > 1e-10 {
            if s_hist.len() >= m {
                s_hist.remove(0);
                y_hist.remove(0);
                rho_hist.remove(0);
            }
            s_hist.push(s);
            y_hist.push(y);
            rho_hist.push(1.0 / sy);
        }

        x = xnew;
        g = gnew;
    }

    x
}

/// Penalty method solver
pub struct PenaltyMethod {
    pub options: PenaltyOptions,
}

impl PenaltyMethod {
    /// Create with default options
    pub fn new() -> Self {
        PenaltyMethod {
            options: PenaltyOptions::default(),
        }
    }

    /// Create with custom options
    pub fn with_options(options: PenaltyOptions) -> Self {
        PenaltyMethod { options }
    }

    /// Compute constraint violation at x
    fn compute_violation<E, G>(
        &self,
        x: &[f64],
        eq_cons: &[E],
        ineq_cons: &[G],
    ) -> f64
    where
        E: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        let eq_viol: f64 = eq_cons.iter().map(|e| e(x).powi(2)).sum();
        let ineq_viol: f64 = ineq_cons.iter().map(|g| g(x).max(0.0).powi(2)).sum();
        (eq_viol + ineq_viol).sqrt()
    }

    /// Solve with equality and inequality constraints.
    ///
    /// # Arguments
    /// - `f`: Objective function taking a `&[f64]`
    /// - `eq_cons`: Equality constraint functions h_j(x) = 0 (slices)
    /// - `ineq_cons`: Inequality constraint functions g_i(x) <= 0 (slices)
    /// - `x0`: Initial point
    pub fn solve_slice<F, E, G>(
        &self,
        f: F,
        eq_cons: &[E],
        ineq_cons: &[G],
        x0: &[f64],
    ) -> OptimizeResult<PenaltyResult>
    where
        F: Fn(&[f64]) -> f64,
        E: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        match self.options.kind {
            PenaltyKind::External => self.solve_external_slice(f, eq_cons, ineq_cons, x0),
            PenaltyKind::Interior => self.solve_interior_slice(f, ineq_cons, x0),
            PenaltyKind::ExactL1 => self.solve_l1_slice(f, eq_cons, ineq_cons, x0),
        }
    }

    /// Solve with ArrayView1 interface (for compatibility)
    pub fn solve<F, E, G>(
        &self,
        f: F,
        eq_cons: &[E],
        ineq_cons: &[G],
        x0: &Array1<f64>,
    ) -> OptimizeResult<PenaltyResult>
    where
        F: Fn(&ArrayView1<f64>) -> f64,
        E: Fn(&ArrayView1<f64>) -> f64,
        G: Fn(&ArrayView1<f64>) -> f64,
    {
        // Wrap ArrayView1 closures to slice-based
        let f_slice = |x: &[f64]| {
            let arr = Array1::from_vec(x.to_vec());
            f(&arr.view())
        };
        let eq_slice: Vec<Box<dyn Fn(&[f64]) -> f64>> = eq_cons.iter().map(|e| {
            Box::new(move |x: &[f64]| {
                let arr = Array1::from_vec(x.to_vec());
                e(&arr.view())
            }) as Box<dyn Fn(&[f64]) -> f64>
        }).collect();
        let ineq_slice: Vec<Box<dyn Fn(&[f64]) -> f64>> = ineq_cons.iter().map(|g| {
            Box::new(move |x: &[f64]| {
                let arr = Array1::from_vec(x.to_vec());
                g(&arr.view())
            }) as Box<dyn Fn(&[f64]) -> f64>
        }).collect();

        let x0_slice: Vec<f64> = x0.iter().copied().collect();
        self.solve_slice(f_slice, &eq_slice, &ineq_slice, &x0_slice)
    }

    fn solve_external_slice<F, E, G>(
        &self,
        f: F,
        eq_cons: &[E],
        ineq_cons: &[G],
        x0: &[f64],
    ) -> OptimizeResult<PenaltyResult>
    where
        F: Fn(&[f64]) -> f64,
        E: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        let mut x = x0.to_vec();
        let mut mu = self.options.mu_init;
        let mut nfev_total = 0usize;
        let mut nit = 0usize;

        for _outer in 0..self.options.max_outer_iter {
            nit += 1;
            let mu_local = mu;

            // Build penalty function capturing current state
            let penalty_at = |xv: &[f64]| -> f64 {
                let obj = f(xv);
                let penalty: f64 = eq_cons.iter().map(|e| mu_local * e(xv).powi(2)).sum::<f64>()
                    + ineq_cons.iter().map(|g| mu_local * g(xv).max(0.0).powi(2)).sum::<f64>();
                obj + penalty
            };

            let new_x = minimize_penalty_subproblem(
                penalty_at,
                &x,
                1000,
                self.options.optimality_tol,
                self.options.eps,
                &mut nfev_total,
            );
            x = new_x;

            // Check convergence after minimization
            let cv = self.compute_violation(&x, eq_cons, ineq_cons);
            if cv <= self.options.constraint_tol {
                let fun = f(&x);
                return Ok(PenaltyResult {
                    x: Array1::from_vec(x),
                    fun,
                    nit,
                    nfev: nfev_total,
                    success: true,
                    message: "Converged: constraint violation below tolerance".to_string(),
                    mu,
                    constraint_violation: cv,
                });
            }

            mu = (mu * self.options.mu_factor).min(self.options.mu_max);
        }

        let cv = self.compute_violation(&x, eq_cons, ineq_cons);
        let fun = f(&x);
        let success = cv <= self.options.constraint_tol;
        Ok(PenaltyResult {
            x: Array1::from_vec(x),
            fun,
            nit,
            nfev: nfev_total,
            success,
            message: if success {
                "Converged".to_string()
            } else {
                format!("Maximum outer iterations reached (cv={:.2e})", cv)
            },
            mu,
            constraint_violation: cv,
        })
    }

    fn solve_interior_slice<F, G>(
        &self,
        f: F,
        ineq_cons: &[G],
        x0: &[f64],
    ) -> OptimizeResult<PenaltyResult>
    where
        F: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        // Verify strict feasibility at starting point
        for g in ineq_cons.iter() {
            if g(x0) >= 0.0 {
                return Err(OptimizeError::InvalidInput(
                    "Interior penalty requires strictly feasible starting point (g_i(x0) < 0)"
                        .to_string(),
                ));
            }
        }

        let mut x = x0.to_vec();
        let mut mu = self.options.mu_init;
        let mut nfev_total = 0usize;
        let mut nit = 0usize;

        for _outer in 0..self.options.max_outer_iter {
            nit += 1;
            if mu < 1e-12 {
                break;
            }

            let mu_local = mu;

            let barrier_fn = |xv: &[f64]| -> f64 {
                let obj = f(xv);
                let barrier: f64 = ineq_cons.iter().map(|g| {
                    let gv = g(xv);
                    if gv < -1e-15 {
                        -mu_local * gv.abs().ln()
                    } else {
                        f64::INFINITY
                    }
                }).sum();
                obj + barrier
            };

            let new_x = minimize_penalty_subproblem(
                barrier_fn,
                &x,
                500,
                mu * 0.01,
                self.options.eps,
                &mut nfev_total,
            );

            // Accept only if still feasible
            let feasible = ineq_cons.iter().all(|g| g(&new_x) < 0.0);
            if feasible {
                x = new_x;
            }

            mu /= self.options.mu_factor;
        }

        let fun = f(&x);
        Ok(PenaltyResult {
            fun,
            x: Array1::from_vec(x),
            nit,
            nfev: nfev_total,
            success: true,
            message: "Interior penalty completed".to_string(),
            mu,
            constraint_violation: 0.0,
        })
    }

    fn solve_l1_slice<F, E, G>(
        &self,
        f: F,
        eq_cons: &[E],
        ineq_cons: &[G],
        x0: &[f64],
    ) -> OptimizeResult<PenaltyResult>
    where
        F: Fn(&[f64]) -> f64,
        E: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        let mut x = x0.to_vec();
        let mu = self.options.mu_init;
        let mut nfev_total = 0usize;
        let mut nit = 0usize;

        for _outer in 0..self.options.max_outer_iter {
            nit += 1;

            let l1_fn = |xv: &[f64]| -> f64 {
                let obj = f(xv);
                let penalty: f64 = eq_cons.iter().map(|e| mu * e(xv).abs()).sum::<f64>()
                    + ineq_cons.iter().map(|g| mu * g(xv).max(0.0)).sum::<f64>();
                obj + penalty
            };

            let new_x = minimize_penalty_subproblem(
                l1_fn,
                &x,
                1000,
                self.options.optimality_tol,
                self.options.eps,
                &mut nfev_total,
            );
            x = new_x;

            // Check convergence
            let eq_cv: f64 = eq_cons.iter().map(|e| e(&x).abs()).sum();
            let ineq_cv: f64 = ineq_cons.iter().map(|g| g(&x).max(0.0)).sum();
            let cv = eq_cv + ineq_cv;

            if cv <= self.options.constraint_tol {
                let fun = f(&x);
                return Ok(PenaltyResult {
                    x: Array1::from_vec(x),
                    fun,
                    nit,
                    nfev: nfev_total,
                    success: true,
                    message: "L1 penalty converged".to_string(),
                    mu,
                    constraint_violation: cv,
                });
            }
        }

        let eq_cv: f64 = eq_cons.iter().map(|e| e(&x).abs()).sum();
        let ineq_cv: f64 = ineq_cons.iter().map(|g| g(&x).max(0.0)).sum();
        let cv = eq_cv + ineq_cv;
        let fun = f(&x);
        Ok(PenaltyResult {
            x: Array1::from_vec(x),
            fun,
            nit,
            nfev: nfev_total,
            success: cv <= self.options.constraint_tol,
            message: "L1 penalty max iterations reached".to_string(),
            mu,
            constraint_violation: cv,
        })
    }
}

impl Default for PenaltyMethod {
    fn default() -> Self {
        PenaltyMethod::new()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// PenaltyMethodKind enum (Static / Dynamic / Adaptive / AugmentedLagrangian)
// ─────────────────────────────────────────────────────────────────────────────

/// Strategy for managing the penalty parameter sequence.
///
/// This enum governs *how* the penalty coefficient is updated across outer
/// iterations, distinct from [`PenaltyKind`] which controls the *form* of the
/// penalty term (quadratic / barrier / L1).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PenaltyMethodKind {
    /// Fixed penalty coefficient — never updated.
    /// Suitable when a good penalty value is known in advance.
    Static,

    /// Penalty is multiplied by a constant factor each outer iteration.
    /// Classic "increasing penalty" approach for exterior methods.
    Dynamic,

    /// Penalty is updated based on constraint violation progress.
    /// Increases faster when violations are not decreasing, avoids ill-
    /// conditioning by capping growth when convergence is progressing well.
    Adaptive,

    /// Augmented Lagrangian — maintains Lagrange multiplier estimates and
    /// updates them after each outer iteration via dual ascent.  The penalty
    /// parameter increases only when the multiplier update stalls.
    AugmentedLagrangian,
}

// ─────────────────────────────────────────────────────────────────────────────
// penalty_function — standalone evaluation helper
// ─────────────────────────────────────────────────────────────────────────────

/// Evaluate the penalised objective at point `x`.
///
/// For *exterior* (quadratic) penalties:
/// ```text
/// P(x, mu) = f(x)
///          + mu * Σ_i max(0, g_i(x))²     [inequality: g_i(x) <= 0]
///          + mu * Σ_j h_j(x)²              [equality:   h_j(x)  = 0]
/// ```
///
/// For *interior* (log-barrier) penalties:
/// ```text
/// B(x, mu) = f(x) - mu * Σ_i ln(-g_i(x))     [only for strictly feasible x]
/// ```
///
/// # Arguments
/// * `x`              - Current iterate (decision vector).
/// * `obj`            - Objective function `f: &[f64] -> f64`.
/// * `ineq_cons`      - Inequality constraints `g_i(x) <= 0`.
/// * `eq_cons`        - Equality constraints `h_j(x) = 0`.
/// * `penalty_coeff`  - Scalar penalty / barrier weight `μ`.
/// * `kind`           - Which form of penalty to apply.
///
/// # Returns
/// Penalised scalar value.  Returns `f64::INFINITY` if the log-barrier is
/// requested but `x` is infeasible (any `g_i(x) >= 0`).
pub fn penalty_function<F, G, H>(
    x: &[f64],
    obj: F,
    ineq_cons: &[G],
    eq_cons: &[H],
    penalty_coeff: f64,
    kind: PenaltyKind,
) -> f64
where
    F: Fn(&[f64]) -> f64,
    G: Fn(&[f64]) -> f64,
    H: Fn(&[f64]) -> f64,
{
    let f_val = obj(x);
    match kind {
        PenaltyKind::External => {
            let ineq_pen: f64 = ineq_cons
                .iter()
                .map(|g| penalty_coeff * g(x).max(0.0).powi(2))
                .sum();
            let eq_pen: f64 = eq_cons
                .iter()
                .map(|h| penalty_coeff * h(x).powi(2))
                .sum();
            f_val + ineq_pen + eq_pen
        }
        PenaltyKind::Interior => {
            let barrier: f64 = ineq_cons
                .iter()
                .map(|g| {
                    let gv = g(x);
                    if gv < -1e-15 {
                        -penalty_coeff * gv.abs().ln()
                    } else {
                        f64::INFINITY
                    }
                })
                .sum();
            f_val + barrier
        }
        PenaltyKind::ExactL1 => {
            let ineq_pen: f64 = ineq_cons
                .iter()
                .map(|g| penalty_coeff * g(x).max(0.0))
                .sum();
            let eq_pen: f64 = eq_cons
                .iter()
                .map(|h| penalty_coeff * h(x).abs())
                .sum();
            f_val + ineq_pen + eq_pen
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// AdaptivePenalty
// ─────────────────────────────────────────────────────────────────────────────

/// Adaptive penalty controller.
///
/// Tracks constraint violation history and dynamically adjusts the penalty
/// coefficient:
/// - If violation has not improved by `improvement_threshold` fraction compared
///   to the previous iteration → multiply penalty by `increase_factor`.
/// - If violation improved substantially and current penalty is high → allow
///   mild reduction to avoid ill-conditioning (optional, controlled by
///   `allow_decrease`).
///
/// This implements a simplified version of the adaptive penalty from:
/// Farmani & Wright (2003), "Self-adaptive fitness formulation for constrained
/// optimization", IEEE TEC 7(5):445-455.
#[derive(Debug, Clone)]
pub struct AdaptivePenalty {
    /// Current penalty coefficient.
    pub penalty_coeff: f64,
    /// Minimum allowed penalty (lower bound on growth).
    pub min_penalty: f64,
    /// Maximum allowed penalty (avoids numerical ill-conditioning).
    pub max_penalty: f64,
    /// Multiplicative increase factor applied when violations stall.
    pub increase_factor: f64,
    /// Multiplicative decrease factor applied when violations decrease rapidly.
    pub decrease_factor: f64,
    /// Relative improvement threshold below which penalty is increased.
    /// E.g., 0.1 means "less than 10% improvement triggers increase".
    pub improvement_threshold: f64,
    /// Whether to allow penalty *decreases* (can reduce ill-conditioning).
    pub allow_decrease: bool,
    /// Stored constraint violation from previous outer iteration.
    prev_violation: f64,
    /// Number of consecutive non-improving iterations.
    stall_count: usize,
    /// Stall patience: how many non-improving iters before increasing penalty.
    pub stall_patience: usize,
}

impl Default for AdaptivePenalty {
    fn default() -> Self {
        AdaptivePenalty {
            penalty_coeff: 1.0,
            min_penalty: 1e-3,
            max_penalty: 1e10,
            increase_factor: 10.0,
            decrease_factor: 0.5,
            improvement_threshold: 0.25,
            allow_decrease: false,
            prev_violation: f64::INFINITY,
            stall_count: 0,
            stall_patience: 1,
        }
    }
}

impl AdaptivePenalty {
    /// Create a new adaptive penalty controller with the given initial coefficient.
    pub fn new(initial_penalty: f64) -> Self {
        AdaptivePenalty {
            penalty_coeff: initial_penalty,
            ..Default::default()
        }
    }

    /// Create with full configuration.
    #[allow(clippy::too_many_arguments)]
    pub fn with_config(
        initial_penalty: f64,
        min_penalty: f64,
        max_penalty: f64,
        increase_factor: f64,
        decrease_factor: f64,
        improvement_threshold: f64,
        allow_decrease: bool,
        stall_patience: usize,
    ) -> Self {
        AdaptivePenalty {
            penalty_coeff: initial_penalty,
            min_penalty,
            max_penalty,
            increase_factor,
            decrease_factor,
            improvement_threshold,
            allow_decrease,
            prev_violation: f64::INFINITY,
            stall_count: 0,
            stall_patience,
        }
    }

    /// Update the penalty coefficient based on `current_violation`.
    ///
    /// Should be called once per outer iteration after the sub-problem has been
    /// solved.  Returns the updated penalty coefficient.
    pub fn update(&mut self, current_violation: f64) -> f64 {
        if self.prev_violation.is_infinite() {
            // First call — just record and return current value
            self.prev_violation = current_violation;
            return self.penalty_coeff;
        }

        let relative_improvement = if self.prev_violation > 1e-15 {
            (self.prev_violation - current_violation) / self.prev_violation
        } else {
            // Already near zero — treat as converged
            1.0
        };

        if relative_improvement < self.improvement_threshold {
            // Not improving fast enough
            self.stall_count += 1;
            if self.stall_count >= self.stall_patience {
                self.penalty_coeff =
                    (self.penalty_coeff * self.increase_factor).min(self.max_penalty);
                self.stall_count = 0;
            }
        } else {
            // Good improvement
            self.stall_count = 0;
            if self.allow_decrease && relative_improvement > 0.5 {
                self.penalty_coeff =
                    (self.penalty_coeff * self.decrease_factor).max(self.min_penalty);
            }
        }

        self.prev_violation = current_violation;
        self.penalty_coeff
    }

    /// Reset internal state (violation history and stall counter).
    pub fn reset(&mut self) {
        self.prev_violation = f64::INFINITY;
        self.stall_count = 0;
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// AugmentedLagrangianSolver
// ─────────────────────────────────────────────────────────────────────────────

/// Options for [`AugmentedLagrangianSolver`].
#[derive(Debug, Clone)]
pub struct AugLagOptions {
    /// Initial penalty parameter ρ.
    pub rho_init: f64,
    /// Maximum penalty parameter.
    pub rho_max: f64,
    /// Factor by which ρ is multiplied when the constraint violation does not
    /// decrease sufficiently.
    pub rho_factor: f64,
    /// Maximum number of outer (multiplier update) iterations.
    pub max_outer_iter: usize,
    /// Constraint violation tolerance (outer loop convergence criterion).
    pub constraint_tol: f64,
    /// Optimality tolerance passed to the inner unconstrained sub-solver.
    pub optimality_tol: f64,
    /// Finite-difference step for inner gradient computation.
    pub eps: f64,
    /// Required relative reduction in violation before multipliers are updated
    /// (otherwise only the penalty grows).
    pub violation_reduction_threshold: f64,
}

impl Default for AugLagOptions {
    fn default() -> Self {
        AugLagOptions {
            rho_init: 1.0,
            rho_max: 1e10,
            rho_factor: 10.0,
            max_outer_iter: 100,
            constraint_tol: 1e-6,
            optimality_tol: 1e-8,
            eps: 1e-7,
            violation_reduction_threshold: 0.25,
        }
    }
}

/// Result from [`AugmentedLagrangianSolver`].
#[derive(Debug, Clone)]
pub struct AugLagResult {
    /// Optimal decision vector.
    pub x: Array1<f64>,
    /// Objective value at x.
    pub fun: f64,
    /// Number of outer iterations performed.
    pub nit: usize,
    /// Total function evaluations (inner + outer).
    pub nfev: usize,
    /// Success flag.
    pub success: bool,
    /// Status message.
    pub message: String,
    /// Final Lagrange multipliers for equality constraints.
    pub lambda_eq: Vec<f64>,
    /// Final Lagrange multipliers for inequality constraints.
    pub lambda_ineq: Vec<f64>,
    /// Final penalty parameter.
    pub rho: f64,
    /// Final constraint violation.
    pub constraint_violation: f64,
}

impl From<AugLagResult> for OptimizeResults<f64> {
    fn from(r: AugLagResult) -> Self {
        OptimizeResults {
            x: r.x,
            fun: r.fun,
            jac: None,
            hess: None,
            constr: None,
            nit: r.nit,
            nfev: r.nfev,
            njev: 0,
            nhev: 0,
            maxcv: 0,
            message: r.message,
            success: r.success,
            status: if r.success { 0 } else { 1 },
        }
    }
}

/// Augmented Lagrangian method solver (Method of Multipliers).
///
/// Solves problems of the form:
/// ```text
/// min   f(x)
/// s.t.  h_j(x) = 0      (equality)
///       g_i(x) <= 0     (inequality)
/// ```
///
/// The augmented Lagrangian for equality constraints is:
/// ```text
/// L_A(x, λ, ρ) = f(x) + Σ_j λ_j h_j(x) + (ρ/2) Σ_j h_j(x)²
/// ```
/// Inequality constraints are handled via the shifted/signed penalty
/// (Rockafellar's form):
/// ```text
/// L_A += Σ_i [ λ_i g_i(x) + (ρ/2) g_i(x)² ]   when  g_i(x) + λ_i/ρ > 0
///      + 0                                        otherwise
/// ```
///
/// Multipliers are updated each outer iteration:
/// ```text
/// λ_j ← λ_j + ρ h_j(x*)     (equality)
/// λ_i ← max(0, λ_i + ρ g_i(x*))   (inequality)
/// ```
///
/// # References
/// - Nocedal & Wright (2006), §17.4, "Augmented Lagrangian Methods".
/// - Bertsekas (1982), "Constrained Optimization and Lagrange Multiplier Methods".
#[derive(Debug, Clone)]
pub struct AugmentedLagrangianSolver {
    /// Solver configuration.
    pub options: AugLagOptions,
}

impl AugmentedLagrangianSolver {
    /// Create with default options.
    pub fn new() -> Self {
        AugmentedLagrangianSolver {
            options: AugLagOptions::default(),
        }
    }

    /// Create with custom options.
    pub fn with_options(options: AugLagOptions) -> Self {
        AugmentedLagrangianSolver { options }
    }

    /// Compute total constraint violation (L2 norm of violations).
    fn compute_violation<E, G>(&self, x: &[f64], eq_cons: &[E], ineq_cons: &[G]) -> f64
    where
        E: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        let eq_sq: f64 = eq_cons.iter().map(|h| h(x).powi(2)).sum();
        let ineq_sq: f64 = ineq_cons.iter().map(|g| g(x).max(0.0).powi(2)).sum();
        (eq_sq + ineq_sq).sqrt()
    }

    /// Solve the augmented Lagrangian problem.
    ///
    /// # Arguments
    /// * `f`        - Objective function.
    /// * `eq_cons`  - Equality constraints `h_j(x) = 0`.
    /// * `ineq_cons`- Inequality constraints `g_i(x) <= 0`.
    /// * `x0`       - Initial iterate.
    pub fn solve<F, E, G>(
        &self,
        f: F,
        eq_cons: &[E],
        ineq_cons: &[G],
        x0: &[f64],
    ) -> OptimizeResult<AugLagResult>
    where
        F: Fn(&[f64]) -> f64,
        E: Fn(&[f64]) -> f64,
        G: Fn(&[f64]) -> f64,
    {
        let n_eq = eq_cons.len();
        let n_ineq = ineq_cons.len();

        // Initialise multipliers at zero
        let mut lambda_eq = vec![0.0_f64; n_eq];
        let mut lambda_ineq = vec![0.0_f64; n_ineq];
        let mut rho = self.options.rho_init;

        let mut x = x0.to_vec();
        let mut nfev_total = 0usize;
        let mut nit = 0usize;
        let mut prev_violation = f64::INFINITY;

        for _outer in 0..self.options.max_outer_iter {
            nit += 1;

            // Snapshot multipliers and penalty for the closure
            let lam_eq_snap = lambda_eq.clone();
            let lam_ineq_snap = lambda_ineq.clone();
            let rho_snap = rho;

            // Augmented Lagrangian function
            let aug_lag = |xv: &[f64]| -> f64 {
                let mut val = f(xv);

                // Equality terms: λ_j h_j + (ρ/2) h_j²
                for (j, h) in eq_cons.iter().enumerate() {
                    let hv = h(xv);
                    val += lam_eq_snap[j] * hv + 0.5 * rho_snap * hv * hv;
                }

                // Inequality terms (Rockafellar shifted form)
                // If  g_i + λ_i/ρ > 0  (active or violated):
                //   contribution = λ_i g_i + (ρ/2) g_i²
                //               = (1/(2ρ)) [ (λ_i + ρ g_i)² - λ_i² ]
                // If  g_i + λ_i/ρ <= 0 (inactive, well inside feasible region):
                //   contribution = -λ_i²/(2ρ)  [constant w.r.t. x, omit]
                for (i, g) in ineq_cons.iter().enumerate() {
                    let gv = g(xv);
                    let shifted = gv + lam_ineq_snap[i] / rho_snap;
                    if shifted > 0.0 {
                        val += lam_ineq_snap[i] * gv + 0.5 * rho_snap * gv * gv;
                    }
                    // else: constraint is inactive, no contribution
                }

                val
            };

            // Minimise the augmented Lagrangian (unconstrained sub-problem)
            let new_x = minimize_penalty_subproblem(
                aug_lag,
                &x,
                2000,
                self.options.optimality_tol,
                self.options.eps,
                &mut nfev_total,
            );

            x = new_x;

            // Compute current violation
            let cv = self.compute_violation(&x, eq_cons, ineq_cons);

            // Check for convergence
            if cv <= self.options.constraint_tol {
                let fun = f(&x);
                return Ok(AugLagResult {
                    x: Array1::from_vec(x),
                    fun,
                    nit,
                    nfev: nfev_total,
                    success: true,
                    message: "Converged: constraint violation below tolerance".to_string(),
                    lambda_eq,
                    lambda_ineq,
                    rho,
                    constraint_violation: cv,
                });
            }

            // Determine whether to update multipliers or just increase penalty
            let violation_improvement = if prev_violation.is_finite() && prev_violation > 1e-15 {
                (prev_violation - cv) / prev_violation
            } else {
                0.0
            };

            if violation_improvement >= self.options.violation_reduction_threshold {
                // Good progress: update multipliers (dual ascent step)
                for (j, h) in eq_cons.iter().enumerate() {
                    lambda_eq[j] += rho * h(&x);
                }
                for (i, g) in ineq_cons.iter().enumerate() {
                    lambda_ineq[i] = (lambda_ineq[i] + rho * g(&x)).max(0.0);
                }
            } else {
                // Poor progress: increase penalty to force constraint satisfaction
                rho = (rho * self.options.rho_factor).min(self.options.rho_max);
            }

            prev_violation = cv;
        }

        // Max iterations reached
        let cv = self.compute_violation(&x, eq_cons, ineq_cons);
        let fun = f(&x);
        let success = cv <= self.options.constraint_tol;
        Ok(AugLagResult {
            x: Array1::from_vec(x),
            fun,
            nit,
            nfev: nfev_total,
            success,
            message: if success {
                "Converged".to_string()
            } else {
                format!(
                    "Maximum outer iterations ({}) reached; cv={:.2e}",
                    self.options.max_outer_iter, cv
                )
            },
            lambda_eq,
            lambda_ineq,
            rho,
            constraint_violation: cv,
        })
    }
}

impl Default for AugmentedLagrangianSolver {
    fn default() -> Self {
        AugmentedLagrangianSolver::new()
    }
}

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

    #[test]
    fn test_penalty_equality_constraint() {
        // min x^2 + y^2  s.t. x + y = 1
        // Solution: x = y = 0.5, f = 0.5
        let f = |x: &[f64]| x[0].powi(2) + x[1].powi(2);
        let h = |x: &[f64]| x[0] + x[1] - 1.0;

        let opts = PenaltyOptions {
            kind: PenaltyKind::External,
            mu_init: 1.0,
            mu_factor: 10.0,
            mu_max: 1e8,
            max_outer_iter: 30,
            constraint_tol: 1e-4,
            ..Default::default()
        };
        let solver = PenaltyMethod::with_options(opts);
        let result = solver
            .solve_slice(f, &[h], &[] as &[fn(&[f64]) -> f64], &[0.0, 0.0])
            .expect("solve failed");

        assert_abs_diff_eq!(result.x[0], 0.5, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[1], 0.5, epsilon = 1e-2);
        assert_abs_diff_eq!(result.fun, 0.5, epsilon = 1e-2);
    }

    #[test]
    fn test_penalty_inequality_constraint() {
        // min x^2 + y^2  s.t. x + y >= 1  (g: 1 - x - y <= 0)
        let f = |x: &[f64]| x[0].powi(2) + x[1].powi(2);
        let g = |x: &[f64]| 1.0 - x[0] - x[1]; // <= 0 means x+y >= 1

        let opts = PenaltyOptions {
            kind: PenaltyKind::External,
            mu_init: 1.0,
            mu_factor: 10.0,
            mu_max: 1e8,
            max_outer_iter: 40,
            constraint_tol: 1e-3,
            ..Default::default()
        };
        let solver = PenaltyMethod::with_options(opts);
        let result = solver
            .solve_slice(f, &[] as &[fn(&[f64]) -> f64], &[g], &[2.0, 2.0])
            .expect("solve failed");

        // Solution: (0.5, 0.5)
        assert_abs_diff_eq!(result.fun, 0.5, epsilon = 1e-2);
    }

    #[test]
    fn test_penalty_no_constraints() {
        // min (x-3)^2 + (y-4)^2 unconstrained
        let f = |x: &[f64]| (x[0] - 3.0).powi(2) + (x[1] - 4.0).powi(2);
        let solver = PenaltyMethod::new();
        let result = solver
            .solve_slice(
                f,
                &[] as &[fn(&[f64]) -> f64],
                &[] as &[fn(&[f64]) -> f64],
                &[0.0, 0.0],
            )
            .expect("solve failed");

        assert_abs_diff_eq!(result.fun, 0.0, epsilon = 1e-3);
        assert_abs_diff_eq!(result.x[0], 3.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[1], 4.0, epsilon = 1e-2);
    }

    #[test]
    fn test_penalty_l1_equality() {
        let f = |x: &[f64]| (x[0] - 1.0).powi(2) + (x[1] - 2.0).powi(2);
        let h = |x: &[f64]| x[0] + x[1] - 3.0;

        let opts = PenaltyOptions {
            kind: PenaltyKind::ExactL1,
            mu_init: 10.0,
            max_outer_iter: 50,
            constraint_tol: 1e-3,
            ..Default::default()
        };
        let solver = PenaltyMethod::with_options(opts);
        let result = solver
            .solve_slice(f, &[h], &[] as &[fn(&[f64]) -> f64], &[0.0, 0.0])
            .expect("solve failed");

        // Solution: (1, 2) satisfies x+y=3
        assert_abs_diff_eq!(result.fun, 0.0, epsilon = 1e-2);
    }

    #[test]
    fn test_penalty_interior_barrier() {
        // min (x-0.5)^2  s.t. x < 1 (g: x - 0.999 <= 0)
        // Start strictly inside: x0 = 0.3
        let f = |x: &[f64]| (x[0] - 0.5).powi(2);
        let g = |x: &[f64]| x[0] - 0.999;
        let opts = PenaltyOptions {
            kind: PenaltyKind::Interior,
            mu_init: 0.1,
            mu_factor: 5.0,
            max_outer_iter: 20,
            ..Default::default()
        };
        let solver = PenaltyMethod::with_options(opts);
        let result = solver
            .solve_slice(f, &[] as &[fn(&[f64]) -> f64], &[g], &[0.3])
            .expect("solve failed");

        assert_abs_diff_eq!(result.x[0], 0.5, epsilon = 0.1);
    }

    #[test]
    fn test_penalty_mixed_constraints() {
        // min x^2 + y^2 + z^2  s.t. x+y+z=3, z<=1
        let f = |x: &[f64]| x[0].powi(2) + x[1].powi(2) + x[2].powi(2);
        let h = |x: &[f64]| x[0] + x[1] + x[2] - 3.0;
        let g = |x: &[f64]| x[2] - 1.0;

        let opts = PenaltyOptions {
            kind: PenaltyKind::External,
            mu_init: 1.0,
            mu_factor: 5.0,
            mu_max: 1e7,
            max_outer_iter: 50,
            constraint_tol: 1e-3,
            ..Default::default()
        };
        let solver = PenaltyMethod::with_options(opts);
        let result = solver
            .solve_slice(f, &[h], &[g], &[0.0, 0.0, 0.0])
            .expect("solve failed");

        // With z<=1 and x+y+z=3: optimal at z=1, x=y=1 -> f=3
        assert!(result.fun <= 4.0, "fun={}", result.fun);
    }

    #[test]
    fn test_penalty_arrayview1_interface() {
        use scirs2_core::ndarray::{array, ArrayView1};
        let f = |x: &ArrayView1<f64>| x[0].powi(2) + x[1].powi(2);
        let h = |x: &ArrayView1<f64>| x[0] + x[1] - 1.0;

        let opts = PenaltyOptions {
            kind: PenaltyKind::External,
            max_outer_iter: 20,
            constraint_tol: 1e-3,
            ..Default::default()
        };
        let solver = PenaltyMethod::with_options(opts);
        let x0 = array![0.0, 0.0];
        let result = solver
            .solve(f, &[h], &[] as &[fn(&ArrayView1<f64>) -> f64], &x0)
            .expect("solve failed");

        // Solution: (0.5, 0.5)
        assert_abs_diff_eq!(result.fun, 0.5, epsilon = 5e-2);
    }

    // ── penalty_function free function ───────────────────────────────────────

    #[test]
    fn test_penalty_function_external_feasible() {
        let f = |x: &[f64]| x[0].powi(2) + x[1].powi(2);
        let g = |x: &[f64]| x[0] + x[1] - 1.0; // g <= 0 means x+y <= 1; at (0,0) g = -1 <= 0

        // At feasible point (0,0): g(x) = -1 < 0, no violation
        let val = penalty_function(
            &[0.0, 0.0],
            f,
            &[g],
            &[] as &[fn(&[f64]) -> f64],
            10.0,
            PenaltyKind::External,
        );
        // No penalty applied: val = f(0,0) = 0
        assert_abs_diff_eq!(val, 0.0, epsilon = 1e-12);
    }

    #[test]
    fn test_penalty_function_external_violated() {
        let f = |x: &[f64]| x[0].powi(2) + x[1].powi(2);
        let g = |x: &[f64]| x[0] + x[1] - 1.0; // at (1,1): g = 1 > 0, violated

        let mu = 5.0;
        let val = penalty_function(
            &[1.0, 1.0],
            f,
            &[g],
            &[] as &[fn(&[f64]) -> f64],
            mu,
            PenaltyKind::External,
        );
        // f = 2, penalty = mu * max(0, 1)^2 = 5
        assert_abs_diff_eq!(val, 2.0 + mu * 1.0_f64.powi(2), epsilon = 1e-12);
    }

    #[test]
    fn test_penalty_function_equality() {
        let f = |_x: &[f64]| 0.0;
        let h = |x: &[f64]| x[0] - 1.0; // h = 0 at x=1; at x=2 h=1

        let mu = 3.0;
        let val = penalty_function(
            &[2.0],
            f,
            &[] as &[fn(&[f64]) -> f64],
            &[h],
            mu,
            PenaltyKind::External,
        );
        // penalty = mu * h^2 = 3 * 1 = 3
        assert_abs_diff_eq!(val, 3.0, epsilon = 1e-12);
    }

    // ── AdaptivePenalty ──────────────────────────────────────────────────────

    #[test]
    fn test_adaptive_penalty_increases_on_stall() {
        let mut ap = AdaptivePenalty::new(1.0);
        ap.increase_factor = 5.0;
        ap.improvement_threshold = 0.1;
        ap.stall_patience = 1;

        // First call: records violation, returns current penalty
        let p0 = ap.update(1.0);
        assert_abs_diff_eq!(p0, 1.0, epsilon = 1e-12);

        // Second call: violation is 0.95 (only 5% improvement < threshold 10%)
        // => stall_count=1 >= patience=1 => penalty should increase
        let p1 = ap.update(0.95);
        assert!(p1 > 1.0, "Penalty should have increased; got {p1}");
    }

    #[test]
    fn test_adaptive_penalty_no_increase_on_good_progress() {
        let mut ap = AdaptivePenalty::new(1.0);
        ap.improvement_threshold = 0.1;
        ap.stall_patience = 1;

        ap.update(1.0); // seed prev_violation
        let p1 = ap.update(0.5); // 50% improvement, well above 10% threshold
        // Penalty should NOT increase (allow_decrease=false by default)
        assert_abs_diff_eq!(p1, 1.0, epsilon = 1e-12);
    }

    #[test]
    fn test_adaptive_penalty_capped_at_max() {
        let mut ap = AdaptivePenalty::with_config(
            1e9, 1e-3, 1e10, 1000.0, 0.5, 0.1, false, 1,
        );
        ap.update(1.0);
        let p = ap.update(1.0); // no improvement → multiply by 1000 → capped at 1e10
        assert!(p <= 1e10, "Penalty exceeded max; got {p}");
    }

    // ── AugmentedLagrangianSolver ────────────────────────────────────────────

    #[test]
    fn test_aug_lag_equality_constraint() {
        // min x^2 + y^2  s.t. x + y = 1
        // Solution: x = y = 0.5, f = 0.5
        let f = |x: &[f64]| x[0].powi(2) + x[1].powi(2);
        let h = |x: &[f64]| x[0] + x[1] - 1.0;

        let opts = AugLagOptions {
            rho_init: 1.0,
            rho_factor: 5.0,
            max_outer_iter: 50,
            constraint_tol: 1e-4,
            ..Default::default()
        };
        let solver = AugmentedLagrangianSolver::with_options(opts);
        let result = solver
            .solve(f, &[h], &[] as &[fn(&[f64]) -> f64], &[0.0, 0.0])
            .expect("AugLag solve failed");

        assert!(result.success || result.constraint_violation < 1e-2,
            "cv={}", result.constraint_violation);
        assert_abs_diff_eq!(result.fun, 0.5, epsilon = 0.05);
    }

    #[test]
    fn test_aug_lag_inequality_constraint() {
        // min x^2  s.t. x >= 1  =>  g(x) = 1 - x <= 0
        // Solution: x = 1
        let f = |x: &[f64]| x[0].powi(2);
        let g = |x: &[f64]| 1.0 - x[0]; // <= 0 means x >= 1

        let opts = AugLagOptions {
            rho_init: 1.0,
            rho_factor: 10.0,
            max_outer_iter: 60,
            constraint_tol: 1e-3,
            ..Default::default()
        };
        let solver = AugmentedLagrangianSolver::with_options(opts);
        let result = solver
            .solve(f, &[] as &[fn(&[f64]) -> f64], &[g], &[0.5])
            .expect("AugLag solve failed");

        // x should be near 1.0
        assert!(
            result.x[0] >= 0.9 && result.x[0] <= 1.2,
            "Expected x~1.0, got x={}",
            result.x[0]
        );
    }

    #[test]
    fn test_aug_lag_no_constraints() {
        // Unconstrained: min (x-2)^2 + (y-3)^2
        let f = |x: &[f64]| (x[0] - 2.0).powi(2) + (x[1] - 3.0).powi(2);
        let solver = AugmentedLagrangianSolver::new();
        let result = solver
            .solve(
                f,
                &[] as &[fn(&[f64]) -> f64],
                &[] as &[fn(&[f64]) -> f64],
                &[0.0, 0.0],
            )
            .expect("AugLag solve failed");

        assert_abs_diff_eq!(result.x[0], 2.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.x[1], 3.0, epsilon = 1e-2);
        assert_abs_diff_eq!(result.fun, 0.0, epsilon = 1e-3);
    }

    #[test]
    fn test_penalty_method_kind_variants() {
        // Just verify the enum can be constructed and compared
        assert_eq!(PenaltyMethodKind::Static, PenaltyMethodKind::Static);
        assert_ne!(PenaltyMethodKind::Dynamic, PenaltyMethodKind::Adaptive);
        assert_ne!(
            PenaltyMethodKind::AugmentedLagrangian,
            PenaltyMethodKind::Static
        );
    }
}