pounce_algorithm/sqp/line_search.rs
1//! l1-merit backtracking line search for SQP. The classic
2//! Han-Powell scheme:
3//!
4//! ```text
5//! φ(x; ν) = f(x) + ν · violation(x)
6//! violation(x) = Σ_i max(bl_i − c_i, 0) + max(c_i − bu_i, 0)
7//! + Σ_j max(xl_j − x_j, 0) + max(x_j − xu_j, 0)
8//! ```
9//!
10//! Step `p` is a descent direction of `φ(·; ν)` whenever
11//! `ν ≥ ‖λ_g‖_∞` (Nocedal-Wright §18.4). We adapt `ν` at every
12//! iteration as `ν ← max(ν, ‖λ_g_qp‖_∞ + buffer)` so the QP-
13//! derived multipliers are always dominated.
14//!
15//! Backtracking is plain Armijo:
16//!
17//! ```text
18//! φ(x + αp) ≤ φ(x) + η · α · D_p φ(x; ν)
19//! ```
20//!
21//! with predicted derivative
22//!
23//! ```text
24//! D_p φ ≈ ∇f(x)ᵀ p − ν · violation(x)
25//! ```
26//!
27//! (correct under the standard assumption that the QP step
28//! reduces the linearized constraint violation to zero).
29//!
30//! Phase 5b commit 5 deliverable. The filter alternative
31//! (Fletcher-Leyffer 2002) is opt-in via
32//! `SqpGlobalization::Filter` and lands as a follow-up.
33
34use crate::sqp::options::SqpOptions;
35use crate::sqp::problem::SqpProblemSpec;
36use pounce_common::types::{NLP_LOWER_BOUND_INF, NLP_UPPER_BOUND_INF, Number};
37
38/// l1 constraint + bound violation `‖max(bl − c, 0) + max(c − bu, 0)‖_1`
39/// (plus the same for variable bounds). Infinite bounds are treated
40/// as never violated.
41pub fn l1_violation(
42 x: &[Number],
43 c_vals: &[Number],
44 bl: &[Number],
45 bu: &[Number],
46 xl: &[Number],
47 xu: &[Number],
48) -> Number {
49 let mut v = 0.0_f64;
50 for (i, &ci) in c_vals.iter().enumerate() {
51 if bl[i] > NLP_LOWER_BOUND_INF {
52 v += (bl[i] - ci).max(0.0);
53 }
54 if bu[i] < NLP_UPPER_BOUND_INF {
55 v += (ci - bu[i]).max(0.0);
56 }
57 }
58 for (j, &xj) in x.iter().enumerate() {
59 if xl[j] > NLP_LOWER_BOUND_INF {
60 v += (xl[j] - xj).max(0.0);
61 }
62 if xu[j] < NLP_UPPER_BOUND_INF {
63 v += (xj - xu[j]).max(0.0);
64 }
65 }
66 v
67}
68
69pub struct LineSearchResult {
70 pub alpha: Number,
71 pub nu: Number,
72 pub x_new: Vec<Number>,
73 pub f_new: Number,
74 pub c_new: Vec<Number>,
75 pub success: bool,
76 /// Present only when the accepted step was a second-order
77 /// correction. Carries the SOC subproblem's own multipliers
78 /// `(λ_g, λ_x)`, which the driver must adopt **verbatim**
79 /// instead of interpolating the original QP's multipliers: the
80 /// step actually taken is the SOC step, so a consistent
81 /// `(step, multipliers)` pair is required for the quasi-Newton
82 /// Hessian update to stay well-conditioned.
83 pub soc_duals: Option<(Vec<Number>, Vec<Number>)>,
84}
85
86/// A second-order correction returned by a [`SocProvider`]: the
87/// corrected full step together with the correction subproblem's
88/// own multipliers, so the driver can keep step and duals
89/// consistent.
90pub struct SocStep {
91 pub p: Vec<Number>,
92 pub lambda_g: Vec<Number>,
93 pub lambda_x: Vec<Number>,
94}
95
96/// Second-order-correction (SOC) provider — the Maratos remedy
97/// (Nocedal-Wright §18.11; Fletcher-Leyffer 2002). Given the
98/// constraint values `c(x + p)` at the rejected **full** step, it
99/// returns a corrected full step `p_soc` (recomputed against the
100/// constraint curvature at the trial point), or `None` if the
101/// correction subproblem could not be solved.
102///
103/// The line searches call it at most once, on the first (α = 1)
104/// trial, and only when that step *increases* the constraint
105/// violation — the signature of the Maratos effect, where a good
106/// Newton step is rejected because the linearized constraints
107/// under-predict the true (curved) violation. The provider itself
108/// is built by the SQP driver ([`crate::sqp::sqp_alg`]), which owns
109/// the QP solver and the linearization data.
110pub type SocProvider<'a> = &'a mut (dyn FnMut(&[Number]) -> Option<SocStep> + 'a);
111
112/// A second-order-correction step is accepted only if it reduces the
113/// constraint violation to at most this fraction of the uncorrected
114/// full-step violation (Wächter-Biegler 2006 §3.3, `κ_soc`). Keeps
115/// the SOC strictly a feasibility-improving correction, so a step
116/// that merely lowers the merit/objective without fixing the
117/// linearization error is rejected in favor of ordinary
118/// backtracking.
119pub(crate) const KAPPA_SOC: Number = 0.99;
120
121/// A second-order correction is a *small* perturbation of the QP
122/// step — the min-norm correction has `‖p̂‖ = O(‖p‖²)`, so near the
123/// solution `‖p_soc‖ ≈ ‖p‖`. We therefore reject any "correction"
124/// that grows the step beyond this multiple of `‖p‖_∞`: far from the
125/// solution, a quasi-Newton Hessian can make the re-solved SOC
126/// subproblem return a much longer step that overshoots and
127/// destabilizes the iteration, which is not what a correction should
128/// do. Bounding the growth keeps the Maratos remedy local without a
129/// separate trust region.
130pub(crate) const SOC_MAX_STEP_GROWTH: Number = 2.0;
131
132/// `‖v‖_∞`, **propagating `NaN` rather than swallowing it** (gh #876).
133///
134/// `f64::max` is defined to *ignore* `NaN`, so the obvious
135/// `fold(0.0, f64::max)` reports the ∞-norm of an all-`NaN` vector as a
136/// perfect `0.0`. Every convergence and acceptance test in this arm is a
137/// comparison of such a norm against a tolerance, and `0.0 <= tol` passes —
138/// so the reduction turns a fully diverged iterate into a declaration of
139/// optimality. `pounce-convex` learned this as gh #222 and has carried a
140/// short-circuiting `inf_norm` since; gh #845 fixed a third instance in
141/// `pounce-sensitivity`. This is the SQP arm's copy.
142///
143/// `NaN` short-circuits, so the norm is genuinely `NaN` and every `<= tol`
144/// test against it is false — which is the correct answer.
145pub(crate) fn inf_norm(v: &[Number]) -> Number {
146 let mut m = 0.0_f64;
147 for &x in v {
148 if x.is_nan() {
149 return Number::NAN;
150 }
151 m = m.max(x.abs());
152 }
153 m
154}
155
156/// Adapt `ν` against the QP multiplier magnitude and run Armijo
157/// backtracking on the l1 merit function. Returns the accepted
158/// step length, the updated ν, and the resulting trial state
159/// (`x_new`, `f_new`, `c_new`) so the caller doesn't have to
160/// re-evaluate.
161#[allow(clippy::too_many_arguments)]
162pub fn l1_merit_line_search<N: SqpProblemSpec>(
163 nlp: &mut N,
164 x: &[Number],
165 p: &[Number],
166 qp_lambda_g: &[Number],
167 grad_f: &[Number],
168 f_curr: Number,
169 c_curr: &[Number],
170 bl: &[Number],
171 bu: &[Number],
172 xl: &[Number],
173 xu: &[Number],
174 current_nu: Number,
175 opts: &SqpOptions,
176 mut soc: Option<SocProvider<'_>>,
177) -> LineSearchResult {
178 // ν adaptation (Han-Powell): dominate the QP multipliers by
179 // an additive safety margin, then clamp at l1_penalty_max so
180 // a pathological |λ_qp| spike doesn't blow the merit into a
181 // regime where Armijo always fails. Nocedal-Wright §18.4
182 // recommends `ν ≥ ‖λ‖_∞`; we use `+ l1_penalty_safety` to
183 // give the test a comfortable inequality.
184 let lambda_inf = qp_lambda_g.iter().map(|l| l.abs()).fold(0.0_f64, f64::max);
185 let nu = current_nu
186 .max(lambda_inf + opts.l1_penalty_safety)
187 .min(opts.l1_penalty_max);
188
189 let viol_curr = l1_violation(x, c_curr, bl, bu, xl, xu);
190 let phi_curr = f_curr + nu * viol_curr;
191
192 let grad_p: Number = grad_f.iter().zip(p.iter()).map(|(g, pi)| g * pi).sum();
193 // Predicted decrease: linear-objective contribution minus
194 // the violation we expect the QP to eliminate.
195 let predicted = grad_p - nu * viol_curr;
196 let eta = 1e-4_f64;
197
198 let mut alpha = 1.0_f64;
199 let mut x_trial = vec![0.0; x.len()];
200 let mut last_f = f_curr;
201 let mut last_c = c_curr.to_vec();
202 let mut first_trial = true;
203 while alpha > opts.bt_min_alpha {
204 for (xt, (&xi, &pi)) in x_trial.iter_mut().zip(x.iter().zip(p.iter())) {
205 *xt = xi + alpha * pi;
206 }
207 let f_trial = nlp.eval_f(&x_trial);
208 let c_trial = nlp.eval_c(&x_trial);
209 let viol_trial = l1_violation(&x_trial, &c_trial, bl, bu, xl, xu);
210 let phi_trial = f_trial + nu * viol_trial;
211 last_f = f_trial;
212 last_c.clone_from(&c_trial);
213
214 let target = phi_curr + eta * alpha * predicted;
215 // Standard Armijo sufficient-decrease (Nocedal-Wright
216 // §3.1). The earlier `|| phi_trial < phi_curr` fallback
217 // (PR #50 review C3) accepted *any* descent and
218 // effectively bypassed the inequality on nonconvex
219 // problems where `predicted ≥ 0` makes the Armijo target
220 // monotone-non-decreasing. We now gate the fallback on
221 // `predicted >= 0` only — i.e. fall back to "any merit
222 // decrease wins" only when the predicted derivative is
223 // not a descent direction, which is the case the original
224 // fallback was intended to cover (cf. Wächter-Biegler 2006
225 // §3.3 backtracking rule).
226 let armijo_ok = if predicted < 0.0 {
227 phi_trial <= target
228 } else {
229 phi_trial < phi_curr
230 };
231 #[cfg(test)]
232 if opts.print_level >= 2 {
233 tracing::debug!(target: "pounce::sqp",
234 " ls trial α={alpha:.3e} phi_t={phi_trial:.4e} \
235 phi_c={phi_curr:.4e} target={target:.4e} pred={predicted:.3e} \
236 grad_p={grad_p:.3e} viol_c={viol_curr:.3e} viol_t={viol_trial:.3e} \
237 ok={armijo_ok}"
238 );
239 }
240 if armijo_ok {
241 return LineSearchResult {
242 alpha,
243 nu,
244 x_new: x_trial,
245 f_new: f_trial,
246 c_new: c_trial,
247 success: true,
248 soc_duals: None,
249 };
250 }
251
252 // Second-order correction (Maratos remedy). Attempted once,
253 // on the full step (α = 1), and only when that step made the
254 // constraint violation worse — otherwise a smaller α already
255 // makes ordinary progress and no correction is needed. The
256 // corrected full step `x + p_soc` is tested against the same
257 // Armijo condition; on rejection we discard it and fall back
258 // to plain backtracking on the original direction `p`.
259 if first_trial {
260 first_trial = false;
261 if viol_trial > viol_curr {
262 if let Some(soc_fn) = soc.as_deref_mut() {
263 if let Some(step) = soc_fn(&c_trial) {
264 // Reject an overshooting "correction" (see
265 // [`SOC_MAX_STEP_GROWTH`]) before spending an
266 // evaluation on it.
267 if inf_norm(&step.p) <= SOC_MAX_STEP_GROWTH * inf_norm(p) {
268 let mut x_soc = vec![0.0; x.len()];
269 for (xt, (&xi, &pi)) in
270 x_soc.iter_mut().zip(x.iter().zip(step.p.iter()))
271 {
272 *xt = xi + pi;
273 }
274 let f_soc = nlp.eval_f(&x_soc);
275 let c_soc = nlp.eval_c(&x_soc);
276 let viol_soc = l1_violation(&x_soc, &c_soc, bl, bu, xl, xu);
277 let phi_soc = f_soc + nu * viol_soc;
278 // Same Armijo gate as above, at α = 1.
279 let armijo_soc = if predicted < 0.0 {
280 phi_soc <= phi_curr + eta * predicted
281 } else {
282 phi_soc < phi_curr
283 };
284 // Feasibility safeguard (Wächter-Biegler
285 // 2006 §3.3): only take the correction if
286 // it genuinely reduces the constraint
287 // violation relative to the uncorrected
288 // full step — otherwise it is not a
289 // second-order *correction* and we fall
290 // back to ordinary backtracking on `p`.
291 let soc_ok = armijo_soc && viol_soc <= KAPPA_SOC * viol_trial;
292 if soc_ok {
293 return LineSearchResult {
294 alpha: 1.0,
295 nu,
296 x_new: x_soc,
297 f_new: f_soc,
298 c_new: c_soc,
299 success: true,
300 soc_duals: Some((step.lambda_g, step.lambda_x)),
301 };
302 }
303 }
304 }
305 }
306 }
307 }
308
309 alpha *= opts.bt_reduction;
310 }
311
312 LineSearchResult {
313 alpha,
314 nu,
315 x_new: x_trial,
316 f_new: last_f,
317 c_new: last_c,
318 success: false,
319 soc_duals: None,
320 }
321}