Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

16. 局部求解:无约束、有界与矩阵自由方法

本章列出局部光滑求解器的入口、选项默认值与报告字段。理论依据分别见第 3–7 章。

16.1 旧式稠密方法:minimize 与 Method

pub fn minimize<P: Evaluator + ?Sized>(
    problem: &P,
    x0: &Vector<f64>,
    method: Method,
    options: &Options,
) -> Result<Solution, Error>

docs.rs API:minimize、Method、Options、Solution。

Method 的取值:

变体方向/模型内存
SteepestDescent\(d=-g\)\(O(n)\)
NormalizedGradient\(d=-g/\lVert g\rVert\)\(O(n)\)
FletcherReeves共轭梯度,\(n+1\) 步重启\(O(n)\)
DampedNewton稠密牛顿方程 + Armijo\(O(n^2)\)
ModifiedNewton正定平移 Hessian\(O(n^2)\)
Bfgs稠密逆 BFGS\(O(n^2)\)
Dfp稠密逆 DFP\(O(n^2)\)
Sr1对称秩一逆更新\(O(n^2)\)
Broyden { phi }Broyden 凸族,0 为 DFP,1 为 BFGS\(O(n^2)\)

Options 组合两类设置:

pub struct Options {
    pub stopping: StopOptions,          // 默认见第 12 章
    pub line_search: ArmijoOptions,     // 默认见第 3 章
}

全部旧式方法使用 Armijo 回溯;稠密牛顿变体读取 evaluate_hessian,得到 None 时返回显式错误。

调用示例(BFGS):

#![allow(unused)]
fn main() {
use nlopt4r::{minimize, Method, Options, Vector};

let problem = (
    |x: &Vector| (x[0] - 1.0).powi(2) + (x[1] + 1.0).powi(2),
    |x: &Vector| Vector::from_vec(vec![2.0 * (x[0] - 1.0), 2.0 * (x[1] + 1.0)]),
);
let result = minimize(&problem, &Vector::zeros(2), Method::Bfgs, &Options::default())?;
assert!(result.converged());
assert!(result.gradient_norm <= 1e-6);
Ok::<(), nlopt4r::Error>(())
}

16.2 单独调用 Armijo 回溯

pub fn armijo<P: Evaluator + ?Sized>(
    problem: &P,
    x: &Vector<f64>,
    direction: &Vector<f64>,
    options: &ArmijoOptions,
) -> Result<Step, Error>

docs.rs API:armijo、ArmijoOptions、Step。

  • 只在起点请求一次值/梯度,后续试探点只请求值;
  • Step 字段:alpha(接受步长)、value(试探点值)、evaluations(试探次数,起点调用另计);
  • 方向需要满足 \(g^\top d<0\);回溯耗尽返回 Error::LineSearchFailed { evaluations, last_step }。

ArmijoOptions 默认值:

字段默认
initial_step1
contraction0.5
sufficient_decrease0.2
max_iterations60

16.3 有界投影梯度

pub fn projected_gradient<P: Evaluator + ?Sized>(
    problem: &P,
    x0: &Vector,
    bounds: &Bounds,
    options: &Options,
) -> Result<Solution, Error>

docs.rs API:projected_gradient、projected_gradient_norm、Bounds。

性质:

  • 初始点先投影到箱体,全部回调点可行;
  • 方向为 \(d=P(x-g)-x\),回溯检验 \(x+\alpha d\),每个试探点都投影到箱体;
  • Solution::gradient_norm 为梯度映射范数 \(\lVert x-P(x-g)\rVert\),活动边界点取零;
  • 使用 Options::stopping 的梯度容差与 Options::line_search 的 Armijo 参数。

独立计算映射范数:

pub fn projected_gradient_norm(
    x: &Vector,
    gradient: &Vector,
    bounds: &Bounds,
) -> Result<f64, Error>

示例:

#![allow(unused)]
fn main() {
use nlopt4r::{Bounds, Options, Vector, bounded::projected_gradient};

let problem = (
    |x: &Vector| (x[0] - 3.0).powi(2),
    |x: &Vector| Vector::from_vec(vec![2.0 * (x[0] - 3.0)]),
);
let bounds = Bounds::uniform(1, -1.0, 1.0)?;
let result = projected_gradient(&problem, &Vector::zeros(1), &bounds, &Options::default())?;
assert!(result.converged());
assert!((result.x[0] - 1.0).abs() < 1e-6);
Ok::<(), nlopt4r::Error>(())
}

16.4 L-BFGS

pub fn lbfgs<P: Evaluator + ?Sized>(
    problem: &P,
    x0: &Vector,
    options: &LbfgsOptions,
) -> Result<Solution, Error>

