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
//! Adaptive mu update — port of `IpAdaptiveMuUpdate.{hpp,cpp}`.
//!
//! Phase 10. The full update reaches into `IpoptCq` for residuals and
//! into a `MuOracle` for the candidate σ; this file ships:
//!
//! * the option struct with upstream defaults from `RegisterOptions`,
//! * the `lower_mu_safeguard` scalar core (lines 753-786),
//! * the globalization-mode enum and the FreeMuMode/FixedMuMode state
//! machine (`UpdateBarrierParameter` lines 252-444),
//! * the `mu_oracle` selector ([`MuOracleKind`]) — `Loqo` runs the
//! closed form; `Probing` / `QualityFunction` drive an affine /
//! centring solve when [`MuUpdate`] is given the search-dir + nlp
//! handles, otherwise fall through to LOQO (mirrors upstream's
//! "oracle returned no candidate" branch at lines 402-408).
use crate::ipopt_cq::IpoptCqHandle;
use crate::ipopt_data::IpoptDataHandle;
use crate::ipopt_nlp::IpoptNlp;
use crate::iterates_vector::IteratesVector;
use crate::kkt::pd_search_dir_calc::PdSearchDirCalc;
use crate::line_search::filter::Filter;
use crate::mu::oracle::loqo::LoqoMuOracle;
use crate::mu::oracle::probing::ProbingMuOracle;
use crate::mu::oracle::quality_function::QualityFunctionMuOracle;
use crate::mu::oracle::r#trait::MuOracle;
use crate::mu::r#trait::MuUpdate;
use pounce_common::types::Number;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
/// `mu_oracle` option from `IpAdaptiveMuUpdate.cpp:RegisterOptions`.
/// Default `QualityFunction` matches upstream (`"quality-function"`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MuOracleKind {
/// Closed-form LOQO rule. No predictor solve required.
Loqo,
/// Mehrotra probing oracle. Needs an affine-step solve.
Probing,
/// Golden-section minimisation of the q(σ) quality function.
/// Needs an affine-step solve plus a centring evaluator.
QualityFunction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdaptiveMuGlobalization {
KktError,
ObjConstrFilter,
NeverMonotoneMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdaptiveMuKktNorm {
OneNorm,
TwoNormSquared,
MaxNorm,
TwoNorm,
}
pub struct AdaptiveMuUpdate {
pub mu_oracle: MuOracleKind,
pub adaptive_mu_globalization: AdaptiveMuGlobalization,
pub adaptive_mu_kkt_norm: AdaptiveMuKktNorm,
pub adaptive_mu_safeguard_factor: Number,
pub adaptive_mu_kkterror_red_iters: usize,
pub adaptive_mu_kkterror_red_fact: Number,
pub filter_max_margin: Number,
pub filter_margin_fact: Number,
pub mu_min: Number,
/// Complementarity tolerance — option `compl_inf_tol`, default 1e-4 per
/// `IpAlgorithmRegOp.cpp`. Not used directly by the adaptive update;
/// enters only through [`Self::certificate_safe_mu_min`], which caps
/// `mu_min` so a strongly scaled-down objective can still reach the
/// termination certificate (pounce#266) — the μ floor lives in scaled
/// space while `compl_inf_tol` is enforced on the *unscaled*
/// complementarity.
pub compl_inf_tol: Number,
/// Upper bound on μ. Sentinel `-1.0` means "not yet computed; init
/// lazily on the first `update_barrier_parameter` call to
/// `mu_max_fact * curr_avrg_compl()`". Mirrors
/// `IpAdaptiveMuUpdate.cpp:160-165` (load step) and
/// `IpAdaptiveMuUpdate.cpp:267-274` (lazy init).
pub mu_max: Number,
/// `mu_max_fact` (default 1e3) — factor for lazy init of `mu_max`.
/// Upstream `IpAdaptiveMuUpdate.cpp:RegisterOptions` line 42.
/// Ignored if the user explicitly sets `mu_max` to a non-sentinel
/// value.
pub mu_max_fact: Number,
/// `tau_min` from `IpAdaptiveMuUpdate.cpp:RegisterOptions`. Used to
/// derive `curr_tau = max(tau_min, 1 - mu)` after each update,
/// mirroring upstream's `IpAdaptiveMuUpdate.cpp:UpdateBarrierParameter`
/// at the post-oracle update.
pub tau_min: Number,
/// Initial mu seed — `mu_init` from `IpoptAlgorithm` registered
/// options. Used to seed `curr_mu` in `initialize`.
pub mu_init: Number,
/// `barrier_tol_factor` (default 10) from upstream
/// `IpMonotoneMuUpdate::RegisterOptions`. Threshold for fixed-mode
/// barrier subproblem completion: reduce μ when
/// `curr_barrier_error ≤ barrier_tol_factor · μ`.
pub barrier_tol_factor: Number,
/// `mu_linear_decrease_factor` (default 0.2) — fixed-mode update
/// uses `min(linear · μ, μ^superlinear_power)`.
pub mu_linear_decrease_factor: Number,
/// `mu_superlinear_decrease_power` (default 1.5).
pub mu_superlinear_decrease_power: Number,
/// `adaptive_mu_monotone_init_factor` (default 0.8). Used by
/// `new_fixed_mu` when no `fix_mu_oracle_` is configured.
pub adaptive_mu_monotone_init_factor: Number,
/// `adaptive_mu_restore_previous_iterate` (default false).
pub restore_accepted_iterate: bool,
/// `sigma_max` / `sigma_min` forwarded to `QualityFunctionMuOracle`
/// on every free-mode call. `sigma_max` is additionally forwarded to
/// `ProbingMuOracle` (upstream `IpProbingMuOracle.cpp` reads the same
/// `sigma_max` option to cap its centering parameter — L3). Defaults
/// from `IpQualityFunctionMuOracle.cpp:RegisterOptions`.
pub sigma_max: Number,
pub sigma_min: Number,
/// `quality_function_norm_type` (default `2-norm-squared`) —
/// norm used to aggregate the three KKT components inside the
/// quality function. Forwarded to `QualityFunctionMuOracle` on
/// every free-mode call. Mirrors
/// `IpQualityFunctionMuOracle.cpp:RegisterOptions`.
pub qf_norm_type: crate::mu::oracle::quality_function::NormType,
/// `quality_function_centrality` (default `none`) — penalty term
/// added to the quality function for centrality deviation.
pub qf_centrality_type: crate::mu::oracle::quality_function::CentralityType,
/// `quality_function_balancing_term` (default `none`) — penalty
/// term added to the quality function when the complementarity
/// is far smaller than the infeasibilities.
pub qf_balancing_term: crate::mu::oracle::quality_function::BalancingTermType,
/// `quality_function_max_section_steps` (default 8) — cap on
/// golden-section iterations when picking σ.
pub qf_max_section_steps: i32,
/// `quality_function_section_sigma_tol` (default 1e-2) — width
/// tolerance in σ-space for the golden-section search.
pub qf_section_sigma_tol: Number,
/// `quality_function_section_qf_tol` (default 0.0) — relative
/// flatness tolerance for the golden-section search.
pub qf_section_qf_tol: Number,
/// `probing_iterate_quality_factor` (default 1e4, pounce-specific;
/// see pounce#58). When the probing (Mehrotra) μ-oracle is about
/// to read `curr_avrg_compl()` for its `mu_curr` input, a single
/// imbalanced `(s_i, z_i)` pair can inflate the average 5+ orders
/// above the stored `data.curr_mu`. Probing then mathematically
/// correctly returns `σ·mu_curr` ≫ previous μ, which throws the
/// iterate out of the convergence neighborhood. This guard
/// short-circuits that case: when `curr_avrg_compl / curr_mu >
/// probing_iterate_quality_factor`, we signal restoration via
/// [`IpoptData::request_resto`] and keep μ unchanged. Set to 0 or
/// any non-positive value to disable.
pub probing_iterate_quality_factor: Number,
/// Upstream tracks `init_*_inf` lazily — sentinel −1 means
/// "not yet captured".
init_dual_inf: Number,
init_primal_inf: Number,
/// FreeMuMode/FixedMuMode flag — port of
/// `IpoptData::FreeMuMode()`. `true` means "let the oracle drive
/// μ"; `false` means "monotone decrease until sufficient progress
/// is made". Initialised to `true` in [`MuUpdate::initialize`]
/// (matches upstream `InitializeImpl` line 239).
free_mu_mode: bool,
/// KKT-error history for `KKT_ERROR` globalization. Bounded length
/// = `adaptive_mu_kkterror_red_iters`. Mirrors `refs_vals_`.
refs_vals: VecDeque<Number>,
/// 2-D `(theta, phi)` filter for `OBJ_CONSTR_FILTER` globalization.
/// Mirrors `filter_` (constructed with `Filter(2)`).
filter: Filter,
/// Snapshot of `curr` at the most recent successful free-mode
/// iterate; restored when switching to fixed mode if
/// `restore_accepted_iterate` is on. Mirrors `accepted_point_`.
accepted_point: Option<IteratesVector>,
/// `no_bounds_` flag — port of `IpAdaptiveMuUpdate.cpp:282-287`.
/// Set to `true` on the first `update_barrier_parameter` call when
/// the iterate has zero bound multipliers (z_l, z_u, v_l, v_u all
/// have dim 0 — e.g. BT3, GENHS28, HS50, equality-only TNLPs).
/// Subsequent calls return `mu_min` immediately. Without this,
/// `mu_max = mu_max_fact * curr_avrg_compl()` evaluates to 0 (no
/// slacks → zero complementarity) and the later `clamp(mu_min,
/// mu_max)` panics with `min > max`.
no_bounds: bool,
}
impl Default for AdaptiveMuUpdate {
fn default() -> Self {
// Defaults from `IpAdaptiveMuUpdate.cpp:RegisterOptions`.
Self {
mu_oracle: MuOracleKind::QualityFunction,
adaptive_mu_globalization: AdaptiveMuGlobalization::ObjConstrFilter,
adaptive_mu_kkt_norm: AdaptiveMuKktNorm::TwoNormSquared,
adaptive_mu_safeguard_factor: 0.0,
adaptive_mu_kkterror_red_iters: 4,
adaptive_mu_kkterror_red_fact: 0.9999,
filter_max_margin: 1.0,
filter_margin_fact: 1e-5,
mu_min: 1e-11,
compl_inf_tol: 1e-4,
// Sentinel; lazy-initialised to `mu_max_fact * avrg_compl`
// on the first `update_barrier_parameter` call. Upstream
// `IpAdaptiveMuUpdate.cpp:164` sets `mu_max_ = -1.` when
// the option is not user-specified.
mu_max: -1.0,
mu_max_fact: 1e3,
tau_min: 0.99,
mu_init: 0.1,
barrier_tol_factor: 10.0,
mu_linear_decrease_factor: 0.2,
mu_superlinear_decrease_power: 1.5,
adaptive_mu_monotone_init_factor: 0.8,
restore_accepted_iterate: false,
sigma_max: 1e2,
sigma_min: 1e-6,
qf_norm_type: crate::mu::oracle::quality_function::NormType::TwoNormSquared,
qf_centrality_type: crate::mu::oracle::quality_function::CentralityType::None,
qf_balancing_term: crate::mu::oracle::quality_function::BalancingTermType::None,
qf_max_section_steps: 8,
qf_section_sigma_tol: 1e-2,
qf_section_qf_tol: 0.0,
probing_iterate_quality_factor: 1e4,
init_dual_inf: -1.0,
init_primal_inf: -1.0,
free_mu_mode: true,
refs_vals: VecDeque::new(),
filter: Filter::new(),
accepted_point: None,
no_bounds: false,
}
}
}
impl AdaptiveMuUpdate {
pub fn new() -> Self {
Self::default()
}
/// Pure-arithmetic predicate behind the probing-oracle iterate-
/// quality guard (pounce#58). Returns `true` when the ratio
/// `avrg_compl / curr_mu` exceeds `factor`. The two non-strict
/// gates (`factor > 0`, `curr_mu > 0`) keep the predicate
/// well-defined when the guard is disabled or when an unusual
/// μ-strategy zeroes `curr_mu`.
pub fn probing_iterate_guard_fires(
factor: Number,
curr_mu: Number,
avrg_compl: Number,
) -> bool {
factor > 0.0 && curr_mu > 0.0 && avrg_compl > factor * curr_mu
}
/// Scalar core of the lazy `mu_max` initialization
/// (`IpAdaptiveMuUpdate.cpp:267-274`): on the first call, when the
/// user did not set `mu_max` explicitly, upstream sets it to
/// `mu_max_fact * curr_avrg_compl()`.
///
/// A warm start (`warm_start_init_point=yes`) can hand us an iterate
/// whose bound multipliers are all zero — pounce does not yet wire
/// `warm_start_mult_bound_push`, so `seed_from_nlp` leaves
/// `z_l`/`z_u`/`v_l`/`v_u` at 0. Then `curr_avrg_compl()` is 0 even
/// though bounds exist, the `no_bounds` short-circuit does NOT fire,
/// `mu_max` collapses to 0, and the later `new_mu.clamp(mu_min,
/// mu_max)` panics with `min > max` (min = mu_min = 1e-11, max = 0).
/// When `avrg` carries no positive complementarity signal (zero, or a
/// NaN handed in by a pathological iterate) fall back to `mu_init` as
/// the proxy — what a cold start's `avrg_compl` is ~scaled to — so the
/// `[mu_min, mu_max]` band stays valid. The final `.max(mu_min)` is a
/// belt-and-suspenders floor against pathological options.
pub fn lazy_mu_max(
mu_max_fact: Number,
avrg: Number,
mu_init: Number,
mu_min: Number,
) -> Number {
let avrg = if avrg > 0.0 { avrg } else { mu_init };
(mu_max_fact * avrg).max(mu_min)
}
/// Scalar core of `AdaptiveMuUpdate::lower_mu_safeguard`
/// (`IpAdaptiveMuUpdate.cpp:753-786`):
/// ```text
/// init_dual_inf ← max(1, dual_inf) if not yet set
/// init_primal_inf ← max(1, primal_inf) if not yet set
/// lower = max(safeguard_factor * dual_inf / init_dual_inf,
/// safeguard_factor * primal_inf / init_primal_inf)
/// if globalization == KKT_ERROR: lower = min(lower, min_ref_val)
/// ```
pub fn lower_mu_safeguard(
&mut self,
dual_inf: Number,
primal_inf: Number,
min_ref_val: Number,
) -> Number {
if self.init_dual_inf < 0.0 {
self.init_dual_inf = dual_inf.max(1.0);
}
if self.init_primal_inf < 0.0 {
self.init_primal_inf = primal_inf.max(1.0);
}
let dual_term = self.adaptive_mu_safeguard_factor * (dual_inf / self.init_dual_inf);
let prim_term = self.adaptive_mu_safeguard_factor * (primal_inf / self.init_primal_inf);
let mut lower = dual_term.max(prim_term);
if self.adaptive_mu_globalization == AdaptiveMuGlobalization::KktError {
lower = lower.min(min_ref_val);
}
lower
}
pub fn reset_init_inf(&mut self) {
self.init_dual_inf = -1.0;
self.init_primal_inf = -1.0;
}
/// Globalization KKT-error proxy — port of
/// `AdaptiveMuUpdate::quality_function_pd_system`
/// (`IpAdaptiveMuUpdate.cpp:629-744`). v1.0 hardwires the
/// max-norm variant (`adaptive_mu_kkt_norm_type=max-norm`,
/// upstream "NM_NORM_MAX") because the existing CQ surface
/// exposes max-norm primal/dual infeasibility cheaply; the
/// other three norm variants follow once `curr_*_infeasibility`
/// learns to dispatch on `NormEnum`. The score sums primal +
/// dual + complementarity (+ optional centrality / balancing
/// — both default off; left as `0`).
fn quality_function_pd_system(&self, cq: &IpoptCqHandle) -> Number {
let cq_ref = cq.borrow();
let primal_inf = cq_ref.curr_primal_infeasibility_max();
let dual_inf = cq_ref.curr_dual_infeasibility_max();
// Max-norm complementarity ≈ avrg_compl is a cheap proxy.
// Upstream's `curr_complementarity(0., NORM_MAX)` would use
// `||s ⊙ z||_∞`; absent that accessor, fall through to the
// average. For the monotonicity test inside
// `check_sufficient_progress` only ratios matter, so the
// proxy preserves the convergence criterion.
let complty = cq_ref.curr_avrg_compl();
primal_inf + dual_inf + complty
}
/// Port of `AdaptiveMuUpdate::CheckSufficientProgress`
/// (`IpAdaptiveMuUpdate.cpp:446-490`). Returns `true` if the
/// current iterate makes acceptable progress under the active
/// globalization rule.
fn check_sufficient_progress(&self, cq: &IpoptCqHandle) -> bool {
match self.adaptive_mu_globalization {
AdaptiveMuGlobalization::KktError => {
if self.refs_vals.len() < self.adaptive_mu_kkterror_red_iters.max(1) {
// Not enough history yet — accept (matches
// upstream's `num_refs >= num_refs_max_` guard).
return true;
}
let curr_error = self.quality_function_pd_system(cq);
self.refs_vals
.iter()
.any(|&r| curr_error <= self.adaptive_mu_kkterror_red_fact * r)
}
AdaptiveMuGlobalization::ObjConstrFilter => {
let cq_ref = cq.borrow();
let curr_f = cq_ref.curr_f();
let curr_theta = cq_ref.curr_constraint_violation();
// `curr_nlp_error` is our analogue of upstream's
// global error margin driver.
let curr_err = cq_ref.curr_nlp_error();
drop(cq_ref);
let margin = self.filter_margin_fact * self.filter_max_margin.min(curr_err);
!self
.filter
.dominated_by_any(curr_theta + margin, curr_f + margin)
}
AdaptiveMuGlobalization::NeverMonotoneMode => true,
}
}
/// Port of `AdaptiveMuUpdate::RememberCurrentPointAsAccepted`
/// (`IpAdaptiveMuUpdate.cpp:492-546`). Records the iterate state
/// for the next sufficient-progress check.
fn remember_current_point_as_accepted(&mut self, data: &IpoptDataHandle, cq: &IpoptCqHandle) {
match self.adaptive_mu_globalization {
AdaptiveMuGlobalization::KktError => {
let curr_error = self.quality_function_pd_system(cq);
if self.refs_vals.len() >= self.adaptive_mu_kkterror_red_iters.max(1) {
self.refs_vals.pop_front();
}
self.refs_vals.push_back(curr_error);
}
AdaptiveMuGlobalization::ObjConstrFilter => {
let cq_ref = cq.borrow();
let f = cq_ref.curr_f();
let theta = cq_ref.curr_constraint_violation();
let it = data.borrow().iter_count;
drop(cq_ref);
self.filter.add(theta, f, it);
}
AdaptiveMuGlobalization::NeverMonotoneMode => {}
}
if self.restore_accepted_iterate {
self.accepted_point = data.borrow().curr.clone();
}
}
/// `mu_min` capped so it can never block the termination certificate
/// (pounce#266) — the adaptive twin of
/// [`crate::mu::monotone::MonotoneMuUpdate::certificate_safe_mu_min`],
/// which carries the full story. The raw absolute `mu_min` (default
/// `1e-11`) lives in μ's scaled space while `compl_inf_tol` is enforced
/// on the *unscaled* complementarity; below
/// `|df| ≈ mu_min·(barrier_tol_factor+1)/compl_inf_tol` an uncapped
/// floor pins the unscaled complementarity above `compl_inf_tol` and
/// the strict certificate is unreachable — in adaptive mode the solve
/// then degrades to `Solved_To_Acceptable_Level` (code 100, outside
/// AMPL's 0..99 solved band) on an iterate sitting at the optimum.
///
/// The restoration sub-builder's `mu_min = 100 · outer_mu_min`
/// safeguard is unaffected for the same reason as in monotone mode:
/// `RestoIpoptNlp` does not override `obj_scaling_factor`, so the resto
/// inner IPM sees `df = 1` and the cap sits far above the safeguard.
pub fn certificate_safe_mu_min(&self, obj_scaling_factor: Number) -> Number {
crate::mu::certificate_safe_mu_min(
self.mu_min,
self.compl_inf_tol,
self.barrier_tol_factor,
obj_scaling_factor,
)
}
/// Floor for the **fixed-mode** (monotone-mode) μ decrease — port of
/// `IpAdaptiveMuUpdate.cpp:328-329`:
///
/// ```cpp
/// new_mu = Max(new_mu,
/// Min(compl_inf_tol_scaled, IpData().tol()) / (barrier_tol_factor_ + 1.));
/// ```
///
/// pounce#511: this branch used to floor at `mu_min` instead — `1e-11`
/// against upstream's `9.09e-10` at default `tol = 1e-8`, ~91× lower,
/// and further with a looser `tol` (at `tol = 1e-6` upstream's floor is
/// `9.09e-8`, four orders up). `mu_min` is the *free*-mode clamp; once the
/// strategy has switched to fixed mode upstream deliberately uses the
/// looser, tolerance-derived floor — that is the point of the switch.
/// Driving the Newton system down to `1e-11` past the accuracy the
/// termination test asks for buys nothing and invites degenerate search
/// directions on an ill-conditioned Jacobian.
///
/// Two details mirror the monotone floor
/// (`MonotoneMuUpdate::update_barrier_parameter`):
///
/// * `compl_inf_tol` is converted into μ's scaled space first
/// (pounce#257 — upstream's `apply_obj_scaling`), since it is enforced
/// on the *unscaled* complementarity while μ and `tol` are scaled;
/// * the result is additionally `max`ed with the certificate-safe
/// `mu_min` (pounce#266) so the restoration sub-builder's
/// `100 · outer_mu_min` safeguard still applies. Capped that way,
/// `mu_min` can only raise the floor, never push it under the
/// certificate.
pub fn fixed_mode_mu_floor(&self, tol: Number, obj_scaling_factor: Number) -> Number {
let dynamic_floor = tol.min(crate::mu::scaled_compl_inf_tol(
self.compl_inf_tol,
obj_scaling_factor,
)) / (self.barrier_tol_factor + 1.0);
self.certificate_safe_mu_min(obj_scaling_factor)
.max(dynamic_floor)
}
/// Port of `AdaptiveMuUpdate::NewFixedMu`
/// (`IpAdaptiveMuUpdate.cpp:583-627`). Selects μ when the state
/// machine drops out of free mode. v1.0 always uses the
/// "average complementarity" branch (no `fix_mu_oracle_` is
/// wired; matches `fixed_mu_oracle = average_compl`).
///
/// The lower clamp is the certificate-safe `mu_min` (pounce#266);
/// capped ≤ raw `mu_min`, so the `[mu_min, mu_max]` band the lazy
/// `mu_max` init guarantees stays valid.
fn new_fixed_mu(&self, cq: &IpoptCqHandle, mu_min: Number) -> Number {
let avrg = cq.borrow().curr_avrg_compl();
let new_mu = self.adaptive_mu_monotone_init_factor * avrg;
new_mu.clamp(mu_min, self.mu_max)
}
/// Upstream's tiny-step termination test (pounce#512), shared by the
/// two sites that throw `TINY_STEP_DETECTED` in
/// `IpAdaptiveMuUpdate.cpp` — `:330-333` in the fixed-mode
/// Fiacco-McCormick decrease and `:377-380` on the free→fixed switch.
/// Both read `tiny_step_flag && new_mu == mu`: a tiny step was
/// detected *and* the update could not move μ, so no further
/// progress is available and the honest exit is "problem solved to
/// best possible numerical accuracy" (`STOP_AT_TINY_STEP`) rather
/// than iterating to the limit.
///
/// Exact equality, like upstream. Both callers reach "unchanged" by
/// clamping to the same bound, which is bit-exact; an epsilon band
/// would instead swallow a genuine — if minute — reduction and stop
/// an iteration early.
fn tiny_step_is_terminal(tiny_step_flag: bool, new_mu: Number, curr_mu: Number) -> bool {
tiny_step_flag && new_mu == curr_mu
}
}
impl MuUpdate for AdaptiveMuUpdate {
/// Port of `IpAdaptiveMuUpdate.cpp:InitializeImpl`. Seeds
/// `curr_mu = mu_init`, `curr_tau = max(tau_min, 1 - mu_init)`,
/// resets the globalization state, and starts in free-μ mode
/// (`SetFreeMuMode(true)` at line 239).
fn initialize(&mut self, data: &IpoptDataHandle) {
// Mirror upstream `IpAdaptiveMuUpdate.cpp:246-247`:
// IpData().Set_mu(1.);
// IpData().Set_tau(0.);
// These are placeholder values so `CalculateSafeSlack` and the
// first output line have something to work with; the actual μ
// is computed by the oracle at iter 0's `update_barrier_parameter`.
// Setting curr_mu = mu_init here (as we used to) skipped the
// oracle's iter-0 call and locked μ at mu_init for the first
// Newton step — diverging from upstream's iter-0 behaviour
// (PFIT3: upstream iter 0 oracle picked μ=1.6e-6, pounce was
// stuck at μ=0.1, producing different iter-1 trial point).
let mut d = data.borrow_mut();
d.curr_mu = 1.0;
d.curr_tau = 0.0;
drop(d);
self.free_mu_mode = true;
self.refs_vals.clear();
self.filter.clear();
self.accepted_point = None;
self.init_dual_inf = -1.0;
self.init_primal_inf = -1.0;
// Reset mu_max sentinel so a re-solve re-runs the lazy init
// against the fresh starting iterate's curr_avrg_compl.
// Upstream re-enters InitializeImpl on each solve which
// (lines 160-165) resets `mu_max_ = -1.` when not user-set.
self.mu_max = -1.0;
// Reset no-bounds detection on re-solve.
self.no_bounds = false;
}
/// Adaptive μ update — port of `UpdateBarrierParameter`
/// (`IpAdaptiveMuUpdate.cpp:252-444`). Runs the FreeMuMode /
/// FixedMuMode state machine:
///
/// * **FreeMuMode**: ask the configured oracle for a candidate
/// (LOQO closed-form, Probing predictor solve, or
/// QualityFunction golden-section). If progress is sufficient,
/// stay in free mode and remember the iterate; otherwise switch
/// to fixed mode at `new_fixed_mu`.
/// * **FixedMuMode**: monotone Fiacco-McCormick reduction
/// (`min(linear · μ, μ^superlinear_power)`). Switch back to
/// free mode once the globalization criterion is satisfied
/// again.
///
/// Probing / QualityFunction silently fall back to LOQO when
/// `nlp` / `pd_search_dir` are unavailable (mirrors upstream
/// lines 402-408).
///
/// Line-search reset: upstream calls `linesearch_->Reset()` at
/// three points — line 339 (fixed-mode decrease), line 386
/// (free→fixed switch) and line 431 (**every** free-mode
/// iteration, whether or not μ moved). The [`MuUpdate`] trait
/// surface carries no line-search handle, so we raise
/// [`IpoptData::request_ls_reset`] at exactly those three points
/// and `IpoptAlgorithm::iterate` performs the reset right after
/// this call returns — the same plumbing the pounce#58 probing
/// guard uses for [`IpoptData::request_resto`]. See pounce#510:
/// the previous "reset when μ changed" proxy in the caller is
/// correct for the monotone update but not for this one, and left
/// the filter holding pre-restoration entries whenever μ happened
/// to stay put.
///
/// [`IpoptData::request_ls_reset`]: crate::ipopt_data::IpoptData::request_ls_reset
/// [`IpoptData::request_resto`]: crate::ipopt_data::IpoptData::request_resto
fn update_barrier_parameter(
&mut self,
data: &IpoptDataHandle,
cq: &IpoptCqHandle,
nlp: Option<&Rc<RefCell<dyn IpoptNlp>>>,
pd_search_dir: Option<&mut PdSearchDirCalc>,
) -> Number {
// Lazy `mu_max` init — port of `IpAdaptiveMuUpdate.cpp:267-274`.
// Upstream computes `mu_max = mu_max_fact * curr_avrg_compl()`
// on the first call when the user did not set `mu_max`
// explicitly. Pounce previously hard-coded `mu_max = 1e5`,
// which let `new_fixed_mu = 0.8 * curr_avrg_compl` cap at 1e5
// — on DECONVBNE that allowed μ to jump from 2.5e-3 to ~2000
// at iter 198, destabilising the rest of the run.
if self.mu_max < 0.0 {
let avrg = cq.borrow().curr_avrg_compl();
self.mu_max = Self::lazy_mu_max(self.mu_max_fact, avrg, self.mu_init, self.mu_min);
}
// No-bounds short-circuit — port of `IpAdaptiveMuUpdate.cpp:282-296`.
// Detect once on the first call whether the iterate has any
// bound multipliers (z_l, z_u, v_l, v_u). When all four are
// dim-zero (equality-only TNLPs: BT3, GENHS28, HS50, METHANL8,
// ...), `curr_avrg_compl()` is 0, hence `mu_max = 0`, and the
// later `clamp(mu_min, mu_max)` panics with `min > max`.
// Upstream sets `mu = mu_min`, `tau = tau_min`, and short-
// circuits all subsequent oracle work; we mirror that.
if !self.no_bounds {
let n_bounds = {
let d = data.borrow();
let c = d.curr.as_ref().expect("curr set");
c.z_l.dim() + c.z_u.dim() + c.v_l.dim() + c.v_u.dim()
};
if n_bounds == 0 {
self.no_bounds = true;
let mut d = data.borrow_mut();
d.curr_mu = self.mu_min;
d.curr_tau = self.tau_min;
return self.mu_min;
}
}
if self.no_bounds {
let mut d = data.borrow_mut();
d.curr_mu = self.mu_min;
d.curr_tau = self.tau_min;
return self.mu_min;
}
// Read-and-clear `tiny_step_flag` — mirrors upstream
// `IpAdaptiveMuUpdate.cpp:297-298`. The flag is consumed by
// this call: without the clear, a single tiny-step detection
// would persist forever and suppress `sufficient_progress` on
// every later outer iter.
let (curr_mu, iter_count, tiny_step_flag) = {
let mut d = data.borrow_mut();
let out = (d.curr_mu, d.iter_count, d.tiny_step_flag);
d.tiny_step_flag = false;
out
};
// NB: do NOT short-circuit at iter_count==0. Upstream's
// `UpdateBarrierParameter` runs the oracle at iter 0 (the
// initialize() above set μ=1.0 as a placeholder only). Skipping
// the oracle here locked μ at the placeholder for the first
// Newton step. Letting the iter-0 path flow through the
// free-μ branch picks up the oracle's choice — the empty
// `refs_vals_` makes `check_sufficient_progress` return true,
// we remember the iterate, then call the oracle below.
// `tiny_step_flag` (and upstream's `CheckSkippedLineSearch()`,
// which is only set in non-rigorous resto mode) forces
// `sufficient_progress = false` when not in `NEVER_MONOTONE_MODE`
// — see `IpAdaptiveMuUpdate.cpp:347-351`. This is what lets a
// stalled outer iter drop into fixed-μ and re-seed μ via
// `new_fixed_mu` instead of the oracle re-driving μ further down.
let force_no_progress = tiny_step_flag
&& self.adaptive_mu_globalization != AdaptiveMuGlobalization::NeverMonotoneMode;
// Certificate-safe μ floor (pounce#266): every place below that
// stops μ from descending — the fixed-mode reduction, the
// fixed-mode re-seed, the oracles' internal clamps, and the final
// band clamp — must use `mu_min` capped into the space the
// certificate lives in, or a strongly scaled-down objective ends
// `Solved_To_Acceptable_Level` on an iterate at the optimum. The
// `no_bounds` short-circuit above keeps the raw `mu_min`: with no
// bound multipliers there is no complementarity to certify.
let obj_scaling_factor = cq.borrow().obj_scaling_factor();
let mu_min = self.certificate_safe_mu_min(obj_scaling_factor);
if !self.free_mu_mode {
// Fixed-mu branch — `cpp:299-342`.
//
// The gate is `sufficient_progress && !tiny_step_flag`
// (`cpp:304`) — plain `tiny_step_flag`, *not* the
// globalization-conditional `force_no_progress`, which
// upstream applies only in the free-mode branch below
// (`cpp:347-351`). Reusing `force_no_progress` here let
// `adaptive_mu_globalization=never-monotone-mode` switch back
// to free mode on a flagged tiny step, which upstream never
// does and which routed around the termination at `cpp:330`.
// At the default `obj-constr-filter` the two are equal, so
// this distinction only moves never-monotone-mode (pounce#512).
let sufficient_progress = !tiny_step_flag && self.check_sufficient_progress(cq);
if sufficient_progress {
// Switch back to free mode and record the iterate —
// upstream `cpp:303-311`. Upstream does NOT return
// here: after flipping `FreeMuMode` to true the first
// if/else ends and control reaches the `if
// FreeMuMode()` block at `cpp:391`, which runs the
// oracle and picks a fresh μ in the SAME iteration.
// Returning `curr_mu` here froze μ on the transition
// iter — PALMER4's iter-15 fixed→free transition kept
// μ at 2.4e-7 instead of letting the oracle drop it to
// mu_min, stalling to Maximum_Iterations_Exceeded.
// Fall through to the oracle call below.
self.free_mu_mode = true;
self.remember_current_point_as_accepted(data, cq);
} else {
// Keep reducing μ Fiacco-McCormick style if the
// barrier subproblem is solved to within
// `barrier_tol_factor · μ`, OR if a tiny step was
// just detected (`cpp:320` `|| tiny_step_flag`).
let sub_problem_error = cq.borrow().curr_barrier_error();
if sub_problem_error <= self.barrier_tol_factor * curr_mu || tiny_step_flag {
let lin = self.mu_linear_decrease_factor * curr_mu;
let sup = curr_mu.powf(self.mu_superlinear_decrease_power);
// Fixed-mode floor is NOT `mu_min` — see
// [`Self::fixed_mode_mu_floor`] (pounce#511).
let tol = data.borrow().tol;
let floor = self.fixed_mode_mu_floor(tol, obj_scaling_factor);
let new_mu = lin.min(sup).max(floor).min(self.mu_max);
// `cpp:330-333` — a tiny step was flagged and the
// decrease left μ where it was (it is pinned at the
// floor), so there is nothing left to try. Upstream
// throws TINY_STEP_DETECTED *before* `Set_mu`/`Set_tau`;
// the flag is unchanged by construction, so returning
// it below is the same iterate either way. Pairing it
// with the #511 floor is upstream's own pairing: the
// termination triggers off the same floor the decrease
// stops at, so it now fires at the tolerance-derived
// floor instead of at `mu_min`.
if Self::tiny_step_is_terminal(tiny_step_flag, new_mu, curr_mu) {
data.borrow_mut().request_tiny_step_stop = true;
}
let new_tau = self.tau_min.max(1.0 - new_mu);
let mut d = data.borrow_mut();
d.curr_tau = new_tau;
// Upstream `cpp:339` — reset inside this branch,
// unconditionally, even when the clamps leave μ
// where it was (pounce#510).
d.request_ls_reset = true;
return new_mu;
}
// Subproblem not yet solved — keep μ. Upstream does NOT
// reset the line search on this path (`cpp:335-341`).
let new_tau = self.tau_min.max(1.0 - curr_mu);
data.borrow_mut().curr_tau = new_tau;
return curr_mu;
}
} else {
// Free-mu branch — `cpp:343-389`.
let sufficient_progress = !force_no_progress && self.check_sufficient_progress(cq);
if sufficient_progress {
self.remember_current_point_as_accepted(data, cq);
// Fall through to the oracle call below.
} else {
if std::env::var("POUNCE_DBG_AMU").is_ok() {
let cqr = cq.borrow();
let theta = cqr.curr_constraint_violation();
let f = cqr.curr_f();
let nlp_err = cqr.curr_nlp_error();
let avrg = cqr.curr_avrg_compl();
drop(cqr);
let margin = self.filter_margin_fact * self.filter_max_margin.min(nlp_err);
let entries: Vec<(Number, Number, i32)> = self
.filter
.entries()
.iter()
.map(|e| (e.theta, e.phi, e.iter))
.collect();
tracing::debug!(target: "pounce::mu",
"[AMU] iter={} free->fixed: curr_mu={:.3e} theta={:.3e} f={:.3e} nlp_err={:.3e} margin={:.3e} avrg_compl={:.3e} new_mu={:.3e} | filter={:?} | force_no_progress={} tiny={}",
iter_count,
curr_mu,
theta,
f,
nlp_err,
margin,
avrg,
self.adaptive_mu_monotone_init_factor * avrg,
entries,
force_no_progress,
tiny_step_flag,
);
}
// Switch into fixed mode.
self.free_mu_mode = false;
if self.restore_accepted_iterate {
if let Some(prev) = self.accepted_point.clone() {
let mut d = data.borrow_mut();
d.set_trial(prev);
d.accept_trial_point();
}
}
let new_mu = self.new_fixed_mu(cq, mu_min);
// `cpp:377-380` — the same termination on the other
// throw site: the switch into fixed mode re-seeded μ to
// the value it already had, so the tiny step cannot be
// walked off by changing μ either. Ordered after the
// free-mode flip and the accepted-iterate restore, as
// upstream is.
if Self::tiny_step_is_terminal(tiny_step_flag, new_mu, curr_mu) {
data.borrow_mut().request_tiny_step_stop = true;
}
let new_tau = self.tau_min.max(1.0 - new_mu);
let mut d = data.borrow_mut();
d.curr_tau = new_tau;
// Upstream `cpp:386` — the free→fixed switch resets the
// line search whether or not `new_fixed_mu` differs from
// the μ we came in with (pounce#510).
d.request_ls_reset = true;
return new_mu;
}
}
// ----- Free-mu oracle call (cpp:391-436) -----
let cq_ref = cq.borrow();
let dual_inf = cq_ref.curr_dual_infeasibility_max();
let primal_inf = cq_ref.curr_primal_infeasibility_max();
let avrg_compl = cq_ref.curr_avrg_compl();
let centrality_xi = cq_ref.curr_centrality_measure();
let nlp_error = cq_ref.curr_nlp_error();
drop(cq_ref);
// τ = max(tau_min, 1 - curr_nlp_error) — upstream cpp:397.
let tau = self.tau_min.max(1.0 - nlp_error);
data.borrow_mut().curr_tau = tau;
let loqo_candidate = || {
let mut oracle = LoqoMuOracle {
mu_min,
mu_max: self.mu_max,
avrg_compl,
centrality_xi,
};
oracle.calculate_mu().unwrap_or(curr_mu)
};
let candidate = match self.mu_oracle {
MuOracleKind::Loqo => loqo_candidate(),
MuOracleKind::Probing => {
// Iterate-quality guard (pounce#58). The probing
// oracle uses `curr_avrg_compl()` for its `mu_curr`
// input (see `mu/oracle/probing.rs:85`). When a single
// imbalanced `(s_i, z_i)` pair inflates the average
// many orders above the stored `data.curr_mu`,
// probing's `σ·mu_curr` correctly returns the inflated
// value and the resulting search direction throws the
// iterate out of the convergence neighborhood. On
// arki0012 this manifests as μ jumping 5 orders at
// iter 155 followed by divergence to "Local
// Infeasibility" at iter 284. We short-circuit by
// signalling restoration and keeping μ unchanged; the
// main loop in `ipopt_alg.rs` consumes the flag
// before the search-direction step.
if Self::probing_iterate_guard_fires(
self.probing_iterate_quality_factor,
curr_mu,
avrg_compl,
) {
if std::env::var("POUNCE_DBG_ORACLE").is_ok() {
tracing::debug!(target: "pounce::mu",
"[PN_PROBE_GUARD] iter={} curr_mu={:.3e} avrg_compl={:.3e} ratio={:.3e} > factor={:.3e} → request_resto",
iter_count,
curr_mu,
avrg_compl,
avrg_compl / curr_mu,
self.probing_iterate_quality_factor,
);
}
// No `request_ls_reset` here: this early return is a
// pounce-specific guard with no upstream counterpart,
// it leaves μ untouched, and the caller hands the
// iterate straight to restoration.
data.borrow_mut().request_resto = true;
return curr_mu;
}
match (nlp, pd_search_dir) {
(Some(nlp), Some(sd)) => {
let mut oracle = ProbingMuOracle {
// Forward the user-set `sigma_max` (default 1e2),
// matching upstream `IpProbingMuOracle.cpp`, which
// reads `options.GetNumericValue("sigma_max", ...)`
// and caps `sigma = Min(sigma, sigma_max_)`. This
// was hard-coded to 100.0, so a user-set `sigma_max`
// reached only the quality-function oracle (L3).
sigma_max: self.sigma_max,
mu_min,
mu_max: self.mu_max,
mu_curr: curr_mu,
mu_aff: curr_mu,
};
oracle
.calculate_mu_with_affine_step(data, cq, nlp, sd, 1.0)
.unwrap_or_else(loqo_candidate)
}
_ => loqo_candidate(),
}
}
MuOracleKind::QualityFunction => match (nlp, pd_search_dir) {
(Some(nlp), Some(sd)) => {
let mut oracle = QualityFunctionMuOracle::new();
oracle.mu_min = mu_min;
oracle.mu_max = self.mu_max;
oracle.sigma_min = self.sigma_min;
oracle.sigma_max = self.sigma_max;
oracle.norm_type = self.qf_norm_type;
oracle.centrality_type = self.qf_centrality_type;
oracle.balancing_term = self.qf_balancing_term;
oracle.max_section_steps = self.qf_max_section_steps;
oracle.section_sigma_tol = self.qf_section_sigma_tol;
oracle.section_qf_tol = self.qf_section_qf_tol;
// Mirrors upstream's `quality_function_search` timer
// around `CalculateMu` in `IpQualityFunctionMuOracle.cpp`.
let timing = data.borrow().timing.clone();
let _qf_guard = timing.quality_function_search.guard();
oracle
.calculate_mu_with_predictor_centering(data, cq, nlp, sd)
.unwrap_or_else(loqo_candidate)
}
_ => loqo_candidate(),
},
};
// Safeguard floor + global band clamp (cpp:410-426).
let lower = self.lower_mu_safeguard(dual_inf, primal_inf, candidate);
let mu = candidate.max(mu_min).max(lower).min(self.mu_max);
// Upstream `cpp:431` — the free-mode block closes with an
// unconditional `linesearch_->Reset()`. This is the point the
// old caller-side "μ changed" proxy missed (pounce#510): it
// fires on every free-mode iteration, including the ones where
// the oracle re-picks the μ we already had, and including the
// fixed→free transition that falls through to here. Filter
// entries are keyed on a barrier parameter *and* an iterate;
// "μ is unchanged" does not make yesterday's entries valid.
data.borrow_mut().request_ls_reset = true;
// NB: upstream `IpAdaptiveMuUpdate.cpp:410-426` does NOT require
// `mu ≤ curr_mu` in free mode — the oracle is allowed to bump
// μ back up. A prior attempt to cap growth here ("HAIFAM
// stability hack") let DECONVBNE's μ plunge from 0.1 to 5e-10
// in ~20 iters and never recover (upstream oscillates μ in
// [-8,-1] for the same range), trapping `inf_du` at 1e13.
// Tiny-step skips are already handled by the
// `tiny_step_flag → force_no_progress → new_fixed_mu` path
// above, which can raise μ via the fixed-mode branch.
mu
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mu::test_fixture;
/// pounce#510: upstream resets the line search on **every** free-mode
/// iteration (`IpAdaptiveMuUpdate.cpp:431`), not only when μ moves.
/// The caller used to infer the reset from `next_mu != mu_before`,
/// which silently skipped it whenever the oracle re-picked the μ we
/// already had — leaving the filter holding entries computed against
/// an iterate and a barrier parameter the algorithm had left behind.
#[test]
fn free_mode_requests_ls_reset_even_when_mu_is_unchanged() {
let mut a = AdaptiveMuUpdate::new();
// Never-monotone globalization keeps the state machine in free
// mode across both calls, which is the endgame this issue is
// about; the filter/KKT variants are covered below.
a.adaptive_mu_globalization = AdaptiveMuGlobalization::NeverMonotoneMode;
let (data, cq) = test_fixture::fixture(0.1);
// First pass: free mode with an empty filter ⇒ sufficient
// progress ⇒ the oracle picks μ.
let mu1 = a.update_barrier_parameter(&data, &cq, None, None);
assert!(a.free_mu_mode);
assert!(data.borrow().request_ls_reset);
// Re-enter at exactly the μ the oracle just chose, on the same
// (unchanged) iterate: μ cannot move, and the pre-fix caller
// would therefore never reset.
data.borrow_mut().request_ls_reset = false;
data.borrow_mut().curr_mu = mu1;
let mu2 = a.update_barrier_parameter(&data, &cq, None, None);
assert_eq!(mu2, mu1, "fixture must hold μ still for this test");
assert!(
data.borrow().request_ls_reset,
"free-mode iteration must request a line-search reset with μ unchanged"
);
}
/// pounce#510: the free→fixed switch is upstream's `cpp:386` reset,
/// which likewise does not care whether `new_fixed_mu` differs from
/// the incoming μ.
#[test]
fn free_to_fixed_switch_requests_ls_reset() {
let mut a = AdaptiveMuUpdate::new();
let (data, cq) = test_fixture::fixture(0.1);
// Seed the filter with the current point, then re-run: the same
// (θ, f) is now dominated, so progress is insufficient and the
// update drops into fixed mode.
let _ = a.update_barrier_parameter(&data, &cq, None, None);
data.borrow_mut().request_ls_reset = false;
let _ = a.update_barrier_parameter(&data, &cq, None, None);
assert!(
!a.free_mu_mode,
"fixture must fall out of free mode for this test"
);
assert!(data.borrow().request_ls_reset);
}
/// pounce#510: the fixed-mode μ decrease is upstream's `cpp:339`
/// reset. Note it fires inside the branch, so a decrease that the
/// `mu_min`/`mu_max` clamps flatten still resets.
#[test]
fn fixed_mode_decrease_requests_ls_reset() {
let mut a = AdaptiveMuUpdate::new();
let (data, cq) = test_fixture::fixture(0.1);
a.free_mu_mode = false;
// Force "no sufficient progress" so the update stays in fixed
// mode, and a barrier tolerance loose enough that the decrease
// branch fires on this (far-from-optimal) iterate.
a.adaptive_mu_globalization = AdaptiveMuGlobalization::KktError;
a.adaptive_mu_kkterror_red_iters = 1;
a.adaptive_mu_kkterror_red_fact = 0.0;
a.refs_vals.push_back(1.0);
a.barrier_tol_factor = 1e6;
// Degenerate decrease factors: `min(1·μ, μ^1) = μ`. The branch is
// taken but μ does not move, so the pre-fix `next_mu != mu_before`
// proxy would have skipped the reset here as well.
a.mu_linear_decrease_factor = 1.0;
a.mu_superlinear_decrease_power = 1.0;
let mu = a.update_barrier_parameter(&data, &cq, None, None);
assert!(!a.free_mu_mode, "must stay in fixed mode for this test");
assert_eq!(mu, 0.1, "flat decrease leaves μ where it was");
assert!(data.borrow().request_ls_reset);
}
/// The one fixed-mode path upstream leaves alone (`cpp:335-341`):
/// the barrier subproblem is not solved yet, μ stays, no reset.
#[test]
fn fixed_mode_without_decrease_does_not_request_ls_reset() {
let mut a = AdaptiveMuUpdate::new();
let (data, cq) = test_fixture::fixture(1e-8);
a.free_mu_mode = false;
// A far-from-optimal iterate at a tiny μ: the barrier error is
// way above `barrier_tol_factor · μ`, and the filter is empty so
// `check_sufficient_progress` must be forced to fail.
a.adaptive_mu_globalization = AdaptiveMuGlobalization::KktError;
a.adaptive_mu_kkterror_red_iters = 1;
a.adaptive_mu_kkterror_red_fact = 0.0;
a.refs_vals.push_back(1.0);
let mu = a.update_barrier_parameter(&data, &cq, None, None);
assert!(!a.free_mu_mode);
assert_eq!(mu, 1e-8);
assert!(!data.borrow().request_ls_reset);
}
/// pounce#266, adaptive twin of the monotone test: the raw `mu_min`
/// clamp must yield to `compl_inf_tol·|df|/(barrier_tol_factor+1)` once
/// |df| drops below `df* = mu_min·(barrier_tol_factor+1)/compl_inf_tol`,
/// or the strict certificate is unreachable and the solve degrades to
/// `Solved_To_Acceptable_Level` (code 100) at the optimum.
#[test]
fn adaptive_mu_min_is_capped_so_certificate_stays_reachable() {
let a = AdaptiveMuUpdate::new();
let df_star = a.mu_min * (a.barrier_tol_factor + 1.0) / a.compl_inf_tol;
assert!((df_star - 1.1e-6).abs() < 1e-21);
for df in [1.0, -1.0, 1e-3, 1e-5, df_star] {
assert_eq!(a.certificate_safe_mu_min(df), a.mu_min);
}
// HS71 × 1e8 computes df = 8.3e-8, under the cliff: the cap engages.
let df = 8.3e-8;
let capped = a.certificate_safe_mu_min(df);
assert!(capped < a.mu_min);
assert!((capped - 1e-4 * 8.3e-8 / 11.0).abs() < 1e-27);
assert_eq!(a.certificate_safe_mu_min(-df), capped);
// Degenerate factors fall back to the unconverted tolerance, whose
// cap (9.09e-6) leaves mu_min alone.
for df in [0.0, Number::NAN, Number::INFINITY] {
assert_eq!(a.certificate_safe_mu_min(df), a.mu_min);
}
// The restoration sub-builder's `mu_min = 100 · outer_mu_min`
// safeguard survives: the resto inner IPM sees df = 1.
let mut resto = AdaptiveMuUpdate::new();
resto.mu_min = 100.0 * a.mu_min;
assert_eq!(resto.certificate_safe_mu_min(1.0), resto.mu_min);
}
/// pounce#511: the fixed-mode decrease must floor at upstream's
/// `Min(compl_inf_tol_scaled, tol)/(barrier_tol_factor+1)`, not at
/// `mu_min`. At default `tol=1e-8`, `compl_inf_tol=1e-4`,
/// `barrier_tol_factor=10` that is `1e-8/11 ≈ 9.09e-10` — ~91× above
/// `mu_min = 1e-11`, and further still at a looser `tol`.
#[test]
fn fixed_mode_floor_matches_upstream_not_mu_min() {
let a = AdaptiveMuUpdate::new();
let floor = a.fixed_mode_mu_floor(1e-8, 1.0);
assert!((floor - 1e-8 / 11.0).abs() < 1e-20, "floor was {floor}");
// ~91× above `mu_min` — the old floor — i.e. nearly two orders.
assert!(floor / a.mu_min > 90.0, "floor was {floor}");
// Looser `tol` raises the floor with it (upstream takes the min of
// `tol` and `compl_inf_tol`, so `tol` binds until it exceeds 1e-4).
assert!((a.fixed_mode_mu_floor(1e-6, 1.0) - 1e-6 / 11.0).abs() < 1e-18);
// Beyond that `compl_inf_tol` binds.
assert!((a.fixed_mode_mu_floor(1e-2, 1.0) - 1e-4 / 11.0).abs() < 1e-18);
}
/// The `compl_inf_tol` half of the floor is converted into μ's scaled
/// space before the `Min` (upstream's `apply_obj_scaling`, pounce#257),
/// so the two disagree whenever objective scaling is active.
#[test]
fn fixed_mode_floor_scales_compl_inf_tol() {
let a = AdaptiveMuUpdate::new();
// df = 1e-6 puts scaled compl_inf_tol at 1e-10, under `tol=1e-8`,
// so it is the binding half: 1e-10/11 ≈ 9.09e-12.
let df = 1e-6;
let floor = a.fixed_mode_mu_floor(1e-8, df);
assert!(
(floor - 1e-4 * df / 11.0).abs() < 1e-24,
"floor was {floor}"
);
// Sign of the scaling factor (maximization poses df < 0) is
// irrelevant — the magnitude is what converts spaces.
assert_eq!(a.fixed_mode_mu_floor(1e-8, -df), floor);
// Degenerate factors fall back to the unconverted tolerance.
for df in [0.0, Number::NAN, Number::INFINITY] {
assert!((a.fixed_mode_mu_floor(1e-8, df) - 1e-8 / 11.0).abs() < 1e-20);
}
}
/// The restoration sub-builder's `mu_min = 100 · outer_mu_min`
/// safeguard still binds when it sits above the tolerance floor: the
/// certificate-safe `mu_min` is `max`ed in, mirroring monotone mode.
#[test]
fn fixed_mode_floor_keeps_resto_mu_min_safeguard() {
let mut resto = AdaptiveMuUpdate::new();
resto.mu_min = 1e-6; // well above tol/(barrier_tol_factor+1) = 9.09e-10
// `RestoIpoptNlp` does not override obj scaling — the resto inner
// IPM sees df = 1, so the cap leaves `mu_min` alone and it wins.
assert_eq!(resto.fixed_mode_mu_floor(1e-8, 1.0), 1e-6);
}
#[test]
fn lower_mu_safeguard_initializes_from_first_call() {
let mut a = AdaptiveMuUpdate::new();
a.adaptive_mu_safeguard_factor = 1e-2;
// First call captures init values.
let _ = a.lower_mu_safeguard(0.5, 2.0, 1.0);
assert_eq!(a.init_dual_inf, 1.0); // max(1, 0.5)
assert_eq!(a.init_primal_inf, 2.0); // max(1, 2.0)
}
#[test]
fn lower_mu_safeguard_takes_max_of_dual_and_primal_terms() {
let mut a = AdaptiveMuUpdate::new();
a.adaptive_mu_safeguard_factor = 1.0;
// Primal term dominates.
let r = a.lower_mu_safeguard(0.1, 5.0, 1e9);
// init_dual = 1, init_primal = 5 → terms: 0.1, 1.0 → max = 1.0.
assert!((r - 1.0).abs() < 1e-15);
}
#[test]
fn kkt_error_globalization_clips_to_min_ref_val() {
let mut a = AdaptiveMuUpdate::new();
a.adaptive_mu_globalization = AdaptiveMuGlobalization::KktError;
a.adaptive_mu_safeguard_factor = 1.0;
// Without clip, safeguard would be 5.0; min_ref_val = 0.1 wins.
let r = a.lower_mu_safeguard(0.1, 5.0, 0.1);
assert!((r - 0.1).abs() < 1e-15);
}
#[test]
fn reset_clears_init_inf() {
let mut a = AdaptiveMuUpdate::new();
a.adaptive_mu_safeguard_factor = 1.0;
let _ = a.lower_mu_safeguard(0.5, 2.0, 1.0);
a.reset_init_inf();
assert_eq!(a.init_dual_inf, -1.0);
assert_eq!(a.init_primal_inf, -1.0);
}
// The trait `update_barrier_parameter` now takes
// `(&IpoptDataHandle, &IpoptCqHandle)`. End-to-end coverage of the
// adaptive path lands alongside the integration test that drives
// `IpoptAlgorithm::optimize` with `mu_strategy=adaptive`; in
// isolation the unit tests above exercise the safeguard
// arithmetic and option defaults.
#[test]
fn default_mu_oracle_is_quality_function() {
let a = AdaptiveMuUpdate::new();
assert_eq!(a.mu_oracle, MuOracleKind::QualityFunction);
}
#[test]
fn mu_oracle_kind_is_distinct() {
assert_ne!(MuOracleKind::Loqo, MuOracleKind::Probing);
assert_ne!(MuOracleKind::Probing, MuOracleKind::QualityFunction);
assert_ne!(MuOracleKind::Loqo, MuOracleKind::QualityFunction);
}
// pounce#58 guard predicate. Numbers below come from the issue
// body's iter 154-155 trace on arki0012.
#[test]
fn probing_iterate_guard_fires_on_arki0012_iter155() {
let curr_mu = 1.98e-11;
let avrg_compl = 8.90e-6;
assert!(AdaptiveMuUpdate::probing_iterate_guard_fires(
1e4, curr_mu, avrg_compl
));
}
#[test]
fn probing_iterate_guard_quiet_on_healthy_iter() {
// iter 154 in the same trace — ratio ≈ 2.2; ought not fire.
let curr_mu = 1.02e-11;
let avrg_compl = 2.24e-11;
assert!(!AdaptiveMuUpdate::probing_iterate_guard_fires(
1e4, curr_mu, avrg_compl
));
}
#[test]
fn probing_iterate_guard_disabled_at_zero_factor() {
// factor=0 ⇒ guard off, even with extreme ratio.
assert!(!AdaptiveMuUpdate::probing_iterate_guard_fires(
0.0, 1e-11, 1.0
));
}
#[test]
fn probing_iterate_guard_disabled_at_negative_factor() {
assert!(!AdaptiveMuUpdate::probing_iterate_guard_fires(
-1.0, 1e-11, 1.0
));
}
#[test]
fn probing_iterate_guard_quiet_when_curr_mu_zero() {
// Pathological `curr_mu = 0` (no-bounds branch zeroes it out).
// Predicate must stay quiet rather than division-by-zero.
assert!(!AdaptiveMuUpdate::probing_iterate_guard_fires(
1e4, 0.0, 1e-6
));
}
// Regression: `mu_strategy=adaptive` + `warm_start_init_point=yes`
// used to panic in `new_mu.clamp(mu_min, mu_max)` with
// "min > max ... min = 1e-11, max = 0.0" — the warm start zeroes the
// bound multipliers, so `curr_avrg_compl()` reads 0 even though
// bounds exist, collapsing `mu_max` to 0. `lazy_mu_max` must keep the
// band valid (mu_max >= mu_min) regardless of the `avrg` it is fed.
#[test]
fn lazy_mu_max_keeps_band_valid_on_zero_avrg_compl() {
let a = AdaptiveMuUpdate::new();
// Warm-start pathology: avrg_compl == 0.
let mu_max = AdaptiveMuUpdate::lazy_mu_max(a.mu_max_fact, 0.0, a.mu_init, a.mu_min);
assert!(
mu_max >= a.mu_min,
"mu_max {mu_max} must not fall below mu_min {}",
a.mu_min
);
// Falls back to the mu_init-scaled band: 1e3 * 0.1 = 100.
assert!((mu_max - a.mu_max_fact * a.mu_init).abs() < 1e-12);
}
#[test]
fn lazy_mu_max_unchanged_for_cold_start() {
let a = AdaptiveMuUpdate::new();
// A healthy cold start hands a positive avrg_compl; the band is
// mu_max_fact * avrg, exactly as before the warm-start guard.
let avrg = 2.5e-3;
let mu_max = AdaptiveMuUpdate::lazy_mu_max(a.mu_max_fact, avrg, a.mu_init, a.mu_min);
assert!((mu_max - a.mu_max_fact * avrg).abs() < 1e-15);
}
#[test]
fn lazy_mu_max_survives_nan_avrg_compl() {
let a = AdaptiveMuUpdate::new();
// A NaN avrg (the other half of the original panic message) must
// not propagate: `avrg > 0.0` is false for NaN, so we fall back.
let mu_max = AdaptiveMuUpdate::lazy_mu_max(a.mu_max_fact, f64::NAN, a.mu_init, a.mu_min);
assert!(mu_max.is_finite() && mu_max >= a.mu_min);
}
// pounce#512 — the shared condition behind both of upstream's
// `TINY_STEP_DETECTED` throws (`IpAdaptiveMuUpdate.cpp:330-333`,
// `:377-380`). Both conjuncts are load-bearing in opposite
// directions: without the flag the update is just at its floor and
// must keep iterating, and without the μ test a tiny step that the
// update *can* still respond to would stop the solve early.
#[test]
fn tiny_step_is_terminal_needs_the_flag_and_an_unmoved_mu() {
let mu = 1e-11;
assert!(AdaptiveMuUpdate::tiny_step_is_terminal(true, mu, mu));
// μ moved — the update has something left to try.
assert!(!AdaptiveMuUpdate::tiny_step_is_terminal(true, 0.2 * mu, mu));
// No tiny step: μ pinned at its floor is the ordinary end-game,
// not a reason to stop.
assert!(!AdaptiveMuUpdate::tiny_step_is_terminal(false, mu, mu));
assert!(!AdaptiveMuUpdate::tiny_step_is_terminal(
false,
0.2 * mu,
mu
));
}
/// Equality is exact, as upstream's `new_mu == mu` is. A reduction of
/// one ulp is a reduction; an epsilon band would call it "unchanged"
/// and terminate an iteration early.
#[test]
fn tiny_step_is_terminal_does_not_round_a_reduction_away() {
let mu = 1e-11;
let nudged = mu - f64::EPSILON * 1e-4;
assert!(nudged < mu, "test setup: the nudge must actually reduce μ");
assert!(!AdaptiveMuUpdate::tiny_step_is_terminal(true, nudged, mu));
}
#[test]
fn probing_iterate_guard_threshold_at_factor_times_mu() {
// Boundary: equality does NOT fire (strict >).
let curr_mu = 1.0e-10;
let factor = 1e4;
assert!(!AdaptiveMuUpdate::probing_iterate_guard_fires(
factor,
curr_mu,
factor * curr_mu
));
// Just above the boundary fires.
assert!(AdaptiveMuUpdate::probing_iterate_guard_fires(
factor,
curr_mu,
factor * curr_mu * (1.0 + 1e-12)
));
}
}