19. 非线性约束:SQP 与增广拉格朗日
本章给出约束问题接口、四个入口与乘子约定。理论部分见第 11 章。
19.1 约束约定
问题形式:
拉格朗日函数
Constraints 携带一个点的约束值与行式雅可比:
pub struct Constraints {
pub equalities: Vector, // h(x)
pub inequalities: Vector, // g(x)
pub equality_jacobian: Matrix, // m_e x n
pub inequality_jacobian: Matrix, // m_i x n
}
docs.rs API:Constraints。
构造函数:
| 构造 | 用途 |
|---|---|
Constraints::empty(n) | 无约束,保留 \(0\times n\) 雅可比 |
Constraints::equalities_only(values, jacobian) | 仅等式 |
Constraints::inequalities_only(values, jacobian) | 仅 \(g(x)\ge0\) |
Constraints::linear(x, ae, be, ai, bi) | 线性约束 \(A_ex=b_e\)、\(A_ix\ge b_i\) |
Constraints::from_nonpositive(...) | 把 \(c(x)\le0\) 翻译为 \(g=-c\ge0\) |
约束个数在一次求解中保持不变。
19.2 问题接口
pub trait ConstrainedProblem: Evaluator {
fn constraints(&self, x: &Vector) -> Constraints;
fn lagrangian_hessian(&self, x: &Vector, mu: &Vector, lambda: &Vector)
-> Option<Matrix> { None }
fn try_constraints(&self, x: &Vector) -> Result<Constraints, EvaluationError> { ... }
fn try_lagrangian_hessian(&self, x: &Vector, mu: &Vector, lambda: &Vector)
-> Result<Option<Matrix>, EvaluationError> { ... }
}
docs.rs API:ConstrainedProblem。
- 目标值/梯度来自
Evaluator或Objective适配器; lagrangian_hessian返回 \(L\) 的 Hessian,两个精确 Hessian 方法要求该值;- 约束回调失败经
try_constraints上报为Error::Evaluation { operation: "constraints" }。
19.3 入口与选项
docs.rs API:Method、minimize、augmented_lagrangian、ConstrainedOptions、ConstrainedSolution。
| 入口 | 算法 | 约束类型 |
|---|---|---|
minimize(..., Method::Sqp, ...) | Powell 阻尼 BFGS + \(l_1\) 精确罚函数 | 等式与不等式 |
minimize(..., Method::NewtonLagrange, ...) | KKT 方程牛顿法 | 仅等式 |
minimize(..., Method::LagrangeSqp, ...) | 稳定化 Lagrange Hessian 的 SQP | 仅等式 |
sqp_with_restoration | SQP + 可行性恢复阶段 | 等式与不等式 |
augmented_lagrangian | PHR 乘子/罚迭代 + BFGS 内层 | 等式与不等式 |
ConstrainedOptions 默认值:
| 字段 | 默认 | 说明 |
|---|---|---|
solver | Options::default() | 外层停止与 Armijo 参数 |
feasibility_tolerance | 1e-6 | 可行性与互补性阈值 |
max_inner_iterations | 1000 | PHR 内层预算,其他入口忽略 |
initial_penalty | 2 | PHR 初始罚参数 |
求解签名:
pub fn minimize<P: ConstrainedProblem + ?Sized>(
p: &P, x0: &Vector, method: Method, options: &ConstrainedOptions,
) -> Result<ConstrainedSolution, Error>
pub fn augmented_lagrangian<P: ConstrainedProblem + ?Sized>(
p: &P, x0: &Vector, options: &ConstrainedOptions,
) -> Result<ConstrainedSolution, Error>
SQP 的 \(l_1\) 罚参数取当前值与“最大乘子绝对值加 1”中的较大者;乘子沿接受步长插值:
PHR 则按 \(\mu\leftarrow\mu-\rho h\)、\(\lambda\leftarrow\max(0,\lambda-\rho g)\) 更新乘子。其内层目标为
罚参数在连续外层迭代 \(k\ge2\) 且残差超过上一轮 0.8 倍时加倍。
19.4 带恢复的 SQP
pub fn sqp_with_restoration<P: ConstrainedProblem + ?Sized>(
p: &P, x0: &Vector, options: &SqpOptions,
) -> Result<SqpSolution, SqpError>
docs.rs API:sqp_with_restoration、SqpOptions、SqpSolution、SqpError、RestorationEvent。
线性化 QP 不可行时进入仅约束的恢复阶段:
- 恢复目标 \(\theta(x)=\tfrac12(\lVert h\rVert^2+\lVert\min(g,0)\rVert^2)\);
- 恢复方向来自阻尼最小二乘 QR;
- 接受条件:违度量至少下降
restoration_reduction比例; - 每次尝试记录一个
RestorationEvent。
SqpOptions 默认值:
| 字段 | 默认 |
|---|---|
solver | ConstrainedOptions::default() |
max_subproblem_iterations | 1000(每个 QP 阶段) |
max_subproblem_evaluations | 10000(整个运行) |
max_restoration_iterations | 100(每次尝试) |
restoration_reduction | 0.1 |
restoration_damping | 1e-3 |
返回结构:
pub struct SqpSolution {
pub constrained: ConstrainedSolution, // 嵌套收敛标志
pub restoration: Vec<RestorationEvent>, // 按执行顺序
}
RestorationEvent 字段:iteration、trigger(触发错误)、initial_violation、final_violation、iterations、failure。
失败时返回 SqpError:
| 字段 | 含义 |
|---|---|
source | 求解器错误与来源链 |
restoration | 已发生的恢复尝试 |
last_constrained | 完整外层检查点(含乘子),有效状态出现前为 None |
19.5 结果判读
docs.rs API:ConstrainedSolution。
ConstrainedSolution 字段:
| 字段 | 含义 |
|---|---|
solution | 目标、拉格朗日驻点范数、迭代与状态 |
equality_multipliers | \(\mu\) |
inequality_multipliers | \(\lambda\ge0\),非活动约束取零 |
constraint_violation | 等式残差与不等式负部残差的最大值 |
complementarity | \(\lVert\min(g,\lambda)\rVert\)(逐分量) |
inner_iterations | PHR 内层总迭代,其他入口为零 |
收敛条件同时满足:
其中 \(\tau\) 为 solver.stopping.gradient_tolerance。
19.6 示例
等式约束二次目标,解析解 \((0,1)\):
#![allow(unused)]
fn main() {
use nlopt4r::{Matrix, Objective, Vector};
use nlopt4r::constrained::{ConstrainedProblem, Constraints, SqpOptions, sqp_with_restoration};
struct Model;
impl Objective for Model {
fn value(&self, x: &Vector) -> f64 {
(x[0] - 1.0).powi(2) + (x[1] - 2.0).powi(2)
}
fn gradient(&self, x: &Vector) -> Vector {
Vector::from_vec(vec![2.0 * (x[0] - 1.0), 2.0 * (x[1] - 2.0)])
}
}
impl ConstrainedProblem for Model {
fn constraints(&self, x: &Vector) -> Constraints {
Constraints {
equalities: Vector::from_vec(vec![x[0] + x[1] - 1.0]),
equality_jacobian: Matrix::from_row_slice(1, 2, &[1.0, 1.0]),
inequalities: Vector::zeros(0),
inequality_jacobian: Matrix::zeros(0, 2),
}
}
}
let mut options = SqpOptions::default();
options.solver.solver.stopping.max_evaluations = Some(500);
let result = sqp_with_restoration(&Model, &Vector::zeros(2), &options)?;
assert!(result.constrained.solution.converged());
assert!(result.constrained.constraint_violation < 1e-6);
Ok::<(), Box<dyn std::error::Error>>(())
}
不等式问题:把约束写成 \(g(x)\ge0\),或使用 Constraints::from_nonpositive 翻译 \(c(x)\le0\)。
19.7 计数与预算
- 外层每次状态评估计入
evaluations.objective、gradient、constraints、jacobian各一次; - 约束雅可比与目标梯度分开计数;
- PHR 内层目标调用共享外层严格预算;
- SQP 子问题状态计入
evaluations.subproblem,与外部回调预算分离; - 预算中断返回非收敛报告,回调/子问题失败保留检查点。