docs.rs API:lbfgs、LbfgsOptions。

  • 存储 history_size 组修正对,内存 \(O(m n)\);
  • 外层步长使用强 Wolfe 条件,接受点的值/梯度直接复用;
  • 曲率不满足 \(s^\top y>10^{-10}\lVert s\rVert\lVert y\rVert\) 的修正对被跳过;
  • 方向异常时重置历史并退回最速下降。

LbfgsOptions 默认值:

字段默认
stoppingStopOptions::default()
line_searchStrongWolfeOptions::default()
history_size10
curvature_tolerance1e-10

示例(二维 Rosenbrock):

#![allow(unused)]
fn main() {
use nlopt4r::{local::{lbfgs, LbfgsOptions}, Vector};

let rosenbrock = (
    |x: &Vector| (1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0] * x[0]).powi(2),
    |x: &Vector| Vector::from_vec(vec![
        -2.0 * (1.0 - x[0]) - 400.0 * x[0] * (x[1] - x[0] * x[0]),
        200.0 * (x[1] - x[0] * x[0]),
    ]),
);
let result = lbfgs(
    &rosenbrock,
    &Vector::from_vec(vec![-1.2, 1.0]),
    &LbfgsOptions::default(),
)?;
assert!(result.converged());
Ok::<(), nlopt4r::Error>(())
}

evaluations.objective 与 evaluations.gradient 统计实际回调次数;预算中断返回非收敛报告。

16.5 Newton-CG(矩阵自由)

pub trait HessianVector: Evaluator {
    fn hessian_vector(&self, x: &[f64], v: &[f64], product: &mut [f64]) -> Result<(), Error>;
}
pub fn newton_cg<P: HessianVector + ?Sized>(
    problem: &P,
    x0: &Vector,
    options: &NewtonCgOptions,
) -> Result<Solution, Error>

docs.rs API:HessianVector、newton_cg、NewtonCgOptions。

  • 内层截断 CG 的迭代上限为 min(max_cg_iterations, n);
  • 内层终止条件 \(\lVert r\rVert\le\texttt{cg_tolerance}\cdot\lVert g\rVert\);
  • 出现非正曲率方向时结束内层,外层沿得到的方向做强 Wolfe 搜索;
  • 每次 Hessian 向量乘积计入 evaluations.hessian。

NewtonCgOptions 默认值:

字段默认
stoppingStopOptions::default()
line_searchStrongWolfeOptions::default()
max_cg_iterations100
cg_tolerance0.1

示例:

#![allow(unused)]
fn main() {
use nlopt4r::{Error, Evaluator, Vector, local::{HessianVector, NewtonCgOptions, newton_cg}};

struct Sphere;
impl Evaluator for Sphere {
    fn evaluate(&self, x: &[f64], gradient: Option<&mut [f64]>) -> Result<f64, Error> {
        if let Some(g) = gradient {
            for (out, &v) in g.iter_mut().zip(x) {
                *out = 2.0 * v;
            }
        }
        Ok(x.iter().map(|v| v * v).sum())
    }
}
impl HessianVector for Sphere {
    fn hessian_vector(&self, _: &[f64], v: &[f64], out: &mut [f64]) -> Result<(), Error> {
        for (out, &v) in out.iter_mut().zip(v) {
            *out = 2.0 * v;
        }
        Ok(())
    }
}
let result = newton_cg(&Sphere, &Vector::from_element(10, 1.0), &NewtonCgOptions::default())?;
assert!(result.converged());
Ok::<(), Error>(())
}

16.6 稠密信赖域

pub fn minimize<P: Evaluator + ?Sized>(
    problem: &P,
    x0: &Vector,
    options: &TrustRegionOptions,
) -> Result<Solution, Error>

docs.rs API:trust_region::minimize、subproblem、TrustRegionOptions、TrustRegionStep。

  • 每步用特征分解全局求解二次子问题 \(\min g^\top d+\tfrac12 d^\top B d\),\(\lVert d\rVert\le\Delta\);
  • 比值 \(\rho=\text{实际下降}/\text{模型下降}\) 超过 acceptance_threshold 时接受;
  • 子问题解落在半径边界且比值达到 0.75 时半径加倍,比值低时半径减半。

TrustRegionOptions 默认值:

字段默认
stoppingStopOptions::default()
initial_radius1
maximum_radius2
acceptance_threshold0.15

子问题可单独调用:

