Skip to main content

Module optimizers

Module optimizers 

Source
Expand description

§Optimization Algorithms for Implicit ODE Solvers

This module provides nonlinear optimization algorithms that are required by implicit ODE solvers in the mini-ode library. Implicit methods (such as Solver::ImplicitEuler and Solver::GLRK4) need to solve nonlinear equations at each timestep, which is accomplished through numerical optimization.

§Design

The optimization API follows a trait-based design that allows extensibility:

  • All optimizers implement the Optimizer trait with a unified optimize() interface
  • Implementations are stateless configuration objects (can be safely shared via Arc)
  • Each optimizer requires only the objective function and initial guess
  • Results are returned as anyhow::Result<Tensor> for consistent error handling

§Available Optimizers

The module exports four gradient-based optimization algorithms:

OptimizerOrderHessianBest For
Newton2ndYesFast convergence, well-conditioned problems
BFGSQuasi-NewtonApproximatedGeneral-purpose, memory-efficient
CG1stNoLarge-scale problems, limited memory
Halley3rdYes (Higher)Very fast convergence

§Usage Example

Creating an optimizer and passing it to an implicit solver:

use mini_ode::optimizers;
use mini_ode::Solver;
use std::sync::Arc;

// Configure Conjugate Gradient optimizer
let optimizer = optimizers::CG::new(50, Some(1e-6), Some(1e-8));

// Use with implicit solver
let solver = Solver::ImplicitEuler {
    step: 0.01,
    optimizer: Arc::new(optimizer),
};

§Implementation Details

  • Automatic differentiation: Optimizers use torch.autograd automatic differentiation to compute gradients and Hessians
  • Line search: Step size is chosen using line search
  • Validation: Output tensors are validated for finite values and proper ranks
  • Warnings: Ill-conditioning and numerical issues produce runtime warnings

§Requirements

These optimizers require the tch crate (libtorch Rust bindings) and depend on:

  • PyTorch tensor operations on CPU or GPU
  • Automatic differentiation support

§Error Handling

Optimizers may return errors for:

  • Input validation failures (non-scalar outputs, rank mismatches)
  • Memory allocation failures (insufficient RAM for Hessian)
  • Convergence failures (max steps reached without meeting tolerances)
  • Numerical issues (NaN/Inf in intermediate calculations)

Structs§

BFGS
Broyden-Fletcher-Goldfarb-Shanno optimization algorithm
CG
Conjugate Gradient optimization algorithm
Halley
Halley optimization algorithm
Newton
Newton optimization algorithm

Traits§

Optimizer
Core trait defining the interface for all optimization algorithms.