#![allow(unused)]
fn main() {
use nlopt4r::{Matrix, Vector, trust_region::subproblem};

let g = Vector::from_vec(vec![2.0, 0.0]);
let step = subproblem(&g, &Matrix::identity(2, 2), 1.0)?;
assert!((step.step.norm() - 1.0).abs() < 1e-8);
Ok::<(), nlopt4r::Error>(())
}

TrustRegionStep 字段:step(位移)、value(模型变化 \(g^\top d+\tfrac12d^\top Bd\))、multiplier(乘子 \(\lambda\ge0\),满足 \((B+\lambda I)d=-g\))。

16.7 Steihaug 矩阵自由信赖域

pub fn steihaug<F>(
    g: &Vector,
    radius: f64,
    options: &SteihaugOptions,
    multiply: F,
) -> Result<SteihaugStep, Error>
where
    F: FnMut(&Vector) -> Result<Vector, Error>

pub fn trust_region_cg<P: HessianVector + ?Sized>(
    problem: &P,
    x0: &Vector,
    options: &TrustRegionCgOptions,
) -> Result<Solution, Error>

docs.rs API:steihaug、trust_region_cg、SteihaugOptions、TrustRegionCgOptions、SteihaugStep、SteihaugTermination。

SteihaugStep 字段:step、value、iterations、hessian_products、termination: SteihaugTermination。内层终止原因:

变体条件
ResidualTolerance残差范数达到起点范数的 relative_tolerance 倍
Boundary正曲率 CG 到达信赖域边界
NegativeCurvature非正曲率方向走到边界上的较优点
MaxIterations内层迭代上限

SteihaugOptions 默认:max_iterations = 100,relative_tolerance = 0.1。 TrustRegionCgOptions 默认:起始半径 1、最大半径 100、接受阈值 0.1,子问题取 SteihaugOptions::default()。

16.8 强 Wolfe 工具

pub fn strong_wolfe<F>(
    value: f64,
    gradient: &Vector,
    direction: &Vector,
    options: &StrongWolfeOptions,
    evaluate: F,
) -> Result<StrongWolfeStep, Error>
where
    F: FnMut(f64) -> Result<(f64, Vector), Error>

docs.rs API:strong_wolfe、StrongWolfeOptions、StrongWolfeStep。

回调按步长返回试探点的值与梯度;接受状态 StrongWolfeStep 含 alpha、value、gradient,可直接复用。StrongWolfeOptions 默认:sufficient_decrease = 1e-4、curvature = 0.9、initial_step = 1、maximum_step = 1e20、max_evaluations = 80。

16.9 一元搜索

pub fn minimize_scalar<F: Fn(f64) -> f64>(
    f: F,
    bounds: (f64, f64),
    tolerance: f64,
    max_iterations: usize,
    method: ScalarMethod,
) -> Result<ScalarSolution, Error>

pub fn minimize_scalar_with_options<F: Fn(f64) -> f64>(
    f: F,
    bounds: (f64, f64),
    method: ScalarMethod,
    options: &ScalarOptions,
) -> Result<ScalarSolution, Error>

docs.rs API:minimize_scalar、minimize_scalar_with_options、ScalarMethod、ScalarOptions、ScalarSolution。

  • minimize_scalar 为便捷入口:单一绝对区间容差与迭代上限,相对容差固定为 \(\sqrt{\varepsilon_{\text{mach}}}\);
  • minimize_scalar_with_options 接受完整的 ScalarOptions 与严格目标预算;
  • ScalarMethod::GoldenSection 与 ScalarMethod::Parabolic(Brent 保护插值);
  • 区间默认容差:绝对 \(10^{-8}\),相对 \(\sqrt{\varepsilon_{\text{mach}}}\);
  • ScalarSolution 字段:x、value、bracket、iterations、status、termination_reason、evaluations。
#![allow(unused)]
fn main() {
use nlopt4r::line_search::{minimize_scalar_with_options, ScalarMethod, ScalarOptions};

let result = minimize_scalar_with_options(
    |x| (x - 0.4).powi(2),
    (-1.0, 2.0),
    ScalarMethod::Parabolic,
    &ScalarOptions::default(),
)?;
assert!((result.x - 0.4).abs() < 1e-6);
Ok::<(), nlopt4r::Error>(())
}

16.10 选择指引

场景入口
小规模、有解析 HessianMethod::DampedNewton、Method::ModifiedNewton
小规模、仅梯度Method::Bfgs、Method::Dfp、Method::Sr1
中大规模、仅梯度local::lbfgs
大规模、可计算 Hessian-向量乘积local::newton_cg、local::trust_region_cg
变量有上下界bounded::projected_gradient
一维连续单峰line_search::minimize_scalar*
少量变量、目标不可微derivative_free(第 17 章)

理论背景